Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DenseEvd"/> class. This object will compute the
        /// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
        /// </summary>
        /// <param name="matrix">The matrix to factor.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
        public DenseEvd(DenseMatrix matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

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

            var order = matrix.RowCount;

            // Initialize matrices for eigenvalues and eigenvectors
            MatrixEv = DenseMatrix.Identity(order);
            MatrixD = matrix.CreateMatrix(order, order);
            VectorEv = new DenseVector(order);

            IsSymmetric = true;

            for (var i = 0; IsSymmetric && i < order; i++)
            {
                for (var j = 0; IsSymmetric && j < order; j++)
                {
                    IsSymmetric &= matrix.At(i, j) == matrix.At(j, i).Conjugate();
                }
            }

            Control.LinearAlgebraProvider.EigenDecomp(IsSymmetric, order, matrix.Values, ((DenseMatrix) MatrixEv).Values,
                ((DenseVector) VectorEv).Values, ((DenseMatrix) MatrixD).Values);
        }
Ejemplo n.º 2
0
        // Computes coefficients of real polynomial (or estimates if values are noised) using Svd
        // Each row of matrix should contain [x, P(x)], at least rank+1 rows
        // Supplied x-es best have magintude in range [1-2], so resulting coefficient matrix is well conditioned
        public static Polynomial EstimatePolynomial(Matrix<float> values, int rank)
        {
            // 1) Create equation Xa = b
            // | x1^n x1^n-1 ... x1 1 | | a0 |   | P(x1) |
            // |                      | | ...| = |       |
            // | xk^n xk^n-1 ... xk 1 | | an |   | P(xk) |
            Matrix<float> X = new DenseMatrix(values.RowCount, rank + 1);
            Vector<float> P = new DenseVector(values.RowCount);

            for(int i = 0; i < values.RowCount; ++i)
            {
                X[i, rank] = 1.0f;
                for(int c = rank - 1; c >= 0; --c)
                {
                    X[i, c] = X[i, c + 1] * values.At(i, 0);
                }
                P[i] = values[i, 1];
            }

            return new Polynomial()
            {
                Coefficents = SvdSolver.Solve(X, P),
                Rank = rank
            };
        }
        public void HeapSortWithDecreasingDoubleArray()
        {
            var sortedIndices = new int[10];
            Vector<Complex> values = new DenseVector(10);
            values[0] = 9;
            values[1] = 8;
            values[2] = 7;
            values[3] = 6;
            values[4] = 5;
            values[5] = 4;
            values[6] = 3;
            values[7] = 2;
            values[8] = 1;
            values[9] = 0;
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                sortedIndices[i] = i;
            }

            IlutpElementSorter.SortDoubleIndicesDecreasing(0, sortedIndices.Length - 1, sortedIndices, values);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                Assert.AreEqual(i, sortedIndices[i], "#01-" + i);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DenseEvd"/> class. This object will compute the
        /// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
        /// </summary>
        /// <param name="matrix">The matrix to factor.</param>
        /// <param name="symmetricity">If it is known whether the matrix is symmetric or not the routine can skip checking it itself.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
        public static DenseEvd Create(DenseMatrix matrix, Symmetricity symmetricity)
        {
            if (matrix.RowCount != matrix.ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSquare);
            }

            var order = matrix.RowCount;

            // Initialize matrices for eigenvalues and eigenvectors
            var eigenVectors = DenseMatrix.CreateIdentity(order);
            var blockDiagonal = new DenseMatrix(order);
            var eigenValues = new DenseVector(order);

            bool isSymmetric;
            switch (symmetricity)
            {
                case Symmetricity.Hermitian:
                    isSymmetric = true;
                    break;
                case Symmetricity.Asymmetric:
                    isSymmetric = false;
                    break;
                default:
                    isSymmetric = matrix.IsHermitian();
                    break;
            }

            Control.LinearAlgebraProvider.EigenDecomp(isSymmetric, order, matrix.Values, eigenVectors.Values, eigenValues.Values, blockDiagonal.Values);

            return new DenseEvd(eigenVectors, eigenValues, blockDiagonal, isSymmetric);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// The main method that uses the Ilutp preconditioner.
        /// </summary>
        public void UseSolver()
        {
            // Create a sparse matrix. For now the size will be 10 x 10 elements
            Matrix matrix = CreateMatrix(10);

            // Create the right hand side vector. The size is the same as the matrix
            // and all values will be 2.0.
            Vector rightHandSideVector = new DenseVector(10, 2.0);

            // Create the Ilutp preconditioner
            Ilutp preconditioner = new Ilutp();

            // Set the drop tolerance. All entries with absolute values smaller than this value will be
            // removed from the preconditioner matrices.
            preconditioner.DropTolerance = 1e-5;
            // Set the relative fill level. This indicates how much additional fill we allow. In this case
            // about 200%
            preconditioner.FillLevel = 200;
            // Set the pivot tolerance. This indicates when pivoting is used. In this case we pivot if
            // the largest off-diagonal entry is twice as big as the diagonal entry.
            preconditioner.PivotTolerance = 0.5;

            // Create the actual preconditioner
            preconditioner.Initialize(matrix);

            // Now that all is set we can solve the matrix equation.
            Vector solutionVector = preconditioner.Approximate(rightHandSideVector);

            // Another way to get the values is by using the overloaded solve method
            // In this case the solution vector needs to be of the correct size.
            preconditioner.Approximate(rightHandSideVector, solutionVector);
        }
Ejemplo n.º 6
0
        public bool ReadFile(string fileName)
        {
            TextReader tr = null;
            try {
                tr = new StreamReader(fileName);
            } catch (Exception e) {
                return false;
            }

            string buffer = tr.ReadLine();
            string[] numbers = buffer.Split(' ');

            N = int.Parse(numbers[0]);
            P = int.Parse(numbers[1]);

            A = new DenseMatrix(P, N);
            for (int i = 0; i < P; i++) {
                buffer = tr.ReadLine();
                numbers = buffer.Split(' ');
                for (int j = 0; j < N; j++) {
                    A[i,j] = int.Parse(numbers[j]);
                }
            }
            B = new DenseVector(P);
            for (int i = 0; i < P; i++) {
                buffer = tr.ReadLine();
                B[i] = int.Parse(buffer);
            }

            return true;
        }
Ejemplo n.º 7
0
        public void SolveWideMatrixThrowsArgumentException()
        {
            var matrix = new SparseMatrix(2, 3);
            Vector input = new DenseVector(2);

            var solver = new GpBiCg();
            Assert.Throws<ArgumentException>(() => solver.Solve(matrix, input));
        }
        public void SolveLongMatrixThrowsArgumentException()
        {
            var matrix = new SparseMatrix(3, 2);
            Vector input = new DenseVector(3);

            var solver = new BiCgStab();
            Assert.Throws<ArgumentException>(() => solver.Solve(matrix, input));
        }
        public void HeapSortWithDuplicateDoubleEntries()
        {
            var sortedIndices = new int[10];
            Vector<Complex> values = new DenseVector(10);
            values[0] = 1;
            values[1] = 1;
            values[2] = 1;
            values[3] = 1;
            values[4] = 2;
            values[5] = 2;
            values[6] = 2;
            values[7] = 2;
            values[8] = 3;
            values[9] = 4;

            for (var i = 0; i < sortedIndices.Length; i++)
            {
                sortedIndices[i] = i;
            }
            IlutpElementSorter.SortDoubleIndicesDecreasing(0, sortedIndices.Length - 1, sortedIndices, values);
            for (var i = 0; i < sortedIndices.Length; i++)
            {
                if (i == 0)
                {
                    Assert.AreEqual(9, sortedIndices[i], "#01-" + i);
                }
                else
                {
                    if (i == 1)
                    {
                        Assert.AreEqual(8, sortedIndices[i], "#01-" + i);
                    }
                    else
                    {
                        if (i < 6)
                        {
                            if ((sortedIndices[i] != 4) &&
                                (sortedIndices[i] != 5) &&
                                (sortedIndices[i] != 6) &&
                                (sortedIndices[i] != 7))
                            {
                                Assert.Fail("#01-" + i);
                            }
                        }
                        else
                        {
                            if ((sortedIndices[i] != 0) &&
                                (sortedIndices[i] != 1) &&
                                (sortedIndices[i] != 2) &&
                                (sortedIndices[i] != 3))
                            {
                                Assert.Fail("#01-" + i);
                            }
                        }
                    }
                }
            }
        }
 public void CanCallUnaryNegationOperatorOnDenseVector()
 {
     var vector = new DenseVector(this._data);
     var other = -vector;
     for (var i = 0; i < this._data.Length; i++)
     {
         Assert.AreEqual(-this._data[i], other[i]);
     }
 }
Ejemplo n.º 11
0
 public void CanCallUnaryPlusOperatorOnDenseVector()
 {
     var vector = new DenseVector(Data);
     var other = +vector;
     for (var i = 0; i < Data.Length; i++)
     {
         AssertHelpers.AreEqual(vector[i], other[i]);
     }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Create standard vector.
        /// </summary>
        /// <param name="size">Size of the vector.</param>
        /// <returns>New vector.</returns>
        protected Vector CreateStandardBcVector(int size)
        {
            Vector vector = new DenseVector(size);
            for (var i = 0; i < size; i++)
            {
                vector[i] = i + 1;
            }

            return vector;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DenseSvd"/> class. This object will compute the
        /// the singular value decomposition when the constructor is called and cache it's decomposition.
        /// </summary>
        /// <param name="matrix">The matrix to factor.</param>
        /// <param name="computeVectors">Compute the singular U and VT vectors or not.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If SVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
        public static DenseSvd Create(DenseMatrix matrix, bool computeVectors)
        {
            var nm = Math.Min(matrix.RowCount, matrix.ColumnCount);
            var s = new DenseVector(nm);
            var u = new DenseMatrix(matrix.RowCount);
            var vt = new DenseMatrix(matrix.ColumnCount);
            Control.LinearAlgebraProvider.SingularValueDecomposition(computeVectors, ((DenseMatrix) matrix.Clone()).Values, matrix.RowCount, matrix.ColumnCount, s.Values, u.Values, vt.Values);

            return new DenseSvd(s, u, vt, computeVectors);
        }
        /// <summary>
        /// Creates a new instance of the Vector class.
        /// </summary>
        /// <param name="data">The array to create this vector from.</param>
        /// <returns>The new <c>Vector</c>.</returns>
        protected override Vector<double> CreateVector(IList<double> data)
        {
            var vector = new DenseVector(data.Count);
            for (var index = 0; index < data.Count; index++)
            {
                vector[index] = data[index];
            }

            return vector;
        }
Ejemplo n.º 15
0
        /// <summary>
        /// The main method that runs the BiCGStab iterative solver.
        /// </summary>
        public void UseSolver()
        {
            // Create a sparse matrix. For now the size will be 10 x 10 elements
            Matrix matrix = CreateMatrix(10);

            // Create the right hand side vector. The size is the same as the matrix
            // and all values will be 2.0.
            Vector rightHandSideVector = new DenseVector(10, 2.0);

            // Create a preconditioner. The possibilities are:
            // 1) No preconditioner - Simply do not provide the solver with a preconditioner.
            // 2) A simple diagonal preconditioner - Create an instance of the Diagonal class.
            // 3) A ILU preconditioner - Create an instance of the IncompleteLu class.
            // 4) A ILU preconditioner with pivoting and drop tolerances - Create an instance of the Ilutp class.

            // Here we'll use the simple diagonal preconditioner.
            // We need a link to the matrix so the pre-conditioner can do it's work.
            IPreConditioner preconditioner = new Diagonal();

            // Create a new iterator. This checks for convergence of the results of the
            // iterative matrix solver.
            // In this case we'll create the default iterator
            IIterator iterator = Iterator.CreateDefault();

            // Create the solver
            BiCgStab solver = new BiCgStab(preconditioner, iterator);

            // Now that all is set we can solve the matrix equation.
            Vector solutionVector = solver.Solve(matrix, rightHandSideVector);

            // Another way to get the values is by using the overloaded solve method
            // In this case the solution vector needs to be of the correct size.
            solver.Solve(matrix, rightHandSideVector, solutionVector);

            // Finally you can check the reason the solver finished the iterative process
            // by calling the SolutionStatus property on the iterator
            ICalculationStatus status = iterator.Status;
            if (status is CalculationCancelled)
                Console.WriteLine("The user cancelled the calculation.");

            if (status is CalculationIndetermined)
                Console.WriteLine("Oh oh, something went wrong. The iterative process was never started.");

            if (status is CalculationConverged)
                Console.WriteLine("Yippee, the iterative process converged.");

            if (status is CalculationDiverged)
                Console.WriteLine("I'm sorry the iterative process diverged.");

            if (status is CalculationFailure)
                Console.WriteLine("Oh dear, the iterative process failed.");

            if (status is CalculationStoppedWithoutConvergence)
                Console.WriteLine("Oh dear, the iterative process did not converge.");
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UserEvd"/> class. This object will compute the
        /// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
        /// </summary>
        /// <param name="matrix">The matrix to factor.</param>
        /// <param name="symmetricity">If it is known whether the matrix is symmetric or not the routine can skip checking it itself.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
        public static UserEvd Create(Matrix<Complex> matrix, Symmetricity symmetricity)
        {
            if (matrix.RowCount != matrix.ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSquare);
            }

            var order = matrix.RowCount;

            // Initialize matricies for eigenvalues and eigenvectors
            var eigenVectors = DenseMatrix.CreateIdentity(order);
            var blockDiagonal = Matrix<Complex>.Build.SameAs(matrix, order, order);
            var eigenValues = new DenseVector(order);

            bool isSymmetric;
            switch (symmetricity)
            {
                case Symmetricity.Hermitian:
                    isSymmetric = true;
                    break;
                case Symmetricity.Asymmetric:
                    isSymmetric = false;
                    break;
                default:
                    isSymmetric = matrix.IsHermitian();
                    break;
            }

            if (isSymmetric)
            {
                var matrixCopy = matrix.ToArray();
                var tau = new Complex[order];
                var d = new double[order];
                var e = new double[order];

                SymmetricTridiagonalize(matrixCopy, d, e, tau, order);
                SymmetricDiagonalize(eigenVectors, d, e, order);
                SymmetricUntridiagonalize(eigenVectors, matrixCopy, tau, order);

                for (var i = 0; i < order; i++)
                {
                    eigenValues[i] = new Complex(d[i], e[i]);
                }
            }
            else
            {
                var matrixH = matrix.ToArray();
                NonsymmetricReduceToHessenberg(eigenVectors, matrixH, order);
                NonsymmetricReduceHessenberToRealSchur(eigenVectors, eigenValues, matrixH, order);
            }

            blockDiagonal.SetDiagonal(eigenValues);

            return new UserEvd(eigenVectors, eigenValues, blockDiagonal, isSymmetric);
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DenseEvd"/> class. This object will compute the
        /// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
        /// </summary>
        /// <param name="matrix">The matrix to factor.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
        public DenseEvd(DenseMatrix matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

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

            var order = matrix.RowCount;

            // Initialize matricies for eigenvalues and eigenvectors
            MatrixEv = DenseMatrix.Identity(order);
            MatrixD = matrix.CreateMatrix(order, order);
            VectorEv = new DenseVector(order);

            IsSymmetric = true;

            for (var i = 0; i < order & IsSymmetric; i++)
            {
                for (var j = 0; j < order & IsSymmetric; j++)
                {
                    IsSymmetric &= matrix[i, j] == matrix[j, i].Conjugate();
                }
            }

            if (IsSymmetric)
            {
                var matrixCopy = matrix.ToArray();
                var tau = new Complex[order];
                var d = new double[order];
                var e = new double[order];

                SymmetricTridiagonalize(matrixCopy, d, e, tau, order);
                SymmetricDiagonalize(((DenseMatrix)MatrixEv).Data, d, e, order);
                SymmetricUntridiagonalize(((DenseMatrix)MatrixEv).Data, matrixCopy, tau, order);

                for (var i = 0; i < order; i++)
                {
                    VectorEv[i] = new Complex(d[i], e[i]);
                }
            }
            else
            {
                var matrixH = matrix.ToArray();
                NonsymmetricReduceToHessenberg(((DenseMatrix)MatrixEv).Data, matrixH, order);
                NonsymmetricReduceHessenberToRealSchur(((DenseVector)VectorEv).Data, ((DenseMatrix)MatrixEv).Data, matrixH, order);
            }

            MatrixD.SetDiagonal(VectorEv);
        }
Ejemplo n.º 18
0
 public void CanCreateVectorFromArray()
 {
     var data = new[] { 1.0, 2.0, 3.0, 4.0 };
     var vector = new DenseVector(data);
     Assert.AreSame(data, vector.Data);
     for( var i = 0; i < data.Length; i++)
     {
         Assert.AreEqual(data[i], vector[i]);
     }
     vector[0] = 100.0;
     Assert.AreEqual(100.0, data[0]);
 }
        public void DetermineStatus()
        {
            var criterium = new FailureStopCriterium();
            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { new Complex(3.0, 0), new Complex(2.0, 0), new Complex(1, 0) });
            var source = new DenseVector(new[] { new Complex(1001.0, 0), Complex.Zero, new Complex(2003.0, 0) });
            var residual = new DenseVector(new[] { new Complex(1.0, 0), new Complex(2.0, 0), new Complex(3, 0) });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationRunning), criterium.Status, "Should be running");
        }
        public void DetermineStatus()
        {
            var criterium = new FailureStopCriterium();
            Assert.IsNotNull(criterium, "There should be a criterium");

            var solution = new DenseVector(new[] { 3.0f, 2.0f, 1.0f });
            var source = new DenseVector(new[] { 1001.0f, 0.0f, 2003.0f });
            var residual = new DenseVector(new[] { 1.0f, 2.0f, 3.0f });

            criterium.DetermineStatus(5, solution, source, residual);
            Assert.IsInstanceOf(typeof(CalculationRunning), criterium.Status, "Should be running");
        }
