Getting reference of a sub-matrix in java

后端 未结 4 1519
悲&欢浪女
悲&欢浪女 2020-12-18 17:26

I need to do parallel processing of sub-matrices recursively (original matrix divided into 4 passed into a method). The matrix is stored as a 2D array. I can\'t copy the ele

4条回答
  •  独厮守ぢ
    2020-12-18 17:41

    How about defining a template class as follows:

    template  >
    class Matrix{
    private:
        S m_structure; //The matrix structure
        I m_rowstart;//Row start index
        I m_columnstart;//Column start index
    }
    

    The main constructors would be

    Matrix();
    Matrix(size_t r, size_t c);//r rows and c columns
    Matrix(size_t r, size_t c, I rowStart, I columnStart);//rowstart and columnstart are given start indices
    Matrix(const Matrix& source);
    

    You would then have functions to return the minimum/max row/column indices of form:

    I MinRowIndex() const;
    

    Next, you have functions to tell the number of rows/columns in the matrix.

    size_t Rows() const;
    

    Then, a function to allow replacement of elements in a row/column by another array of elements

    void Row(I row, const Array& val);//Replace row
    

    Then, overload () to allow access to element in a given row and column

    const V& operator ()(I row, I column) const;//Get Element
    V& operator() (I row, I column);
    

    Although computational tests may need to be done to check the benefit of this method as compared to maintaining a big matrix and individual start/stop indices of various submatrices (as suggested by Lagerbaer in the other thread) one advantage here is that each submatrix is independent. They can be transposed, moved around, replaced. You probably may need to maintain a higher-level matrix structure in which this Matrix is a sub-structure.

    But it does seem to satisfy your question of being able to reference the submatrices independently.

提交回复
热议问题