Ejemplo n.º 1
0
/*
 *              /// <summary>
 * /// Creating an additional polynom for using Lagrange's interpolating method.
 * /// </summary>
 * /// <param name="arg"></param>
 * /// <param name="val"></param>
 *              public RealPolynom Interpolate(double[] arg)
 *              {
 *                      if(arg.Length != val.Length)
 *                      {
 *                              throw new Exception("Number of arguments is not eqal to number of values");
 *                      }
 *                      RealPolynom r = new RealPolynom(arg.Length);
 *                      r[0] = 1;
 *                      RealPolynom mult = new RealPolynom(2);
 *                      mult[1] = 1;
 *                      for(int i = 0; i < arg.Length; i++)
 *                      {
 *                              for(int j = 0; j < arg.Length; j++)
 *                              {
 *                                      if(i != j){}
 *                              }
 *                      }
 *
 *              }*/

        /// <summary>
        /// Clones itself
        /// </summary>
        /// <returns>Clone</returns>
        public object Clone()
        {
            RealPolynom p = new RealPolynom(coeffs.Length);

            p.coeffs = coeffs.Clone() as double[];
            return(p);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sum
        /// </summary>
        /// <param name="p">First term</param>
        /// <param name="q">Second term</param>
        /// <returns>Sum</returns>
        static public RealPolynom operator +(RealPolynom p, RealPolynom q)
        {
            RealPolynom r = new RealPolynom(0);

            if (p.coeffs.Length < q.coeffs.Length)
            {
                for (int i = 0; i < p.coeffs.Length; i++)
                {
                    r.coeffs[i] = p.coeffs[i] + q.coeffs[i];
                }
                for (int i = p.coeffs.Length - 1; i < q.coeffs.Length; i++)
                {
                    r.coeffs[i] = q.coeffs[i];
                }
                return(r);
            }

            for (int i = 0; i < q.coeffs.Length; i++)
            {
                r.coeffs[i] = p.coeffs[i] + q.coeffs[i];
            }
            for (int i = q.coeffs.Length - 1; i < p.coeffs.Length; i++)
            {
                r.coeffs[i] = p.coeffs[i];
            }

            r.Reduce();

            return(r);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Minus constant left
        /// </summary>
        /// <param name="d">Constant</param>
        /// <param name="p">Polynom</param>
        /// <returns>Result polynom</returns>
        static public RealPolynom operator -(double d, RealPolynom p)
        {
            RealPolynom r = -p;

            r.coeffs[0] += d;
            return(r);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Minus constant
        /// </summary>
        /// <param name="p">Polynom</param>
        /// <param name="d">Constant</param>
        /// <returns>Result polynom</returns>
        static public RealPolynom operator -(RealPolynom p, double d)
        {
            RealPolynom r = p.Clone() as RealPolynom;

            r.coeffs[0] -= d;
            return(r);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Plus constant left
        /// </summary>
        /// <param name="d">Constant</param>
        /// <param name="p">Polynom</param>
        /// <returns>Result polynom</returns>
        static public RealPolynom operator +(double d, RealPolynom p)
        {
            RealPolynom r = p.Clone() as RealPolynom;

            r.coeffs[0] += d;
            r.Reduce();
            return(r);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Derivation
        /// </summary>
        /// <param name="p">Prototype</param>
        /// <returns>Redult polynon</returns>
        static public RealPolynom operator !(RealPolynom p)
        {
            RealPolynom r = new RealPolynom(p.coeffs.Length - 1);

            for (int i = 0; i < r.coeffs.Length; i++)
            {
                r.coeffs[i] = p.coeffs[i + 1] * i;
            }
            return(r);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Integration
        /// </summary>
        /// <param name="p">Prototype</param>
        /// <returns>Redult polynon</returns>
        static public RealPolynom operator ~(RealPolynom p)
        {
            RealPolynom r = new RealPolynom(p.coeffs.Length + 1);

            for (int i = 0; i < p.coeffs.Length; i++)
            {
                r.coeffs[i + 1] = p.coeffs[i] / (double)(i + 1);
            }
            r.coeffs[0] = 0;
            return(r);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Multiply to constant left
        /// </summary>
        /// <param name="d">Constant</param>
        /// <param name="p">Polynom</param>
        /// <returns>Result polynom</returns>
        static public RealPolynom operator *(double d, RealPolynom p)
        {
            RealPolynom r = p.Clone() as RealPolynom;

            for (int i = 0; i < r.coeffs.Length; i++)
            {
                r.coeffs[i] *= d;
            }
            r.Reduce();
            return(r);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Minus
        /// </summary>
        /// <param name="p">Argument</param>
        /// <returns>Minus</returns>
        static public RealPolynom operator -(RealPolynom p)
        {
            RealPolynom r = new RealPolynom(p.coeffs.Length);

            for (int i = 0; i < p.coeffs.Length; i++)
            {
                p.coeffs[i] = -p.coeffs[i];
            }

            return(r);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Divide to constant
        /// </summary>
        /// <param name="p">Polynom</param>
        /// <param name="d">Constant</param>
        /// <returns>Result polynom</returns>
        static public RealPolynom operator /(RealPolynom p, double d)
        {
            if (d == 0)
            {
                throw new Exception("Can not divide by zero");
            }
            RealPolynom r = p.Clone() as RealPolynom;

            for (int i = 0; i < r.coeffs.Length; i++)
            {
                r.coeffs[i] /= d;
            }
            return(r);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Multiplication
        /// </summary>
        /// <param name="p">First term</param>
        /// <param name="q">Second term</param>
        /// <returns>Product</returns>
        static public RealPolynom operator *(RealPolynom p, RealPolynom q)
        {
            if ((p.isZero) | (q.isZero))
            {
                RealPolynom r = new RealPolynom(1);
                return(r);
            }

            RealPolynom s = new RealPolynom(p.coeffs.Length + q.coeffs.Length - 1);

            for (int i = 0; i < p.coeffs.Length; i++)
            {
                for (int j = 0; j < q.coeffs.Length; j++)
                {
                    s.coeffs[i + j] += p.coeffs[i] * q.coeffs[j];
                }
            }
            return(s);
        }