Deep RTS
matrix.h
Go to the documentation of this file.
1/* =================================================================================================
2
3(c - MIT) T.W.J. de Geus (Tom) | tom@geus.me | www.geus.me | github.com/tdegeus/cppmat
4
5================================================================================================= */
6
7#ifndef CPPMAT_MATRIX_H
8#define CPPMAT_MATRIX_H
9
10#include <vector>
11#include <cassert>
12#include <algorithm>
13#include <iomanip>
14
15namespace cppmat {
16
17#define MAX_DIM 6
18
19// =================================================================================================
20// cppmat::matrix
21// =================================================================================================
22
23template<class X>
24class matrix
25{
26private:
27
28 std::vector<X> m_data; // data container
29 size_t m_ndim=0; // actual number of dimensions
30 size_t m_size=0; // total number of entries == data.size() == prod(shape)
31 size_t m_shape[MAX_DIM]; // number of entries in each dimensions
32 size_t m_strides[MAX_DIM]; // stride length for each index
33
34public:
35
36 // constructors
37 matrix(){};
38 matrix(const std::vector<size_t> &shape);
39 matrix(const std::vector<size_t> &shape, X D);
40
41 template<typename Iterator>
42 matrix(const std::vector<size_t> &shape, Iterator first, Iterator last);
43
44 // resize
45 void resize (const std::vector<size_t> &shape);
46 void reshape(const std::vector<size_t> &shape);
47 void chdim (size_t ndim);
48
49 // get dimensions
50 size_t size() const;
51 size_t ndim() const;
52 size_t shape(size_t i) const;
53 std::vector<size_t> shape() const;
54 std::vector<size_t> strides(bool bytes=false) const;
55
56 // index operators: access plain storage
57 X& operator[](size_t i);
58 const X& operator[](size_t i) const;
59
60 // index operators: access using matrix indices
61 X& operator()(size_t a);
62 const X& operator()(size_t a) const;
63 X& operator()(size_t a, size_t b);
64 const X& operator()(size_t a, size_t b) const;
65 X& operator()(size_t a, size_t b, size_t c);
66 const X& operator()(size_t a, size_t b, size_t c) const;
67 X& operator()(size_t a, size_t b, size_t c, size_t d);
68 const X& operator()(size_t a, size_t b, size_t c, size_t d) const;
69 X& operator()(size_t a, size_t b, size_t c, size_t d, size_t e);
70 const X& operator()(size_t a, size_t b, size_t c, size_t d, size_t e) const;
71 X& operator()(size_t a, size_t b, size_t c, size_t d, size_t e, size_t f);
72 const X& operator()(size_t a, size_t b, size_t c, size_t d, size_t e, size_t f) const;
73
74 // pointer / iterators
75 X* data();
76 const X* data() const;
77 auto begin();
78 auto begin() const;
79 auto end();
80 auto end() const;
81
82 // basic initialization
83 void setConstant(X D);
84 void setZero();
85 void setOnes();
86 void zeros();
87 void ones();
88
89 // arithmetic operators
94 matrix<X>& operator*= (const X &B);
95 matrix<X>& operator/= (const X &B);
96 matrix<X>& operator+= (const X &B);
97 matrix<X>& operator-= (const X &B);
98
99 // basic algebra
100 X min() const;
101 X max() const;
102 X sum() const;
103 double mean() const;
104 double average(const matrix<X> &weights) const;
105
106 // formatted print; NB also "operator<<" is defined
107 void printf(std::string fmt) const;
108
109};
110
111// arithmetic operators
112template<class X> inline matrix<X> operator* (const matrix<X> &A, const matrix<X> &B);
113template<class X> inline matrix<X> operator/ (const matrix<X> &A, const matrix<X> &B);
114template<class X> inline matrix<X> operator+ (const matrix<X> &A, const matrix<X> &B);
115template<class X> inline matrix<X> operator- (const matrix<X> &A, const matrix<X> &B);
116template<class X> inline matrix<X> operator* (const matrix<X> &A, const X &B);
117template<class X> inline matrix<X> operator/ (const matrix<X> &A, const X &B);
118template<class X> inline matrix<X> operator+ (const matrix<X> &A, const X &B);
119template<class X> inline matrix<X> operator- (const matrix<X> &A, const X &B);
120template<class X> inline matrix<X> operator* (const X &A, const matrix<X> &B);
121template<class X> inline matrix<X> operator/ (const X &A, const matrix<X> &B);
122template<class X> inline matrix<X> operator+ (const X &A, const matrix<X> &B);
123template<class X> inline matrix<X> operator- (const X &A, const matrix<X> &B);
124
125// =================================================================================================
126
127} // namespace ...
128
129#endif
130
Definition: matrix.h:25
matrix< X > & operator*=(const matrix< X > &B)
const X & operator[](size_t i) const
size_t shape(size_t i) const
void chdim(size_t ndim)
matrix(const std::vector< size_t > &shape, X D)
const X & operator()(size_t a, size_t b, size_t c, size_t d) const
matrix< X > & operator/=(const matrix< X > &B)
const X & operator()(size_t a, size_t b, size_t c, size_t d, size_t e, size_t f) const
X & operator()(size_t a, size_t b, size_t c, size_t d)
matrix< X > & operator+=(const matrix< X > &B)
const X & operator()(size_t a, size_t b, size_t c, size_t d, size_t e) const
X sum() const
const X & operator()(size_t a) const
auto begin() const
X & operator[](size_t i)
X max() const
void reshape(const std::vector< size_t > &shape)
size_t ndim() const
matrix< X > & operator-=(const matrix< X > &B)
X min() const
X & operator()(size_t a, size_t b, size_t c, size_t d, size_t e)
matrix(const std::vector< size_t > &shape, Iterator first, Iterator last)
matrix()
Definition: matrix.h:37
double average(const matrix< X > &weights) const
X & operator()(size_t a, size_t b, size_t c, size_t d, size_t e, size_t f)
X & operator()(size_t a, size_t b)
size_t size() const
X & operator()(size_t a, size_t b, size_t c)
void resize(const std::vector< size_t > &shape)
std::vector< size_t > shape() const
std::vector< size_t > strides(bool bytes=false) const
double mean() const
X & operator()(size_t a)
void setConstant(X D)
const X & operator()(size_t a, size_t b, size_t c) const
matrix(const std::vector< size_t > &shape)
const X * data() const
void printf(std::string fmt) const
const X & operator()(size_t a, size_t b) const
auto end() const
#define MAX_DIM
Definition: matrix.h:17
Definition: matrix.h:15
matrix< X > operator+(const matrix< X > &A, const matrix< X > &B)
matrix< X > operator-(const matrix< X > &A, const matrix< X > &B)
matrix< X > operator*(const matrix< X > &A, const matrix< X > &B)
matrix< X > operator/(const matrix< X > &A, const matrix< X > &B)