1
$\begingroup$

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:

(I have trouble putting mathjax latex here)stieltjes

$\endgroup$
1
  • $\begingroup$ Please, post code always in copyable form, so that other users do not have to retype everything. $\endgroup$ Feb 21, 2020 at 8:42

1 Answer 1

1
$\begingroup$

Just a bunch of notes:

Why Quiet?

The Quiet[..., Series::sbyc] just deactivates a specific error message that the authors seems to have struggled with.

Why 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.

Why 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
      ]
     ]
    };
$\endgroup$
3
  • $\begingroup$ @ Henrik Schumacher I'm not good at doing mathematica code, but I believe that the original code was intended as such: $N[StieltjesE[5, .2]]$....output will be 1.08169...but I still don't understand the mathematica code. That's why I was asking if someone could convert it into mathematical equation and/or formula. $\endgroup$
    – Vick
    Feb 21, 2020 at 10:04
  • $\begingroup$ The only difference to the orginal syntax is that you have to use StieltjesE[5][.2] by now. (No need for btw. if you input inexact numbers btw.) $\endgroup$ Feb 21, 2020 at 11:42
  • 1
    $\begingroup$ What the function does is a Laurant exansion about $x = \infty$. $\endgroup$ Feb 21, 2020 at 11:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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