# Help With Linear Ecuations In Lua

### #1 Guest_Alberto_*

Posted 13 August 2006 - 01:50 PM

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("...")

Please anyone can help me?

Thanks a lot

Alberto

### #2

Posted 13 August 2006 - 06:46 PM

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

Works by me.

### #3

Posted 14 August 2006 - 08:31 AM

You must quote the whole CAS expression, with or without the command solve, i.e.,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("...")

Please anyone can help me?

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

Posted 15 August 2006 - 06:12 AM

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

Posted 15 August 2006 - 08:51 AM

In a nutshell: You are trying to extensively mix CAS and Lua calculations. After developing several algorithms in CPLua, my personal conclusion is this:Can I use the all the CAS functions of Matrix-calculation?

*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

Posted 15 August 2006 - 09: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

Posted 15 August 2006 - 09:19 AM

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, useProper code for this operation:

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

x=cas.elem(mm,1,1)()instead.

### #8

Posted 15 August 2006 - 09:42 AM

### #9

Posted 17 August 2006 - 05:47 AM

only Lua functions

### #10 Guest_Alberto_*

Posted 17 August 2006 - 10:57 PM

### #12

Posted 19 August 2006 - 06:20 AM

the firts row of the matrix you have a zero because in that case it does not work

I'll add it tomorrow...

### #13

Posted 19 August 2006 - 08:46 AM

*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

Posted 19 August 2006 - 08:55 AM

*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

Posted 20 August 2006 - 03:20 AM

Gauss-Jordan

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users