7

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.

mat
  • 40,498
  • 3
  • 51
  • 78
Valtteri
  • 463
  • 1
  • 5
  • 11

1 Answers1

3

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?

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
CapelliC
  • 59,646
  • 5
  • 47
  • 90
  • 1
    Not really, to be honest, but I think that I could write a solver for second degree polynomial and then use that. But thanks anyway. – Valtteri Apr 12 '13 at 18:13
  • Why is such software difficult to find? Does it not exist? Does it not exist for Prolog? – Erik Kaplun Nov 07 '15 at 08:24
  • 1
    @ErikAllik: it's a fairly difficult topic, and I don't know of a site from where to download a CAS. At least in a book (google for The art of Prolog, by Sterling-Shapiro), a CAS was the subject of a chapter, maybe could be worth to implement as a starting point... – CapelliC Nov 07 '15 at 08:50
  • 1
    @ErikAllik: well, I didn't searched well enough, I found PRESS to [download](http://dream.inf.ed.ac.uk/software/press/press-src.tgz) – CapelliC Nov 07 '15 at 09:07
  • 1
    A better download link for PRESS is https://github.com/maths/PRESS (from https://stackoverflow.com/questions/13690136/im-curious-if-logic-programs-can-do-algebra) – Erik Kaplun Jan 25 '21 at 10:25