Is there any algorithm that can find the sign of an arbitrary symbolic algebraic expression given in a "Tree - Form"?
I know that a general algorithm doesn't exist because the zero recognizion problem is undecidable for an arbitrary expression, but how should I approach the problem of finding the sign of an expression? (how is this done in computer algebra?)
For example: sign(sqrt(2)-1) = ?
Evaluate the function value
You need function evaluator engine for that (it is not that hard to code) there is no way to evaluate sign only if you want to support +,- operations !!! All my function evaluators works like this:
compile the source text of the function
First create supported functions table (id,num of operands,name,pointer to function) like:
These will be the building blocks to any supported expression you need to evaluate. Do not forget to code all functions in your code too. Handle brackets
(
,)
also as functions (push,pop
). Group your functions by number of operands so+,-
are with 1 and 2 operands (two different functions each !!!).Now from expression extract:
Into some kind of table/list:
And now finally construct compiled function string. My strings are set of two
int
's. First istype
(which table to use) and second isid
(index in table).for example expression:
types:
functions:
There are no variables or constants. The numbers are:
compiled string:
After compilation you need to interpret the string and evaluate it's value.
init variables
read first record of compiled string
if it is value (number,constant,variable) set appropriate
op?
value with it and increment operand counteropn++
.if it is function set
fx,fxn
code with itif
opn == fxn
You reached needed operand count so execute function
fx
and init next functionif not end of string goto #2 but with next string record
at the end
op1
should hold your output valuesome example functions (C++ implementation):
[Notes]
You have to handle special cases like
function = "";
. Also beware spacing, case sensitivity because any error in compilation invalidates the result.Speed is not a big issue this is interpreting-evaluation not numerical solution. All operations are called the same times as you would do on the paper.
You should also handle mathematic errors (overflows,invalid operands,
NaN
,Inf
...)I usually group functions with the same number of operands to own type to simplify things