Page 1 of 1

Computing an Algebra sequence

Posted: Thu Feb 26, 2004 8:58 pm
by chris
How would one compute an algebra sequence like this 'x + 3 = 4' or 'x + 4 * 3 = x + 12' in C?

Re:Computing an Algebra sequence

Posted: Thu Feb 26, 2004 11:42 pm
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

Re:Computing an Algebra sequence

Posted: Fri Feb 27, 2004 11:04 am
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.

Re:Computing an Algebra sequence

Posted: Fri Feb 27, 2004 12:23 pm
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 ?

Re:Computing an Algebra sequence

Posted: Sat Feb 28, 2004 10:33 am
by chris
It was just something I was curious about. Maybe I'll try it sometime in the future :).

Re:Computing an Algebra sequence

Posted: Sat Feb 28, 2004 10:47 am
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.

Re:Computing an Algebra sequence

Posted: Sat Feb 28, 2004 11:35 am
by lsjnv
tree ? how about seaweeds ?!
parsing from left to right,
ignoring operant priorities and intermediate parenthesis,
using tail recursion ..

Formula *formula = ParseBinaryFunction();

amateurs!

Re:Computing an Algebra sequence

Posted: Sat Feb 28, 2004 12:24 pm
by Tim
"lsjnv" (if that is your real name), this is a web message board for general programming, not a compiler research class.

Re:Computing an Algebra sequence

Posted: Sun Feb 29, 2004 12:12 am
by kzdbv