fastglmm
Massively scalable generalized linear mixed models
Toggle main menu visibility
Loading...
Searching...
No Matches
inst
include
spectralDecomp.h
Go to the documentation of this file.
1
2
#ifndef SPECTRAL_DECOMP_H_
3
#define SPECTRAL_DECOMP_H_
4
5
// if -D USE_R, use RcppArmadillo library
6
#ifdef USE_R
7
#include <RcppArmadillo.h>
8
#else
9
#include <armadillo>
10
#endif
11
12
#include "
misc.h
"
13
14
using namespace
arma;
15
16
namespace
fastglmmLib
{
17
18
typedef
enum
{
19
GENERAL
,
20
CATEGORICAL
21
}
ZTYPE
;
22
28
template
<
typename
T>
29
class
spectralDecomp
{
30
31
public
:
32
spectralDecomp
() {};
33
38
spectralDecomp
(
39
const
T &
U
,
40
const
vec &
s
,
41
const
ZTYPE
&
type
=
GENERAL
) :
42
U
(
U
),
43
s
(
s
),
44
Z
(
scaleEachRow
(
U
, sqrt(
s
))),
45
type
(
type
)
46
{}
47
51
spectralDecomp
(
52
const
T &
Z
,
53
const
ZTYPE
&
type
=
GENERAL
) :
54
Z
(
Z
),
55
type
(
type
){
56
57
vec one(
Z
.n_rows, fill::ones);
58
reweight
(one);
59
}
60
61
// Copy constructor
62
spectralDecomp
(
const
spectralDecomp
& other):
63
U
(other.
U
),
64
s
(other.
s
),
65
Z
(other.
Z
),
66
Zw
(other.
Zw
),
67
V
(other.
V
),
68
type
(other.
type
){
69
}
70
76
void
reweight
(
const
vec &weights,
const
bool
&sort =
false
){
77
78
switch
(
type
){
79
case
GENERAL
:
80
Zw
=
scaleEachCol
(
Z
, sqrt(weights));
81
eig_sym(
s
,
V
, mat(
Zw
.t() *
Zw
) );
82
U
=
scaleEachRow
(
Zw
*
V
, 1 / sqrt(
s
));
83
break
;
84
case
CATEGORICAL
:
85
s
= (weights.t() *
Z
).t();
86
U
=
scaleRowsCols
(
Z
, sqrt(weights), 1 / sqrt(
s
));
87
break
;
88
}
89
90
// mat Z_recon = scaleRowsCols(U, 1 / sqrt(weights), sqrt(s)) * V.t();
91
// Z_recon.print("Z_recon:");
92
93
if
( sort ){
94
// reorder by decreasing eigen-value
95
uvec idx = sort_index(
s
,
"descend"
);
96
s
=
s
(idx);
97
U
=
U
.cols(idx);
98
}
99
}
100
102
T
get_U
()
const
{
103
return
U
;
104
}
105
107
vec
get_s
()
const
{
108
return
s
;
109
}
110
112
mat
get_V
()
const
{
113
114
mat V_ret;
115
116
switch
(
type
){
117
case
GENERAL
:
118
V_ret =
V
;
119
break
;
120
case
CATEGORICAL
:
121
V_ret = eye<mat>(
U
.n_cols,
U
.n_cols);
122
break
;
123
}
124
125
return
V_ret;
126
}
127
128
ZTYPE
get_type
()
const
{
129
return
type
;
130
}
131
132
protected
:
133
T
U
;
134
vec
s
;
135
T
Z
,
Zw
;
136
mat
V
;
137
ZTYPE
type
;
138
};
139
140
141
142
143
}
144
145
146
147
148
#endif
fastglmmLib::spectralDecomp::type
ZTYPE type
Definition
spectralDecomp.h:137
fastglmmLib::spectralDecomp::reweight
void reweight(const vec &weights, const bool &sort=false)
Definition
spectralDecomp.h:76
fastglmmLib::spectralDecomp::spectralDecomp
spectralDecomp(const T &Z, const ZTYPE &type=GENERAL)
Definition
spectralDecomp.h:51
fastglmmLib::spectralDecomp::spectralDecomp
spectralDecomp(const spectralDecomp &other)
Definition
spectralDecomp.h:62
fastglmmLib::spectralDecomp::Zw
T Zw
Definition
spectralDecomp.h:135
fastglmmLib::spectralDecomp::Z
T Z
Definition
spectralDecomp.h:135
fastglmmLib::spectralDecomp::get_V
mat get_V() const
Definition
spectralDecomp.h:112
fastglmmLib::spectralDecomp::get_s
vec get_s() const
Definition
spectralDecomp.h:107
fastglmmLib::spectralDecomp::get_type
ZTYPE get_type() const
Definition
spectralDecomp.h:128
fastglmmLib::spectralDecomp::spectralDecomp
spectralDecomp(const T &U, const vec &s, const ZTYPE &type=GENERAL)
Definition
spectralDecomp.h:38
fastglmmLib::spectralDecomp::U
T U
Definition
spectralDecomp.h:133
fastglmmLib::spectralDecomp::s
vec s
Definition
spectralDecomp.h:134
fastglmmLib::spectralDecomp::spectralDecomp
spectralDecomp()
Definition
spectralDecomp.h:32
fastglmmLib::spectralDecomp::get_U
T get_U() const
Definition
spectralDecomp.h:102
fastglmmLib::spectralDecomp::V
mat V
Definition
spectralDecomp.h:136
misc.h
scaleEachCol
mat scaleEachCol(const mat &X, const vec &w)
Definition
misc.h:17
scaleEachRow
mat scaleEachRow(const mat &X, const vec &w)
Definition
misc.h:32
scaleRowsCols
T scaleRowsCols(const T &X, const vec &w1, const vec &w2)
Definition
misc.h:49
fastglmmLib
Definition
CleanData.h:17
fastglmmLib::ZTYPE
ZTYPE
Definition
spectralDecomp.h:18
fastglmmLib::CATEGORICAL
@ CATEGORICAL
Definition
spectralDecomp.h:20
fastglmmLib::GENERAL
@ GENERAL
Definition
spectralDecomp.h:19
Generated by
1.17.0