File: Matrix.cpp

package info (click to toggle)
rcpp 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,480 kB
  • sloc: cpp: 27,436; ansic: 7,778; sh: 53; makefile: 2
file content (455 lines) | stat: -rw-r--r-- 11,281 bytes parent folder | download | duplicates (4)
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
//
// Matrix.cpp: Rcpp R/C++ interface class library -- Matrix unit tests
//
// Copyright (C) 2013 - 2016  Dirk Eddelbuettel, Romain Francois and Kevin Ushey
//
// This file is part of Rcpp.
//
// Rcpp is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Rcpp is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.

#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_opequals(SEXP x) {
  NumericMatrix xx;
  xx = NumericMatrix(x);
  return xx;
}

// [[Rcpp::export]]
IntegerMatrix matrix_integer_diag(){
    return IntegerMatrix::diag( 5, 1 ) ;
}

// [[Rcpp::export]]
CharacterMatrix matrix_character_diag(){
    return CharacterMatrix::diag( 5, "foo" ) ;
}

// [[Rcpp::export]]
NumericMatrix matrix_numeric_ctor1(){
    return NumericMatrix(3);
}

// [[Rcpp::export]]
NumericMatrix matrix_numeric_ctor2(){
    return NumericMatrix(3,3);
}

// [[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]]
IntegerVector integer_matrix_indexing_lhs( IntegerVector 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]]
double runit_NumericMatrix_row_const( const NumericMatrix m){
    NumericMatrix::ConstRow 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]]
std::string runit_CharacterMatrix_row_const( const CharacterMatrix m ){
    CharacterMatrix::ConstRow first_row = m.row(0) ;
    std::string res(
    	std::accumulate(
    		first_row.begin(), first_row.end(), std::string() ) ) ;
    return res ;
}

// [[Rcpp::export]]
IntegerVector runit_GenericMatrix_row( GenericMatrix m ){
    GenericMatrix::Row first_row = m.row(0) ;
    IntegerVector out( first_row.size() ) ;
    std::transform(
    	first_row.begin(), first_row.end(),
    	out.begin(),
    	unary_call<SEXP,int>( Function("length" ) ) ) ;
    return out ;
}

// [[Rcpp::export]]
IntegerVector runit_GenericMatrix_row_const( const GenericMatrix m ){
    GenericMatrix::ConstRow first_row = m.row(0) ;
    IntegerVector out( first_row.size() ) ;
    std::transform(
    	first_row.begin(), first_row.end(),
    	out.begin(),
    	unary_call<SEXP,int>( Function("length" ) ) ) ;
    return out ;
}

// [[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]]
double runit_NumericMatrix_column_const( const NumericMatrix m ){
    NumericMatrix::ConstColumn 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 );
    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() ) ;
    }
    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]]
std::string runit_CharacterMatrix_column_const( const CharacterMatrix m){
    CharacterMatrix::ConstColumn col = m.column(0) ;
    std::string res(
        std::accumulate( col.begin(), col.end(), std::string() )
    ) ;
    return res ;
}

// [[Rcpp::export]]
IntegerVector runit_GenericMatrix_column( GenericMatrix m ){
    GenericMatrix::Column col = m.column(0) ;
    IntegerVector out( col.size() ) ;
    std::transform(
    	   col.begin(), col.end(),
    	   out.begin(),
    	   unary_call<SEXP,int>( Function("length" ) )
    ) ;
    return wrap(out) ;
}

// [[Rcpp::export]]
IntegerVector runit_GenericMatrix_column_const( const GenericMatrix m ){
    GenericMatrix::ConstColumn col = m.column(0) ;
    IntegerVector out( col.size() ) ;
    std::transform(
    	   col.begin(), col.end(),
    	   out.begin(),
    	   unary_call<SEXP,int>( Function("length" ) )
    ) ;
    return wrap(out) ;
}

// [[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]]
NumericMatrix runit_SubMatrix( ){
    NumericMatrix xx(4, 5);
    xx(0,0) = 3;
    xx(0,1) = 4;
    xx(0,2) = 5;
    xx(1,_) = xx(0,_);
    xx(_,3) = xx(_,2);
    SubMatrix<REALSXP> yy = xx( Range(0,2), Range(0,3) ) ;
    NumericMatrix res = yy ;
    return res;
}

// [[Rcpp::export]]
void runit_rownames_colnames_proxy(
    NumericMatrix x, CharacterVector row_names, CharacterVector col_names) {
    rownames(x) = row_names;
    colnames(x) = col_names;
}

// [[Rcpp::export]]
void runit_rownames_proxy(NumericMatrix x) {
    rownames(x) = CharacterVector::create("A", "B", "C");
}

// [[Rcpp::export]]
NumericMatrix runit_no_init_matrix() {
    NumericMatrix x = no_init(2, 2);
    for (int i = 0; i < 4; i++) {
        x[i] = i;
    }
    return x;
}

