Skip to main content
added 60 characters in body
Source Link
Lukas Lang
  • 34.4k
  • 1
  • 56
  • 99

Notice here that I used the operator form of Apply. This effectively converts the function Function[{x,y,z,a,b,c},...] which takes 6 arguments into a function that takes a list of length 6 as a single argument. This is required since selectSelect will pass the lists of length 6 as a single argument to the filter criterion. The Apply then effectively splits this list into separate arguments.

Notice here that I used the operator form of Apply. This effectively converts the function Function[{x,y,z,a,b,c},...] which takes 6 arguments into a function that takes a list of length 6 as a single argument. This is required since select will pass the lists of length 6 as a single argument to the filter criterion. The Apply then effectively splits this list into separate arguments.

Notice here that I used the operator form of Apply. This effectively converts the function Function[{x,y,z,a,b,c},...] which takes 6 arguments into a function that takes a list of length 6 as a single argument. This is required since Select will pass the lists of length 6 as a single argument to the filter criterion. The Apply then effectively splits this list into separate arguments.

Source Link
Lukas Lang
  • 34.4k
  • 1
  • 56
  • 99

Effectively, {a,b}=... sets both a and b simultaneously to the corresponding elements of the right side (see Set). So for example:

{a, b} = {1, 2};

a
(* 1 *)

b
(* 2 *)

Now, in your example, we can look at what the function (the (...)&, see also Function) gets as argument using Echo:

u = RandomReal[{0, 1}, {3, 5}];
v = Map[({x, y, z, a, b} = Echo@#; {x, y, z, a, b, 1 - a - b}) &, u]

(* {0.524485,0.374012,0.209276,0.447658,0.534618} *)
(* {0.945336,0.0270697,0.206729,0.00877572,0.881564} *)
(* {0.725822,0.263445,0.160514,0.247397,0.798175} *)
(* {{0.524485, 0.374012, 0.209276, 0.447658, 0.534618, 
  0.0177245}, {0.945336, 0.0270697, 0.206729, 0.00877572, 0.881564, 
  0.10966}, {0.725822, 0.263445, 0.160514, 0.247397, 
  0.798175, -0.0455723}} *)

So it just gets one row from the 20x5 matrix, as expected. This now means that {x,y,z,a,b} get set to those 5 values. The second part is then the result of the function, so using the just set values for a,b,x,y,z, we compute the list {x,y,z,a,b,1-a-b}. This is then returned from the function (see also CompoundExpression).

That being said, I would have rewritten the line as follows:

v = Apply[Function[{x, y, z, a, b}, {x, y, z, a, b, 1 - a - b}], u, {1}];

This applies (see Apply) the function taking 5 arguments (called x,y,z,a,b) to each row of the matrix, and then computes the same expression as before. I just find it a lot more explicit in what's going on, and it doesn't needlessly modify global variables.

The third line follows the same logic, so we can use a similar strategy to rewrite it for improved readability:

w = Take[Select[v, Apply@Function[{x, y, z, a, b, c}, x^2 + y^2 + z^2 <= 1 && x + y - 1 <= z <= -x + y + 1 && z >= -x - y - 1]], 3]

Notice here that I used the operator form of Apply. This effectively converts the function Function[{x,y,z,a,b,c},...] which takes 6 arguments into a function that takes a list of length 6 as a single argument. This is required since select will pass the lists of length 6 as a single argument to the filter criterion. The Apply then effectively splits this list into separate arguments.