How to convert infix expression with functions in expression to binary tree? - algorithm

Here is the example expression: (A + B * C^D - A + (-)B)/(sin(A) + pow(C, D) - ln((A + B )/ C))
I was thinking about converting this infix expression to postfix where functions sin, ln and pow have same priority as other operands like A,B,C...
Then I wanted to convert that postfix expression to tree using stack?
Is this good idea or is there something better I can do?

Related

How to identify the order of evaluation of a mathematical expression and its correctness?

Is a - b + c - d = a + c - b - d mathematically correct?
I believe this statement can be correct but only sometimes if the order of evaluation does not matter so if I were to do
{(a - b) + c} - d and choose numbers that would evaluate to {(a + c) - b} - d where b and c are both the same numbers then this may be correct.
Is there a more mathematical and logical explanation for this?
I also think it has to do with associativity but that would prove that this statement is never correct since addition and multiplication are associative (separately) but not addition and subtraction together
This highly depends on the definition of + and -. So far as you have written, they are but free untyped infix symbols so it's hard to tell.
A simple example. Suppose values are of fixed-width floating point type (like one of those defined in IEEE-754, for instance). Next, if we have
a = 10e100
b = -10e-100
c = -10e100
d = -10e-100
and the expressions are evaluated greedily left-to-right, then
a - b + c - d = ((a - b) + c) - d
When the type has enough order bits to contain decimal orders of -100 and 100, but its mantissa is not wide enough to correctly represent 10e100 + 10e-100, specifically, the RHS argument is simply lost in this expression, then the value of the whole large expression is
((10e100 - -10e-100) + -10e100) - -10e-100 =
= (10e100 + -10e100) - -10e-100 = 0 - -10e-100 = 10e-100
But the second expression would evaluate to
((a + c) - b) - d = ((10e100 + -10e100) - -10e-100) - -10e-100 = 20e-100
So you see, the result can differ by 100% depending on the order of evaluation.

How to deal with unary minus and exponentiation in an expression parser

I know that exponentiation has higher precedence that the unary minus. However if I build an expression parser based on that I still can’t parse expressions like 2—-3. In order to deal with these I’ve found I also need to add unary minus handling to the factor production rule that is one precedence higher than exponentiation. Is this how the unary minus and exponetiation is usually dealt with? I’ve not found anything online or in books that talks about this particular situation. I was wondering whether making exponentiation and unary operators having equal precedence you help?
I'm hand crafting a recursive descent parser, I tried merging the power and unary production rules together but it didn't seem to work. What does work is the following EBNF
factor = '(' expression ')' | variable | number | '-' factor
power = factor { '^' factor }
unaryTerm = ['-' | '+'] power
term = unaryTerm { factorOp unaryTerm }
expression = term { termOp term }
termOp = '+' | '-'
factorOp = '*' | '/'
Unless you have unusual requirements, putting both unary minus and exponentiation in the same non-terminal will work fine, because exponentiation is right-associative: (Yacc/bison syntax)
atom: ID
| '(' expr ')'
factor
: atom
| '-' factor
| atom '^' factor
term: factor
| term '*' factor
expr: term
| expr '+' term
| expr '-' term
Indeed, exponentiation being right-associative is virtually required for this syntax to be meaningful. Consider the alternative, with a left-associative operator.
Let's say we have two operators, ⊕ and ≀, with ⊕ being left associative and binding more tightly than ≀, so that ≀ a ⊕ b is ≀(a ⊕ b).
Since ⊕ is left associative, we would expect a ⊕ b ⊕ c to be parsed as (a ⊕ b) ⊕ c. But then we get an oddity. Is a ⊕ ≀ b ⊕ c the same as (a ⊕ ≀b) ⊕ c) or the same as a ⊕ ≀(b ⊕ c))? Both options seem to violate the simple patterns. [Note 1]
Certainly, an unambiguous grammar could be written for each case, but which one would be less surprising to a programmer who was just going by the precedence chart? The most likely result would be a style requirement that ≀ expressions always be fully parenthesized, even if the parentheses are redundant. (C style guides are full of such recommendations, and many compilers will chide you for using correct but "unintuitive" expressions.)
Notes:
If you use precedence declarations, you'll get a ⊕ ≀(b ⊕ c)), which might or might not be intuitive, depending on your intuitions.

