Page 1 of 2
Code for NxN Matrix? - Solved
Posted: Sun Jul 21, 2019 8:13 pm
by DR White
I am working on an electrical app that needs matrixes to solve for the currents in the circuit. Unfortunately, I have been working to develop an algorithm for matrixes for more than a week, without success. Does anyone have any matrix solving code that they would like to share?
Thanks,
David
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 9:29 pm
by richmond62
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 10:14 pm
by DR White
I was trying to develop a matrix solving algorithm using "expansion coefficient" method.
Similar to format below:
Find det(A) using expansion by minors
.......| 1, 3, 5 |
A = | 0, 5, 1 |
.......| 6, 5, 0 |
Following the above definition, the matrix A can be expanded to minor matrices:
..................|5, 1 |............|0, 1 |...........|0, 5 | ( I added the dots for spacing of brackets)
det(𝐴)=(1)x|5, 0 | + (-3)x|6, 0 | + (5)x|6, 5|
= 1×(−5) − 3×(−6) + 5×(−30)
= −137
Re: Code for NxN Matrix?
Posted: Sun Jul 21, 2019 10:35 pm
by [-hh]
What do you want? A matrix cannot be "solved".
Probably you wish to
solve a system of n linear equations with n variables.
There is no need to do that with determinants.
Here is a simple algorithm I wrote for image processing that does it. The input is
-- the coefficients matrix of n rows of which each contains n items of numbers (as array of arrays)
-- the target vector of n rows of which each contains one number (as array)
If the system is solvable, then the solution for the n variables is given as array.
Code: Select all
function gaussCF M,b,n -- CFG did that without computer ...
repeat with i=1 to n
put i into k
repeat with j=1+i to n
if abs(M[j][i]) > abs(M[k][i]) then put j into k
end repeat
if k <> i then
put M[i] into tmp; put M[k] into M[i]; put tmp into M[k]
put b[i] into tmp; put b[k] into b[i]; put tmp into b[k]
end if
repeat with j=1+i to n
put M[j][i]/M[i][i] into t
repeat with k=1+i to n
subtract t*M[i][k] from M[j][k]
end repeat
subtract t*b[i] from b[j]
end repeat
end repeat
repeat with i=n down to 1
repeat with j=1+i to n
subtract M[i][j]*b[j] from b[i]
end repeat
divide b[i] by M[i][i]
end repeat
return b
end gaussCF
A typical usage is
Code: Select all
on mouseUp
put fld "Matrix" into mat -- n rows, each has n cols=items
put fld "Target" into tgt -- n rows
put the num of lines of mat into n
repeat with i=1 to n
put line i of mat into t
split t by comma; put t into M[i]
put line i of tgt into b[i]
end repeat
put gaussCF(M,b,n) into x
--> changed for correct components order, see post below <--
repeat with i=1 to n
put x[i] into line i of x0
end repeat
put x0 into fld "solution" -- n rows if solvable
end mouseUp
In order to compute the inverse matrix (if it exists) of the n x n coefficients matrix compute the n solutions with the n unit vectors of the standard basis as targets:
Code: Select all
-- computes (if existent) the inverse of a n by n matrix
on mouseUp
put fld "Matrix" into mat -- n rows, each has n cols=items
put the num of lines of mat into n
repeat with i=1 to n
put line i of mat into t
split t by comma; put t into M[i]
put 0 into b[i]
end repeat
repeat with k=1 to n
put b into c
put 1 into c[k]
put gaussCF(M,c,n) into x[k]
end repeat
--set numberformat to "0.###"
repeat with i=1 to n
repeat with k=1 to n
put x[k][i] into item k of line i of mout
end repeat
end repeat
put mout into fld "inverse" -- n rows of which each has n cols=items
end mouseUp
[Edit. Removed a superfluous line in the last code snippet]
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 11:10 pm
by DR White
-hh,
What goes in the "Target" fld?
Thanks,
David
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 11:16 pm
by [-hh]
What goes in the "Target" fld?
The components of the vector b of the matrix equation Ax=b.
A is the the matrix of coefficients (we call it M above), x is the solution vector.
https://en.wikipedia.org/wiki/System_of ... _equations
You are looking for a vector x such that the matrix equation Ax yields the vector b (so b is often called "target vector").
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 11:26 pm
by DR White
I Read your response.
I read the link.
I still don't know what to put in the target field.
I would like to solve the following equations with your code:
2X + 2Y + 2Z = 0
3X + 4Y + 2Z = 0
2X + 4Y + 3Z = 0
For the above 3x3 matrix, how would the "Matrix" and "Target" fields be filled?
Thanks,
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 11:37 pm
by [-hh]
The coefficients matrix M is
2,2,2
3,4,2
2,4,3
The target vector is (the components right of the equations)
0
0
0
The solution is (of course)
0
0
0
That is x=0, y=0, z=0
For example for the target vector
3
3
3
we get the solution
1
-0.5
1
that is x=1, y=-0.5, z=1.
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 11:45 pm
by DR White
-hh,
I apologize for the poor job I have done communicating my request.
I hate to impose on 4 1/2 minutes of your time, but if you could watch the attached link,
you would better understand what I am trying to do.
https://www.youtube.com/watch?v=2naaCxfbq_M
Thanks,
David
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 11:51 pm
by DR White
In the video, they solved the Matrix by "inspection". You can see that after the determinant is found, then I can substitute voltage values to find currents. The "inspection" method does not work on larger Matrix. That's why I was asking about code for solving larger matrix, like 6x6.
Thanks for helping,
David
Re: Code for 6x6 Matrix?
Posted: Sun Jul 21, 2019 11:59 pm
by [-hh]
[The video example has n=3:
The matrix field is
6,-2,-4
-2,10,-8
-4,-8,18
The target field is
16
-40
0
The solution computes to
-2.571429
-7.714286
-4
that is i1=-2.571429, i2=-7.714286, i3=-4.]
The "inspection" method/determinant method of the video is also possible for larger n. But it is generally slower than the above method and thus mainly used for theoretical considerations (e.g. model building in economics).
The algorithm above is ready for any (solvable) n by n system. It becomes slow for "very large" n only. Should solve your 6 by 6 systems in a few millisecs...
Re: Code for 6x6 Matrix?
Posted: Mon Jul 22, 2019 12:46 am
by DR White
-hh,
You are AWESOME!!!!!!!!!!!!
You are AWESOME!!!!!!!!!!!!
That algorithm works GREAT!
THANK YOU!
THANK YOU!
THANK YOU!
Forever grateful,
David
Re: Code for 6x6 Matrix? - Solved
Posted: Mon Jul 22, 2019 5:01 pm
by dunbarx
David appears to be somewhat impressed by Hermann.
Craig
Re: Code for 6x6 Matrix? - Solved
Posted: Mon Jul 22, 2019 6:50 pm
by FourthWorld
dunbarx wrote: ↑Mon Jul 22, 2019 5:01 pm
David appears to be somewhat impressed by Hermann.
Most of us are. Hermann delivers good work.
Re: Code for 6x6 Matrix? - Solved
Posted: Mon Jul 22, 2019 8:23 pm
by SparkOut
Anyone who isn't impressed with Hermann and his work is just pretending. DR White is super impressed, and rightly so. I am always incredibly impressed.
(I didn't even understand the original question, even in my native language. )
Vielen Dank wieder, Hermann!