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: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Code for 6x6 Matrix? - Little Problem

Post by DR White » Wed Jul 24, 2019 2:08 am

The matrix routine works great on matrixes 9x9 and smaller, but not on larger ones.
(I know I only asked for 6x6 matrixes, but it was doing so well I thought I would not limit the user to 6x6)

Notice how the last solution of the first matrix became the second solution of the last
matrix.


***** Solutions 1 -5 should be the same for both Matrixes *****

9x9 Works

--put fld "Matrix"

300,-200,0,-100,0,0,0,0,0
-200,500,-100,0,-100,-100,0,0,0
0,-100,100,0,0,0,0,0,0
-100,0,0,200,0,0,-100,0,0
0,-100,0,0,300,-100,0,-100,0
0,-100,0,0,-100,300,-100,0,0
0,0,0,-100,0,-100,400,0,-100
0,0,0,0,-100,0,0,200,0
0,0,0,0,0,0,-100,0,200

--put fld "Target"
100
0
0
0
0
0
0
0
0

--- fld "solution"
1.269624573e+00
9.761092150e-01
9.761092150e-01
8.566552894e-01
6.689419801e-01
6.962457343e-01
4.436860068e-01
3.344709901e-01
2.218430034e-01




10x10 Does not work

--put fld "Matrix"
300,-200,0,-100,0,0,0,0,0,0
-200,500,-100,0,-100,-100,0,0,0,0
0,-100,100,0,0,0,0,0,0,0
-100,0,0,200,0,0,-100,0,0,0
0,-100,0,0,300,-100,0,-100,0,0
0,-100,0,0,-100,300,-100,0,0,0
0,0,0,-100,0,-100,500,0,-200,0
0,0,0,0,-100,0,0,200,0,0
0,0,0,0,0,0,-200,0,400,-200
0,0,0,0,0,0,0,0,-200,300


--put fld "Target"
100
0
0
0
0
0
0
0
0
0

--- fld "solution"

1.269624573e+00
2.218430033e-01
9.761092147e-01
9.761092147e-01
8.566552894e-01
6.689419797e-01
6.962457337e-01
4.436860066e-01
3.344709900e-01
3.327645049e-01


Has anyone used the matrix routine in this thread to do 10x10?

Thanks,

David

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

Re: Code for 6x6 Matrix? - Little Problem

Post by [-hh] » Wed Jul 24, 2019 9:06 am

The algorithm is correct.
The problem is the line "combine x" in the "typical usage" above: For up to 9 elements "combine" preserves the order of an array with numeric keys, beginning from 10 elements the components are not yet in the correct order.

So here is, for your comfort, all you need:

Again the algorithm (unchanged):

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 (now converting the solution from array correctly also for n>9, see the last 4 lines):

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 -- the solution (if solvable)
   repeat with i=1 to n
      put x[i] into line i of x0
   end repeat
   put x0 into fld "solution" -- n rows
end mouseUp
The inverse coefficient matrix is computed as given above (unchanged).
shiftLock happens

DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Code for 6x6 Matrix? - Little Problem

Post by DR White » Wed Jul 24, 2019 12:08 pm

Hermann,

That change made all the difference int the world!
I tried solving 14 matrixes and it worked perfectly!

Just for your information, I highly regard the time and knowledge of the fine people of this forum. I worked 12 hours yesterday, reviewing my code, researching on the internet and modifying my code before reaching out for help from the forum.

There are many good reasons to use LC to develop your apps, but the one that sets LC apart from the other languages is the UNBELIEVABLE support from the people like Hermann (there are about 10 others that are EXTREMELY knowledgeable with different areas of expertise). The app that I am currently working on, would have been "Dead in the water", without having a strong matrix solving routine.

To anybody who is exploring which language to learn for developing apps, you cannot go wrong with LiveCode because of it's intuitive nature and unmatched support from this forum!!!!!

I am going to make a donation to LC today in supporting the doubling of the LC staff.

Thanks again for the UNBELIEVABLE support from this forum over the last 6 years,

David

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

Re: Code for 6x6 Matrix? - Solved

Post by [-hh] » Wed Jul 24, 2019 12:57 pm

Please keep in mind:
The algorithm doesn't check whether the linear system is solvable. In your case it is clear from the engineering problem.
The algorithm returns crazy data if the linear system is not solvable.
Also, there may occur numerical problems if LC's numbers are not exact enough. For such "extreme" cases one needs a computer algebra system like Mathematica to have "exact" results.

As a first check: The matrix-multiplication of the coefficient matrix by the solution vector should give the target vector. Here the script for solve with a first check:

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
   if tab is in mat then set itemdel to tab
   repeat with i=1 to n
      put line i of mat into t
      split t by the itemdel; put t into M[i]
      put line i of tgt into b[i]
   end repeat
   put gaussCF(M,b,n) into x
   -- first check of solution
   repeat with i=1 to n
      put 0 into e[i]
      repeat with j=1 to n
         add M[i][j]*x[j] to e[i]
      end repeat
      put e[i] - b[i] into line i of c -- error
      put x[i] into line i of s -- solution
   end repeat
   put s into fld "solution" -- n rows if solvable
   put c into fld "error" -- should return n rows of zero
end mouseUp
If a component of the error is not zero then the system is not solvable or you have a numerical (exactness) problem.

Try the above with matrix
1,2,3
4,5,6
7,8,9
and target vector (non-solvable system)
1
0
0
or target vector (solvable system)
1
1
1

Or check the determinant for unique solvability (though it is hard to say whether it is "exactly" zero). Given time I'll post a sample stack "linearSolve" that helps with that.
shiftLock happens

DR White
Posts: 718
Joined: Fri Aug 23, 2013 12:29 pm

Re: Code for 6x6 Matrix? - Solved

Post by DR White » Wed Jul 24, 2019 2:15 pm

Thanks so much,

David

Post Reply