Define operator in prolog

I am doing a lot of prolog exercises to improve my logic skills. But i'm stuck with the request of an exercise.
What i have to do is to define an operator i , in a way that: if the user inputs a complex number with this syntax , via the prompt ( so i use the read(X) operator)
(4+ i 7) - (2+ i 3).
i get as result
2+ i 4
I've understood how to define an operator in Prolog,i've studied the op operator but i dont know how make that subtraction operation really happen
Your first problem is that xfx defines a binary operator, and you want a unary operator, so you need a declaration like this:
:- op(600, xf, i).
Your second problem is that, there is no circumstance in which entering arithmetic expressions at the Prolog query prompt will result in anything like reduction happening automatically. See:
?- 3 + 4 * 7.
ERROR: Undefined procedure: (+)/2 (DWIM could not correct goal)
?- X = 3 + 4 * 7.
X = 3+4*7.
In order to cause arithmetic to be evaluated, you have to use the is/2 operator:
?- X is 3 + 4 * 7.
X = 31.
Try and think of is/2 as just another predicate, relating a numeric value with an expression. There is no way in ISO Prolog to modify the behavior of is/2, so you'll have to make an evaluation predicate of your own and use that:
eval((A + B i) + (C + D i), E + F i) :-
E is A + C,
F is B + D.
Once you have that, you can use it the usual way:
?- eval((3 + 4 i) + (7 + 8 i), X).
X = 10+12 i.
As you can see, this is probably going to get tedious, but it will work. If you want to gin up more comprehensive support for complex numbers by hand, you should consider making a metainterpreter.

Why won't this Mathematica code maximize?

f[n_] := ((A*n^a)^(1/s) +
c*(B*(a*c*(B/A)^(1/s)*n^(1 - (a/s)))^(-(a*s)/(a - s)))^(1/s))^s +
b*log (1 - n - ((a*c*(B/A)^(1/s)*n^(1 - (a/s)))^(-(a*s)/(a - s))))
d/dn (f (n))
d/dn (f[n])
D[f[n], n]
solve (D[f[n], n] = 0)
0
Solve[D[f[n], n] = 0, n]
Solve[0, n]
Maximize[f[n], n]
Maximize[b log (1 - n - (a (B/A)^(1/s) c n^(1 - a/s))^(-((a s)/(a - s)))) + ((A n^a)^(1/s)
+ c (B (a (B/A)^(1/s) c n^(1 - a/s))^(-((a s)/(a - s))))^(1/s))^s, n]
I am not getting anything returning for any of these functions. Any idea why?
Attaching a photo of the mathematica script:
First of all, you're using solve with a lowercase, which is just an undefined variable. To use the function Solve you need to write it with a capital letter. In the same way, you have to write Log with a capital letter, not a lower-case letter, since it's a built in function.
Second, your open parenthesis is not a bracket. Functions in Mathematica require brackets, like Solve[ ... ], not Solve( ).
Third, you're using = instead of ==. The single equals = is used to store variables, the double equals == is used to represent equality.
See if you can get it to work after remedying these errors.

Converting terenary and boolean operators from infix to postfix

How can I convert these two examples from infix to postfix?
Example 1:
max = (a > b) ? a : b
Example 2:
(a != 0) ? ((b != 0) ? True : False) : False
For both expressions, I thought I would just have to remove the brackets. However, when I try to convert back from postfix to infix, the expression is invalid. I know how to do simple operations:
Infix: (((a + b) * (c + d) + a) * c - 6) * b
Postfix: a b + c d + * a + c * 6 - b *
...but I'm not sure how to convert max and boolean expressions.
Instead of thinking about '+', '-', and '*' as operations apart from
boolean operators or max / min functions, I think you could understand
the problem better by simply saying, "All of these are operators with 2 operands --
ie, they are binary operators."
Then, the problem is setting up your expression tree so that the root is an
operator ('+', '-', 'max' or whatever), and the children are operands. Producing
infix or postfix is simply a question of how you traverse the expression tree.

Resources