2
$\begingroup$

I have a function (say $f$) which takes two arguments. The first argument will either be an integer or a linear combination of f's, and the second argument will always be an integer. If we do have the former case i.e. a linear combination of f's, then those f's themselves just have integers as arguments.

So the first case would look like e.g. $f[2,3]$ while the second case would look like e.g. $f[f[1,4] + 2f[3,7] - 6f[5,2], 3]$.

There may be many terms in the sum in the first argument. In the 'linear combination of f's' case, I want to distribute this over the first argument while pulling out any constants, so e.g. the relevant case above should simplify to $f[f[1,4],3] + 2f[f[3,7],3] - 6f[f[5,2], 3]$.

I'd be very grateful for advice on a sensible solution to this!

$\endgroup$

1 Answer 1

4
$\begingroup$
ClearAll[f];

f[p_Plus, y_] := f[#, y] & /@ p;
f[a_?NumericQ * x_, y_] := a f[x, y];

f[f[1, 4] + 2 f[3, 7] - 6 f[5, 2], 3]

(* f[f[1, 4], 3] + 2 f[f[3, 7], 3] - 6 f[f[5, 2], 3] *)
$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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