Beispiel #1
0
        public static PolynomialMonomial operator +(PolynomialMonomial f, PolynomialMonomial g)
        {
            if (f is null || g is null)
            {
                throw new ArgumentNullException();
            }

            if (!f.VariableName.Equals(g.VariableName))
            {
                throw new PolynomialMonomialVariableNameException(f, g);
            }

            var result    = new RationalMonomialsNumber[Math.Max(f.Degree, g.Degree) + 1];
            var minDegree = Math.Min(f.Degree, g.Degree);

            for (var d = 0; d <= minDegree; ++d)
            {
                result[d] = f[d] + g[d];
            }

            for (var d = minDegree + 1; d <= f.Degree; ++d)
            {
                result[d] = f[d];
            }
            for (var d = minDegree + 1; d <= g.Degree; ++d)
            {
                result[d] = g[d];
            }

            return(new PolynomialMonomial(result, f.VariableName));
        }
Beispiel #2
0
        private static (PolynomialMonomial, PolynomialMonomial) DivisionWithRemainder(PolynomialMonomial f,
                                                                                      PolynomialMonomial g)
        {
            var resultLength = Math.Max(f.Degree - g.Degree + 1, 0);
            var result       = new RationalMonomialsNumber[resultLength];

            var fCoefficients = (RationalMonomialsNumber[])f._coefficients.Clone();
            var leadingG      = g[g.Degree];

            for (var d1 = f.Degree; d1 >= g.Degree; --d1)
            {
                if (fCoefficients[d1].IsZero)
                {
                    continue;
                }

                var newCoefficient = fCoefficients[d1] / leadingG;
                var monomDegree    = d1 - g.Degree;
                result[monomDegree] = newCoefficient;

                for (var d2 = 0; d2 <= g.Degree; ++d2)
                {
                    fCoefficients[monomDegree + d2] -= newCoefficient * g[d2];
                }
            }

            var q = new PolynomialMonomial(result, f.VariableName);

            var r = new PolynomialMonomial(fCoefficients, f.VariableName);

            return(q, r);
        }
Beispiel #3
0
        public PolynomialMonomial SetSigns(List <Sign> signs)
        {
            if (signs.Count > _coefficients.Length)
            {
                throw new ArgumentException();
            }

            var newCoef = new RationalMonomialsNumber[_coefficients.Length + 1];

            var degree = Degree;

            foreach (var s in signs)
            {
                newCoef[degree] = _coefficients[degree].SetSign(s);
                degree--;
            }

            while (degree >= 0)
            {
                newCoef[degree] = _coefficients[degree];
                degree--;
            }

            return(new PolynomialMonomial(newCoef, VariableName));
        }
Beispiel #4
0
        public PolynomialMonomial GetDerivative()
        {
            if (IsZero)
            {
                return(this);
            }

            var result = new RationalMonomialsNumber[Degree];

            for (var d = 1; d <= Degree; d++)
            {
                result[d - 1] = this[d] * d;
            }

            return(new PolynomialMonomial(result, VariableName));
        }
Beispiel #5
0
        public static PolynomialMonomial operator *(PolynomialMonomial f, PolynomialMonomial g)
        {
            if (f is null || g is null)
            {
                throw new ArgumentNullException();
            }

            if (!f.VariableName.Equals(g.VariableName))
            {
                throw new PolynomialMonomialVariableNameException(f, g);
            }

            if (f.IsZero)
            {
                return(f);
            }
            if (g.IsZero)
            {
                return(g);
            }

            var result = new RationalMonomialsNumber[f.Degree + g.Degree + 1];

            for (var i = 0; i < result.Length; ++i)
            {
                result[i] = new RationalMonomialsNumber(0, 1);
            }

            for (var d1 = 0; d1 <= f.Degree; ++d1)
            {
                for (var d2 = 0; d2 <= g.Degree; ++d2)
                {
                    result[d1 + d2] += f[d1] * g[d2];
                }
            }

            return(new PolynomialMonomial(result, f.VariableName));
        }
Beispiel #6
0
        public RationalMonomialsNumber Pow(int degree)
        {
            if (degree < 0)
            {
                throw new ArgumentOutOfRangeException();
            }

            RationalMonomialsNumber res = 1;
            var cur = this;

            while (degree != 0)
            {
                if (degree % 2 == 1)
                {
                    res *= cur;
                }

                degree /= 2;
                cur    *= cur;
            }

            return(res);
        }
Beispiel #7
0
 public Hypothesis(RationalMonomialsNumber number, Sign sign)
 {
     Number = number;
     Sign   = sign;
 }
Beispiel #8
0
 public static RationalMonomialsNumber Inverse(RationalMonomialsNumber number)
 {
     return(number.Inverse());
 }
Beispiel #9
0
 public bool Equals(RationalMonomialsNumber other)
 {
     return(_numerator.Equals(other._numerator) && _denominator.Equals(other._denominator));
 }