0

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!

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Nick Murphy
  • 189
  • 4
  • (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 – Lukas Lang Aug 22 '17 at 08:22
  • I guess this is the answer: 131982 – Kuba Aug 22 '17 at 08:23

1 Answers1

2

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  *)
LouisB
  • 12,528
  • 1
  • 21
  • 31