Expression transformation problem - c++

Let's say we have the following statement: s = 3 * a * b - 2 * c, where s, a, b and c are variables. Also, we used Shunting Yard algorithm to build RPN expression, so now we can assign values to variables a, b and c and calculate s value by using simple RPN evaluator.
But, the problem is that I should be able to calculate a value of any variable a, b or c when values of all other variables are set.
So, I need to transform existing expression somehow to get a set of expressions:
a = (s + 2 * c) / (3 * b)
b = (s + 2 * c) / (3 * a)
c = (3 * a * b - s) / 2
How can I generate such expressions on basis of one original statement? Is there any standard approaches for solving such problems?
Constraints:
A set of available operators: +, -, *, /, including unary + and -
operators *, / and = can't have the same variable on both sides (e.g. s = a * a, or s = a + s are not acceptable)
Thanks

See this first: Postfix notation to expression tree to convert your RPN into a tree.
Once you have the equation left expression = right expression change this to left expression - right expression = 0 and create a tree of left expression - right expression via Shunting Yard and the above answer. Thus when you evaluate the tree, you must get the answer as 0.
Now based on your restrictions, observe that if a variable (say x) is unknown, the resulting expression will always be of the form
(ax + b)/(cx + d) where a,b,c,d will depend on the other variables.
You can now recursively compute the expression as a tuple (a,b,c,d).
In the end, you will end up solving the linear equation
(ax + b)/(cx + d) = 0 giving x = -b/a
This way you don't have to compute separate expressions for each variable. One expression tree is enough. And given the other variables, you just recursively compute the tuple (a,b,c,d) and solve the linear equation in the end.
The (incomplete) pseudocode will be
TupleOrValue Eval (Tree t) {
if (!t.ContainsVariable) {
blah;
return value;
}
Tuple result;
if (t.Left.ContainsVariable) {
result = Eval(t.Left);
value = Eval(t.Right);
return Compose(t.Operator, result, value);
} else {
result = Eval(t.Right);
value = Eval(t.Left);
return Compose(t.Operator, result, value);
}
}
Tuple Compose(Operator op, Tuple t, Value v) {
switch (op) {
case 'PLUS': return new Tuple(t.a + v*t.c, t.b + v*t.d, t.c, t.d);
// (ax+b)/(cx+d) + v = ( (a + vc)x + b + dv)/(cx + d)
// blah
}
}
For an example, if the expression is x+y-z = 0. The tree will be
+
/ \
x -
/ \
y z
For y=5 and z=2.
Eval (t.Right) will return y-z = 3 as that subtree does not contain x.
Eval(t.Left) will return (1,0,0,1) which corresponds to (1x + 0)/(0x + 1). Note: the above pseudo-code is incomplete.
Now Compose of (1,0,0,1) with the value 3 will give (1 + 3*0, 0 + 3*1, 0, 1) = (1,3,0,1) which corresponds to (x + 3)/(0x + 1).
Now if you want to solve this you take x to be -b/a = -3/1 = -3
I will leave the original answer:
In general it will be impossible.
For instance consider the expression
x*x*x*x*x + a*x*x*x*x + b*x*x*x + c*x*x + d*x = e
Getting an expression for x basically corresponds to find the roots of the polynomial
x5 + ax4 + bx3 + cx2 + dx -e
which has been proven to be impossible in general, if you want to use +,-,/,* and nth roots. See Abel Ruffini Theorem.
Are there are some restrictions you forgot to mention, which might simplify the problem?

