Beispiel #1
0
        private Coeff getCoeff(string elem)
        {
            if (elem.Contains("E"))
            {
                elem = elem.Replace("Ep", "E+");
                elem = elem.Replace("Em", "Em");
            }

            var c = elem.Split('^')[0].Replace("x", "").Replace("(", "").Replace(")", "").Split('/');

            Coeff toAdd;

            if (c.Length > 1)
            {
                var right = BigInteger.Parse(c[0]);
                var left  = BigInteger.Parse(c[1]);
                toAdd = new Coeff(right, left);
            }
            else
            {
                if (c.First() == "-")
                {
                    toAdd = new Coeff(-1, 1);
                }
                else if (c.First() != "" && c.First() != "-" && c.First() != "+")
                {
                    toAdd = new Coeff(BigInteger.Parse(c[0]), 1);
                }
                else
                {
                    toAdd = new Coeff(1, 1);
                }
            }
            return(toAdd);
        }
Beispiel #2
0
        private void removeZerosBefore()
        {
            var tmp = coeffs.ToList();

            tmp.Reverse();

            int   i = 0;
            Coeff c = tmp[i];

            while (c.up == 0)
            {
                tmp.Remove(c);
                if (tmp.Count > 0)
                {
                    c = tmp[i];
                }
                else
                {
                    c = new Coeff(0, 1);
                }
            }

            coeffs = tmp;
            coeffs.Reverse();
        }
Beispiel #3
0
        private void workWithCoeff(string elem)
        {
            if (elem == "")
            {
                return;
            }

            if (!elem.Contains("x"))
            {
                addInCoeffs(0, getCoeff(elem));
                return;
            }

            if (!elem.Contains("^"))
            {
                addInCoeffs(1, getCoeff(elem));
                return;
            }

            var pow = int.Parse(elem.Split('^')[1]);


            Coeff toAdd = getCoeff(elem);


            addInCoeffs(pow, toAdd);
        }
Beispiel #4
0
        public static KeyValuePair <Polynome, Polynome> operator /(Polynome p1, Polynome p2)
        {
            var result = new Polynome("0");

            if (p1.coeffs.Count < p2.coeffs.Count)
            {
                return(new KeyValuePair <Polynome, Polynome>(new Polynome("0"), p1));
            }

            var index = p1.coeffs.Count - 1;

            int pow = p1.coeffs.Count - p2.coeffs.Count;

            var divided = new Polynome(p1.coeffs);

            var divider = new Polynome("0");

            do
            {
                divider = new Polynome("0");

                Coeff c = divided.coeffs[index] / p2.coeffs[p2.coeffs.Count - 1];

                result = result + new Polynome(c + "x^" + pow);

                divider = p2 * new Polynome(c + "x^" + pow);

                divided -= divider;

                index--;
                pow--;
            } while (pow >= 0);

            return(new KeyValuePair <Polynome, Polynome>(result, divided));
        }
Beispiel #5
0
 private void addInCoeffs(int pow, Coeff coeff)
 {
     if (coeffs.Count <= pow)
     {
         int dest = pow - coeffs.Count;
         for (int i = 0; i <= dest; i++)
         {
             coeffs.Add(new Coeff(0, 0));
         }
     }
     coeffs[pow] = coeff;
 }
Beispiel #6
0
        public static Coeff operator /(Coeff c1, Coeff c2)
        {
            Coeff temp;

            if (c2.up < 0)
            {
                temp = new Coeff(-c2.down, -c2.up);
            }
            else
            {
                temp = new Coeff(c2.down, c2.up);
            }
            return(c1 * temp);
        }
Beispiel #7
0
        public static Coeff operator -(Coeff c1, Coeff c2)
        {
            if (c1.down == 0 && c1.up == 0)
            {
                return(new Coeff(-c2.up, c2.down));
            }
            if (c2.up == 0 && c2.down == 0)
            {
                return(c1);
            }
            Coeff res = new Coeff(c1.up * c2.down - c2.up * c1.down, c1.down * c2.down);

            return(res);
        }
Beispiel #8
0
        public static Polynome Lagrange(List <int> values)
        {
            List <Polynome> parts = new List <Polynome>();

            for (int i = 0; i < values.Count; ++i)
            {
                var coef = 1;
                var tek  = new List <Polynome>();
                for (int j = 0; j < values.Count; ++j)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    var newTemp = new Polynome(new List <Coeff>()
                    {
                        new Coeff(-j, 1),
                        new Coeff(1, 1)
                    });
                    //var newTemp = new Coeff(-j, 1);new Fraction[]
                    //{
                    //    new Fraction(-j, 1),
                    //    new Fraction(1, 1)
                    //});
                    tek.Add(newTemp);
                    coef *= (i - j);
                }
                if (tek.Count != 0)
                {
                    var t    = new Coeff(values[i], coef);
                    var temp = tek.Aggregate((x, y) => x * y);
                    temp.coeffs = temp.coeffs.Select(x => x * t).ToList();
                    //temp.coeffs.Clear();
                    if (temp.coeffs.Count != 0)
                    {
                        parts.Add(temp);
                    }
                }
            }

            if (parts.Count == 0)
            {
                return(new Polynome("0"));
            }

            return(parts.Aggregate((i, k) => i + k));
            //return null;
        }
Beispiel #9
0
        public static Polynome operator *(Polynome p1, Polynome p2)
        {
            List <Coeff> result = new List <Coeff>();

            var maxPow = p1.coeffs.Count + p2.coeffs.Count;

            for (int i = 0; i < maxPow; i++)
            {
                result.Add(new Coeff(0, 0));
            }

            for (int i = 0; i < p1.coeffs.Count; i++)
            {
                for (int j = 0; j < p2.coeffs.Count; j++)
                {
                    var c   = new Coeff(p1.coeffs[i] * p2.coeffs[j]);
                    var pow = i + j;
                    result[pow] += c;
                }
            }

            return(new Polynome(result));
        }
Beispiel #10
0
 public Coeff(Coeff c)
 {
     this.down = c.down;
     this.up   = c.up;
 }