Thread: need help to transpose a matrix

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    1

    need help to transpose a matrix

    Dear C++ users,

    I am using the transpose function in a header file from the Scythe library, named la.h, to transpose a matrix. Here is what is in the la.h:
    Code:
    001#ifndef SCYTHE_LA_H
    002#define SCYTHE_LA_H
    003
    004#include "matrix.h"
    005#include "algorithm.h"
    006#include "error.h"
    007#ifdef SCYTHE_LAPACK
    008#include "lapack.h"
    009#endif
    010
    011
    012#include <numeric>
    013#include <algorithm>
    014#include <set>
    015
    016namespace scythe {
    017
    018  namespace {
    019    typedef unsigned int uint;
    020  }
    021
    022  /* Matrix transposition */
    023
    024  /*!\brief Transpose a Matrix.
    025   *
    026   * This function transposes \a M, returning a Matrix \a R where each
    027   * element of \a M, \f$M_ij\f$ is placed in position \f$R_ji\f$.
    028   * Naturally, the returned Matrix has M.cols() rows and M.rows()
    029   * columns.
    030   *
    031   * \param M The Matrix to transpose.
    032   *
    033   * \throw scythe_alloc_error (Level 1)
    034   *
    035   */
    036  template <matrix_order RO,typename T,
    037            matrix_order PO>
    038  Matrix<T, RO, Concrete>
    039  t (const Matrix<T,PO,Concrete>& M)
    040  {
    041    uint rows = M.rows();
    042    uint cols = M.cols();
    043    Matrix<T,RO,Concrete> ret(cols, rows, false);
    044    if (PO == Col)
    045      copy<Col,Row>(M, ret);
    046    else
    047      copy<Row,Col>(M, ret);
    048
    049    SCYTHE_VIEW_RETURN(T, RO, Concrete, ret)
    050  }
    051  template <typename T, matrix_order O, matrix_style S>
    052  Matrix<T, O, Concrete>
    053  t (const Matrix<T,O,S>& M)
    054  {
    055    return t<T,O,Concrete>(M);
    056  }
    057} // end namespace scythe
    058
    059#endif /* SCYTHE_LA_H */
    Here is my main.cpp:
    Code:
    000#include <iostream>
    001#include "la.h"
    002#include "lapack.h"
    003#include "matrix.h"
    004using namespace scythe;
    005using namespace std;
    006int main()
    007{
    008    int val2[9]={2,2,2,2,2,2,2,2};
    009    const Matrix<int, Col, Concrete> SS(2,4,val2);
    010    Matrix<> MM = t(SS);
    011}
    And the error message I got is:
    la.h||In function `scythe::Matrix<T_type, ORDER, Concrete> scythe::t(const scythe::Matrix<T, O, S>&) [with T = int, scythe::matrix_order O = Col, scythe::matrix_style S = Concrete]':|
    main.cpp|11|instantiated from here|
    la.h|55|error: cannot convert `scythe::matrix_style' to `scythe::matrix_order' in initialization|
    la.h|55|error: non-constant `<expression error>' cannot be used as template argument|

    Can anyone help to figure out what is wrong here?

    Thank you!

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Matrix<> MM = t(SS);

    Remove the angle brackets, if you want the compiler to figure out the type of matrix MM is. Adding them in causes the compiler to expect correct expressions related to the type of MM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Matrix
    By alex 2010 in forum C++ Programming
    Replies: 0
    Last Post: 06-24-2010, 09:40 AM
  2. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  3. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  4. Help with matrix transpose
    By sass208 in forum C Programming
    Replies: 6
    Last Post: 12-09-2006, 02:02 AM