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$
4
  • $\begingroup$ Don't know why it gives the error. Oddly enough, Position[N@m, 1.0] gives the correct answer. $\endgroup$ Commented Jan 12, 2015 at 8:15
  • $\begingroup$ Related: (4946), (22948) $\endgroup$ Commented Jan 12, 2015 at 9:16
  • 1
    $\begingroup$ ... also related: Why Infinity /. _?(# < 6 &) -> 0 replaces Infinity with ComplexInfinity?. $\endgroup$ Commented Jan 12, 2015 at 9:38
  • $\begingroup$ @kguler Even closer relation! I forgot about that one. This is almost a duplicate of it. $\endgroup$ Commented Jan 12, 2015 at 9:39

2 Answers 2

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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.