Advertisement
yahorrr

Untitled

Oct 18th, 2022
978
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.47 KB | None | 0 0
  1. using System;
  2.  
  3. namespace PolynomialTask
  4. {
  5.     /// <summary>
  6.     /// Represents a polynomial of integer degree n (in one variable, with real coefficients) - a[n] * x^n + a[n-1] * x^(n-1) + a[n-2] * x^(n-2) +...+ a[1] * x + a[0].
  7.     /// <see cref="http://www.berkeleycitycollege.edu/wp/wjeh/files/2015/01/algebra_note_polynomial.pdf"/>
  8.     /// Implements <see cref="ICloneable"/> and <see cref="IEquatable{T}"/> interfaces.
  9.     /// </summary>
  10.     public sealed class Polynomial : IEquatable<Polynomial>, ICloneable
  11.     {
  12.         /// <summary>
  13.         ///  Internal structure for storing coefficients of polynomial.
  14.         /// </summary>
  15.         private readonly double[] coefficients;
  16.  
  17.         /// <summary>
  18.         /// Initializes static members of the <see cref="Polynomial"/> class.
  19.         /// Set the default value of the accuracy of equality comparing two real numbers to double.Epsilon.
  20.         /// </summary>
  21.         static Polynomial()
  22.         {
  23.             AppSettings = new AppSettings { Epsilon = double.Epsilon, };
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Initializes a new instance of the <see cref="Polynomial"/> class.
  28.         /// </summary>
  29.         /// <param name="coefficients">Coefficients of polynomial according rule coefficients[0] -> a[0], coefficients[1] -> a[1], ..., coefficients[n] -> a[n].
  30.         /// </param>
  31.         /// <exception cref="ArgumentNullException">Thrown when array of coefficients is null.</exception>
  32.         /// <exception cref="ArgumentException">Thrown when array of coefficients is empty.</exception>
  33.         /// <example>
  34.         /// 0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1 -> { -1, 0.2, 3.313, 0.004, 0.05, 0.16 };
  35.         /// 3.3*x^2+2.001*x+1.21394 -> { 1.21394, 2.001, 3.3 }.
  36.         /// </example>
  37.         public Polynomial(params double[]? coefficients)
  38.         {
  39.             if (coefficients == null)
  40.             {
  41.                 throw new ArgumentNullException(nameof(coefficients), "Array of coefficients is null.");
  42.             }
  43.  
  44.             if (coefficients.Length == 0)
  45.             {
  46.                 throw new ArgumentException("Array of coefficients is empty.", nameof(coefficients));
  47.             }
  48.  
  49.             this.Degree = coefficients.Length - 1;
  50.  
  51.             this.coefficients = new double[coefficients.Length];
  52.             Array.Copy(coefficients, this.coefficients, coefficients.Length);
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Gets the AppSettings value.
  57.         /// <see cref="AppSettings"/> class.
  58.         /// </summary>
  59.         public static AppSettings AppSettings { get; }
  60.  
  61.         /// <summary>
  62.         /// Gets the degree value.
  63.         /// </summary>
  64.         /// <example>
  65.         /// The degree of polynomial 3.3*x^2+2.001*x+1.21394 is equal 2;
  66.         /// The degree of polynomial 0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1 is equal 5.
  67.         /// </example>
  68.         public int Degree { get; }
  69.  
  70.         /// <summary>
  71.         /// Returns polynomial coefficient at degree `index`.
  72.         /// </summary>
  73.         /// <param name="index">The zero-based index of the coefficient to get.</param>
  74.         /// <returns>The polynomial coefficient associated with the specified index.</returns>
  75.         /// <exception cref="ArgumentOutOfRangeException">Index is not a valid.</exception>
  76.         /// <example>
  77.         /// For polynomial 3.3*x^2+2.001*x+1.21394 at degree 1 returns 2.001;
  78.         /// For polynomial 0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1 at degree 2 returns 3.313.
  79.         /// For polynomial 0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1 at degree 4 returns 0.05.
  80.         /// </example>
  81.         public double this[int index]
  82.         {
  83.             get
  84.             {
  85.                 if (index < 0 || index > this.coefficients.Length - 1)
  86.                 {
  87.                     throw new ArgumentOutOfRangeException(nameof(index), "Index is not a valid.");
  88.                 }
  89.  
  90.                 return this.coefficients[index];
  91.             }
  92.  
  93.             private set
  94.             {
  95.                 if (index < 0 || index > this.coefficients.Length - 1)
  96.                 {
  97.                     throw new ArgumentOutOfRangeException(nameof(index), "Index is not a valid.");
  98.                 }
  99.  
  100.                 this.coefficients[index] = value;
  101.             }
  102.         }
  103.  
  104.         /// <summary>
  105.         /// Calculates the sum of two polynomials.
  106.         /// </summary>
  107.         /// <param name="lhs">Left-hand side operand.</param>
  108.         /// <param name="rhs">Right-hand side operand.</param>
  109.         /// <returns>The sum of two polynomials.</returns>
  110.         /// <exception cref="ArgumentNullException">Left-hand side operand or right-hand side operand is null.</exception>
  111.         /// <example>
  112.         /// (3.3*x^2+2.001*x+1.21394) + (0.002*x+0.1) => 3.3*x^2+2.003*x+1.31394;
  113.         /// (0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1) + (-4.4*x^3+3.3*x^2-2.2*x+1.1) => 0.16*x^5+0.05*x^4-4.396*x^3+6.613*x^2-2*x+0.1.
  114.         /// </example>
  115.         public static Polynomial operator +(Polynomial? lhs, Polynomial? rhs)
  116.         {
  117.             throw new NotImplementedException("You need to implement this method.");
  118.         }
  119.  
  120.         /// <summary>
  121.         /// Calculates the difference of two polynomials.
  122.         /// </summary>
  123.         /// <param name="lhs">Left-hand side operand.</param>
  124.         /// <param name="rhs">Right-hand side operand.</param>
  125.         /// <returns>The difference of two polynomials.</returns>
  126.         /// <exception cref="ArgumentNullException">Left-hand side operand or right-hand side operand is null.</exception>
  127.         /// <example>
  128.         /// (0.002*x+0.1) - (3.3*x^2+2.001*x+1.21394) = -3.3*x^2-1.999*x-1.11394;
  129.         /// (9*x^5-0.896*x^4+4.879*x^3+3.987*x^2-2.569*x+1.204) - (4*x^3-3*x^2-2*x+1) = 9*x^5-0.896*x^4+0.879*x^3+6.987*x^2-0.569*x+0.204.
  130.         /// </example>
  131.         public static Polynomial operator -(Polynomial? lhs, Polynomial? rhs)
  132.         {
  133.             throw new NotImplementedException("You need to implement this method.");
  134.         }
  135.  
  136.         /// <summary>
  137.         /// Calculates the product of two polynomials.
  138.         /// </summary>
  139.         /// <param name="lhs">Left-hand side operand.</param>
  140.         /// <param name="rhs">Right-hand side operand.</param>
  141.         /// <returns>The product of two polynomials.</returns>
  142.         /// <exception cref="ArgumentNullException">Left-hand side operand or right-hand side operand is null.</exception>
  143.         /// <example>
  144.         /// (3.3*x^2+2.001*x+1.21394) * (0.002*x+0.1) = 0.0066*x^3+0.334*x^2+0.20253*x+0.12139;
  145.         /// (0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1) * (-4.4*x^3+3.3*x^2-2.2*x+1.1) = -0.704*x^8+0.308*x^7-0.2046*x^6-14.498*x^5+10.0991*x^4-2.2242*x^3-0.0957*x^2+2.42*x-1.1.
  146.         /// </example>
  147.         public static Polynomial operator *(Polynomial? lhs, Polynomial? rhs)
  148.         {
  149.             throw new NotImplementedException("You need to implement this method.");
  150.         }
  151.  
  152.         /// <summary>
  153.         /// Determines whether polynomials are equal based on the equality of the coefficients at the same degrees.
  154.         /// Use Epsilon value of <see cref="AppSettings"/> class to equality compare coefficients of polynomials.
  155.         /// </summary>
  156.         /// <param name="lhs">Left-hand side operand.</param>
  157.         /// <param name="rhs">Right-hand side operand.</param>
  158.         /// <returns>true if left and right are equal; otherwise, false.</returns>
  159.         /// <example>
  160.         /// 0.5*x+1 = 0.49999999*x+1, if Polynomial.AppSettings.Epsilon = 0.00001;
  161.         /// 5.89*x-10.12300013 = 5.89*x-10.123, if Polynomial.AppSettings.Epsilon = 0.00001;
  162.         /// -0.123 = -0.1230000065, if Polynomial.AppSettings.Epsilon = 0.00001.
  163.         /// -0.123 != -0.123065, if Polynomial.AppSettings.Epsilon = 0.00001.
  164.         /// </example>
  165.         public static bool operator ==(Polynomial? lhs, Polynomial? rhs)
  166.         {
  167.             throw new NotImplementedException("You need to implement this method.");
  168.         }
  169.  
  170.         /// <summary>
  171.         /// Determines whether polynomials are not equal based on the equality of the coefficients at the same degrees.
  172.         /// Use Epsilon value of <see cref="AppSettings"/> class to equality compare coefficients of polynomials.
  173.         /// </summary>
  174.         /// <param name="lhs">Left-hand side operand.</param>
  175.         /// <param name="rhs">Right-hand side operand.</param>
  176.         /// <returns>true if left and right are not equal; otherwise, false.</returns>
  177.         /// <example>
  178.         /// 0.5*x+1 = 0.49999999*x+1, if Polynomial.AppSettings.Epsilon = 0.00001;
  179.         /// 5.89*x-10.12300013 = 5.89*x-10.123, if Polynomial.AppSettings.Epsilon = 0.00001;
  180.         /// -0.123 = -0.1230000065, if Polynomial.AppSettings.Epsilon = 0.00001.
  181.         /// -0.123 != -0.123065, if Polynomial.AppSettings.Epsilon = 0.00001.
  182.         /// </example>
  183.         public static bool operator !=(Polynomial? lhs, Polynomial? rhs)
  184.         {
  185.             throw new NotImplementedException("You need to implement this method.");
  186.         }
  187.  
  188.         /// <summary>
  189.         /// Calculates the sum of two polynomials.
  190.         /// </summary>
  191.         /// <param name="lhs">Left-hand side operand.</param>
  192.         /// <param name="rhs">Right-hand side operand polynomial.</param>
  193.         /// <returns>The sum of two polynomials.</returns>
  194.         /// <exception cref="ArgumentNullException">Left-hand side operand or right-hand side operand is null.</exception>
  195.         /// <example>
  196.         /// (3.3*x^2+2.001*x+1.21394) + (0.002*x+0.1) => 3.3*x^2+2.003*x+1.31394;
  197.         /// (0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1) + (-4.4*x^3+3.3*x^2-2.2*x+1.1) => 0.16*x^5+0.05*x^4-4.396*x^3+6.613*x^2-2*x+0.1.
  198.         /// </example>
  199.         public static Polynomial Add(Polynomial? lhs, Polynomial? rhs)
  200.         {
  201.             if (lhs)
  202.             {
  203.                
  204.             }
  205.         }
  206.  
  207.         /// <summary>
  208.         /// Calculates the difference of two polynomials.
  209.         /// </summary>
  210.         /// <param name="lhs">Left-hand side operand.</param>
  211.         /// <param name="rhs">Right-hand side operand polynomial.</param>
  212.         /// <returns>The difference of two polynomials.</returns>
  213.         /// <exception cref="ArgumentNullException">Left-hand side operand or right-hand side operand is null.</exception>
  214.         /// <example>
  215.         /// (0.002*x+0.1) - (3.3*x^2+2.001*x+1.21394) = -3.3*x^2-1.999*x-1.11394;
  216.         /// (9*x^5-0.896*x^4+4.879*x^3+3.987*x^2-2.569*x+1.204) - (4*x^3-3*x^2-2*x+1) = 9*x^5-0.896*x^4+0.879*x^3+6.987*x^2-0.569*x+0.204.
  217.         /// </example>
  218.         public static Polynomial Subtract(Polynomial? lhs, Polynomial? rhs)
  219.         {
  220.             throw new NotImplementedException("You need to implement this method.");
  221.         }
  222.  
  223.         /// <summary>
  224.         /// Calculates the product of two polynomials.
  225.         /// </summary>
  226.         /// <param name="lhs">Left-hand side operator.</param>
  227.         /// <param name="rhs">Right-hand side operator.</param>
  228.         /// <returns>The product of two polynomials.</returns>
  229.         /// <exception cref="ArgumentNullException">Left-hand side operand or right-hand side operand is null.</exception>
  230.         /// <example>
  231.         /// (3.3*x^2+2.001*x+1.21394) * (0.002*x+0.1) = 0.0066*x^3+0.334*x^2+0.20253*x+0.12139;
  232.         /// (0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1) * (-4.4*x^3+3.3*x^2-2.2*x+1.1) = -0.704*x^8+0.308*x^7-0.2046*x^6-14.498*x^5+10.0991*x^4-2.2242*x^3-0.0957*x^2+2.42*x-1.1.
  233.         /// </example>
  234.         public static Polynomial Multiply(Polynomial? lhs, Polynomial? rhs)
  235.         {
  236.             throw new NotImplementedException("You need to implement this method.");
  237.         }
  238.  
  239.         /// <summary>
  240.         /// Determines whether or not polynomials are equal based on the equality of the coefficients at the same degrees.
  241.         /// Use Epsilon value of <see cref="AppSettings"/> class to equality compare coefficients of polynomials.
  242.         /// </summary>
  243.         /// <param name="obj">The object to equality compare.</param>
  244.         /// <returns>true if polynomials are equal; otherwise, false.</returns>
  245.         public override bool Equals(object? obj)
  246.         {
  247.             throw new NotImplementedException("You need to implement this method.");
  248.         }
  249.  
  250.         /// <summary>
  251.         /// Determines whether or not polynomials are equal based on the equality of the coefficients at the same degrees.
  252.         /// Use Epsilon value of <see cref="AppSettings"/> class to equality compare coefficients of polynomials.
  253.         /// </summary>
  254.         /// <param name="other">The polynomial to equality compare.</param>
  255.         /// <returns>true if polynomials are equal; otherwise, false.</returns>
  256.         public bool Equals(Polynomial? other)
  257.         {
  258.             throw new NotImplementedException("You need to implement this method.");
  259.         }
  260.  
  261.         /// <summary>
  262.         /// Calculates the hash code for this instance.
  263.         /// </summary>
  264.         /// <returns>A 32-bit signed integer hash code.</returns>
  265.         public override int GetHashCode()
  266.         {
  267.             throw new NotImplementedException("You need to implement this method.");
  268.         }
  269.  
  270.         /// <summary>
  271.         /// Creates the string representation of current <see cref="Polynomial"/> class instance.
  272.         /// </summary>
  273.         /// <returns>The string representation of the current instance.</returns>
  274.         /// <example>
  275.         /// For polynomial with coefficients { 0.0001, -0.003, 0.31, -0.00731, 0.000402, 0.000300021 } -> "0.000300021*x^5+0.000402*x^4-0.00731*x^3+0.31*x^2-0.003*x+0.0001"
  276.         /// For polynomial with coefficients { -1.1, -0.0000007, -0.0957, -2.2242, 10.0991, -14.498, -0.2046, 0.0000012, -0.704 } -> "-0.704*x^8-0.2046*x^6-14.498*x^5+10.0991*x^4-2.2242*x^3-0.0957*x^2-1.1"
  277.         /// For polynomial with coefficients { -1, 0.2, 3.313, 0.004, 0.05, 0.16 } -> "0.16*x^5+0.05*x^4+0.004*x^3+3.313*x^2+0.2*x-1".
  278.         /// </example>
  279.         public override string ToString()
  280.         {
  281.             throw new NotImplementedException("You need to implement this method.");
  282.         }
  283.  
  284.         /// <summary>
  285.         /// Calculates a polynomial value in point x.
  286.         /// </summary>
  287.         /// <param name="x">value of variable.</param>
  288.         /// <returns>Polynomial value in point x.</returns>
  289.         /// <example>
  290.         /// Value of polynomial 1.004*x^2+0.014*x+3 in the point -0.5 is equal 3.244;
  291.         /// Value of polynomial 3.3*x^2+2.001*x+1.21394 in the point 1.5 is equal 11.64044.
  292.         /// </example>
  293.         public double CalculateValue(double x)
  294.         {
  295.             throw new NotImplementedException("You need to implement this method.");
  296.         }
  297.  
  298.         /// <summary>
  299.         /// Gets copy of coefficients of the polynomial instance.
  300.         /// </summary>
  301.         /// <returns>Coefficients of the polynomial.</returns>
  302.         public double[] GetCoefficients()
  303.         {
  304.             throw new NotImplementedException("You need to implement this method.");
  305.         }
  306.  
  307.         /// <summary>
  308.         /// Creates a shallow copy.
  309.         /// </summary>
  310.         /// <returns>A shallow copy.</returns>
  311.         public Polynomial Clone()
  312.         {
  313.             throw new NotImplementedException("You need to implement this method.");
  314.         }
  315.  
  316.         /// <summary>
  317.         /// Creates a shallow copy.
  318.         /// </summary>
  319.         /// <returns>A shallow copy.</returns>
  320.         object ICloneable.Clone()
  321.         {
  322.             throw new NotImplementedException("You need to implement this method.");
  323.         }
  324.     }
  325. }
  326.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement