-
Notifications
You must be signed in to change notification settings - Fork 4
Fixing sym
method
#196
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
base: main
Are you sure you want to change the base?
Fixing sym
method
#196
Conversation
Thanks for opening a Pull Request. If you want to perform a review write a comment saying: @ansys-reviewer-bot review |
@ansys-reviewer-bot review |
Okay, I will trigger a review of your PR. |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #196 +/- ##
==========================================
+ Coverage 92.66% 92.84% +0.18%
==========================================
Files 2 2
Lines 586 587 +1
==========================================
+ Hits 543 545 +2
+ Misses 43 42 -1 |
@ansys-reviewer-bot review |
Okay, I will trigger a review of your PR. |
@ansys-reviewer-bot review |
Okay, I will trigger a review of your PR. |
src/ansys/math/core/math.py
Outdated
if sym is None: | ||
if linalg.issymmetric(arr): | ||
sym = True | ||
else: | ||
sym = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issymmetric
function from linalg
expects a NumPy array, not a sparse matrix. The condition might not work as intended with a sparse matrix. You can convert the sparse matrix to a dense one using arr.toarray()
and then use issymmetric
or implement a custom approach to check symmetry on the sparse matrix.
src/ansys/math/core/math.py
Outdated
if mapdl_version < 23.2: # pragma: no cover | ||
raise VersionError("``kron`` requires MAPDL version 2023R2") | ||
|
||
if not isinstance(obj, AnsMath): | ||
if not isinstance(obj, AnsMathObj): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve the error message, you could include the object type in the description:
if mapdl_version < 23.2: # pragma: no cover | |
raise VersionError("``kron`` requires MAPDL version 2023R2") | |
if not isinstance(obj, AnsMath): | |
if not isinstance(obj, AnsMathObj): | |
if not isinstance(obj, AnsMathObj): | |
raise TypeError(f"Must be an AnsMath object, but got {type(obj)}.") |
@@ -1,7 +1,9 @@ | |||
import os |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, the patch looks good. A few suggestions and comments will be provided below.
|
||
@pytest.fixture | ||
def sparse_asym_mat(): | ||
return sparse.random(5000, 5000, density=0.05, format="csr") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider setting a random seed for reproducibility before generating the sparse random matrix.
@@ -182,19 +182,35 @@ def test_shape(mm): | |||
assert m1.shape == shape | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace the current assert message with a more informative one:
assert sparse_mat.data.nbytes // 1024**2 > 4, f"Matrix size is not over gRPC message limit (4 MB): {sparse_mat.data.nbytes // 1024**2} MB" |
tests/test_math.py
Outdated
@@ -301,7 +317,7 @@ def test_kron_product_unsupported_dtype(mm): | |||
if mapdl_version < 23.2: | |||
pytest.skip("Requires MAPDL 2023 R2 or later.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace the current assert message with a more informative one:
pytest.skip("Requires MAPDL 2023 R2 or later.") | |
with pytest.raises(TypeError, match=r"Must be an AnsMath object. not a local object"): |
@@ -720,6 +736,22 @@ def test_invalid_sparse_name(mm): | |||
mm.matrix(mat, name=1) | |||
|
|||
|
|||
def test_sym_dmat(mm, dense_sym_mat): | |||
dmat = mm.matrix(dense_sym_mat) | |||
if not server_meets_version(mm._server_version, (0, 5, 0)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice addition of tests for symmetric dense and sparse matrices. It helps check if the server version meets the requirements.
Please make the suggested changes to the assert messages for better readability and submit the patch for review again.
Symmetric matrices are stored with upper triangular ones within MAPDL. storage_matrix = [[x x x x],
[. x x x],
[. . x x],
[. . . x]]
symmetric_matrix = storage_matrix + storage_matrix.T - diag(storage_matrix) |
Closes #195.