コード例 #1
0
ファイル: Matrix.Solve.cs プロジェクト: simple555a/Solarfist
        /// <summary>
        /// Solves the matrix equation Ax = b, where A is the coefficient matrix (this matrix), b is the solution vector and x is the unknown vector.
        /// </summary>
        /// <param name="input">The solution vector <c>b</c>.</param>
        /// <param name="result">The result vector <c>x</c>.</param>
        /// <param name="solver">The iterative solver to use.</param>
        /// <param name="stopCriteria">Criteria to control when to stop iterating.</param>
        /// <param name="preconditioner">The preconditioner to use for approximations.</param>
        public IterationStatus TrySolveIterative(Vector <T> input, Vector <T> result, IIterativeSolver <T> solver, IPreconditioner <T> preconditioner, params IIterationStopCriterion <T>[] stopCriteria)
        {
            var iterator = new Iterator <T>(stopCriteria.Length == 0 ? Build.IterativeSolverStopCriteria() : stopCriteria);

            return(TrySolveIterative(input, result, solver, iterator, preconditioner));
        }
コード例 #2
0
 private void InitializeSolver()
 {
     result = Vector<double>.Build.Dense(n * m);
       Control.LinearAlgebraProvider = new OpenBlasLinearAlgebraProvider();
     //Control.UseNativeMKL();
     //Control.LinearAlgebraProvider = new MklLinearAlgebraProvider();
     //Control.UseManaged();
     var iterationCountStopCriterion = new IterationCountStopCriterion<double>(1000);
     var residualStopCriterion = new ResidualStopCriterion<double>(1e-7);
     monitor = new Iterator<double>(iterationCountStopCriterion, residualStopCriterion);
     solver = new BiCgStab();
     preconditioner = new MILU0Preconditioner();
 }
コード例 #3
0
ファイル: Matrix.Solve.cs プロジェクト: simple555a/Solarfist
        // Iterative Solvers: Full

        /// <summary>
        /// Solves the matrix equation Ax = b, where A is the coefficient matrix (this matrix), b is the solution vector and x is the unknown vector.
        /// </summary>
        /// <param name="input">The solution vector <c>b</c>.</param>
        /// <param name="result">The result vector <c>x</c>.</param>
        /// <param name="solver">The iterative solver to use.</param>
        /// <param name="iterator">The iterator to use to control when to stop iterating.</param>
        /// <param name="preconditioner">The preconditioner to use for approximations.</param>
        public IterationStatus TrySolveIterative(Vector <T> input, Vector <T> result, IIterativeSolver <T> solver, Iterator <T> iterator = null, IPreconditioner <T> preconditioner = null)
        {
            if (iterator == null)
            {
                iterator = new Iterator <T>(Build.IterativeSolverStopCriteria());
            }

            if (preconditioner == null)
            {
                preconditioner = new UnitPreconditioner <T>();
            }

            solver.Solve(this, input, result, iterator, preconditioner);

            return(iterator.Status);
        }
