Can anyone please help me determine what the following Mathematica code is doing in terms of Math equations:
(I'm having trouble putting the code here, so I attached in a picture)
Is the code doing the equation in the next image:
Can anyone please help me determine what the following Mathematica code is doing in terms of Math equations:
(I'm having trouble putting the code here, so I attached in a picture)
Is the code doing the equation in the next image:
Just a bunch of notes:
Quiet
?The Quiet[..., Series::sbyc]
just deactivates a specific error message that the authors seems to have struggled with.
Set
?The author tried to use the memoization technique. Suppose you want to define an expression f[n]
that is really costly to compute (and all symbolic calculations tend to be costly compared to, e.g., plain machine number crunching), then the definition
f[n_] := <<very complicated and costly expression in n>>
will lead to bad performance when the function f
is called a bazillion times. The SetDelayed
:=
tells Mathematica to reevaluate the right hand side whenever an expression of the form f[n]
pops up. But that is not necessary; it would suffice to compute f[n]
only once for a given n
and store it in a retrievable form, for example in a hash table. This can be done by
f[n_] := f[n] = <<very complicated and costly expression in n>>
That looks weird but it does the following: Suppose that I execute f[1]
. Then the rule above tells Mathematica to execute
f[1] = <<very complicated and costly expression in n=1>>
Now a new definition of f[1]
is stored in the symbol f
; you can find it with ??f
. (At the same time, the result of the computation is returned as the outcome of f[1]
, because the return value of Set
(=
) is always the right hand side.)
This new rule is more specific than f[n_] := ...
and Mathematica always looks up the most specific rule. so the next time when you evaluate f[1]
, Mathematica will just read off the stored value of f[1]
without doing the actual computations.
Hold
?That has to do with the fact that the expansion variable in Series
has to be symbolic, so one has to ensure that it is shielded from global definitions. This is why Module[{xl},...]
is used. The problem with such local variables is that they do not play well with rule-based function definitions. But as it is done here, it is probably not what the author intended. If I were to implement this, I would use Function
with a formal symbol (which cannot be overwritten) as follows:
ClearAll[StieltjesE];
StieltjesE[n_] := StieltjesE[n] = Function @@ {\[FormalX],
Normal[
Quiet[
Series[4^(n - 1) Beta[n, n]/ LegendreQ[n - 1, 0, 3, \[FormalX]], {\[FormalX], ∞, 0}],
Series::sbyc
]
],
Listable
};
This is not listable in the variable n
, but I don't think that this was the main point to the author.
Now you can do
StieltjesE[2]
and you get an anonymous function with the second Taylor polynomial:
Function[\[FormalX], -(6/5) + 2 \[FormalX]^2, Listable]
So you can do
StieltjesE[2][Pi]
to actually evaluate it. Anonymous function is also Listable
, so it threads over list like this:
xlist = Subdivide[0., 1., 6];
StieltjesE[2]
StieltjesE[2][xlist]
{-1.2, -1.14444, -0.977778, -0.7, -0.311111, 0.188889, 0.8}
However, in this case Listable
was not necessary at all because polynomial expressions are built from Plus
, Times
and Power
which are already Listable
. So the following would work equally well:
ClearAll[StieltjesE];
StieltjesE[n_] := StieltjesE[n] = Function @@ {\[FormalX],
Normal[
Quiet[
Series[4^(n - 1) Beta[n, n]/ LegendreQ[n - 1, 0, 3, \[FormalX]], {\[FormalX], ∞, 0}],
Series::sbyc
]
]
};
StieltjesE[5][.2]
by now. (No need for btw. if you input inexact numbers btw.)
$\endgroup$
Feb 21, 2020 at 11:42