# Linearity of a function in Mathematica

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.

-

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] ]


and

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].

-
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