using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _08_Interpolacja
{
    public class Interpolacja
    {
        int N;
        double X;
        double[] x;
        double[] y;

        public double WielLagr1(double[] x, double[] y, double X)
        {
            this.x = x;
            this.y = y;
            this.X = X;
            N = x.Length;
            double sum = 0;
            for (int i = 0; i < N; i++)
                sum += y[i] * u(i);
            return sum;
        }
        private double u(int i)
        {
            double w = 1;
            for (int j = 0; j < N; j++)
                if (j != i)
                    w *= ((X - x[j]) / (x[i] - x[j]));
            return w;
        }


        private double omega()
        {
            double om = 1;
            foreach (double xi in x)
                om *= (X - xi);
            return om;
        }

        private double um(int i)
        {
            double w = 1/(X - x[i]);
            for (int j = 0; j < N; j++)
                if (j != i)
                    w /= (x[i] - x[j]);
            return w;
        }


        public double WielLagr2(double[] x, double[] y, double X)
        {
            this.x = x;
            this.y = y;
            this.X = X;
            N = x.Length;
            
            double sum = 0;
            for (int i = 0; i < N; i++)
                sum += y[i] * um(i);
            return sum* omega();
        }

    }
}
