File: eigs_gen.cpp

package info (click to toggle)
r-cran-rspectra 0.16-0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 736 kB
  • sloc: cpp: 4,917; ansic: 256; makefile: 2
file content (370 lines) | stat: -rw-r--r-- 12,540 bytes parent folder | download
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
#include <RcppEigen.h>
#include <GenEigs.h>
#include <SpectraC.h>
#include "matops.h"
#include "matops_c.h"

using namespace Spectra;
using Rcpp::as;

/************************ Macros to generate code ************************/

#define EIG_COMMON_CODE                                                        \
if(user_initvec)                                                               \
    eigs.init(initvec);                                                        \
else                                                                           \
    eigs.init();                                                               \
nconv = eigs.compute(maxitr, tol);                                             \
if(nconv < nev)                                                                \
    Rcpp::warning("only %d eigenvalue(s) converged, less than k = %d",         \
                  nconv, nev);                                                 \
evals = Rcpp::wrap(eigs.eigenvalues());                                        \
if(retvec)                                                                     \
    evecs = Rcpp::wrap(eigs.eigenvectors());                                   \
else                                                                           \
    evecs = R_NilValue;                                                        \
niter = eigs.num_iterations();                                                 \
nops = eigs.num_operations();



#define EIG_CODE_REGULAR(RULE, OPTYPE)                                         \
GenEigsSolver<double, RULE, OPTYPE> eigs(op, nev, ncv);                        \
EIG_COMMON_CODE



#define EIG_CODE_REAL_SHIFT(RULE, OPTYPE)                                      \
GenEigsRealShiftSolver<double, RULE, OPTYPE> eigs(op, nev, ncv, sigmar);       \
EIG_COMMON_CODE



#define EIG_CODE_COMPLEX_SHIFT(RULE, OPTYPE)                                   \
GenEigsComplexShiftSolver<double, RULE, OPTYPE> eigs(op, nev, ncv, sigmar, sigmai);      \
EIG_COMMON_CODE



