Re: how to build pattern for a square matrix of reals?
- To: mathgroup at smc.vnet.net
- Subject: [mg98487] Re: how to build pattern for a square matrix of reals?
- From: m.r at inbox.ru
- Date: Fri, 10 Apr 2009 04:59:26 -0400 (EDT)
- References: <grhp95$mj7$1@smc.vnet.net>
sagra... at gmail.com wrote:
> As in subject: how to build a pattern that will specify that given
> argument of a function has to be square matrix of reals? Tried with
> following for "square matrix" part:
> f[x_?MatrixQ[#] && Dimensions[#][[1]] == Dimensions[#][[2]]
> &] := ...
> but it doesn't seem to be working...
>
> Thanks.
Regarding the type checking, a fast way to do that is to use MatchQ[#,
_Real]& as the test in MatrixQ. It is recognized as a special case and
for packed arrays it'll be O(1):
In[1]:= m = RandomReal[1, {4*^3, 4*^3}];
In[2]:= Timing[MatchQ[m, {{_Real..}..}]]
Out[2]= {0.906, True}
In[3]:= Timing[MatrixQ[m, MatchQ[#, _Real]&]]
Out[3]= {0., True}
Maxim Rytin
m.r at inbox.ru