help needed for lagrange interpolation formula.

hi guys, i did a function for lagrange interpolation formula. However i`m stuck on how do i substitute values into the power

here is my code snippet

1
2
3
4
5
6
7
8
9
10
11
    for(int i=1;i<getSecretShare+1;i++)
    {
        //calculateValue=getSecret+(10*p)+(2 * pow(p,2));

            int storeThreshold=getThreshold;
            calculateValue=getSecret+(10*i)+(2 * pow(i,2));
            calculateFinalValue=calculateValue % 17 ;
            cout<<"the calculated value is "<<calculateFinalValue<<endl;
        

    }


the formula that i want to implement is that a + a1x^1 + a2x^2 + a(t-1) ^t-1
i have gotten the substitution of the x value out.
However, i am stuck on how do i calculate the values according to the threshold which the user keys in
for example if the user keys in 3
it will into account the no. of shares to generate which is for example 5 in this case, then it will then take into account the threshold keyed in , which is 3, i will then calculate using the formula using the no. of shares and also threshold.
if the threshold is 3 and the number of shares to generate is 5
i should have the following
y(1) = 13 + a1(1)^1 +a2(1)^2
y(2) = 13 + a2(2)^1 +a2(2)^2

the power depends on the threshold which is t-1, 3-1 =2
so i will have to generate two a values based on that and also calculate them according to the power that is dependent on the threshold
the first value being to the power of 1 and the 2nd being to the power of 2
if the threshold is 4.

then i will generate 3 a values and then calculate them
y(1) = 13 + a1(1)^1 + a2(1)^1 + a3(1)^3

and so on..

Hope you guys can help me with this problem. Thanks in advance!
Last edited on
You could use a loop. You'll need to store all the coefficients a0,a1,a2, ... in a bidimensionnal array.
1
2
3
4
5
6
7
8
for(int k = 0; k < Whatever; k++)
{
    y[k] = 0;
    for(int degree = 0; degree < threshold; degree++)
    {
        y[k] += pow(a[k][degree], degree);
    }
}

ok so i will have to store the coefficients in a 2d array for this and i presume that a is the name of the 2d array?
why is there a y[k] in the outer for loop?
Last edited on
It's your y
ah i see! thanks for your help!
Topic archived. No new replies allowed.