Computing an Algebra sequence
Computing an Algebra sequence
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
in a very hard way. PS: in the second one, X is undefined.chris wrote: How would one compute an algebra sequence like this 'x + 3 = 4' or 'x + 4 * 3 = x + 12' in C?
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
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
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 ?
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
It was just something I was curious about. Maybe I'll try it sometime in the future .
Re:Computing an Algebra sequence
Here's an algorithm I put together recently:
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.
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);
};
Re:Computing an Algebra sequence
tree ? how about seaweeds ?!
parsing from left to right,
ignoring operant priorities and intermediate parenthesis,
using tail recursion ..
Formula *formula = ParseBinaryFunction();
amateurs!
parsing from left to right,
ignoring operant priorities and intermediate parenthesis,
using tail recursion ..
Formula *formula = ParseBinaryFunction();
amateurs!
Re:Computing an Algebra sequence
"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
http://www.csd.uch.gr/~hy352/assignment ... ment_1.pdf
http://www.csd.uch.gr/~hy352/files/solution1.zip
excel spreadsheets in software engineering ..
http://www.csd.uch.gr/~hy352/files/solution1.zip
excel spreadsheets in software engineering ..