Exemple #1
0
        public virtual double Laguerre(double lo, double hi, double fLo, double fHi)
        {
            Complex[] c = ComplexUtils.convertToComplex(Coefficients);

            Complex initial = new Complex(0.5 * (lo + hi), 0);
            Complex z = ComplexSolver.Solve(c, initial);
            if (ComplexSolver.IsRoot(lo, hi, z))
            {
                return z.Real;
            }
            else
            {
                double r = double.NaN;
                // Solve all roots and select the one we are seeking.
                Complex[] root = ComplexSolver.SolveAll(c, initial);
                for (int i = 0; i < root.Length; i++)
                {
                    if (ComplexSolver.IsRoot(lo, hi, root[i]))
                    {
                        r = root[i].Real;
                        break;
                    }
                }
                return r;
            }
        }
Exemple #2
0
        public virtual double Laguerre(double lo, double hi, double fLo, double fHi)
        {
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.apache.commons.math3.complex.Complex c[] = org.apache.commons.math3.complex.ComplexUtils.convertToComplex(getCoefficients());
            Complex[] c = ComplexUtils.ConvertToComplex(GetCoefficients());

//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.apache.commons.math3.complex.Complex initial = new org.apache.commons.math3.complex.Complex(0.5 * (lo + hi), 0);
            Complex initial = new Complex(0.5 * (lo + hi), 0);
//JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final':
//ORIGINAL LINE: final org.apache.commons.math3.complex.Complex z = complexSolver.solve(c, initial);
            Complex z = complexSolver.Solve(c, initial);

            if (complexSolver.IsRoot(lo, hi, z))
            {
                return(z.GetReal());
            }
            else
            {
                double r = Double.NaN;
                // Solve all roots and select the one we are seeking.
                Complex[] root = complexSolver.SolveAll(c, initial);
                for (int i = 0; i < root.Length; i++)
                {
                    if (complexSolver.IsRoot(lo, hi, root[i]))
                    {
                        r = root[i].GetReal();
                        break;
                    }
                }
                return(r);
            }
        }
        void paraDT(PolynomialTrie trie, out DataTable substitutaions, out DataTable definiteIntegral)
        {
            substitutaions   = new DataTable("subtitutions in polynomial");
            definiteIntegral = new DataTable("Definite integral on polynomial");
            foreach (var title in Constants.SubstitutionColumnsName)
            {
                substitutaions.Columns.Add(title, typeof(string));
            }
            foreach (var title in Constants.DefiniteIntColumnsName)
            {
                definiteIntegral.Columns.Add(title, 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)
                {
                    substitutaions.Rows.Add(substitutaions.NewRow());
                    definiteIntegral.Rows.Add(definiteIntegral.NewRow());
                    int indexSub    = substitutaions.Rows.Count - 1;
                    int indexDefint = definiteIntegral.Rows.Count - 1;
                    substitutaions.Rows[indexSub]["Polynomial"]      = SoFar.ToString();
                    definiteIntegral.Rows[indexDefint]["Polynomial"] = SoFar.ToString();
                    bool foundSubstitution = false;
                    bool foundDefint       = false;
                    foreach (var special in current._special)
                    {
                        if (special.Key == 's')
                        {
                            foundSubstitution = true;
                            foreach (var substitutaion in special.Value as paraStore)
                            {
                                substitutaions.Rows[indexSub]["X"]      = ComplexUtils.ComplexToString(substitutaion.Key[0]);
                                substitutaions.Rows[indexSub]["Result"] = ComplexUtils.ComplexToString(substitutaion.Value);
                            }
                        }
                        else if (special.Key == 'd')
                        {
                            foundDefint = true;
                            foreach (var defint in special.Value as paraStore)
                            {
                                definiteIntegral.Rows[indexSub]["A"]      = ComplexUtils.ComplexToString(defint.Key[0]);
                                definiteIntegral.Rows[indexSub]["B"]      = ComplexUtils.ComplexToString(defint.Key[1]);
                                definiteIntegral.Rows[indexSub]["Result"] = ComplexUtils.ComplexToString(defint.Value);
                            }
                        }
                    }
                    if (!foundSubstitution)
                    {
                        substitutaions.Rows.RemoveAt(substitutaions.Rows.Count - 1);
                    }
                    if (!foundDefint)
                    {
                        definiteIntegral.Rows.RemoveAt(definiteIntegral.Rows.Count - 1);
                    }
                }
            }
        }
        DataTable OneOperationDT(PolynomialTrie trie)
        {
            DataTable result = new DataTable("Operations on one polynomial");

            foreach (var title in Constants.OnePolynomialColumnsName)
            {
                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)
                {
                    result.Rows.Add(result.NewRow());
                    int index = result.Rows.Count - 1;
                    result.Rows[index]["Polynomial"] = SoFar.ToString();
                    bool foundOperation = false;
                    foreach (var special in current._special)
                    {
                        if (Constants.operationsName[special.Key] == "Result")
                        {
                            foundOperation = true;
                            result.Rows[index][Constants.OnePolynomialColumnsName[special.Key]] = (special.Value as Polynomial).ToString();
                        }
                        else if (Constants.operationsName[special.Key] == "listComplex")
                        {
                            foundOperation = true;
                            result.Rows[index][Constants.OnePolynomialColumnsName[special.Key]] = ComplexUtils.ComplexListToString(special.Value as List <Complex>);
                        }
                    }
                    if (!foundOperation)
                    {
                        result.Rows.RemoveAt(result.Rows.Count - 1);
                    }
                }
            }
            return(result);
        }
        XElement dfs(Node root)
        {
            XElement Xroot = new XElement("Node");
            Stack <List <Object> > dfsStack = new Stack <List <Object> >();

            dfsStack.Push(new List <Object>());
            dfsStack.Peek().Add(Xroot);
            dfsStack.Peek().Add(null);
            dfsStack.Peek().Add(root);
            while (dfsStack.Count != 0)
            {
                var      temporaryList = dfsStack.Pop();
                XElement Xcurrent      = temporaryList[0] as XElement;
                XElement Xparent       = temporaryList[1] as XElement;
                Node     currentNode   = temporaryList[2] as Node;
                Xcurrent.Add(new XElement("isEnd", currentNode.isEnd.ToString()));
                if (Xparent != null)
                {
                    Xparent.Add(Xcurrent);
                }
                foreach (var edge in currentNode._children)
                {
                    var newChild = new XElement("Node");
                    newChild.Add(new XElement("Edge",
                                              new XElement("Degree", edge.Key.Key.ToString()),
                                              new XElement("Coefficient",
                                                           new XElement("Real", edge.Key.Value.Real),
                                                           new XElement("Imaginary", edge.Key.Value.Imaginary))));
                    dfsStack.Push(new List <Object>());
                    dfsStack.Peek().Add(newChild);
                    dfsStack.Peek().Add(Xcurrent);
                    dfsStack.Peek().Add(edge.Value);
                }
                if (currentNode.isEnd)
                {
                    foreach (var special in currentNode._special)
                    {
                        string   Name     = Constants.operationsName[special.Key];
                        XElement newChild = new XElement(Name);
                        if (Name == "listComplex")
                        {
                            foreach (var solution in (special.Value as List <Complex>))
                            {
                                newChild.Add(new XElement("root", ComplexUtils.ComplexToString(solution)));
                            }
                        }
                        else if (Name == "Result")
                        {
                            newChild.Add(new XElement("poly", special.Value.ToString()));
                        }
                        else if (Name == "params")
                        {
                            foreach (var operation in (special.Value as paraStore))
                            {
                                XElement op = new XElement("operation");
                                foreach (var para in operation.Key)
                                {
                                    op.Add(new XElement("para", para.ToString()));
                                }
                                op.Add(new XElement("Result", operation.Value.ToString()));
                                newChild.Add(op);
                            }
                        }
                        else if (Name == "Tree")
                        {
                            newChild = dfs((special.Value as Trie).root);
                        }
                        newChild.Name = Name;
                        newChild.Add(new XElement("Edge", special.Key.ToString()));
                        Xcurrent.Add(newChild);
                    }
                }
            }
            return(Xroot);
        }
        Node dfs(XElement Xroot)
        {
            Node root = new Node();
            Stack <List <Object> > dfsStack = new Stack <List <Object> >();

            dfsStack.Push(new List <Object>());
            dfsStack.Peek().Add(root);
            dfsStack.Peek().Add(null);
            dfsStack.Peek().Add(null);
            dfsStack.Peek().Add(Xroot);
            while (dfsStack.Count != 0)
            {
                var      temporaryList = dfsStack.Pop();
                Node     currentNode   = temporaryList[0] as Node;
                Node     parentNode    = temporaryList[1] as Node;
                XElement currentXNode  = temporaryList[3] as XElement;
                currentNode.isEnd = currentXNode.Element("isEnd").Value == "True";
                if (parentNode != null)
                {
                    KeyValuePair <int, Complex> Edge = (KeyValuePair <int, Complex>)temporaryList[2];
                    parentNode._children.Add(Edge, currentNode);
                }
                foreach (var edge in currentXNode.Elements("Node"))
                {
                    int    degree    = int.Parse(edge.Element("Edge").Element("Degree").Value);
                    double real      = double.Parse(edge.Element("Edge").Element("Coefficient").Element("Real").Value);
                    double imaginary = double.Parse(edge.Element("Edge").Element("Coefficient").Element("Imaginary").Value);
                    Node   newNode   = new Node();
                    dfsStack.Push(new List <Object>());
                    dfsStack.Peek().Add(newNode);
                    dfsStack.Peek().Add(currentNode);
                    dfsStack.Peek().Add(new KeyValuePair <int, Complex>(degree, new Complex(real, imaginary)));
                    dfsStack.Peek().Add(edge);
                }
                if (currentNode.isEnd)
                {
                    foreach (var edge in currentXNode.Elements("Tree"))
                    {
                        char edgeChar = edge.Element("Edge").Value[0];
                        currentNode._special.Add(edgeChar, new Trie(dfs(edge)));
                    }
                    foreach (var edge in currentXNode.Elements("listComplex"))
                    {
                        char edgeChar = edge.Element("Edge").Value[0];
                        currentNode._special.Add(edgeChar, ComplexUtils.complexListParse(edge.Elements("root")));
                    }
                    foreach (var edge in currentXNode.Elements("params"))
                    {
                        char edgeChar   = edge.Element("Edge").Value[0];
                        var  operations = new paraStore(new ListComparer <Complex>());
                        foreach (var op in edge.Elements("operation"))
                        {
                            operations.Add(ComplexUtils.complexListParse(op.Elements("para")), ComplexUtils.ParseComplex(op.Element("Result").Value));
                        }
                        currentNode._special.Add(edgeChar, operations);
                    }
                    foreach (var edge in currentXNode.Elements("Result"))
                    {
                        char edgeChar   = edge.Element("Edge").Value[0];
                        var  resultPoly = new Polynomial(edge.Element("poly").Value);
                        currentNode._special.Add(edgeChar, resultPoly);
                    }
                }
            }
            return(root);
        }
