# Solve conditional term with mathematica [duplicate]

Solve mathematical conditional expressions with Mathematica, e.g if $$x+y=3$$ and $$xy=-1$$ , what is result $$\frac{x^3+y^3}{x^2+y^2}$$

The lazy way to do this is to evaluate

Solve[{z == (x^3 + y^3)/(x^2 + y^2), x + y == 3, x y == -1}, z, {x, y}]
{{z -> 36/11}}


The clever way to do this is to recognize that your rational function is symmetric, and is thus representable in terms of the symmetric polynomials:

Table[SymmetricPolynomial[k, {x, y}], {k, 2}]
{x + y, x y}


which means you can leverage SymmetricReduction[]:

expr = (x^3 + y^3)/(x^2 + y^2)
Total[SymmetricReduction[Numerator[expr], {x, y}, {s1, s2}]]/
Total[SymmetricReduction[Denominator[expr], {x, y}, {s1, s2}]]
(s1^3 - 3 s1 s2)/(s1^2 - 2 s2)


after which you can plug the specified values in:

% /. Thread[{s1, s2} -> {3, -1}]
36/11


(The Newton-Girard formulae are relevant here.)

The even more clever way is to recall Vieta's formulae

Collect[(t - x) (t - y), t, Simplify]
t^2 + t (-x - y) + x y


and recall that power sums of the form x^k + y^k satisfy a linear difference equation, with the recurrence coefficients being given by the symmetric expressions embodied in Vieta's formulae. Thus,

#2/#1 & @@ LinearRecurrence[{3 (* x + y *), 1 (* -x y *)},
{2 (* x^0 + y^0 *), 3 (* x^1 + y^1 *)},
{2, 3} + 1]
36/11


A simpler way is as follows.

Simplify[(x^3 + y^3)/(x^2 + y^2), {x + y == 3, x y == -1}]


36/11

We consider that a[n]=x^n + y^n. To deduce the recurrence relation we compare with x^(n + 2) + y^(n + 2) and x^(n + 1) + y^(n + 1) up to a factor x+y.

Simplify[x^(n + 2) + y^(n + 2) - (x + y) (x^(n + 1) + y^(n + 1)),
n ∈ PositiveIntegers]


-x y (x^n + y^n).

It means that

Simplify[
x^(n + 2) + y^(n + 2) == (x + y) (x^(n + 1) + y^(n + 1)) -
x y (x^n + y^n), n ∈ PositiveIntegers]


True.

When x+y==3,x y==-1, the recurrence relation a[n + 2] == 3 a[n + 1] + a[n], and a[1] == 3, a[0] == 2 hold.

RSolve[{a[n + 2] == 3 a[n + 1] + a[n], a[1] == 3, a[0] == 2}, a[n], n]


RecurrenceTable[{a[n + 2] == 3 a[n + 1] + a[n], a[1] == 3,
a[0] == 2}, a, {n, 2, 3}]


{11, 36}.

a[3]/a[2]==36/11.

• Since you bring up RSolve[], you could also note that RSolveValue[] can give the ratio at once: RSolveValue[{a[n + 2] == 3 a[n + 1] + a[n], a[1] == 3, a[0] == 2}, a[3]/a[2], n] // Simplify Commented Jul 22, 2022 at 23:43
• @J.M. Thanks for provide this skill. Commented Jul 22, 2022 at 23:46

One way could be to first solve for $$x,y$$ using Solve command, and then replace all the solutions into the expression.

Something like

ClearAll[x,y]
expr=(x^3+y^3)/(x^2+y^2);
eq1=x+y==3;
eq2=x*y==-1;
sol=Solve[{eq1,eq2},{x,y}]


expr/.sol


These are the same numerically

N[%]