Foad S. Farimani
Foad S. Farimani

Reputation: 14008

get the expression out of solve results

Cosider the simple solution:

 sol: solve(b * x - a, x);
     a
[x = -]
     b

how can I get the expression part sol: a / b out of the above result?

solution was offered to me here.

Upvotes: 1

Views: 505

Answers (2)

Foad S. Farimani
Foad S. Farimani

Reputation: 14008

Thanks to Johann Weilharter I found one way to extract the expression:

 sol: ev(x, solve(b * x - a, x)[1]);

Of course, if there is more than one solution, you need to change 1 to the specific instance.

Alternatively, as pointed out in the comments of the question, one can also use

sol: rhs(first(solve(b * x - a, x)));

oneliner to do the job.

Upvotes: 1

Kashish Goyal
Kashish Goyal

Reputation: 117

What you need is a symbolic evaluation library. If you are considering a python implementation, you can use SymPy.

import sympy as sym

x = sym.Symbol('x')
b = sym.Symbol('b')
a = sym.Symbol('a')
sol = sym.solve((b * x - a), x)
print(sol)

------
[a/b]

Upvotes: 0

Related Questions