0
$\begingroup$

I am trying the following construction but I get an error message.

Let l be a list consisting of pairs of integers.

Let further m be a list consisting of a pair of positive integers. I want to do something like the following:

 Scan[(l[[1,#[1]]+l[[1,#[2]])^2,m]

That is, I want to for each list in m, add l[[1,#[1]] and l[[1,#[2]] together, and then take the square where # is the current list. I get the error message:

Part::pkspec1: The expression #1[1] cannot be used as a part specification.

I suppose I am doing something illegal with the syntax. My question is:

Is there some better way to do this? Or how can I make this work?

$\endgroup$
1
  • 1
    $\begingroup$ whenever # shows up, there really should be a & somewhere along the way. I do not see & in your code? $\endgroup$
    – Nasser
    Commented Aug 27, 2017 at 12:33

1 Answer 1

3
$\begingroup$

There are a number of glitches, so it is difficult to infer exactly what you actually want to be doing. You'll get better answers if you can give some sample values of $l$, $m$ and your desired output. Still:

1) You need an ampersand & as Nasser commented.

2) You want to access the elements of the current list so you will use double brackets for list access, e.g. #[[1]] not passing arguments as to a function #[1].

3) Scan has no footprint if its scanning function doesn't make an assignment or Throw an exception or Sow information. You probably want to use Map so you can access the output.

4) Since l consists only of pairs, if you have the m elements access the second index, m can only have values of 1 or 2 or you will throw an error. Since you said "positive integers" and not just 1 or 2, you may want to exchange the order of your indexing.

If $m$ list elements only contain values 1 or 2:

l = RandomInteger[{-5, 5}, {20, 2}]

(* Out: {{4, -1}, {4, 3}, {0, -2}, {1, 4}, {4, -4}, {-3, -2}, {-1, 1}, {4, 
  2}, {-3, -2}, {1, 5}, {2, -4}, {4, -4}, {0, 2}, {3, -4}, {1, 
  3}, {0, -3}, {0, 3}, {-2, 2}, {-3, 3}, {2, 3}} *)

 m= RandomInteger[{1, 2}, {20, 2}]

(* Out: {{2, 2}, {2, 2}, {2, 1}, {2, 1}, {1, 1}, {1, 2}, {1, 1}, {2, 2}, {2, 
  2}, {2, 1}, {1, 2}, {1, 2}, {2, 1}, {2, 1}, {2, 1}, {1, 1}, {1, 
  1}, {2, 1}, {1, 1}, {1, 2}} *)

 Map[(l[[1, #[[1]]]] + l[[1, #[[2]]]])^2 &, m]

yields:

{4, 4, 9, 9, 64, 9, 64, 4, 4, 9, 9, 9, 9, 9, 9, 64, 64, 9, 64, 9}

If the values of m can range over the length of $l$:

If $m$ can be as follows:

 m = RandomInteger[{1, Length[l]}, {Length[l], 2}]

(* Out: {{5, 14}, {3, 12}, {15, 4}, {8, 2}, {17, 2}, {3, 1}, {3, 9}, {11, 
  15}, {5, 17}, {19, 1}, {19, 6}, {5, 14}, {14, 17}, {6, 17}, {12, 
  14}, {11, 2}, {9, 20}, {12, 2}, {15, 18}, {7, 2}} *)

you will need to change your indexing order. Why? A call of Map[(l[[1, #[[1]]]] + l[[1, #[[2]]]])^2 &, m] would result in error, since you're asking for e.g the 5th element of a 2 element list. Maybe you meant:

Map[(l[[#[[1]], 1]] + l[[#[[2]], 1]])^2 &, m]

yielding:

{49, 16, 4, 64, 16, 16, 9, 9, 16, 1, 36, 49, 9, 9, 49, 36, 1, 64, 1, 9}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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