File: MSpline_export.cpp

package info (click to toggle)
r-cran-splines2 0.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,512 kB
  • sloc: cpp: 1,988; ansic: 165; sh: 13; makefile: 2
file content (139 lines) | stat: -rw-r--r-- 4,997 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
//
// R package splines2 by Wenjie Wang and Jun Yan
// Copyright (C) 2016-2021
//
// This file is part of the R package splines2.
//
// The R package splines2 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 3 of the License, or any later
// version (at your option). See the GNU General Public License at
// <https://www.gnu.org/licenses/> for details.
//
// The R package splines2 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.
//

#include <RcppArmadillo.h>
// [[Rcpp::plugins(cpp11)]]

#include <splines2Armadillo.h>

// [[Rcpp::export]]
Rcpp::NumericMatrix rcpp_mSpline(
    const arma::vec& x,
    const unsigned int df,
    const unsigned int degree,
    const arma::vec& internal_knots,
    const arma::vec& boundary_knots,
    const unsigned int derivs = 0,
    const bool integral = false,
    const bool complete_basis = true
    )
{
    Rcpp::NumericMatrix out;
    splines2::MSpline ms_obj;
    // if df > 0 and knots are not specified
    // auto set internal knots based on df
    if (df > 0 && internal_knots.n_elem == 0) {
        // compute actual spline degree of freedom
        const unsigned int wo_intercept {
            static_cast<unsigned int>(! complete_basis)
        };
        unsigned int spline_df { df + wo_intercept };
        ms_obj = splines2::MSpline(x, spline_df, degree, boundary_knots);
    } else {
        // else ignore df
        ms_obj = splines2::MSpline(x, internal_knots,
                                   degree, boundary_knots);
    }
    // 1) basis, 2) derivative, or 3) integral
    if (integral && derivs == 0) {
        // integrals
        out = splines2::arma2rmat(ms_obj.integral(complete_basis));
    } else if ((! integral && derivs == 0) || (integral && derivs == 1)) {
        // bases
        out = splines2::arma2rmat(ms_obj.basis(complete_basis));
    } else {
        // derivatives
        out = splines2::arma2rmat(
            ms_obj.derivative(derivs - static_cast<unsigned int>(integral),
                              complete_basis)
            );
    }
    // add attributes
    out.attr("dimnames") = Rcpp::List::create(
        R_NilValue, splines2::char_seq_len(out.ncol())
        );
    out.attr("x") = splines2::arma2rvec(x);
    out.attr("degree") = static_cast<int>(ms_obj.get_degree());
    out.attr("knots") = splines2::arma2rvec(ms_obj.get_internal_knots());
    out.attr("Boundary.knots") =
        splines2::arma2rvec(ms_obj.get_boundary_knots());
    out.attr("derivs") = static_cast<int>(derivs);
    out.attr("integral") = integral;
    out.attr("periodic") = false;
    out.attr("intercept") = complete_basis;
    return out;
}


// [[Rcpp::export]]
Rcpp::NumericMatrix rcpp_periodic_mSpline(
    const arma::vec& x,
    const unsigned int df,
    const unsigned int degree,
    const arma::vec& internal_knots,
    const arma::vec& boundary_knots,
    const unsigned int derivs = 0,
    const bool integral = false,
    const bool complete_basis = true
    )
{
    Rcpp::NumericMatrix out;
    splines2::PeriodicMSpline ms_obj;
    // if df > 0 and knots are not specified
    // auto set internal knots based on df
    if (df > 0 && internal_knots.n_elem == 0) {
        // compute actual spline degree of freedom
        const unsigned int wo_intercept {
            static_cast<unsigned int>(! complete_basis)
        };
        unsigned int spline_df { df + wo_intercept };
        ms_obj = splines2::PeriodicMSpline(
            x, spline_df, degree, boundary_knots);
    } else {
        // else ignore df
        ms_obj = splines2::PeriodicMSpline(
            x, internal_knots, degree, boundary_knots);
    }
    // 1) basis, 2) derivative, or 3) integral
    if (integral && derivs == 0) {
        // integrals
        out = splines2::arma2rmat(ms_obj.integral(complete_basis));
    } else if ((! integral && derivs == 0) || (integral && derivs == 1)) {
        // bases
        out = splines2::arma2rmat(ms_obj.basis(complete_basis));
    } else {
        // derivatives
        out = splines2::arma2rmat(
            ms_obj.derivative(derivs - static_cast<unsigned int>(integral),
                              complete_basis)
            );
    }
    // add attributes
    out.attr("dimnames") = Rcpp::List::create(
        R_NilValue, splines2::char_seq_len(out.ncol())
        );
    out.attr("x") = splines2::arma2rvec(x);
    out.attr("degree") = static_cast<int>(ms_obj.get_degree());
    out.attr("knots") = splines2::arma2rvec(ms_obj.get_internal_knots());
    out.attr("Boundary.knots") =
        splines2::arma2rvec(ms_obj.get_boundary_knots());
    out.attr("derivs") = static_cast<int>(derivs);
    out.attr("integral") = integral;
    out.attr("periodic") = true;
    out.attr("intercept") = complete_basis;
    return out;
}