40
41
42:- module(nf_r,
43 [
44 {}/1,
45 nf/2,
46 entailed/1,
47 split/3,
48 repair/2,
49 nf_constant/2,
50 wait_linear/3,
51 nf2term/2
52 ]). 53
54:- use_module('../clpqr/geler',
55 [
56 geler/3
57 ]). 58:- use_module(bv_r,
59 [
60 export_binding/2,
61 log_deref/4,
62 solve/1,
63 'solve_<'/1,
64 'solve_=<'/1,
65 'solve_=\\='/1
66 ]). 67:- use_module(ineq_r,
68 [
69 ineq_one/4,
70 ineq_one_s_p_0/1,
71 ineq_one_s_n_0/1,
72 ineq_one_n_p_0/1,
73 ineq_one_n_n_0/1
74 ]). 75:- use_module(store_r,
76 [
77 add_linear_11/3,
78 normalize_scalar/2
79 ]). 80:- use_module('../clpqr/highlight', []). 81:- use_module(library(error), [instantiation_error/1, type_error/2]). 82
83
84goal_expansion(geler(X,Y),geler(clpr,X,Y)).
85
87
95
96{Rel} :-
97 var(Rel),
98 !,
99 instantiation_error(Rel).
100{R,Rs} :-
101 !,
102 {R},{Rs}.
103{R;Rs} :-
104 !,
105 ({R};{Rs}). 106{L < R} :-
107 !,
108 nf(L-R,Nf),
109 submit_lt(Nf).
110{L > R} :-
111 !,
112 nf(R-L,Nf),
113 submit_lt(Nf).
114{L =< R} :-
115 !,
116 nf(L-R,Nf),
117 submit_le( Nf).
118{<=(L,R)} :-
119 !,
120 nf(L-R,Nf),
121 submit_le(Nf).
122{L >= R} :-
123 !,
124 nf(R-L,Nf),
125 submit_le(Nf).
126{L =\= R} :-
127 !,
128 nf(L-R,Nf),
129 submit_ne(Nf).
130{L =:= R} :-
131 !,
132 nf(L-R,Nf),
133 submit_eq(Nf).
134{L = R} :-
135 !,
136 nf(L-R,Nf),
137 submit_eq(Nf).
138{Rel} :-
139 type_error(clpr_constraint, Rel).
140
147
148entailed(C) :-
149 negate(C,Cn),
150 \+ {Cn}.
151
156
157negate(Rel,_) :-
158 var(Rel),
159 !,
160 throw(instantiation_error(entailed(Rel),1)).
161negate((A,B),(Na;Nb)) :-
162 !,
163 negate(A,Na),
164 negate(B,Nb).
165negate((A;B),(Na,Nb)) :-
166 !,
167 negate(A,Na),
168 negate(B,Nb).
169negate(A<B,A>=B) :- !.
170negate(A>B,A=<B) :- !.
171negate(A=<B,A>B) :- !.
172negate(A>=B,A<B) :- !.
173negate(A=:=B,A=\=B) :- !.
174negate(A=B,A=\=B) :- !.
175negate(A=\=B,A=:=B) :- !.
176negate(Rel,_) :-
177 type_error(clpr_constraint, Rel).
178
197
198submit_eq([]). 199submit_eq([T|Ts]) :-
200 submit_eq(Ts,T).
201submit_eq([],A) :- submit_eq_b(A). 202submit_eq([B|Bs],A) :- submit_eq_c(A,B,Bs). 203
207
209submit_eq_b(v(_,[])) :-
210 !,
211 fail.
213submit_eq_b(v(_,[X^P])) :-
214 var(X),
215 P > 0,
216 !,
217 export_binding(X,0.0).
219submit_eq_b(v(_,[NL^1])) :-
220 nonvar(NL),
221 nl_invertible(NL,X,0.0,Inv),
222 !,
223 nf(-Inv,S),
224 nf_add(X,S,New),
225 submit_eq(New).
227submit_eq_b(Term) :-
228 term_variables(Term,Vs),
229 geler(Vs,nf_r:resubmit_eq([Term])).
230
234
236submit_eq_c(v(I,[]),B,Rest) :-
237 !,
238 submit_eq_c1(Rest,B,I).
240submit_eq_c(A,B,Rest) :- 241 A = v(_,[X^1]),
242 var(X),
243 B = v(_,[Y^1]),
244 var(Y),
245 linear(Rest),
246 !,
247 Hom = [A,B|Rest],
248 249 nf_length(Hom,0,Len),
250 log_deref(Len,Hom,[],HomD),
251 solve(HomD).
253submit_eq_c(A,B,Rest) :-
254 Norm = [A,B|Rest],
255 term_variables(Norm,Vs),
256 geler(Vs,nf_r:resubmit_eq(Norm)).
257
261
265submit_eq_c1([], v(K,[X^P]), I) :-
266 var(X),
267 P =\= 0,
268 0 > (-I/K),
269 integer(P) =:= P,
270 1 =:= integer(P) mod 2,
271 !,
272 Val is -((I/K) ** (1/P)),
273 export_binding(X,Val).
277submit_eq_c1([], v(K,[X^P]), I) :-
278 var(X),
279 P =\= 0,
280 0 =< (-I/K),
281 integer(P) =:= P,
282 0 =:= integer(P) mod 2,
283 !,
284 Val is (-I/K) ** (1/P),
285 ( export_binding(X,Val)
286 ;
287 ValNeg is -Val,
288 export_binding(X, ValNeg)
289 ).
292submit_eq_c1([], v(K,[X^P]), I) :-
293 var(X),
294 P =\= 0,
295 0 =< (-I/K),
296 !,
297 Val is (-I/K) ** (1/P),
298 export_binding(X,Val).
300submit_eq_c1([], v(_K,[X^_P]), _I) :-
301 var(X),
302 !,
303 fail.
307submit_eq_c1([], v(K,[X^P]), I) :-
308 nonvar(X),
309 X = exp(_,_), 310 1 =:= abs(P),
311 0 >= I,
312 0 >= K,
313 !,
314 fail.
317submit_eq_c1([],v(K,[NL^P]),I) :-
318 nonvar(NL),
319 ( P =:= 1,
320 Y is -I/K
321 ; P =:= -1,
322 Y is -K/I
323 ),
324 nl_invertible(NL,X,Y,Inv),
325 !,
326 nf(-Inv,S),
327 nf_add(X,S,New),
328 submit_eq(New).
330submit_eq_c1(Rest,B,I) :-
331 B = v(_,[Y^1]),
332 var(Y),
333 linear(Rest),
334 !,
335 336 Hom = [B|Rest],
337 nf_length(Hom,0,Len),
338 normalize_scalar(I,Nonvar),
339 log_deref(Len,Hom,[],HomD),
340 add_linear_11(Nonvar,HomD,LinD),
341 solve(LinD).
343submit_eq_c1(Rest,B,I) :-
344 Norm = [v(I,[]),B|Rest],
345 term_variables(Norm,Vs),
346 geler(Vs,nf_r:resubmit_eq(Norm)).
347
349
353
355submit_lt([]) :- fail.
357submit_lt([A|As]) :- submit_lt(As,A).
358
362
364submit_lt([],v(K,P)) :- submit_lt_b(P,K).
366submit_lt([B|Bs],A) :- submit_lt_c(Bs,A,B).
367
371
373submit_lt_b([],I) :-
374 !,
375 I < -1.0e-10.
377submit_lt_b([X^1],K) :-
378 var(X),
379 !,
380 ( K > 1.0e-10
381 -> ineq_one_s_p_0(X) 382 ; ineq_one_s_n_0(X) 383 ).
385submit_lt_b(P,K) :-
386 term_variables(P,Vs),
387 geler(Vs,nf_r:resubmit_lt([v(K,P)])).
388
392
394submit_lt_c([],A,B) :-
395 A = v(I,[]),
396 B = v(K,[Y^1]),
397 var(Y),
398 !,
399 ineq_one(strict,Y,K,I).
401submit_lt_c(Rest,A,B) :-
402 Norm = [A,B|Rest],
403 ( linear(Norm)
404 -> 'solve_<'(Norm)
405 ; term_variables(Norm,Vs),
406 geler(Vs,nf_r:resubmit_lt(Norm))
407 ).
408
413
415submit_le([]).
417submit_le([A|As]) :- submit_le(As,A).
418
422
424submit_le([],v(K,P)) :- submit_le_b(P,K).
426submit_le([B|Bs],A) :- submit_le_c(Bs,A,B).
427
431
433submit_le_b([],I) :-
434 !,
435 I < 1.0e-10.
437submit_le_b([X^1],K) :-
438 var(X),
439 !,
440 ( K > 1.0e-10
441 -> ineq_one_n_p_0(X) 442 ; ineq_one_n_n_0(X) 443 ).
445submit_le_b(P,K) :-
446 term_variables(P,Vs),
447 geler(Vs,nf_r:resubmit_le([v(K,P)])).
448
452
454submit_le_c([],A,B) :-
455 A = v(I,[]),
456 B = v(K,[Y^1]),
457 var(Y),
458 !,
459 ineq_one(nonstrict,Y,K,I).
461submit_le_c(Rest,A,B) :-
462 Norm = [A,B|Rest],
463 ( linear(Norm)
464 -> 'solve_=<'(Norm)
465 ; term_variables(Norm,Vs),
466 geler(Vs,nf_r:resubmit_le(Norm))
467 ).
468
473
474submit_ne(Norm1) :-
475 ( nf_constant(Norm1,K)
476 -> \+ (K >= -1.0e-10, K =< 1.0e-10) 477 ; linear(Norm1)
478 -> 'solve_=\\='(Norm1)
479 ; term_variables(Norm1,Vs),
480 geler(Vs,nf_r:resubmit_ne(Norm1))
481 ).
482
486
487linear([]).
488linear(v(_,Ps)) :- linear_ps(Ps).
489linear([A|As]) :-
490 linear(A),
491 linear(As).
492
497
498linear_ps([]).
499linear_ps([V^1]) :- var(V). 500
505:- meta_predicate wait_linear( ?, ?, :). 507wait_linear(Term,Var,Goal) :-
508 nf(Term,Nf),
509 ( linear(Nf)
510 -> Var = Nf,
511 call(Goal)
512 ; term_variables(Nf,Vars),
513 geler(Vars,nf_r:wait_linear_retry(Nf,Var,Goal))
514 ).
518resubmit_eq(N) :-
519 repair(N,Norm),
520 submit_eq(Norm).
521resubmit_lt(N) :-
522 repair(N,Norm),
523 submit_lt(Norm).
524resubmit_le(N) :-
525 repair(N,Norm),
526 submit_le(Norm).
527resubmit_ne(N) :-
528 repair(N,Norm),
529 submit_ne(Norm).
530wait_linear_retry(Nf0,Var,Goal) :-
531 repair(Nf0,Nf),
532 ( linear(Nf)
533 -> Var = Nf,
534 call(Goal)
535 ; term_variables(Nf,Vars),
536 geler(Vars,nf_r:wait_linear_retry(Nf,Var,Goal))
537 ).
539
544
545nl_invertible(sin(X),X,Y,Res) :- Res is asin(Y).
546nl_invertible(cos(X),X,Y,Res) :- Res is acos(Y).
547nl_invertible(tan(X),X,Y,Res) :- Res is atan(Y).
548nl_invertible(exp(B,C),X,A,Res) :-
549 ( nf_constant(B,Kb)
550 -> A > 1.0e-10,
551 Kb > 1.0e-10,
552 TestKb is Kb - 1.0, 553 \+ (TestKb >= -1.0e-10, TestKb =< 1.0e-10),
554 X = C, 555 Res is log(A)/log(Kb)
556 ; nf_constant(C,Kc),
557 \+ (A >= -1.0e-10, A =< 1.0e-10), 558 Kc > 1.0e-10, 559 X = B, 560 Res is A**(1.0/Kc)
561 ).
562
564
571
573nf(X,Norm) :-
574 var(X),
575 !,
576 Norm = [v(1.0,[X^1])].
577nf(X,Norm) :-
578 number(X),
579 !,
580 nf_number(X,Norm).
582nf(#(Const),Norm) :-
583 monash_constant(Const,Value),
584 !,
585 Norm = [v(Value,[])].
587nf(-A,Norm) :-
588 !,
589 nf(A,An),
590 nf_mul_factor(v(-1.0,[]),An,Norm).
591nf(+A,Norm) :-
592 !,
593 nf(A,Norm).
595nf(A+B,Norm) :-
596 !,
597 nf(A,An),
598 nf(B,Bn),
599 nf_add(An,Bn,Norm).
600nf(A-B,Norm) :-
601 !,
602 nf(A,An),
603 nf(-B,Bn),
604 nf_add(An,Bn,Norm).
606nf(A*B,Norm) :-
607 !,
608 nf(A,An),
609 nf(B,Bn),
610 nf_mul(An,Bn,Norm).
611nf(A/B,Norm) :-
612 !,
613 nf(A,An),
614 nf(B,Bn),
615 nf_div(Bn,An,Norm).
617nf(Term,Norm) :-
618 nonlin_1(Term,Arg,Skel,Sa1),
619 !,
620 nf(Arg,An),
621 nf_nonlin_1(Skel,An,Sa1,Norm).
623nf(Term,Norm) :-
624 nonlin_2(Term,A1,A2,Skel,Sa1,Sa2),
625 !,
626 nf(A1,A1n),
627 nf(A2,A2n),
628 nf_nonlin_2(Skel,A1n,A2n,Sa1,Sa2,Norm).
630nf(Term,_) :-
631 type_error(clpr_expression, Term).
632
636
637nf_number(N,Res) :-
638 number(N),
639 ( (N >= -1.0e-10, N =< 1.0e-10) 640 -> Res = []
641 ; Res = [v(N,[])]
642 ).
643
644nonlin_1(abs(X),X,abs(Y),Y).
645nonlin_1(sin(X),X,sin(Y),Y).
646nonlin_1(cos(X),X,cos(Y),Y).
647nonlin_1(tan(X),X,tan(Y),Y).
648nonlin_2(min(A,B),A,B,min(X,Y),X,Y).
649nonlin_2(max(A,B),A,B,max(X,Y),X,Y).
650nonlin_2(exp(A,B),A,B,exp(X,Y),X,Y).
651nonlin_2(pow(A,B),A,B,exp(X,Y),X,Y). 652nonlin_2(A^B,A,B,exp(X,Y),X,Y).
653
654nf_nonlin_1(Skel,An,S1,Norm) :-
655 ( nf_constant(An,S1)
656 -> nl_eval(Skel,Res),
657 nf_number(Res,Norm)
658 ; S1 = An,
659 Norm = [v(1.0,[Skel^1])]).
660nf_nonlin_2(Skel,A1n,A2n,S1,S2,Norm) :-
661 ( nf_constant(A1n,S1),
662 nf_constant(A2n,S2)
663 -> nl_eval(Skel,Res),
664 nf_number(Res,Norm)
665 ; Skel=exp(_,_),
666 nf_constant(A2n,Exp),
667 integerp(Exp,I)
668 -> nf_power(I,A1n,Norm)
669 ; S1 = A1n,
670 S2 = A2n,
671 Norm = [v(1.0,[Skel^1])]
672 ).
673
675nl_eval(abs(X),R) :- R is abs(X).
676nl_eval(sin(X),R) :- R is sin(X).
677nl_eval(cos(X),R) :- R is cos(X).
678nl_eval(tan(X),R) :- R is tan(X).
681nl_eval(min(X,Y),R) :- R is min(X,Y).
682nl_eval(max(X,Y),R) :- R is max(X,Y).
683nl_eval(exp(X,Y),R) :- R is X**Y.
684
685monash_constant(X,_) :-
686 var(X),
687 !,
688 fail.
689monash_constant(p,3.14259265).
690monash_constant(pi,3.14259265).
691monash_constant(e,2.71828182).
692monash_constant(zero,1.0e-10).
693
697
698nf_constant([],0.0).
699nf_constant([v(K,[])],K).
700
708
709split([],[],0.0).
710split([First|T],H,I) :-
711 ( First = v(I,[])
712 -> H = T
713 ; I = 0.0,
714 H = [First|T]
715 ).
716
722
723nf_add([],Bs,Bs).
724nf_add([A|As],Bs,Cs) :- nf_add(Bs,A,As,Cs).
725
726nf_add([],A,As,Cs) :- Cs = [A|As].
727nf_add([B|Bs],A,As,Cs) :-
728 A = v(Ka,Pa),
729 B = v(Kb,Pb),
730 compare(Rel,Pa,Pb),
731 nf_add_case(Rel,A,As,Cs,B,Bs,Ka,Kb,Pa).
732
739nf_add_case(<,A,As,Cs,B,Bs,_,_,_) :-
740 Cs = [A|Rest],
741 nf_add(As,B,Bs,Rest).
742nf_add_case(>,A,As,Cs,B,Bs,_,_,_) :-
743 Cs = [B|Rest],
744 nf_add(Bs,A,As,Rest).
745nf_add_case(=,_,As,Cs,_,Bs,Ka,Kb,Pa) :-
746 Kc is Ka + Kb,
747 ( (Kc >= -1.0e-10, Kc =< 1.0e-10) 748 -> nf_add(As,Bs,Cs)
749 ; Cs = [v(Kc,Pa)|Rest],
750 nf_add(As,Bs,Rest)
751 ).
752
753nf_mul(A,B,Res) :-
754 nf_length(A,0,LenA),
755 nf_length(B,0,LenB),
756 nf_mul_log(LenA,A,[],LenB,B,Res).
757
758nf_mul_log(0,As,As,_,_,[]) :- !.
759nf_mul_log(1,[A|As],As,Lb,B,R) :-
760 !,
761 nf_mul_factor_log(Lb,B,[],A,R).
762nf_mul_log(2,[A1,A2|As],As,Lb,B,R) :-
763 !,
764 nf_mul_factor_log(Lb,B,[],A1,A1b),
765 nf_mul_factor_log(Lb,B,[],A2,A2b),
766 nf_add(A1b,A2b,R).
767nf_mul_log(N,A0,A2,Lb,B,R) :-
768 P is N>>1,
769 Q is N-P,
770 nf_mul_log(P,A0,A1,Lb,B,Rp),
771 nf_mul_log(Q,A1,A2,Lb,B,Rq),
772 nf_add(Rp,Rq,R).
773
774
776nf_add_2(Af,Bf,Res) :- 777 Af = v(Ka,Pa),
778 Bf = v(Kb,Pb),
779 compare(Rel,Pa,Pb),
780 nf_add_2_case(Rel,Af,Bf,Res,Ka,Kb,Pa).
781
782nf_add_2_case(<,Af,Bf,[Af,Bf],_,_,_).
783nf_add_2_case(>,Af,Bf,[Bf,Af],_,_,_).
784nf_add_2_case(=,_, _,Res,Ka,Kb,Pa) :-
785 Kc is Ka + Kb,
786 ( (Kc >= -1.0e-10, Kc =< 1.0e-10) 787 -> Res = []
788 ; Res = [v(Kc,Pa)]
789 ).
790
794nf_mul_k([],_,[]).
795nf_mul_k([v(I,P)|Vs],K,[v(Ki,P)|Vks]) :-
796 Ki is K*I,
797 nf_mul_k(Vs,K,Vks).
798
803nf_mul_factor(v(K,[]),Sum,Res) :-
804 !,
805 nf_mul_k(Sum,K,Res).
806nf_mul_factor(F,Sum,Res) :-
807 nf_length(Sum,0,Len),
808 nf_mul_factor_log(Len,Sum,[],F,Res).
809
815
816nf_mul_factor_log(0,As,As,_,[]) :- !.
817nf_mul_factor_log(1,[A|As],As,F,[R]) :-
818 !,
819 mult(A,F,R).
820nf_mul_factor_log(2,[A,B|As],As,F,Res) :-
821 !,
822 mult(A,F,Af),
823 mult(B,F,Bf),
824 nf_add_2(Af,Bf,Res).
825nf_mul_factor_log(N,A0,A2,F,R) :-
826 P is N>>1, 827 Q is N-P,
828 nf_mul_factor_log(P,A0,A1,F,Rp),
829 nf_mul_factor_log(Q,A1,A2,F,Rq),
830 nf_add(Rp,Rq,R).
831
835
836mult(v(Ka,La),v(Kb,Lb),v(Kc,Lc)) :-
837 Kc is Ka*Kb,
838 pmerge(La,Lb,Lc).
839
843
844pmerge([],Bs,Bs).
845pmerge([A|As],Bs,Cs) :- pmerge(Bs,A,As,Cs).
846
847pmerge([],A,As,Res) :- Res = [A|As].
848pmerge([B|Bs],A,As,Res) :-
849 A = Xa^Ka,
850 B = Xb^Kb,
851 compare(R,Xa,Xb),
852 pmerge_case(R,A,As,Res,B,Bs,Ka,Kb,Xa).
853
860
861pmerge_case(<,A,As,Res,B,Bs,_,_,_) :-
862 Res = [A|Tail],
863 pmerge(As,B,Bs,Tail).
864pmerge_case(>,A,As,Res,B,Bs,_,_,_) :-
865 Res = [B|Tail],
866 pmerge(Bs,A,As,Tail).
867pmerge_case(=,_,As,Res,_,Bs,Ka,Kb,Xa) :-
868 Kc is Ka + Kb,
869 ( Kc =:= 0
870 -> pmerge(As,Bs,Res)
871 ; Res = [Xa^Kc|Tail],
872 pmerge(As,Bs,Tail)
873 ).
874
878
880nf_div([],_,_) :-
881 !,
882 zero_division.
884nf_div([v(K,P)],Sum,Res) :-
885 !,
886 Ki is 1.0/K,
887 mult_exp(P,-1,Pi),
888 nf_mul_factor(v(Ki,Pi),Sum,Res).
889nf_div(D,A,[v(1.0,[(A/D)^1])]).
890
894zero_division :- fail. 895
900mult_exp([],_,[]).
901mult_exp([X^P|Xs],K,[X^I|Tail]) :-
902 I is K*P,
903 mult_exp(Xs,K,Tail).
910nf_power(N,Sum,Norm) :-
911 integer(N),
912 compare(Rel,N,0),
913 ( Rel = (<)
914 -> Pn is -N,
915 916 binom(Sum,Pn,Inorm),
917 nf_div(Inorm,[v(1.0,[])],Norm)
918 ; Rel = (>)
919 -> 920 binom(Sum,N,Norm)
921 ; Rel = (=)
922 -> 923 Norm = [v(1.0,[])]
924 ).
929nf_power_pos(1,Sum,Norm) :-
930 !,
931 Sum = Norm.
932nf_power_pos(N,Sum,Norm) :-
933 N1 is N-1,
934 nf_power_pos(N1,Sum,Pn1),
935 nf_mul(Sum,Pn1,Norm).
940binom(Sum,1,Power) :-
941 !,
942 Power = Sum.
943binom([],_,[]).
944binom([A|Bs],N,Power) :-
945 ( Bs = []
946 -> nf_power_factor(A,N,Ap),
947 Power = [Ap]
948 ; Bs = [_|_]
949 -> factor_powers(N,A,v(1.0,[]),Pas),
950 sum_powers(N,Bs,[v(1.0,[])],Pbs,[]),
951 combine_powers(Pas,Pbs,0,N,1,[],Power)
952 ).
953
954combine_powers([],[],_,_,_,Pi,Pi).
955combine_powers([A|As],[B|Bs],L,R,C,Pi,Po) :-
956 nf_mul(A,B,Ab),
957 nf_mul_k(Ab,C,Abc),
958 nf_add(Abc,Pi,Pii),
959 L1 is L+1,
960 R1 is R-1,
961 C1 is C*R//L1,
962 combine_powers(As,Bs,L1,R1,C1,Pii,Po).
963
964nf_power_factor(v(K,P),N,v(Kn,Pn)) :-
965 Kn is K**N,
966 mult_exp(P,N,Pn).
967
968factor_powers(0,_,Prev,[[Prev]]) :- !.
969factor_powers(N,F,Prev,[[Prev]|Ps]) :-
970 N1 is N-1,
971 mult(Prev,F,Next),
972 factor_powers(N1,F,Next,Ps).
973sum_powers(0,_,Prev,[Prev|Lt],Lt) :- !.
974sum_powers(N,S,Prev,L0,Lt) :-
975 N1 is N-1,
976 nf_mul(S,Prev,Next),
977 sum_powers(N1,S,Next,L0,[Prev|Lt]).
978
980repair(Sum,Norm) :-
981 nf_length(Sum,0,Len),
982 repair_log(Len,Sum,[],Norm).
983repair_log(0,As,As,[]) :- !.
984repair_log(1,[v(Ka,Pa)|As],As,R) :-
985 !,
986 repair_term(Ka,Pa,R).
987repair_log(2,[v(Ka,Pa),v(Kb,Pb)|As],As,R) :-
988 !,
989 repair_term(Ka,Pa,Ar),
990 repair_term(Kb,Pb,Br),
991 nf_add(Ar,Br,R).
992repair_log(N,A0,A2,R) :-
993 P is N>>1,
994 Q is N-P,
995 repair_log(P,A0,A1,Rp),
996 repair_log(Q,A1,A2,Rq),
997 nf_add(Rp,Rq,R).
998
999repair_term(K,P,Norm) :-
1000 length(P,Len),
1001 repair_p_log(Len,P,[],Pr,[v(1.0,[])],Sum),
1002 nf_mul_factor(v(K,Pr),Sum,Norm).
1003
1004repair_p_log(0,Ps,Ps,[],L0,L0) :- !.
1005repair_p_log(1,[X^P|Ps],Ps,R,L0,L1) :-
1006 !,
1007 repair_p(X,P,R,L0,L1).
1008repair_p_log(2,[X^Px,Y^Py|Ps],Ps,R,L0,L2) :-
1009 !,
1010 repair_p(X,Px,Rx,L0,L1),
1011 repair_p(Y,Py,Ry,L1,L2),
1012 pmerge(Rx,Ry,R).
1013repair_p_log(N,P0,P2,R,L0,L2) :-
1014 P is N>>1,
1015 Q is N-P,
1016 repair_p_log(P,P0,P1,Rp,L0,L1),
1017 repair_p_log(Q,P1,P2,Rq,L1,L2),
1018 pmerge(Rp,Rq,R).
1019
1020repair_p(Term,P,[Term^P],L0,L0) :- var(Term).
1021repair_p(Term,P,[],L0,L1) :-
1022 nonvar(Term),
1023 repair_p_one(Term,TermN),
1024 nf_power(P,TermN,TermNP),
1025 nf_mul(TermNP,L0,L1).
1031repair_p_one(Term,TermN) :-
1032 nf_number(Term,TermN), 1033 !.
1034repair_p_one(A1/A2,TermN) :-
1035 repair(A1,A1n),
1036 repair(A2,A2n),
1037 !,
1038 nf_div(A2n,A1n,TermN).
1039repair_p_one(Term,TermN) :-
1040 nonlin_1(Term,Arg,Skel,Sa),
1041 repair(Arg,An),
1042 !,
1043 nf_nonlin_1(Skel,An,Sa,TermN).
1044repair_p_one(Term,TermN) :-
1045 nonlin_2(Term,A1,A2,Skel,Sa1,Sa2),
1046 repair(A1,A1n),
1047 repair(A2,A2n),
1048 !,
1049 nf_nonlin_2(Skel,A1n,A2n,Sa1,Sa2,TermN).
1050repair_p_one(Term,TermN) :-
1051 nf(Term,TermN).
1052
1053nf_length([],Li,Li).
1054nf_length([_|R],Li,Lo) :-
1055 Lii is Li+1,
1056 nf_length(R,Lii,Lo).
1061
1063nf2term([],0.0).
1065nf2term([F|Fs],T) :-
1066 f02t(F,T0), 1067 yfx(Fs,T0,T). 1068
1069yfx([],T0,T0).
1070yfx([F|Fs],T0,TN) :-
1071 fn2t(F,Ft,Op),
1072 T1 =.. [Op,T0,Ft],
1073 yfx(Fs,T1,TN).
1074
1079f02t(v(K,P),T) :-
1080 ( 1081 P = []
1082 -> T = K
1083 ; TestK is K - 1.0, 1084 (TestK >= -1.0e-10, TestK =< 1.0e-10)
1085 -> p2term(P,T)
1086 ; TestK is K + 1.0, 1087 (TestK >= -1.0e-10, TestK =< 1.0e-10)
1088 -> T = -Pt,
1089 p2term(P,Pt)
1090 ; T = K*Pt,
1091 p2term(P,Pt)
1092 ).
1093
1098fn2t(v(K,P),Term,Op) :-
1099 ( TestK is K - 1.0, 1100 (TestK >= -1.0e-10, TestK =< 1.0e-10)
1101 -> Term = Pt,
1102 Op = +
1103 ; TestK is K + 1.0, 1104 (TestK >= -1.0e-10, TestK =< 1.0e-10)
1105 -> Term = Pt,
1106 Op = -
1107 ; K < -1.0e-10 1108 -> Kf is -K,
1109 Term = Kf*Pt,
1110 Op = -
1111 ; 1112 Term = K*Pt,
1113 Op = +
1114 ),
1115 p2term(P,Pt).
1116
1118p2term([X^P|Xs],Term) :-
1119 ( Xs = []
1120 -> pe2term(X,Xt),
1121 exp2term(P,Xt,Term)
1122 ; Xs = [_|_]
1123 -> Term = Xst*Xtp,
1124 pe2term(X,Xt),
1125 exp2term(P,Xt,Xtp),
1126 p2term(Xs,Xst)
1127 ).
1128
1130exp2term(1,X,X) :- !.
1131exp2term(-1,X,1.0/X) :- !.
1132exp2term(P,X,Term) :-
1133 1134 Term = X^P.
1135
1136pe2term(X,Term) :-
1137 var(X),
1138 Term = X.
1139pe2term(X,Term) :-
1140 nonvar(X),
1141 X =.. [F|Args],
1142 pe2term_args(Args,Argst),
1143 Term =.. [F|Argst].
1144
1145pe2term_args([],[]).
1146pe2term_args([A|As],[T|Ts]) :-
1147 nf2term(A,T),
1148 pe2term_args(As,Ts).
1149
1156
1157transg(resubmit_eq(Nf)) -->
1158 {
1159 nf2term([],Z),
1160 nf2term(Nf,Term)
1161 },
1162 [clpr:{Term=Z}].
1163transg(resubmit_lt(Nf)) -->
1164 {
1165 nf2term([],Z),
1166 nf2term(Nf,Term)
1167 },
1168 [clpr:{Term<Z}].
1169transg(resubmit_le(Nf)) -->
1170 {
1171 nf2term([],Z),
1172 nf2term(Nf,Term)
1173 },
1174 [clpr:{Term=<Z}].
1175transg(resubmit_ne(Nf)) -->
1176 {
1177 nf2term([],Z),
1178 nf2term(Nf,Term)
1179 },
1180 [clpr:{Term=\=Z}].
1181transg(wait_linear_retry(Nf,Res,Goal)) -->
1182 {
1183 nf2term(Nf,Term)
1184 },
1185 [clpr:{Term=Res},Goal].
1186
1187integerp(X) :-
1188 floor(X)=:=X.
1189
1190integerp(X,I) :-
1191 floor(X)=:=X,
1192 I is integer(X).
1193
1194 1197:- multifile
1198 sandbox:safe_primitive/1. 1199
1200sandbox:safe_primitive(nf_r:{_}).
1201sandbox:safe_primitive(nf_r:entailed(_))