Group Abstract Group Abstract

Message Boards Message Boards

0
|
2.4K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

{m__List?MatrixQ} vs {m__?MatrixQ}.

Posted 3 years ago

See my following example:

In[2553]:= f//ClearAll;
f[{m__List?MatrixQ},c_?MatrixQ]:={{m},c}
g//ClearAll;
g[{m__?MatrixQ},c_?MatrixQ]:={{m},c}
f[{IdentityMatrix[2],IdentityMatrix[2]},IdentityMatrix[2]]
%==g[{IdentityMatrix[2],IdentityMatrix[2]},IdentityMatrix[2]]

Out[2557]= {{{{1, 0}, {0, 1}}, {{1, 0}, {0, 1}}}, {{1, 0}, {0, 1}}}

Out[2558]= True

As you can see, {m__List?MatrixQ} and {m__?MatrixQ} give the same results. I wonder if they are equivalent in all cases.

Regards,
Zhao

POSTED BY: Hongyi Zhao
2 Replies
Posted 3 years ago

However, after applying the Normal command, MatrixQ will return true in all cases:

In[19]:= randomData = RandomReal[1, {6, 4, 4}];
randomNumericArrays = NumericArray /@ randomData;
MatchQ[randomData, {___List?MatrixQ}]
MatchQ[randomNumericArrays, {___List?MatrixQ}]
MatchQ[randomNumericArrays//Normal, {___List?MatrixQ}]

Out[21]= True

Out[22]= False

Out[23]= True
POSTED BY: Hongyi Zhao

I wonder if they are equivalent in all cases.

No - there are some objects that are MatrixQ but have a different head than List. These could be one of the structured array objects like SparseArray or NumericArray among others. For example:

In[109]:= randomData = RandomReal[1, {6, 4, 4}];
In[110]:= randomNumericArrays = NumericArray /@ randomData

In[111]:= MatchQ[randomData, {___List?MatrixQ}]

Out[111]= True

In[112]:= MatchQ[randomNumericArrays, {___List?MatrixQ}]

Out[112]= False
POSTED BY: Jason Biggs
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard