(*##################################################################*) (*# #*) (*# Tensor.m: Mathematica package to perform tensor calculations. #*) (*# #*) (*##################################################################*) (*:Title: Tensor *) (*:Author: Bob Bacus *) (*:Copyright: Copyright 1997, Bob Bacus *) (*:Package Version: 1.0 *) (*:Mathematica Version: 2.2.1 *) (*: Summary: This package contains functions to perform tensor calculations in Euclidean space. *) (*:Keywords: tensor *) (*:Discussion: *) (*:Context: tensor.m *) (*:Source: *) (*:History: *) (*:Limitations: *) Print["##################################################################"] Print["# #"] Print["# Tensor.m : Mathematica package to perform tensor calculations. #"] Print["# Type '?Command' for help on individual commands. #"] Print["# #"] Print["# Commands : Rank, TensorProduct, TensorDerivative #"] Print["# #"] Print["##################################################################"] (*--------------------------------*) (*| Rank[A] returns the rank |*) (*| of the tensor A |*) (*--------------------------------*) Rank[A_]:=Dimensions[Dimensions[A]][[1]] (*---------------------------------------*) (*| BracketRemove[list] is a utility |*) (*| to remove the outer set of brackets |*) (*| from a list |*) (*---------------------------------------*) BracketRemove[list_]:=StringTake[ToString[list],{2,StringLength[ToString[list]]-1}] (*------------------------------------------------------------------------*) (*| TensorProduct[A,list1,B,list2] returns the product of two tensors |*) (*| A and B. list1 and list2 are lists containing the indices of A and B |*) (*| respectivly. |*) (*| |*) (*| Example: TensorProduct[X[s,t,3,2], {u}, T[s,t,3,2], {u,v}] |*) (*| |*) (*| returns a rank 1 tensor (the index u is contracted out), the result |*) (*| of (X[mu] T[mu,nu]) |*) (*------------------------------------------------------------------------*) TensorProduct::usage="TensorProduct[A, list1, B, list2] returns the product of two tensors\n A and B. list1 and list 2 are lists containing the indices of A and B\n respectivly.\n\n Example: TensorProduct[X[s,t,3,2], {u}, T[s,t,3,2], {u,v}]\n\n returns a rank 1 tensor (the index u is contracted out), the result\n of X[mu] T[mu nu]" TensorProduct[A_,list1_,B_,list2_]:= ( If[(Rank[list1] != 1) || (Rank[list2] != 1), Print["List1 and List2 must be lists of Rank = 1"]]; If[(Rank[ToExpression[A]] != Dimensions[list1][[1]]), Print["List1 must have length equal to Rank[A]"]]; If[(Rank[ToExpression[B]] != Dimensions[list2][[1]]), Print["List2 must have length equal to Rank[B]"]]; list3={}; For[i=1,i<=Length[list1],i++, list3=Append[list3,{list1[[i]],1,Dimensions[ToExpression[A]][[i]]}]]; list4={}; For[i=1,i<=Length[list2],i++, list4=Append[list4,{list2[[i]],1,Dimensions[ToExpression[B]][[i]]}]]; list5:=Intersection[list3,list4]; list6:=Join[list3,list4]; For[i=0,i<=Length[list5],i++, j=0; While[j 0) && (Length[list6] > 0), ToExpression[StringJoin["Table[Sum[",ToString[InputForm[A]],"[[", BracketRemove[list1],"]]*",ToString[InputForm[B]],"[[", BracketRemove[list2],"]],",BracketRemove[list5],"],", BracketRemove[list6],"]"]]] ) (*---------------------------------------------------------------------*) (*| TensorDerivative[list1,index,Tensor,list2] returns the derivative |*) (*| of Tensor w.r.t the variables given in list1. index is the |*) (*| index of the derivative operator; list2 is a list of the indices |*) (*| of Tensor. |*) (*| |*) (*| Example: TensorDerivative[{s,t}, a, X[s,t,p,q], {u}] |*) (*| |*) (*| returns a rank 2 tensor, the result of (d[a] X[mu]) |*) (*---------------------------------------------------------------------*) TensorDerivative::usage="TensorDerivative[list1,index,Tensor,list2] returns the derivative\n of Tensor w.r.t the variables given in list1. index is the\n index of the derivative operator; list2 is a list of the indices\n of Tensor.\n\n Example: TensorDerivative[{s,t}, a, X[s,t,p,q], {u}]\n\n returns a rank 2 tensor, the result of (d[a] X[mu])" TensorDerivative[list1_,index_,Tensor_,list2_]:= ( StrTensor=ToString[InputForm[Tensor]]; Which[((list2 == {}) || ((list2 != {}) && !MemberQ[list2,index])), ToExpression[StringJoin["Table[D[",StrTensor,",", ToString[list1],"[[i]]],{i,1,",ToString[Length[list1]],"}]"]], (MemberQ[list2,index] && (Length[list2] == 1)), ToExpression[StringJoin["Sum[D[",StrTensor,"[[i]],", ToString[list1],"[[i]]],{i,1,",ToString[Length[list1]],"}]"]], (MemberQ[list2,index] && (Length[list2] > 1)), (list3={}; For[i=1,i<=Length[list2],i++, If[(ToString[index] != ToString[list2[[i]]]), list3=Append[list3,{list2[[i]],1,Dimensions[ ToExpression[StrTensor]][[i]]}]]]; ToExpression[StringJoin["Table[Sum[D[",StrTensor,"[[", BracketRemove[list2],"]],",ToString[list1],"[[", ToString[index],"]]],{",ToString[index],",1,", ToString[Length[list1]],"}],",BracketRemove[list3],"]"]] ) ] ) Print[" "]