Skip to content

Commit

Permalink
Added isEye and isZero to the Linear Algebra module. (#22695)
Browse files Browse the repository at this point in the history
Relevant issue:
#22682
It was pointed out that we should have `isZero` and `isEye` methods in
the Linear Algebra module. This PR adds these.
Added two `isEye` and `isZero` to the linear algebra module. 

- [ ] Paratest
[reviewed by @jeremiah-corrado]
  • Loading branch information
Iainmon authored Jul 12, 2023
2 parents 74cb382 + a1cfda4 commit cafd659
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
45 changes: 45 additions & 0 deletions modules/packages/LinearAlgebra.chpl
Original file line number Diff line number Diff line change
Expand Up @@ -1450,6 +1450,42 @@ private proc _isDiag(A: [?D] ?eltType) {
return true;
}

/* Return `true` if matrix is the additive identity (zero matrix). */
proc isZero(A: [?D] ?eltType) where isDenseMatrix(A) {
return _isZero(A);
}

private proc _isZero(A: [?D] ?eltType) {
if D.rank != 2 then
compilerError("Rank is not 2");

for (i, j) in D {
if A[i, j] != 0 then return false;
}
return true;
}

private proc _isEye(A: [?D] ?eltType) {
if D.rank != 2 then
compilerError("Rank is not 2");

if !isSquare(A) then return false;

for (i, j) in D {
if i == j {
if A[i, j] != 1 then return false;
} else {
if A[i, j] != 0 then return false;
}
}

return true;
}

/* Return `true` if matrix is the multiplicative identity (identity matrix). */
proc isEye(A: [?D] ?eltType) where isDenseMatrix(A) {
return _isEye(A);
}

/* Return `true` if matrix is Hermitian */
proc isHermitian(A: [?D]) where isDenseMatrix(A) {
Expand Down Expand Up @@ -3400,6 +3436,15 @@ module Sparse {
return _isDiag(A);
}

/* Return `true` if sparse matrix is the additive identity (zero matrix). */
proc isZero(A: [?D] ?eltType) where A.isSparse() {
return _isZero(A);
}

/* Return `true` if sparse matrix is the multiplicative identity (identity matrix). */
proc isEye(A: [?D] ?eltType) where A.isSparse() {
return _isEye(A);
}

/* Return ``true`` if matrix is Hermitian. Supports CSR and COO arrays. */
proc isHermitian(A: [?D]) where A.isSparse() {
Expand Down
10 changes: 10 additions & 0 deletions test/library/packages/LinearAlgebra/correctness/testIdentity.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use LinearAlgebra;


config const n = 100;

var A = eye(n);
writeln(isEye(A));

var B = Matrix([0,0,0], [0,0,0], eltType=real);
writeln(isEye(B));
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
true
false
13 changes: 13 additions & 0 deletions test/library/packages/LinearAlgebra/correctness/testZero.chpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use LinearAlgebra;


config const n = 100;
config const m = 200;

var A = Matrix(n,m,eltType=int(64));

writeln(isZero(A));

var B = eye(n);
writeln(isZero(B));

2 changes: 2 additions & 0 deletions test/library/packages/LinearAlgebra/correctness/testZero.good
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
true
false

0 comments on commit cafd659

Please sign in to comment.