Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added isEye and isZero to the Linear Algebra module. #22695

Merged
merged 4 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have a separate implementation and public proc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the implicit convention that I saw. Such as

/* Return `true` if matrix is diagonal. */
proc isDiag(A: [?D] ?eltType) where isDenseMatrix(A) {
  return _isDiag(A);
}

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

  // Check if any element not along the diagonal is nonzero
  for (i, j) in D {
    if i != j && A[i, j] != 0 then return false;
  }
  return true;
}

where _isDiag is used in two different functions, one for dense matrices and one for sparse matrices.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense. I suspect other procs do it this way so that sparse and dense versions can use the same underlying implementation. I'd keep it this way.

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
jeremiah-corrado marked this conversation as resolved.
Show resolved Hide resolved
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
jeremiah-corrado marked this conversation as resolved.
Show resolved Hide resolved
false