public string PolynomialAddTests2(double[] coefficients, int[] degrees, double coefficient, int degree)
        {
            var polynomial1 = new Polynomial(coefficients, degrees);
            var tuple       = new Tuple <double, int>(coefficient, degree);

            return(Polynomial.Add(polynomial1, tuple).ToString());
        }
        public void PolynomialToString_randomPolynomial()
        {
            Random     rand = new Random(DateTime.Now.Second);
            Polynomial a    = new Polynomial();
            int        n    = rand.Next(2, 1000);

            for (int i = 0; i < n; i++)
            {
                a.Add(new Term(rand.Next(), new System.Numerics.Complex(rand.NextDouble(), rand.NextDouble())));
            }

            String[] s = a.ToString().Split('\n');
            Assert.IsFalse(s.Length == 0);
            double temp  = 0.0;
            int    itemp = 0;
            int    cnt   = 0;

            foreach (var line in s)
            {
                if (string.IsNullOrWhiteSpace(line)) // last line
                {
                    Assert.AreEqual(a.Count, cnt);
                    continue;
                }
                cnt++;
                string[] current = line.Split('(', ')', ',');
                Assert.AreEqual(6, current.Length);
                Assert.IsTrue(double.TryParse(current[1], out temp));
                Assert.IsTrue(double.TryParse(current[2], out temp));
                Assert.IsTrue(int.TryParse(current[4], out itemp));
            }
        }
示例#3
0
    //////////////////////////////////////////////////////////////////////
    //définition des opérations de math de base
    //////////////////////////////////////////////////////////////////////

    //définition de l'addition
    public static Polynomial operator +(Polynomial polynomial, float f)
    {
        Polynomial clone = polynomial.Clone();

        clone.Add(f);
        return(clone);
    }
示例#4
0
        public void Add_secondPolynomIsNull_ThrowsArgumentNullException()
        {
            Polynomial firstPolynim  = null;
            Polynomial secondPolynom = new Polynomial(new double[] { 1 });

            Assert.Throws <ArgumentNullException>(() => Polynomial.Add(firstPolynim, secondPolynom));
        }
        public void AddTest()
        {
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    var msg = String.Format("At i={0}, j={1}", i, j);
                    var n = Math.Max(i, j) + 1;
                    var tgt = new double[n];
                    tgt[i] += 1;
                    tgt[j] += 1;

                    var c1 = new double[i + 1];
                    var c2 = new double[j + 1];

                    c1[i] = 1.0;
                    c2[j] = 1.0;

                    var p1 = new Polynomial(c1);
                    var p2 = new Polynomial(c2);

                    var p_res = Polynomial.Add(p1, p2);
                    var p_tar = new Polynomial(tgt);

                    Assert.AreEqual(p_tar.Coefficients.Length, p_res.Coefficients.Length, "length mismatch");
                    for (int k = 0; k < p_res.Coefficients.Length; k++)
                    {
                        Assert.AreEqual(p_tar.Coefficients[k], p_res.Coefficients[k], msg);
                    }
                }
            }
        }
示例#6
0
 public static void Main(String[] args) {
   // The integer polynomial 2 + 5x + x^2
   Polynomial<Int> ip = 
     new Polynomial<Int>(new Int[] { new Int(2), new Int(5), new Int(1) });
   Console.WriteLine(ip.Eval(new Int(10)));            // 152
   Console.WriteLine(ip.Add(ip).Eval(new Int(10)));    // 304 = 152 + 152
   Console.WriteLine(ip.Mul(ip).Eval(new Int(10)));    // 23104 = 152 * 152
 }
示例#7
0
        public void Add_BothPolynomialsAreEmpty_EmptyPolynomialReturned()
        {
            var p1             = new Polynomial(new List <double>());
            var p2             = new Polynomial(new List <double>());
            var expectedResult = new Polynomial(new List <double>());

            Assert.AreEqual(expectedResult, p1.Add(p2));
        }
示例#8
0
        public void Add_TwoPolynomials_ResultPolynomial(double[] first, double[] second, double[] result)
        {
            Polynomial firstPol    = new Polynomial(first);
            Polynomial secondPol   = new Polynomial(second);
            Polynomial expectedPol = new Polynomial(result);
            Polynomial actualPol   = Polynomial.Add(firstPol, secondPol);

            Assert.IsTrue(expectedPol.Equals(actualPol), "Wrong result of Add operation!");
        }
