Thread: Linear Equation Solver

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    42

    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.

  2. #2
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I have seen very minimal Python, but post the Python code and maybe we could figure something out.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    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++.

  4. #4
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    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/
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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++ .....
    Last edited by grumpy; 10-20-2005 at 02:21 PM.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    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.
    Last edited by EvilGuru; 10-21-2005 at 07:04 AM.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    42
    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.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. solve linear equation
    By unix7777 in forum C++ Programming
    Replies: 4
    Last Post: 11-12-2008, 11:47 PM
  2. Quadrtic equation solver with graphics.
    By joeyzt in forum Windows Programming
    Replies: 18
    Last Post: 07-20-2003, 02:35 PM
  3. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 11:46 AM