Suppose I define:
RealVector[expr_]:=VectorQ[expr,NumberQ[#]&&Head[#]===Real)];
Then:
RealVector[{1,2,3}]; RealVector[{1.,2.,3.}];
should evaluate to False and True, resp. But I get False and False. Why?
Suppose I define:
RealVector[expr_]:=VectorQ[expr,NumberQ[#]&&Head[#]===Real)];
Then:
RealVector[{1,2,3}]; RealVector[{1.,2.,3.}];
should evaluate to False and True, resp. But I get False and False. Why?
In Mathematica, Element[1,Reals] returns True since integers are subset of the reals. But Head[1] is Integer. So, since you need to check for Head of each element. One way might be
realVector[x_List] := VectorQ[x, NumericQ] && (AllTrue[x, (Head[#] === Real) &])
Now
realVector[{1., 2., 3.}] (*True*)
realVector[{1, 2, 3}] (*False*)
realVector[{1., 2., 3}] (*False, since one element is not Real*)
realVector[{Pi, 1., 2.}] (*False*)
btw, you do not need VectorQ[x, NumericQ] in the above, but I thought it might be faster to short circuit the test. You can also just use
realVector[x_List] := AllTrue[x, (Head[#] === Real) &]
And this should work also.
You have syntax errors (e.g., no & for making a function). Moreover, there is no need to check if an element is a number if you're also checking whether it is in the set of Reals.
This should work for you:
realVector[expr_] := VectorQ[expr, # \[Element] Reals &];
True for realVector[{1, 2, 3}], I think the OP wants this to return False. This happens because integer also returns True for Element[#,Reals], so a Head test is needed.
– Nasser
Jan 27 '15 at 08:11
Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!
– Jan 27 '15 at 05:51&:RealVector[expr_] := VectorQ[expr, NumberQ[#] && Head[#] === Real &]– Michael E2 Jan 27 '15 at 12:05