Re: Tensors in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg37634] Re: Tensors in Mathematica
- From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
- Date: Thu, 7 Nov 2002 06:41:18 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Earlier I tried to show how to define a function called TensorQ. However, my earlier version would give the wrong answer if given a tensor when one or more tensor element is not an atom. I have a much improved version below. ------------------- In[5]:= TensorQ[expr_List]:= With[{rank=TensorRank[expr]}, FreeQ[ Head /@ Level[ expr, {rank},Heads->False ], List, {1}, Heads->False ] ]; TensorQ[_]=False; TensorQ[expr_,test_]:= TensorQ[expr] &&TrueQ[ And @@ test /@ Level[ expr, {3}, Heads®False ] ]; ----------- Here TensorQ knows t1 is a tensor even though (3+Pi) isn't an atom. In[5]:= t1={{{3,2},{3,3+Pi},{0,3}},{{0,3},{7,0},{5,2}},{{1,0},{2,0},{1,2}}}; TensorQ[t1] Out[6]= True ----------- TensorQ knows t1 is a tensor and all elements of the tensor are numeric. In[7]:= TensorQ[t1,NumericQ] Out[7]= True ----------- In the next line we get False because NumberQ[3+Pi] returns False. In[8]:= TensorQ[t1,NumberQ] Out[8]= False ----------- TensorQ knows t2 below isn't a tensor. In[9]:= t2={{{3,2},{3,3+Pi},{0,3}},{{0,3},{7,0},{5,2}},{{1,0,0},{2,0},{1,2}}}; TensorQ[t2] Out[9]= False ---------- Regards, Ted Ersek Download my latest Mathematica Tips, Tricks from http://www.verbeia.com/mathematica/tips/Tricks.html or from http://www.mathsource.com/Content/General/Tutorials/0212-331