Code for NxN Matrix? - Solved
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Code for NxN Matrix? - Solved
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
Thanks,
David
Last edited by DR White on Wed Jul 24, 2019 4:29 pm, edited 4 times in total.
-
- Livecode Opensource Backer
- Posts: 9359
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Code for 6x6 Matrix?
What do you mean by "solve" ?
http://mathforum.org/library/drmath/view/55482.html
https://www.khanacademy.org/math/linear ... eterminant
http://chemesoftstore.com/PMatrixMath.htm
https://www.chegg.com/homework-help/que ... g-q5949277
If you know how to solve a 6x6 matrix on paper, then all you need to do is express the operations you go through in a way LiveCode understands.
http://mathforum.org/library/drmath/view/55482.html
https://www.khanacademy.org/math/linear ... eterminant
http://chemesoftstore.com/PMatrixMath.htm
https://www.chegg.com/homework-help/que ... g-q5949277
If you know how to solve a 6x6 matrix on paper, then all you need to do is express the operations you go through in a way LiveCode understands.
Re: Code for 6x6 Matrix?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Code for NxN Matrix?
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.
A typical usage is
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:
[Edit. Removed a superfluous line in the last code snippet]
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
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
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
Last edited by [-hh] on Fri Jan 24, 2020 11:41 am, edited 3 times in total.
shiftLock happens
Re: Code for 6x6 Matrix?
-hh,
What goes in the "Target" fld?
Thanks,
David
What goes in the "Target" fld?
Thanks,
David
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Code for 6x6 Matrix?
The components of the vector b of the matrix equation Ax=b.What goes in the "Target" fld?
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").
shiftLock happens
Re: Code for 6x6 Matrix?
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,
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,
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Code for 6x6 Matrix?
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.
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.
shiftLock happens
Re: Code for 6x6 Matrix?
-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
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?
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
Thanks for helping,
David
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Code for 6x6 Matrix?
[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...
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...
shiftLock happens
Re: Code for 6x6 Matrix?
-hh,
You are AWESOME!!!!!!!!!!!!
You are AWESOME!!!!!!!!!!!!
That algorithm works GREAT!
THANK YOU!
THANK YOU!
THANK YOU!
Forever grateful,
David
You are AWESOME!!!!!!!!!!!!
You are AWESOME!!!!!!!!!!!!
That algorithm works GREAT!
THANK YOU!
THANK YOU!
THANK YOU!
Forever grateful,
David
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Code for 6x6 Matrix? - Solved
David appears to be somewhat impressed by Hermann.
Craig
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9825
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Code for 6x6 Matrix? - Solved
Most of us are. Hermann delivers good work.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Code for 6x6 Matrix? - Solved
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!
(I didn't even understand the original question, even in my native language. )
Vielen Dank wieder, Hermann!