Exemple #7
0
 /// <summary>
 /// Find a complex root for the polynomial with the given coefficients,
 /// starting from the given initial value.
 /// <br/>
 /// Note: This method is not part of the API of <seealso cref="BaseUnivariateSolver"/>.
 /// </summary>
 /// <param name="coefficients"> Polynomial coefficients. </param>
 /// <param name="initial"> Start value. </param>
 /// <returns> the point at which the function value is zero. </returns>
 /// <exception cref="org.apache.commons.math3.exception.TooManyEvaluationsException">
 /// if the maximum number of evaluations is exceeded. </exception>
 /// <exception cref="NullArgumentException"> if the {@code coefficients} is
 /// {@code null}. </exception>
 /// <exception cref="NoDataException"> if the {@code coefficients} array is empty.
 /// @since 3.1 </exception>
 public virtual Complex SolveComplex(double[] coefficients, double initial)
 {
     Setup(int.MaxValue, new PolynomialFunction(coefficients), double.NegativeInfinity, double.PositiveInfinity, initial);
     return ComplexSolver.Solve(ComplexUtils.convertToComplex(coefficients), new Complex(initial, 0d));
 }
Exemple #8
0
 /// <summary>
 /// Find all complex roots for the polynomial with the given
 /// coefficients, starting from the given initial value.
 /// <para>
 /// Note: This method is not part of the API of <seealso cref="BaseUnivariateSolver"/>.</para>
 /// </summary>
 /// <param name="coefficients"> polynomial coefficients </param>
 /// <param name="initial"> start value </param>
 /// <param name="maxEval"> maximum number of evaluations </param>
 /// <returns> the full set of complex roots of the polynomial </returns>
 /// <exception cref="org.apache.commons.math3.exception.TooManyEvaluationsException">
 /// if the maximum number of evaluations is exceeded when solving for one of the roots </exception>
 /// <exception cref="NullArgumentException"> if the {@code coefficients} is
 /// {@code null} </exception>
 /// <exception cref="NoDataException"> if the {@code coefficients} array is empty
 /// @since 3.5 </exception>
 public virtual Complex[] SolveAllComplex(double[] coefficients, double initial, int maxEval)
 {
     Setup(maxEval, new PolynomialFunction(coefficients), double.NegativeInfinity, double.PositiveInfinity, initial);
     return(complexSolver.SolveAll(ComplexUtils.ConvertToComplex(coefficients), new Complex(initial, 0d)));
 }
Exemple #9
0
 public override string ToString()
 {
     return(ComplexUtils.ComplexToString(Coefficient) + "X" + Degree.ToString());
 }
Exemple #10
0
 public Term(string _input)
 {
     string[] seperate = _input.Split('X');
     Coefficient = ComplexUtils.ParseComplex(seperate[0]);
     Degree      = int.Parse(seperate[1]);
 }