Класс Polynomial
1 часть заданияunknown
csharp
2 years ago
5.7 kB
7
Indexable
using System; namespace WindowsFormsApp1 { using System.Text; public class Polynomial { private readonly double[] _coefficients; public double[] Coefficients => (double[])_coefficients.Clone(); public int Degree => _coefficients.Length - 1; public Polynomial(double[] c) { var n = c.Length; if (n == 0) throw new ArgumentException("Пустой массив коэффициентов полиномов"); while (n > 1 && c[n - 1] == 0.0) --n; var ctx = new double[n]; Array.Copy(c, 0, ctx, 0, n); _coefficients = ctx; } public double Evaluate(double x) { var n = Degree; var result = _coefficients[n]; for (var j = n - 1; j >= 0; --j) result = x * result + _coefficients[j]; return result; } public static Polynomial operator +(Polynomial u, Polynomial p) { var lowLength = Math.Min(u._coefficients.Length, p._coefficients.Length); var highLength = Math.Max(u._coefficients.Length, p._coefficients.Length); var newCoefficients = new double[highLength]; for (var i = 0; i < lowLength; ++i) { newCoefficients[i] = u._coefficients[i] + p._coefficients[i]; } var src = u._coefficients.Length < p._coefficients.Length ? p._coefficients : u._coefficients; Array.Copy(src, lowLength, newCoefficients, lowLength, highLength - lowLength); return new Polynomial(newCoefficients); } public static Polynomial operator -(Polynomial u, Polynomial p) { var lowLength = Math.Min(u._coefficients.Length, p._coefficients.Length); var highLength = Math.Max(u._coefficients.Length, p._coefficients.Length); var newCoefficients = new double[highLength]; int i; for (i = 0; i < lowLength; ++i) newCoefficients[i] = u._coefficients[i] - p._coefficients[i]; if (u._coefficients.Length < p._coefficients.Length) { for (i = lowLength; i < highLength; ++i) newCoefficients[i] = -p._coefficients[i]; } else { Array.Copy(u._coefficients, lowLength, newCoefficients, lowLength, highLength - lowLength); } return new Polynomial(newCoefficients); } public static Polynomial operator --(Polynomial p) { var n = p.Degree; // Проверяем, является ли полином уже многочленом нулевой степени if (n == 0) return new Polynomial(new[] { 0.0 }); var newCoefficients = new double[n]; Array.Copy(p._coefficients, 0, newCoefficients, 0, n); return new Polynomial(newCoefficients); } public static Polynomial operator *(Polynomial u, Polynomial p) { var newCoefficients = new double[u._coefficients.Length + p._coefficients.Length - 1]; for (var i = 0; i < newCoefficients.Length; ++i) { newCoefficients[i] = 0.0; var n = Math.Min(u._coefficients.Length, i + 1); for (var j = Math.Max(0, i + 1 - p._coefficients.Length); j < n; ++j) { newCoefficients[i] += u._coefficients[j] * p._coefficients[i - j]; } } return new Polynomial(newCoefficients); } public Polynomial Differentiate() { var degree = Degree; if (degree == 0) return new Polynomial(new double[] { 0, 0 }); var result = new double[degree]; for (var i = degree; i > 0; --i) result[i - 1] = i * _coefficients[i]; return new Polynomial(result); } public Polynomial Integrate() { var degree = Degree; if (degree == 0) return new Polynomial(new double[] { 0, 0 }); var result = new double[degree]; for (var i = degree; i > 0; --i) result[i - 1] = _coefficients[i] / (i + 1); return new Polynomial(result); } public override string ToString() { StringBuilder s = new StringBuilder(); for (var i = 0; i < _coefficients.Length; ++i) { if (_coefficients[i] != 0.0) { if (s.Length > 0) { s.Append(_coefficients[i] < 0.0 ? " - " : " + "); } else if (_coefficients[i] < 0.0) { s.Append('-'); } double absAi = Math.Abs(_coefficients[i]); if (absAi - 1.0 != 0.0) { s.Append(absAi).Append(' '); } s.Append('x'); if (i > 1) { s.Append('^').Append(i); } } } return s.ToString(); } } }
Editor is loading...
Leave a Comment