0
$\begingroup$

Hello I am trying to separate a function and get a list

f[x_, y_] := Sin[x]+Exp[Cos[x]]; u = List @@ f[x, y]

Output is what I want and that is

{E^Cos[x], Sin[x]}

If f[x_,y_]:= Sin[x+y] the output is {x + y}. I wanted output to be {Sin[x+y]}. Is there a way to do this?

Thank you

Erdem

$\endgroup$
  • 1
    $\begingroup$ Evaluate List @@ f //Trace to see what is happening. Also note f is not defining a function in either of the examples in your post. $\endgroup$ – Edmund Nov 12 '17 at 17:26
  • $\begingroup$ Thanks, the solution is there. I just need to find the way to extract it. $\endgroup$ – Erdem Nov 12 '17 at 17:55
  • $\begingroup$ You need to say more about the scope of the solution you are looking for. The @@ notation (for Apply) replaces the head of the expression. In the first example, you replace a head of Plus, and apparently you want to replace the head. In the second, you replace a head of Sin, but apparently you do not want to replace the head. So, what is your actual goal, generally stated? $\endgroup$ – Alan Nov 12 '17 at 18:17
  • $\begingroup$ I want to able to separate the function for summations and subtractions. Like Sin[x+y]-1+ Sin[x] Exp[Cos[z]] to {Sin[x+y],-1,Sin[x] Exp[Cos[z]]} $\endgroup$ – Erdem Nov 12 '17 at 18:26
  • $\begingroup$ As @Alan explained, List@@ replaces the head of the expression by List, which is fine if the head is, for instance Plus. In the second example, the head is Sin, so use {f[x, y]} instead to obtain {Sin[x+y]}. Use FullForm to see the internal structure of an expression, but be prepared for a lot of output. $\endgroup$ – bbgodfrey Nov 12 '17 at 18:36
1
$\begingroup$

It is still a bit unclear, but apparently you want to split an expression into a list of terms. You could try this:

terms[expr_] := If[Head@expr === Plus, Level[expr, 1], {expr}]
$\endgroup$
  • $\begingroup$ I did not want to be too wordy. I am trying to check if the function is made of "subfunctions" that are separated by + or -. This will allow me to treat each "subfunction" separately and hoping to gain some computation time. And it looks like what you wrote is working. $\endgroup$ – Erdem Nov 13 '17 at 21:37

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.