Ejemplo n.º 21
0
        public void ApproximateWithNullVector()
        {
            const int Size = 10;
            var newMatrix = CreateUnitMatrix(Size);
            var vector = CreateStandardBcVector(Size);

            var preconditioner = CreatePreconditioner();
            preconditioner.Initialize(newMatrix);

            Vector<double> result = new DenseVector(vector.Count + 10);
            preconditioner.Approximate(null, result);
        }
Ejemplo n.º 22
0
        /// <summary>
        /// Creates a convergence monitor.
        /// </summary>
        public void UseMonitor()
        {
            // Set the maximum increase in residual that we allow before
            // stopping the iteration because of divergence.
            double maximumIncrease = 0.1;

            // Set the maximum number of iterations that the convergence monitor
            // will allow before indicating that the iteration must be stopped.
            int maximumNumberOfIterations = 100;

            // Set the value the residual must maximally have before the
            // convergence monitor will declare that the iteration has converged.
            // Note that this residual is relative to the starting point which is
            // determined by the matrix norm.
            double minimumResidual = 1E-5;

            List<IIterationStopCriterium> criteria = new List<IIterationStopCriterium>();
            criteria.Add(new FailureStopCriterium());
            criteria.Add(new DivergenceStopCriterium(maximumIncrease));
            criteria.Add(new IterationCountStopCriterium(maximumNumberOfIterations));
            criteria.Add(new ResidualStopCriterium(minimumResidual));

            // Create the iterator
            Iterator monitor = new Iterator(criteria);

            // To be able to use the convergence monitor we'll need a solution vector and
            // a residual vector. For now fill both with silly numbers.
            Vector sourceVector = new DenseVector(10, 3.5);
            Vector solutionVector = new DenseVector(10, 2.5);
            Vector residualVector = new DenseVector(10, 1.5);

            // Check the solution status. There should not be a real status because
            // we haven't done anything yet
            Console.WriteLine("Solution status: " + monitor.Status.ToString());

            // Now use the monitor in a fake iteration. If all goes well this iteration should
            // stop because the number of iterations becomes larger than the number of iterations
            // we allow.
            int currentIteration = 0;
            while ((monitor.Status is CalculationRunning) || (monitor.Status is CalculationIndetermined))
            {
                currentIteration++;
                Console.WriteLine("Working. Iteration no: " + currentIteration.ToString());
                monitor.DetermineStatus(currentIteration, solutionVector, sourceVector, residualVector);
            }

            // Indicate that we exited the loop
            Console.WriteLine("Stopped working.");
            // Indicate why we exited the loop. Note that this should say
            // SolutionStatus.IterationBoundsReached.
            Console.WriteLine("Stop reason: " + monitor.Status.ToString());
        }
        public void CanAddTwoDenseVectorsUsingOperator()
        {
            var vector = new DenseVector(this._data);
            var other = new DenseVector(this._data);
            var result = vector + other;

            for (var i = 0; i < this._data.Length; i++)
            {
                Assert.AreEqual(this._data[i], vector[i], "Making sure the original vector wasn't modified.");
                Assert.AreEqual(this._data[i], other[i], "Making sure the original vector wasn't modified.");
                Assert.AreEqual(this._data[i] * 2.0, result[i]);
            }
        }
Ejemplo n.º 24
0
        public void CanAddTwoDenseVectorsUsingOperator()
        {
            var vector = new DenseVector(Data);
            var other = new DenseVector(Data);
            var result = vector + other;
            CollectionAssert.AreEqual(Data, vector, "Making sure the original vector wasn't modified.");
            CollectionAssert.AreEqual(Data, other, "Making sure the original vector wasn't modified.");

            for (var i = 0; i < Data.Length; i++)
            {
                Assert.AreEqual(Data[i] * 2.0, result[i]);
            }
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Check the result.
        /// </summary>
        /// <param name="preconditioner">Specific preconditioner.</param>
        /// <param name="matrix">Source matrix.</param>
        /// <param name="vector">Initial vector.</param>
        /// <param name="result">Result vector.</param>
        protected override void CheckResult(IPreConditioner preconditioner, SparseMatrix matrix, Vector vector, Vector result)
        {
            Assert.AreEqual(typeof(Diagonal), preconditioner.GetType(), "#01");

            // Compute M * result = product
            // compare vector and product. Should be equal
            Vector product = new DenseVector(result.Count);
            matrix.Multiply(result, product);
            for (var i = 0; i < product.Count; i++)
            {
                Assert.IsTrue(vector[i].AlmostEqual(product[i], -Epsilon.Magnitude()), "#02-" + i);
            }
        }
Ejemplo n.º 26
0
        public void CanAddTwoDenseVectorsUsingOperator()
        {
            var vector = new DenseVector(Data);
            var other = new DenseVector(Data);
            var result = vector + other;

            for (var i = 0; i < Data.Length; i++)
            {
                AssertHelpers.AreEqual(Data[i], vector[i]);
                AssertHelpers.AreEqual(Data[i], other[i]);
                AssertHelpers.AreEqual(Data[i] * 2.0f, result[i]);
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Initializes a new instance of the <see cref="UserEvd"/> class. This object will compute the
        /// the eigenvalue decomposition when the constructor is called and cache it's decomposition.
        /// </summary>
        /// <param name="matrix">The matrix to factor.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If EVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
        public static UserEvd Create(Matrix<Complex> matrix)
        {
            if (matrix.RowCount != matrix.ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSquare);
            }

            var order = matrix.RowCount;

            // Initialize matricies for eigenvalues and eigenvectors
            var eigenVectors = DenseMatrix.CreateIdentity(order);
            var blockDiagonal = matrix.CreateMatrix(order, order);
            var eigenValues = new DenseVector(order);

            var isSymmetric = true;

            for (var i = 0; isSymmetric && i < order; i++)
            {
                for (var j = 0; isSymmetric && j < order; j++)
                {
                    isSymmetric &= matrix.At(i, j) == matrix.At(j, i).Conjugate();
                }
            }

            if (isSymmetric)
            {
                var matrixCopy = matrix.ToArray();
                var tau = new Complex[order];
                var d = new double[order];
                var e = new double[order];

                SymmetricTridiagonalize(matrixCopy, d, e, tau, order);
                SymmetricDiagonalize(eigenVectors, d, e, order);
                SymmetricUntridiagonalize(eigenVectors, matrixCopy, tau, order);

                for (var i = 0; i < order; i++)
                {
                    eigenValues[i] = new Complex(d[i], e[i]);
                }
            }
            else
            {
                var matrixH = matrix.ToArray();
                NonsymmetricReduceToHessenberg(eigenVectors, matrixH, order);
                NonsymmetricReduceHessenberToRealSchur(eigenVectors, eigenValues, matrixH, order);
            }

            blockDiagonal.SetDiagonal(eigenValues);

            return new UserEvd(eigenVectors, eigenValues, blockDiagonal, isSymmetric);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DenseSvd"/> class. This object will compute the
        /// the singular value decomposition when the constructor is called and cache it's decomposition.
        /// </summary>
        /// <param name="matrix">The matrix to factor.</param>
        /// <param name="computeVectors">Compute the singular U and VT vectors or not.</param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">If SVD algorithm failed to converge with matrix <paramref name="matrix"/>.</exception>
        public DenseSvd(DenseMatrix matrix, bool computeVectors)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

            ComputeVectors = computeVectors;
            var nm = Math.Min(matrix.RowCount, matrix.ColumnCount);
            VectorS = new DenseVector(nm);
            MatrixU = new DenseMatrix(matrix.RowCount);
            MatrixVT = new DenseMatrix(matrix.ColumnCount);
            Control.LinearAlgebraProvider.SingularValueDecomposition(computeVectors, ((DenseMatrix)matrix.Clone()).Data, matrix.RowCount, matrix.ColumnCount, ((DenseVector)VectorS).Data, ((DenseMatrix)MatrixU).Data, ((DenseMatrix)MatrixVT).Data);
        }
        public void CanCreateDenseVectorFromArray()
        {
            var data = new double[Data.Length];
            Array.Copy(Data, data, Data.Length);
            var vector = new DenseVector(data);

            for (var i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], vector[i]);
            }

            vector[0] = 100.0;
            Assert.AreEqual(100.0, data[0]);
        }
Ejemplo n.º 30
0
        public void CanCreateDenseVectorFromArray()
        {
            var data = new Complex32[Data.Length];
            Array.Copy(Data, data, Data.Length);
            var vector = new DenseVector(data);

            for (var i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], vector[i]);
            }

            vector[0] = new Complex32(10.0f, 1);
            Assert.AreEqual(new Complex32(10.0f, 1), data[0]);
        }
Ejemplo n.º 31
0
        /// <summary>
        /// Run example
        /// </summary>
        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 = DenseMatrix.OfArray(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 criteria to monitor an iterative calculation. There are next available stop criteria:
            // - DivergenceStopCriterion: monitors an iterative calculation for signs of divergence;
            // - FailureStopCriterion: monitors residuals for NaN's;
            // - IterationCountStopCriterion: monitors the numbers of iteration steps;
            // - ResidualStopCriterion: monitors residuals if calculation is considered converged;

            // Stop calculation if 1000 iterations reached during calculation
            var iterationCountStopCriterion = new IterationCountStopCriterion <double>(1000);

            // Stop calculation if residuals are below 1E-10 --> the calculation is considered converged
            var residualStopCriterion = new ResidualStopCriterion <double>(1e-10);

            // Create monitor with defined stop criteria
            var monitor = new Iterator <double>(iterationCountStopCriterion, residualStopCriterion);

            // Create Multiple-Lanczos Bi-Conjugate Gradient Stabilized solver
            var solver = new MlkBiCgStab();

            // 1. Solve the matrix equation
            var resultX = matrixA.SolveIterative(vectorB, solver, monitor);

            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(monitor.Status);
            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();
        }
Ejemplo n.º 32
0
        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);
            }
        }
Ejemplo n.º 33
0
        private DenseMatrix CalculateTransformation(PointCloud scene_in, PointCloud model_in, DenseMatrix T_0)
        {
            var T = new DenseMatrix[maxIter];

            T[0] = T_0;

            var startTime = DateTime.Now;

            // perform deep copy of input scene because we modify it directly in transform calculation
            var scene = (PointCloud)scene_in.Clone();
            // no need to clone the model since we don't modify it
            var model = model_in;

            // precompute KdTree of model (this doesn't change for each model-scene pair)
            var   kdModel = KdTree.Construct(3, (List <Vector>)model);
            int   k       = 1;
            float err     = 0f;

            for (; k < maxIter; k++)
            {
                // Step 1 - Apply previous transform T_k-1
                for (int i = 0; i < scene.Points.Count; i++)
                {
                    scene.Points[i].ApplyAffineTransformation(T[k - 1]);
                }

                // Step 2 - Find correspondances
                var pairs = new PointPair <Vector> [scene.Points.Count];
                for (int i = 0; i < scene.Points.Count; i++)
                {
                    var nn = kdModel.FindNearestNeighbour(scene.Points[i]);
                    pairs[i] = new PointPair <Vector>(scene.Points[i], nn);
                }

                // Step 3 - Check convergence criterion
                err = 0;
                foreach (var pair in pairs)
                {
                    var v = pair.Scene - pair.Model;
                    // measure 'distance' or error between colour of these points
                    err += (float)Math.Sqrt(v.DotProduct(v));
                }
                err /= scene.Points.Count; // average error
                if (err <= this.minError)
                {
                    break;                       // transform T_k-1 was best! terminate.
                }
                if (k + 1 == maxIter)
                {
                    break;                   // pointless compute ahead? else, must compute better transform!...
                }
                // Step 4 - Calculate composite matrix H
                var H = new DenseMatrix(3);
                for (int i = 0; i < pairs.Length; i++)
                {
                    H += (DenseMatrix)DenseVector.OuterProduct(pairs[i].Scene, pairs[i].Model);
                }

                // Step 5 - Decompose H-matrix using SVD; calculate 3x3 rotation matrix R
                var svd = H.Svd(true);
                // Check if special reflection case (det(V) < 0)
                var VT = svd.VT();
                if (VT.Transpose().Determinant() < 0)
                {
                    VT.SetRow(2, -VT.Row(2));                                   // negate row 3
                }
                DenseMatrix R = (DenseMatrix)(svd.U() * VT);

                // Step 6 - Calculate complete affine transformation matrix, T
                // --> R_ext   = 4x4 transformation matrix using R rotation
                var R_ext = DenseMatrix.Identity(4);
                R_ext.SetSubMatrix(0, 3, 0, 3, R.Transpose());

                // --> T       = C_model R_ext C_scene
                T[k] = R_ext; // new transform ready (from T[k-1] * Scene -> Model)
            }

            // reached optimal transformation sequence, return compound matrix T_opt = T_n * ... * T_1 * T_0
            var T_opt = DenseMatrix.Identity(4);

            foreach (var Tk in T)
            {
                if (Tk == null)
                {
                    break;
                }
                T_opt = Tk * T_opt;
            }

            if (DebugSettings.Default.DebugMode)
            {
                this.log.WriteLine("{0},{1},{2}", this.logN++, k, err);
            }

            return(T_opt);
        }
Ejemplo n.º 34
0
        public static void Main(string[] args)
        {
            var     tasks = new List <ILPTask> (new TasksXmlReader("tasks2.xml").ReadTasks().Values);
            ILPTask task  = null;

            task = new ILPTask()
            {
                C = DenseVector.OfEnumerable(new [] {
                    -3.5, 1, 0, 0, 0,
                }),
                B = DenseVector.OfEnumerable(new [] {
                    15.0, 6, 0
                }),
                M = 3,
                N = 5,
                A = DenseMatrix.OfArray(new [, ] {
                    { 5.0, -1, 1, 0, 0 },
                    { -1.0, 2, 0, 1, 0 },
                    { -7.0, 2, 0, 0, 1 },
                }),
                DL = DenseVector.OfEnumerable(new [] {
                    0.0, 0, 0, 0, 0,
                }),
                DR = DenseVector.OfEnumerable(new [] {
                    1e100, 1e100, 1e100, 1e100, 1e100,
                }),
            };


            task = new ILPTask()
            {
                C = DenseVector.OfEnumerable(new [] {
                    2.0, -5, 0, 0, 0
                }),
                B = DenseVector.OfEnumerable(new [] {
                    -1.0, 10, 3,
                }),
                M = 3,
                N = 5,
                A = DenseMatrix.OfArray(new [, ] {
                    { -2.0, -1, 1, 0, 0 },
                    { 3.0, 1, 0, 1, 0 },
                    { -1.0, 1, 0, 0, 1 },
                }),
                DL = DenseVector.OfEnumerable(new [] {
                    0.0, 0, 0, 0, 0,
                }),
                DR = DenseVector.OfEnumerable(new [] {
                    1e100, 1e100, 1e100, 1e100, 1e100,
                }),
            };


            task = new ILPTask()
            {
                C = DenseVector.OfEnumerable(new [] {
                    21.0, 11, 0
                }),
                B = DenseVector.OfEnumerable(new [] {
                    13.0,
                }),
                M = 1,
                N = 3,
                A = DenseMatrix.OfArray(new [, ] {
                    { 7.0, 4, 1 },
                }),
                DL = DenseVector.OfEnumerable(new [] {
                    0.0, 0, 0,
                }),
                DR = DenseVector.OfEnumerable(new [] {
                    1e100, 1e100, 1e100,
                }),
            };



            task = new ILPTask()
            {
                C = DenseVector.OfEnumerable(new [] {
                    2.0, 1
                }),
                B = DenseVector.OfEnumerable(new [] {
                    3.0,
                }),
                M = 1,
                N = 2,
                A = DenseMatrix.OfArray(new [, ] {
                    { 2.0, 1 },
                }),
                DL = DenseVector.OfEnumerable(new [] {
                    0.0, 0,
                }),
                DR = DenseVector.OfEnumerable(new [] {
                    1e100, 1e100,
                }),
            };


            task = new ILPTask()
            {
                C = DenseVector.OfEnumerable(new [] {
                    //2.0, -3, 1, 12, -14, 0, 5,
                    -2.0, 3, -1, -12, 14, 0, -5,
                }),
                B = DenseVector.OfEnumerable(new [] {
                    20.0, 4, 14
                }),
                M = 3,
                N = 7,
                A = DenseMatrix.OfArray(new [, ] {
                    { 2.0, -3, 4, 5, 6, -8, 4 },
                    { -3.0, 4, -5, 1, 0, 12, -7 },
                    { 1.0, 1, 1, 1, 1, 1, 1 },
                }),
                DL = DenseVector.OfEnumerable(new [] {
                    0.0, 0, 0, 0, 0, 0, 0, 0,
                    //-1.0, -2, 0, -3, -2, -1, -4, -4
                }),
                DR = DenseVector.OfEnumerable(new [] {
                    1e100, 1e100, 1e100,
                    1e100, 1e100, 1e100,
                    1e100, 1e100,
                    //3.0, 4, 5, 3, 4, 5, 3, 4
                }),
            };


            //task = tasks[2];
            var result = task.SolveILPByCutoff();

            //var result = task.SolveILByBranching ();
            //var result = task.SolveILPByCutoff ();
            Console.WriteLine("=========== FINAL SOLUTION");
            Console.WriteLine(result);
            Console.WriteLine("VALUE");
            Console.WriteLine(task.C * result);
        }
