Git Product home page Git Product logo

lagrange_polynomial's Introduction

Lagrange polynomial

Note

I am interested in how interpolating polynomial function by some x y point

And I found Lagrange polynomial can do that

This project started when I am in secondary school, originally written in C, and I rewrote it in C#

Note

This is part of CWKSC/MyLib_Csharp C# library, old and no longer maintain

If you include this library, its functionality is already included

There are some extension methods for printing on Demo

For example: "42".Print() == Console.Write("42"), "42".Printlnln() == Console.WriteLine("42" + "\n");

Usage

(double, double) points = { /*...*/ };
LagrangePolynomial fx = new LagrangePolynomial(points);

fx.Generate(); // return formula string
fx.Print(); // print formula // Equal to Console.Write(fx.Generate()) or fx.Generate().Print()
fx.Invoke(<x>); // input x to formula and return result (double)

// Also provide static method
LagrangePolynomial.Generate(points);
LagrangePolynomial.Print(points);
LagrangePolynomial.Invoke(<x>, points);

Demo

(double, double)[] points = { (2, 7), (0, -9), (3, -3), (4, 6), (-5, 3), (1, 5), (-6, 2) };

("Generate LagrangePolynomial of " + points.ToStr()).Printlnln();

// Instance
"Using Instance Method".Printlnln();

LagrangePolynomial fx = new LagrangePolynomial(points);

"f(x) = ".Println();
fx.Generate().Printlnln();
"f(5) = ".Print();
fx.Invoke(5).Printlnlnln();


// Static Method
"Using Static Method".Printlnln();

"f(x) = ".Println();
LagrangePolynomial.Generate(points).Printlnln();
"f(5) = ".Print();
LagrangePolynomial.Invoke(5, points).Println();

Output

Generate LagrangePolynomial of (2, 7), (0, -9), (3, -3), (4, 6), (-5, 3), (1, 5), (-6, 2)

Using Instance Method

f(x) =
(7)*((x-0)/(2))*((x-3)/(-1))*((x-4)/(-2))*((x--5)/(7))*((x-1)/(1))*((x--6)/(8))+((x-2)/(-2))*(-9)*((x-3)/(-3))*((x-4)/(-4))*((x--5)/(5))*((x-1)/(-1))*((x--6)/(6))+((x-2)/(1))*((x-0)/(3))*(-3)*((x-4)/(-1))*((x--5)/(8))*((x-1)/(2))*((x--6)/(9))+((x-2)/(2))*((x-0)/(4))*((x-3)/(1))*(6)*((x--5)/(9))*((x-1)/(3))*((x--6)/(10))+((x-2)/(-7))*((x-0)/(-5))*((x-3)/(-8))*((x-4)/(-9))*(3)*((x-1)/(-6))*((x--6)/(1))+((x-2)/(-1))*((x-0)/(1))*((x-3)/(-2))*((x-4)/(-3))*((x--5)/(6))*(5)*((x--6)/(7))+((x-2)/(-8))*((x-0)/(-6))*((x-3)/(-9))*((x-4)/(-10))*((x--5)/(-1))*((x-1)/(-7))*(2)

f(5) = 121.34126984126983


Using Static Method

f(x) =
(7)*((x-0)/(2))*((x-3)/(-1))*((x-4)/(-2))*((x--5)/(7))*((x-1)/(1))*((x--6)/(8))+((x-2)/(-2))*(-9)*((x-3)/(-3))*((x-4)/(-4))*((x--5)/(5))*((x-1)/(-1))*((x--6)/(6))+((x-2)/(1))*((x-0)/(3))*(-3)*((x-4)/(-1))*((x--5)/(8))*((x-1)/(2))*((x--6)/(9))+((x-2)/(2))*((x-0)/(4))*((x-3)/(1))*(6)*((x--5)/(9))*((x-1)/(3))*((x--6)/(10))+((x-2)/(-7))*((x-0)/(-5))*((x-3)/(-8))*((x-4)/(-9))*(3)*((x-1)/(-6))*((x--6)/(1))+((x-2)/(-1))*((x-0)/(1))*((x-3)/(-2))*((x-4)/(-3))*((x--5)/(6))*(5)*((x--6)/(7))+((x-2)/(-8))*((x-0)/(-6))*((x-3)/(-9))*((x-4)/(-10))*((x--5)/(-1))*((x-1)/(-7))*(2)

f(5) = 121.34126984126983

lagrange_polynomial's People

Contributors

cwksc avatar

Stargazers

JackSon_tm.m avatar Jerry Lum avatar niyaziberber avatar Chizi avatar  avatar DSEnotHard avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.