Esempio n. 1
0
        /// <summary>Divides the polynomial by the given polynomial and stores the remainder in <paramref name="remainder"/>.</summary>
        public void Divide(Polynomial value, out Polynomial remainder)
        {
            if (value == null)
            {
                throw new ArgumentNullException();
            }

            double mainFactor = 1 / value.coefficients[value.Degree]; // this should catch division by zero

            double[] remain = new double[Length];
            ArrayUtility.FastCopy(coefficients, remain, Length);

            int i = Degree;

            for (int end = Degree - value.Degree; i > end; i--)
            {
                coefficients[i] = 0;
            }
            for (; i >= 0; i--)
            {
                double quot = remain[value.Degree + i] * mainFactor;
                coefficients[i] = quot;
                for (int j = value.Degree + i - 1; j >= i; j--)
                {
                    remain[j] -= quot * value.coefficients[j - i];
                }
            }
            for (i = value.Degree; i <= Degree; i++)
            {
                remain[i] = 0;
            }

            remainder = new Polynomial(remain, false);
            Degree   -= value.Degree;
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public void EvaluateJacobian(double[] input, Matrix matrix)
        {
            if (input == null || matrix == null)
            {
                throw new ArgumentNullException();
            }

            function.Evaluate(input, output);
            ArrayUtility.FastCopy(input, input2, input.Length);
            for (int x = 0; x < input2.Length; x++)
            {
                double arg = input[x], h = arg * factor;
                if (h == 0)
                {
                    h = factor;
                }
                input2[x] = arg + h;         // this trick ensures that the value of h added to the input value is exactly the same as the value of h used
                h         = input2[x] - arg; // in the divisor later. essentially, it eliminates a source of error caused by the difference of precision
                function.Evaluate(input2, output2);
                input2[x] = arg;
                for (int y = 0; y < output.Length; y++)
                {
                    matrix[y, x] = (output2[y] - output[y]) / h;
                }
            }
        }
Esempio n. 3
0
        Polynomial(double[] coefficients, int length, int degree)
        {
            double[] array = new double[length];
            ArrayUtility.FastCopy(coefficients, array, degree + 1);

            this.coefficients = array;
            Degree            = degree;
        }
Esempio n. 4
0
        void Initialize(double[] coefficients, bool clone)
        {
            int degree = coefficients.Length; // find the degree of the polynomial

            while (degree > 0 && coefficients[--degree] == 0)
            {
            }

            // if the array is substantially larger than needed or we have to clone it, then reallocate it
            if (clone || coefficients.Length - degree > 5 && degree <= coefficients.Length / 2)
            {
                double[] array = new double[degree + 1];
                ArrayUtility.FastCopy(coefficients, array, array.Length);
                coefficients = array;
            }

            this.coefficients = coefficients;
            Degree            = degree;
        }
Esempio n. 5
0
        /// <inheritdoc/>
        public void EvaluateGradient(double[] input, double[] gradient)
        {
            if (input == null || gradient == null)
            {
                throw new ArgumentNullException();
            }

            double value = function.Evaluate(input);

            ArrayUtility.FastCopy(input, input2, input.Length);
            for (int x = 0; x < input2.Length; x++)
            {
                double arg = input[x], h = arg * factor;
                if (h == 0)
                {
                    h = factor;
                }
                input2[x] = arg + h;         // this trick ensures that the value of h added to the input value is exactly the same as the value of h used
                h         = input2[x] - arg; // in the divisor later. essentially, it eliminates a source of error caused by the difference of precision
                double value2 = function.Evaluate(input2);
                input2[x]   = arg;
                gradient[x] = (value2 - value) / h;
            }
        }
Esempio n. 6
0
 /// <summary>Returns an array containing the coefficients of the polynomial. The coefficients will be returned in the same format as
 /// that accepted by the <see cref="Polynomial(double[])"/> constructor. That is, the first value will be the constant term, the second
 /// value will be the linear term, etc.
 /// </summary>
 public double[] ToArray()
 {
     double[] array = new double[Length];
     ArrayUtility.FastCopy(coefficients, array, Length);
     return(array);
 }
Esempio n. 7
0
 /// <summary>Copies the coefficients of the polynomial into the array at the given index. The coefficients will be written in the same
 /// format as that accepted by the <see cref="Polynomial(double[])"/> constructor. That is, the first value will be the constant term,
 /// the second value will be the linear term, etc.
 /// </summary>
 public void CopyTo(double[] array, int index)
 {
     Utility.ValidateRange(array, index, Length);
     ArrayUtility.FastCopy(coefficients, 0, array, index, Length);
 }
Esempio n. 8
0
 /// <summary>Copies data from another vector into this one.</summary>
 public void Assign(Vector other)
 {
     ValidateSameSize(other);
     ArrayUtility.FastCopy(other.data, data, data.Length);
 }
Esempio n. 9
0
 void Initialize(double[] data, int index, int length)
 {
     Utility.ValidateRange(data, index, length);
     this.data = new double[length];
     ArrayUtility.FastCopy(data, index, this.data, 0, length);
 }
Esempio n. 10
0
 /// <summary>Returns an array containing a copy of the components from the vector.</summary>
 public double[] ToArray()
 {
     double[] array = new double[Size];
     ArrayUtility.FastCopy(data, array, array.Length);
     return(array);
 }
Esempio n. 11
0
 /// <summary>Copies the elements of the vector into an array at the given index.</summary>
 public void CopyTo(double[] array, int index)
 {
     Utility.ValidateRange(array, index, Size);
     ArrayUtility.FastCopy(data, 0, array, index, data.Length);
 }