forked from duetosymmetry/TraceFree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTraceFree.m
248 lines (146 loc) · 11.5 KB
/
TraceFree.m
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
(* ::Package:: *)
(************************************************************************)
(* This file was generated automatically by the Mathematica front end. *)
(* It contains Initialization cells from a Notebook file, which *)
(* typically will have the same name as this file except ending in *)
(* ".nb" instead of ".m". *)
(* *)
(* This file is intended to be loaded into the Mathematica kernel using *)
(* the package loading commands Get or Needs. Doing so is equivalent *)
(* to using the Evaluate Initialization Cells menu command in the front *)
(* end. *)
(* *)
(* DO NOT EDIT THIS FILE. This entire file is regenerated *)
(* automatically each time the parent Notebook file is saved in the *)
(* Mathematica front end. Any changes you make to this file will be *)
(* overwritten. *)
(************************************************************************)
xAct`TraceFree`$xTensorVersionExpected={"1.0.5",{2013,1,27}};
xAct`TraceFree`$Version={"0.1.0",{2013,11,12}}
(* TraceFree: Make tensors trace-free at definition time *)
(* Copyright (C) 2013 Leo C. Stein *)
(* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License,or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place-Suite 330, Boston, MA 02111-1307, USA.
*)
(* :Title: TraceFree *)
(* :Author: Leo C. Stein *)
(* :Summary: Make tensors trace-free at definition time. *)
(* :Brief Discussion: TraceFree is a package to demonstrate using xTension. It adds an option, TraceFree, to DefTensor to declare a tensor trace-free at the time of definition. *)
(* :Context: xAct`TraceFree` *)
(* :Package Version: 0.1.0 *)
(* :Copyright: Leo C. Stein (2013) *)
(* :History: TODO *)
(* :Keywords: TODO *)
(* :Source: TraceFree.nb *)
(* :Warning: TODO *)
(* :Mathematica Version: 8.0 and later *)
(* :Limitations: *)
(* :Acknowledgements: *)
If[Unevaluated[xAct`xCore`Private`$LastPackage]===xAct`xCore`Private`$LastPackage,xAct`xCore`Private`$LastPackage="xAct`TraceFree`"];
BeginPackage["xAct`TraceFree`",{"xAct`xTensor`","xAct`xPerm`","xAct`xCore`"}]
If[Not@OrderedQ@Map[Last,{$xTensorVersionExpected,xAct`xTensor`$Version}],Throw@Message[General::versions,"xTensor",xAct`xTensor`$Version,$xTensorVersionExpected]]
Print[xAct`xCore`Private`bars]
Print["Package xAct`TraceFree` version ",$Version[[1]],", ",$Version[[2]]];
Print["Copyright (C) 2013, Leo C. Stein, under the General Public License."];
Off[General::shdw]
xAct`xForm`Disclaimer[]:=Print["These are points 11 and 12 of the General Public License:\n\nBECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM `AS IS\.b4 WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n\nIN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES."]
On[General::shdw]
If[xAct`xCore`Private`$LastPackage==="xAct`TraceFree`",
Unset[xAct`xCore`Private`$LastPackage];
Print[xAct`xCore`Private`bars];
Print["These packages come with ABSOLUTELY NO WARRANTY; for details type Disclaimer[]. This is free software, and you are welcome to redistribute it under certain conditions. See the General Public License for details."];
Print[xAct`xCore`Private`bars]]
TraceFree::usage="TraceFree is an option for DefTensor to declare a tensor trace-free. Use TraceFree->{{{a,b},metricg},...} to declare that the contraction using metricg on indices (-a,-b) vanishes. Use delta for a trace without metric. See TraceFreeRules for usage details.";
TraceFreeRules::usage="TraceFreeRules[tensor[inds],{a,b},met] returns a list of rules which make tensor[inds] tracefree on indices {a,b} using metric met for contraction. a,b must both appear in inds and be from the same VBundle. met can either be a metric, inverse metric, or delta. Other input forms:
TraceFreeRules[tensor[inds],{{{a1,b1},met1},{{a2,b2},met2},...}] returns the Union of all rules from each metric-and-index-pair.
TraceFreeRules[tensor[inds],{a,b,...},met] makes tensor tracefree on every pair of indices from {a,b,...}.
TraceFreeRules[tensor[inds],{a,b},{met1,met2,...}] uses every metric in the list on the set of indices {a,b}. Can be combined with above.
TraceFreeRules[tensor[inds],{a,...}] uses the First metric from the VBundle of {a,...}.";
SetTraceFreeRules::usage="SetTraceFreeRules[tensor[inds],arguments...] uses AutomaticRules to make the rules from TraceFreeRules[tensor[inds],arguments] automatic. See TraceFreeRules.";
Begin["`Private`"]
(****************************** 2. Main code for TraceFree *****************************)
FirstMetricQ[met_?MetricQ]:=met===First@MetricsOfVBundle@VBundleOfMetric@met;
InverseMetricQ[x_?xTensorQ]:=Length@TensorID[x]>0&&First@TensorID[x]===xAct`xTensor`Private`InvMetric;
InverseMetricQ[delta]:=False; (* This is a bug in xTensor 1.0.5 and earlier: TensorID@delta Throws a Message *)
InverseMetricQ[_]:=False;
Inv[imet_?InverseMetricQ]:=MasterOf@imet;
IndexPos[list_List,ind_]:=Position[list,ind,{1},Heads->False]
ALIndexQ[x_]:=AIndexQ[x]||LIndexQ[x]
NoDummiesQ[tensor_[inds___?ALIndexQ]]:=IndicesOf[Dummy][tensor[inds]]===IndexList[]
NoDummiesQ[_]:=False;
HasDummiesQ[x_]:=!NoDummiesQ[x]
MakeLIPatternRule={LI[x_]:>LI[_]};
TraceFree::dummies="Tensor must be given with no dummies (no internal contractions).";
TraceFree::sameVBinds="Indices `1` and `2` are not on the same VBundle.";
TraceFree::deltaCharacter="When using delta, exactly one index must be up and one index down.";
TraceFree::sameVBmet="Indices `1` and `2` are not from the VBundle of metric `3`.";
TraceFree::doesntappear="Index `1` does not appear in `2`.";
CheckMetInds[{a_?AIndexQ,b_?AIndexQ},met_?MetricQ]:=If[VBundleOfIndex[a]!=VBundleOfMetric[met],Throw@Message[TraceFree::sameVBmet,a,b,met]];;
CheckMetInds[{a_?AIndexQ,b_?AIndexQ},met_?InverseMetricQ]:=CheckMetInds[{a,b},Inv@met];
CheckMetInds[{a_?AIndexQ,b_?AIndexQ},delta]:=If[(DownIndexQ[a]&&DownIndexQ[b])||(UpIndexQ[a]&&UpIndexQ[b]),Throw@Message[TraceFree::deltaCharacter]];
CheckTraceFreeRules[tensor_[inds___?ALIndexQ],{a_?AIndexQ,b_?AIndexQ},met:_?MetricQ|_?InverseMetricQ|delta]:=Block[{},
If[HasDummiesQ[tensor[inds]],
Throw@Message[TraceFree::dummies]];
If[VBundleOfIndex[a]!=VBundleOfIndex[b],
Throw@Message[TraceFree::sameVBinds,a,b]];
If[Length@IndexPos[{inds},a]==0,
Throw@Message[TraceFree::doesntappear,a,tensor[inds]]];
If[Length@IndexPos[{inds},b]==0,
Throw@Message[TraceFree::doesntappear,b,tensor[inds]]];
CheckMetInds[{a,b},met];
];
TraceFreeRules[tens_,{inds__?AIndexQ}]/;(Length[{inds}]>=2)&&(1==Length@Union[VBundleOfIndex/@{inds}]):=Module[{metrics=MetricsOfVBundle@VBundleOfIndex@First@{inds}},TraceFreeRules[tens,{inds},First@metrics]/;(Length@metrics>=1)]
TraceFreeRules[tens_,{{inds__?AIndexQ},met_}]:=TraceFreeRules[tens,{inds},met];
TraceFreeRules[tens_,arguments:{{_,_}..}]:=Union@@(TraceFreeRules[tens,#]&/@arguments);
TraceFreeRules[tens_,slots_,metrics_List]:=Union@@(TraceFreeRules[tens,slots,#]&/@metrics)
TraceFreeRules[tens_,slots_List?(Length[#]>2&),met_]:=Union@@(TraceFreeRules[tens,#,met]&/@Subsets[slots,{2}])
TraceFreeRules[tensor_?xTensorQ[inds___?ALIndexQ],{a_?AIndexQ,b_?AIndexQ},met:_?MetricQ|_?InverseMetricQ|delta]:=
With[{tensWithLIPats=tensor[inds]/.MakeLIPatternRule},
(* Checks *)
CheckTraceFreeRules[tensor[inds],{a,b},met];
(* Except in the case of delta, we need rules with met appearing explicitly. *)
With[{metExplicitRules=If[met===delta,{},MakeRule[{tensWithLIPats met[ChangeIndex@a,ChangeIndex@b],0},PatternIndices->All,MetricOn->All,Evaluate->False,UseSymmetries->True]]},
(* If we have a metric or not, generate rules without met (whether met is delta or not, this is handled the same with ContractMetric *)
With[{contractedRules=MakeRule[{Evaluate@ContractMetric[tensWithLIPats met[ChangeIndex@a,ChangeIndex@b]],0},PatternIndices->All,MetricOn->All,Evaluate->False,UseSymmetries->True]},
(* We can also slightly simplify the rules because we know their structure *)
(* This is the final result *)
Join[metExplicitRules,contractedRules]/.(x_:>_):>(x:>0)//Union
]
]
];
SetTraceFreeRules[tens_,rest__]:=With[{subhead=SubHead@tens},
AutomaticRules[subhead,TraceFreeRules[tens,rest]]]
If[FreeQ[First/@Options[DefTensor],TraceFree],
Unprotect[DefTensor];
Options[DefTensor]=Append[Options[DefTensor],TraceFree->{}];
Protect[DefTensor];];
(* Two argument forms -- convert to three argument forms *)
(* Using First metric, if there is one *)
CheckTraceFree[tens_,{inds__?AIndexQ}]/;(Length[{inds}]>=2)&&(1==Length@Union[VBundleOfIndex/@{inds}]):=Module[{metrics=MetricsOfVBundle@VBundleOfIndex@First@{inds}},CheckTraceFree[tens,{inds},First@metrics]/;(Length@metrics>=1)];
(* Convert to three argument form *)
CheckTraceFree[tens_,{{inds__?AIndexQ},met_}]:=CheckTraceFree[tens,{inds},met];
CheckTraceFree[tens_,arguments:{{_,_}..}]:=CheckTraceFree[tens,#]&/@arguments;
(* Three argument forms *)
(* Thread over metrics *)
CheckTraceFree[tens_,slots_,metrics_List]:=CheckTraceFree[tens,slots,#]&/@metrics;
(* Thread over several indices *)
CheckTraceFree[tens_,slots_?(Length[#]>2&),metric_]:=CheckTraceFree[tens,#,metric]&/@Subsets[slots,{2}];
CheckTraceFree[tens_,{a_?AIndexQ,b_?AIndexQ},met:_?MetricQ|_?InverseMetricQ|delta]:=Module[{},
(* Now use CheckTraceFreeRules for the logic *)
CheckTraceFreeRules[tens,{a,b},met];
];
CheckTraceFree[tens_,rest__]:=Throw@Message[TraceFree::invalid,{rest},"format for TraceFree"];
(* Check the input *)
DefTensorBeginning[head_[indices___],dependencies_,sym_,options___]:=Module[{tf=OptionValue[DefTensor,{options},TraceFree]},
If[tf=!={},CheckTraceFree[head[indices],tf]];
];
(* Call SetTraceFreeRules *)
DefTensorEnd[head_[indices___],dependencies_,sym_,options___]:=Module[{tf=OptionValue[DefTensor,{options},TraceFree]},
If[tf=!={},SetTraceFreeRules[head[indices],tf]];
];
xTension["TraceFree`",DefTensor,"Beginning"]=DefTensorBeginning;
xTension["TraceFree`",DefTensor,"End"]=DefTensorEnd;
Protect[TraceFree];
End[];
EndPackage[];