Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a table that is 27 x 27, each entry corresponds to an element from the periodic table. I use the replacement rules in element list to allow me to convert a list of elements into a list of coordinates, so that I can access values from the table. Here test is a list of elements whose values I want to access. The function getBigSeg returns {0,0} if the core == shell, and otherwise returns the value from the table.

bigSegTable = Table[i + j, {i, 27}, {j, 27}];
getBigSeg[core_, shell_] := 
 If[core == shell, {0, 0}, {bigSegTable[[core, shell ]] , 
   bigSegTable[[core, shell]]}]
    elementList = {"Sc" -> 1, "Ti" -> 2, "V" -> 3, "Cr" -> 4, "Mn" -> 5, 
   "Fe" -> 6, "Co" -> 7, "Ni" -> 8, "Cu" -> 9, "Zn" -> 10, "Y" -> 11, 
   "Zr" -> 12, "Nb" -> 13, "Mo" -> 14, "Tc" -> 15, "Ru" -> 16, 
   "Rh" -> 17, "Pd" -> 18, "Ag" -> 19, "Hf" -> 20, "Ta" -> 21, 
   "W" -> 22, "Re" -> 23, "Os" -> 24, "Ir" -> 25, "Pt" -> 26, 
   "Au" -> 27};

test = {{"Ti", "Ag"}, {"Zr", "Ag"}, {"Nb", "Ag"}, {"Tc", "Ag"}, {"Ru",
     "Ag"}, {"Ag", "Ag"}, {"Hf", "Ag"}, {"Ta", "Ag"}, {"Re", 
    "Ag"}, {"Os", "Ag"}, {"Ir", "Ag"}};

I want to know why if I do: getBigSeg[#1, #2] & @@@ test /. elementList

I get an error. But I do not get an error if I do

    getBigSeg[#1, #2] & @@@ {{2, 19}, {12, 19}, {13, 19}, {15, 19}, {16, 
   19}, {19, 19}, {20, 19}, {21, 19}, {23, 19}, {24, 19}, {25, 19}}

This is strange to me because

test /. elementList

gives

{{2, 19}, {12, 19}, {13, 19}, {15, 19}, {16, 
   19}, {19, 19}, {20, 19}, {21, 19}, {23, 19}, {24, 19}, {25, 19}}

So I do not understand why I am getting an error, and what is different between the two expressions.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

It is due to the precedence of the operators (see http://reference.wolfram.com/mathematica/tutorial/OperatorInputForms.html ).

Apply ( @@ and @@@ ) have a higher precedence than ReplaceAll ( /@ ). Consequently,

getBigSeg[#1,#2]&@@@test/.elementList 

is equivalent to

(getBigSeg[#1,#2]&@@@test)/.elementList 

To avoid the error messages you want to use parentheses to control the evaluation order:

getBigSeg[#1,#2]&@@@(test/.elementList)
share|improve this answer
    
One easy way to check this if working in the frontend is to place the cursor on test and then keep pressing ctrl-. repeatedly. –  acl Jun 4 at 18:16
    
On a Mac, rapidly clicking rather than using ctrl will progressively extend the highlighting. –  Bob Hanlon Jun 4 at 18:29
    
That too. I think it works on Linux too, so presumably also on Windows. –  acl Jun 4 at 18:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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