This project is read-only.

The following simple example was created to test the speed and accuracy of StarMath's matrix inversion capabilities. It is included within the “tester.exe” project in the source code. 

            const int size = 500;

            var now = DateTime.Now;

            var r = new Random();

            var A = new double[size,size];

            for (var i = 0; i < size; i++)

                for (var j = 0; j < size; j++)

                    A[i, j] = (200*r.NextDouble()) - 100.0;

            Console.WriteLine("start invert check for matrix of size: " + size);

            var B = A.inverse();  // or  StarMath.inverse(A);

            var C = A.multiply(B).subtract(StarMath.makeIdentity(size));

            var error = C.norm2();

            var interval = DateTime.Now - now;

            Console.WriteLine("end invert, error = " + error);

            Console.WriteLine("time = " + interval);

 

The following shows an explanation for these steps.

            const int size = 500;

            var now = DateTime.Now;

            var r = new Random();

First, a matrix of 500 by 500 elements is created with values between –100 and 100.

            var A = new double[size,size];

            for (var i = 0; i < size; i++)

                for (var j = 0; j < size; j++)

                    A[i, j] = (200*r.NextDouble()) - 100.0;

            Console.WriteLine("start invert check for matrix of size: " + size);

B is assigned to the inverse of A (this is the most time consuming step, of course). Note, in StarMath, most of the functions modify the original values – A is the same after this function call.

            var B = StarMath.inverse(A); or with extensions, this can be made more compact:

            var B = A.inverse();

In order to test the accuracy of the inversion, A and B are multiply (A*A^-1), which should yield the identity matrix. So, the identity matrix is subtracted to produce a matrix, C, which would ideally be all zeros.

            var C = StarMath.subtract(StarMath.multiply(A, B), StarMath.makeIdentity(size); or with extensions, this can be compact:

            var C = A.multiply(B).subtract(StarMath.makeIdentity(size));

By taking the norm of matrix C, we can arrive at the root-mean-square error. Note, the norm2  and normP functions take an optional second argument (not shown), which is a boolean (the default of which is false) called dontDoPRoot. When this is true, the norm function skips the step the sqaure-root (or p-root) is taken.

            var error = StarMath.norm2(C); or with extensions, this can be :

            var error = C.norm2();

 

            var interval = DateTime.Now - now;

            Console.WriteLine("end invert, error = " + error);

            Console.WriteLine("time = " + interval);

 

Last edited Nov 15, 2014 at 3:13 AM by mattica, version 2

Comments

No comments yet.