3
$\begingroup$

This is such a simple question its almost embarrassing, and I'm surprised I haven't figured out where I'm going wrong with this..Probably because I'm really new to the commands Function and Apply and I might be using them wrong. But anyways, here it goes:

Input:

Clear[g2, x];
g2 := Function[x, Apply[Log[1 + x^2] + 6 x + x^3,x]]
g2[{2, 3}]

Output:

{20 + Log[5], 45 + Log[10]}[2, 3]

I have the answer but whats going on with the [2,3]?? I thought I need the extra "x" because Apply takes 2 arguments..

Here is an example I am trying to follow:

Input:

Clear[f2];
f2 := Function[pt, Sqrt[Apply[Plus, pt^2]]]
f2[{3, 4, 12}]

Output:

13

$\endgroup$
  • 2
    $\begingroup$ Slightly off-topic, if you set a Function, you don't need SetDelayed (:=); instead, Set (=) should be used. i.e. g2 = ... would be sufficient (and efficient too, because f2 and g2 are evaluated only once) $\endgroup$ – JungHwan Min Nov 5 '16 at 4:53
3
$\begingroup$

It is not clear what you are trying to do. The example you are trying to follow is not similar to your function g2. Since all of the functions used in g2 are Listable there is no need to use Apply

Clear[g2, x];
g2 := Function[x, Log[1 + x^2] + 6 x + x^3]
g2[{2, 3}]

(*  {20 + Log[5], 45 + Log[10]}  *)

Log[1 + #^2] + 6 # + #^3 &[{2, 3}]

(*  {20 + Log[5], 45 + Log[10]}  *)

If the components of g2 weren't Listable you could use Map

Log[1 + #^2] + 6 # + #^3 & /@ {2, 3}

(*  {20 + Log[5], 45 + Log[10]}  *)

Showing that they are all equivalent

% == %% == %%%

(*  True  *)
$\endgroup$
  • $\begingroup$ Sorry for the unclear question. Thanks for your clarification because I was obviously using Apply in an inappropriate way $\endgroup$ – Brandon Nov 5 '16 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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