Ejemplo n.º 35
0
        /// <summary>
        /// Initializes the preconditioner and loads the internal data structures.
        /// </summary>
        /// <param name="matrix">
        /// The <see cref="Matrix"/> upon which this preconditioner is based. Note that the
        /// method takes a general matrix type. However internally the data is stored
        /// as a sparse matrix. Therefore it is not recommended to pass a dense matrix.
        /// </param>
        /// <exception cref="ArgumentNullException"> If <paramref name="matrix"/> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square matrix.</exception>
        public void Initialize(Matrix matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }

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

            var sparseMatrix = (matrix is SparseMatrix) ? matrix as SparseMatrix : SparseMatrix.OfMatrix(matrix);

            // The creation of the preconditioner follows the following algorithm.
            // spaceLeft = lfilNnz * nnz(A)
            // for i = 1, .. , n
            // {
            //    w = a(i,*)
            //    for j = 1, .. , i - 1
            //    {
            //        if (w(j) != 0)
            //        {
            //            w(j) = w(j) / a(j,j)
            //            if (w(j) < dropTol)
            //            {
            //                w(j) = 0;
            //            }
            //            if (w(j) != 0)
            //            {
            //                w = w - w(j) * U(j,*)
            //            }
            //        }
            //    }
            //
            //    for j = i, .. ,n
            //    {
            //        if w(j) <= dropTol * ||A(i,*)||
            //        {
            //            w(j) = 0
            //        }
            //    }
            //
            //    spaceRow = spaceLeft / (n - i + 1) // Determine the space for this row
            //    lfil = spaceRow / 2  // space for this row of L
            //    l(i,j) = w(j) for j = 1, .. , i -1 // only the largest lfil elements
            //
            //    lfil = spaceRow - nnz(L(i,:))  // space for this row of U
            //    u(i,j) = w(j) for j = i, .. , n // only the largest lfil - 1 elements
            //    w = 0
            //
            //    if max(U(i,i + 1: n)) > U(i,i) / pivTol then // pivot if necessary
            //    {
            //        pivot by swapping the max and the diagonal entries
            //        Update L, U
            //        Update P
            //    }
            //    spaceLeft = spaceLeft - nnz(L(i,:)) - nnz(U(i,:))
            // }
            // Create the lower triangular matrix
            _lower = new SparseMatrix(sparseMatrix.RowCount);

            // Create the upper triangular matrix and copy the values
            _upper = new SparseMatrix(sparseMatrix.RowCount);

            // Create the pivot array
            _pivots = new int[sparseMatrix.RowCount];
            for (var i = 0; i < _pivots.Length; i++)
            {
                _pivots[i] = i;
            }

            Vector workVector   = new DenseVector(sparseMatrix.RowCount);
            Vector rowVector    = new DenseVector(sparseMatrix.ColumnCount);
            var    indexSorting = new int[sparseMatrix.RowCount];

            // spaceLeft = lfilNnz * nnz(A)
            var spaceLeft = (int)_fillLevel * sparseMatrix.NonZerosCount;

            // for i = 1, .. , n
            for (var i = 0; i < sparseMatrix.RowCount; i++)
            {
                // w = a(i,*)
                sparseMatrix.Row(i, workVector);

                // pivot the row
                PivotRow(workVector);
                var vectorNorm = workVector.Norm(Double.PositiveInfinity);

                // for j = 1, .. , i - 1)
                for (var j = 0; j < i; j++)
                {
                    // if (w(j) != 0)
                    // {
                    //     w(j) = w(j) / a(j,j)
                    //     if (w(j) < dropTol)
                    //     {
                    //         w(j) = 0;
                    //     }
                    //     if (w(j) != 0)
                    //     {
                    //         w = w - w(j) * U(j,*)
                    //     }
                    if (workVector[j] != 0.0)
                    {
                        // Calculate the multiplication factors that go into the L matrix
                        workVector[j] = workVector[j] / _upper[j, j];
                        if (Math.Abs(workVector[j]) < _dropTolerance)
                        {
                            workVector[j] = 0.0f;
                        }

                        // Calculate the addition factor
                        if (workVector[j] != 0.0)
                        {
                            // vector update all in one go
                            _upper.Row(j, rowVector);

                            // zero out columnVector[k] because we don't need that
                            // one anymore for k = 0 to k = j
                            for (var k = 0; k <= j; k++)
                            {
                                rowVector[k] = 0.0f;
                            }

                            rowVector.Multiply(workVector[j], rowVector);
                            workVector.Subtract(rowVector, workVector);
                        }
                    }
                }

                // for j = i, .. ,n
                for (var j = i; j < sparseMatrix.RowCount; j++)
                {
                    // if w(j) <= dropTol * ||A(i,*)||
                    // {
                    //     w(j) = 0
                    // }
                    if (Math.Abs(workVector[j]) <= _dropTolerance * vectorNorm)
                    {
                        workVector[j] = 0.0f;
                    }
                }

                // spaceRow = spaceLeft / (n - i + 1) // Determine the space for this row
                var spaceRow = spaceLeft / (sparseMatrix.RowCount - i + 1);

                // lfil = spaceRow / 2  // space for this row of L
                var fillLevel = spaceRow / 2;
                FindLargestItems(0, i - 1, indexSorting, workVector);

                // l(i,j) = w(j) for j = 1, .. , i -1 // only the largest lfil elements
                var lowerNonZeroCount = 0;
                var count             = 0;
                for (var j = 0; j < i; j++)
                {
                    if ((count > fillLevel) || (indexSorting[j] == -1))
                    {
                        break;
                    }

                    _lower[i, indexSorting[j]] = workVector[indexSorting[j]];
                    count             += 1;
                    lowerNonZeroCount += 1;
                }

                FindLargestItems(i + 1, sparseMatrix.RowCount - 1, indexSorting, workVector);

                // lfil = spaceRow - nnz(L(i,:))  // space for this row of U
                fillLevel = spaceRow - lowerNonZeroCount;

                // u(i,j) = w(j) for j = i + 1, .. , n // only the largest lfil - 1 elements
                var upperNonZeroCount = 0;
                count = 0;
                for (var j = 0; j < sparseMatrix.RowCount - i; j++)
                {
                    if ((count > fillLevel - 1) || (indexSorting[j] == -1))
                    {
                        break;
                    }

                    _upper[i, indexSorting[j]] = workVector[indexSorting[j]];
                    count             += 1;
                    upperNonZeroCount += 1;
                }

                // Simply copy the diagonal element. Next step is to see if we pivot
                _upper[i, i] = workVector[i];

                // if max(U(i,i + 1: n)) > U(i,i) / pivTol then // pivot if necessary
                // {
                //     pivot by swapping the max and the diagonal entries
                //     Update L, U
                //     Update P
                // }

                // Check if we really need to pivot. If (i+1) >=(mCoefficientMatrix.Rows -1) then
                // we are working on the last row. That means that there is only one number
                // And pivoting is useless. Also the indexSorting array will only contain
                // -1 values.
                if ((i + 1) < (sparseMatrix.RowCount - 1))
                {
                    if (Math.Abs(workVector[i]) < _pivotTolerance * Math.Abs(workVector[indexSorting[0]]))
                    {
                        // swap columns of u (which holds the values of A in the
                        // sections that haven't been partitioned yet.
                        SwapColumns(_upper, i, indexSorting[0]);

                        // Update P
                        var temp = _pivots[i];
                        _pivots[i] = _pivots[indexSorting[0]];
                        _pivots[indexSorting[0]] = temp;
                    }
                }

                // spaceLeft = spaceLeft - nnz(L(i,:)) - nnz(U(i,:))
                spaceLeft -= lowerNonZeroCount + upperNonZeroCount;
            }

            for (var i = 0; i < _lower.RowCount; i++)
            {
                _lower[i, i] = 1.0f;
            }
        }
Ejemplo n.º 36
0
        public static Tuple<double[,], double> AffineAlign(double[] sx, double[] sy, double[] tx, double[] ty)
        {
            List<Vector<double>> src = new List<Vector<double>>();
            for (int i = 0; i < sx.Length; i++)
            {
                src.Add(DenseVector.OfArray(new[] { sx[i], sy[i], 1 }));
            }

            var a = DenseMatrix.OfRowVectors(src);

            var vec1 = a.TransposeThisAndMultiply(a).Inverse().Multiply(a.Transpose()).Multiply(DenseVector.OfArray(tx));
            var vec2 = a.TransposeThisAndMultiply(a).Inverse().Multiply(a.Transpose()).Multiply(DenseVector.OfArray(ty));
            var affine = DenseMatrix.OfRowVectors(vec1, vec2);


            //calculate errors
            List<double> errors = new List<double>();
            for (int i = 0; i < src.Count; i++)
            {
                errors.Add((affine * src[i] - DenseVector.OfArray(new[] { tx[i], ty[i] })).PointwisePower(2).Norm(1));
            }

            return new Tuple<double[,], double>(new double[,]
                {
                    {affine[0, 0], affine[0, 1], 0, affine[0, 2]},
                    {affine[1, 0], affine[1, 1], 0, affine[1, 2]},
                    {0, 0, 1, 0},
                    {0, 0, 0, 1},
                },
                errors.Max());
        }
 public void ParseIfMissingClosingParenThrowsFormatException()
 {
     Assert.That(() => DenseVector.Parse("(1"), Throws.TypeOf <FormatException>());
     Assert.That(() => DenseVector.Parse("[1"), Throws.TypeOf <FormatException>());
 }
Ejemplo n.º 38
0
        public Vector Sigmoid(Vector z)
        {
            //g = 1.0 ./ (1.0 + exp(-z));

            return(DenseVector.Create(z.Count, i => 1 / (1 + Math.Pow(Math.E, -1 * z[i]))));
        }
Ejemplo n.º 39
0
        /// <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<Numerics.Complex32> matrix, Vector<Numerics.Complex32> input, Vector<Numerics.Complex32> 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;

            // 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);
            }

            if (input.Count != matrix.RowCount)
            {
                throw Matrix.DimensionsDontMatch<ArgumentException>(input, matrix);
            }

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

            if (_preconditioner == null)
            {
                _preconditioner = new UnitPreconditioner<Numerics.Complex32>();
            }

            _preconditioner.Initialize(matrix);

            // x_0 is initial guess
            // Take x_0 = 0
            var xtemp = new DenseVector(input.Count);

            // r_0 = b - Ax_0
            // This is basically a SAXPY so it could be made a lot faster
            var residuals = new DenseVector(matrix.RowCount);
            CalculateTrueResidual(matrix, residuals, xtemp, input);

            // Define the temporary scalars
            Numerics.Complex32 beta = 0;

            // Define the temporary vectors
            // rDash_0 = r_0
            var rdash = DenseVector.OfVector(residuals);

            // t_-1 = 0
            var t = new DenseVector(residuals.Count);
            var t0 = new DenseVector(residuals.Count);

            // w_-1 = 0
            var w = new DenseVector(residuals.Count);

            // Define the remaining temporary vectors
            var c = new DenseVector(residuals.Count);
            var p = new DenseVector(residuals.Count);
            var s = new DenseVector(residuals.Count);
            var u = new DenseVector(residuals.Count);
            var y = new DenseVector(residuals.Count);
            var z = new DenseVector(residuals.Count);

            var temp = new DenseVector(residuals.Count);
            var temp2 = new DenseVector(residuals.Count);
            var temp3 = new DenseVector(residuals.Count);

            // for (k = 0, 1, .... )
            var iterationNumber = 0;
            while (ShouldContinue(iterationNumber, xtemp, input, residuals))
            {
                // p_k = r_k + beta_(k-1) * (p_(k-1) - u_(k-1))
                p.Subtract(u, temp);

                temp.Multiply(beta, temp2);
                residuals.Add(temp2, p);

                // Solve M b_k = p_k
                _preconditioner.Approximate(p, temp);

                // s_k = A b_k
                matrix.Multiply(temp, s);

                // alpha_k = (r*_0 * r_k) / (r*_0 * s_k)
                var alpha = rdash.ConjugateDotProduct(residuals)/rdash.ConjugateDotProduct(s);

                // y_k = t_(k-1) - r_k - alpha_k * w_(k-1) + alpha_k s_k
                s.Subtract(w, temp);
                t.Subtract(residuals, y);

                temp.Multiply(alpha, temp2);
                y.Add(temp2, temp3);
                temp3.CopyTo(y);

                // Store the old value of t in t0
                t.CopyTo(t0);

                // t_k = r_k - alpha_k s_k
                s.Multiply(-alpha, temp2);
                residuals.Add(temp2, t);

                // Solve M d_k = t_k
                _preconditioner.Approximate(t, temp);

                // c_k = A d_k
                matrix.Multiply(temp, c);
                var cdot = c.ConjugateDotProduct(c);

                // cDot can only be zero if c is a zero vector
                // We'll set cDot to 1 if it is zero to prevent NaN's
                // Note that the calculation should continue fine because
                // c.DotProduct(t) will be zero and so will c.DotProduct(y)
                if (cdot.Real.AlmostEqual(0, 1) && cdot.Imaginary.AlmostEqual(0, 1))
                {
                    cdot = 1.0f;
                }

                // Even if we don't want to do any BiCGStab steps we'll still have
                // to do at least one at the start to initialize the
                // system, but we'll only have to take special measures
                // if we don't do any so ...
                var ctdot = c.ConjugateDotProduct(t);
                Numerics.Complex32 eta;
                Numerics.Complex32 sigma;
                if (((_numberOfBiCgStabSteps == 0) && (iterationNumber == 0)) || ShouldRunBiCgStabSteps(iterationNumber))
                {
                    // sigma_k = (c_k * t_k) / (c_k * c_k)
                    sigma = ctdot/cdot;

                    // eta_k = 0
                    eta = 0;
                }
                else
                {
                    var ydot = y.ConjugateDotProduct(y);

                    // yDot can only be zero if y is a zero vector
                    // We'll set yDot to 1 if it is zero to prevent NaN's
                    // Note that the calculation should continue fine because
                    // y.DotProduct(t) will be zero and so will c.DotProduct(y)
                    if (ydot.Real.AlmostEqual(0, 1) && ydot.Imaginary.AlmostEqual(0, 1))
                    {
                        ydot = 1.0f;
                    }

                    var ytdot = y.ConjugateDotProduct(t);
                    var cydot = c.ConjugateDotProduct(y);

                    var denom = (cdot*ydot) - (cydot*cydot);

                    // sigma_k = ((y_k * y_k)(c_k * t_k) - (y_k * t_k)(c_k * y_k)) / ((c_k * c_k)(y_k * y_k) - (y_k * c_k)(c_k * y_k))
                    sigma = ((ydot*ctdot) - (ytdot*cydot))/denom;

                    // eta_k = ((c_k * c_k)(y_k * t_k) - (y_k * c_k)(c_k * t_k)) / ((c_k * c_k)(y_k * y_k) - (y_k * c_k)(c_k * y_k))
                    eta = ((cdot*ytdot) - (cydot*ctdot))/denom;
                }

                // u_k = sigma_k s_k + eta_k (t_(k-1) - r_k + beta_(k-1) u_(k-1))
                u.Multiply(beta, temp2);
                t0.Add(temp2, temp);

                temp.Subtract(residuals, temp3);
                temp3.CopyTo(temp);
                temp.Multiply(eta, temp);

                s.Multiply(sigma, temp2);
                temp.Add(temp2, u);

                // z_k = sigma_k r_k +_ eta_k z_(k-1) - alpha_k u_k
                z.Multiply(eta, z);
                u.Multiply(-alpha, temp2);
                z.Add(temp2, temp3);
                temp3.CopyTo(z);

                residuals.Multiply(sigma, temp2);
                z.Add(temp2, temp3);
                temp3.CopyTo(z);

                // x_(k+1) = x_k + alpha_k p_k + z_k
                p.Multiply(alpha, temp2);
                xtemp.Add(temp2, temp3);
                temp3.CopyTo(xtemp);

                xtemp.Add(z, temp3);
                temp3.CopyTo(xtemp);

                // r_(k+1) = t_k - eta_k y_k - sigma_k c_k
                // Copy the old residuals to a temp vector because we'll
                // need those in the next step
                residuals.CopyTo(t0);

                y.Multiply(-eta, temp2);
                t.Add(temp2, residuals);

                c.Multiply(-sigma, temp2);
                residuals.Add(temp2, temp3);
                temp3.CopyTo(residuals);

                // beta_k = alpha_k / sigma_k * (r*_0 * r_(k+1)) / (r*_0 * r_k)
                // But first we check if there is a possible NaN. If so just reset beta to zero.
                beta = (!sigma.Real.AlmostEqual(0, 1) || !sigma.Imaginary.AlmostEqual(0, 1)) ? alpha/sigma*rdash.ConjugateDotProduct(residuals)/rdash.ConjugateDotProduct(t0) : 0;

                // w_k = c_k + beta_k s_k
                s.Multiply(beta, temp2);
                c.Add(temp2, w);

                // Get the real value
                _preconditioner.Approximate(xtemp, result);

                // Now check for convergence
                if (!ShouldContinue(iterationNumber, result, input, residuals))
                {
                    // Recalculate the residuals and go round again. This is done to ensure that
                    // we have the proper residuals.
                    CalculateTrueResidual(matrix, residuals, result, input);
                }

                // Next iteration.
                iterationNumber++;
            }
        }
