Solving a system of equations in Prolog

Suppose I have a number X and I want to solve a system of equations, for example Y + Z = X, Z * Y = 1.

Now it has solutions Y = 1 / Z and Z = (sqrt (X * X-4) + X) / 2 or (X- (sqrt (X * X-4))) / 2.

Therefore, I can write in Prolog:

solve(X,Y,Z):- Y is (sqrt(X*X-4)+X)/2, Z is 1/Y.
solve(X,Y,Z):- Y is (X-(sqrt(X*X-4)))/2,Z is 1/Y.

It works.

BUT

this requires most of the preliminary work on my part, in fact, its decision in advance and just ask Prolog to evaluate the answer.

Is there a way to get Z and Y without deciding X in advance?

I can't just write things like

solve(X,Y,Z):- X is Y+Z, Z is 1/Y.

due to an error creating the instance.

+5
source share
1 answer

I think that you will need CAS to symbolically solve the system as you "manually". Such a SW is not easy to find and build.

, (clprq) :

:- [library(clpr)].
solve(X,Y,Z) :- {Y+Z=X, Z*Y=1}.

?- solve(3,Y,Z).
{Z=3.0-Y, -1.0+Z*Y=0.0},
{-1.0+Z*Y=0.0},
{-1.0+Z*Y=0.0}.

?

+3

All Articles