Beispiel #1
0
// Matrix A's first n columns are orthonormal
// so A.Columns(1,n).t() * A.Columns(1,n) is the identity matrix.
// Fill out the remaining columns of A to make them orthonormal
// so A.t() * A is the identity matrix 
void extend_orthonormal(Matrix& A, int n)
{
   REPORT
   Tracer et("extend_orthonormal");
   int nr = A.nrows(); int nc = A.ncols();
   if (nc > nr) Throw(IncompatibleDimensionsException(A));
   if (n > nc) Throw(IncompatibleDimensionsException(A));
   ColumnVector SSR;
   { Matrix A1 = A.Columns(1,n); SSR = A1.sum_square_rows(); }
   for (int i = n; i < nc; ++i)
   {
      // pick row with smallest SSQ
      int k; SSR.minimum1(k);
      // orthogonalise column with 1 at element k, 0 elsewhere
      // next line is rather inefficient
      ColumnVector X = - A.Columns(1, i) * A.SubMatrix(k, k, 1, i).t();
      X(k) += 1.0;
      // normalise
      X /= sqrt(X.SumSquare());
      // update row sums of squares
      for (k = 1; k <= nr; ++k) SSR(k) += square(X(k));
      // load new column into matrix
      A.Column(i+1) = X;
   }
}
Beispiel #2
0
void VectorizeScan(std::vector<Index>& scan,
				   const Matrix<Real> &M)
{
	Index index(0,0);
	scan.reserve(M.Rows() * M.Columns());
	for (unsigned i=0; i<scan.capacity(); ++i) {
		scan.push_back(index);
	}
	for (int i=0; i<M.Rows(); ++i) {
		for (int j=0; j<M.Columns(); ++j) {
			scan[M(i,j)].first	= i;
			scan[M(i,j)].second	= j;
		}
	}
}
Beispiel #3
0
void PCL_FUNC InPlaceGaussJordan( Matrix& A, Matrix& B )
{
   A.SetUnique();
   B.SetUnique();
   if ( (*API->Numerical->GaussJordanInPlaceD)( A.DataPtr(), B.DataPtr(), A.Rows(), B.Columns() ) == api_false )
      throw APIFunctionError( "GaussJordanInPlaceD" );
}
Beispiel #4
0
bool WriteMatrixToFile(const Matrix<Real> &M, const std::string& filename) {
    std::ofstream file(filename.c_str());

    if (!file.is_open()) {
        std::cout << "File not found: " << filename << std::endl;
        return false;
    }

    file << M.Rows() << "\n";
    file << M.Columns() << "\n";
    for (int r=0; r<M.Rows(); ++r) {
        for (int c=0; c<M.Columns(); ++c) {
            file << M(r,c) << " ";
        }
        file << "\n";
    }
    return true;
}
Beispiel #5
0
void PCL_FUNC InPlaceSVDImplementation( Matrix& A, Vector& W, Matrix& V )
{
   A.SetUnique();
   int m = A.Rows();
   int n = A.Columns();
   W = Vector( n );
   V = Matrix( n, n );
   if ( (*API->Numerical->SVDInPlaceD)( A.DataPtr(), W.DataPtr(), V.DataPtr(), m, n ) == api_false )
      throw APIFunctionError( "SVDInPlaceD" );
}
Beispiel #6
0
bool Matrix::Equal( Matrix& a, Matrix& b )
{
    if( a.Type() != b.Type() )
        return false;

    if( a.Rows() != b.Rows() ||
        a.Columns() != b.Columns() )
        return false;

    for( UINT r=0; r < a.Rows(); r++ )
    {
        for( UINT c=0; c < a.Columns(); c++ )
        {
            DataType* pA = a.Get(r, c);
            DataType* pB = b.Get(r, c);

            if( !DataType::Equal( pA, pB ) )
                return false;
        }
    }

    return true;
}
Beispiel #7
0
void CircularShift(const Matrix& X1, int first, int last)
{
      Matrix X; UpperTriangularMatrix U1, U2;
      int n = X1.Ncols();

      // Try right circular shift of columns
      X = X1; QRZ(X, U1);
      RightCircularUpdateCholesky(U1, first, last);
      X = X1.Columns(1,first-1) | X1.Column(last)
         | X1.Columns(first,last-1) | X1.Columns(last+1,n);
      QRZ(X, U2);
      X = U1 - U2; Clean(X, 0.000000001); Print(X);

      // Try left circular shift of columns
      X = X1; QRZ(X, U1);
      LeftCircularUpdateCholesky(U1, first, last);
      X = X1.Columns(1,first-1) | X1.Columns(first+1,last)
         | X1.Column(first) | X1.Columns(last+1,n);
      QRZ(X, U2);
      X = U1 - U2; Clean(X, 0.000000001); Print(X);
}
Beispiel #8
0
SCENARIO("Matrix init", "[init]") 
{
	GIVEN("The number of rows and columns") 
	{
		auto rows = 3;
		auto columns = 4;
		WHEN("Create instansce of Matrix") 
		{
			Matrix<int> A(rows, columns);
			Matrix<int> B;
			THEN("The number of rows and columns must be preserved") 
			{
				REQUIRE(A.Rows() == rows);
				REQUIRE(A.Columns() == columns);
				REQUIRE(B.Rows() == 0);
				REQUIRE(B.Columns() == 0);
			}
		}
	}
}

SCENARIO("Matrix operator >>", "[Fill]") 
{
	std::ifstream input("A.txt");
	Matrix<int> A(2, 2);
	std::cout << endl;
	REQUIRE( input >> A );
	REQUIRE( A[0][0] == 2 );
	REQUIRE( A[0][1] == 1 );
	REQUIRE( A[1][0] == 1 );
	REQUIRE( A[1][1] == 2 );