Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a function which has something like

myFunc[q,a state[c,d]]

a could be anything, and I want to tell Mathematica that this part of the function is linear, so that the coefficient of state[c,d] can be taken outside so that it achieves this result

a myFunc[q,state[c,d]]

Can this be done?

Why is this important? I'm doing simulations on quantum mechanics, and operators have to be linear with respect to numbers, and this is important to make calculations easier.

share|improve this question
up vote 3 down vote accepted

You can try this

myFunc[q_, f_ + g_] := myFunc[q, f] + myFunc[q, g]
myFunc[q_, a_ f_state] := a myFunc[q, f] /; FreeQ[a, state]

So that

myFunc[q, a state[c, d]]
   a myFunc[q, state[c,d] ]


myFunc[q, a state[c, d] + b  state[e, h]]
   a myFunc[q, state[c, d]] + b myFunc[q, state[e, h]]

You can still assign a specific definition to myFunc[q_, f_state].

share|improve this answer
Thank you so much, you're a genius :) – The Quantum Physicist Dec 10 '13 at 16:16
You are Flattening me. --- No, wait... – Peltio Dec 10 '13 at 17:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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