Jurman algorithm (morn) Details: matrix operation, morn Matrix
Introduction to the Juran algorithm (morn): matrix operations (1)
In the past two days, some people have questioned the running speed of the Juran algorithm. I have never tested it, so it is normal to be questioned. So I plan to test it later, next, we will introduce the sentence and mans algorithm.
This is the first time that I wrote a blog about the Juran algorithm,The Juran algorithm is a lightweight algorithm library written only..
This article describes the simplest matrix operations in the algorithm library.
Test procedure
First, let's take a look at the test program. This program is no longer simple. it just opens a matrix of n × n and then assigns random values between (0, 1) elements in the matrix. Three basic matrix operations are tested. ① first, calculate the determining factor of the matrix. ② If the determining factor is not 0, calculate the inverse of the matrix; ③ multiply the original matrix by the inverse matrix to verify whether it is equal to the unit matrix.
#include
#include
#include "morn_Math.h"void main(){ int i,j; MORNMatrix *mat; MORNMatrix *inv; MORNMatrix *mul; int size = 8; float det; FILETIME beg,end; printf("use MORN\n"); mat = mornMatrixCreate(size,size,NULL); inv = mornMatrixCreate(size,size,NULL); mul = mornMatrixCreate(size,size,NULL); for(j=0;j
data[j][i] = ((float)rand())/((float)RAND_MAX); printf("\nmat is:\n"); PrintMat(mat); GetSystemTimeAsFileTime(&beg); det = mornMatrixDetValue(mat); GetSystemTimeAsFileTime(&end); printf("\ndet is %f\n",det); printf("mat determinant time use is %d\n",(end.dwLowDateTime-beg.dwLowDateTime)); if(det==0) return; GetSystemTimeAsFileTime(&beg); mornMatrixInverse(mat,inv); GetSystemTimeAsFileTime(&end); printf("\ninv is:\n"); PrintMat(inv); printf("mat invere time use is %d\n",(end.dwLowDateTime-beg.dwLowDateTime)); GetSystemTimeAsFileTime(&beg); mornMatrixMul(mat,inv,mul); GetSystemTimeAsFileTime(&end); printf("\nmul is:\n"); PrintMat(mul); printf("mat mult time use is %d\n",(end.dwLowDateTime-beg.dwLowDateTime)); mornMatrixRelease(mat); mornMatrixRelease(inv); mornMatrixRelease(mul);}
Check the result
Here I tried the 8x8 matrix.
The results do not have anything that can be explained at all, and they fully conform to expectations.
Compare Eigen
Eigen is a high-level C ++ library that effectively supports linear algebra, matrix and vector operations, numerical analysis, and related algorithms (copied from Baidu encyclopedia ), why is it worse than Eigen because it is widely used.
The program of Eigen will not be pasted. Take a look at the screenshot:
We can see that the results of sentences and Eigen are exactly the same.
Performance
The above results also show that for a simple matrix such as 8x8, the period and Eigen are both very fast, so it will not take much time to test.
Now we need to enlarge the matrix of the test. First use a matrix of 200x200 to check the result (the matrix will not be printed)
The gap is very obvious, and the period is much faster (The unit here is 100ns, but this time is related to the computer hardware. The absolute value is meaningless, and the comparison is meaningful), right.
Try the 1000x1000 matrix and check the result:
A larger matrix will not happen, so I have no patience to wait until it returns the result.
The conclusion is obvious that the sentence is more than 10 times faster than Eigen.
When I have time to write a matrix operation for a period of time, let's look at the specific implementation algorithm.