Example #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;
            }
        }
        public void When_ExampleComplexMatrix1_Expect_MatlabReference()
        {
            // Build the example matrix
            Complex[][] matrix =
            {
                new Complex[] { 0,  0,               0,                     0,  1, 0, 1,                 0 },
                new Complex[] { 0,  0,               0,                     0, -1, 1, 0,                 0 },
                new[]         { 0,  0, new Complex(0.0, 0.000628318530717959),  0, 0, 0,             -1, 1 },
                new Complex[] { 0,  0,               0,                 0.001,  0, 0, 0,                -1 },
                new Complex[] { 1, -1,               0,                     0,  0, 0, 0,                 0 },
                new Complex[] { 0,  1,               0,                     0,  0, 0, 0,                 0 },
                new Complex[] { 1,  0,              -1,                     0,  0, 0, 0,                 0 },
                new[]         { 0,  0,               1,                    -1,  0, 0, 0, new Complex(0.0, -1.5707963267949)}
            };
            Complex[] rhs       = { 0, 0, 0, 0, 0, 24.0 };
            Complex[] reference =
            {
                new Complex(24,                                  0),
                new Complex(24,                                  0),
                new Complex(24,                                  0),
                new Complex(23.999940782519708, -0.037699018824477),
                new Complex(-0.023999940782520, -0.015041945718407),
                new Complex(-0.023999940782520, -0.015041945718407),
                new Complex(0.023999940782520,   0.015041945718407),
                new Complex(0.023999940782520, -0.000037699018824)
            };

            // build the matrix
            var solver = new ComplexSolver();

            for (var r = 0; r < matrix.Length; r++)
            {
                for (var c = 0; c < matrix[r].Length; c++)
                {
                    if (!matrix[r][c].Equals(Complex.Zero))
                    {
                        solver.GetMatrixElement(r + 1, c + 1).Value = matrix[r][c];
                    }
                }
            }

            // Add some zero elements
            solver.GetMatrixElement(7, 7);
            solver.GetRhsElement(5);

            // Build the Rhs vector
            for (var r = 0; r < rhs.Length; r++)
            {
                if (!rhs[r].Equals(Complex.Zero))
                {
                    solver.GetRhsElement(r + 1).Value = rhs[r];
                }
            }

            // Solver
            solver.OrderAndFactor();
            var solution = new DenseVector <Complex>(solver.Order);

            solver.Solve(solution);

            // Check!
            for (var r = 0; r < reference.Length; r++)
            {
                Assert.AreEqual(reference[r].Real, solution[r + 1].Real, 1e-12);
                Assert.AreEqual(reference[r].Imaginary, solution[r + 1].Imaginary, 1e-12);
            }
        }
Example #3
0
        static void Main()
        {
            //library uses one solver type (more will come if there will be demand): dense output stepper based on runge_kutta_dopri5 with standard error bounds 10^(-6) for the steps.
            var solver = new Solver();

            const double @from = 0.0;
            const double to    = 25.0;
            const double step  = 0.1;
            //Say we have a class describing our system:
            var myLorenz = new LorenzOde
            {
                InitialConditions = new StateType(new[] { 10, 1.0, 1.0 })
            };

            // All we need to solve it:
            solver.ConvenienceSolve(myLorenz, from, to, step);


            //library class provides a simple to use Lambda API for ODE system defenition (example of lorenz, 50 steps)
            var lorenz = new LambdaOde
            {
                InitialConditions = new StateType(new[] { 10, 1.0, 1.0 }),
                OdeObserver       = (x, t) => Console.WriteLine("{0} : {1} : {2}", x[0], x[1], x[2]),
                OdeSystem         =
                    (x, dxdt, t) => {
                    const double sigma = 10.0;
                    const double r     = 28.0;
                    const double b     = 8.0 / 3.0;
                    dxdt[0] = sigma * (x[1] - x[0]);
                    dxdt[1] = r * x[0] - x[1] - x[0] * x[2];
                    dxdt[2] = -b * x[2] + x[0] * x[1];
                }
            };

            // And all we need to solve it:
            solver.ConvenienceSolve(lorenz, from, to, step);

            // We can select stepper that our stepper would use
            solver.StepperCode = StepperTypeCode.RungeKutta4;

            // We can select how our IntegrateFunction will work:
            solver.Solve(lorenz, from, step, to, IntegrateFunctionTypeCode.Adaptive);

            // We can integrate for first N steps
            solver.Solve(lorenz, from, step, 5);

            // Or at given time periods
            solver.Solve(lorenz, new StateType(new[] { 0, 10.0, 100.0, 1000.0 }), step);

            Console.ReadLine();
            Console.WriteLine("Complex Test");

            var complexSolver = new ComplexSolver();

            const double eta   = 2;
            const double alpha = 1;

            var complexEta   = new Complex(1, eta);
            var complexAlpha = new Complex(1, alpha);

            var stuartLandauOscillator = new ComplexLambdaOde
            {
                InitialConditions = new ComplexStateType {
                    new Complex(1, 0)
                },
                OdeObserver = (x, t) => Console.WriteLine("{0}", x[0]),
                OdeSystem   =
                    (x, dxdt, t) =>
                {
                    dxdt[0] = complexEta * x[0] - (complexAlpha * x[0].Norm()) * x[0];
                }
            };

            // And all we need to solve it:
            complexSolver.ConvenienceSolve(stuartLandauOscillator, from, to, step);

            // We can select stepper that our stepper would use
            complexSolver.StepperCode = StepperTypeCode.RungeKutta4;

            // We can select how our IntegrateFunction will work:
            complexSolver.Solve(stuartLandauOscillator, from, step, to, IntegrateFunctionTypeCode.Adaptive);

            // We can integrate for first N steps
            complexSolver.Solve(stuartLandauOscillator, from, step, 5);

            // Or at given time periods
            complexSolver.Solve(stuartLandauOscillator, new StateType(new[] { 0, 10.0, 100.0, 1000.0 }), step);

            Console.ReadLine();
        }
Example #4
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));
 }