#define EIG_CODE_GENERATOR(SOLVER, OPTYPE)                                     \
switch(rule)                                                                   \
{                                                                              \
    case WHICH_LM :                                                            \
        { EIG_CODE_ ## SOLVER(WHICH_LM, OPTYPE) }                              \
        break;                                                                 \
    case WHICH_LR :                                                            \
        { EIG_CODE_ ## SOLVER(WHICH_LR, OPTYPE) }                              \
        break;                                                                 \
    case WHICH_LI :                                                            \
        { EIG_CODE_ ## SOLVER(WHICH_LI, OPTYPE) }                              \
        break;                                                                 \
    case WHICH_SM :                                                            \
        { EIG_CODE_ ## SOLVER(WHICH_SM, OPTYPE) }                              \
        break;                                                                 \
    case WHICH_SR :                                                            \
        { EIG_CODE_ ## SOLVER(WHICH_SR, OPTYPE) }                              \
        break;                                                                 \
    case WHICH_SI :                                                            \
        { EIG_CODE_ ## SOLVER(WHICH_SI, OPTYPE) }                              \
        break;                                                                 \
    default:                                                                   \
        Rcpp::stop("unsupported selection rule");                              \
}

/************************ Macros to generate code ************************/



/************************ Regular mode ************************/
Rcpp::RObject run_eigs_gen(
    MatProd* op, int n, int nev, int ncv, int rule,
    int maxitr, double tol, bool retvec,
    bool user_initvec, const double* initvec
)
{
    Rcpp::RObject evals, evecs;
    int nconv = 0, niter = 0, nops = 0;

    EIG_CODE_GENERATOR(REGULAR, MatProd)

    return Rcpp::List::create(
        Rcpp::Named("values")  = evals,
        Rcpp::Named("vectors") = evecs,
        Rcpp::Named("nconv")   = nconv,
        Rcpp::Named("niter")   = niter,
        Rcpp::Named("nops")    = nops
    );
}


RcppExport SEXP eigs_gen(
    SEXP A_mat_r, SEXP n_scalar_r, SEXP k_scalar_r,
    SEXP params_list_r, SEXP mattype_scalar_r
)
{
    BEGIN_RCPP

    Rcpp::List params_rcpp(params_list_r);

    int n        = as<int>(n_scalar_r);
    int nev      = as<int>(k_scalar_r);
    int ncv      = as<int>(params_rcpp["ncv"]);
    int rule     = as<int>(params_rcpp["which"]);
    double tol   = as<double>(params_rcpp["tol"]);
    int maxitr   = as<int>(params_rcpp["maxitr"]);
    bool retvec  = as<bool>(params_rcpp["retvec"]);
    int mattype  = as<int>(mattype_scalar_r);

    const double* initvec = NULL;
    bool user_initvec = as<bool>(params_rcpp["user_initvec"]);
    if(user_initvec)
    {
        Rcpp::NumericVector v0 = params_rcpp["initvec"];
        initvec = v0.begin();
    }

    MatProd* op = get_mat_prod_op(A_mat_r, n, n, params_list_r, mattype);;
    Rcpp::RObject res = run_eigs_gen(op, n, nev, ncv, rule,
                                     maxitr, tol, retvec,
                                     user_initvec, initvec);

    delete op;

    return res;

    END_RCPP
}
/************************ Regular mode ************************/



/************************ Real shift mode ************************/
Rcpp::RObject run_eigs_real_shift_gen(
    RealShift* op, int n, int nev, int ncv, int rule,
    double sigmar, int maxitr, double tol, bool retvec,
    bool user_initvec, const double* initvec
)
{
    Rcpp::RObject evals, evecs;
    int nconv = 0, niter = 0, nops = 0;

    EIG_CODE_GENERATOR(REAL_SHIFT, RealShift)

    return Rcpp::List::create(
        Rcpp::Named("values")  = evals,
        Rcpp::Named("vectors") = evecs,
        Rcpp::Named("nconv")   = nconv,
        Rcpp::Named("niter")   = niter,
        Rcpp::Named("nops")    = nops
    );
}

RcppExport SEXP eigs_real_shift_gen(
    SEXP A_mat_r, SEXP n_scalar_r, SEXP k_scalar_r,
    SEXP params_list_r, SEXP mattype_scalar_r
)
{
    BEGIN_RCPP

    Rcpp::List params_rcpp(params_list_r);

    int n         = as<int>(n_scalar_r);
    int nev       = as<int>(k_scalar_r);
    int ncv       = as<int>(params_rcpp["ncv"]);
    int rule      = as<int>(params_rcpp["which"]);
    double tol    = as<double>(params_rcpp["tol"]);
    int maxitr    = as<int>(params_rcpp["maxitr"]);
    bool retvec   = as<bool>(params_rcpp["retvec"]);
    int mattype   = as<int>(mattype_scalar_r);
    double sigmar = as<double>(params_rcpp["sigmar"]);

    const double* initvec = NULL;
    bool user_initvec = as<bool>(params_rcpp["user_initvec"]);
    if(user_initvec)
    {
        Rcpp::NumericVector v0 = params_rcpp["initvec"];
        initvec = v0.begin();
    }

    RealShift* op = get_real_shift_op_gen(A_mat_r, n, params_list_r, mattype);
    Rcpp::RObject res = run_eigs_real_shift_gen(op, n, nev, ncv, rule,
                                                sigmar, maxitr, tol, retvec,
                                                user_initvec, initvec);

    delete op;

    return res;

    END_RCPP
}
/************************ Real shift mode ************************/



/************************ Complex shift mode ************************/
Rcpp::RObject run_eigs_complex_shift_gen(
    ComplexShift* op, int n, int nev, int ncv, int rule,
    double sigmar, double sigmai, int maxitr, double tol, bool retvec,
    bool user_initvec, const double* initvec
)
{
    Rcpp::RObject evals, evecs;
    int nconv = 0, niter = 0, nops = 0;

    EIG_CODE_GENERATOR(COMPLEX_SHIFT, ComplexShift)

    return Rcpp::List::create(
        Rcpp::Named("values")  = evals,
        Rcpp::Named("vectors") = evecs,
        Rcpp::Named("nconv")   = nconv,
        Rcpp::Named("niter")   = niter,
        Rcpp::Named("nops")    = nops
    );
}

RcppExport SEXP eigs_complex_shift_gen(
    SEXP A_mat_r, SEXP n_scalar_r, SEXP k_scalar_r,
    SEXP params_list_r, SEXP mattype_scalar_r
)
{
    BEGIN_RCPP

    Rcpp::List params_rcpp(params_list_r);

    int n         = as<int>(n_scalar_r);
    int nev       = as<int>(k_scalar_r);
    int ncv       = as<int>(params_rcpp["ncv"]);
    int rule      = as<int>(params_rcpp["which"]);
    double tol    = as<double>(params_rcpp["tol"]);
    int maxitr    = as<int>(params_rcpp["maxitr"]);
    bool retvec   = as<bool>(params_rcpp["retvec"]);
    int mattype   = as<int>(mattype_scalar_r);
    double sigmar = as<double>(params_rcpp["sigmar"]);
    double sigmai = as<double>(params_rcpp["sigmai"]);

    const double* initvec = NULL;
    bool user_initvec = as<bool>(params_rcpp["user_initvec"]);
    if(user_initvec)
    {
        Rcpp::NumericVector v0 = params_rcpp["initvec"];
        initvec = v0.begin();
    }

    ComplexShift* op = get_complex_shift_op(A_mat_r, n, params_list_r, mattype);
    Rcpp::RObject res = run_eigs_complex_shift_gen(op, n, nev, ncv, rule, sigmar, sigmai,
                                                   maxitr, tol, retvec,
                                                   user_initvec, initvec);

    delete op;

    return res;

    END_RCPP
}
/************************ Complex shift mode ************************/



/************************ C interface ************************/
void eigs_gen_c(
    mat_op op, int n, int k,
    const spectra_opts* opts, void* data,
    int* nconv, int* niter, int* nops,
    double* evals_r, double* evals_i, double* evecs_r, double* evecs_i, int* info
)
{
    BEGIN_RCPP

    CMatProd cmat_op(op, n, data);
    Rcpp::List res;
    try {
        res = run_eigs_gen((MatProd*) &cmat_op, n, k, opts->ncv, opts->rule,
                           opts->maxitr, opts->tol, opts->retvec != 0,
                           false, NULL);
        *info = 0;
    } catch(...) {
        *info = 1;  // indicates error
    }

    *nconv = Rcpp::as<int>(res["nconv"]);
    *niter = Rcpp::as<int>(res["niter"]);
    *nops  = Rcpp::as<int>(res["nops"]);
    Rcpp::ComplexVector val = res["values"];
    for(int i = 0; i < val.length(); i++)
    {
        Rcomplex c = val[i];
        evals_r[i] = c.r;
        evals_i[i] = c.i;
    }
    if(opts->retvec != 0)
    {
        Rcpp::ComplexMatrix vec = res["vectors"];
        for(int i = 0; i < vec.length(); i++)
        {
            Rcomplex c = val[i];
            evecs_r[i] = c.r;
            evecs_i[i] = c.i;
        }
    }

    VOID_END_RCPP
}

void eigs_gen_shift_c(
    mat_op op, int n, int k, double sigmar, double sigmai,
    const spectra_opts* opts, void* data,
    int* nconv, int* niter, int* nops,
    double* evals_r, double* evals_i, double* evecs_r, double* evecs_i, int* info
)
{
    BEGIN_RCPP

    Rcpp::List res;
    try {
        if(std::abs(sigmai) <= 1e-12)
        {
            CRealShift cmat_op(op, n, data);
            res = run_eigs_real_shift_gen(
                      (RealShift*) &cmat_op, n, k, opts->ncv, opts->rule,
                      sigmar, opts->maxitr, opts->tol, opts->retvec != 0,
                      false, NULL
                  );
        }
        else {
            CComplexShift cmat_op(op, n, data);
            res = run_eigs_complex_shift_gen(
                      (ComplexShift*) &cmat_op, n, k, opts->ncv, opts->rule,
                      sigmar, sigmai, opts->maxitr, opts->tol, opts->retvec != 0,
                      false, NULL
                  );
        }

        *info = 0;
    } catch(...) {
        *info = 1;  // indicates error
    }

    *nconv = Rcpp::as<int>(res["nconv"]);
    *niter = Rcpp::as<int>(res["niter"]);
    *nops  = Rcpp::as<int>(res["nops"]);
    Rcpp::ComplexVector val = res["values"];
    for(int i = 0; i < val.length(); i++)
    {
        Rcomplex c = val[i];
        evals_r[i] = c.r;
        evals_i[i] = c.i;
    }
    if(opts->retvec != 0)
    {
        Rcpp::ComplexMatrix vec = res["vectors"];
        for(int i = 0; i < vec.length(); i++)
        {
            Rcomplex c = val[i];
            evecs_r[i] = c.r;
            evecs_i[i] = c.i;
        }
    }

    VOID_END_RCPP
}
/************************ C interface ************************/