1 2 310 # Simplify(expr_IsList) <-- MapSingle("Simplify",expr); 4 515 # Simplify(Complex(_r,_i)) <-- Complex(Simplify(r),Simplify(i)); 6 720 # Simplify((_xex) == (_yex)) <-- (Simplify(xex-yex) == 0); 8 920 # Simplify((_xex) > (_yex)) <-- (Simplify(xex-yex) > 0); 1020 # Simplify((_xex) < (_yex)) <-- (Simplify(xex-yex) < 0); 1120 # Simplify((_xex) >= (_yex)) <-- (Simplify(xex-yex) >= 0); 1220 # Simplify((_xex) <= (_yex)) <-- (Simplify(xex-yex) <= 0); 1320 # Simplify((_xex) !== (_yex)) <-- (Simplify(xex-yex) !== 0); 14 15// conditionals 1625 # Simplify(if (_a) _b) <-- "if" @ {Simplify(a), Simplify(b)}; 1725 # Simplify(_a else _b) <-- "else" @ {Simplify(a), Simplify(b)}; 18 1950 # Simplify(_expr) <-- MultiSimp(Eval(expr)); 20 21 22Eliminate(_var,_replace,_function) <-- Simplify(Subst(var,replace)function); 23//ExpandBrackets(_xx) <-- SimpExpand(SimpImplode(SimpFlatten(xx))); 24ExpandBrackets(x) := NormalForm(MM(x)); 25 26Function("Flatten",{body,flattenoper}) 27[ 28 DoFlatten(body); 29]; 30 31RuleBase("DoFlatten",{doflattenx}); 32UnFence("DoFlatten",1); 33 3410 # DoFlatten(_doflattenx)_(Type(doflattenx)=flattenoper) <-- 35 Apply("Concat",MapSingle("DoFlatten",Tail(Listify(doflattenx)))); 3620 # DoFlatten(_doflattenx) <-- { doflattenx }; 37 38 3910 # UnFlatten({},_op,_identity) <-- identity; 4020 # UnFlatten(list_IsList,_op,_identity) <-- 41 Apply(op,{Head(list),UnFlatten(Tail(list),op,identity)}); 42 43 44RuleBase("SimpAdd",{x,y}); 45RuleBase("SimpMul",{x,y}); 46RuleBase("SimpDiv",{x,y}); 47 48 4910 # SimpFlatten((_x)+(_y)) <-- SimpAdd(SimpFlatten(x),SimpFlatten(y)); 5010 # SimpFlatten((_x)-(_y)) <-- SimpAdd(SimpFlatten(x),SimpMul(-1,SimpFlatten(y))); 5110 # SimpFlatten( -(_y)) <-- SimpMul(-1,SimpFlatten(y)); 52 5310 # SimpFlatten((_x)*(_y)) <-- SimpMul(SimpFlatten(x),SimpFlatten(y)); 5410 # SimpFlatten((_x)/(_y)) <-- SimpDiv(SimpFlatten(x),SimpFlatten(y)); 5510 # SimpFlatten((_x)^(n_IsPositiveInteger)) <-- 56 SimpMul(SimpFlatten(x),SimpFlatten(x^(n-1))); 57 58100 # SimpFlatten(_x) <-- 59[ 60 x; 61]; 62 6310 # SimpExpand(SimpAdd(_x,_y)) <-- SimpExpand(x) + SimpExpand(y); 6410 # SimpExpand(SimpMul(_x,_y)) <-- SimpExpand(x) * SimpExpand(y); 6510 # SimpExpand(SimpDiv(_x,_y)) <-- SimpExpand(x) / SimpExpand(y); 6620 # SimpExpand(_x) <-- x; 67 68/* Distributed multiplication rule */ 6910 # SimpImplode(SimpMul(SimpAdd(_x,_y),_z)) <-- 70 SimpImplode(SimpAdd(SimpImplode(SimpMul(x,z)), 71 SimpImplode(SimpMul(y,z)))); 7210 # SimpImplode(SimpMul(_z,SimpAdd(_x,_y))) <-- 73 SimpImplode(SimpAdd(SimpImplode(SimpMul(z,x)), 74 SimpImplode(SimpMul(z,y)))); 75/* Distributed division rule */ 7610 # SimpImplode(SimpDiv(SimpAdd(_x,_y),_z)) <-- 77 SimpImplode(SimpAdd(SimpImplode(SimpDiv(x,z)), 78 SimpImplode(SimpDiv(y,z)))); 79 80 81 8220 # SimpImplode(SimpAdd(_x,_y)) <-- 83 SimpAdd(SimpImplode(x),SimpImplode(y)); 8420 # SimpImplode(SimpMul(_x,_y)) <-- 85 SimpMul(SimpImplode(x),SimpImplode(y)); 8620 # SimpImplode(SimpDiv(_x,_y)) <-- 87 SimpDiv(SimpImplode(x),SimpImplode(y)); 8830 # SimpImplode(_x) <-- x; 89 90 91////////////////////////////////////////////////// 92/// ExpandFrac --- normalize rational functions (no simplification) 93////////////////////////////////////////////////// 94 955 # ExpandFrac(expr_IsList) <-- MapSingle("ExpandFrac", expr); 96 97// expression does not contain fractions 9810 # ExpandFrac(_expr)_Not(HasFuncSome(expr, "/", {Atom("+"), Atom("-"), *, /, ^})) <-- expr; 9915 # ExpandFrac(a_IsRationalOrNumber) <-- a; 10020 # ExpandFrac(_expr) <-- ExpandFrac'combine(GetNumerDenom(expr)); 101 102ExpandFrac'combine({_a, _b}) <-- a/b; 103 104/// GetNumerDenom(x) returns a pair of expressions representing normalized numerator and denominator; GetNumerDenom(x, a) multiplies the numerator by the number a 105GetNumerDenom(_expr, _a) <-- GetNumerDenom(expr)*{a,1}; 106 107// on expressions that are not fractions, we return unit denominator 10810 # GetNumerDenom(_expr)_Not(HasFuncSome(expr, "/", {Atom("+"), Atom("-"), *, /, ^})) <-- {expr, 1}; 109// rational numbers are not simplified 11015 # GetNumerDenom(a_IsRationalOrNumber) <-- {a, 1}; 111// arithmetic 11220 # GetNumerDenom(_a + _b) <-- ExpandFrac'add(GetNumerDenom(a), GetNumerDenom(b)); 11320 # GetNumerDenom(_a - _b) <-- ExpandFrac'add(GetNumerDenom(a), GetNumerDenom(b, -1)); 11420 # GetNumerDenom(- _a) <-- GetNumerDenom(a, -1); 11520 # GetNumerDenom(+ _a) <-- GetNumerDenom(a); 11620 # GetNumerDenom(_a * _b) <-- ExpandFrac'multiply(GetNumerDenom(a), GetNumerDenom(b)); 11720 # GetNumerDenom(_a / _b) <-- ExpandFrac'divide(GetNumerDenom(a), GetNumerDenom(b)); 118// integer powers 11920 # GetNumerDenom(_a ^ b_IsInteger)_(b > 1) <-- ExpandFrac'multiply(GetNumerDenom(a), GetNumerDenom(a^(b-1))); 12020 # GetNumerDenom(_a ^ b_IsInteger)_(b < -1) <-- ExpandFrac'divide(GetNumerDenom(1), GetNumerDenom(a^(-b))); 12120 # GetNumerDenom(_a ^ b_IsInteger)_(b = -1) <-- ExpandFrac'divide(GetNumerDenom(1), GetNumerDenom(a)); 122// non-integer powers are not considered to be rational functions 12325 # GetNumerDenom(_a ^ _b) <-- {a^b, 1}; 124 125// arithmetic on fractions; not doing any simplification here, whereas we might want to 126ExpandFrac'add({_a, _b}, {_c, _d}) <-- {a*d+b*c, b*d}; 127ExpandFrac'multiply({_a, _b}, {_c, _d}) <-- {a*c, b*d}; 128ExpandFrac'divide({_a, _b}, {_c, _d}) <-- {a*d, b*c}; 129 130