Rcpp Version 1.0.14
Loading...
Searching...
No Matches
diag.h
Go to the documentation of this file.
1
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2
//
3
// diag.h: Rcpp R/C++ interface class library -- diag
4
//
5
// Copyright (C) 2010 - 2011 Dirk Eddelbuettel and Romain Francois
6
//
7
// This file is part of Rcpp.
8
//
9
// Rcpp is free software: you can redistribute it and/or modify it
10
// under the terms of the GNU General Public License as published by
11
// the Free Software Foundation, either version 2 of the License, or
12
// (at your option) any later version.
13
//
14
// Rcpp is distributed in the hope that it will be useful, but
15
// WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
// GNU General Public License for more details.
18
//
19
// You should have received a copy of the GNU General Public License
20
// along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21
22
#ifndef Rcpp__sugar__diag_h
23
#define Rcpp__sugar__diag_h
24
25
namespace
Rcpp
{
26
namespace
sugar{
27
28
template
<
int
RTYPE,
bool
NA,
typename
T>
29
class
Diag_Extractor
:
public
Rcpp::VectorBase
< RTYPE ,NA, Diag_Extractor<RTYPE,NA,T> > {
30
public
:
31
typedef
typename
Rcpp::MatrixBase<RTYPE,NA,T>
MAT_TYPE
;
32
typedef
typename
Rcpp::traits::storage_type<RTYPE>::type
STORAGE
;
33
34
Diag_Extractor
(
const
MAT_TYPE
&
object_
) :
object
(
object_
),
n
(0) {
35
int
nr =
object
.nrow() ;
36
int
nc =
object
.ncol() ;
37
n
= (nc < nr ) ? nc : nr ;
38
}
39
40
inline
STORAGE
operator[]
(
int
i )
const
{
41
return
object
( i, i ) ;
42
}
43
inline
R_xlen_t
size
()
const
{
return
n
; }
44
45
private
:
46
const
MAT_TYPE
&
object
;
47
R_xlen_t
n
;
48
} ;
49
50
51
template
<
int
RTYPE,
bool
NA,
typename
T>
52
class
Diag_Maker
:
public
Rcpp::MatrixBase
< RTYPE ,NA, Diag_Maker<RTYPE,NA,T> > {
53
public
:
54
typedef
typename
Rcpp::VectorBase<RTYPE,NA,T>
VEC_TYPE
;
55
typedef
typename
Rcpp::traits::storage_type<RTYPE>::type
STORAGE
;
56
57
Diag_Maker
(
const
VEC_TYPE
&
object_
) :
object
(
object_
),
n
(
object_
.
size
()) {}
58
59
inline
STORAGE
operator()
(
int
i,
int
j )
const
{
60
return
(i==j) ?
object
[i] : 0 ;
61
}
62
inline
R_xlen_t
size
()
const
{
return
static_cast<
R_xlen_t
>
(
n
) *
n
; }
63
inline
int
ncol
()
const
{
return
n
; }
64
inline
int
nrow
()
const
{
return
n
; }
65
66
private
:
67
const
VEC_TYPE
&
object
;
68
int
n
;
69
} ;
70
71
template
<
typename
T>
struct
diag_result_type_trait
{
72
typedef
typename
Rcpp::traits::if_
<
73
Rcpp::traits::matrix_interface<T>::value
,
74
Diag_Extractor< T::r_type::value , T::can_have_na::value , T >
,
75
Diag_Maker< T::r_type::value , T::can_have_na::value , T >
76
>
::type
type
;
77
} ;
78
79
}
// sugar
80
81
template
<
typename
T>
82
inline
typename
sugar::diag_result_type_trait<T>::type
83
diag
(
const
T& t ){
84
return
typename
sugar::diag_result_type_trait<T>::type
( t ) ;
85
}
86
87
88
}
// Rcpp
89
#endif
90
Rcpp::MatrixBase
Definition
MatrixBase.h:32
Rcpp::VectorBase
Definition
VectorBase.h:29
Rcpp::object
Definition
Module.h:65
Rcpp::sugar::Diag_Extractor
Definition
diag.h:29
Rcpp::sugar::Diag_Extractor::size
R_xlen_t size() const
Definition
diag.h:43
Rcpp::sugar::Diag_Extractor::STORAGE
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition
diag.h:32
Rcpp::sugar::Diag_Extractor::MAT_TYPE
Rcpp::MatrixBase< RTYPE, NA, T > MAT_TYPE
Definition
diag.h:31
Rcpp::sugar::Diag_Extractor::n
R_xlen_t n
Definition
diag.h:47
Rcpp::sugar::Diag_Extractor::Diag_Extractor
Diag_Extractor(const MAT_TYPE &object_)
Definition
diag.h:34
Rcpp::sugar::Diag_Extractor::operator[]
STORAGE operator[](int i) const
Definition
diag.h:40
Rcpp::sugar::Diag_Extractor::object
const MAT_TYPE & object
Definition
diag.h:46
Rcpp::sugar::Diag_Maker
Definition
diag.h:52
Rcpp::sugar::Diag_Maker::STORAGE
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition
diag.h:55
Rcpp::sugar::Diag_Maker::operator()
STORAGE operator()(int i, int j) const
Definition
diag.h:59
Rcpp::sugar::Diag_Maker::VEC_TYPE
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition
diag.h:54
Rcpp::sugar::Diag_Maker::n
int n
Definition
diag.h:68
Rcpp::sugar::Diag_Maker::Diag_Maker
Diag_Maker(const VEC_TYPE &object_)
Definition
diag.h:57
Rcpp::sugar::Diag_Maker::nrow
int nrow() const
Definition
diag.h:64
Rcpp::sugar::Diag_Maker::size
R_xlen_t size() const
Definition
diag.h:62
Rcpp::sugar::Diag_Maker::ncol
int ncol() const
Definition
diag.h:63
Rcpp::sugar::Diag_Maker::object
const VEC_TYPE & object
Definition
diag.h:67
Rcpp
Rcpp API.
Definition
algo.h:28
Rcpp::diag
sugar::diag_result_type_trait< T >::type diag(const T &t)
Definition
diag.h:83
Rcpp::as
T as(SEXP x)
Definition
as.h:151
Rcpp::sugar::diag_result_type_trait
Definition
diag.h:71
Rcpp::sugar::diag_result_type_trait::type
Rcpp::traits::if_< Rcpp::traits::matrix_interface< T >::value, Diag_Extractor< T::r_type::value, T::can_have_na::value, T >, Diag_Maker< T::r_type::value, T::can_have_na::value, T > >::type type
Definition
diag.h:76
Rcpp::traits::if_
Definition
if_.h:29
Rcpp::traits::matrix_interface
Definition
matrix_interface.h:47
Rcpp::traits::storage_type::type
SEXP type
Definition
storage_type.h:36
inst
include
Rcpp
sugar
matrix
diag.h
Generated on Sun Jan 12 2025 11:21:43 for Rcpp Version 1.0.14 by
1.9.8