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 want to define a function that involves a differentiation step that Mathematica can do easily, which might be of the form

f[a_,b_,z_,j_]:=D[Hypergeometric1F1[a, b, z^2], {z, 2 j}]

(This will not work when given numeric z, but that is only a small part of the problem.)

I will be using this for a limited set of j's quite intensively in z. It is therefore important to me that the symbolic differentiation only take place once, so that Mathematica is not repeatedly trying to do the same symbolic manipulations when running over a plot or a numerical integration.

What is the cleanest/most elegant way to do this sort of partial memoization procedure?

share|improve this question
Maybe you can try and remove the : ? – b.gatessucks Mar 21 '13 at 13:44
@b.gatessucks that doesn't do anything, but leave it symbolic form. There's not enough information to perform the derivative. – rcollyer Mar 21 '13 at 13:46
Have you tried to create a non global variable inside your function that stores the derivative? You could then use replacement rules to call upon the derivative. – Sascha Mar 21 '13 at 13:50
up vote 3 down vote accepted

A general approach in this kind of situations is to use memoization. Here, however, some of the parameters should remain patterns (general), so you can use something like this:

f[a_, b_, z_, j_] :=
   Block[{al, bl, zl},
     f[al_, bl_, zl_, j] = D[Hypergeometric1F1[al, bl, zl^2], {zl, 2 j}];
     f[a, b, z, j]

Upon the first call, a special global rule (definition) will be created for any specific j, and all subsequent calls with this j will use this definition:


    (2 a Hypergeometric1F1[1+a,1+b,z^2])/b+(4 a (1+a) z^2 
      Hypergeometric1F1[2+a,2+b,z^2])/(b (1+b))

Inspecting the definition now:


f[al$_,bl$_,zl$_,1]=(2 al$ Hypergeometric1F1[1+al$,1+bl$,zl$^2])/bl$+
    (4 al$ (1+al$) zl$^2 Hypergeometric1F1[2+al$,2+bl$,zl$^2])/(bl$ (1+bl$))

\*SubscriptBox[\(\[PartialD]\), \({zl, 2\ j}\)]\(Hypergeometric1F1[al, bl, 
\*SuperscriptBox[\(zl\), \(2\)]]\)\);f[a,b,z,j]]
share|improve this answer

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.