2
$\begingroup$

Am I doing something wrong here?

 m = {{Infinity, Infinity, Infinity}, {Infinity, 1, 2}, {Infinity, 2, 3}};
 Position[m, 1]
 (*{{1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {2, 1, 1}, {2, 2}, {3, 1, 1}}*)

What is the above? I can get correct position by

m = {{Infinity, Infinity, Infinity}, {Infinity, 1, 2}, {Infinity, 2, 3}};
Position[m, 1, {2}]
(*{{2, 2}}*)

But why it only affect the value 1?

m = {{Infinity, Infinity, Infinity}, {Infinity, 1, 2}, {Infinity, 2, 3}};
Position[m, 3]
(* {{3, 3}} *)

If I remove Infinity, then I can find position of 1 ok, without using {2}

m = {{9, 9, 9}, {9, 1, 2}, {9, 2, 3}};
Position[m, 1]
(* {{2, 2}} *)

Why does it behave different with 1? And what does this {{1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {2, 1, 1}, {2, 2}, {3, 1, 1}} mean?

Mathematica graphics

$\endgroup$
2
$\begingroup$

Position is a pattern matching function, and pattern matching is performed on (something close to) the FullForm of an expression. Therefore when designing patterns, and especially when debugging them, the first thing to do is to look at the FullForm of the expression(s) you are working with. In this case:

Infinity // FullForm
DirectedInfinity[1]

This expression is not atomic:

Infinity // AtomQ
False

It is therefore accessible (or exposed) to pattern matching, and the 1 inside it is discovered by Position.

Note: some atomic expressions are overloaded to partially work with patterns. For examples please see Why can't a string be formed by head String?

$\endgroup$
2
$\begingroup$

I get the same on OS X. The cause of the "problem" can be seen by first evaluating

Position[m, 1, {3}]

(* {{1, 1, 1}, {1, 2, 1}, {1, 3, 1}, {2, 1, 1}, {3, 1, 1}} *)

say what??!!!

But then

FullForm[m]
(* List[List[DirectedInfinity[1],DirectedInfinity[1],DirectedInfinity[1]],List[DirectedInfinity[1],1,2],List[DirectedInfinity[1],2,3]] *)

...explains it

$\endgroup$

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.