// [[Rcpp::export]]
NumericMatrix runit_no_init_matrix_ctor() {
    NumericMatrix x(no_init(2, 2));
    for (int i = 0; i < 4; i++) {
        x[i] = i;
    }
    return x;
}

// [[Rcpp::export]]
int runit_no_init_matrix_ctor_nrow() {
    NumericMatrix x(no_init(2, 2));
    return x.nrow();
}

void runit_const_Matrix_column_set( NumericMatrix::Column& col1, const NumericMatrix::Column& col2 ){
    col1 = col2 ;
}

// [[Rcpp::export]]
NumericVector runit_const_Matrix_column( const NumericMatrix& m ){
   NumericMatrix::Column col1( m, 0 ) ;
   NumericMatrix::Column col2( m, 1 ) ;
   runit_const_Matrix_column_set(col1, col2) ;
   return col1 ;
}

// [[Rcpp::export]]
int mat_access_with_bounds_checking(const IntegerMatrix m, int i, int j) {
    return m.at(i, j);
}


// [[Rcpp::export]]
IntegerMatrix transposeInteger(const IntegerMatrix & x) {
    return transpose(x);
}

// [[Rcpp::export]]
NumericMatrix transposeNumeric(const NumericMatrix & x) {
    return transpose(x);
}

// [[Rcpp::export]]
CharacterMatrix transposeCharacter(const CharacterMatrix & x) {
    return transpose(x);
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_plus(const NumericMatrix & x, double y) {
    return x + y;
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_plus2(const NumericMatrix & x, double y) {
    return y + x;
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_plus3(const NumericMatrix & x, double y) {
    NumericMatrix z(x.rows(), x.cols());
    z = x + y;
    return x;
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_plus4(const NumericMatrix & x, double y) {
    NumericMatrix z(x.rows(), x.cols());
    z = y + x;
    return x;
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_divide(const NumericMatrix & x, double y) {
    return x / y;
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_divide2(const NumericMatrix & x, double y) {
    return y / x;
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_divide3(const NumericMatrix & x, double y) {
    NumericMatrix z(x.rows(), x.cols());
    z = x / y;
    return x;
}

// [[Rcpp::export]]
NumericMatrix matrix_scalar_divide4(const NumericMatrix & x, double y) {
    NumericMatrix z(x.rows(), x.cols());
    z = y / x;
    return x;
}

// 24 October 2016
// eye, ones, and zeros static member functions

// [[Rcpp::export]]
Rcpp::NumericMatrix dbl_eye(int n) {
    return Rcpp::NumericMatrix::eye(n);
}

// [[Rcpp::export]]
Rcpp::IntegerMatrix int_eye(int n) {
    return Rcpp::IntegerMatrix::eye(n);
}

// [[Rcpp::export]]
Rcpp::ComplexMatrix cx_eye(int n) {
    return Rcpp::ComplexMatrix::eye(n);
}

// [[Rcpp::export]]
Rcpp::LogicalMatrix lgl_eye(int n) {
    return Rcpp::LogicalMatrix::eye(n);
}


// [[Rcpp::export]]
Rcpp::NumericMatrix dbl_ones(int n) {
    return Rcpp::NumericMatrix::ones(n);
}

// [[Rcpp::export]]
Rcpp::IntegerMatrix int_ones(int n) {
    return Rcpp::IntegerMatrix::ones(n);
}

// [[Rcpp::export]]
Rcpp::ComplexMatrix cx_ones(int n) {
    return Rcpp::ComplexMatrix::ones(n);
}

// [[Rcpp::export]]
Rcpp::LogicalMatrix lgl_ones(int n) {
    return Rcpp::LogicalMatrix::ones(n);
}


// [[Rcpp::export]]
Rcpp::NumericMatrix dbl_zeros(int n) {
    return Rcpp::NumericMatrix::zeros(n);
}

// [[Rcpp::export]]
Rcpp::IntegerMatrix int_zeros(int n) {
    return Rcpp::IntegerMatrix::zeros(n);
}

// [[Rcpp::export]]
Rcpp::ComplexMatrix cx_zeros(int n) {
    return Rcpp::ComplexMatrix::zeros(n);
}

// [[Rcpp::export]]
Rcpp::LogicalMatrix lgl_zeros(int n) {
    return Rcpp::LogicalMatrix::zeros(n);
}

// --- Diagonal Fill

// [[Rcpp::export]]
Rcpp::NumericMatrix num_diag_fill(Rcpp::NumericMatrix x, double diag_val) {
    x.fill_diag(diag_val);
    return x;
}

// [[Rcpp::export]]
Rcpp::CharacterMatrix char_diag_fill(Rcpp::CharacterMatrix x, std::string diag_val) {
    x.fill_diag(diag_val);
    return x;
}