示例#9
0
        public double[] AddTest(double[] firstCoefficients, double[] secondCoefficients)
        {
            var firstPolynim  = new Polynomial(firstCoefficients);
            var secondPolynom = new Polynomial(secondCoefficients);

            var result = Polynomial.Add(firstPolynim, secondPolynom);

            return(result.Coefficients);
        }
示例#10
0
        public void AdditionTests(string a, string b, string expected)
        {
            var a_number = new Polynomial(a);
            var b_number = new Polynomial(b);

            var actual = a_number.Add(b_number).ToBit();

            Assert.AreEqual(expected, actual);
        }
        public void PolynomialAdd_ValidTwoPolynomial_ValidResult()
        {
            Polynomial poly1  = new Polynomial(1, 2, 3);
            Polynomial poly2  = new Polynomial(1, 2, 3);
            Polynomial result = Polynomial.Add(poly1, poly2);

            Polynomial expected = new Polynomial(2, 4, 6);

            Assert.AreEqual(expected, result);
        }
        static void Main(string[] args)
        {
            double[] arr  = { 1, 0, -2, -3, 5, 0, 7 };
            double[] arr1 = { 4, 0, 3, 2, 2 };
            double[] arr2 = { 4, 0, 3, 2, 2 };
            double[] arr3 = { 9, -1, 0, -7, 4 };
            double[] arr4 = { 0.9, -4, -0.0001, 0.00001, -0.4, 0.002, -0.007 };

            Polynomial obj  = new Polynomial(arr);
            Polynomial obj1 = new Polynomial(arr1);
            Polynomial obj2 = new Polynomial(arr2);
            Polynomial obj3 = new Polynomial(arr3, 0.001);
            Polynomial obj4 = new Polynomial(arr4, 0.000003);

            double[]   arr01 = { 0.9, -4, -0.0001, 0.00001, -0.4, 0.002, -0.007 };
            Polynomial obj01 = new Polynomial(arr01, 0.001);

            Console.WriteLine(obj01.toString());

            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Полиномы:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("obj3 : ");
            Console.WriteLine(obj3.toString());
            Console.WriteLine("obj4 : ");
            Console.WriteLine(obj4.toString());

            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Умножение:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("obj3 * obj4 : ");
            Console.WriteLine((obj3 * obj4).toString());
            Console.WriteLine(Polynomial.Multiply(obj3, obj4).toString());
            Console.WriteLine("obj3 * 7 : ");
            Console.WriteLine(Polynomial.MultiplyConst(obj3, 7).toString());

            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Сложение:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine((obj3 + obj4).toString());
            Console.WriteLine();
            Console.WriteLine(Polynomial.Add(obj3, obj4).toString());

            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Разность:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine((obj3 - obj4).toString());
            Console.WriteLine();
            Console.WriteLine(Polynomial.Subtract(obj3, obj4).toString());

            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine("Равны ли:");
            Console.WriteLine("----------------------------------------------------");
            Console.WriteLine(obj3.Equals(obj4));
        }
        public void AddTestCase(double[] d1, double[] d2, double[] sum)
        {
            var p1 = new Polynomial(d1);
            var p2 = new Polynomial(d2);

            var expected = new Polynomial(sum);

            Polynomial actual = Polynomial.Add(p1, p2);

            Assert.True(actual == expected);
        }
示例#14
0
        public Polynomial Fit(double[] data)
        {
            Polynomial polynomial = new Polynomial();
            Vector     coeffs     = GetCoeffs(data);

            for (int i = 0; i < coeffs.Length; i++)
            {
                polynomial.Add((uint)i, coeffs[i]);
            }

            return(polynomial);
        }
