-2

I wanted to simplify my program a little bit and I am testing math.net, so for example I got matrix 2x2,

det(A) = a * d - b * c = 71 * 137 - 130 * 107 = -4183

Can sb tell me what is going on here, on the second screenshot, you can see that Math.Net Determinant() Function returns -4183,00000000000000018. How is it correct for the given matrix? Where this result came from? If it is double, it should be -4183.0.

Is it some kind of algorithm which count "well enough" but "much faster" for large data?

Screenshot 1

Screenshot 2

Second question, out of curiosity, what would be the quickest way to invert matrix modulo 256, using EXACTLY this method:

A^(-1) = 1/det(A) * (A^D)^T

where by (A^D)^T I mean transposed matrix of cofactors (I believe how it is called in English)

I wrote a method doing that, which works for Matrices or Multidimensional arrays, but I am curious what is the proper way of doing it in math.net, but using the equation I mentioned.

And as always, I truly appreciate every answer guys.

(btw yes, I am aware that I am doing to many casts, vars are declared many times, but try to ignore that, this is simply a testing field)

To make my 1st question more clear (You can click '-' all you want, I don't care :))

@Szab Thank you for the answer, I know that there is such behavior for decimal numbers, but to be more precise: I would like to know, why the result - 4183,00000000000000018 is different than: This Result There are no decimal places here, C# show very clearly that

det(A) = a * d - b * c = 71.0 * 137.0 - 130.0 * 107.0 = -4183.0

for a, b, c, d and det being all doubles.

/edit

Question answered, thank you all very much.

rvnlord
  • 3,487
  • 3
  • 23
  • 32
  • I don't understand the first part of the question. The calculation you provided is correct to my understanding. – Codor Mar 07 '15 at 20:08
  • On the second screenshot its 4183,00000000000000018. – rvnlord Mar 07 '15 at 20:10
  • Do you mean - 4183,00000000000000018 instead of 4183,00000000000000018? The behaviour you observe is apparently an accuracy issue; `double` is not an exact data type. – Codor Mar 07 '15 at 20:14
  • Determinant() Function of Math.Net returns - 4183,00000000000000018, it should return EXACTLY -4183, where that wrong result came from? How - 4183,00000000000000018 can EVER be correct for the given 2x2 Matrix? – rvnlord Mar 07 '15 at 20:16
  • You might want to read the question ([Is floating point math broken?](http://stackoverflow.com/q/588004/1364007)) on StackOverflow. – Wai Ha Lee Mar 07 '15 at 20:22
  • A recommendation for you - since your checking if the matrix is invertible in a certain ring (Z256 in your case) you're most definitely operating in the world of integers, so you should use integer based type (like `int` or `long`) as the generic parameter for the matrix. – Grx70 Mar 07 '15 at 20:31
  • Thank, you I am aware of this behavior but look here: Why is this exact http://i.stack.imgur.com/6aOfs.png? There are no decimal places when counting det of my matrix. @Grx70 I am testing with doubles (some operations are not possible in math.net while I got Matrix. Still Multiplying and substracting numbers WITH NO decimal places should return EXACT values. – rvnlord Mar 07 '15 at 20:43

1 Answers1

2

I think the first problem occurs just because the algorithm you are using is working with values of type double. As you may know, every number is represented in computer's memory as a binary value. The problem with this representation is that not every decimal value may be represented as binary number with 100% accuracy (just like you can't represent 1/3 with 100% accuracy). It's not the algorithm's fault.

Another example of this behaviour:

double a = 86.24;
double b = 86.25;
double c = b - a; // Should be 0.01, but is equal to 0.010000000000005116
Szab
  • 1,263
  • 8
  • 18
  • Thank you for the answer, I know that there is such behavior for decimal numbers, but to be more precise, I would like to know why the result - 4183,00000000000000018 is different than this: http://i.stack.imgur.com/6aOfs.png – rvnlord Mar 07 '15 at 20:38
  • 1
    @user3783852 Most likely because the determinant is not computed basing on the definition, but some other method is used (like Laplace expansion) which may introduce extra floating point arithmetic (in simple cases like the one you mentioned), but is far more scalable. – Grx70 Mar 07 '15 at 20:43
  • Your formula for 2x2 matrix determinant is pretty simple, but as you probably know, for bigger matrices such formulas don't exist so some kind of algorithm must be applied. Algorithms like this perform series of complex calculations to give you the result (the algorithm must apply to every matrix so it always calculates determinant the same way, no matter what size it is) and the inaccuracy is probably the product of one of those calculations. – Szab Mar 07 '15 at 20:48
  • Oh thanks, that is the answer, I was pretty sure of two things and I was wrong about them: 1. I thought, that math.net checks what is the most efficiant way of finding det of 2x2 matrix. 2. I assumed that this simple method IS actually the most efficiant way of doing so. And yes I am well aware of the methods that can be applied to finding det of NxN matrices. Thank All of you. – rvnlord Mar 07 '15 at 20:55
  • @user3783852 You can always write an extension method that uses the most efficient way by your standards - for instance, that uses the definition based method for matrices of size 2x2 or 3x3 and falls back to the Math.NET implementation for larger ones. – Grx70 Mar 07 '15 at 20:58