|
Shinya Kitaoka |
810553 |
#pragma once
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef TLIN_MATRIX_H
|
|
Toshihiro Shimizu |
890ddd |
#define TLIN_MATRIX_H
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "assert.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tlin_basicops.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
namespace tlin {
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//----------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
/*!
|
|
Toshihiro Shimizu |
890ddd |
The tlin::matrix class represents matrices in tlin-compatible algorithms.
|
|
Toshihiro Shimizu |
890ddd |
This implementation both serves as a reference to other would-be matrix types,
|
|
Shinya Kitaoka |
120a6e |
specifying the core methods they must provide to work in place of a
|
|
Shinya Kitaoka |
120a6e |
tcg::matrix;
|
|
Toshihiro Shimizu |
890ddd |
plus, it provides the naive, universal dense-format of a matrix type.
|
|
Shinya Kitaoka |
120a6e |
Please, observe that a tlin::matrix is stored in column-major order, unlike
|
|
Shinya Kitaoka |
120a6e |
common
|
|
Shinya Kitaoka |
120a6e |
C matrices, since it has to comply with the usual Fortran-style matrices
|
|
Shinya Kitaoka |
120a6e |
supported
|
|
Toshihiro Shimizu |
890ddd |
by every BLAS implementation. In practice, this means that the values() array
|
|
Toshihiro Shimizu |
890ddd |
stores \b columns in consecutive data blocks.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Shinya Kitaoka |
120a6e |
class matrix {
|
|
Shinya Kitaoka |
120a6e |
int m_rows;
|
|
Shinya Kitaoka |
120a6e |
int m_cols;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
T *m_entries;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Shinya Kitaoka |
120a6e |
matrix() : m_rows(0), m_cols(0), m_entries(0) {}
|
|
Shinya Kitaoka |
120a6e |
matrix(int rows, int cols)
|
|
Shinya Kitaoka |
120a6e |
: m_rows(rows), m_cols(cols), m_entries(new T[rows * cols]) {
|
|
Shinya Kitaoka |
120a6e |
memset(m_entries, 0, m_rows * m_cols * sizeof(T));
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
~matrix() { delete[] m_entries; }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
matrix(const matrix &mat)
|
|
Shinya Kitaoka |
120a6e |
: m_rows(mat.m_rows)
|
|
Shinya Kitaoka |
120a6e |
, m_cols(mat.m_cols)
|
|
Shinya Kitaoka |
120a6e |
, m_entries(new T[m_rows * m_cols]) {
|
|
Shinya Kitaoka |
120a6e |
memcpy(m_entries, mat.m_entries, m_rows * m_cols * sizeof(T));
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
matrix &operator=(const matrix &mat) {
|
|
Shinya Kitaoka |
120a6e |
if (m_rows != mat.m_rows || m_cols != mat.m_cols) {
|
|
Shinya Kitaoka |
120a6e |
delete[] m_entries;
|
|
Shinya Kitaoka |
120a6e |
m_rows = mat.m_rows, m_cols = mat.m_cols,
|
|
Shinya Kitaoka |
120a6e |
m_entries = new T[m_rows * m_cols];
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
memcpy(m_entries, mat.m_entries, m_rows * m_cols * sizeof(T));
|
|
Shinya Kitaoka |
120a6e |
return *this;
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
int rows() const { return m_rows; }
|
|
Shinya Kitaoka |
120a6e |
int cols() const { return m_cols; }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
T &at(int row, int col) { return m_entries[m_rows * col + row]; }
|
|
Shinya Kitaoka |
120a6e |
const T &get(int row, int col) const { return m_entries[m_rows * col + row]; }
|
|
Shinya Kitaoka |
120a6e |
const T &operator()(int row, int col) const { return get(row, col); }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
//-----------------------------------------------------------------------------
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
// Dense-specific methods
|
|
Shinya Kitaoka |
120a6e |
matrix(int rows, int cols, T val)
|
|
Shinya Kitaoka |
120a6e |
: m_rows(rows), m_cols(cols), m_entries(new T[rows * cols]) {
|
|
Shinya Kitaoka |
120a6e |
fill(val);
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
T *values() { return m_entries; }
|
|
Shinya Kitaoka |
120a6e |
const T *values() const { return m_entries; }
|
|
Shinya Kitaoka |
120a6e |
|
|
Shinya Kitaoka |
120a6e |
void fill(const T &val) {
|
|
Shinya Kitaoka |
120a6e |
memset(m_entries, val, m_rows * m_cols * sizeof(T));
|
|
Shinya Kitaoka |
120a6e |
}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//-------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// The Standard matrix data type in tlin is double
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
typedef tlin::matrix<double> mat;</double>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
} // namespace tlin
|
|
Toshihiro Shimizu |
890ddd |
|
|
Shinya Kitaoka |
120a6e |
#endif // TLIN_MATRIX_H
|