示例#15
0
        public void Add_LeftAddendIsEmpty_RightPolynomialReturned()
        {
            var p1 = new Polynomial(new List <double>());
            var p2 = new Polynomial(new List <double> {
                1, 20
            });
            var expectedResult = new Polynomial(new List <double> {
                1, 20
            });

            Assert.AreEqual(expectedResult, p1.Add(p2));
        }
        void SecondTreeExtraction(Trie trie, Polynomial firstPolynomial, ref DataTable result)
        {
            Polynomial SoFar = new Polynomial();
            Stack <Tuple <Node, Term, bool> > dfsStack = new Stack <Tuple <Node, Term, bool> >();

            dfsStack.Push(new Tuple <Node, Term, bool>(trie.root, new Term(0, 0), false));
            while (dfsStack.Count != 0)
            {
                if (dfsStack.Peek().Item3)
                {
                    dfsStack.Pop();
                    if (SoFar.Count != 0)
                    {
                        SoFar.Remove(SoFar.Back.Degree);
                    }
                    continue;
                }
                Node current = dfsStack.Peek().Item1;
                Term edge    = dfsStack.Peek().Item2;
                if (edge.Coefficient != 0)
                {
                    SoFar.Add(edge);
                }
                dfsStack.Pop();
                dfsStack.Push(new Tuple <Node, Term, bool>(current, edge, true));
                foreach (var child in current._children)
                {
                    dfsStack.Push(new Tuple <Node, Term, bool>(child.Value, new Term(child.Key), false));
                }
                if (current.isEnd)
                {
                    result.Rows.Add(result.NewRow());
                    int index = result.Rows.Count - 1;
                    result.Rows[index]["Polynomial1"] = firstPolynomial.ToString();
                    result.Rows[index]["Polynomial2"] = SoFar.ToString();
                    bool foundOperation = false;
                    foreach (var special in current._special)
                    {
                        if (Constants.operationsName[special.Key] == "Result")
                        {
                            foundOperation = true;
                            result.Rows[index][Constants.TwoPolynomialsColumnsName[special.Key]] = (special.Value as Polynomial).ToString();
                        }
                    }
                    if (!foundOperation)
                    {
                        result.Rows.RemoveAt(result.Rows.Count - 1);
                    }
                }
            }
        }
        /// <summary>
        /// The following tokens are supported:
        ///     Variables: x
        ///     Operators: +, -, *, ^
        ///
        /// Exponent type: uint
        /// Coefficient type: double
        ///
        /// Example: 2.5x^3+x+1, or 2.5*x^3+x+1
        /// </summary>
        /// <exception cref="SyntaxException"/>
        public static Polynomial TryParse(string polynomialString)
        {
            string     s          = PrepareString(polynomialString);
            Polynomial polynomial = new Polynomial();

            int pos = 0;

            polynomial.Add(TryParseItem(s, ref pos));

            while (pos < s.Length)
            {
                if (s[pos] == '+' || s[pos] == '-')
                {
                    polynomial.Add(TryParseItem(s, ref pos));
                }
                else
                {
                    throw new SyntaxException(pos);
                }
            }

            return(polynomial);
        }
示例#18
0
        public void Add_PolynomialsHaveDifferentCoefficientsCount_CorrectSumReturned()
        {
            var p1 = new Polynomial(new List <double> {
                1, 20, 4, -5, 13
            });
            var p2 = new Polynomial(new List <double> {
                1, 12, 8, 3
            });
            var expectedResult = new Polynomial(new List <double> {
                2, 32, 12, -2, 13
            });

            Assert.AreEqual(expectedResult, p1.Add(p2));
        }
示例#19
0
        public void Add_NullReference_ArgumentNullException(double[] c1, double[] c2)
        {
            Polynomial p1 = null, p2 = null;

            if (c1 != null)
            {
                p1 = new Polynomial(c1);
            }
            if (c2 != null)
            {
                p2 = new Polynomial(c2);
            }
            Assert.Throws <ArgumentNullException>(() => Polynomial.Add(p1, p2));
        }
示例#20
0
        public string PolynomialAddTests1(double[] coefficients1, int[] degrees1, double[] coefficients2, int[] degrees2)
        {
            var polynomial1 = new Polynomial(coefficients1, degrees1);
            var polynomial2 = new Polynomial(coefficients2, degrees2);

            var result1 = Polynomial.Add(polynomial1, polynomial2).ToString();
            var result2 = (polynomial1 + polynomial2).ToString();

            if (result1 != result2)
            {
                Assert.Fail();
            }

            return(result1);
        }
        DataTable TwoPolynomialsDT(PolynomialTrie trie)
        {
            DataTable result = new DataTable("Operations on two polynomials");

            foreach (var title in Constants.TwoPolynomialsColumnsName)
            {
                result.Columns.Add(title.Value, typeof(string));
            }
            Polynomial SoFar = new Polynomial();
            Stack <Tuple <Node, Term, bool> > dfsStack = new Stack <Tuple <Node, Term, bool> >();

            dfsStack.Push(new Tuple <Node, Term, bool>(trie.main.root, new Term(0, 0), false));
            while (dfsStack.Count != 0)
            {
                if (dfsStack.Peek().Item3)
                {
                    dfsStack.Pop();
                    if (SoFar.Count != 0)
                    {
                        SoFar.Remove(SoFar.Back.Degree);
                    }
                    continue;
                }
                Node current = dfsStack.Peek().Item1;
                Term edge    = dfsStack.Peek().Item2;
                if (edge.Coefficient != 0)
                {
                    SoFar.Add(edge);
                }
                dfsStack.Pop();
                dfsStack.Push(new Tuple <Node, Term, bool>(current, edge, true));
                foreach (var child in current._children)
                {
                    dfsStack.Push(new Tuple <Node, Term, bool>(child.Value, new Term(child.Key), false));
                }
                if (current.isEnd)
                {
                    foreach (var special in current._special)
                    {
                        if (Constants.operationsName[special.Key] == "Tree")
                        {
                            SecondTreeExtraction(special.Value as Trie, SoFar, ref result);
                        }
                    }
                }
            }
            return(result);
        }
        public void Test_Polynomial_Add_Zero()
        {
            var f = new Polynomial(new List <int> {
                1, 2, 3
            });
            var g = new Polynomial(new List <int> {
                0
            });

            var expected = new List <int> {
                1, 2, 3
            };
            var actual = f.Add(g).Coefficients;

            Assert.Equal(expected, actual);
        }
