2
$\begingroup$

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.

$\endgroup$

1 Answer 1

3
$\begingroup$

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

$\endgroup$
1
  • 4
    $\begingroup$ You are Flattening me. --- No, wait... $\endgroup$
    – Peltio
    Dec 10, 2013 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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