Converting terenary and boolean operators from infix to postfix - postfix-operator

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.

Related

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

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?

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.

Sign computation with a macro

What the following expression computes, exactly?
#define SIGN(x) ((x < 0) ? -1 : (x > 0))
what yields if x is zero, less than zero, more than zero?
I guess I know the answer, but I'd like to check for clarity...
Thank you
EDIT: added missing parenthesis
EDIT: more info here
First, the macro doesn't compute anything. It is substituted into a source code, expanded, and the resulting text gets compiled. What the resulting text is, depends on the way you use the macro, especially on what parameter you give.
Second, the macro lacks one closing paren, so it probably would not give you a meaningful expression to be compiled.
Third, even when you add the lacking paren:
#define SIGN(x) ((x < 0) ? -1 : (x > 0))
it is possible you get unexpected results if you use the macro in a non-simplest way. For example,
SIGN(a ^ b)
would result in
((a ^ b < 0) ? -1 : (a ^ b > 0))
which is interpreted in C and C++ as
((a ^ (b < 0)) ? -1 : (a ^ (b > 0)))
which certainly is not what we intend.
You should add parentheses to avoid unwanted operators binding – for:
#define SIGN(x) (((x) < 0) ? -1 : ((x) > 0))
the above example will yield a sensible expression
(((a ^ b) < 0) ? -1 : ((a ^ b) > 0))
but that still doesn't protect you against unwanted double increment/decrement in case of plus-plus or minus-minus operators or double execution of a function in case the expression substituted for x contains a function call.
It does exactly what you probably think it does, gives -1 for negative numbers, 0 for zero, and 1 for positive numbers.
However, you should generally avoid function-like macros since they will not do what you expect if, for example, you try to calculate SIGN(value++). Since they're simple text substitutions, that would resolve to:
((value++ < 0) ? -1 : (value++ > 0)
You're far better off just using real functions and letting the compiler inline them if it decides it's worth it. You can also suggest to the compiler that inlining it, with the inline keyword, but keep in mind it is a suggestion.
That macro got a stray parenthesis.
It looks like it is meant to be an implementation of signum function, which returns -1, 1 or 0 depending on value of argument.
For sake of being safe and writing C++ code, it is prudent
to replace macro by template, similar to
template <class T>
int SIGN( T x )
{
return (x < T(0)) ? -1 : (x > T(0));
}
First comparision is argument of trenary operator ?:. Ternary would return -1 if expression evaluates to true , i.e. x is less than 0, otherwise it would return result of x > T(0).
That expression would evaluated to 0 if x equals to 0, otherwise it would be evaluated to 1.
Note that my implementation is not ideal, you can find better implementation elsewhere on SO.
An alternative expression can be:
return (T(0)<x) - (T(0)>x);
Which may be more effective with platforms that implement certain CPU instructions
If you use it with values only and not expressions that macro will produce -1, 0, 1, otherwise you may have serious problems. The tricky part is (x>0). Lets read the standard:
5.9 Relational operators [expr.rel]
The operators < (less than), > (greater than), <= (less than or equal
to), and >= (greater than or equal to) all yield false or true. The
type of the result is bool.
and
3.9.1 Fundamental types [basic.fundamental]
Values of type bool are either true or false. Values of type bool participate in integral promotions (4.5).
Thus x>0 is either true or false.
4.5 Integral promotions [conv.prom]
A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.
and is promoted to either 1 or 0 (respectively).

How many stacks are needed for converting the below infix expression to postfix expression?

If the expression is E = A + B, where A = log(3x+4), B = log(log(3x+4)+5), and I want to define a postfix expression for it, so how many stacks are required for this?
I'm not able to approach it because of these logarithmic terms, so please clarify this?
This question can be solved using Dijkstra's Shunting Yard Algorithm. A single stack is enough. Logarithmic functions can be solved as any other function. The precedence is in order () > log > * > +. Given E = A + B, supplying the values we'd have:
E = log(3*x + 4) + log(log(3*x + 4) +5).
Considering 3x as 3*x and scanning from left to right and applying the Shunting algorithm step by step, the Postfix expression will be:
3 x * 4 + log 3 x * 4 + 5 + log log +
I used single space to delimit operators and operands for readability. A few key points from Shunting Yard Algorithm that would help you solve this and similar questions are:
The postfix expression does not contain any parentheses.
The operators maintain their order in both prefix and postfix expressions.
Operators with lower precedence are at lower below the operators with higher precedence in stack. If a higher precedence operator is at the top of the stack and a lower precedence symbol is being scanned, then all operators with higher precedence are popped out of stack and written in output and lower precedence operator is pushed in the stack.
The single stack enough.
For example let examine a few simple expressions:
a + b * 1 gives a b 1 * +
a * b + 1 gives a b * 1 +
As you can see, we can simple copy each constant or variable from an input stream to an output, but we should process operators. Let's use the operators' stack. For each operator from an input stream we can store it to the stack, or we can copy it to an output stream.
To make a decision we should compare a precedence of the operator on the top of the stack, and a precedence of the next operator from the input stream.
For * > + (as in the first example above) you need to push the next operator * to the stack. Also you need to push the first operator to the stack.
For + <= * (as in the second example) you need to copy the operator to the output stream.
At the end of the input stream you need to pop all remaining operators from the stack and copy them to the output stream.
Expressions with parentheses: just consider ( as operator with a highest precedence. Copy it to the stack. With the ) do pop operators from the stack till the ( and put them to the output. Then pop ( but don't put ( and ) symbols.
a * (b + c * d) gives:
a — *
a b — * ( +
a b c — * ( + *
a b c d * + — *
a b c d * + *
Functions: 3 * log(x + 2) is x 2 + log 3 * in the postfix form, so you just can consider the precedence of () > log > *.
Now important note: you need the one stack to output an expression in postfix form. But you need two stacks to evaluate the same expression — first to variables/constants, and second to operators.
PostFix expression of that would be :
E 3 x * 4 + log 3 x * 4 + log 5 + log + =
Going from left to right if you come across operator then you simply put it in between the two operands that are in front of it. for example 2 3 + => 2 + 3 except for log in case of log it would be something like this 2 3 log => 2 log(3) if there is a situation like this 2 3 log + then => 2 log(3) + => 2 + log(3) Enjoy the following steps
step 1: E, (3*x), 4, +, log, 3, x, *, 4, +, log, 5, +, log, +,=
note **,** is just for the separations you can ignore if you want
step 2: E, (3x + 4 ), log, 3, x, *, 4, +, log, 5, +, log, +, =
step 3: E, log(3x + 4), 3, x, *, 4, +, log, 5, +, log, +, =
step 4: E, log(3x + 4), (3*x), 4, +, log, 5, +, log, +, =
step 5: E, log(3x + 4), (3x + 4), log, 5, +, log, +, =
step 6: E, log(3x + 4), log(3x + 4), 5, +, log, +, =
step 7: E, log(3x + 4), (log(3x +4) + 5 ), log, +, =
step 8: E, log(3x + 4), log(log(3x + 4) + 5), +, =
step 9: E, log(3x + 4) + log(log(3x+ 4) + 5), =
step 10: E = log(3x + 4) + log(log(3x+4) + 5)
A stack is needed to solve the given expression
E = A + B, where A = log(3x+4), B = log(log(3x+4)+5)
regardless of the fact that it seems too complex. You can also plug in the equivalent expressions of A and B in expression E and then solve the entire expression E as a single expression.
Therefore, after solving, the equivalent postfix expression will be:
3x*4+log3x*4+log5+log+

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.

Resources