# Need help with C++ code

• C/++/#

## Summary:

need help with C++ code

## Main Question or Discussion Point

I'm not at all C++ literate, but I need to understand what a C++ code is doing for a math problem regarding the Stieltjes polynomials.
Especially, I want to know what is happening to the "num" and "den" in the code; the code is in this link:

Stieltjes

Related Programming and Computer Science News on Phys.org
FactChecker
Gold Member
Do you have the reference that it mentions in a comment at the top? In another comment, it says that it is trying to follow the exact notation in the reference. That might help you.

Do you have the reference that it mentions in a comment at the top? In another comment, it says that it is trying to follow the exact notation in the reference. That might help you.
It refers to this paper: Patterson but I don't quite understand how the num and den are being used in order to yield

$$E_{m}(x) = \frac{num}{den} * ??$$

DrClaude
Mentor
Have you read the comments in the code? It is an implementation of eq. (12) in the article, and a pretty straightforward one at that.

Have you read the comments in the code? It is an implementation of eq. (12) in the article, and a pretty straightforward one at that.
I don't understand the C++ code and once the num and den are obtained, I just don't understand what is happening to them ...can you help me please?

The num/den are coefficients, but what do we do with them and to which degree of the Legendre polynomial, do we multiply them with in order to obtain an evaluation of the Stieltjes polynomials?

DrClaude
Mentor
The variables num and den stand for numerator and denominator, as they are the numerator and denominator of the RHS of eq. (12) that multiply ##S_{i-1,k}/S{r-k,k}##. Note that the definition of ##S_{i,k}/S{r-k,k}## is recursive, so the variable ratio keeps the current value of ##S_{i-1,k}/S{r-k,k}## to be kept for the next iteration.

The variable m_a appears to be the ##a## in eq. (11). The use of -= implements the subtraction of terms in eq. (11).

sysprog
The variables num and den stand for numerator and denominator, as they are the numerator and denominator of the RHS of eq. (12) that multiply ##S_{i-1,k}/S{r-k,k}##. Note that the definition of ##S_{i,k}/S{r-k,k}## is recursive, so the variable ratio keeps the current value of ##S_{i-1,k}/S{r-k,k}## to be kept for the next iteration.

The variable m_a appears to be the ##a## in eq. (11). The use of -= implements the subtraction of terms in eq. (11).
I'm trying to code them in VBA. For example for ##m=5## I obtain the following num/den: ##140/176##, ##180/440## and ##720/936##.

How do I multiply these coefficients with ##\frac{S_{i-1,k}}{S_{r-k,k}}##?
Does that mean, I must multiply by eq.(7) and integrate as well or just the ##P_{2i-1-q}(x)##?

VBA code for Stieltjes:
Function stielt(x, m)
n = m - 1
If n Mod 2 <> 0 Then
q = 1
r = (n - 1) / 2 + 2
Else
q = 0
r = n / 2 + 1
End If

t = 0
For k = 1 To r - 1
For i = r + 1 - k To r
ii = i - 1
iii = r - k
num = (n - q + 2 * (i + k - 1)) * (n + q + 2 * (k - i + 1)) * (n - 1 - q + 2 * (i - k)) * (2 * (k + i - 1) - 1 - q - n)
den = (n - q + 2 * (i - k)) * (2 * (k + i - 1) - q - n) * (n + 1 + q + 2 * (k - i)) * (n - 1 - q + 2 * (i + k))
t = t - (num / den * legpoly(x, 2 * ii - 1 - q) / legpoly(x, 2 * iii - 1 - q))
MsgBox " " & num & vbCrLf & " " & den & vbCrLf & "k=" & k & vbCrLf & "i=" & i & vbCrLf & " " & t
Next
Next
stielt = t
End Function

DrClaude
Mentor
Translating to another language should be trivial. Simply convert the instructions to VBA.

Translating to another language should be trivial. Simply convert the instructions to VBA.
I already used eq. (6) to obtain a system of simultaneous equations and solved them by Gauss-Jordan Elimination to obtain the coefficients, and once I've done that, I just have to multiply the latter with Legendre polynomials as per eq. (4) and I got my evaluation for Stieltes polynomials in terms of ##E_{m}(x)=##

However using just eqs. (6) and (4) and obtaining system of equations and solving them to obtain coefficients is tedious.

Whereas the eq.(12) num/den is already providing the coefficients without simultaneous equations. But I don't understand what I am to do with the obtained coefficients in this case to evaluate it for the Stieltjes polynomials!

DrClaude
Mentor
Whereas the eq.(12) num/den is already providing the coefficients without simultaneous equations. But I don't understand what I am to do with the obtained coefficients in this case to evaluate it for the Stieltjes polynomials!
That is taken care of here:
C++:
    Real operator()(Real x) const
{
// Trivial implementation:
// Em += m_a[i]*legendre_p(2*i - 1, x);  m odd
// Em += m_a[i]*legendre_p(2*i - 2, x);  m even
It appears to be the same thing you said you were doing, namely taking the coefficients and multiplying by the relevant Legendre polynomial.

That is taken care of here:
C++:
    Real operator()(Real x) const
{
// Trivial implementation:
// Em += m_a[i]*legendre_p(2*i - 1, x);  m odd
// Em += m_a[i]*legendre_p(2*i - 2, x);  m even
It appears to be the same thing you said you were doing, namely taking the coefficients and multiplying by the relevant Legendre polynomial.
I don't get the required result. The coefficients for m=5 are as above and multiplying by the legendre polynomials as per the formula you posted above does not cut it....