I have the next infix expression:
(i730 + ssg2LQ) + ((+ G0 /(3064 + 68324.06)) / 28)
and I try to convert it to postfix notation, for this I used some on-line converters and all of them gave me the answer
1730 sg2LQ + G0 3064 68324.06 + / + 28 / +
but someone told me that the answer was
i730 sg2LQ + G0 + 3064 68324.06 + / 28 / +
and now I am confused, who is right? who is wrong? how is this expression properly converted?
The second answer
i730 sg2LQ + G0 + 3064 68324.06 + / 28 / +
is definitely wrong because it will first compute (i730 + ssg2LQ) + G0.
Perhaps this answer was obtained by a tool that was confused by unary +.
Related
I have three equations and three unknowns. When I run it in Maple, I get the error "division by zero". What should I do to fix this error?Do you know the reason for this, please?
This is the Maple code
restart;
r := 32*(6*m^2*xi + m^2 + 1)*(alpha*m^2 - 4*xi*sqrt(m^2/xi) + alpha)^2/(m^2*(alpha*m^2 - 16*xi*sqrt(m^2/xi) + alpha)^2*xi);
ns := -((m^2 + 1)*alpha*(alpha^2*(1/6 + xi)*m^6 + ((2*xi + 1/2)*alpha^2 - 3500*xi^2 - (1750*xi)/3)*m^4 + ((xi + 1/2)*alpha^2 - 8*xi^2 - (1762*xi)/3)*m^2 + alpha^2/6 - 4*xi)*sqrt(m^2/xi) - (2*alpha^2*(1/6 + xi)*m^8)/3 + (((2*xi)/3 + 1/3)*alpha^2 - 16*xi^2 - (8*xi)/3)*m^6 + (((10*xi)/3 + 5/3)*alpha^2 + (8*xi)/3)*m^4 + ((2*xi + 17/9)*alpha^2 + (16*xi)/3)*m^2 + (2*alpha^2)/3)/(2*sqrt(m^2/xi)*m^2*(6*m^2*xi + m^2 + 1)*xi*(alpha*m^2 - 4*xi*sqrt(m^2/xi) + alpha));
nt := -((alpha*m^2 - 4*xi*sqrt(m^2/xi) + alpha)*(alpha*(m^2 + 1)*sqrt(m^2/xi) - 4*m^2))/(12*sqrt(m^2/xi)*xi*m^2);
solve([0.9605 <= ns and ns <= 0.9693, 0 < r and r < 0.056, -0.76 < nt and nt < 0.52], {alpha, m, xi});
I tried to use help maple and use the command it wrote. But either I don't know how to write the command or this command is not correct to use.
here is the link to some pictures of my work:command in help maple
error in program
The documentation to this packages is available here, but for me it is impossible to transfer my real-valued 3 objective function to a form that it works.
I'm trying to solve it with the nsga2 function - and I think nsga3 should work the same way?!
My objective functions, which I want to minimize, are:
f1 <- 1640.2823 + 2.3573285*x[1] + 2.3220035*x[2] +4.5688768*x[3] + 7.7213633*x[4] + 4.4559504*x[5]
f2 <- 6.5856 + 1.15*x[1] - 1.0427*x[2] + 0.9738*x[3] + 0.8364*x[4] - 0.3695*x[1]*x[4] + 0.0861*x[1]*x[5] + 0.3628*x[2]*x[4] - 0.1106*x[1]^2 - 0.3437*x[3]^2 + 0.1764*x[4]^2
f3 <- -0.0551 + 0.0181*x[1] + 0.1024*x[2] + 0.0421*x[3] - 0.0073*x[1]*x[2] + 0.024*x[2]*x[3] - 0.0118*x[2]*x[4] - 0.0204*x[3]*x[4] - 0.008*x[3]*x[5] - 0.0241*x[2]^2 + 0.0109*x[4]^2
And x has to be between one and three (these are the constraints)
1 <= x[i] <= 3
I appreciate any hints... so many thanks in advance!
I have a linear system of four equations with four variables $a,b,c,d$ and two parameters $i,h$ where equations are roughly of the form
$$a h^3 i^3 + b h^2 i^2 +c h i +d=0$$
I want to get $a,b,c,d$ in terms of $i,h$.
Is this possible in SymPy? If not, can someone recomend how to do it on some other way?
For completeness, the answer is yes, solve in Sympy solves systems of equations with parameters. An example using the equation you stated:
from sympy import *
var('a b c d i h')
eqns = [a*h**3*i**3 + b*h**2*i**2 + c*h*i + d, a+b+c+d, a-b*h*i**2 -c - d, a+b+c-h**2 - i**2]
solve(eqns, [a,b,c,d])
The first argument of solve is a list of equations, the second the list of variables to solve for. The output is a solution, presented as a dictionary:
{c: (h**2 + i**2)*(-h**4*i**5 + h**3*i**3 - 2*h**2*i**2 + h*i**2 + 1)/(h*i*(-h**3*i**4 + h**2*i**2 + h*i**2 - 2*h*i + 1)),
b: -(2*h**2 + 2*i**2)/(h*i*(h**2*i**3 + h*i**2 - h*i + 1)),
a: (-h**3*i**2 + h**2 - h*i**4 + i**2)/(h*i*(h**2*i**3 + h*i**2 - h*i + 1)),
d: -h**2 - i**2}
I have an expressions like:
a*b*c + d*e + f - g*h*h + i*a
In other words: terms can be either added or subtracted, and each term is a product of some of the symbols.
Is there a way to come up with a minimal/simpler expression, basically the reverse of expand? I tried simplify and factor but I cannot get them to work. For example:
a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4
should turn into:
(a - b)**4 - a
but it stays unchanged when using the said commands.
PS: If this is something what SymPy simply cannot do, could you please suggest an alternative which does this?
See sympy factor simple relationship. SymPy's factor only knows how to factor the entire expression, but if you know the term you want to rewrite things in terms of, you can use a trick with subs, like:
>>> expr = a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4
>>> expr.subs(a, x + b).expand()
-b + x**4 - x
>>> expr.subs(a, x + b).expand().subs(x, a - b)
-a + (a - b)**4
Basically, let x = a - b, so that a = x + b. Then replace a with x + b, expand things out, and replace it back.
For your more complicated example, SymPy is actually smart enough to replace a*b in an expression correctly:
>>> expr = (a*b - c*d)**2 - a
>>> expr = expr.expand()
>>> expr
a**2*b**2 - 2*a*b*c*d - a + c**2*d**2
>>> expr.subs(a*b, x + c*d)
-a + c**2*d**2 - 2*c*d*(c*d + x) + (c*d + x)**2
>>> expr.subs(a*b, x + c*d).expand()
-a + x**2
>>> expr.subs(a*b, x + c*d).expand().subs(x, a*b - c*d)
-a + (a*b - c*d)**2
Another possible approach to this problem would be to try using factor on subsets of the terms in an expression (itertools.combinations could be useful here). For instance, to try factoring all combinations of all terms but one from your original expression:
>>> args = Add.make_args(expr)
>>> for comb in combinations(args, len(args) - 1):
... print(factor(Add(*comb)) + Add(*(set(args) - set(comb))))
...
a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4
a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4
a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4
a**4 - 4*a**3*b + 6*a**2*b**2 - 4*a*b**3 - a + b**4
-a + (a - b)**4
a*(a**3 - 4*a**2*b + 6*a*b**2 - 4*b**3 - 1) + b**4
You could check not isinstance(factored_expr, Add) to filter out the ones that aren't factored.
If you know the function in advance, then you can use more powerful software packages such as Maple to reduce the expression before putting it into your computer code. There is a optimization package in Maple, which reduces the expression into sub-expressions such that it takes advantage of repeated operations in the expression. Also you can factorize very complicated expressions in a much reliable way.
In addition such software also can create programming code as output, which you can directly paste in your program. If you do not have access to Maple or Mathematica software, you can also use a free (but powerful) software called maxima. http://maxima.sourceforge.net/
I need to write a cumulative summation function in R but I've been hitting a brick wall. The function has the following structure:
a*x1
a*x2 + a^2*x1
a*x3 + a^2*x2 + a^3*x1
a*x4 + a^2*x3 + a^3*x2 + a^4*x1
And so on. cumsum doesn't seem to work for this type of function. Is there any way this could be implemented in R?
Since your recursion is
u[n+1] = a * ( x[n+1] + u[n] )
i.e.,
u[n+1]/a = x[n+1] + a * u[n]/a,
you can use filter:
x <- 1:5
a <- 2
a*filter(1:5, a, method="recursive")
# Compare with the expected values
a*x[1]
a*x[2] + a^2*x[1]
a*x[3] + a^2*x[2] + a^3*x[1]
a*x[4] + a^2*x[3] + a^3*x[2] + a^4*x[1]