Computing an Algebra sequence

Programming, for all ages and all languages.
Post Reply
chris

Computing an Algebra sequence

Post by chris »

How would one compute an algebra sequence like this 'x + 3 = 4' or 'x + 4 * 3 = x + 12' in C?
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:Computing an Algebra sequence

Post by Candy »

chris wrote: How would one compute an algebra sequence like this 'x + 3 = 4' or 'x + 4 * 3 = x + 12' in C?
in a very hard way. PS: in the second one, X is undefined.

If I may advise C++ for clarity:

Make a class for numbers, and a class for each operator. Parse the file (compiler generation techniques, search for LALR or LL(1)) and create a tree with the calculations. Make each side automatically calculate their constants (such as the 4*3) and then start adding mathematical sequences in the equals-class that for instance, move one to another side with opposite sign (solving the first), divides by some number, multiplies with some number, things like that.

And then, you'd end up with a new Maple.

Try that, it's a tried, tested & true version of algebra. It solves even things a friend of mine couldn't fit on a page, and the integral of that. After half an hour, it must be said, but still.

HTH, Candy
Schol-R-LEA

Re:Computing an Algebra sequence

Post by Schol-R-LEA »

Can you give more details about the assignment project? What is the purpose or goal? Does it have to be done in C? Can you use a notation that is easier to parse than infix (e.g., RPN)? Does it need to work with equations in two or more variables, and if so, can you just simplify the equations, or do you have to solve them (i.e., produce a graph of the function)? Inquiring minds want to know.
rtgb

Re:Computing an Algebra sequence

Post by rtgb »

a simpler approach could transfer everything to one side, so as f(x)=0, seeking suitable x values (bolzano theorem)
still, parsing a function from stdin is cumbersome - reverse polish notation and a couple of parenthesis help somewhat
not every equation has a sollution you know how to find, especially if x is an integer
in such cases (x real), calculus provides a way to study the graph of f(x), through its derivatives
is it for monday ?
chris

Re:Computing an Algebra sequence

Post by chris »

It was just something I was curious about. Maybe I'll try it sometime in the future :).
Tim

Re:Computing an Algebra sequence

Post by Tim »

Here's an algorithm I put together recently:

Code: Select all

Formula *ParseBinaryFunction(Stream *stream)
{
    Formula *formula = ParseUnaryFunction();

    char operator = stream->PeekChar();
    if (IsBinaryOperator(operator))
    {
        stream->SkipChar();
        formula = new BinaryFunction(formula, 
            operator, 
            ParseBinaryFunction(stream));
    }

    return formula;
}

Formula *ParseUnaryFunction(Stream *stream)
{
    char ch = stream->PeekChar();
    if (isdigit(ch))
        return new Constant(stream->ReadNumber());
    else
        return new Variable(stream->ReadIdentifier());
}

class Formula
{
};

class BinaryFunction
{
public:
    BinaryFunction(Formula *lhs, char operator, Formula *rhs);
};

class Constant
{
public:
    Constant(double number);
};

class Variable
{
public:
    Variable(std::string name);
};
Calling ParseBinaryFunction builds up a parse tree from the stream provided. Stream::PeekChar looks at the next character in the stream but does not read it. SkipChar reads the next character. ReadNumber attempts to parse the next token in the stream as a decimal number. ReadIdentifier attempts to parse the next token in the stream as a variable name.
lsjnv

Re:Computing an Algebra sequence

Post by lsjnv »

tree ? how about seaweeds ?!
parsing from left to right,
ignoring operant priorities and intermediate parenthesis,
using tail recursion ..

Formula *formula = ParseBinaryFunction();

amateurs!
Tim

Re:Computing an Algebra sequence

Post by Tim »

"lsjnv" (if that is your real name), this is a web message board for general programming, not a compiler research class.
Post Reply