TensorQ

• To: mathgroup at smc.vnet.net
• Subject: [mg20686] TensorQ
• From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
• Date: Sun, 7 Nov 1999 02:10:16 -0500
• Sender: owner-wri-mathgroup at wolfram.com

```I was thinking it would be nice to have a function that determines if
something is a vector, matrix, or higher dimension analogue of a matrix.  Is
something like that called a tensor?  I know almost nothing about tensors.
Anyway I wrote an elegant program that performs such a test and called it
TensorQ.

In[1]:=
TensorQ[tnsr_List] := With[{ s1=Level[tnsr,{TensorRank[tnsr]}]  },
FreeQ[s1,_List,{1}]
]

Suppose (tnsr) above is a vector, matrix, or similar structure with greater
dimensions.
Then (s1) will be all the elements of the vector, matrix, etc.
Next I use FreeQ to see if any of these elements have the head List.
I give FreeQ the level specification {1} which tells it to ignore lists
inside the elements.

TensorQ  does what I want in the examples below.

----------------
In[2]:=
TensorQ[  {1, 2, foo[3, {1}] }  ]
Out[2]=
True

In[3]:=
TensorQ[{{1,2,3},{2,3,4}}]
Out[3]=
True

In[4]:=
TensorQ[ {{{2, 1}, {3, 2}}, {{4, foo[3, {1}]}, {5, 4}}} ]
Out[4]=
True

In[5]:=
TensorQ[{1,2,{3}}]
Out[5]=
False

In[6]:=
TensorQ[{{1,2,3},{2,3,4,5}}]
Out[6]:=
False

In[7]:=
TensorQ[{{{2,1},{3,2}},{{4,3,4},{5,4}}}]
Out[7]=
False

In[8]:=
TensorQ[{f[1,2,3],f[3,4,5]}]
Out[8]=
True

You can read about Level and level specification at my web site.

--------------------
Regards,
Ted Ersek

For Mathematica tips, tricks see
http://www.dot.net.au/~elisha/ersek/Tricks.html

```

• Follow-Ups:
• Prev by Date: Re: Q about using Round.
• Next by Date: Re: Solution of this equation