Author Topic: Matrices and Determinants  (Read 408 times)

darkownt

• PixInsight Enthusiast
• Posts: 85
Matrices and Determinants
« on: 2012 August 22 21:44:10 »
Hello All:

Currently I am using Excel to solve 30 unknowns of a set of 30 linear equations with a 30x30 matrix.

Ideally I would have a script in PJSR do this (it is PixInsight processing related).

Are there matrix solution or determinant calculating tools standard in PJSR?  If not can I find a library to "add" this capabillity to it?

Since my matrix is 30x30 I really need a det[] function that takes an array of any arbitrary size.

cheers
darkownt

mschuster

• PixInsight Old Hand
• Posts: 325
Re: Matrices and Determinants
« Reply #1 on: 2012 August 23 00:59:19 »
PJSR lacks access to some of PI's useful functionality. As a work around check out Numerical Recipes, 3rd Edition by Press. The LUdcmp functions are what you need, they are short and easy to implement. I have used this code multiple times, it works well.

Regards,
Mike

georg.viehoever

• PixInsight Jedi Knight
• Posts: 1533
Re: Matrices and Determinants
« Reply #2 on: 2012 August 23 01:57:38 »
PCL (the C++ base of PJSR has a matrix class, but it does not have a det() implementation. Even  the inverse() implementation is incomplete http://pixinsight.com/developer/pcl/doc/html/classpcl_1_1GenericMatrix.html#a560a084f75d4ca53cc1e17e95dec3bc2 , so I doubt that there is a det() function in PJSR. Maybe PJSR gives you access to the Gauss-Seidel implementation in PCL.

Georg
Georg (6 inch Newton, unmodified Canon EOS40D, unguided EQ5 mount)

Juan Conejero

• PTeam Member
• PixInsight Jedi Grand Master
• Posts: 3878
Re: Matrices and Determinants
« Reply #3 on: 2012 August 23 09:30:17 »
Hi,

PJSR implements Gauss-Jordan elimination, which can be used for matrix inversion and to solve systems of linear equations. As Mike has pointed out, decomposition methods are more efficient to solve linear systems. However, Gauss-Jordan is stable and rock-solid and, if this is not a really critical part of your implementation, and/or if the problem that you are trying to solve isn't very large, it does the job in a straightforward way. Also, Gauss-Jordan is the method of choice if you need both the inverse matrix and the solution.

Example snippet for matrix inversion:

Code: [Select]
`function printMatrix( M, name ){   if ( name )      console.writeln( name, ':' );   for ( var i = 0; i < M.rows; ++i )   {      for ( var j = 0; j < M.cols; ++j )         console.write( format( "%12g", M.at( i, j ) ) );      console.writeln();   }}var A = new Matrix( 1, 2, 3,                    0, 1, 4,                    5, 6, 0 );var Ai = A.inverse();printMatrix( A, "A" );printMatrix( Ai, "Ai" );`
Example to solve a linear system:

Code: [Select]
`/* Solve: * *   x +   y +   z = 5 * 2*x + 3*y + 5*z = 8 * 4*x       + 5*z = 2 */ var X = new Matrix( 1, 1, 1,                    2, 3, 5,                    4, 0, 5 );var Y = new Matrix( 0, 0, 5,                    0, 0, 8,                    0, 0, 2 );var R = Math.solve( X, Y );// R[0] is the inverse of matrix X// Last column of R[1] is the solution vectorprintMatrix( R[1], "R" ); // x=3 y=4 z=-2`
There is also a Math.svd() method that computes the singular value decomposition of a matrix. I can also put an example of this routine, if necessary.

The next versions of PCL and PJSR extend the Vector and Matrix classes and objects considerably. There is no determinant calculation function for Matrix because we don't need it for image processing purposes (we only need to know if the determinant exists, actually), but we can implement one if required.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

darkownt

• PixInsight Enthusiast
• Posts: 85
Re: Matrices and Determinants
« Reply #4 on: 2012 August 23 16:46:54 »
Thanks Juan.

As long as 30x30 matrices can be used looks like Math.solve is just whatl I need.

thanks!