示例#23
0
        public void TestAddition()
        {
            string expecting = "24*X - 1";

            Polynomial first  = Polynomial.Parse("12*X + 2");
            Polynomial second = Polynomial.Parse("12*X - 3");

            Polynomial result = Polynomial.Add(first, second);

            TestContext.WriteLine($"({first}) + ({second})");
            TestContext.WriteLine("");
            TestContext.WriteLine($"Result   = {result}");
            TestContext.WriteLine($"Expecting: {expecting}");

            Assert.AreEqual(expecting, result.ToString());
        }
示例#24
0
        public void AddFunction_RandomPolynomial78_ReturnTrue()
        {
            double[]   input1  = new double[] { 0, 0.001, 2.003, 0, 0, -0.0000005, 0, -6.87002, 0, 0, 0.0000001 };
            double     eps     = 0.0001;
            Polynomial result1 = new Polynomial(input1, eps);

            double[]   input2  = new double[] { 0, 0.001, 2.003, 0, 0, -0.0000005, 0, -6.87002, 0, 0, 0.0000001 };
            Polynomial result2 = new Polynomial(input2, eps);

            Polynomial sumResult = Polynomial.Add(result1, result2);

            double[] arrExpected = { 0, 0.002, 4.006, 0, 0, -0.000001, 0, -13.74004, 0, 0, 0.0000002 };

            Polynomial sumExpeted = new Polynomial(arrExpected, eps);

            Assert.AreEqual(sumResult.toString(), sumExpeted.toString());
        }
示例#25
0
        public void AddFunction_RandomPolynomial12_ReturnTrue()
        {
            double[]   input1  = new double[] { 9, -1, 0, -7, 4 };
            double     eps     = 0.01;
            Polynomial result1 = new Polynomial(input1, eps);

            double[]   input2  = new double[] { 0.9, -4, -0.0001, 0.00001, -0.4, 0.002, -0.007 };
            Polynomial result2 = new Polynomial(input2, eps);

            Polynomial sumResult = Polynomial.Add(result1, result2);

            double[] arrExpected = { 9.9, -5, -0.0001, -7, 3.6, 0.002, -0.007 };

            Polynomial sumExpeted = new Polynomial(arrExpected, eps);

            Assert.AreEqual(sumResult.toString(), sumExpeted.toString());
        }