Ejemplo n.º 40
0
        /// <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 <see cref="Matrix"/>, <c>A</c>.</param>
        /// <param name="input">The solution <see cref="Vector"/>, <c>b</c>.</param>
        /// <param name="result">The result <see cref="Vector"/>, <c>x</c>.</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 void Solve(Matrix <Complex> matrix, Vector <Complex> input, Vector <Complex> result, Iterator <Complex> iterator, IPreconditioner <Complex> preconditioner)
        {
            if (matrix.RowCount != matrix.ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSquare, "matrix");
            }

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

            if (input.Count != matrix.RowCount)
            {
                throw Matrix.DimensionsDontMatch <ArgumentException>(input, result);
            }

            if (iterator == null)
            {
                iterator = new Iterator <Complex>();
            }

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

            preconditioner.Initialize(matrix);

            // Compute r_0 = b - Ax_0 for some initial guess x_0
            // In this case we take x_0 = vector
            // This is basically a SAXPY so it could be made a lot faster
            var residuals = new DenseVector(matrix.RowCount);

            CalculateTrueResidual(matrix, residuals, result, input);

            // Choose r~ (for example, r~ = r_0)
            var tempResiduals = residuals.Clone();

            // create seven temporary vectors needed to hold temporary
            // coefficients. All vectors are mangled in each iteration.
            // These are defined here to prevent stressing the garbage collector
            var vecP     = new DenseVector(residuals.Count);
            var vecPdash = new DenseVector(residuals.Count);
            var nu       = new DenseVector(residuals.Count);
            var vecS     = new DenseVector(residuals.Count);
            var vecSdash = new DenseVector(residuals.Count);
            var temp     = new DenseVector(residuals.Count);
            var temp2    = new DenseVector(residuals.Count);

            // create some temporary double variables that are needed
            // to hold values in between iterations
            Complex currentRho = 0;
            Complex alpha      = 0;
            Complex omega      = 0;

            var iterationNumber = 0;

            while (iterator.DetermineStatus(iterationNumber, result, input, residuals) == IterationStatus.Continue)
            {
                // rho_(i-1) = r~^T r_(i-1) // dotproduct r~ and r_(i-1)
                var oldRho = currentRho;
                currentRho = tempResiduals.ConjugateDotProduct(residuals);

                // if (rho_(i-1) == 0) // METHOD FAILS
                // If rho is only 1 ULP from zero then we fail.
                if (currentRho.Real.AlmostEqualNumbersBetween(0, 1) && currentRho.Imaginary.AlmostEqualNumbersBetween(0, 1))
                {
                    // Rho-type breakdown
                    throw new NumericalBreakdownException();
                }

                if (iterationNumber != 0)
                {
                    // beta_(i-1) = (rho_(i-1)/rho_(i-2))(alpha_(i-1)/omega(i-1))
                    var beta = (currentRho / oldRho) * (alpha / omega);

                    // p_i = r_(i-1) + beta_(i-1)(p_(i-1) - omega_(i-1) * nu_(i-1))
                    nu.Multiply(-omega, temp);
                    vecP.Add(temp, temp2);
                    temp2.CopyTo(vecP);

                    vecP.Multiply(beta, vecP);
                    vecP.Add(residuals, temp2);
                    temp2.CopyTo(vecP);
                }
                else
                {
                    // p_i = r_(i-1)
                    residuals.CopyTo(vecP);
                }

                // SOLVE Mp~ = p_i // M = preconditioner
                preconditioner.Approximate(vecP, vecPdash);

                // nu_i = Ap~
                matrix.Multiply(vecPdash, nu);

                // alpha_i = rho_(i-1)/ (r~^T nu_i) = rho / dotproduct(r~ and nu_i)
                alpha = currentRho * 1 / tempResiduals.ConjugateDotProduct(nu);

                // s = r_(i-1) - alpha_i nu_i
                nu.Multiply(-alpha, temp);
                residuals.Add(temp, vecS);

                // Check if we're converged. If so then stop. Otherwise continue;
                // Calculate the temporary result.
                // Be careful not to change any of the temp vectors, except for
                // temp. Others will be used in the calculation later on.
                // x_i = x_(i-1) + alpha_i * p^_i + s^_i
                vecPdash.Multiply(alpha, temp);
                temp.Add(vecSdash, temp2);
                temp2.CopyTo(temp);
                temp.Add(result, temp2);
                temp2.CopyTo(temp);

                // Check convergence and stop if we are converged.
                if (iterator.DetermineStatus(iterationNumber, temp, input, vecS) != IterationStatus.Continue)
                {
                    temp.CopyTo(result);

                    // Calculate the true residual
                    CalculateTrueResidual(matrix, residuals, result, input);

                    // Now recheck the convergence
                    if (iterator.DetermineStatus(iterationNumber, result, input, residuals) != IterationStatus.Continue)
                    {
                        // We're all good now.
                        return;
                    }

                    // Continue the calculation
                    iterationNumber++;
                    continue;
                }

                // SOLVE Ms~ = s
                preconditioner.Approximate(vecS, vecSdash);

                // temp = As~
                matrix.Multiply(vecSdash, temp);

                // omega_i = temp^T s / temp^T temp
                omega = temp.ConjugateDotProduct(vecS) / temp.ConjugateDotProduct(temp);

                // x_i = x_(i-1) + alpha_i p^ + omega_i s^
                temp.Multiply(-omega, residuals);
                residuals.Add(vecS, temp2);
                temp2.CopyTo(residuals);

                vecSdash.Multiply(omega, temp);
                result.Add(temp, temp2);
                temp2.CopyTo(result);

                vecPdash.Multiply(alpha, temp);
                result.Add(temp, temp2);
                temp2.CopyTo(result);

                // for continuation it is necessary that omega_i != 0.0
                // If omega is only 1 ULP from zero then we fail.
                if (omega.Real.AlmostEqualNumbersBetween(0, 1) && omega.Imaginary.AlmostEqualNumbersBetween(0, 1))
                {
                    // Omega-type breakdown
                    throw new NumericalBreakdownException();
                }

                if (iterator.DetermineStatus(iterationNumber, result, input, residuals) != IterationStatus.Continue)
                {
                    // Recalculate the residuals and go round again. This is done to ensure that
                    // we have the proper residuals.
                    // The residual calculation based on omega_i * s can be off by a factor 10. So here
                    // we calculate the real residual (which can be expensive) but we only do it if we're
                    // sufficiently close to the finish.
                    CalculateTrueResidual(matrix, residuals, result, input);
                }

                iterationNumber++;
            }
        }
Ejemplo n.º 41
0
        private static void ReadSignalsAndNoises()
        {
            var noisesPath  = ConfigurationManager.AppSettings["NoisesFilePath"];
            var signalsPath = ConfigurationManager.AppSettings["SignalsFilePath"];
            var targetsPath = ConfigurationManager.AppSettings["TargetsFilePath"];

            FedKfSim.Noises  = new DenseMatrix(FileParser.Read4ColonFile(noisesPath));
            FedKfSim.Signals = new DenseMatrix(FileParser.Read4ColonFile(signalsPath));
            FedKfSim.Targets = new DenseMatrix(FileParser.Read3ColonFile(targetsPath));

            var    measCov = new DenseMatrix(4);
            double c00 = 0, c01 = 0, c02 = 0, c03 = 0, c11 = 0, c12 = 0, c13 = 0, c22 = 0, c23 = 0, c33 = 0;

            Vector <double> v1 = new DenseVector(1);
            Vector <double> v2 = new DenseVector(1);
            Vector <double> v3 = new DenseVector(1);
            Vector <double> v4 = new DenseVector(1);
            var             s1 = new DescriptiveStatistics(new double[1]);
            var             s2 = new DescriptiveStatistics(new double[1]);
            var             s3 = new DescriptiveStatistics(new double[1]);
            var             s4 = new DescriptiveStatistics(new double[1]);

            var t00 = Task.Run(() =>
            {
                v1  = FedKfSim.Noises.Column(0);
                s1  = new DescriptiveStatistics(v1);
                c00 = s1.Variance;
            });

            var t11 = Task.Run(() =>
            {
                v2  = FedKfSim.Noises.Column(1);
                s2  = new DescriptiveStatistics(v2);
                c11 = s2.Variance;
            });

            var t22 = Task.Run(() =>
            {
                v3  = FedKfSim.Noises.Column(2);
                s3  = new DescriptiveStatistics(v3);
                c22 = s3.Variance;
            });

            var t33 = Task.Run(() =>
            {
                v4  = FedKfSim.Noises.Column(3);
                s4  = new DescriptiveStatistics(v4);
                c33 = s4.Variance;
            });

            Task.WaitAll(new[] { t00, t11, t22, t33 });

            var t01 = Task.Run(() => c01 = CalcVariance(v1, s1.Mean, v2, s2.Mean, FedKfSim.Noises.RowCount));
            var t02 = Task.Run(() => c02 = CalcVariance(v1, s1.Mean, v3, s3.Mean, FedKfSim.Noises.RowCount));
            var t03 = Task.Run(() => c03 = CalcVariance(v1, s1.Mean, v4, s4.Mean, FedKfSim.Noises.RowCount));

            var t12 = Task.Run(() => c12 = CalcVariance(v2, s2.Mean, v3, s3.Mean, FedKfSim.Noises.RowCount));
            var t13 = Task.Run(() => c13 = CalcVariance(v2, s2.Mean, v4, s4.Mean, FedKfSim.Noises.RowCount));

            var t23 = Task.Run(() => c23 = CalcVariance(v3, s3.Mean, v4, s4.Mean, FedKfSim.Noises.RowCount));

            Task.WaitAll(new[] { t01, t02, t03, t12, t13, t23 });

            measCov[0, 0] = c00; measCov[0, 1] = c01; measCov[0, 2] = c02; measCov[0, 3] = c03;
            measCov[1, 0] = c01; measCov[1, 1] = c11; measCov[1, 2] = c12; measCov[1, 3] = c13;
            measCov[2, 0] = c02; measCov[2, 1] = c12; measCov[2, 2] = c22; measCov[2, 3] = c23;
            measCov[3, 0] = c03; measCov[3, 1] = c13; measCov[3, 2] = c23; measCov[3, 3] = c33;

            FedKfSim.SensorsOutputCovariances = measCov;
        }
        /// <summary>
        /// Run example
        /// </summary>
        public void Run()
        {
            // Format vector output to console
            var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone();

            formatProvider.TextInfo.ListSeparator = " ";

            // Create new empty vector
            var vectorA = new DenseVector(10);

            Console.WriteLine(@"Empty vector A");
            Console.WriteLine(vectorA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 1. Fill vector by data using indexer []
            for (var i = 0; i < vectorA.Count; i++)
            {
                vectorA[i] = i;
            }

            Console.WriteLine(@"1. Fill vector by data using indexer []");
            Console.WriteLine(vectorA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 2. Fill vector by data using SetValues method
            vectorA.SetValues(new[] { 9.0, 8.0, 7.0, 6.0, 5.0, 4.0, 3.0, 2.0, 1.0, 0.0 });
            Console.WriteLine(@"2. Fill vector by data using SetValues method");
            Console.WriteLine(vectorA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 3. Convert Vector to double[]
            var data = vectorA.ToArray();

            Console.WriteLine(@"3. Convert vector to double array");
            for (var i = 0; i < data.Length; i++)
            {
                Console.Write(data[i].ToString("#0.00\t", formatProvider) + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 4. Convert Vector to column matrix. A matrix based on this vector in column form (one single column)
            var columnMatrix = vectorA.ToColumnMatrix();

            Console.WriteLine(@"4. Convert vector to column matrix");
            Console.WriteLine(columnMatrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 5. Convert Vector to row matrix. A matrix based on this vector in row form (one single row)
            var rowMatrix = vectorA.ToRowMatrix();

            Console.WriteLine(@"5. Convert vector to row matrix");
            Console.WriteLine(rowMatrix.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 6. Clone vector
            var cloneA = vectorA.Clone();

            Console.WriteLine(@"6. Clone vector");
            Console.WriteLine(cloneA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 7. Clear vector
            cloneA.Clear();
            Console.WriteLine(@"7. Clear vector");
            Console.WriteLine(cloneA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 8. Copy part of vector into another vector. If you need to copy all data then use CopoTy(vector) method.
            vectorA.CopyTo(cloneA, 3, 3, 4);
            Console.WriteLine(@"8. Copy part of vector into another vector");
            Console.WriteLine(cloneA.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 9. Get part of vector as another vector
            var subvector = vectorA.SubVector(0, 5);

            Console.WriteLine(@"9. Get subvector");
            Console.WriteLine(subvector.ToString("#0.00\t", formatProvider));
            Console.WriteLine();

            // 10. Enumerator usage
            Console.WriteLine(@"10. Enumerator usage");
            foreach (var value in vectorA)
            {
                Console.Write(value.ToString("#0.00\t", formatProvider) + @" ");
            }

            Console.WriteLine();
            Console.WriteLine();

            // 11. Indexed enumerator usage
            Console.WriteLine(@"11. Enumerator usage");
            foreach (var value in vectorA.GetIndexedEnumerator())
            {
                Console.WriteLine(@"Index = {0}; Value = {1}", value.Key, value.Value.ToString("#0.00\t", formatProvider));
            }

            Console.WriteLine();
        }
Ejemplo n.º 43
0
        /// <summary>
        /// Approximates the solution to the matrix equation <b>Ax = b</b>.
        /// </summary>
        /// <param name="rhs">The right hand side vector.</param>
        /// <param name="lhs">The left hand side vector. Also known as the result vector.</param>
        public void Approximate(Vector <float> rhs, Vector <float> lhs)
        {
            if (rhs == null)
            {
                throw new ArgumentNullException("rhs");
            }

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

            if (_decompositionLU == null)
            {
                throw new ArgumentException(Resources.ArgumentMatrixDoesNotExist);
            }

            if ((lhs.Count != rhs.Count) || (lhs.Count != _decompositionLU.RowCount))
            {
                throw new ArgumentException(Resources.ArgumentVectorsSameLength);
            }

            // Solve:
            // Lz = y
            // Which gives
            // for (int i = 1; i < matrix.RowLength; i++)
            // {
            //     z_i = l_ii^-1 * (y_i - SUM_(j<i) l_ij * z_j)
            // }
            // NOTE: l_ii should be 1 because u_ii has to be the value
            Vector <float> rowValues = new DenseVector(_decompositionLU.RowCount);

            for (var i = 0; i < _decompositionLU.RowCount; i++)
            {
                // Clear the rowValues
                rowValues.Clear();
                _decompositionLU.Row(i, rowValues);

                var sum = 0.0f;
                for (var j = 0; j < i; j++)
                {
                    sum += rowValues[j] * lhs[j];
                }

                lhs[i] = rhs[i] - sum;
            }

            // Solve:
            // Ux = z
            // Which gives
            // for (int i = matrix.RowLength - 1; i > -1; i--)
            // {
            //     x_i = u_ii^-1 * (z_i - SUM_(j > i) u_ij * x_j)
            // }
            for (var i = _decompositionLU.RowCount - 1; i > -1; i--)
            {
                _decompositionLU.Row(i, rowValues);

                var sum = 0.0f;
                for (var j = _decompositionLU.RowCount - 1; j > i; j--)
                {
                    sum += rowValues[j] * lhs[j];
                }

                lhs[i] = 1 / rowValues[i] * (lhs[i] - sum);
            }
        }
Ejemplo n.º 44
0
 DataSet CloneDataSet(DataSet data)
 {
     return(new DataSet(DenseMatrix.OfMatrix(data.Input), DenseVector.OfVector(data.Output), data.DatabaseAInputLength));
 }
Ejemplo n.º 45
0
        public void SolvePoissonMatrixAndBackMultiply()
        {
            // Create the matrix
            var matrix = new SparseMatrix(100);

            // Assemble the matrix. We assume we're solving the Poisson equation
            // on a rectangular 10 x 10 grid
            const int GridSize = 10;

            // The pattern is:
            // 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0
            for (var i = 0; i < matrix.RowCount; i++)
            {
                // Insert the first set of -1's
                if (i > (GridSize - 1))
                {
                    matrix[i, i - GridSize] = -1;
                }

                // Insert the second set of -1's
                if (i > 0)
                {
                    matrix[i, i - 1] = -1;
                }

                // Insert the centerline values
                matrix[i, i] = 4;

                // Insert the first trailing set of -1's
                if (i < matrix.RowCount - 1)
                {
                    matrix[i, i + 1] = -1;
                }

                // Insert the second trailing set of -1's
                if (i < matrix.RowCount - GridSize)
                {
                    matrix[i, i + GridSize] = -1;
                }
            }

            // Create the y vector
            var y = DenseVector.Create(matrix.RowCount, Complex.One);

            // Create an iteration monitor which will keep track of iterative convergence
            var monitor = new Iterator <Complex>(new IterationCountStopCriterion <Complex>(MaximumIterations),
                                                 new ResidualStopCriterion <Complex>(ConvergenceBoundary),
                                                 new DivergenceStopCriterion <Complex>(),
                                                 new FailureStopCriterion <Complex>());

            var solver = new BiCgStab();

            // Solve equation Ax = y
            var x = matrix.SolveIterative(y, solver, monitor);

            // Now compare the results
            Assert.IsNotNull(x, "#02");
            Assert.AreEqual(y.Count, x.Count, "#03");

            // Back multiply the vector
            var z = matrix.Multiply(x);

            // Check that the solution converged
            Assert.IsTrue(monitor.Status == IterationStatus.Converged, "#04");

            // Now compare the vectors
            for (var i = 0; i < y.Count; i++)
            {
                Assert.GreaterOrEqual(ConvergenceBoundary, (y[i] - z[i]).Magnitude, "#05-" + i);
            }
        }
Ejemplo n.º 46
0
 protected abstract DenseVector ComputeDelta(double time, DenseVector val);
Ejemplo n.º 47
0
        public static DenseVector Conv(this DenseVector dvSource, StandardConvolutionKernels kernelType, int kernelHalfWidth = 10)
        {
            DenseVector dvKernel = ConvKernel(kernelType, kernelHalfWidth);

            return(dvSource.Conv(dvKernel));
        }
        private void Enter_Meds_Regression_Click(object sender, EventArgs e)
        {
            checkexist(Initial_MedBox.Text);
            if (existence != true)
            {
                MessageBox.Show("The Initial Medicine Doesn't Exist in The Database");
            }
            else
            {
                initialfill(Initial_MedBox.Text, "Quality_of_Sleep");
                checkexist(Secondary_MedBox.Text);
                if (existence != true)
                {
                    MessageBox.Show("The Secondary Medicine Doesn't Exist in The Database");
                }
                else
                {
                    if (!(string.IsNullOrWhiteSpace(Tertiary_MedBox.Text)))
                    {
                        checkexist(Tertiary_MedBox.Text);
                        if (existence != true)
                        {
                            MessageBox.Show("The Tertiary Medicine Doesn't Exist in The Database");
                        }
                        else
                        {
                            testregrescon.Open();
                            checkcorrespondence(3);
                        }
                    }
                    else
                    {
                        testregrescon.Open();
                        checkcorrespondence(2);
                    }
                    //we go here

                    double[] firstdose    = InitialDose.ToArray();
                    double[] seconddose   = SecondDose.ToArray();
                    double[] attributes   = Attribute.ToArray();
                    double[] optionaldose = OptionalDose.ToArray();
                    MessageBox.Show(firstdose.Length.ToString());
                    MessageBox.Show(seconddose.Length.ToString());
                    MessageBox.Show(optionaldose.Length.ToString());
                    if (optionaldose.Length == 0)
                    {
                        double[,] doub = new double[firstdose.Length, 3];
                        for (int i = 0; i < firstdose.Length; i++)
                        {
                            doub[i, 0] = 1.0;
                            if (i == seconddose.Length - 1 && seconddose[i] == seconddose[i - 1])
                            {
                                doub[i, 1] = firstdose[i] + 0.1;
                            }
                            else
                            {
                                doub[i, 1] = firstdose[i];
                            }
                        }
                        for (int g = 0; g < seconddose.Length; g++)
                        {
                            if (g == seconddose.Length - 1 && seconddose[g] == seconddose[g - 1])
                            {
                                doub[g, 2] = seconddose[g] + 0.1;
                            }
                            else
                            {
                                doub[g, 2] = seconddose[g];
                            }
                        }
                        var A = DenseMatrix.OfArray(doub);

                        var b  = new DenseVector(attributes);
                        var h  = A.QR().Solve(b);
                        var q  = h[0];
                        var q2 = h[1];
                        var q3 = h[2];
                        //    PUT GRAPH STUFF HERE
                    }
                    else
                    {
                        double[,] doub = new double[firstdose.Length, 4];
                        for (int i = 0; i < firstdose.Length; i++)
                        {
                            doub[i, 0] = 1.0;
                            if (i == seconddose.Length - 1 && seconddose[i] == seconddose[i - 1])
                            {
                                doub[i, 1] = firstdose[i] + 0.1;
                            }
                            else
                            {
                                doub[i, 1] = firstdose[i];
                            }
                        }
                        for (int g = 0; g < seconddose.Length; g++)
                        {
                            if (g == seconddose.Length - 1 && seconddose[g] == seconddose[g - 1])
                            {
                                doub[g, 2] = seconddose[g] + 0.1;
                            }
                            else
                            {
                                doub[g, 2] = seconddose[g];
                            }
                        }
                        for (int yo = 0; yo < optionaldose.Length; yo++)
                        {
                            if (yo == seconddose.Length - 1 && seconddose[yo] == seconddose[yo - 1])
                            {
                                doub[yo, 3] = seconddose[yo] + 0.1;
                            }
                            else
                            {
                                doub[yo, 3] = seconddose[yo];
                            }
                        }
                        var A = DenseMatrix.OfArray(doub);

                        var b  = new DenseVector(attributes);
                        var h  = A.QR().Solve(b);
                        var q  = h[0];
                        var q2 = h[1];
                        var q3 = h[2];
                        var q4 = h[4];
                        //    PUT GRAPH STUFF HERE
                    }
                }
            }
        }
Ejemplo n.º 49
0
        /// <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>
        /// <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 void Solve(Matrix <Complex> matrix, Vector <Complex> input, Vector <Complex> result, Iterator <Complex> iterator, IPreconditioner <Complex> preconditioner)
        {
            if (matrix.RowCount != matrix.ColumnCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixSquare, nameof(matrix));
            }

            if (input.Count != matrix.RowCount || result.Count != input.Count)
            {
                throw Matrix.DimensionsDontMatch <ArgumentException>(matrix, input, result);
            }

            if (iterator == null)
            {
                iterator = new Iterator <Complex>();
            }

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

            preconditioner.Initialize(matrix);

            // Choose an initial guess x_0
            // Take x_0 = 0
            var xtemp = new DenseVector(input.Count);

            // Choose k vectors q_1, q_2, ..., q_k
            // Build a new set if:
            // a) the stored set doesn't exist (i.e. == null)
            // b) Is of an incorrect length (i.e. too long)
            // c) The vectors are of an incorrect length (i.e. too long or too short)
            var useOld = false;

            if (_startingVectors != null)
            {
                // We don't accept collections with zero starting vectors so ...
                if (_startingVectors.Count <= NumberOfStartingVectorsToCreate(_numberOfStartingVectors, input.Count))
                {
                    // Only check the first vector for sizing. If that matches we assume the
                    // other vectors match too. If they don't the process will crash
                    if (_startingVectors[0].Count == input.Count)
                    {
                        useOld = true;
                    }
                }
            }

            _startingVectors = useOld ? _startingVectors : CreateStartingVectors(_numberOfStartingVectors, input.Count);

            // Store the number of starting vectors. Not really necessary but easier to type :)
            var k = _startingVectors.Count;

            // r_0 = b - Ax_0
            // This is basically a SAXPY so it could be made a lot faster
            var residuals = new DenseVector(matrix.RowCount);

            CalculateTrueResidual(matrix, residuals, xtemp, input);

            // Define the temporary values
            var c = new Complex[k];

            // Define the temporary vectors
            var gtemp = new DenseVector(residuals.Count);

            var u     = new DenseVector(residuals.Count);
            var utemp = new DenseVector(residuals.Count);
            var temp  = new DenseVector(residuals.Count);
            var temp1 = new DenseVector(residuals.Count);
            var temp2 = new DenseVector(residuals.Count);

            var zd = new DenseVector(residuals.Count);
            var zg = new DenseVector(residuals.Count);
            var zw = new DenseVector(residuals.Count);

            var d = CreateVectorArray(_startingVectors.Count, residuals.Count);

            // g_0 = r_0
            var g = CreateVectorArray(_startingVectors.Count, residuals.Count);

            residuals.CopyTo(g[k - 1]);

            var w = CreateVectorArray(_startingVectors.Count, residuals.Count);

            // FOR (j = 0, 1, 2 ....)
            var iterationNumber = 0;

            while (iterator.DetermineStatus(iterationNumber, xtemp, input, residuals) == IterationStatus.Continue)
            {
                // SOLVE M g~_((j-1)k+k) = g_((j-1)k+k)
                preconditioner.Approximate(g[k - 1], gtemp);

                // w_((j-1)k+k) = A g~_((j-1)k+k)
                matrix.Multiply(gtemp, w[k - 1]);

                // c_((j-1)k+k) = q^T_1 w_((j-1)k+k)
                c[k - 1] = _startingVectors[0].ConjugateDotProduct(w[k - 1]);
                if (c[k - 1].Real.AlmostEqualNumbersBetween(0, 1) && c[k - 1].Imaginary.AlmostEqualNumbersBetween(0, 1))
                {
                    throw new NumericalBreakdownException();
                }

                // alpha_(jk+1) = q^T_1 r_((j-1)k+k) / c_((j-1)k+k)
                var alpha = _startingVectors[0].ConjugateDotProduct(residuals) / c[k - 1];

                // u_(jk+1) = r_((j-1)k+k) - alpha_(jk+1) w_((j-1)k+k)
                w[k - 1].Multiply(-alpha, temp);
                residuals.Add(temp, u);

                // SOLVE M u~_(jk+1) = u_(jk+1)
                preconditioner.Approximate(u, temp1);
                temp1.CopyTo(utemp);

                // rho_(j+1) = -u^t_(jk+1) A u~_(jk+1) / ||A u~_(jk+1)||^2
                matrix.Multiply(temp1, temp);
                var rho = temp.ConjugateDotProduct(temp);

                // If rho is zero then temp is a zero vector and we're probably
                // about to have zero residuals (i.e. an exact solution).
                // So set rho to 1.0 because in the next step it will turn to zero.
                if (rho.Real.AlmostEqualNumbersBetween(0, 1) && rho.Imaginary.AlmostEqualNumbersBetween(0, 1))
                {
                    rho = 1.0;
                }

                rho = -u.ConjugateDotProduct(temp) / rho;

                // r_(jk+1) = rho_(j+1) A u~_(jk+1) + u_(jk+1)
                u.CopyTo(residuals);

                // Reuse temp
                temp.Multiply(rho, temp);
                residuals.Add(temp, temp2);
                temp2.CopyTo(residuals);

                // x_(jk+1) = x_((j-1)k_k) - rho_(j+1) u~_(jk+1) + alpha_(jk+1) g~_((j-1)k+k)
                utemp.Multiply(-rho, temp);
                xtemp.Add(temp, temp2);
                temp2.CopyTo(xtemp);

                gtemp.Multiply(alpha, gtemp);
                xtemp.Add(gtemp, temp2);
                temp2.CopyTo(xtemp);

                // Check convergence and stop if we are converged.
                if (iterator.DetermineStatus(iterationNumber, xtemp, input, residuals) != IterationStatus.Continue)
                {
                    // Calculate the true residual
                    CalculateTrueResidual(matrix, residuals, xtemp, input);

                    // Now recheck the convergence
                    if (iterator.DetermineStatus(iterationNumber, xtemp, input, residuals) != IterationStatus.Continue)
                    {
                        // We're all good now.
                        // Exit from the while loop.
                        break;
                    }
                }

                // FOR (i = 1,2, ...., k)
                for (var i = 0; i < k; i++)
                {
                    // z_d = u_(jk+1)
                    u.CopyTo(zd);

                    // z_g = r_(jk+i)
                    residuals.CopyTo(zg);

                    // z_w = 0
                    zw.Clear();

                    // FOR (s = i, ...., k-1) AND j >= 1
                    Complex beta;
                    if (iterationNumber >= 1)
                    {
                        for (var s = i; s < k - 1; s++)
                        {
                            // beta^(jk+i)_((j-1)k+s) = -q^t_(s+1) z_d / c_((j-1)k+s)
                            beta = -_startingVectors[s + 1].ConjugateDotProduct(zd) / c[s];

                            // z_d = z_d + beta^(jk+i)_((j-1)k+s) d_((j-1)k+s)
                            d[s].Multiply(beta, temp);
                            zd.Add(temp, temp2);
                            temp2.CopyTo(zd);

                            // z_g = z_g + beta^(jk+i)_((j-1)k+s) g_((j-1)k+s)
                            g[s].Multiply(beta, temp);
                            zg.Add(temp, temp2);
                            temp2.CopyTo(zg);

                            // z_w = z_w + beta^(jk+i)_((j-1)k+s) w_((j-1)k+s)
                            w[s].Multiply(beta, temp);
                            zw.Add(temp, temp2);
                            temp2.CopyTo(zw);
                        }
                    }

                    beta = rho * c[k - 1];
                    if (beta.Real.AlmostEqualNumbersBetween(0, 1) && beta.Imaginary.AlmostEqualNumbersBetween(0, 1))
                    {
                        throw new NumericalBreakdownException();
                    }

                    // beta^(jk+i)_((j-1)k+k) = -(q^T_1 (r_(jk+1) + rho_(j+1) z_w)) / (rho_(j+1) c_((j-1)k+k))
                    zw.Multiply(rho, temp2);
                    residuals.Add(temp2, temp);
                    beta = -_startingVectors[0].ConjugateDotProduct(temp) / beta;

                    // z_g = z_g + beta^(jk+i)_((j-1)k+k) g_((j-1)k+k)
                    g[k - 1].Multiply(beta, temp);
                    zg.Add(temp, temp2);
                    temp2.CopyTo(zg);

                    // z_w = rho_(j+1) (z_w + beta^(jk+i)_((j-1)k+k) w_((j-1)k+k))
                    w[k - 1].Multiply(beta, temp);
                    zw.Add(temp, temp2);
                    temp2.CopyTo(zw);
                    zw.Multiply(rho, zw);

                    // z_d = r_(jk+i) + z_w
                    residuals.Add(zw, zd);

                    // FOR (s = 1, ... i - 1)
                    for (var s = 0; s < i - 1; s++)
                    {
                        // beta^(jk+i)_(jk+s) = -q^T_s+1 z_d / c_(jk+s)
                        beta = -_startingVectors[s + 1].ConjugateDotProduct(zd) / c[s];

                        // z_d = z_d + beta^(jk+i)_(jk+s) * d_(jk+s)
                        d[s].Multiply(beta, temp);
                        zd.Add(temp, temp2);
                        temp2.CopyTo(zd);

                        // z_g = z_g + beta^(jk+i)_(jk+s) * g_(jk+s)
                        g[s].Multiply(beta, temp);
                        zg.Add(temp, temp2);
                        temp2.CopyTo(zg);
                    }

                    // d_(jk+i) = z_d - u_(jk+i)
                    zd.Subtract(u, d[i]);

                    // g_(jk+i) = z_g + z_w
                    zg.Add(zw, g[i]);

                    // IF (i < k - 1)
                    if (i < k - 1)
                    {
                        // c_(jk+1) = q^T_i+1 d_(jk+i)
                        c[i] = _startingVectors[i + 1].ConjugateDotProduct(d[i]);
                        if (c[i].Real.AlmostEqualNumbersBetween(0, 1) && c[i].Imaginary.AlmostEqualNumbersBetween(0, 1))
                        {
                            throw new NumericalBreakdownException();
                        }

                        // alpha_(jk+i+1) = q^T_(i+1) u_(jk+i) / c_(jk+i)
                        alpha = _startingVectors[i + 1].ConjugateDotProduct(u) / c[i];

                        // u_(jk+i+1) = u_(jk+i) - alpha_(jk+i+1) d_(jk+i)
                        d[i].Multiply(-alpha, temp);
                        u.Add(temp, temp2);
                        temp2.CopyTo(u);

                        // SOLVE M g~_(jk+i) = g_(jk+i)
                        preconditioner.Approximate(g[i], gtemp);

                        // x_(jk+i+1) = x_(jk+i) + rho_(j+1) alpha_(jk+i+1) g~_(jk+i)
                        gtemp.Multiply(rho * alpha, temp);
                        xtemp.Add(temp, temp2);
                        temp2.CopyTo(xtemp);

                        // w_(jk+i) = A g~_(jk+i)
                        matrix.Multiply(gtemp, w[i]);

                        // r_(jk+i+1) = r_(jk+i) - rho_(j+1) alpha_(jk+i+1) w_(jk+i)
                        w[i].Multiply(-rho * alpha, temp);
                        residuals.Add(temp, temp2);
                        temp2.CopyTo(residuals);

                        // We can check the residuals here if they're close
                        if (iterator.DetermineStatus(iterationNumber, xtemp, input, residuals) != IterationStatus.Continue)
                        {
                            // Recalculate the residuals and go round again. This is done to ensure that
                            // we have the proper residuals.
                            CalculateTrueResidual(matrix, residuals, xtemp, input);
                        }
                    }
                } // END ITERATION OVER i

                iterationNumber++;
            }

            // copy the temporary result to the real result vector
            xtemp.CopyTo(result);
        }
Ejemplo n.º 50
0
        /// <summary>Computations that depend on the observed value of vVector__179</summary>
        private void Changed_vVector__179()
        {
            if (this.Changed_vVector__179_iterationsDone == 1)
            {
                return;
            }
            this.vVector__179_marginal = new PointMass <Vector[]>(this.VVector__179);
            // The constant 'vVectorGaussian179'
            VectorGaussian vVectorGaussian179 = VectorGaussian.FromNatural(DenseVector.FromArray(new double[3] {
                1547829870.0, 525077980.0, 200270.0
            }), new PositiveDefiniteMatrix(new double[3, 3] {
                { 4254590363351.0, 1127383488860.0, 433199230.0 }, { 1127383488860.0, 482723521821.0, 146764360.0 }, { 433199230.0, 146764360.0, 56221.0 }
            }));

            this.vVector537_marginal_F = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian179);
            // Buffer for ReplicateOp_Divide.Marginal<VectorGaussian>
            VectorGaussian vVector537_rep_B_toDef = default(VectorGaussian);

            // Message to 'vVector537_rep' from Replicate factor
            vVector537_rep_B_toDef = ReplicateOp_Divide.ToDefInit <VectorGaussian>(vVectorGaussian179);
            // Message to 'vVector537_marginal' from Variable factor
            this.vVector537_marginal_F = VariableOp.MarginalAverageConditional <VectorGaussian>(vVector537_rep_B_toDef, vVectorGaussian179, this.vVector537_marginal_F);
            DistributionStructArray <Gaussian, double> vdouble__537_F = default(DistributionStructArray <Gaussian, double>);

            // Create array for 'vdouble__537' Forwards messages.
            vdouble__537_F = new DistributionStructArray <Gaussian, double>(1);
            for (int index179 = 0; index179 < 1; index179++)
            {
                vdouble__537_F[index179] = Gaussian.Uniform();
            }
            DistributionStructArray <Gaussian, double> vdouble__538_F = default(DistributionStructArray <Gaussian, double>);

            // Create array for 'vdouble__538' Forwards messages.
            vdouble__538_F = new DistributionStructArray <Gaussian, double>(1);
            for (int index179 = 0; index179 < 1; index179++)
            {
                vdouble__538_F[index179] = Gaussian.Uniform();
            }
            DistributionRefArray <VectorGaussian, Vector> vVector537_rep_F = default(DistributionRefArray <VectorGaussian, Vector>);
            DistributionRefArray <VectorGaussian, Vector> vVector537_rep_B = default(DistributionRefArray <VectorGaussian, Vector>);

            // Create array for 'vVector537_rep' Forwards messages.
            vVector537_rep_F = new DistributionRefArray <VectorGaussian, Vector>(1);
            // Create array for 'vVector537_rep' Backwards messages.
            vVector537_rep_B = new DistributionRefArray <VectorGaussian, Vector>(1);
            for (int index179 = 0; index179 < 1; index179++)
            {
                vVector537_rep_B[index179] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian179);
                vVector537_rep_F[index179] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian179);
            }
            // Buffer for ReplicateOp_Divide.UsesAverageConditional<VectorGaussian>
            VectorGaussian vVector537_rep_F_marginal = default(VectorGaussian);

            // Message to 'vVector537_rep' from Replicate factor
            vVector537_rep_F_marginal = ReplicateOp_Divide.MarginalInit <VectorGaussian>(vVectorGaussian179);
            // Message to 'vVector537_rep' from Replicate factor
            vVector537_rep_F_marginal = ReplicateOp_Divide.Marginal <VectorGaussian>(vVector537_rep_B_toDef, vVectorGaussian179, vVector537_rep_F_marginal);
            // Buffer for InnerProductOp.InnerProductAverageConditional
            // Create array for replicates of 'vVector537_rep_F_index179__AMean'
            Vector[] vVector537_rep_F_index179__AMean = new Vector[1];
            for (int index179 = 0; index179 < 1; index179++)
            {
                // Message to 'vdouble__538' from InnerProduct factor
                vVector537_rep_F_index179__AMean[index179] = InnerProductOp.AMeanInit(vVector537_rep_F[index179]);
            }
            // Buffer for InnerProductOp.AMean
            // Create array for replicates of 'vVector537_rep_F_index179__AVariance'
            PositiveDefiniteMatrix[] vVector537_rep_F_index179__AVariance = new PositiveDefiniteMatrix[1];
            for (int index179 = 0; index179 < 1; index179++)
            {
                // Message to 'vdouble__538' from InnerProduct factor
                vVector537_rep_F_index179__AVariance[index179] = InnerProductOp.AVarianceInit(vVector537_rep_F[index179]);
                // Message to 'vVector537_rep' from Replicate factor
                vVector537_rep_F[index179] = ReplicateOp_Divide.UsesAverageConditional <VectorGaussian>(vVector537_rep_B[index179], vVector537_rep_F_marginal, index179, vVector537_rep_F[index179]);
            }
            // Create array for 'vdouble__538_marginal' Forwards messages.
            this.vdouble__538_marginal_F = new DistributionStructArray <Gaussian, double>(1);
            for (int index179 = 0; index179 < 1; index179++)
            {
                this.vdouble__538_marginal_F[index179] = Gaussian.Uniform();
            }
            // Message from use of 'vdouble__538'
            DistributionStructArray <Gaussian, double> vdouble__538_use_B = default(DistributionStructArray <Gaussian, double>);

            // Create array for 'vdouble__538_use' Backwards messages.
            vdouble__538_use_B = new DistributionStructArray <Gaussian, double>(1);
            for (int index179 = 0; index179 < 1; index179++)
            {
                vdouble__538_use_B[index179] = Gaussian.Uniform();
                // Message to 'vdouble__538' from InnerProduct factor
                vVector537_rep_F_index179__AVariance[index179] = InnerProductOp.AVariance(vVector537_rep_F[index179], vVector537_rep_F_index179__AVariance[index179]);
                // Message to 'vdouble__538' from InnerProduct factor
                vVector537_rep_F_index179__AMean[index179] = InnerProductOp.AMean(vVector537_rep_F[index179], vVector537_rep_F_index179__AVariance[index179], vVector537_rep_F_index179__AMean[index179]);
                // Message to 'vdouble__538' from InnerProduct factor
                vdouble__538_F[index179] = InnerProductOp.InnerProductAverageConditional(vVector537_rep_F_index179__AMean[index179], vVector537_rep_F_index179__AVariance[index179], this.VVector__179[index179]);
                // Message to 'vdouble__538_marginal' from DerivedVariable factor
                this.vdouble__538_marginal_F[index179] = DerivedVariableOp.MarginalAverageConditional <Gaussian>(vdouble__538_use_B[index179], vdouble__538_F[index179], this.vdouble__538_marginal_F[index179]);
            }
            // Create array for 'vdouble__537_marginal' Forwards messages.
            this.vdouble__537_marginal_F = new DistributionStructArray <Gaussian, double>(1);
            for (int index179 = 0; index179 < 1; index179++)
            {
                this.vdouble__537_marginal_F[index179] = Gaussian.Uniform();
            }
            // Message from use of 'vdouble__537'
            DistributionStructArray <Gaussian, double> vdouble__537_use_B = default(DistributionStructArray <Gaussian, double>);

            // Create array for 'vdouble__537_use' Backwards messages.
            vdouble__537_use_B = new DistributionStructArray <Gaussian, double>(1);
            for (int index179 = 0; index179 < 1; index179++)
            {
                vdouble__537_use_B[index179] = Gaussian.Uniform();
                // Message to 'vdouble__537' from GaussianFromMeanAndVariance factor
                vdouble__537_F[index179] = GaussianFromMeanAndVarianceOp.SampleAverageConditional(vdouble__538_F[index179], 0.1);
                // Message to 'vdouble__537_marginal' from Variable factor
                this.vdouble__537_marginal_F[index179] = VariableOp.MarginalAverageConditional <Gaussian>(vdouble__537_use_B[index179], vdouble__537_F[index179], this.vdouble__537_marginal_F[index179]);
            }
            this.Changed_vVector__179_iterationsDone = 1;
        }
        private void RepresentAnalytic()
        {
            #region Прописываем текстовые маркеры

            #region Y

            //MCvFont theFont = new MCvFont(FONT.CV_FONT_HERSHEY_PLAIN, 1.0d, 1.0d);
            //theFont.thickness = 1;

            // замерим высоту подписей по X по значению в минимуме

            string       strMinXvalueMarker     = (xAxisValuesConversionToRepresentTicksValues == null)?(xSpaceMin.ToString()) :(xAxisValuesConversionToRepresentTicksValues(xSpaceMin));
            TextBarImage minXvalueMarkerTextBar = new TextBarImage(strMinXvalueMarker,
                                                                   new Image <Bgr, byte>(new Size(pictureWidth, pictureHeight)));
            int barHeight = minXvalueMarkerTextBar.textBarSize.Height;
            if (btmServiceSpaceGapY < 1.5 * barHeight)
            {
                btmServiceSpaceGapY = Convert.ToInt32(1.5 * barHeight);
            }


            double dMarkersCount = (double)(pictureHeight - (btmServiceSpaceGapY + topServiceSpaceGapY)) / 30.0d;
            dMarkersCount = (dMarkersCount > 10.0d) ? (10.0d) : (dMarkersCount);
            double dRulerValueGap = (overallFuncMax - overallFuncMin) / (double)dMarkersCount;
            //dRulerValueGap = (dRulerValueGap < 1.0d) ? (1.0d) : dRulerValueGap;
            int    deciGap          = Convert.ToInt32(Math.Truncate(Math.Log(dRulerValueGap, 2.0d)));
            double rulerValueGap    = Math.Pow(2.0, (double)deciGap);
            double lowerMarkerValue = overallFuncMin - overallFuncMin % rulerValueGap;// Math.IEEERemainder(overallFuncMin, rulerValueGap);
            lowerMarkerValue = (lowerMarkerValue < overallFuncMin) ? (lowerMarkerValue + rulerValueGap) : (lowerMarkerValue);

            List <double> yMarkersValues = new List <double>();
            yMarkersValues.Add(lowerMarkerValue);
            double currentMarkerValue = lowerMarkerValue;
            while (currentMarkerValue <= overallFuncMax)
            {
                currentMarkerValue += rulerValueGap;
                yMarkersValues.Add(currentMarkerValue);
            }

            List <TextBarImage> lTextBars = new List <TextBarImage>();
            foreach (double markerValue in yMarkersValues)
            {
                string currMarkerPresentation = markerValue.ToString();
                if (yAxisValuesConversionToRepresentTicksValues != null)
                {
                    currMarkerPresentation = yAxisValuesConversionToRepresentTicksValues(markerValue);
                }

                TextBarImage currTextBar = new TextBarImage(currMarkerPresentation,
                                                            new Image <Bgr, byte>(new Size(pictureWidth, pictureHeight)));
                lTextBars.Add(currTextBar);
            }
            int maxYlabelWidth = lTextBars.Max(textBar => textBar.textBarSize.Width);
            if (leftServiceSpaceGapX < maxYlabelWidth)
            {
                leftServiceSpaceGapX = maxYlabelWidth;
            }



            currentMarkerValue = lowerMarkerValue;
            double nextYPositionDouble = (1.0d - ((currentMarkerValue - overallFuncMin) / (overallFuncMax - overallFuncMin))) * (double)(pictureHeight - topServiceSpaceGapY - btmServiceSpaceGapY) + topServiceSpaceGapY;
            while (nextYPositionDouble > topServiceSpaceGapY)
            {
                double        yPositionDouble = (1.0d - ((currentMarkerValue - overallFuncMin) / (overallFuncMax - overallFuncMin))) * (double)(pictureHeight - topServiceSpaceGapY - btmServiceSpaceGapY) + topServiceSpaceGapY;
                int           yPosition       = Convert.ToInt32(Math.Round(yPositionDouble));
                LineSegment2D theLine         = new LineSegment2D(new Point(leftServiceSpaceGapX, yPosition), new Point(leftServiceSpaceGapX - 5, yPosition));
                Bgr           markerColor     = colorGreen;
                theImage.Draw(theLine, markerColor, 2);

                string currMarkerPresentation = currentMarkerValue.ToString();
                if (yAxisValuesConversionToRepresentTicksValues != null)
                {
                    currMarkerPresentation = yAxisValuesConversionToRepresentTicksValues(currentMarkerValue);
                }

                //theImage.Draw(currMarkerPresentation, ref theFont, new Point(2, yPosition), markerColor);

                TextBarImage currSignImage = new TextBarImage(currMarkerPresentation, theImage);
                currSignImage.PtSurroundingBarStart = new Point(0, yPosition - currSignImage.textHeight);
                theImage = theImage.Add(currSignImage.TextSignImageAtOriginalBlank(markerColor));


                currentMarkerValue += rulerValueGap;
                nextYPositionDouble = (1.0d - ((currentMarkerValue - overallFuncMin) / (overallFuncMax - overallFuncMin))) * (double)(pictureHeight - topServiceSpaceGapY - btmServiceSpaceGapY) + topServiceSpaceGapY;
            }

            #endregion Y



            #region X

            double rulerValueGapX    = 0.0d;
            double lowerMarkerValueX = 0.0d;
            bool   markersCountRight = false;
            int    initialDivider    = 30;
            double dMarkersCountX    = (double)(pictureWidth - (leftServiceSpaceGapX + rightServiceSpaceGapX)) / (double)initialDivider;
            while (!markersCountRight)
            {
                dMarkersCountX = (dMarkersCountX > 10.0d) ? (10.0d) : (dMarkersCountX);
                double dRulerValueGapX = (xSpaceMax - xSpaceMin) / (double)dMarkersCountX;
                //int deciGapX = Convert.ToInt32(Math.Truncate(Math.Log(dRulerValueGapX, 2.0d)));
                //rulerValueGapX = Math.Pow(2.0, (double)deciGapX);
                rulerValueGapX    = dRulerValueGapX;
                lowerMarkerValueX = xSpaceMin - (xSpaceMin % rulerValueGapX); // Math.IEEERemainder(xSpaceMin, rulerValueGapX);
                lowerMarkerValueX = (lowerMarkerValueX < xSpaceMin) ? (lowerMarkerValueX + rulerValueGapX) : (lowerMarkerValueX);

                double        firstMarkerValueX = lowerMarkerValueX;
                List <double> xMarkersValues    = new List <double>();
                xMarkersValues.Add(firstMarkerValueX);
                currentMarkerValue = firstMarkerValueX;
                while (currentMarkerValue <= xSpaceMax)
                {
                    currentMarkerValue += rulerValueGapX;
                    xMarkersValues.Add(currentMarkerValue);
                }

                List <TextBarImage> lTextBarsXaxis = new List <TextBarImage>();
                foreach (double markerValue in xMarkersValues)
                {
                    string currMarkerPresentation = markerValue.ToString();
                    if (xAxisValuesConversionToRepresentTicksValues != null)
                    {
                        currMarkerPresentation = xAxisValuesConversionToRepresentTicksValues(markerValue);
                    }

                    TextBarImage currTextBar = new TextBarImage(currMarkerPresentation,
                                                                new Image <Bgr, byte>(new Size(pictureWidth, pictureHeight)));
                    lTextBarsXaxis.Add(currTextBar);
                }

                int totalTextBarsWidth = lTextBarsXaxis.Sum(textBar => textBar.textBarSize.Width);
                if (totalTextBarsWidth > pictureWidth - leftServiceSpaceGapX - rightServiceSpaceGapX)
                {
                    dMarkersCountX = dMarkersCountX - 1.0d;
                    if (dMarkersCountX <= 2)
                    {
                        dMarkersCountX    = 2.0d;
                        markersCountRight = true;
                    }
                }
                else
                {
                    markersCountRight = true;
                }
            }


            double currentMarkerValueX = lowerMarkerValueX;

            double nextXPositionDouble = leftServiceSpaceGapX + ((currentMarkerValueX - xSpaceMin) / (xSpaceMax - xSpaceMin)) * (double)(pictureWidth - leftServiceSpaceGapX - rightServiceSpaceGapX);
            while (nextXPositionDouble <= pictureWidth - rightServiceSpaceGapX)
            {
                double        xPositionDouble = leftServiceSpaceGapX + ((currentMarkerValueX - xSpaceMin) / (xSpaceMax - xSpaceMin)) * (double)(pictureWidth - leftServiceSpaceGapX - rightServiceSpaceGapX);
                int           xPosition       = Convert.ToInt32(Math.Round(xPositionDouble));
                LineSegment2D theLine         = new LineSegment2D(new Point(xPosition, pictureHeight - btmServiceSpaceGapY), new Point(xPosition, pictureHeight - btmServiceSpaceGapY + 5));
                Bgr           markerColor     = colorGreen;
                theImage.Draw(theLine, markerColor, 2);

                string currMarkerPresentation = currentMarkerValueX.ToString();
                if (xAxisValuesConversionToRepresentTicksValues != null)
                {
                    currMarkerPresentation = xAxisValuesConversionToRepresentTicksValues(currentMarkerValueX);
                }

                TextBarImage currSignImage = new TextBarImage(currMarkerPresentation, theImage);
                currSignImage.PtSurroundingBarStart = new Point(Convert.ToInt32(xPosition - currSignImage.textBarSize.Width / 2), pictureHeight - btmServiceSpaceGapY + 10);
                theImage = theImage.Add(currSignImage.TextSignImageAtOriginalBlank(markerColor));

                currentMarkerValueX += rulerValueGapX;
                nextXPositionDouble  = leftServiceSpaceGapX + ((currentMarkerValueX - xSpaceMin) / (xSpaceMax - xSpaceMin)) * (double)(pictureWidth - leftServiceSpaceGapX - rightServiceSpaceGapX);
            }

            #endregion X
            #endregion Прописываем текстовые маркеры



            int xValuesCount = pictureWidth - leftServiceSpaceGapX - rightServiceSpaceGapX;// оставляем место на шкалу Y

            List <Point> rulerVertices = new List <Point>();
            rulerVertices.Add(new Point(leftServiceSpaceGapX, pictureHeight - btmServiceSpaceGapY));
            rulerVertices.Add(new Point(pictureWidth - rightServiceSpaceGapX, pictureHeight - btmServiceSpaceGapY));
            rulerVertices.Add(new Point(pictureWidth - rightServiceSpaceGapX, topServiceSpaceGapY));
            rulerVertices.Add(new Point(leftServiceSpaceGapX, topServiceSpaceGapY));
            theImage.DrawPolyline(rulerVertices.ToArray(), true, colorGreen, 2);

            double koeff = (pictureHeight - btmServiceSpaceGapY - topServiceSpaceGapY) / (overallFuncMax - overallFuncMin);
            koeffY = ((double)pictureHeight - btmServiceSpaceGapY - topServiceSpaceGapY) / (overallFuncMax - overallFuncMin);
            koeffX = ((double)pictureWidth - leftServiceSpaceGapX - rightServiceSpaceGapX) / (xSpaceMax - xSpaceMin);
            if (equalScale)
            {
                koeff  = Math.Min(koeffY, koeffX);
                koeffY = koeff;
                koeffX = koeff;
            }


            if (drawZeroLines)
            {
                int zeroYcoordinate = Convert.ToInt32(pictureHeight - btmServiceSpaceGapY - (0 - overallFuncMin) * koeffY);
                int zeroXcoordinate = Convert.ToInt32(leftServiceSpaceGapX + (0 - xSpaceMin) * koeffX);

                List <Point> rulerXVertices = new List <Point>();
                rulerXVertices.Add(new Point(leftServiceSpaceGapX, zeroYcoordinate));
                rulerXVertices.Add(new Point(pictureWidth - rightServiceSpaceGapX, zeroYcoordinate));
                theImage.DrawPolyline(rulerXVertices.ToArray(), false, colorGreen, 2);

                List <Point> rulerYVertices = new List <Point>();
                rulerYVertices.Add(new Point(zeroXcoordinate, topServiceSpaceGapY));
                rulerYVertices.Add(new Point(zeroXcoordinate, pictureHeight - btmServiceSpaceGapY));
                theImage.DrawPolyline(rulerYVertices.ToArray(), false, colorGreen, 2);
            }



            DenseVector dvXSpaceValues = DenseVector.Create(xValuesCount, new Func <int, double>(i => xSpaceMin + ((double)i / ((double)xValuesCount - 1.0d)) * (xSpaceMax - xSpaceMin)));
            DenseVector parametersList = null;

            for (int i = 0; i < theRepresentingFunctions.Count; i++)
            {
                Func <DenseVector, double, double> theRepresentingFunction = theRepresentingFunctions[i];
                DenseVector currentParametersList = parameters[i];

                DenseVector dvFuncValues = DenseVector.Create(xValuesCount, new Func <int, double>(j => theRepresentingFunction(currentParametersList, dvXSpaceValues[j])));
                double      funcMax      = dvFuncValues.Max();
                double      funcMin      = dvFuncValues.Min();
                overallFuncMax = (funcMax > overallFuncMax) ? (funcMax) : (overallFuncMax);
                overallFuncMin = (funcMin < overallFuncMin) ? (funcMin) : (overallFuncMin);
            }



            for (int i = 0; i < theRepresentingFunctions.Count; i++)
            {
                Func <DenseVector, double, double> theRepresentingFunction = theRepresentingFunctions[i];
                DenseVector currentParametersList = parameters[i];

                //DenseVector dvXSpaceValues = DenseVector.Create(xValuesCount, new Func<int, double>(i => xSpaceMin + ((double)i / ((double)xValuesCount - 1.0d)) * (xSpaceMax - xSpaceMin)));
                parametersList = null;
                DenseVector dvFuncValues     = DenseVector.Create(xValuesCount, new Func <int, double>(j => theRepresentingFunction(currentParametersList, dvXSpaceValues[j])));
                Bgr         currentLineColor = lineColors[i];


                DenseVector xCoordinates = DenseVector.Create(xValuesCount, new Func <int, double>(j => ((double)leftServiceSpaceGapX + j)));
                DenseVector yCoordinates = DenseVector.Create(xValuesCount, new Func <int, double>(j =>
                {
                    double pixValue = koeff * (dvFuncValues[j] - overallFuncMin);
                    return(pictureHeight - btmServiceSpaceGapY - pixValue);
                }));

                List <Point> funcRepresentationPoints = new List <Point>();
                for (int j = 0; j < xValuesCount; j++)
                {
                    if (double.IsNaN(yCoordinates[j]))
                    {
                        continue;
                    }
                    funcRepresentationPoints.Add(new Point(Convert.ToInt32(xCoordinates[j]), Convert.ToInt32(yCoordinates[j])));
                }
                theImage.DrawPolyline(funcRepresentationPoints.ToArray(), false, currentLineColor, 2);
            }
        }
Ejemplo n.º 52
0
        public void SolvePoissonMatrixAndBackMultiply()
        {
            // Create the matrix
            var matrix = new SparseMatrix(25);

            // Assemble the matrix. We assume we're solving the Poisson equation
            // on a rectangular 5 x 5 grid
            const int GridSize = 5;

            // The pattern is:
            // 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0
            for (var i = 0; i < matrix.RowCount; i++)
            {
                // Insert the first set of -1's
                if (i > (GridSize - 1))
                {
                    matrix[i, i - GridSize] = -1;
                }

                // Insert the second set of -1's
                if (i > 0)
                {
                    matrix[i, i - 1] = -1;
                }

                // Insert the centerline values
                matrix[i, i] = 4;

                // Insert the first trailing set of -1's
                if (i < matrix.RowCount - 1)
                {
                    matrix[i, i + 1] = -1;
                }

                // Insert the second trailing set of -1's
                if (i < matrix.RowCount - GridSize)
                {
                    matrix[i, i + GridSize] = -1;
                }
            }

            // Create the y vector
            var y = DenseVector.Create(matrix.RowCount, i => 1);

            // Due to datatype "float" it can happen that solution will not converge for specific random starting vectors
            // That's why we will do 3 tries
            for (var iteration = 0; iteration <= 3; iteration++)
            {
                // Create an iteration monitor which will keep track of iterative convergence
                var monitor = new Iterator <float>(new IIterationStopCriterium <float>[]
                {
                    new IterationCountStopCriterium <float>(MaximumIterations),
                    new ResidualStopCriterium(ConvergenceBoundary),
                    new DivergenceStopCriterium(),
                    new FailureStopCriterium()
                });
                var solver = new MlkBiCgStab(monitor);

                // Solve equation Ax = y
                Vector <float> x;
                try
                {
                    x = solver.Solve(matrix, y);
                }
                catch (Exception)
                {
                    continue;
                }

                if (!(monitor.HasConverged))
                {
                    continue;
                }

                // Now compare the results
                Assert.IsNotNull(x, "#02");
                Assert.AreEqual(y.Count, x.Count, "#03");

                // Back multiply the vector
                var z = matrix.Multiply(x);

                // Now compare the vectors
                for (var i = 0; i < y.Count; i++)
                {
                    Assert.IsTrue(Math.Abs(y[i] - z[i]).IsSmaller(ConvergenceBoundary, 1), "#04-" + i);
                }

                return;
            }
        }
Ejemplo n.º 53
0
        /// <summary>
        ///  calc transform for single scale parameter
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public static Tuple<Matrix<double>, double, Vector<double>, List<double>> Align(List<Vector<double>> origin, List<Vector<double>> target)
        {
            var x = DenseMatrix.OfColumnVectors(origin);
            var y = DenseMatrix.OfColumnVectors(target);

            var mx = x.RowSums() / x.ColumnCount;
            var my = y.RowSums() / y.ColumnCount;

            var xc = x - DenseMatrix.OfColumnVectors(Enumerable.Repeat(mx, x.ColumnCount));
            var yc = y - DenseMatrix.OfColumnVectors(Enumerable.Repeat(my, x.ColumnCount));

            var sx = (xc.PointwisePower(2)).RowNorms(1).Sum() / xc.ColumnCount;
            var sy = (yc.PointwisePower(2)).RowNorms(1).Sum() / yc.ColumnCount;

            var Sxy = yc * xc.Transpose() / xc.ColumnCount;

            var svd = Sxy.Svd();
            var U = svd.U;
            var D = svd.W;
            var VT = svd.VT;

            var rankSxy = Sxy.Rank();
            var detSxy = Sxy.Determinant();
            var S = DiagonalMatrix.CreateIdentity(x.RowCount);

            if (rankSxy > x.ColumnCount - 1)
            {
                if (detSxy < 0)
                {
                    S[x.RowCount, x.RowCount] = -1;
                }
                else if (rankSxy == x.RowCount - 1)
                {
                    if (U.Determinant() * VT.Determinant() < 0)
                    {
                        S[x.RowCount, x.RowCount] = -1;
                    }
                }
                else
                {
                    var r = DiagonalMatrix.CreateIdentity(origin.First().Count);
                    var c = 1;
                    var t = DenseVector.OfArray(new Double[origin.First().Count]);

                    return new Tuple<Matrix<double>, double, Vector<double>, List<double>>(r, c, t, new List<double>() { 0d });
                }
            }

            var R = U * S * VT;
            var C = (D * S).Trace() / sx;
            var T = my - C * R * mx;


            //calculate errors
            List<double> errors = new List<double>();
            for (int i = 0; i < origin.Count; i++)
            {
                errors.Add((target[i] - (C * R * origin[i] + T)).PointwisePower(2).Norm(1));
            }


            return new Tuple<Matrix<double>, double, Vector<double>, List<double>>(R, C, T, errors);
        }
Ejemplo n.º 54
0
        private static void LearnLatentFeatures(PrefRelations PR_train, int maxEpoch,
                                                double learnRate, double regularizationOfUser, double regularizationOfItem,
                                                int factorCount, out List <Vector <double> > P, out List <Vector <double> > Q)
        {
            //regularizationOfUser = 0;
            //regularizationOfItem = 0;
            int userCount = PR_train.UserCount;
            int itemCount = PR_train.ItemCount;

            // User latent vectors with default seed
            P = new List <Vector <double> >();
            Q = new List <Vector <double> >();
            ContinuousUniform uniformDistribution = new ContinuousUniform(0, 0.1, new Random(Config.Seed));

            //var p = Utils.CreateRandomMatrixFromUniform(userCount, factorCount, 0, 0.1, Config.Seed);
            for (int i = 0; i < userCount; i++)
            {
                P.Add(DenseVector.CreateRandom(factorCount, uniformDistribution));
            }
            for (int i = 0; i < itemCount; i++)
            {
                Q.Add(DenseVector.CreateRandom(factorCount, uniformDistribution));
            }
            //   P = Utils.CreateRandomMatrixFromUniform(userCount, factorCount, 0, 0.1, Config.Seed);
            // Item latent vectors with a different seed
            //Q = Utils.CreateRandomMatrixFromUniform(factorCount, itemCount, 0, 0.1, Config.Seed + 1);

            // SGD
            double previousErrorSum = long.MaxValue;

            for (int epoch = 0; epoch < maxEpoch; ++epoch)
            {
                // For each epoch, we will iterate through all
                // preference relations of all users

                // Loop through each user
                foreach (var pair in PR_train.PreferenceRelationsByUser)
                {
                    int          indexOfUser = pair.Key;
                    SparseMatrix preferenceRelationsOfUser = pair.Value;

                    // For each preference relation of this user, update the latent feature vectors
                    foreach (var entry in preferenceRelationsOfUser.EnumerateIndexed(Zeros.AllowSkip))
                    {
                        int indexOfItem_i = entry.Item1;
                        int indexOfItem_j = entry.Item2;
                        //Console.WriteLine(preferenceRelationsOfUser[indexOfItem_i, indexOfItem_j]);
                        //Console.WriteLine(preferenceRelationsOfUser[indexOfItem_j, indexOfItem_i]);
                        if (indexOfItem_i >= indexOfItem_j)
                        {
                            continue;
                        }

                        // Warning: here we need to convert the customized preference indicators
                        // from 1,2,3 into 0,0.5,1 for match the scale of predicted pi, which is in range [0,1]
                        double prefRelation_uij = 0;
                        if (entry.Item3 == Config.Preferences.Preferred)
                        {
                            prefRelation_uij = 1.0;
                        }
                        else if (entry.Item3 == Config.Preferences.EquallyPreferred)
                        {
                            prefRelation_uij = 0.5;
                        }
                        else if (entry.Item3 == Config.Preferences.LessPreferred)
                        {
                            prefRelation_uij = 0.0;
                        }
                        else
                        {
                            Debug.Assert(true, "Should not be here.");
                        }

                        // TODO: Maybe it can be faster to do two dot products to remove the substraction (lose sparse  property I think)
                        double PQ_ui        = P[indexOfUser].DotProduct(Q[indexOfItem_i]);
                        double PQ_uj        = P[indexOfUser].DotProduct(Q[indexOfItem_j]);
                        double estimate_uij = PQ_ui - PQ_uj;
                        //double estimate_uij = P.Row(indexOfUser).DotProduct(Q.Column(indexOfItem_i) - Q.Column(indexOfItem_j));   // Eq. 2


                        double exp_estimate_uij        = Math.Exp(estimate_uij);                      // enumerator in Eq. 2
                        double normalized_estimate_uij = SpecialFunctions.InverseLogit(estimate_uij); // pi_uij in paper

                        //Debug.Assert(prefRelation_uij >= 0 && prefRelation_uij <= 1);
                        //Debug.Assert(normalized_estimate_uij >= 0 && normalized_estimate_uij <= 1);


                        // The error term in Eq. 6-9. Note that the author's paper incorrectly puts a power on the error
                        double e_uij = prefRelation_uij - normalized_estimate_uij;
                        //double e_uij = Math.Pow(prefRelation_uij - normalized_estimate_uij, 2) ;  // from Eq. 3&6
                        double e_uij_derivative = (e_uij * normalized_estimate_uij) / (1 + exp_estimate_uij);

                        // Update feature vectors
                        Vector <double> P_u  = P[indexOfUser];
                        Vector <double> Q_i  = Q[indexOfItem_i];
                        Vector <double> Q_j  = Q[indexOfItem_j];
                        Vector <double> Q_ij = Q_i - Q_j;

                        P[indexOfUser] += Q_ij.Multiply(e_uij_derivative * learnRate) - P_u.Multiply(regularizationOfUser * learnRate);

                        // Eq. 7, note that the author's paper incorrectly writes + regularization
                        //Vector<double> P_u_updated = P_u + (Q_ij.Multiply(e_uij_derivative) - P_u.Multiply(regularizationOfUser)).Multiply(learnRate);
                        //P[indexOfUser] = P_u_updated;
                        Vector <double> P_u_derivative = P_u.Multiply(e_uij_derivative * learnRate);
                        // Eq. 8, note that the author's paper incorrectly writes + regularization
                        //Vector<double> Q_i_updated = Q_i + (P_u_derivative - Q_i.Multiply(regularizationOfItem * learnRate));
                        //Q[indexOfItem_i] = Q_i_updated;

                        Q[indexOfItem_i] += (P_u_derivative - Q_i.Multiply(regularizationOfItem * learnRate));

                        // Eq. 9, note that the author's paper incorrectly writes + regularization
                        //Vector<double> Q_j_updated = Q_j - (P_u_derivative - Q_j.Multiply(regularizationOfItem * learnRate));
                        //Q[indexOfItem_j] =Q_j_updated;
                        Q[indexOfItem_j] -= (P_u_derivative - Q_j.Multiply(regularizationOfItem * learnRate));

                        double estimate_uij_updated            = P[indexOfUser].DotProduct(Q[indexOfItem_i] - Q[indexOfItem_j]); // Eq. 2
                        double exp_estimate_uij_updated        = Math.Exp(estimate_uij_updated);                                 // enumerator in Eq. 2
                        double normalized_estimate_uij_updated = SpecialFunctions.InverseLogit(estimate_uij_updated);            // pi_uij in paper
                        //double e_uij_updated = Math.Pow(prefRelation_uij - normalized_estimate_uij_updated, 2);  // from Eq. 3&6
                        double e_uij_updated = prefRelation_uij - normalized_estimate_uij_updated;                               // from Eq. 3&6

                        //double debug1 = Math.Abs(e_uij) - Math.Abs(e_uij_updated);
                        // Debug.Assert(debug1 > 0);    // After update the error should be smaller

                        #region Loop version of gradient update

                        /*
                         * for (int k = 0; k < factorCount; ++k)
                         * {
                         *  double factorOfUser = P[indexOfUser, k];
                         *  double factorOfItem_i = Q[k, indexOfItem_i];
                         *  double factorOfItem_j = Q[k, indexOfItem_j];
                         *
                         *  // TODO: Seperate user/item regularization coefficient
                         *  P[indexOfUser, k] += learnRate * (e_uij * normalized_estimate_uij * factorOfUser - regularization * factorOfUser);
                         *  // Two items are updated in different directions
                         *  Q[k, indexOfItem_i] += learnRate * (normalized_estimate_uij * factorOfItem_i - regularization * factorOfItem_i);
                         *  // Two items are updated in different directions
                         *  Q[k, indexOfItem_j] -= learnRate * (normalized_estimate_uij * factorOfItem_j - regularization * factorOfItem_j);
                         * }
                         */
                        #endregion
                    }
                }

                // Display the current regularized error see if it converges
                double currentErrorSum = 0;
                //if (epoch == 0 || epoch == maxEpoch - 1 || epoch % (int)Math.Ceiling(maxEpoch * 0.1) == 4)
                if (true)
                {
                    double eSum = 0;
                    foreach (var pair in PR_train.PreferenceRelationsByUser)
                    {
                        int          indexOfUser = pair.Key;
                        SparseMatrix preferenceRelationsOfUser = pair.Value;

                        // For each preference relation of this user, update the latent feature vectors
                        foreach (var entry in preferenceRelationsOfUser.EnumerateIndexed(Zeros.AllowSkip))
                        {
                            int indexOfItem_i = entry.Item1;
                            int indexOfItem_j = entry.Item2;

                            if (indexOfItem_i >= indexOfItem_j)
                            {
                                continue;
                            }

                            double prefRelation_uij = 0;
                            if (entry.Item3 == Config.Preferences.Preferred)
                            {
                                prefRelation_uij = 1.0;
                            }
                            else if (entry.Item3 == Config.Preferences.EquallyPreferred)
                            {
                                prefRelation_uij = 0.5;
                            }
                            else if (entry.Item3 == Config.Preferences.LessPreferred)
                            {
                                prefRelation_uij = 0.0;
                            }
                            else
                            {
                                Debug.Assert(true, "Should not be here.");
                            }

                            // TODO: Maybe it can be faster to do two dot products to remove the substraction (lose sparse  property I think)
                            double estimate_uij            = P[indexOfUser].DotProduct(Q[indexOfItem_i] - Q[indexOfItem_j]); // Eq. 2
                            double normalized_estimate_uij = SpecialFunctions.InverseLogit(estimate_uij);                    // Eq. 2
                            eSum += Math.Pow((prefRelation_uij - normalized_estimate_uij), 2);                               // Sum the error of this preference relation

                            // Sum the regularization term
                            //for (int k = 0; k < factorCount; ++k)
                            // {
                            //     eSum += (regularizationOfUser * 0.5) * (Math.Pow(P[indexOfUser, k], 2)
                            //         + Math.Pow(Q[k, indexOfItem_i], 2) + Math.Pow(Q[k, indexOfItem_j], 2));
                            // }
                        }
                    }
                    double regularizationPenaty = regularizationOfUser * P.Sum(x => x.SquaredSum());
                    regularizationPenaty += regularizationOfItem * Q.Sum(x => x.SquaredSum());
                    eSum += regularizationPenaty;

                    // Record the current error
                    currentErrorSum = eSum;

                    Utils.PrintEpoch("Epoch", epoch, maxEpoch, "Learning error", eSum.ToString("0.0"), true);
                    // Stop the learning if the regularized error falls below a certain threshold
                    // Actually we only check it once every several epoches
                    if (previousErrorSum - currentErrorSum < 0.0001)
                    {
                        Console.WriteLine("Improvment less than 0.0001, learning stopped.");
                        break;
                    }
                    previousErrorSum = currentErrorSum;
                }
            }
        }
Ejemplo n.º 55
0
        public void ReturnEvaluatedIndividual(Individual ind)
        {
            bool didAdd = _featureMap.Add(ind);

            if (ind.Generation != _generation)
            {
                return;
            }

            _feature_mean += DenseVector.OfArray(ind.Features);
            if (didAdd)
            {
                _parents.Add(ind);
            }
            _individualsEvaluated++;
            _populationCount++;

            if (_populationCount >= _params.PopulationSize)
            {
                int  numParents   = _parents.Count;
                bool needsRestart = numParents == 0;
                Console.WriteLine(ind.EmitterID + " " + numParents);

                // Only update if we have parents.
                if (numParents > 0)
                {
                    _feature_mean /= _params.PopulationSize;
                    foreach (Individual cur in _parents)
                    {
                        LA.Vector <double> dv =
                            DenseVector.OfArray(cur.Features) - _feature_mean;
                        cur.Delta = _direction.DotProduct(dv);
                    }
                    _parents = _parents.OrderByDescending(o => o.Delta).ToList();

                    // Calculate fresh weights for the number of elites found
                    var weights = LA.Vector <double> .Build.Dense(numParents);

                    for (int i = 0; i < numParents; i++)
                    {
                        weights[i] = Math.Log(numParents + 0.5) - Math.Log(i + 1);
                    }
                    weights /= weights.Sum();

                    // Dynamically update the hyperparameters for CMA-ES
                    double sumWeights = weights.Sum();
                    double sumSquares = weights.Sum(x => x * x);
                    double mueff      = sumWeights * sumWeights / sumSquares;
                    double cc         = (4 + mueff / _numParams) / (_numParams + 4 + 2 * mueff / _numParams);
                    double cs         = (mueff + 2) / (_numParams + mueff + 5);
                    double c1         = 2 / (Math.Pow(_numParams + 1.3, 2) + mueff);
                    double cmu        = Math.Min(1 - c1,
                                                 2 * (mueff - 2 + 1 / mueff) / (Math.Pow(_numParams + 2, 2) + mueff));
                    double damps = 1 + 2 * Math.Max(0, Math.Sqrt((mueff - 1) / (_numParams + 1)) - 1) + cs;
                    double chiN  = Math.Sqrt(_numParams) *
                                   (1.0 - 1.0 / (4.0 * _numParams) + 1.0 / (21.0 * Math.Pow(_numParams, 2)));

                    // Recombination of the new mean
                    LA.Vector <double> oldMean = _mean;
                    _mean = LA.Vector <double> .Build.Dense(_numParams);

                    for (int i = 0; i < numParents; i++)
                    {
                        _mean += DenseVector.OfArray(_parents[i].ParamVector) * weights[i];
                    }

                    // Update the evolution path
                    LA.Vector <double> y = _mean - oldMean;
                    LA.Vector <double> z = _C.Invsqrt * y;
                    _ps = (1.0 - cs) * _ps + (Math.Sqrt(cs * (2.0 - cs) * mueff) / _mutationPower) * z;
                    double left = _ps.DotProduct(_ps) / _numParams /
                                  (1.0 - Math.Pow(1.0 - cs, 2 * _individualsEvaluated / _params.PopulationSize));
                    double right = 2.0 + 4.0 / (_numParams + 1.0);
                    double hsig  = left < right ? 1 : 0;
                    _pc = (1.0 - cc) * _pc + hsig * Math.Sqrt(cc * (2.0 - cc) * mueff) * y;

                    // Covariance matrix update
                    double c1a = c1 * (1.0 - (1.0 - hsig * hsig) * cc * (2.0 - cc));
                    _C.C *= (1.0 - c1a - cmu);
                    _C.C += c1 * _pc.OuterProduct(_pc);
                    for (int i = 0; i < _params.NumParents; i++)
                    {
                        LA.Vector <double> dv = DenseVector.OfArray(_parents[i].ParamVector) - oldMean;
                        _C.C += weights[i] * cmu *dv.OuterProduct(dv) / (_mutationPower * _mutationPower);
                    }

                    if (checkStop(_parents))
                    {
                        needsRestart = true;
                    }
                    else
                    {
                        _C.UpdateEigensystem();
                    }

                    // Update sigma
                    double cn          = cs / damps;
                    double sumSquarePs = _ps.DotProduct(_ps);
                    _mutationPower *= Math.Exp(Math.Min(1, cn * (sumSquarePs / _numParams - 1) / 2));
                }

                if (needsRestart)
                {
                    reset();
                }

                _generation++;
                _individualsDispatched = 0;
                _populationCount       = 0;
                _parents.Clear();
            }
        }
Ejemplo n.º 56
0
        /// <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 matrix, Vector input, Vector 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;

            // 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);
            }

            if (input.Count != matrix.RowCount)
            {
                throw new ArgumentException(Resources.ArgumentMatrixDimensions);
            }

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

            if (_preconditioner == null)
            {
                _preconditioner = new UnitPreconditioner();
            }

            _preconditioner.Initialize(matrix);

            var d = new DenseVector(input.Count);
            var r = new DenseVector(input);

            var uodd  = new DenseVector(input.Count);
            var ueven = new DenseVector(input.Count);

            var v = new DenseVector(input.Count);
            var pseudoResiduals = new DenseVector(input);

            var x     = new DenseVector(input.Count);
            var yodd  = new DenseVector(input.Count);
            var yeven = new DenseVector(input);

            // Temp vectors
            var temp  = new DenseVector(input.Count);
            var temp1 = new DenseVector(input.Count);
            var temp2 = new DenseVector(input.Count);

            // Initialize
            var startNorm = input.Norm(2);

            // Define the scalars
            float alpha = 0;
            float eta   = 0;
            float theta = 0;

            var tau = startNorm;
            var rho = tau * tau;

            // Calculate the initial values for v
            // M temp = yEven
            _preconditioner.Approximate(yeven, temp);

            // v = A temp
            matrix.Multiply(temp, v);

            // Set uOdd
            v.CopyTo(ueven);

            // Start the iteration
            var iterationNumber = 0;

            while (ShouldContinue(iterationNumber, result, input, pseudoResiduals))
            {
                // First part of the step, the even bit
                if (IsEven(iterationNumber))
                {
                    // sigma = (v, r)
                    var sigma = v.DotProduct(r);
                    if (sigma.AlmostEqual(0, 1))
                    {
                        // FAIL HERE
                        _iterator.IterationCancelled();
                        break;
                    }

                    // alpha = rho / sigma
                    alpha = rho / sigma;

                    // yOdd = yEven - alpha * v
                    v.Multiply(-alpha, temp1);
                    yeven.Add(temp1, yodd);

                    // Solve M temp = yOdd
                    _preconditioner.Approximate(yodd, temp);

                    // uOdd = A temp
                    matrix.Multiply(temp, uodd);
                }

                // The intermediate step which is equal for both even and
                // odd iteration steps.
                // Select the correct vector
                var uinternal = IsEven(iterationNumber) ? ueven : uodd;
                var yinternal = IsEven(iterationNumber) ? yeven : yodd;

                // pseudoResiduals = pseudoResiduals - alpha * uOdd
                uinternal.Multiply(-alpha, temp1);
                pseudoResiduals.Add(temp1, temp2);
                temp2.CopyTo(pseudoResiduals);

                // d = yOdd + theta * theta * eta / alpha * d
                d.Multiply(theta * theta * eta / alpha, temp);
                yinternal.Add(temp, d);

                // theta = ||pseudoResiduals||_2 / tau
                theta = pseudoResiduals.Norm(2) / tau;
                var c = 1 / (float)Math.Sqrt(1 + (theta * theta));

                // tau = tau * theta * c
                tau *= theta * c;

                // eta = c^2 * alpha
                eta = c * c * alpha;

                // x = x + eta * d
                d.Multiply(eta, temp1);
                x.Add(temp1, temp2);
                temp2.CopyTo(x);

                // Check convergence and see if we can bail
                if (!ShouldContinue(iterationNumber, result, input, pseudoResiduals))
                {
                    // Calculate the real values
                    _preconditioner.Approximate(x, result);

                    // Calculate the true residual. Use the temp vector for that
                    // so that we don't pollute the pseudoResidual vector for no
                    // good reason.
                    CalculateTrueResidual(matrix, temp, result, input);

                    // Now recheck the convergence
                    if (!ShouldContinue(iterationNumber, result, input, temp))
                    {
                        // We're all good now.
                        return;
                    }
                }

                // The odd step
                if (!IsEven(iterationNumber))
                {
                    if (rho.AlmostEqual(0, 1))
                    {
                        // FAIL HERE
                        _iterator.IterationCancelled();
                        break;
                    }

                    var rhoNew = pseudoResiduals.DotProduct(r);
                    var beta   = rhoNew / rho;

                    // Update rho for the next loop
                    rho = rhoNew;

                    // yOdd = pseudoResiduals + beta * yOdd
                    yodd.Multiply(beta, temp1);
                    pseudoResiduals.Add(temp1, yeven);

                    // Solve M temp = yOdd
                    _preconditioner.Approximate(yeven, temp);

                    // uOdd = A temp
                    matrix.Multiply(temp, ueven);

                    // v = uEven + beta * (uOdd + beta * v)
                    v.Multiply(beta, temp1);
                    uodd.Add(temp1, temp);

                    temp.Multiply(beta, temp1);
                    ueven.Add(temp1, v);
                }

                // Calculate the real values
                _preconditioner.Approximate(x, result);

                iterationNumber++;
            }
        }
Ejemplo n.º 57
0
        public void SolvePoissonMatrixAndBackMultiply()
        {
            // Create the matrix
            var matrix = new SparseMatrix(25);

            // Assemble the matrix. We assume we're solving the Poisson equation
            // on a rectangular 5 x 5 grid
            const int GridSize = 5;

            // The pattern is:
            // 0 .... 0 -1 0 0 0 0 0 0 0 0 -1 4 -1 0 0 0 0 0 0 0 0 -1 0 0 ... 0
            for (var i = 0; i < matrix.RowCount; i++)
            {
                // Insert the first set of -1's
                if (i > (GridSize - 1))
                {
                    matrix[i, i - GridSize] = -1;
                }

                // Insert the second set of -1's
                if (i > 0)
                {
                    matrix[i, i - 1] = -1;
                }

                // Insert the centerline values
                matrix[i, i] = 4;

                // Insert the first trailing set of -1's
                if (i < matrix.RowCount - 1)
                {
                    matrix[i, i + 1] = -1;
                }

                // Insert the second trailing set of -1's
                if (i < matrix.RowCount - GridSize)
                {
                    matrix[i, i + GridSize] = -1;
                }
            }

            // Create the y vector
            Vector y = new DenseVector(matrix.RowCount, 1);

            // Create an iteration monitor which will keep track of iterative convergence
            var monitor = new Iterator(new IIterationStopCriterium[]
            {
                new IterationCountStopCriterium(MaximumIterations),
                new ResidualStopCriterium(ConvergenceBoundary),
                new DivergenceStopCriterium(),
                new FailureStopCriterium()
            });
            var solver = new TFQMR(monitor);

            // Solve equation Ax = y
            var x = solver.Solve(matrix, y);

            // Now compare the results
            Assert.IsNotNull(x, "#02");
            Assert.AreEqual(y.Count, x.Count, "#03");

            // Back multiply the vector
            var z = matrix.Multiply(x);

            // Check that the solution converged
            Assert.IsTrue(monitor.Status is CalculationConverged, "#04");

            // Now compare the vectors
            for (var i = 0; i < y.Count; i++)
            {
                Assert.IsTrue((y[i] - z[i]).Magnitude.IsSmaller(1e-4f, 1), "#05-" + i);
            }
        }
Ejemplo n.º 58
0
 public void RandomWithNumberOfElementsLessThanZeroThrowsArgumentException()
 {
     Assert.That(() => DenseVector.CreateRandom(-2, new ContinuousUniform()), Throws.TypeOf <ArgumentOutOfRangeException>());
 }
Ejemplo n.º 59
0
        /// <summary>Computations that depend on the observed value of vVector__264 and vdouble__792</summary>
        private void Changed_vVector__264_vdouble__792()
        {
            if (this.Changed_vVector__264_vdouble__792_iterationsDone == 1)
            {
                return;
            }
            this.vVector__264_marginal = new PointMass <Vector[]>(this.VVector__264);
            this.vdouble__792_marginal = new DistributionStructArray <Gaussian, double>(5622, delegate(int index264) {
                return(Gaussian.Uniform());
            });
            this.vdouble__792_marginal = Distribution.SetPoint <DistributionStructArray <Gaussian, double>, double[]>(this.vdouble__792_marginal, this.Vdouble__792);
            // The constant 'vVectorGaussian264'
            VectorGaussian vVectorGaussian264 = VectorGaussian.FromNatural(DenseVector.FromArray(new double[3] {
                0.0, 0.0, 0.0
            }), new PositiveDefiniteMatrix(new double[3, 3] {
                { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }
            }));

            this.vVector793_marginal_F = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian264);
            // Message from use of 'vdouble__793'
            DistributionStructArray <Gaussian, double> vdouble__793_use_B = default(DistributionStructArray <Gaussian, double>);

            // Create array for 'vdouble__793_use' Backwards messages.
            vdouble__793_use_B = new DistributionStructArray <Gaussian, double>(5622);
            for (int index264 = 0; index264 < 5622; index264++)
            {
                vdouble__793_use_B[index264] = Gaussian.Uniform();
                // Message to 'vdouble__793_use' from GaussianFromMeanAndVariance factor
                vdouble__793_use_B[index264] = GaussianFromMeanAndVarianceOp.MeanAverageConditional(this.Vdouble__792[index264], 0.1);
            }
            DistributionRefArray <VectorGaussian, Vector> vVector793_rep_B = default(DistributionRefArray <VectorGaussian, Vector>);

            // Create array for 'vVector793_rep' Backwards messages.
            vVector793_rep_B = new DistributionRefArray <VectorGaussian, Vector>(5622);
            for (int index264 = 0; index264 < 5622; index264++)
            {
                vVector793_rep_B[index264] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian264);
                // Message to 'vVector793_rep' from InnerProduct factor
                vVector793_rep_B[index264] = InnerProductOp.AAverageConditional(vdouble__793_use_B[index264], this.VVector__264[index264], vVector793_rep_B[index264]);
            }
            // Buffer for ReplicateOp_Divide.Marginal<VectorGaussian>
            VectorGaussian vVector793_rep_B_toDef = default(VectorGaussian);

            // Message to 'vVector793_rep' from Replicate factor
            vVector793_rep_B_toDef = ReplicateOp_Divide.ToDefInit <VectorGaussian>(vVectorGaussian264);
            // Message to 'vVector793_rep' from Replicate factor
            vVector793_rep_B_toDef = ReplicateOp_Divide.ToDef <VectorGaussian>(vVector793_rep_B, vVector793_rep_B_toDef);
            // Message to 'vVector793_marginal' from Variable factor
            this.vVector793_marginal_F = VariableOp.MarginalAverageConditional <VectorGaussian>(vVector793_rep_B_toDef, vVectorGaussian264, this.vVector793_marginal_F);
            DistributionStructArray <Gaussian, double> vdouble__793_F = default(DistributionStructArray <Gaussian, double>);

            // Create array for 'vdouble__793' Forwards messages.
            vdouble__793_F = new DistributionStructArray <Gaussian, double>(5622);
            for (int index264 = 0; index264 < 5622; index264++)
            {
                vdouble__793_F[index264] = Gaussian.Uniform();
            }
            DistributionRefArray <VectorGaussian, Vector> vVector793_rep_F = default(DistributionRefArray <VectorGaussian, Vector>);

            // Create array for 'vVector793_rep' Forwards messages.
            vVector793_rep_F = new DistributionRefArray <VectorGaussian, Vector>(5622);
            for (int index264 = 0; index264 < 5622; index264++)
            {
                vVector793_rep_F[index264] = ArrayHelper.MakeUniform <VectorGaussian>(vVectorGaussian264);
            }
            // Buffer for ReplicateOp_Divide.UsesAverageConditional<VectorGaussian>
            VectorGaussian vVector793_rep_F_marginal = default(VectorGaussian);

            // Message to 'vVector793_rep' from Replicate factor
            vVector793_rep_F_marginal = ReplicateOp_Divide.MarginalInit <VectorGaussian>(vVectorGaussian264);
            // Message to 'vVector793_rep' from Replicate factor
            vVector793_rep_F_marginal = ReplicateOp_Divide.Marginal <VectorGaussian>(vVector793_rep_B_toDef, vVectorGaussian264, vVector793_rep_F_marginal);
            // Buffer for InnerProductOp.InnerProductAverageConditional
            // Create array for replicates of 'vVector793_rep_F_index264__AMean'
            Vector[] vVector793_rep_F_index264__AMean = new Vector[5622];
            for (int index264 = 0; index264 < 5622; index264++)
            {
                // Message to 'vdouble__793' from InnerProduct factor
                vVector793_rep_F_index264__AMean[index264] = InnerProductOp.AMeanInit(vVector793_rep_F[index264]);
            }
            // Buffer for InnerProductOp.AMean
            // Create array for replicates of 'vVector793_rep_F_index264__AVariance'
            PositiveDefiniteMatrix[] vVector793_rep_F_index264__AVariance = new PositiveDefiniteMatrix[5622];
            for (int index264 = 0; index264 < 5622; index264++)
            {
                // Message to 'vdouble__793' from InnerProduct factor
                vVector793_rep_F_index264__AVariance[index264] = InnerProductOp.AVarianceInit(vVector793_rep_F[index264]);
                // Message to 'vVector793_rep' from Replicate factor
                vVector793_rep_F[index264] = ReplicateOp_Divide.UsesAverageConditional <VectorGaussian>(vVector793_rep_B[index264], vVector793_rep_F_marginal, index264, vVector793_rep_F[index264]);
            }
            // Create array for 'vdouble__793_marginal' Forwards messages.
            this.vdouble__793_marginal_F = new DistributionStructArray <Gaussian, double>(5622);
            for (int index264 = 0; index264 < 5622; index264++)
            {
                this.vdouble__793_marginal_F[index264] = Gaussian.Uniform();
                // Message to 'vdouble__793' from InnerProduct factor
                vVector793_rep_F_index264__AVariance[index264] = InnerProductOp.AVariance(vVector793_rep_F[index264], vVector793_rep_F_index264__AVariance[index264]);
                // Message to 'vdouble__793' from InnerProduct factor
                vVector793_rep_F_index264__AMean[index264] = InnerProductOp.AMean(vVector793_rep_F[index264], vVector793_rep_F_index264__AVariance[index264], vVector793_rep_F_index264__AMean[index264]);
                // Message to 'vdouble__793' from InnerProduct factor
                vdouble__793_F[index264] = InnerProductOp.InnerProductAverageConditional(vVector793_rep_F_index264__AMean[index264], vVector793_rep_F_index264__AVariance[index264], this.VVector__264[index264]);
                // Message to 'vdouble__793_marginal' from DerivedVariable factor
                this.vdouble__793_marginal_F[index264] = DerivedVariableOp.MarginalAverageConditional <Gaussian>(vdouble__793_use_B[index264], vdouble__793_F[index264], this.vdouble__793_marginal_F[index264]);
            }
            this.Changed_vVector__264_vdouble__792_iterationsDone = 1;
        }
Ejemplo n.º 60
0
 protected ContinuousDynamics(double t0, DenseVector InitialValues)
 {
     Time          = t0;
     CurrentValues = InitialValues;
 }