-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
198 lines (174 loc) · 4.77 KB
/
Matrix.cpp
File metadata and controls
198 lines (174 loc) · 4.77 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <Rcpp.h>
using namespace Rcpp ;
// [[Rcpp::export]]
double matrix_numeric( NumericMatrix m){
double trace = 0.0 ;
for( size_t i=0 ; i<4; i++){
trace += m(i,i) ;
}
return trace ;
}
// [[Rcpp::export]]
std::string matrix_character( CharacterMatrix m){
std::string trace ;
for( size_t i=0 ; i<4; i++){
trace += m(i,i) ;
}
return trace;
}
// [[Rcpp::export]]
List matrix_generic( GenericMatrix m){
List output( m.ncol() ) ;
for( size_t i=0 ; i<4; i++){
output[i] = m(i,i) ;
}
return output ;
}
// [[Rcpp::export]]
NumericMatrix matrix_numeric_ctor2(){
return NumericMatrix(3,3,0.0);
}
// [[Rcpp::export]]
int integer_matrix_indexing( IntegerMatrix m){
int trace = 0.0 ;
for( size_t i=0 ; i<4; i++){
trace += m(i,i) ;
}
return trace ;
}
// [[Rcpp::export]]
IntegerMatrix integer_matrix_indexing_lhs( IntegerMatrix m ){
for( size_t i=0 ; i<4; i++){
m(i,i) = 2 * i ;
}
return m ;
}
// [[Rcpp::export]]
double runit_NumericMatrix_row( NumericMatrix m){
NumericMatrix::Row first_row = m.row(0) ;
return std::accumulate( first_row.begin(), first_row.end(), 0.0 ) ;
}
// [[Rcpp::export]]
std::string runit_CharacterMatrix_row( CharacterMatrix m ){
CharacterMatrix::Row first_row = m.row(0) ;
std::string res(
std::accumulate(
first_row.begin(), first_row.end(), std::string() ) ) ;
return res ;
}
// [[Rcpp::export]]
double runit_NumericMatrix_column( NumericMatrix m ){
NumericMatrix::Column col = m.column(0) ;
return std::accumulate( col.begin(), col.end(), 0.0 ) ;
}
// [[Rcpp::export]]
NumericMatrix runit_NumericMatrix_cumsum( NumericMatrix input ){
int nr = input.nrow(), nc = input.ncol() ;
NumericMatrix output(nr, nc) ;
NumericVector tmp( nr, 0 );
for( int i=0; i<nc; i++){
tmp = tmp + input.column(i) ;
// NumericMatrix::Column target( output, i ) ;
// std::copy( tmp.begin(), tmp.end(), target.begin() ) ;
output(_,i) = tmp ;
}
return output ;
}
// [[Rcpp::export]]
std::string runit_CharacterMatrix_column( CharacterMatrix m){
CharacterMatrix::Column col = m.column(0) ;
std::string res(
std::accumulate( col.begin(), col.end(), std::string() )
) ;
return res ;
}
// [[Rcpp::export]]
List runit_Row_Column_sugar( NumericMatrix x){
NumericVector r0 = x.row(0) ;
NumericVector c0 = x.column(0) ;
return List::create(
r0,
c0,
x.row(1),
x.column(1),
x.row(1) + x.column(1)
) ;
}
// [[Rcpp::export]]
NumericMatrix runit_NumericMatrix_colsum( NumericMatrix input ){
int nc = input.ncol() ;
NumericMatrix output = clone<NumericMatrix>( input ) ;
for( int i=1; i<nc; i++){
output(_,i) = output(_,i-1) + input(_,i) ;
}
return output ;
}
// [[Rcpp::export]]
NumericMatrix runit_NumericMatrix_rowsum( NumericMatrix input ){
int nr = input.nrow();
NumericMatrix output = clone<NumericMatrix>( input ) ;
for( int i=1; i<nr; i++){
output(i,_) = output(i-1,_) + input(i,_) ;
}
return output ;
}
// [[Rcpp::export]]
CharacterVector character_matrix_indexing( CharacterMatrix m ){
std::string trace;
for( size_t i=0 ; i<4; i++){
trace += m(i,i) ;
}
return wrap( trace ) ;
}
// [[Rcpp::export]]
CharacterMatrix character_matrix_indexing_lhs( CharacterMatrix m ){
for( size_t i=0 ; i<4; i++){
m(i,i) = "foo" ;
}
return m ;
}
// [[Rcpp::export]]
CharacterVector character_matrix_row_iteration_incr( CharacterMatrix m ){
std::string pasted_row;
CharacterMatrix::Row row(m(1, _));
CharacterMatrix::Row::iterator i_row(row.begin());
for( size_t i=0 ; i<4; i++){
pasted_row += *i_row++;
}
return wrap( pasted_row ) ;
}
// [[Rcpp::export]]
CharacterVector character_matrix_row_iteration_decr( CharacterMatrix m ){
std::string pasted_row;
CharacterMatrix::Row row(m(1, _));
CharacterMatrix::Row::iterator i_row(row.end());
i_row--; // Step back from 'one past the end' to 'last element'.
// Only copy the last three elements, to avoid creating an invalid
// 'one before the beginning' iterator:
for( size_t i=0 ; i<3; i++){
pasted_row += *i_row--;
}
return wrap( pasted_row ) ;
}
// [[Rcpp::export]]
CharacterVector rownames_get(IntegerMatrix m){
return rownames(m) ;
}
// [[Rcpp::export]]
IntegerMatrix rownames_set(IntegerMatrix m){
rownames(m) = {"z","y","x","w","v"};
return m ;
}
// [[Rcpp::export]]
CharacterVector colnames_get(IntegerMatrix m){
return colnames(m) ;
}
// [[Rcpp::export]]
IntegerMatrix colnames_set(IntegerMatrix m){
colnames(m) = {"z","y","x","w"};
return m ;
}
// [[Rcpp::export]]
int test_SquareMatrix( SquareIntegerMatrix m ){
return 0 ;
}