0
$\begingroup$

I am having trouble understanding what is going wrong when trying to use the Coefficient function on expressions in InputForm. A very simple example serves to illustrate.

In[1]:= a = 3 x + 4 y // InputForm

Out[1]//InputForm=
3*x + 4*y

In[2]:= Coefficient[a, x]

Out[2]= 0

Is there any way to make this work, or convert the old expression back into Standard form? Cheers!

$\endgroup$
2
  • $\begingroup$ (Sadly,) many functions in MMA do not work with ...Form wrappers the way you might expect (i.e. by simply ignoring them). If you look at the FullForm of a, you'll see that everything is wrapped inside InputForm. And an expression of the form InputForm[...] has coeffient 0 for x, so the result you're getting is technically correct $\endgroup$
    – Lukas Lang
    Commented Aug 22, 2017 at 8:22
  • $\begingroup$ I guess this is the answer: 131982 $\endgroup$
    – Kuba
    Commented Aug 22, 2017 at 8:23

1 Answer 1

2
$\begingroup$

We can use FullForm to see what a really is, as

a = 3 x + 4 y // InputForm
a // FullForm
(*  InputForm[Plus[Times[3,x],Times[4,y]]]  *)

We see that a has the head InputForm. What we want to pass to Coefficient is the expression that has the head Plus. We can do that with

Coefficient[a//First,x]
(*  3  *)

Do we really need the head of a to be InputForm? If not, we could use parentheses like this

(a = 3 x + 4 y) // InputForm
(*  3*x + 4*y  *)

Coefficient[a,x]
(*  3  *)
$\endgroup$
1
  • $\begingroup$ OK! great, thank you, looks like I need to learn a bit about "head" in mathematica $\endgroup$ Commented Aug 22, 2017 at 8:32

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.