3

I'm using SWI-Prolog and I'm very new and still learning. Practically speaking, this question is a follow-up to Why can't my rule solve for X in a simple algebraic equation?

I have imported clpq.

?- use_module(library(clpq)).
true.

My equation is 6x2 + 7x - 3 = 0, in which x is literally -1.5 or also 1/3 if using the quadratic equation.

?- {(6 * X ^ 2) + (7 * X) - 3 = 0}.
{-3+7*X+6*X^2=0}.

?- {(6 * X ^ 2) + (7 * X) - 3 = 0}, R is float(X).
ERROR: is/2: Arguments are not sufficiently instantiated

Well, it reduced the equation slightly by changing the subtraction of 3 to the addition of -3, but it didn't solve for X. My first step was to try giving it the answer and seeing if it accepts it.

?- X is -1.5, (6 * X ^ 2) + (7 * X) - 3 =:= 0.
X = -1.5.

Hmm... everything looks good there. For completeness, I also ran the quadratic equations. ("Equations" plural because I guess there's no ± operator in Prolog. At least, I couldn't find one.)

?- A is 6, B is 7, C is -3, X is (-B + sqrt((B ^ 2) - (4 * A * C))) / (2 * A).
A = 6,
B = 7,
C = -3,
X = 0.3333333333333333.

?- A is 6, B is 7, C is -3, X is (-B - sqrt((B ^ 2) - (4 * A * C))) / (2 * A).
A = 6,
B = 7,
C = -3,
X = -1.5.

Okay, everything seems to check out. So, why couldn't cplq solve my equation?

Community
  • 1
  • 1

1 Answers1

1

CLPQ has limitations for non-linear constraints. For example, the non-linear constraint 6x2 + 7x - 3 = 0 can only be solved when a value for x is given, so CLPQ cannot solve quadratic or any other non-linear equations. Instead you could do:

Note that with clpr you could solve equations like:

?- {9 = X^2}.
X = 3.0 ;
X = -3.0 ;

CLPQ would give you:

?- {9 = X^2}.
{9-X^2=0}.

false.

For quadratic equations you could do (exactly as you example):

 solve(X^2 + P*X + Q = 0):-
         X = -(P/2) + ((P/2)^2 - Q)^(1/2);
         X = -(P/2) - ((P/2)^2 - Q)^(1/2).

with CLPQ (or CLPR):

?- solve(X^2+2*X+1=0).
X = - (2/2)+ ((2/2)^2-1)^ (1/2) ;
X = - (2/2)- ((2/2)^2-1)^ (1/2).
coder
  • 12,313
  • 5
  • 30
  • 49