0
$\begingroup$

Good morning. I would like to know if there is a way to implement memoization if there is more than one variable involved in the memoization; or more accurately, for my problem, have the memoization itself be a function of a second variable. Let me explain.

I'm working with a certain set of numbers called Hypergeometric Bernoulli Numbers (and down the road the polynomial analogues...) which are defined recursively as

$$B_{N,0}=1$$

$$B_{N,k}=-\binom{N+k}{k}^{-1}\sum_{j=0}^{k-1}\binom{N+k}{j}B_{N,j}$$

I had a previous post here which helped me in recalling how the memoization process works in Mathematica. And it produced exactly what I needed; except I need to be able to have my resulting outputs be functions of $N$. It seems during my previous process, the $N$ is unavailable for evaluation. How can I amend my previous code to allow the memoized terms to be functions themselves?

Here was my code attempt: enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ People here generally like users to post code as Mathematica code instead of just images or TeX, so they can copy-paste it. It makes it convenient for them and more likely you will get someone to help you. You may find this meta Q&A helpful $\endgroup$
    – Michael E2
    Nov 28, 2020 at 16:29

1 Answer 1

4
$\begingroup$

You can for example memoize a function B[k] for each value of $k$, so that we can call B[k][n] with any argument $n$:

B[0] = Function[n, 1];
B[k_Integer /; k >= 1] := B[k] = Function[n,
  Evaluate@Together@FunctionExpand[
    -Sum[Binomial[n+k,j]*B[j][n], {j,0,k-1}]/Binomial[n+k,k]]]

Then make an abbreviation, so that B[n,k] calls B[k][n] for cleaner access:

B[n_, k_Integer /; k >= 0] := B[k][n]

Test:

B[n, 0]
(*    1    *)

B[n, 1]
(*    -1/(1 + n)    *)

B[n, 2]
(*    2/((1 + n)^2 (2 + n))    *)

If you now check with ?B you'll see the memoized functions B[0], B[1], B[2].

$\endgroup$

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