示例#26
0
        public void AddFunction_RandomPolynomial34_ReturnTrue()
        {
            double[]   input1  = new double[] { 19, 0, 0, -0.45, 0, 4, -0.2, 3, 0, -8 };
            double     eps     = 0.01;
            Polynomial result1 = new Polynomial(input1, eps);

            double[]   input2  = new double[] { 1, 0, 0, -0.5, 0, 0, 18 };
            Polynomial result2 = new Polynomial(input2, eps);

            Polynomial sumResult = Polynomial.Add(result1, result2);

            double[] arrExpected = { 20, 0, 0, -0.95, 0, 4, 17.8, 3, 0, -8 };

            Polynomial sumExpeted = new Polynomial(arrExpected, eps);

            Assert.AreEqual(sumResult.toString(), sumExpeted.toString());
        }
        public void PolynomialAddition_randomPolynomial()
        {
            Random     rand = new Random(DateTime.Now.Second);
            Polynomial a = new Polynomial(), b = new Polynomial();
            int        n = rand.Next(2, 1000);

            for (int i = 0; i < n; i++)
            {
                a.Add(new Term(rand.Next(), new System.Numerics.Complex(rand.NextDouble(), rand.NextDouble())));
            }
            n = rand.Next(2, 1000);
            for (int i = 0; i < n; i++)
            {
                b.Add(new Term(rand.Next(), new System.Numerics.Complex(rand.NextDouble(), rand.NextDouble())));
            }


            Polynomial c = a + b;

            String msg = "random Polynomials test failed";

            foreach (var term in c)
            {
                Assert.AreNotEqual(0.0, term.Coefficient, msg);
                Assert.IsTrue(a.Contains(term.Degree) || b.Contains(term.Degree), msg);
                if (a.Contains(term.Degree))
                {
                    if (b.Contains(term.Degree))
                    {
                        Assert.AreEqual(a.CoefficientOf(term.Degree) + b.CoefficientOf(term.Degree), term.Coefficient, msg);
                    }
                    else
                    {
                        Assert.AreEqual(a.CoefficientOf(term.Degree), term.Coefficient, msg);
                    }
                }
                else
                {
                    Assert.IsTrue(b.Contains(term.Degree), msg);
                    Assert.AreEqual(b.CoefficientOf(term.Degree), term.Coefficient, msg);
                }
            }
        }
示例#28
0
        public void DivideLongTest()
        {
            var p11  = new Polynomial(2.0d);
            var p21  = new Polynomial(2.0d);
            var tpl1 = Polynomial.DivideLong(p11, p21);

            testEqual(new double[] { 1.0 }, tpl1.Item1);
            testEqual(new double[] { 0.0 }, tpl1.Item2);

            var p12  = new Polynomial(new double[] { 2.0d, 2.0d });
            var p22  = new Polynomial(2.0d);
            var tpl2 = Polynomial.DivideLong(p12, p22);

            testEqual(new double[] { 1.0, 1.0 }, tpl2.Item1);
            testEqual(new double[] { 0.0 }, tpl2.Item2);

            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    var msg = String.Format("At i={0}, j={1}", i, j);
                    var ci  = new double[i + 2];
                    var cj  = new double[j + 2];
                    ci[ci.Length - 1] = 2;
                    ci[ci.Length - 2] = 1;
                    cj[cj.Length - 1] = 2;
                    cj[cj.Length - 2] = 1;

                    var pi   = new Polynomial(ci);
                    var pj   = new Polynomial(cj);
                    var tgt  = Polynomial.Add(pi, pj);
                    var tpl3 = Polynomial.DivideLong(tgt, pi);
                    var pquo = tpl3.Item1;
                    var prem = tpl3.Item2;
                    var pres = (pquo * pi) + prem;
                    pres.Trim();

                    testEqual(pres, tgt, msg);
                }
            }
        }
示例#29
0
        public void TestAdd()
        {
            var a = new Polynomial();

            a[0] = 5;
            a[4] = 3;
            a[8] = -10;

            var b = new Polynomial();

            b[1]  = 1000;
            b[4]  = 100;
            b[8]  = 10;
            b[13] = -20;

            var s = a.Add(b);

            Assert.AreEqual(5, s[0]);
            Assert.AreEqual(1000, s[1]);
            Assert.AreEqual(103, s[4]);
            Assert.AreEqual(0, s[8]);
            Assert.AreEqual(-20, s[13]);
        }
示例#30
0
        static void Main(string[] args)
        {
            Random x = new Random(1256);

            PolynomialTrie _pl = new PolynomialTrie();


            for (int j = 0; j < 100; j++)
            {
                Polynomial _rand  = new Polynomial();
                Polynomial _rand2 = new Polynomial();
                for (int i = 0; i < 100; i++)
                {
                    _rand.Add(new Term((int)Math.Abs(x.Next((int)1E6)), new Complex(x.NextDouble(), x.NextDouble())));
                    _rand2.Add(new Term((int)Math.Abs(x.Next((int)1E6)), new Complex(x.NextDouble(), x.NextDouble())));
                }
                _pl.insert(_rand, '*', _rand2, _rand * _rand2);
                Console.Write("Mult " + j.ToString());
                _pl.insert(_rand, '/', _rand2, _rand * _rand2);
                Console.Write("Div " + j.ToString());
                Console.WriteLine();
            }
            Console.Write("Done");
        }
 public Polynomial AddMonomial_Test(Polynomial a, int coeff, int degree) => a.Add(coeff, degree);