Example #1
0
        public static double CalculateSsu()
        {
            // Stopwatch start
            Timer.Restart();
            var evector = new DenseVector(Length);
            for (var i = 0; i < Length; i++ )
            {
                QMatrix[i, 0] = 1;
                evector[i] = 0;
            }
            evector[0] = 1.0;
            var qtilde = new SparseMatrix(QMatrix.Transpose().ToArray());

            var v = new BiCgStab().Solve(qtilde, evector);
            var result = v.Sum() - UpStates.Sum(i => v[i]);

            // Stopwatch stop and store statistic
            _timeToSSU = (ulong)Timer.ElapsedTicks;
            Timer.Stop();

            return result;
        }
        /// <summary>
        /// Run example
        /// </summary>
        /// <seealso cref="http://en.wikipedia.org/wiki/Biconjugate_gradient_stabilized_method">Biconjugate gradient stabilized method</seealso>
        public void Run()
        {
            // Format matrix output to console
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();
            formatProvider.TextInfo.ListSeparator = " ";

            // Solve next system of linear equations (Ax=b):
            // 5*x + 2*y - 4*z = -7
            // 3*x - 7*y + 6*z = 38
            // 4*x + 1*y + 5*z = 43

            // Create matrix "A" with coefficients
            var matrixA = new DenseMatrix(new[,] { { 5.00, 2.00, -4.00 }, { 3.00, -7.00, 6.00 }, { 4.00, 1.00, 5.00 } });
            Console.WriteLine(@"Matrix 'A' with coefficients");
            Console.WriteLine(matrixA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Create vector "b" with the constant terms.
            var vectorB = new DenseVector(new[] { -7.0, 38.0, 43.0 });
            Console.WriteLine(@"Vector 'b' with the constant terms");
            Console.WriteLine(vectorB.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // Create stop criteriums to monitor an iterative calculation. There are next available stop criteriums:
            // - DivergenceStopCriterium: monitors an iterative calculation for signs of divergence;
            // - FailureStopCriterium: monitors residuals for NaN's;
            // - IterationCountStopCriterium: monitors the numbers of iteration steps;
            // - ResidualStopCriterium: monitors residuals if calculation is considered converged;

            // Stop calculation if 1000 iterations reached during calculation
            var iterationCountStopCriterium = new IterationCountStopCriterium(1000);

            // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged
            var residualStopCriterium = new ResidualStopCriterium(1e-10);

            // Create monitor with defined stop criteriums
            var monitor = new Iterator(new IIterationStopCriterium[] { iterationCountStopCriterium, residualStopCriterium });

            // Create Bi-Conjugate Gradient Stabilized solver
            var solver = new BiCgStab(monitor);

            // 1. Solve the matrix equation
            var resultX = solver.Solve(matrixA, vectorB);
            Console.WriteLine(@"1. Solve the matrix equation");
            Console.WriteLine();

            // 2. Check solver status of the iterations.
            // Solver has property IterationResult which contains the status of the iteration once the calculation is finished.
            // Possible values are:
            // - CalculationCancelled: calculation was cancelled by the user;
            // - CalculationConverged: calculation has converged to the desired convergence levels;
            // - CalculationDiverged: calculation diverged;
            // - CalculationFailure: calculation has failed for some reason;
            // - CalculationIndetermined: calculation is indetermined, not started or stopped;
            // - CalculationRunning: calculation is running and no results are yet known;
            // - CalculationStoppedWithoutConvergence: calculation has been stopped due to reaching the stopping limits, but that convergence was not achieved;
            Console.WriteLine(@"2. Solver status of the iterations");
            Console.WriteLine(solver.IterationResult);
            Console.WriteLine();

            // 3. Solution result vector of the matrix equation
            Console.WriteLine(@"3. Solution result vector of the matrix equation");
            Console.WriteLine(resultX.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 4. Verify result. Multiply coefficient matrix "A" by result vector "x"
            var reconstructVecorB = matrixA * resultX;
            Console.WriteLine(@"4. Multiply coefficient matrix 'A' by result vector 'x'");
            Console.WriteLine(reconstructVecorB.ToString("#0.00\t", formatProvider));
            Console.WriteLine();
        }
Example #3
0
        public static double CalculateMttf()
        {
            Timer.Restart();

            // Store necessary variables of formula
            var partialLength = (ushort)UpStates.Count();
            var pmatrix = new DenseMatrix(partialLength, partialLength);
            var hvector = new DenseVector(partialLength);

            // Convert QMatrix to PMatrix
            for( var i = 0; i < partialLength; i++ )
            {
                var diagonal = QMatrix[UpStates[i], UpStates[i]];
                hvector[i] = diagonal;
                for( var j = 0; j < partialLength; j++ )
                {
                    pmatrix[i, j] = -QMatrix[UpStates[i], UpStates[j]]/diagonal;
                }
            }

            // Invert PMatrix and multiply by H-vector to get MTTF
            var result = new BiCgStab().Solve(pmatrix, hvector);

            _timeToMTTF = (ulong)Timer.ElapsedTicks;

            return result[0];
        }