Question

Assume I have a number X and I wish to solve system of equations, say Y+Z=X, Z*Y = 1.

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

So 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.

This works.

BUT

it requires a lot of preliminary work from my part, essentially solving it beforehand and just asking Prolog to evaluate the answer.

Is there some way I can get Z and Y, without solving X beforehand?

I cannot just write stuff like

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

because of instantiation error.

Was it helpful?

Solution

I think that you would need a CAS, to symbolically solve the system, as you did "by hand". Such SW is neither easy to find nor to build.

If a pragmatic approach can do it for you, library(clprq) could help:

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

yields

?- 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}.

does this make sense?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top