Example #1
0
/***********************************************************************//**
 * @brief GSparseMatrix to GSymMatrix storage class convertor
 *
 * @param[in] matrix Sparse matrix (GSparseMatrix).
 *
 * @exception GException::matrix_not_symmetric
 *            Sparse matrix is not symmetric.
 *
 * Converts a sparse matrix into the symmetric storage class. If the input
 * matrix is not symmetric, an exception is thrown.
 ***************************************************************************/
GSymMatrix::GSymMatrix(const GSparseMatrix& matrix)
{
    // Initialise class members for clean destruction
    init_members();

    // Allocate matrix memory
    alloc_members(matrix.rows(), matrix.cols());

    // Fill matrix
    for (int col = 0; col < matrix.cols(); ++col) {
        for (int row = col; row < matrix.rows(); ++row) {
            double value_ll = matrix(row,col);
            double value_ur = matrix(col,row);
            if (value_ll != value_ur) {
                throw GException::matrix_not_symmetric(G_CAST_SPARSEMATRIX,
                                                       matrix.rows(),
                                                       matrix.cols());
            }
            (*this)(row, col) = matrix(row, col);
        }
    }

    // Return
    return;
}
Example #2
0
/***********************************************************************//**
 * @brief Set Euler rotation matrix around z axis
 *
 * @param[in] angle Rotation angle (degrees)
 ***************************************************************************/
void GMatrix::eulerz(const double& angle)
{
    // Free members
    GMatrixBase::free_members();

    // Initialise members
    GMatrixBase::init_members();

    // Construct 3*3 matrix
    alloc_members(3,3);

    // Compute angles
    double cosangle = cos(angle * deg2rad);
    double sinangle = sin(angle * deg2rad);

    // Set matrix elements
    (*this)(0,0) =  cosangle;
    (*this)(0,1) = -sinangle;
    (*this)(0,2) =       0.0;
    (*this)(1,0) =  sinangle;
    (*this)(1,1) =  cosangle;
    (*this)(1,2) =       0.0;
    (*this)(2,0) =       0.0;
    (*this)(2,1) =       0.0;
    (*this)(2,2) =       1.0;

    // Return
    return;
}
Example #3
0
/***********************************************************************//**
 * @brief Matrix constructor
 *
 * @param[in] rows Number of rows.
 * @param[in] cols Number of columns.
 *
 * This method allocates a generic matrix object with the specified number
 * of rows and columns.
 ***************************************************************************/
GMatrix::GMatrix(const int& rows, const int& cols) : GMatrixBase()
{
    // Initialise class members for clean destruction
    init_members();

    // Allocate matrix memory
    alloc_members(rows, cols);

    // Return
    return;
}
Example #4
0
/***********************************************************************//**
 * @brief Vector constructor
 *
 * @param[in] num Number of elements in vector.
 *
 * Initialises a vector with @p num elements. All vector elements will be
 * set to 0.
 ***************************************************************************/
GVector::GVector(const int& num)
{
    // Initialise class members
    init_members();

    // Store vector size
    m_num = num;

    // Allocate vector (filled with 0)
    alloc_members();

    // Return
    return;
}
Example #5
0
/***********************************************************************//**
 * @brief Copy class members
 *
 * @param[in] vector Vector from which members should be copied.
 ***************************************************************************/
void GVector::copy_members(const GVector& vector)
{
    // Copy attributes
    m_num = vector.m_num;

    // Copy elements
    if (m_num > 0) {
        alloc_members();
        for (int i = 0; i <  m_num; ++i) {
            m_data[i] = vector.m_data[i];
        }
    }

    // Return
    return;
}
Example #6
0
/***********************************************************************//**
 * @brief Single element vector constructor
 *
 * @param[in] a Vector element.
 *
 * Initialises a vector with a single element.
 ***************************************************************************/
GVector::GVector(const double& a)
{
    // Initialise class members
    init_members();

    // Store vector size
    m_num = 1;

    // Allocate vector
    alloc_members();

    // Set value
    m_data[0] = a;

    // Return
    return;
}
Example #7
0
/***********************************************************************//**
 * @brief GSparseMatrix to GMatrix storage class convertor
 *
 * @param[in] matrix Sparse matrix (GSparseMatrix).
 *
 * This constructor converts a sparse matrix (of type GSparseMatrix) into a
 * generic matrix. As the result is generic, the conversion will succeed in
 * all cases. 
 ***************************************************************************/
GMatrix::GMatrix(const GSparseMatrix& matrix) : GMatrixBase(matrix)
{
    // Initialise class members for clean destruction
    init_members();

    // Construct matrix
    alloc_members(matrix.rows(), matrix.cols());

    // Fill matrix
    for (int col = 0; col < matrix.cols(); ++col) {
        for (int row = 0; row < matrix.rows(); ++row) {
            (*this)(row, col) = matrix(row, col);
        }
    }

    // Return
    return;
}
Example #8
0
/***********************************************************************//**
 * @brief Three elements vector constructor
 *
 * @param[in] a First vector element.
 * @param[in] b Second vector element.
 * @param[in] c Third vector element.
 *
 * Initialises a vector with three elements.
 ***************************************************************************/
GVector::GVector(const double& a, const double& b, const double& c)
{
    // Initialise class members
    init_members();

    // Store vector size
    m_num = 3;

    // Allocate vector
    alloc_members();

    // Set values
    m_data[0] = a;
    m_data[1] = b;
    m_data[2] = c;

    // Return
    return;
}
Example #9
0
/***********************************************************************//**
 * @brief GSymMatrix to GMatrix storage class convertor
 *
 * @param[in] matrix Symmetric matrix (GSymMatrix).
 *
 * This constructor converts a symmetric matrix (of type GSymMatrix) into a
 * generic matrix. As the result is generic, the conversion will succeed in
 * all cases. 
 ***************************************************************************/
GMatrix::GMatrix(const GSymMatrix& matrix) : GMatrixBase(matrix)
{
    // Initialise class members for clean destruction
    init_members();

    // Allocate matrix memory
    alloc_members(matrix.rows(), matrix.cols());

    // Fill matrix. We benefit here from the symmetry of the matrix and
    // loop only over the lower triangle of the symmetric matrix to perform
    // the fill.
    for (int col = 0; col < matrix.cols(); ++col) {
        for (int row = col; row < matrix.rows(); ++row) {
            double value      = matrix(row, col);
            (*this)(row, col) = value;
            (*this)(col, row) = value;
        }
    }

    // Return
    return;
}