Code for NxN Matrix? - Solved

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Code for NxN Matrix? - Solved

Post by DR White » Sun Jul 21, 2019 8:13 pm

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
Last edited by DR White on Wed Jul 24, 2019 4:29 pm, edited 4 times in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9359
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Code for 6x6 Matrix?

Post by richmond62 » Sun Jul 21, 2019 9:29 pm

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.

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Code for 6x6 Matrix?

Post by DR White » Sun Jul 21, 2019 10:14 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Code for NxN Matrix?

Post by [-hh] » Sun Jul 21, 2019 10:35 pm

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]
Last edited by [-hh] on Fri Jan 24, 2020 11:41 am, edited 3 times in total.
shiftLock happens

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Code for 6x6 Matrix?

Post by DR White » Sun Jul 21, 2019 11:10 pm

-hh,

What goes in the "Target" fld?

Thanks,

David

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Code for 6x6 Matrix?

Post by [-hh] » Sun Jul 21, 2019 11:16 pm

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").
shiftLock happens

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Code for 6x6 Matrix?

Post by DR White » Sun Jul 21, 2019 11:26 pm

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,

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Code for 6x6 Matrix?

Post by [-hh] » Sun Jul 21, 2019 11:37 pm

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.
shiftLock happens

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Code for 6x6 Matrix?

Post by DR White » Sun Jul 21, 2019 11:45 pm

-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

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Code for 6x6 Matrix?

Post by DR White » Sun Jul 21, 2019 11:51 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Code for 6x6 Matrix?

Post by [-hh] » Sun Jul 21, 2019 11:59 pm

[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...
shiftLock happens

DR White
Posts: 686
Joined: Fri Aug 23, 2013 12:29 pm
Location: Virginia, USA

Re: Code for 6x6 Matrix?

Post by DR White » Mon Jul 22, 2019 12:46 am

-hh,

You are AWESOME!!!!!!!!!!!!

You are AWESOME!!!!!!!!!!!!

That algorithm works GREAT!

THANK YOU!

THANK YOU!

THANK YOU!

Forever grateful,

David

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Code for 6x6 Matrix? - Solved

Post by dunbarx » Mon Jul 22, 2019 5:01 pm

David appears to be somewhat impressed by Hermann.

Craig

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9825
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Code for 6x6 Matrix? - Solved

Post by FourthWorld » Mon Jul 22, 2019 6:50 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Code for 6x6 Matrix? - Solved

Post by SparkOut » Mon Jul 22, 2019 8:23 pm

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!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”