-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.h
More file actions
158 lines (127 loc) · 4.95 KB
/
Copy pathMatrix.h
File metadata and controls
158 lines (127 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef Rcpp__vector__Matrix_h
#define Rcpp__vector__Matrix_h
namespace Rcpp{
template <int RTYPE, typename Storage>
class Matrix :
public SugarMatrixExpression< typename Vector<RTYPE>::value_type, Matrix<RTYPE,Storage> >
{
private:
typedef Vector<RTYPE,Storage> Vec ;
Vec vec ;
int* dims ;
public:
typedef typename Vector<RTYPE,Storage>::Proxy Proxy;
typedef typename Vector<RTYPE,Storage>::iterator iterator;
typedef typename Vector<RTYPE,Storage>::const_iterator const_iterator;
typedef MatrixColumn<RTYPE, Matrix> Column;
typedef MatrixRow<RTYPE, Matrix> Row;
Matrix(int nr, int nc) : vec(nr*nc){
set_dimensions(nr,nc) ;
}
template <typename U>
Matrix(int nr, int nc, const U& u) : vec(nr*nc, u){
set_dimensions(nr,nc) ;
}
Matrix() : vec(0){
set_dimensions(0,0) ;
}
Matrix( SEXP x ){
SEXP d = Rf_getAttrib(x,R_DimSymbol) ;
if( d == R_NilValue || Rf_xlength(d) != 2)
stop("not a matrix") ;
vec = x ;
dims = INTEGER(d) ;
}
Matrix( const Matrix& other ) = default ;
Matrix& operator=( const Matrix& ) = default ;
template <typename eT, typename Expr>
Matrix( const SugarMatrixExpression<eT,Expr>& expr ) : vec(expr.nrow() * expr.ncol()) {
set_dimensions( expr.nrow(), expr.ncol() ) ;
expr.apply(*this) ;
}
template <typename eT, typename Expr>
Matrix& operator=( const SugarMatrixExpression<eT,Expr>& expr ) {
if( nrow() != expr.nrow() || ncol() != expr.ncol() ) {
stop("incompatible dimensions, expecting (%d,%d), got (%d,%d)", nrow(), ncol(), expr.nrow(), expr.ncol()) ;
}
expr.apply(*this) ;
return *this ;
}
inline operator SEXP() const { return vec ; }
inline int nrow() const { return dims[0] ; }
inline int ncol() const { return dims[1] ; }
inline R_xlen_t size() const { return vec.size() ; }
inline iterator begin(){
RCPP_DEBUG( "Matrix::begin() = %p", vec.begin() ) ;
return vec.begin() ;
}
inline iterator end(){ return vec.end(); }
inline const iterator begin() const {
RCPP_DEBUG( "Matrix::begin() const = %p", vec.begin() ) ;
return vec.begin() ;
}
inline const iterator end() const { return vec.end(); }
inline Proxy operator[](R_xlen_t i){ return vec[i] ; }
inline const Proxy operator[](R_xlen_t i) const{ return vec[i] ; }
inline Proxy operator()(int i, int j) {
return vec[offset(i,j)] ;
}
inline const Proxy operator()(int i, int j) const {
return const_cast<Vec&>(vec)[offset(i,j)] ;
}
inline Column column(int i){ return Column(*this, i) ; }
inline Column operator()(internal::NamedPlaceHolder, int i){ return column(i); }
inline Row row(int i){ return Row(*this, i) ; }
inline Row operator()(int i, internal::NamedPlaceHolder){ return row(i); }
template <typename T>
inline Matrix& operator+=( T x ){
for( auto& val: *this){
val += x;
}
return *this ;
}
template <typename T>
inline Matrix& operator*=( T x ){
for( auto& val: *this){
val *= x;
}
return *this ;
}
template <typename T>
inline Matrix& operator/=( T x ){
for( auto& val: *this){
val /= x;
}
return *this ;
}
template <typename T>
inline Matrix& operator-=( T x ){
for( auto& val: *this){
val -= x;
}
return *this ;
}
private:
inline R_xlen_t offset(int i, int j) const {
return i + nrow()*j ;
}
inline void set_dimensions(int nr, int nc){
Shield<SEXP> dim = Rf_allocVector(INTSXP, 2 ) ;
dims = INTEGER(dim) ;
dims[0] = nr ;
dims[1] = nc ;
Rf_setAttrib(vec, R_DimSymbol, dim ) ;
}
} ;
typedef Matrix<CPLXSXP> ComplexMatrix ;
typedef Matrix<INTSXP> IntegerMatrix ;
typedef Matrix<LGLSXP> LogicalMatrix ;
typedef Matrix<REALSXP> NumericMatrix ;
typedef Matrix<REALSXP> DoubleMatrix ;
typedef Matrix<RAWSXP> RawMatrix ;
typedef Matrix<STRSXP> CharacterMatrix ;
typedef Matrix<STRSXP> StringMatrix ;
typedef Matrix<VECSXP> GenericMatrix ;
typedef Matrix<EXPRSXP> ExpressionMatrix ;
}
#endif