Ejemplo n.º 1
0
        public IMatrix <double> GetSolution(IROMatrix <double> B)
        {
            var result = new JaggedArrayMatrix(m, B.ColumnCount);

            Solve(B, result);
            return(result);
        }
Ejemplo n.º 2
0
        /** Return the upper triangular factor
         *          @return     R
         */

        public JaggedArrayMatrix GetR()
        {
            var X = new JaggedArrayMatrix(n, n);

            double[][] R = X.Array;
            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i < j)
                    {
                        R[i][j] = QR[i][j];
                    }
                    else if (i == j)
                    {
                        R[i][j] = Rdiag[i];
                    }
                    else
                    {
                        R[i][j] = 0.0;
                    }
                }
            }
            return(X);
        }
Ejemplo n.º 3
0
        /** Generate and return the (economy-sized) orthogonal factor
         *          @return     Q
         */

        public JaggedArrayMatrix GetQ()
        {
            var X = new JaggedArrayMatrix(m, n);

            double[][] Q = X.Array;
            for (int k = n - 1; k >= 0; k--)
            {
                for (int i = 0; i < m; i++)
                {
                    Q[i][k] = 0.0;
                }
                Q[k][k] = 1.0;
                for (int j = k; j < n; j++)
                {
                    if (QR[k][k] != 0)
                    {
                        double s = 0.0;
                        for (int i = k; i < m; i++)
                        {
                            s += QR[i][k] * Q[i][j];
                        }
                        s = -s / QR[k][k];
                        for (int i = k; i < m; i++)
                        {
                            Q[i][j] += s * QR[i][k];
                        }
                    }
                }
            }
            return(X);
        }
Ejemplo n.º 4
0
        /** Return the Householder vectors
         *          @return     Lower trapezoidal matrix whose columns define the reflections
         */

        public JaggedArrayMatrix GetH()
        {
            var X = new JaggedArrayMatrix(m, n);

            double[][] H = X.Array;
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i >= j)
                    {
                        H[i][j] = QR[i][j];
                    }
                    else
                    {
                        H[i][j] = 0.0;
                    }
                }
            }
            return(X);
        }
Ejemplo n.º 5
0
        /** Least squares solution of A*X = B
         *          @param B    A Matrix with as many rows as A and any number of columns.
         *          @return     X that minimizes the two norm of Q*R*X-B.
         *          @exception  IllegalArgumentException  Matrix row dimensions must agree.
         *          @exception  RuntimeException  Matrix is rank deficient.
         */

        public void Solve(IROMatrix <double> B, IMatrix <double> result)
        {
            if (B.RowCount != m)
            {
                throw new ArgumentException("Matrix row dimensions must agree.");
            }
            if (!IsFullRank())
            {
                throw new Exception("Matrix is rank deficient.");
            }

            // Copy right hand side
            int nx = B.ColumnCount;

            double[][] X;
            if (_solveMatrixWorkspace != null && _solveMatrixWorkspace.RowCount == B.RowCount && _solveMatrixWorkspace.ColumnCount == B.ColumnCount)
            {
                X = _solveMatrixWorkspace.Array;
                MatrixMath.Copy(B, _solveMatrixWorkspace);
            }
            else
            {
                X = JaggedArrayMath.GetMatrixCopy(B);
                _solveMatrixWorkspace = new JaggedArrayMatrix(X, B.RowCount, B.ColumnCount);
            }

            // Compute Y = transpose(Q)*B
            for (int k = 0; k < n; k++)
            {
                for (int j = 0; j < nx; j++)
                {
                    double s = 0.0;
                    for (int i = k; i < m; i++)
                    {
                        s += QR[i][k] * X[i][j];
                    }
                    s = -s / QR[k][k];
                    for (int i = k; i < m; i++)
                    {
                        X[i][j] += s * QR[i][k];
                    }
                }
            }
            // Solve R*X = Y;
            for (int k = n - 1; k >= 0; k--)
            {
                for (int j = 0; j < nx; j++)
                {
                    X[k][j] /= Rdiag[k];
                }
                for (int i = 0; i < k; i++)
                {
                    for (int j = 0; j < nx; j++)
                    {
                        X[i][j] -= X[k][j] * QR[i][k];
                    }
                }
            }

            MatrixMath.Submatrix(_solveMatrixWorkspace, result, 0, 0);
        }
Ejemplo n.º 6
0
    public static void CalculatePRESS(
      IROMatrix matrixX,
      IROMatrix xLoads,
      IROMatrix yLoads,
      IROMatrix xScores,
      IROVector crossProduct,
      int numberOfFactors,
      out IROVector PRESS)
    {
      IMatrix predictedY = new JaggedArrayMatrix(yLoads.Rows,yLoads.Columns);
      IVector press = VectorMath.CreateExtensibleVector(numberOfFactors+1);
      PRESS = press;

      press[0] = MatrixMath.SumOfSquares(yLoads);
      for(int nf=0;nf<numberOfFactors;nf++)
      {
        Predict(matrixX,xLoads,yLoads,xScores,crossProduct,nf,predictedY,null);
        press[nf+1] = MatrixMath.SumOfSquaredDifferences(yLoads,predictedY);
      }
    }
Ejemplo n.º 7
0
		/** Least squares solution of A*X = B
		@param B    A Matrix with as many rows as A and any number of columns.
		@return     X that minimizes the two norm of Q*R*X-B.
		@exception  IllegalArgumentException  Matrix row dimensions must agree.
		@exception  RuntimeException  Matrix is rank deficient.
		*/

		public void Solve(IROMatrix B, IMatrix result)
		{
			if (B.Rows != m)
			{
				throw new ArgumentException("Matrix row dimensions must agree.");
			}
			if (!this.IsFullRank())
			{
				throw new Exception("Matrix is rank deficient.");
			}

			// Copy right hand side
			int nx = B.Columns;
			double[][] X;
			if (_solveMatrixWorkspace != null && _solveMatrixWorkspace.Rows == B.Rows && _solveMatrixWorkspace.Columns == B.Columns)
			{
				X = _solveMatrixWorkspace.Array;
				MatrixMath.Copy(B, _solveMatrixWorkspace);
			}
			else
			{
				X = JaggedArrayMath.GetMatrixCopy(B);
				_solveMatrixWorkspace = new JaggedArrayMatrix(X, B.Rows, B.Columns);
			}

			// Compute Y = transpose(Q)*B
			for (int k = 0; k < n; k++)
			{
				for (int j = 0; j < nx; j++)
				{
					double s = 0.0;
					for (int i = k; i < m; i++)
					{
						s += QR[i][k] * X[i][j];
					}
					s = -s / QR[k][k];
					for (int i = k; i < m; i++)
					{
						X[i][j] += s * QR[i][k];
					}
				}
			}
			// Solve R*X = Y;
			for (int k = n - 1; k >= 0; k--)
			{
				for (int j = 0; j < nx; j++)
				{
					X[k][j] /= Rdiag[k];
				}
				for (int i = 0; i < k; i++)
				{
					for (int j = 0; j < nx; j++)
					{
						X[i][j] -= X[k][j] * QR[i][k];
					}
				}
			}

			MatrixMath.Submatrix(_solveMatrixWorkspace, result, 0, 0);
		}
Ejemplo n.º 8
0
		public IMatrix GetSolution(IROMatrix A, IROMatrix B)
		{
			Decompose(A);
			JaggedArrayMatrix result = new JaggedArrayMatrix(m, B.Columns);
			Solve(B, result);
			return result;
		}
Ejemplo n.º 9
0
		/** Generate and return the (economy-sized) orthogonal factor
		@return     Q
		*/

		public JaggedArrayMatrix GetQ()
		{
			JaggedArrayMatrix X = new JaggedArrayMatrix(m, n);
			double[][] Q = X.Array;
			for (int k = n - 1; k >= 0; k--)
			{
				for (int i = 0; i < m; i++)
				{
					Q[i][k] = 0.0;
				}
				Q[k][k] = 1.0;
				for (int j = k; j < n; j++)
				{
					if (QR[k][k] != 0)
					{
						double s = 0.0;
						for (int i = k; i < m; i++)
						{
							s += QR[i][k] * Q[i][j];
						}
						s = -s / QR[k][k];
						for (int i = k; i < m; i++)
						{
							Q[i][j] += s * QR[i][k];
						}
					}
				}
			}
			return X;
		}
Ejemplo n.º 10
0
		/** Return the upper triangular factor
		@return     R
		*/

		public JaggedArrayMatrix GetR()
		{
			JaggedArrayMatrix X = new JaggedArrayMatrix(n, n);
			double[][] R = X.Array;
			for (int i = 0; i < n; i++)
			{
				for (int j = 0; j < n; j++)
				{
					if (i < j)
					{
						R[i][j] = QR[i][j];
					}
					else if (i == j)
					{
						R[i][j] = Rdiag[i];
					}
					else
					{
						R[i][j] = 0.0;
					}
				}
			}
			return X;
		}
Ejemplo n.º 11
0
		/** Return the Householder vectors
		@return     Lower trapezoidal matrix whose columns define the reflections
		*/

		public JaggedArrayMatrix GetH()
		{
			JaggedArrayMatrix X = new JaggedArrayMatrix(m, n);
			double[][] H = X.Array;
			for (int i = 0; i < m; i++)
			{
				for (int j = 0; j < n; j++)
				{
					if (i >= j)
					{
						H[i][j] = QR[i][j];
					}
					else
					{
						H[i][j] = 0.0;
					}
				}
			}
			return X;
		}