コード例 #4
0
ファイル: Matrix.Solve.cs プロジェクト: simple555a/Solarfist
        /// <summary>
        /// Solves the matrix equation AX = B, where A is the coefficient matrix (this matrix), B is the solution matrix and X is the unknown matrix.
        /// </summary>
        /// <param name="input">The solution matrix <c>B</c>.</param>
        /// <param name="result">The result matrix <c>X</c></param>
        /// <param name="solver">The iterative solver to use.</param>
        /// <param name="iterator">The iterator to use to control when to stop iterating.</param>
        /// <param name="preconditioner">The preconditioner to use for approximations.</param>
        public IterationStatus TrySolveIterative(Matrix <T> input, Matrix <T> result, IIterativeSolver <T> solver, Iterator <T> iterator = null, IPreconditioner <T> preconditioner = null)
        {
            if (RowCount != input.RowCount || input.RowCount != result.RowCount || input.ColumnCount != result.ColumnCount)
            {
                throw DimensionsDontMatch <ArgumentException>(this, input, result);
            }

            if (iterator == null)
            {
                iterator = new Iterator <T>(Build.IterativeSolverStopCriteria());
            }

            if (preconditioner == null)
            {
                preconditioner = new UnitPreconditioner <T>();
            }

            for (var column = 0; column < input.ColumnCount; column++)
            {
                var solution = Vector <T> .Build.Dense(RowCount);

                solver.Solve(this, input.Column(column), solution, iterator, preconditioner);

                foreach (var element in solution.EnumerateIndexed(Zeros.AllowSkip))
                {
                    result.At(element.Item1, column, element.Item2);
                }
            }

            return(iterator.Status);
        }
        /// <summary>
        /// Solves the matrix equation Ax = b, where A is the coefficient matrix, b is the
        /// solution vector and x is the unknown vector.
        /// </summary>
        /// <param name="matrix">The coefficient matrix, <c>A</c>.</param>
        /// <param name="input">The solution vector, <c>b</c></param>
        /// <param name="result">The result vector, <c>x</c></param>
        public void Solve(Matrix <float> matrix, Vector <float> input, Vector <float> result)
        {
            // If we were stopped before, we are no longer
            // We're doing this at the start of the method to ensure
            // that we can use these fields immediately.
            _hasBeenStopped = false;
            _currentSolver  = null;

            // Error checks
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            if (matrix.RowCount != matrix.ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
            }

            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (result == null)
            {
                throw new ArgumentNullException("result");
            }

            if (result.Count != input.Count)
            {
                throw new ArgumentException(Resources.ArgumentVectorsSameLength);
            }

            // Initialize the solver fields
            // Set the convergence monitor
            if (_iterator == null)
            {
                _iterator = Iterator.CreateDefault();
            }

            // Load the solvers into our own internal data structure
            // Once we have solvers we can always reuse them.
            if (_solvers.Count == 0)
            {
                LoadSolvers();
            }

            // Create a copy of the solution and result vectors so we can use them
            // later on
            var internalInput  = input.Clone();
            var internalResult = result.Clone();

            foreach (var solver in _solvers.TakeWhile(solver => !_hasBeenStopped))
            {
                // Store a reference to the solver so we can stop it.
                _currentSolver = solver;

                try
                {
                    // Reset the iterator and pass it to the solver
                    _iterator.ResetToPrecalculationState();
                    solver.SetIterator(_iterator);

                    // Start the solver
                    solver.Solve(matrix, internalInput, internalResult);
                }
                catch (Exception)
                {
                    // The solver broke down.
                    // Log a message about this
                    // Switch to the next preconditioner.
                    // Reset the solution vector to the previous solution
                    input.CopyTo(internalInput);
                    _status = RunningStatus;
                    continue;
                }

                // There was no fatal breakdown so check the status
                if (_iterator.Status is CalculationConverged)
                {
                    // We're done
                    break;
                }

                // We're not done
                // Either:
                // - calculation finished without convergence
                if (_iterator.Status is CalculationStoppedWithoutConvergence)
                {
                    // Copy the internal result to the result vector and
                    // continue with the calculation.
                    internalInput.CopyTo(input);
                }
                else
                {
                    // - calculation failed --> restart with the original vector
                    // - calculation diverged --> restart with the original vector
                    // - Some unknown status occurred --> To be safe restart.
                    input.CopyTo(internalInput);
                }
            }

            // Inside the loop we already copied the final results (if there are any)
            // So no need to do that again.

            // Clean up
            // No longer need the current solver
            _currentSolver = null;

            // Set the final status
            _status = _iterator.Status;
        }
コード例 #6
0
        /// <summary>
        /// Solves the matrix equation AX = B, where A is the coefficient matrix (this matrix), B is the solution matrix and X is the unknown matrix.
        /// </summary>
        /// <param name="input">The solution matrix <c>B</c>.</param>
        /// <param name="result">The result matrix <c>X</c></param>
        /// <param name="solver">The iterative solver to use.</param>
        /// <param name="stopCriteria">Criteria to control when to stop iterating.</param>
        public IterationStatus TrySolveIterative(Matrix <T> input, Matrix <T> result, IIterativeSolver <T> solver, params IIterationStopCriterium <T>[] stopCriteria)
        {
            var iterator = new Iterator <T>(stopCriteria.Length == 0 ? Build.IterativeSolverStopCriteria() : stopCriteria);

            return(TrySolveIterative(input, result, solver, iterator));
        }
コード例 #7
0
        public double CalculateGradient(IIterativeSolver solver)
        {
            SolverPCG pcg = (SolverPCG)solver;

            return((pcg.VectorZ * pcg.VectorR) / (pcg.VectorP * pcg.VectorQ));
        }
        public double CalculateGradient(IIterativeSolver solver)
        {
            SolverPCG pcg = (SolverPCG)solver;

            return(SearchVectors.CalculateReorthogonalizedGradient(pcg.VectorP, pcg.VectorQ, pcg.VectorR, p, q));
        }
        public void CalculateSearchVector(IIterativeSolver solver)
        {
            SolverPCG pcg = (SolverPCG)solver;

            SearchVectors.CalculateReorthogonalizedSearchVector(pcg.VectorZ, pcg.VectorP, p, q);
        }