Handling of elements in Symmetric matrices #816
alexkaratarakis
started this conversation in
Ideas
Replies: 1 comment
-
Good point indeed, thanks for raising this. Yes, I agree, preventing writing on the other side with an exception would be safer, without actually having much of an impact. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
When working with symmetric matrices there is a potential risk of misuse because element ij and element ji is the same value. Therefore if a user makes a modification on both triangles of the matrix, the result would not be what the user expects.
For example, user A have a 3x3 SymmetricDenseMatrix:
[0, 0, 0]
[
, 0, 0],~, 0][
Since the matrix is symmetric (s)he proceeds to add 5 to (0,2), so:
[0, 0, 5]
[
, 0, 0],~, 0][
The symmetric matrix is then expected to return 5 in both matrix[2,0] and matrix[0,2], which stands true.
On the other hand, there is a risk of adding the value twice with (for example) the intention to preserve symmetry, If add 5 to both (0,2) and (2,0) the result is:
[0, 0, 10]
[
, 0, 0], ~, 0][
which is not what is expected. The actual cases would involve writing on both sides of the matrix for whatever reason and won't be so clear as the provided example so we have the danger of allowing silent errors which would be hard to debug.
I would like to know your opinion about how you would want to handle this in mathnet.
My opinion, and the handling I currently use, is to disallow writing on the lower triangle of the matrix (throw an exception when attempted). Only writing on the diagonal and upper triangle is allowed, so you are alerted in case of mis-use. This has caught a huge amount of bugs.
Beta Was this translation helpful? Give feedback.
All reactions