5
$\begingroup$

I have a very simple question, and I'm sure there is a way to do this but I cannot find it:

if I want to create a function that's defined only for matrices, I can do it like this:

f[x_?MatrixQ]:=Det[x];

but what if I want to define a function which is defined only for matrices whose entries are numbers? I would like to combine MatrixQ with NumberQ for its matrix elements but I'm not sure how to make this work.

$\endgroup$
9
$\begingroup$

I was going for a syntactic pattern thing, but actually the two-argument form of MatrixQ should work here:

f[x_?(MatrixQ[#,NumberQ]&)]

MatrixQ[expr,test] gives True only if test yields True when applied to each of the matrix elements in expr.

$\endgroup$
  • $\begingroup$ I had tried something like this, but without the parenthesis. This works, thanks! $\endgroup$ – bnado Apr 29 '16 at 12:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.