hongyizhao
(Hongyi Zhao)
September 6, 2022, 7:56am
1
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
jasonb
(Jason Biggs)
September 6, 2022, 9:01pm
2
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
hongyizhao
(Hongyi Zhao)
September 6, 2022, 11:15pm
3
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