% % DERIVATIVES + SIMPLIFICATION % % X' = 1 d( X, X, 1). % Y' = 0 if Y is number or atom different from X d( Y, X, 0) :- atomic(Y), Y \= X. % subsumed by the more general rule below % (C*Y)' = C*Y' if C is constant respect to X d( C*Y, X, C*Z ) :- d( C, X, 0 ), % C is constant wrt X <-> C'=0 !, % cut away all remaining choices to reduce bactrack d( Y, X, Z). % (A+B)' = A' + B' d( A+B, X, C+D ) :- d( A, X, B ), d( C, X, D). % subsumed by A+B rule if we canonicalize the espression % (A-B)' = A' - B' d( A-B, X, C-D ) :- d( A, X, B ), d( C, X, D). % (A*B)' = AB'+BA' d( A*B, X, A*D+B*C ) :- d(A, X, C), d(B, X, D). % % TASK: add other derivation rules % % (X^C)' = CX^(C-1) if C constant wrt X % % (F(G))'= F'(G)*G' % % (e^X)' = e^X % % ... % % TASK: apply simplification (see below) every time a derivation is applied? % % % TASK: simplification (one single step) % % X*1 = X % 1*X = X (**) simp(X*1, X). simp(1*X, X). % 0+X = X % X+0 = X (**) simp(X+0, X). simp(0+X, X). % X^0 = 1 % X^1 = X % X-X = 0 (*) % X+X = 2*X % C*D = CD simp( A*B, C ) :- number(A), number(B), C is A*B. % AX+BX = (A+B)*X % AX-BX = (A-B)*X (*) % X^2-Y^2 = (X-Y)*(X+Y) % % TASK: repeated simplification separated by single-step rules % one-step simplification should be repeated until there is no % more steps applicable, a new predicate could do it recursively simplify(X, Z) :- simp(X, Y), !, simplify(Y,Z). simplify(A+B, C+D) :- simplify(A, C), simplify(B, D). simplify(A*B, C*D) :- simplify(A, C), simplify(B, D). simplify(X, X). % % TASK: number canonicalization % integers/rationals AND floats (WHY TWO TYPES?) % - choose one type and convert the other to it % % TASK: subtraction canonicalization to reduce operators (*) % -X = (-1)*X % % TASK: normalization of expressions to reduce the number of rules % - commutative operations -> reorder the elements (or try both cases?) (**) % A*B = B*A % - distribution of * over +/- (try both cases -> infinite loop) % A*(B+C) => A*B + A*C % - associative operations -> reorder the operators or try all cases? % A*B*C = (A*B)*C = A*(B*C) % % TASK: monomial canonicalization (order the literals) % numeric_constant * literal part % % TASK: polynomial canonicalization (order monomials and collect similar monomials) %