infix notaion:x=a+b*(c-d+e/f)/(g*h)+i
I transform infix into postfix,and I have two answers.I can't sure that which is correct.
1.x a b c d - e f / + g h * / * + i + =
2.x a b c d - e f / + * g h * / + i + =
I transform post-fix into motion sequence,and found both stacks is empty.
So, I want to ask whether has possibility of two answers.
If you do the evaluation, you'll see that your question comes down to whether (b*(c-d+e/f))/(g*h) is the same as b*((c-d+e/f)/(g*h))
The answer is that they're the same. That is:
(x*y)/z == x*(y/z)
Related
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.
the algorithm is pretty straight forward I implemented it as soon as I saw newtons equation
function sq(x , e , g ){
g = g || x / 2
if(Math.abs( g * g - x ) < e )
return g
else
return sq( x , e , ( g + x / g ) / 2 )
}
now here is the thing on really small values the algorithm gives a way off answer and on really large values the algorithm exceeds the call stack .
I understand why
what I don't understand is in the first condition ..
if(Math.abs(g*g -x) < e ) why!! if we divide by x before comparing solves the problem e.g:
if(Math.abs(g*g -x) / x < e)
function sq(x , e , g ){
g = g || x / 2
if(Math.abs( g * g - x ) / x < e )
return g
else
return sq( x , e , ( g + x / g ) / 2 )
}
call the function like this first arg is the number you wanna compute the square root of , second is epsilon which is the range is which when I get a value should be acceptable , you could define an initial guess as a third argument
e.g:
sq( 9 , 0.01)
or:
sq(9 , 0.01 , 2)
Typically, you would specify ε as a fraction of g, not x, since normally you would want to say that the result has some precision ("six digits", for example) which is necessarily relative to the result. But it doesn't make much difference aside from interpreting the meaning of the ε parameter.
It is certainly the case that choosing some absolute error threshold makes no sense unless you know that the possible arguments are within a very restricted set of values.
In Maxima, I want to change the following equation:
ax+b-c-d=0
into the following format
(ax+b)/(c+d)=1
Note:
something like ax+b-c-d+1=1 is not what I want.
Basically I want to have positive elements in one side and negative elements in another side, then divide the positive elements by the negative elements.
Here is a quick attempt. It handles some equations of the form you described, but it's probably easy to find some which it can't handle. Maybe it works well enough, or at least provides some inspiration.
ptermp (e) := symbolp(e) or (numberp(e) and e > 0)
or ((op(e) = "+" or op(e) = "*") and every (ptermp, args(e)));
matchdeclare (pterm, ptermp);
matchdeclare (otherterm, all);
defrule (r1, pterm + otherterm = 0, ratsimp (pterm/(-otherterm)) = 1);
NOTE: the catch-all otherterm must be precede pterm alphabetically! This is a useful, but obscure, consequence of the simplification of "+" expressions and the pattern-matching process ... sorry for the obscurity.
Examples:
apply1 (a*x - b - c + d = 0, r1);
a x + d
------- = 1
c + b
apply1 (a*x - (b + g) - 2*c + d*e*f = 0, r1);
a x + d e f
----------- = 1
g + 2 c + b
Some time ago I asked a question on math.stackexchange and got an answer. I have difficulties deriving an algorithm from that answer because my background is in design and hope some of you can help me.
The original question with visual sketch and possible answer are here:
https://math.stackexchange.com/questions/667432/triangle-with-two-constraints-each-corner-on-a-given-line
The question was: Given 3 3-dimensional lines (a, b and c) that coincide in a common point S and a given Point B on b, I'm looking for a point A on a and a point C on c where AB and BC have the same length and the angle ABC is 90 degrees.
I will have to implement this algorithm in an imperative language, any code in C++, Java, imperative pseudo-code or similar is fine.
Also, different approaches to this problem are equally welcome. Plus: Thanks for any hints, if the complete solution is indeed too time-consuming!
The two key formulas are
(I've replied the derivation for the formulas in the mathematics stack exchange site)
Substituting the first in the second gives in the end a 4th degree equation that is quite annoying to solve with a closed form. I've therefore used instead a trivial numerical solver in Python:
# function to solve (we look for t such that f(t)=0)
def f(t):
s = (t*cB - B2) / (t*ac - aB)
return s*s - 2*s*aB - t*t + 2*t*cB
# given f and an interval to search generates all solutions in the range
def solutions(f, x0, x1, n=100, eps=1E-10):
X = [x0 + i*(x1 - x0)/(n - 1) for i in xrange(n)]
Y = map(f, X)
for i in xrange(n-1):
if (Y[i]<0 and Y[i+1]>=0 or Y[i+1]<0 and Y[i]>=0):
xa, xb = X[i], X[i+1]
ya, yb = Y[i], Y[i+1]
if (xb - xa) < eps:
# Linear interpolation
# 0 = ya + (x - xa)*(yb - ya)/(xb - xa)
yield xa - ya * (xb - xa) / (yb - ya)
else:
for x in solutions(f, xa, xb, n, eps):
yield x
The search algorithm samples the function in the interval and when it finds two adjacent samples that are crossing the f=0 line repeats the search recursively between those two samples (unless the interval size is below a specified limit, approximating the function with a line and computing the crossing point in that case).
I've tested the algorithm generating random problems and solving them with
from random import random as rnd
for test in xrange(1000):
a = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
b = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
c = normalize((rnd()-0.5, rnd()-0.5, rnd()-0.5))
L = rnd() * 100
B = tuple(x*L for x in b)
aB = dot(a, B)
cB = dot(c, B)
B2 = dot(B, B)
ac = dot(a, c)
sols = list(solutions(f, -1000., 1000.))
And there are cases in which the solutions are 0, 1, 2, 3 or 4. For example the problem
a = (-0.5900900304960981, 0.4717596600172049, 0.6551614908475357)
b = (-0.9831451620384042, -0.10306322574446096, 0.15100848274062748)
c = (-0.6250439408232388, 0.49902426033920616, -0.6002456660677057)
B = (-33.62793897729328, -3.5252208930692497, 5.165162011403056)
has four distinct solutions:
s = 57.3895941365 , t = -16.6969433689
A = (-33.865027354189415, 27.07409541837935, 37.59945205363035)
C = (10.436323283003153, -8.332179814593692, 10.022267893763457)
|A - B| = 44.5910029061
|C - B| = 44.5910029061
(A - B)·(C - B) = 1.70530256582e-13
s = 43.619078237 , t = 32.9673082734
A = (-25.739183207076163, 20.5777215193455, 28.577540327140607)
C = (-20.606016281518986, 16.45148662649085, -19.78848391300571)
|A - B| = 34.5155582156
|C - B| = 34.5155582156
(A - B)·(C - B) = 1.13686837722e-13
s = -47.5886624358 , t = 83.8222109697
A = (28.08159526800866, -22.450411211385674, -31.17825902887765)
C = (-52.39256507303229, 41.82931682916268, -50.313918854788845)
|A - B| = 74.0747844969
|C - B| = 74.0747844969
(A - B)·(C - B) = 4.54747350886e-13
s = 142.883074325 , t = 136.634726869
A = (-84.31387768560096, 67.4064705656035, 93.61148799140805)
C = (-85.40270813540043, 68.1840435123674, -82.01440263735996)
|A - B| = 124.189861967
|C - B| = 124.189861967
(A - B)·(C - B) = -9.09494701773e-13
Write two quadratic equations for lambda, mu unknowns (just above matrix forms).
Solve this system with paper, pen and head, or with any mathematical software like Maple, Mathematica, Matlab, Derive etc. You will have 4th order equation. It has closed-form solution - apply Ferrari or Kardano method and get real roots, find mu, lambda, then point coordinates.
I have this problem containing some inequations and requirement to minimize a value. After doing some research on the Internet, I came to conclusion that using Prolog might be the easiest way to solve it. However, I never used Prolog before, and I would hate to waste my time learning it just to discover that it is not the right tool for this job.
Please, if you know Prolog, take a look at this problem and tell me if Prolog is the right one. Or, if you know of some other language that is really suited for this.
a + b + c >= 100
d + e + f >= 50
g + h >= 30
if (8b + 2e + 7h > 620) then y = 0.8 else y = 1.0
if (d > 35) then x = 0.9 else x = 1.0
5xa + 8yb + 5c + 3xd + 2ye + 2f + 6xg + 7yh = w.
I need to find the values for a, b, c, d, e, f, g and h that minimize w.
Please note that the above is only an example. In real program, I would use up to 10000 variables and up to 20 if..then clauses. This rules out linear programming as an alternative technique because it would take a prohibitive amount of RAM and time to test all LP problems.
I'm not really asking for code, although I'd be grateful for some hint how to tackle this if Prolog is really good for it. Thanks.
You could have a look at Constraint Logic Programming, either CLP(R), CLP(Q) or CLP(FD).
Your problem can be encoded into CLP(R) as follows (I think):
:- use_module(library(clpr)).
test(sol([A,B,C,D,E,F,G,H], [X,Y,W])) :-
{A >=0, B >=0, C>=0, D>=0, E>=0, F>=0, G>=0, H>=0},
{A + B + C >= 100},
{D + E + F >= 50},
{G + H >= 30},
{5*X*A + 8*Y*B + 5*C + 3*X*D + 2*Y*E + 2*F + 6*X*G + 7*Y*H = W},
(({8*B + 2*E + 7*H > 620},{Y=0.8}) ; ({8*B + 2*E + 7*H =35},{X=0.9}) ; ({D=
Using SICStus Prolog, I get the following answer:
| ?- test(A).
A = sol([_A,0.0,_B,0.0,_C,_D,30.0,0.0],[1.0,1.0,780.0]),
{_A=100.0-_B},
{_C=50.0-_D},
{_B==0.0},
{_D>=0.0} ? ;
no
You can solve this using linear programming (LP), but the problem needs some modification before you can chuck it into an LP solver. An LP problem basically involves maximising or minimising a function given some constraints.
First, split the problem into two problems (as LP does not support the two if conditions you have):
Constraints:
a + b + c >= 100
d + e + f >= 50
g + h >= 30
8b + 2e + 7h > 620
Linear function:
5 * 0.8a + 8 * 1.0b + 5c + 3 * 0.8d + 2 * 1.0e + 2f + 6 * 0.8g + 7 * 1.0h = w
And
Constraints:
a + b + c >= 100
d + e + f >= 50
g + h >= 30
d > 35
Linear function:
5 * 1.0a + 8 * 0.9b + 5c + 3 * 1.0d + 2 * 0.9e + 2f + 6 * 1.0g + 7 * 0.9h = w
After you run both separately by the LP solver, the solution will come out with the values of a, b, c, d, e, f, g, h and w. Pick the smaller value of w and the corresponding values of a, b, c, d, e, f, g, h.
How does this work?
The two if conditions are effectively similar to the other constraints listed, just that they entail different values of x and y. Since the two conditions are mutually exclusive (given that both cannot be satisfied as x and y will hence have different values), you can split them into two separate LP problems. As a result, you solve the LP problems individually, and hence you will arrive at a minimised value of w.
For an LP solver, go to the linear programming Wikipedia article as linked above. There are tools like Excel and some others which are easier to use, but if you want a program, then there are numerical languages which are good at this, or general purpose languages like C that can do this with a library like glpk.
Hope this helps!
I haven't worked with similar problems before, so I can't give you any direct suggestions. However, I would not use Prolog for it. Prolog is really good for dealing with symbolic problems (a classic example would be the Einstein puzzle), but its math support is very awkward; it feels like math was tacked on as an afterthought.