public LagrangePolynomial(double[] nodes, Function function) { Polynomial result = new Polynomial(EMPTY); _nodes = (double[]) nodes.Clone(); if (CheckForRepeats(_nodes)) throw new Exception("Points mustn't be repeated"); for (int i = 0; i < _nodes.Length; i++) { result += function(nodes[i]) * Basis(i); } _coefficients = result.Coefficients; }
static void Main(string[] args) { try { Console.SetWindowSize(80, 40); double[] array1 = { 12.6, 14.5, 0, 7 }; Polynomial polyA = new Polynomial(array1); Console.WriteLine("polyA = " + polyA); double[] array2 = { 0, -20, 23 }; Polynomial polyB = new Polynomial(array2); Console.WriteLine("polyB = " + polyB); Console.WriteLine(); Console.WriteLine(); Console.WriteLine("polyA + polyB = " + (polyA + polyB)); Console.WriteLine("polyA - polyB = " + (polyA - polyB)); Console.WriteLine("polyA * polyB = " + (polyA * polyB)); Console.WriteLine(); Console.WriteLine("polyA + 69 = " + (polyA + 69)); Console.WriteLine("69 + polyB = " + (69 + polyB)); Console.WriteLine("polyA - 69 = " + (polyA - 69)); Console.WriteLine("69 - polyB = " + (69 - polyB)); Console.WriteLine(); Console.WriteLine("polyA * 3 = " + (polyA * 3)); Console.WriteLine("3 * polyB = " + (3 * polyB)); Console.WriteLine("polyA / 2 = " + (polyA / 2)); Console.WriteLine("polyB / 2 = " + (polyB / 2)); Console.WriteLine(); Console.WriteLine(); Console.WriteLine(); double[] points1 = { -1.5, -0.75, 0, 0.75, 1.5 }; Polynomial polyC = new LagrangePolynomial(points1, Math.Tan); Console.WriteLine("For f(x)=tg(x) in points:"); foreach (double point in points1) { Console.WriteLine(" x = " + point); } Console.WriteLine("Interpolation formula is:"); Console.WriteLine(polyC); Console.WriteLine(); double[,] points2 = //{ { 0, 0, 0 } } { { -1.5, -14.1014 }, { -0.75, -0.931596 }, { 0, 0 }, { 0.75, 0.931596 }, { 1.5, 14.1014 } }; polyC = new LagrangePolynomial(points2); Console.WriteLine("The same polynomial, but it's set up with 2D array in the same points:"); Console.WriteLine(polyC); Console.WriteLine(); Point[] points3 = { new Point(-1.5, -14.1014), new Point(-0.75, -0.931596), new Point(0, 0), new Point(0.75, 0.931596), new Point(1.5, 14.1014) }; Console.WriteLine("The same again, but points are set up as Point objects:"); polyC = new LagrangePolynomial(points3); Console.WriteLine(polyC); Console.WriteLine(); Console.WriteLine(); LagrangePolynomial polyD = new LagrangePolynomial(-5, 0.5, 20, Math.Sin); Console.WriteLine("Just trying to get result in the point pi/2 from polinomial by f(x)=sin(x):"); Console.WriteLine(polyD.GetResult(Math.PI / 2).ToString("0.#######")); Console.ReadKey(); } catch (Exception e) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(e); Console.ReadKey(); } }
public LagrangePolynomial(double firstNode, double step, int count, Function function) { if (step == 0) throw new Exception("Points mustn't be repeated"); _nodes = new double[count]; _nodes[0] = firstNode; for (int i = 1; i < count; i++) _nodes[i] = _nodes[i - 1] + step; Polynomial result = new Polynomial(EMPTY); for (int i = 0; i < count; i++) { result += function(_nodes[i]) * Basis(i); } _coefficients = result.Coefficients; }
public LagrangePolynomial(double[,] points) { //points[i, 0] - should store X coordinates, points[i, 1] - Y coordinates if (points.GetLength(1) != 2) throw new Exception("Points should belong to the two-dimensional plane."); int count = points.GetLength(0); _nodes = new double[count]; for (int i = 0; i < count; i++) { _nodes[i] = points[i, 0]; } if (CheckForRepeats(_nodes)) throw new Exception("Points mustn't be repeated"); Polynomial result = new Polynomial(EMPTY); for (int i = 0; i < count; i++) { result += points[i, 1] * Basis(i); } _coefficients = result.Coefficients; }
public LagrangePolynomial(Point[] points) { _nodes = new double[points.Length]; for (int i = 0; i < points.Length; i++) { _nodes[i] = points[i].X; } if (CheckForRepeats(_nodes)) throw new Exception("Points mustn't be repeated"); Polynomial result = new Polynomial(EMPTY); for (int i = 0; i < points.Length; i++) { result += points[i].Y * Basis(i); } _coefficients = result.Coefficients; }
public Polynomial Basis(int index) { Polynomial poly = new Polynomial(NOT_EMPTY); for (int i = 0; i < _nodes.Length; i++) { if (i != index) { double[] polyCoefficients = { _nodes[i], -1 }; poly *= (new Polynomial(polyCoefficients)) / (_nodes[index] - _nodes[i]); } } return poly; }
public Polynomial(Polynomial poly) { // create copy of poly _coefficients = poly.Coefficients; }