-
Notifications
You must be signed in to change notification settings - Fork 2k
Add an overload to IsSymmetric that accepts a tolerance #856
Comments
Not paranoid ;) [Test]
public void IsSymmetricTest2()
{
for (int i = 0; i < 1000; i++)
{
double[,] a = Matrix.Random(2);
double[,] b = Matrix.Random(2);
var result = a.Add(b).Add(a.Transpose()).Add(b.Transpose());
if (!result.IsSymmetric())
throw new NonSymmetricMatrixException();
}
} |
Hi @AlexJCross, Thanks for opening the issue 😄 No, you are not paranoid. It is quite common for almost symmetric - but not quite symmetric - matrices to exist. However, I would suggest using the same API as the IsEquals method, using "atol" and "rtol" parameters. The reason for that is that this is similar to the API used in sklearn and scipy and that therefore some users might be more used to. However, unless you need such a method for the other issues you have been working on, maybe you could leave this one as a future task. I can also try to take care of this issue - but if you think you would like to give you a try, please feel free to do so! Regards, |
I like it (never knew the IsEquals can compare relative!): public static double IsSymmetric(this double[,] a, double atol = 0, double rtol = 0) Ok, well I need it for an issue so I'll check in something but it might be an internal static for now. If you get time to industrialise it before me, by all means do. Thanks v much, |
It wasn't too hard to adapt it from the existing IsEqual implementations, I might be able to commit it in the following minutes. Tests are still running though. |
Thanks! |
Added in 3.8.0. |
Currently
IsSymmetric()
is implemented as a generic method onIComparable
. It would be good if it accepted a tolerance. For large symmetric matrices, it feels restrictive if a tiny floating point error caused this to returnfalse
.Will need to be a T4 template job (or just a few special cases like
double
andfloat
) asIComparable
does not allow difference comparisons (AFAIK). I imagine it is faster if it's not generic. Proposed API:so you could say,
M[i,j]
cannot be different toM[j,i]
by more than 0.0001% or maybe 1e-12 etc.Question
@cesarsouza Is this useful or am I being paranoid (I want it for #832)? Can you envisage any scenario where a matrix that should be symmetric is not exactly symmetric because of tiny rounding errors? if you can, assign this to me, if you can't, feel free to close this issue.
Thanks!
Alex
The text was updated successfully, but these errors were encountered: