0
$\begingroup$

I am implementing a matrix functionality and the API to the matrix could be open to third party users and therefore it is not a good idea to assume the size of matrix that would be passed.

Part of the API is a function that can be used to get the diagonal of a created matrix. While it is easy to identify the diagonal for matrices (Row x Column) where $R=C$, I am not sure how to obtain the diagonal matrix if $R < C$ or $C > R$. This is being implemented using C++.

Some comments and thoughts on how to implement this algorithm would be appreciated.

    // Matrix definition
    class Matrix{
    private:
      vector<vector<double> > matrix;
      unsigned rows;
      unsigned cols;
    public:
      Matrix(unsigned rows, unsigned cols, double init);
      vector<double> diag_vec(const Matrix& aMatrix);
    };

  //Matrix implementation
    vector<double> diag_vec(const Matrix& aMatrix){
      vector<double> diagonal(aMatrix.rows);
      for(int i=0; i<aMatrix.rows; i++){
         for(int j=0; aMatrix.cols; j++){
             if(i == j)
               diagonal.push_back(aMatrix[row][col];
          }
       }
       return diagonal;
      }
$\endgroup$
5
  • $\begingroup$ How are you defining the diagonal of a non-square matrix? $\endgroup$
    – The Count
    Commented Nov 18, 2016 at 16:36
  • $\begingroup$ @The Count I wouldn't say it's a problem of definition : everybody agrees with the fact that the diagonal is $a_{11},a_{22},... a_{qq}$ where $q=min(C,R}$. It is a problem of internal representation of the matrices in the API Dennis wants to write. Could you say a little more about this, Dennis ? $\endgroup$
    – Jean Marie
    Commented Nov 18, 2016 at 17:37
  • $\begingroup$ @JeanMarie that's what I figured, I just wanted to be sure. $\endgroup$
    – The Count
    Commented Nov 18, 2016 at 19:42
  • $\begingroup$ @JeanMarie Yes my perception of a diagonal is as you have indicated. $\endgroup$
    – qboomerang
    Commented Nov 19, 2016 at 10:44
  • $\begingroup$ I have added some code snippet to demonstrate the vector diag_vec(aMatrix) function that I am talking about. For example what would be the correct diagonal for a 2x3 or 3 x 2 matrix ? I was not aware of the constraint q=min(C, R} could you please expound on that a bit? $\endgroup$
    – qboomerang
    Commented Nov 19, 2016 at 11:01

1 Answer 1

0
$\begingroup$

I recall that the diagonal of a rectangular matrix is $a_{1,1},a_{2,2},\cdots a_{q,q}$ where $q:=min(C,R)$.

Let us assume that your internal representation of a matrix is a vector (one dimensional array) of concatenated rows: row $1$ then row $2$, etc.

Then diagonal entries $a_{ii}$ are retrieved at "addresses" given by the following formula:

$$1+k*(R+1) \ \ \ \ \text{ for} \ \ \ \ k=0,1\cdots (q-1).$$

($q:=min(C,R)$ as defined above).

An example with $C=5$ and $R=4$:

$$\matrix{\boxed{1}&2&3&4&5\\6&\boxed{7}&8&9&10\\11&12&\boxed{13}&14&15\\16&17&18&\boxed{19}&20}\ \ \rightarrow \ \ \boxed{1} \ 2\ 3 \ 4 \ 5 \ 6 \ \boxed{7} \ 8 \ 9 \ 10 \ 11 \ 12 \ \boxed{13} \ 14 \cdots$$

$\endgroup$
2
  • $\begingroup$ Thanks for the comments. I see what you meant that the internal representation of the matrix also matters. In this linear representation then the code would be $\endgroup$
    – qboomerang
    Commented Nov 19, 2016 at 12:13
  • $\begingroup$ -implemented in one loop, I will update the initial question for future reference $\endgroup$
    – qboomerang
    Commented Nov 19, 2016 at 12:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .