3

I need help using Apply (@@).

In the following code

f[x_, y_] := x^3 - y*Cos[Pi*(x/y - y)] 
coord = {{3, 5}, {4, 6}, {3, 5}}
f @@@ coord

Grad[f[x, y], {x, y}]

Grad[f[x, y], {x, y}] @@@ coord

I am getting a Apply function across the list but is not doing the actual calculation over the list. Why is the Grad function not working when applied to coord?

glS
  • 7,623
  • 1
  • 21
  • 61
  • Check out this link: http://mathematica.stackexchange.com/questions/18/where-can-i-find-examples-of-good-mathematica-programming-practice or get the Elementary Introduction to the Wolfram Language book – SumNeuron Sep 18 '16 at 15:27

1 Answers1

8

The reason your last line is not working is because Grad[f[x, y], {x, y}] is not a function, but a list of (operations applied to) symbols.

What you need is instead a function taking two inputs and giving two outputs. One way to do this is by explicitly defining one

(* define function *)
gradf[x_, y_] := Evaluate[Grad[f[x, y], {x, y}]]
(* apply function to coord *)
gradf @@@ coord

(* {{27 - Sqrt[5/8 + Sqrt[5]/8] \[Pi], 
  1/4 (1 - Sqrt[5]) + 28/5 Sqrt[5/8 + Sqrt[5]/8] \[Pi]}, {48 + (
   Sqrt[3] \[Pi])/2, 
  1/2 - (10 \[Pi])/Sqrt[3]}, {27 - Sqrt[5/8 + Sqrt[5]/8] \[Pi], 
  1/4 (1 - Sqrt[5]) + 28/5 Sqrt[5/8 + Sqrt[5]/8] \[Pi]}} *)

If you want to stick in using a shortcut notation, not defining an extra function, you can use

Function[{x, y}, Evaluate@Grad[f[x, y], {x, y}]] @@@ coord

Note that the use of Evaluate is necessary here, otherwise the function would replace the values of the elements of coord also in the derivation variables {x, y}.

glS
  • 7,623
  • 1
  • 21
  • 61