这是我第一次使用,clpr
所以如果这对你没有帮助,我恳求精神错乱,但对我来说,这里的关键似乎是dump/3
用来将约束转换回 Prolog 表达式,然后像任何其他结构一样遍历它。所以我通过这样做再次获得约束:
?- relation(independents([var(x1,X1),var(x2,X2),var(x3,X3)]),
dependent(var(y,Y))),
dump([X1,X2,X3,Y],[x1,x2,x3,y], [y=Eqn]).
Eqn = 3.0+0.5*x1+0.6*x2+0.7*x3
我认为值得记住这在引擎盖下的样子write_canonical
:
+(+(+(3.0,*(0.5,x1)),*(0.6,x2)),*(0.7,x3))
遍历多项式应该只涉及几个简单的情况;以下实际上可能是矫枉过正:
coefficient(X=Y, Var, Coeff) :-
coefficient(X, Var, Coeff) ; coefficient(Y, Var, Coeff).
coefficient(X+Y, Var, Coeff) :-
coefficient(X, Var, Coeff) ; coefficient(Y, Var, Coeff).
coefficient(X-Y, Var, Coeff) :-
coefficient(X, Var, Coeff) ; coefficient(Y, Var, Coeff).
coefficient(X*Y, X, Y) :-
atomic(X), atomic(Y).
coefficient(X*Y, Var, Coeff) :-
coefficient(X, Var, Coeff) ; coefficient(Y, Var, Coeff).
您的基本情况实际上是 X*Y 情况,它们都是原子的。其余的子句实际上只是为了展开嵌套。这似乎可以满足您的要求:
?- relation(independents([var(x1,X1),var(x2,X2),var(x3,X3)]),
dependent(var(y,Y))),
dump([X1,X2,X3,Y],[x1,x2,x3,y], [y=Eqn]),
coefficient(Eqn, Var, Coeff).
Eqn = 3.0+0.5*x1+0.6*x2+0.7*x3,
Var = 0.5,
Coeff = x1,
{Y=3.0+0.5*X1+0.6*X2+0.7*X3} ;
Eqn = 3.0+0.5*x1+0.6*x2+0.7*x3,
Var = 0.6,
Coeff = x2,
{Y=3.0+0.5*X1+0.6*X2+0.7*X3} ;
Eqn = 3.0+0.5*x1+0.6*x2+0.7*x3,
Var = 0.7,
Coeff = x3,
{Y=3.0+0.5*X1+0.6*X2+0.7*X3} ;
false.
要真正概括这一点,您可能需要使用maplist
et。人。将您的独立/受抚养人列表转换为您需要传递给的变量dump/3
,然后处理结果中有多个方程式的情况,但我认为这对您来说不会很有挑战性。
希望这可以帮助!