6
$\begingroup$

I have a number of functions that depend on a variable x. I am using a second, integer variable to index these functions, i.e. something like

f[x_, 1] = a
f[x_, 2] = b x + c

When taking derivatives of these functions with D, it works the way I intend, i.e.

D[f[x, 1], x]
D[f[x, 2], x]

returns 0 respectively b. Now, however, I have a set of differential equations involving derivatives of these functions. These are expressed by Mathematica as

Derivative[1, 0][f][x, 1]
Derivative[1, 0][f][x, 2]

which remains unevaluated. Is there any way to have Derivative ignore the second variable? More specifically, I would like to have a way of doing something along the lines of

Derivative[1][g][x]

and then replacing g by f[x, 1]. I guess I could just assign specific names to each of the functions f[x, k], but in the end I want to sum them all up

Sum[f[x, k],{k, 1, n}]

for which it would be convienient to have some index variable rather than writing out the sum over different functions. Maybe I am approaching the whole idea of indexing functions in the wrong way?

$\endgroup$
7
$\begingroup$

You can put the index in the first pair of brackets, and the variable in the second, like so:

f[1][x_] = a;
f[2][x_] := b x + c;

(Note that the second assignment uses SetDelayed instead of Set. This is necessary because the variable x occurs on the right-hand-side).

You can view f[index] as the function acting on x. The derivatives are computed as follows:

f[1]'[x]
0
f[2]'[x]
 b

Computing the sum is then as easy as Sum[f[k][x],{k,1,n}]

$\endgroup$
  • $\begingroup$ +1 For currying. Not a trick used often enough here, IMHO! $\endgroup$ – evanb Sep 12 '14 at 17:24
  • $\begingroup$ Thanks for the answer! I did not know that using multiple levels of arguments was possible. $\endgroup$ – Andreas Sep 12 '14 at 19:27

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.