| #pragma once |
| |
| #ifndef TLIN_MATRIX_H |
| #define TLIN_MATRIX_H |
| |
| #include "assert.h" |
| |
| #include "tlin_basicops.h" |
| |
| namespace tlin { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| template <typename T> |
| class matrix { |
| int m_rows; |
| int m_cols; |
| |
| T *m_entries; |
| |
| public: |
| matrix() : m_rows(0), m_cols(0), m_entries(0) {} |
| matrix(int rows, int cols) |
| : m_rows(rows), m_cols(cols), m_entries(new T[rows * cols]) { |
| memset(m_entries, 0, m_rows * m_cols * sizeof(T)); |
| } |
| ~matrix() { delete[] m_entries; } |
| |
| matrix(const matrix &mat) |
| : m_rows(mat.m_rows) |
| , m_cols(mat.m_cols) |
| , m_entries(new T[m_rows * m_cols]) { |
| memcpy(m_entries, mat.m_entries, m_rows * m_cols * sizeof(T)); |
| } |
| |
| matrix &operator=(const matrix &mat) { |
| if (m_rows != mat.m_rows || m_cols != mat.m_cols) { |
| delete[] m_entries; |
| m_rows = mat.m_rows, m_cols = mat.m_cols, |
| m_entries = new T[m_rows * m_cols]; |
| } |
| |
| memcpy(m_entries, mat.m_entries, m_rows * m_cols * sizeof(T)); |
| return *this; |
| } |
| |
| int rows() const { return m_rows; } |
| int cols() const { return m_cols; } |
| |
| T &at(int row, int col) { return m_entries[m_rows * col + row]; } |
| const T &get(int row, int col) const { return m_entries[m_rows * col + row]; } |
| const T &operator()(int row, int col) const { return get(row, col); } |
| |
| |
| |
| |
| matrix(int rows, int cols, T val) |
| : m_rows(rows), m_cols(cols), m_entries(new T[rows * cols]) { |
| fill(val); |
| } |
| |
| T *values() { return m_entries; } |
| const T *values() const { return m_entries; } |
| |
| void fill(const T &val) { |
| memset(m_entries, val, m_rows * m_cols * sizeof(T)); |
| } |
| }; |
| |
| |
| |
| |
| |
| typedef tlin::matrix<double> mat; |
| |
| } |
| |
| #endif |