The basic answer is you have to apply algebra to the set of equations you have, to produce equations that you want.
In general, if you start with this symbolic equation:
s = 3 * a * b - 2 * c
and you add constraints for s, a, and c:
s = ...
a = ...
c = ...
you need to apply standard laws of algebra to rearrange the set of equations to produce what you want, in this case, a formula for b:
b = ...
If you add different constraints, you need the same laws of algebra, applied in different ways.
If your equations are all of the form (as your example is not) of
left_hand_side_variable_n = combination_of_variables
then you can use rules for solving simultaneous equations. For linear combinations, this is pretty straightforward (you learned how to do this high school). And you can even set up a standard matrix and solve using a standard solver package without doing algebra.
If the equations are not linear, then you may not be able to find a solution no matter how good your math is (see other answers for examples). To the extent it is possible to do so, you can use a computer algebra system (CAS) to manipulate formulas. They do so by representing the formulas essentially as [math] abstract syntax trees, not as RPN, and apply source-to-source transformation rules (you would call these "algebra rules" from high school). They usually have a pretty big set of built-in rules ("knowledge") already. Some CAS will attempt to solve such systems of equations for you using the built-in rules; others, you have to tell what sequence of algebraic laws to apply in what order.
You may also use a constraint solver, which is a special kind of computer algebra system focused only on answering the kind of question you've posed, e.g., "with this set of constraints, and specific values for variables, what is the value of other variables?". These are pretty good if your equations follows the forms they are designed to solve; otherwise no gaurantee but that's not surprising.
One can also use a program transformation system, which manipulate arbitrary "syntax trees", because algrebra trees are just a special case of syntax trees. To use such a system, you define the langauge to be manipulated (e.g., conventional algebra) and the rules by which it can be manipulated, and the order in which to apply the rules. [The teacher did exactly this for you in your intro algebra class, but not in such a formal way] Here's an example of a my program transformation system manipulating algebra. For equation solving, you want much the same rules, but a different order of application.
Finally, if you want to do this in a C++ program, either you have to simulate one of the above more general mechanisms (which is a huge amount of work), or you have narrow what you are willing to solve significantly (e.g., "linear combinations") so that you can take advantage of a much simpler algorithm.

There is a quite straight-forward one for very basic expressions (like in your example) where each variable occurs mostly once and every operator is binary. The algorithm is mostly what you would do by hand.
The variable we are looking for will be x in the following lines. Transform your expression into the form f(...,x,...) == g(...). Either variable x is already on the left hand side or you just switch both sides.
You now have two functions consisting of applications of binary operators to sub-expressions, i.e. f = o_1(e_1,e_2) where each e_i is either a variable, a number or another function e_i = o_i(e_j, e_k). Think of this as the binary tree representation where nodes are operators and leafs are variables or numbers. Same applies for g.
Yyou can apply the following algorithm (our goal is to transform the tree into one representing the expression x == h(...):
while f != x:
// note: f is not a variable but x is a subexpression of f
// and thus f has to be of the form binop(e_1, e_2)
if x is within e_1:
case f = e_1 + e_2: // i.e. e_1 + e_2 = g
g <- g - e_2
case f = e_1 - e_2: // i.e. e_1 - e_2 = g
g <- g + e_2
case f = e_1 * e_2: // i.e. e_1 * e_2 = g
g <- g / e_2
case f = e_1 / e_2: // i.e. e_1 / e_2 = g
g <- g * e_2
f <- e_1
else if x is within e_2:
case f = e_1 + e_2: // i.e. e_1 + e_2 = g
g <- g - e_2
case f = e_1 - e_2: // i.e. e_1 - e_2 = g
g <- g + e_2
case f = e_1 * e_2: // i.e. e_1 * e_2 = g
g <- g / e_2
case f = e_1 / e_2: // i.e. e_1 / e_2 = g
g <- g * e_2
f <- e_1
Now that f = x and f = g was saved during all steps we have x = g as solution.
In each step you ensure that x remains on the lhs and at the same time you reduce the depth of the lhs by one. Thus this algorithm will terminate after a finite amount of steps.
In your example (solve for b):
f = 3a*b*2c*- and g = s
f = 3a*b* and g = s2c*+
f = b and g = s2c*+3a*/
and thus b = (s + 2*c)/(3*a).
If you have more operators you can extend the algorithm but you might run into problems if they are not invertible.

Related

Problems when substituting a matrix in a polynomial

Example: let
M = Matrix([[1,2],[3,4]]) # and
p = Poly(x**3 + x + 1) # then
p.subs(x,M).expand()
gives the error :
TypeError: cannot add <class'sympy.matrices.immutable.ImmutableDenseMatrix'> and <class 'sympy.core.numbers.One'>
which is very plausible since the two first terms become matrices but the last term (the constant term) is not a matrix but a scalar. To remediate to this situation I changed the polynomial to
p = Poly(x**3 + x + x**0) # then
the same error persists. Am I obliged to type the expression by hand, replacing x by M? In this example the polynomial has only three terms but in reality I encounter (multivariate polynomials with) dozens of terms.
So I think the question is mainly revolving around the concept of Matrix polynomial:
(where P is a polynomial, and A is a matrix)
I think this is saying that the free term is a number, and it cannot be added with the rest which is a matrix, effectively the addition operation is undefined between those two types.
TypeError: cannot add <class'sympy.matrices.immutable.ImmutableDenseMatrix'> and <class 'sympy.core.numbers.One'>
However, this can be circumvented by defining a function that evaluates the matrix polynomial for a specific matrix. The difference here is that we're using matrix exponentiation, so we correctly compute the free term of the matrix polynomial a_0 * I where I=A^0 is the identity matrix of the required shape:
from sympy import *
x = symbols('x')
M = Matrix([[1,2],[3,4]])
p = Poly(x**3 + x + 1)
def eval_poly_matrix(P,A):
res = zeros(*A.shape)
for t in enumerate(P.all_coeffs()[::-1]):
i, a_i = t
res += a_i * (A**i)
return res
eval_poly_matrix(p,M)
Output:
In this example the polynomial has only three terms but in reality I encounter (multivariate polynomials with) dozens of terms.
The function eval_poly_matrix above can be extended to work for multivariate polynomials by using the .monoms() method to extract monomials with nonzero coefficients, like so:
from sympy import *
x,y = symbols('x y')
M = Matrix([[1,2],[3,4]])
p = poly( x**3 * y + x * y**2 + y )
def eval_poly_matrix(P,*M):
res = zeros(*M[0].shape)
for m in P.monoms():
term = eye(*M[0].shape)
for j in enumerate(m):
i,e = j
term *= M[i]**e
res += term
return res
eval_poly_matrix(p,M,eye(M.rows))
Note: Some sanity checks, edge cases handling and optimizations are possible:
The number of variables present in the polynomial relates to the number of matrices passed as parameters (the former should never be greater than the latter, and if it's lower than some logic needs to be present to handle that, I've only handled the case when the two are equal)
All matrices need to be square as per the definition of the matrix polynomial
A discussion about a multivariate version of the Horner's rule features in the comments of this question. This might be useful to minimize the number of matrix multiplications.
Handle the fact that in a Matrix polynomial x*y is different from y*x because matrix multiplication is non-commutative . Apparently poly functions in sympy do not support non-commutative variables, but you can define symbols with commutative=False and there seems to be a way forward there
About the 4th point above, there is support for Matrix expressions in SymPy, and that can help here:
from sympy import *
from sympy.matrices import MatrixSymbol
A = Matrix([[1,2],[3,4]])
B = Matrix([[2,3],[3,4]])
X = MatrixSymbol('X',2,2)
Y = MatrixSymbol('Y',2,2)
I = eye(X.rows)
p = X**2 * Y + Y * X ** 2 + X ** 3 - I
display(p)
p = p.subs({X: A, Y: B}).doit()
display(p)
Output:
For more developments on this feature follow #18555

SML operator and operand do not agree

I am trying to write my first function in sml. It takes a tuple and returns the sum of first element times 10, second element times 6 and the third, and then divides by 10. I don't know what I am doing wrong here I get this error operator and operand do not agree [tycon mismatch].
fun rgb2gray(rgb: (int*int*int))=
let
val x = (#1rgb * 3 )+ (#2rgb * 6 )+ (#3rgb)
in
x=x/10
end
x=x/10 is an equality comparison (and will only be true if x is zero), and / is for dividing reals, not integers.
(+, -, and * are overloaded, but / isn't.)
Integer division is called div, and since the value of the function should be x div 10, you only need to write x div 10, without any =.
It's more common to use pattern matching than selectors for deconstructing structures, and I would write your function like this:
fun rgb2gray (r, g, b) = (r * 3 + g * 6 + b) div 10
Since molbdnilo already provided an answer, here is an alternative way you can do this using records:
type rgb = { r : int, g : int, b : int }
fun rgb2gray (color : rgb) : int =
(#r color * 3 +
#g color * 6 +
#b color) div 10
or equivalently by pattern matching on records:
fun rgb2gray ({ r = r, g = g, b = b } : rgb) : int =
(r * 3 + g * 6 + b) div 10
Records are like tuples, but where their parts are named rather than numbered (hence #r instead of #1). The syntax is a bit more fiddly, but the upside is that you don't accidentally mix two colors up as easily. Perhaps for RGB values it's hard to mix them up anyway, since the notion of R, G and B in that exact order is quite ingrained into a lot of programmers. Still, this is another option.
Since it appears that others have already helped you solve the problem I thought that I would point out that after the end you need an ; after it since the function is done.

Range Update - Range Query using Fenwick Tree

http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224
For example being told that the value of the function or f (i) of the index-i is an i ^ k, for k> = 0 and always stay on this matter. Given query like the following:
Add value array [i], for all a <= i <= b as v Determine the total
array [i] f (i), for each a <= i <= b (remember the previous function
values ​​clarification)
To work on this matter, can be formed into Query (x) = m * g (x) - c,
where g (x) is f (1) + f (2) + ... + f (x).
To accomplish this, we
need to know the values ​​of m and c. For that, we need 2 separate
BIT. Observations below for each update in the form of ab v. To
calculate the value of m, virtually identical to the Range Update -
Point Query. We can get the following observations for each value of
i, which may be:
i <a, m = 0
a <= i <= b, m = v
b <i, m = 0
By using the following observation, it is clear that the Range Update - Point Query can be used on any of the BIT. To calculate the value of c, we need to observe the possibility for each value of i, which may be:
i <a, then c = 0
a <= i <= b, then c = v * g (a - 1)
b <i, c = v * (g (b) - g (a - 1))
Again, we need Range Update - Point Query, but in a different BIT.
Oiya, for a little help, I wrote the value of g (x) for k <= 3 yes: p:
k = 0 -> x
k = 1 -> x * (x + 1) / 2
k = 2 -> x * (x + 1) * (2x + 1) / 6
k = 3 -> (x * (x + 1) / 2) ^ 2
Now, example problem SPOJ - Horrible Queries . This problem is
similar issues that have described, with k = 0. Note also that
sometimes there is a matter that is quite extreme, where the function
is not for one type of k, but it could be some that polynomial shape!
Eg LA - Alien Abduction Again . To work on this problem, the solution
is, for each rank we make its BIT counter m respectively. BIT combined
to clear the counters c it was fine.
How can we used this concept if:
Given an array of integers A1,A2,…AN.
Given x,y: Add 1×2 to Ax, add 2×3 to Ax+1, add 3×4 to Ax+2, add 4×5 to
Ax+3, and so on until Ay.
Then return Sum of the range [Ax,Ay].

Optimization to find complex number as input

I am wondering if there is a C/C++ library or Matlab code technique to determine real and complex numbers using a minimization solver. Here is a code snippet showing what I would like to do. For example, suppose that I know Utilde, but not x and U variables. I want to use optimization (fminsearch) to determine x and U, given Utilde. Note that Utilde is a complex number.
x = 1.5;
U = 50 + 1i*25;
x0 = [1 20]; % starting values
Utilde = U * (1 / exp(2 * x)) * exp( 1i * 2 * x);
xout = fminsearch(#(v)optim(v, Utilde), x0);
function diff = optim(v, Utilde)
x = v(1);
U = v(2);
diff = abs( -(Utilde/U) + (1 / exp(2 * x)) * exp( 1i * 2 * x ) );
The code above does not converge to the proper values, and xout = 1.7318 88.8760. However, if U = 50, which is not a complex number, then xout = 1.5000 50.0000, which are the proper values.
Is there a way in Matlab or C/C++ to ensure proper convergence, given Utilde as a complex number? Maybe I have to change the code above?
If there isn't a way to do this natively in Matlab, then perhaps one
gist of the question is this: Is there a multivariate (i.e.
Nelder-Mead or similar algorithm) optimization library that is able
to work with real and complex inputs and outputs?
Yet another question is whether the function is convergent or not. I
don't know if it is the algorithm or the function. Might I need to change something in the Utilde = U * (1 / exp(2 * x)) * exp( 1i * 2 * x) expression to make it convergent?
The main problem here is that there is no unique solution to this optimization or parameter fitting problem. For example, looking at the expected and actual results above, Utilde is equivalent (ignoring round-off differences) for the two (x, U) pairs, i.e.
Utilde(x = 1.5, U = 50 + 25i) = Utilde(x = 1.7318, U = 88.8760)
Although I have not examined it in depth, I even suspect that for any value of x, you can find an U that computes to Utilde(x, U) = Utilde(x = 1.5, U = 50 + 25i).
The solution here would thus be to further constrain the parameter fitting problem so that the solver yields any solution that can be considered acceptable. Alternatively, reformulate Utilde to have a unique value for any (x, U) pair.
UPDATE, AUG 1
Given reasonable starting values, it actually seems like it is sufficient to restrict x to be real-valued. Performing unconstrained non-linear optimization using the diff function formulated above, I get the following result:
x = 1.50462926953244
U = 50.6977768845879 + 24.7676554234729i
diff = 3.18731710515855E-06
However, changing the starting guess to values more distant from the desired values does yield different solutions, so restricting x to be real-values does not alone provide a unique solution to the problem.
I have implemented this in C#, using the BOBYQA optimizer, but the numerics should be the same as above. If you want to try outside of Matlab, it should also be relatively simple to turn the C# code below into C++ code using the std::complex class and an (unconstrained) nonlinear C++ optimizer of your own choice. You could find some C++ compatible codes that do not require gradient computation here, and there is also various implementations available in Numerical Recipes. For example, you could access the C version of NR online here.
For reference, here are the relevant parts of my C# code:
class Program
{
private static readonly Complex Coeff = new Complex(-2.0, 2.0);
private static readonly Complex UTilde0 = GetUTilde(1.5, new Complex(50.0, 25.0));
static void Main(string[] args)
{
double[] vars = new[] {1.0, 25.0, 0.0}; // xstart = 1.0, Ustart = 25.0
BobyqaExitStatus status = Bobyqa.FindMinimum(GetObjfnValue, vars.Length, vars);
}
public static Complex GetUTilde(double x, Complex U)
{
return U * Complex.Exp(Coeff * x);
}
public static double GetObjfnValue(int n, double[] vars)
{
double x = vars[0];
Complex U = new Complex(vars[1], vars[2]);
return Complex.Abs(-UTilde0 / U + Complex.Exp(Coeff * x));
}
}
The documentation for fminsearch says how to deal with complex numbers in the limitations section:
fminsearch only minimizes over the real numbers, that is, x must only consist of real numbers and f(x) must only return real numbers. When x has complex variables, they must be split into real and imaginary parts.
You can use the functions real and imag to extract the real and imaginary parts, respectively.
It appears that there is no easy way to do this, even if both x and U are real numbers. The equation for Utilde is not well-posed for an optimization problem, and so it must be modified.
I've tried to code up my own version of the Nelder-Mead optimization algorithm, as well as tried Powell's method. Neither seem to work well for this problem, even when I attempted to modify these methods.

Moving out before brackets with XOR

If I had the sum of products like z*a + z*b + z*c + ... + z*y, it would be possible to move the z factor, which is the same, out before brackets: z(a + b + c + ... y).
I'd like to know how it is possible (if it is) to do the same trick if bitwise XOR is used instead of multiplication.
z^a + z^b + ... z^y -> z^(a + b + ... + y)
Perhaps a, b, c ... should be preprocessed, such as logically negated or something else, before adding? z could change, so preprocessing, if it's needed, shouldn't depend on particular z value.
From Wikipedia:
Distributivity: with no binary function, not even with itself
So, no, unfortunately, you can't do anything like that with XOR.
To prove that a general formula does not hold you only need to prove a contradiction in a limited case.
We can reduce it to show that this does not hold:
(a^b) * c = (a^c) * (b^c)
It is trivial to show that one base case fails as such:
a = 3
b = 1
c = 1
(a^b) * c = (3^1) * 1 = 2
(a^c) * (b^c) = 2 * 0 = 0
Using the same case you can show that (a*b) ^ c = (a^c) * (b^c) and (a + b) ^ c = (a^c) + (b^c) do not hold either.
Hence, equality does not hold in a general case.
Equality can hold in special cases though, which is an entirely different subject.