1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/* Tensor package. This code intends to simplify tensorial expressions.
*/
/* functions internal to tensors */
RuleBase("Delta",{ind1,ind2});
RuleBase("TList",{head,tail});
RuleBase("TSum",{indices,body});
RuleBase("TD",{ind});
RuleBase("X",{ind});
/* And the simplificaiton rules for X, addition, subtraction
and multiplication */
10 # (TD(_i)X(_j)) <-- Delta(i,j);
10 # (TD(_i) ( (_f) + (_g) ) ) <-- (TD(i)f) + (TD(i)g);
10 # (TD(_i) ( (_f) - (_g) ) ) <-- (TD(i)f) - (TD(i)g);
10 # (TD(_i) ( - (_g) ) ) <-- - TD(i)g;
10 # (TD(_i) ( (_f) * (_g) ) ) <-- (TD(i)f)*g + f*(TD(i)g);
10 # (TD(_i) ( (_f) ^ (n_IsPositiveInteger) ) ) <-- n*(TD(i)f)*f^(n-1);
10 # (TD(_i)Delta(_j,_k)) <-- 0;
10 # (TD(_i)f_IsNumber) <-- 0;
/* The only TSum summation simplification: summing over no indices
means no summation. */
10 # (TSum({})(_body)) <-- body;
/* Explicit summation when Ndim is defined. This summation will
be invoked when using TExplicitSum. */
20 # (TSum(_indices)(_body))_(IsInteger(Ndim)) <--
LocalSymbols(index,i,sum)
[
Local(index,i,sum);
index:=indices[1];
sum:=0;
MacroLocal(index);
For(i:=1,i<=Ndim,i++)
[
MacroSet(index,i);
sum:=sum+Eval(TSum(Tail(indices))body);
];
sum;
];
/* TExplicitSum sets the dimension of the space under consideration,
so summation can proceed */
(TExplicitSum(Ndim_IsInteger)(_body)) <-- Eval(body);
/* Move the delta factors to the front, so they can be simplified
away. It uses ApplyDelta to move a factor either to the front
or to the back of the list. Input is a list of factors, as
returned by Flatten(expressions,"*")
*/
MoveDeltas(_list) <--
[
Local(result,i,nr);
result:={};
nr:=Length(list);
For(i:=1,i<=nr,i++)
[
ApplyDelta(result,list[i]);
];
result;
];
10 # ApplyDelta(_result,Delta(_i,_j)) <--
DestructiveInsert(result,1,Delta(i,j));
20 # ApplyDelta(_result,(_x) ^ (n_IsInteger))_(n>0) <--
[
Local(i);
For(i:=1,i<=n,i++)
[
ApplyDelta(result,x);
];
];
100 # ApplyDelta(_result,_term) <--
DestructiveAppend(result,term);
/* TSimplify : expand brackets, and send the expression of addition
of terms to TSimplifyAux */
TSimplify(TSum(_indices)(_f)) <--
[
TSimplifyAux(TSum(indices)ExpandBrackets(f));
];
/* TSimplifyAux : simplify each term independently */
10 # TSimplifyAux(TSum(_indices)((_f) + (_g))) <--
TSimplifyAux(TSum(FlatCopy(indices))(f)) +
TSimplifyAux(TSum(FlatCopy(indices))(g));
10 # TSimplifyAux(TSum(_indices)((_f) - (_g))) <--
TSimplifyAux(TSum(FlatCopy(indices))(f)) -
TSimplifyAux(TSum(FlatCopy(indices))(g));
10 # TSimplifyAux(TSum(_indices)( - (_g))) <--
- TSimplifyAux(TSum(indices)(g));
40 # TSimplifyAux(TSum(_indices)_body) <--
[
Local(flat);
/* Convert expressions of the form (a*b*c) to {a,b,c} */
flat:=Flatten(body,"*");
/* Move the deltas to the front. */
flat:=MoveDeltas(flat);
/* Simplify the deltas away (removing the required indices) */
flat:=TSumRest(flat);
/* Determine if there are indices the summand still depends on */
Local(varlist,independ,nrdims);
varlist:=VarList(flat);
independ:=Intersection(indices,varlist);
nrdims:=Length(indices)-Length(independ);
/* Return result, still summing over the indices not removed by deltas */
Ndim^nrdims*TSum(independ)flat;
];
/* Terminating condition for the tensorial simplification */
10 # TSumSimplify(TList(Delta(_ind,_ind),_list))_Contains(indices,ind) <--
[
/* Remove the index from the list of indices to sum over, since
it is now implicitly summed over by simplifying the delta */
DestructiveDelete(indices,Find(indices,ind));
/* Return result simplified for this delta */
Ndim*TSumRest(list);
];
11 # TSumSimplify(TList(Delta(_ind1,_ind2),_list))_
Contains(indices,ind2) <--
[
/* Remove the index from the list of indices to sum over, since
it is now implicitly summed over by simplifying the delta */
DestructiveDelete(indices,Find(indices,ind2));
/* Return result simplified for this delta */
TSumRest( Subst(ind2,ind1)list );
];
11 # TSumSimplify(TList(Delta(_ind1,_ind2),_list))_
Contains(indices,ind1) <--
[
/* Remove the index from the list of indices to sum over, since
it is now implicitly summed over by simplifying the delta */
DestructiveDelete(indices,Find(indices,ind1));
/* Return result simplified for this delta */
TSumRest( Subst(ind1,ind2)list );
];
1010 # TSumSimplify(TList(_term,_list)) <--
[
term*TSumRest(list);
];
10 # TSumRest({}) <-- 1;
20 # TSumRest(_list) <--
[
TSumSimplify(TList(Head(list),Tail(list)));
];
UnFence("TSumSimplify",1);
UnFence("TSumRest",1);
UnFence("TSum",2);
|