# Help With Linear Ecuations In Lua

14 replies to this topic

### #1 Guest_Alberto_*

Guest_Alberto_*
• Guests

Posted 13 August 2006 - 12:50 PM

Hi! I want to resolve a simple linear ecuations in lua, but how?
I wrote tis but don't work:

print (cas.solve({"5x-y=2","6x+y=15"},{x,y}))
also i tried with expresions (ex: f6=cas("...")
Thanks a lot

Alberto

### #2 -Tom-

-Tom-

Casio Freak

• Members
• 104 posts
• Location:Poland

• Calculators:

Posted 13 August 2006 - 05:46 PM

Try this

`print (cas.solve("{5x-y=2,6x+y=15},{x,y}"))`

Works by me.

### #3 PAP

PAP

Casio Overlord

• Members
• 681 posts
• Gender:Male
• Location:Somewhere in Europe.
• Interests:Computer Algebra, Numerical Analysis.

• Calculators:
ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 14 August 2006 - 07:31 AM

Hi! I want to resolve a simple linear ecuations in lua, but how?
I wrote tis but don't work:
print (cas.solve({"5x-y=2","6x+y=15"},{x,y}))
also i tried with expresions (ex: f6=cas("...")

You must quote the whole CAS expression, with or without the command solve, i.e.,
`print(cas.solve("{5x-y=2,6x+y=15},{x,y}"))`
or, equivalently,
`print(cas("solve({5x-y=2,6x+y=15},{x,y})"))`

However, you should not use the above expressions: they give a CP list, {x=17/11.y=63/11}, which is just printed on the screen, nothing more than that. The list is not really useful, since it cannot be used in a CPLua program (it is a CPLua variable of the type "userdata"). However, you can get numerical values for x and y in CPLua as follows:
```cas("solve({5x-y=2,6x+y=15},{x,y})=>aux")
x=cas("getRight(aux[1])")()
y=cas("getRight(aux[2])")()```
Here, the CP variable "aux" is used to store the list, then CAS functions are used to get the numerical values for x and y, which can be used in your CPLua program. If you have questions on how this code works, let me know.

### #4 omegavirus

omegavirus

Casio Freak

• Members
• 150 posts
• Gender:Male
• Location:Morelia, M?xico

• Calculators:

Posted 15 August 2006 - 05:12 AM

I am also having problems whit "linear equations" cause I am programming the gauss Jordan method to solve
equation but I have problems whit the manipulation of matrix en CPLua, I tried so many thing using just
CPLua or using the CAS but I have problems...

How can I do this in CPLua using the CAS and without it?

`1 x [2,3,4]`

sorry about the dumb question but I really have problems whit that...

Another question whit this code:

```m={{4,-2,3},{3,2,6}}
mm=cas("m")
y=cas.elem(mm,1,1)
x=tonumber(cas.elem(mm,1,1))```

it prints this:

```(a blank space)
nil```

Can I use the all the CAS functions of Matrix-calculation?

Thanks...

### #5 PAP

PAP

Casio Overlord

• Members
• 681 posts
• Gender:Male
• Location:Somewhere in Europe.
• Interests:Computer Algebra, Numerical Analysis.

• Calculators:
ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 15 August 2006 - 07:51 AM

Can I use the all the CAS functions of Matrix-calculation?

In a nutshell: You are trying to extensively mix CAS and Lua calculations. After developing several algorithms in CPLua, my personal conclusion is this: avoid CAS operations in CPLua programs as much as possible; it needs headache-programming, and I don't think that it is really needed. In most cases, it is better to use CPLua for-loops to perform matrix operations, instead of using the CAS. For example, to get a part of a matrix, use LNAutils' function Part (or write your own function for this); for matrix multiplication, use MatMul (also included in LNAutils). Calling the CAS to do similar operations needs to (1) convert a CPLua table to a CAS matrix, (2) perform the operation using CAS functions, (3) convert the result to CPLua form. This makes your program difficult to read and to debug. Of course, if you need symbolic computations, you can't avoid using the CAS within a CPLua program; however, the problems you have posted involve numerical (not symbolic) computations, so, in these cases, my opinion is that you should avoid the CAS completely.

Btw, the Gauss-Jordan method for solving linear systems is not used in practice, because it is slow, compared to more sophisticated methods. Gauss-Jordan is only used as an introductory method for educational purposes. If you just want to solve a linear system efficiently, use LU decomposition instead (it is included in LNA), or QR decomposition; both methods are much faster than Gauss-Jordan. Anyway, if you really need to write a CPLua program implementing the Gauss-Jordan method, you can easily do it without using the CAS at all.

### #6 -Tom-

-Tom-

Casio Freak

• Members
• 104 posts
• Location:Poland

• Calculators:

Posted 15 August 2006 - 08:03 AM

```m={{4,-2,3},{3,2,6}} <----mistake
mm=cas("m")  <----- mistake
y=cas.elem(mm,1,1) <----must be converted to nuber as below
x=tonumber(cas.elem(mm,1,1))```

Proper code for this operation:
```m="[[4,-2,3],[3,2,6]]"
mm=cas(m)
x=tonumber(cas.elem(mm,1,1))```

If want to use 'Y' in Lua calculations, must be converted to number, same as 'X'.

### #7 PAP

PAP

Casio Overlord

• Members
• 681 posts
• Gender:Male
• Location:Somewhere in Europe.
• Interests:Computer Algebra, Numerical Analysis.

• Calculators:
ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 15 August 2006 - 08:19 AM

Proper code for this operation:

```m="[[4,-2,3],[3,2,6]]"
mm=cas(m)
x=tonumber(cas.elem(mm,1,1))```

Are you sure that your code is correct? I think it's not: x=tonumber(cas.elem(mm,1,1)) is incorrect. To convert the result to a CPLua number, use
`x=cas.elem(mm,1,1)()`

### #8 -Tom-

-Tom-

Casio Freak

• Members
• 104 posts
• Location:Poland

• Calculators:

Posted 15 August 2006 - 08:42 AM

Of course... sorry for that. My mistake.

### #9 omegavirus

omegavirus

Casio Freak

• Members
• 150 posts
• Gender:Male
• Location:Morelia, M?xico

• Calculators:

Posted 17 August 2006 - 04:47 AM

only Lua functions

### #10 Guest_Alberto_*

Guest_Alberto_*
• Guests

Posted 17 August 2006 - 09:57 PM

Works fine !Thanks a lot of all people

### #11 omegavirus

omegavirus

Casio Freak

• Members
• 150 posts
• Gender:Male
• Location:Morelia, M?xico

• Calculators:

Posted 18 August 2006 - 04:32 AM

Here is it, thanks PAP for LNA!!!

Gauss-Jodan

### #12 omegavirus

omegavirus

Casio Freak

• Members
• 150 posts
• Gender:Male
• Location:Morelia, M?xico

• Calculators:

Posted 19 August 2006 - 05:20 AM

In the program I'm missing a function that swith the rows of the matrix, you need it in cases when in
the firts row of the matrix you have a zero because in that case it does not work

### #13 PAP

PAP

Casio Overlord

• Members
• 681 posts
• Gender:Male
• Location:Somewhere in Europe.
• Interests:Computer Algebra, Numerical Analysis.

• Calculators:
ClassPad 300 (plus an old Casio model, with only a few Kb ram).

Posted 19 August 2006 - 07:46 AM

Hmmm, you need row interchanging even if the first row hasn't a leading zero. In each iteration, you should make sure that the pivoting element is greater than any other element after it: In the first iteration, the pivoting element is (1,1), and must be greater (in absolute value) than (1,2), (1,3), .... In the second iteration, the pivoting element is (2,2), and must be greater than (2,3), (2,4), ...., and so on. If it's not, you must interchange rows so that the pivoting element is greater than all elements after it in the same column. This is called "Gauss-Jordan method with partial pivoting" and it must be implemented in your algorithm, otherwise your program will fail to solve "stiff" linear systems, due to round-off errors. My LU decomposition allgorithm uses partial pivoting to eliminate round-off errors; since the Gauss-Jordan method needs more calculations than LU decomposition, partial pivoting is mandatory in your program. Please take this advice seriously: if you don't implement partial pivoting, you should not trust your program results: it will work correctly only in simple cases, but I can give you tons of examples where your program will give completely erroneous results.

### #14 omegavirus

omegavirus

Casio Freak

• Members
• 150 posts
• Gender:Male
• Location:Morelia, M?xico

• Calculators:

Posted 19 August 2006 - 07:55 AM

I noted that and I was thinking how to solve it, I will think a solution first and then try to do it in CPLua and then I will see you partial pivoting is mandatory in your LU decomposition algorithm...

To tell you the true you are like the programation and Numerical Analysis teacher that I've never had...

### #15 omegavirus

omegavirus

Casio Freak

• Members
• 150 posts
• Gender:Male
• Location:Morelia, M?xico

• Calculators: