Linear Equation Solver

• 10-20-2005
EvilGuru
Linear Equation Solver
I am trying to port a mathematics library that I made in Python a few years ago to C++. It is not as hard as I had expected it to be, however, I am having trouble converting my Linear Equation Solver into C++. This is because it uses Complex numbers as well as the 'eval' function (it is amazingly only three lines of code). Can anyone give me any pointers on how I should convert this kind of thing into C++? I am just not sure as to the best way to tackle this problem in C++.
Thanks for any advice that you can give me.
• 10-20-2005
jmd15
I have seen very minimal Python, but post the Python code and maybe we could figure something out.
• 10-20-2005
EvilGuru
This is the python code, it makes use of eval() and Complex (imaginary numbers).
Code:

```def solve(eq,var='x'):     eq1 = eq.replace("=","-(") + ")"     c = eval(eq1,{var:1j})     return -c.real/c.imag```
>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2")
3236.0

I do not think that something along the same lines as the python one can be done using C++.
• 10-20-2005
jmd15
Well it looks like you are finding and replacing certain symbols. You can search a string for a certain character, or that symbol, in other words. Check out this function: strchr() that comes from cstring. More on it here. That doesn't solve all your problems though. Check out some of these math functions to see if you can use any: http://www.cplusplus.com/ref/cmath/
• 10-20-2005
grumpy
Quote:

Originally Posted by EvilGuru
This is the python code, it makes use of eval() and Complex (imaginary numbers).
Code:

```def solve(eq,var='x'):     eq1 = eq.replace("=","-(") + ")"     c = eval(eq1,{var:1j})     return -c.real/c.imag```
>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2")
3236.0

I do not think that something along the same lines as the python one can be done using C++.

Sure it could. C++ is turing complete, just as Python is. Which means that if it is computable, it can be done in either language.

The first line is a simple set of operations on a stringf (replacingf instance(s?) of "=" with "-(" and appending a ")' to the resultant string).

The second line is (I assume) parsing the string. A C++ equivalent function would accept a std::string as firt argument, and return a std::complex value. You'll need to explain what the syntax {var:1j} does, but mapping that into some C++ equivalent is certainly possible.

As to whether it is easier to do this in C++, or not .... that's another question. The reason we have different programming languages is that some languages are better suited to some tasks than others.

IIRC, the core of the Python interpreter is written in either C or C++ .....
• 10-21-2005
EvilGuru
The
Code:

`{var:1j}`
passes in a dictionary containg one item, var (which defaults to 'x') as a complex number. It would be evaluated as
Code:

`{'x':1j}`
with the result of it being assigned to c.
To make the code more portable to other languages I would prefer not to use the complex number type, or using a custom form of a complex number.
• 10-22-2005
grumpy
You'll have to specify in non-python specific language what is meant by "a dictionary containing one item as a complex number" and how "eval" works with it. While the mapping of that into C++ probably won't be trivial (eg a "dictionary" is not a basic concept in the C++ language so you'll have to implement all operations on dictionaries in a library, possibly in a class library, so you can use them in one-liners like you can in python).

The problem with mapping "high level" functions from languages like python (or perl, or ...) is that you have to implement a whole lot of helper code to make those functions work in C++. It's certainly possible to do that, if difficult.

You might want to do a hunt for clones of matlab or maple. They are proprietary interpreted languages for doing numerical work and symbolic logic respectively. Both would require a parser that can work with expressions. There are some free clones available, some (I understand) with source code in C/C++. The source for such a parser might give you a useful starting point.
• 10-22-2005
EvilGuru
A dictionary is like a hash map, or an array where they keys are strings rather than integers. The eval statement is the nasty one as it is a high level thing. It evaluates and compiles a piece of code (e.g. 5 + 6 /8 * 2) and returns the result. The second parameter is for passing in variables. Here is how the python function works.
Quote:

Here's how I think it works
Code:
eq1 = eq.replace("=","-(") + ")"

Ok, that's easy. If you have x-2 = 2*x this line returns x-2 -(2*x).
If you would do the calculation by hand, you would end up with something like a*x + b, where the x is the solution. Well, this line
Code:
c = eval(eq1,{var:1j})

does something similar. It sets x to 1j and evaluates the expression:
Code:
x - 2 - 2*x # now set x to 1j
1j - 2 - 2*1j # gives
2 - 1j

Now, 2 - 1j is not really a complex number, where j would be sqrt(-1), but j here is our x, the solution. All we have to do now is to calculate j.
Code:
a + bj
bj = -a
j = (-a)/b