File: updateMatrices.cpp

package info (click to toggle)
r-cran-forecast 8.13-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 2,248 kB
  • sloc: cpp: 975; ansic: 648; sh: 13; makefile: 2
file content (189 lines) | stat: -rw-r--r-- 4,793 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
/*
 * updateMatrices.cpp
 *
 *  Created on: 03/11/2011
 *      Author: srazbash
 */


#include "calcBATS.h"

using namespace Rcpp ;

SEXP updateFMatrix(SEXP F_s, SEXP smallPhi_s, SEXP alpha_s, SEXP beta_s, SEXP gammaBold_s, SEXP ar_s, SEXP ma_s, SEXP tau_s) {
	BEGIN_RCPP

	NumericMatrix F_r(F_s);
	arma::mat F(F_r.begin(), F_r.nrow(), F_r.ncol(), false);

	double *beta, *alpha = &REAL(alpha_s)[0];
	int *tau, p, q, betaAdjust;
	int zero = 0;

	if(!Rf_isNull(gammaBold_s)) {
		tau = &INTEGER(tau_s)[0];
	} else {
		tau = &zero;
	}

	if(!Rf_isNull(beta_s)) {
		beta = &REAL(beta_s)[0];
		double *smallPhi = &REAL(smallPhi_s)[0];
		F(0,1) = *smallPhi;
		F(1,1) = *smallPhi;
		betaAdjust = 1;
	} else {
		betaAdjust = 0;
	}

	if(!Rf_isNull(ar_s)) {
		//Rprintf("before arma::mat ar\n");
		NumericMatrix ar_r(ar_s);
		arma::mat ar(ar_r.begin(), ar_r.nrow(), ar_r.ncol(), false);
		//Rprintf("after arma::mat ar\n");
		p = ar.n_cols;
		//Rprintf("line-a-before\n");
		F.submat(0,(betaAdjust+ *tau+1),0,(betaAdjust+ *tau+p)) = *alpha * ar;
		//Rprintf("line-a-after\n");
		if(betaAdjust == 1) {
			//Rprintf("line-b-before\n");
			F.submat(1,(betaAdjust+ *tau+1),1,(betaAdjust+ *tau+p)) = *beta * ar;
			//Rprintf("line-b-after\n");
		}
		if(*tau > 0) {
			//Rprintf("la\n");
			NumericMatrix gammaBold_r(gammaBold_s);
			//Rprintf("la-2\n");
			arma::mat gammaBold(gammaBold_r.begin(), gammaBold_r.nrow(), gammaBold_r.ncol(), false);
			//Rprintf("la-3\n");
			//arma::mat gammaBold = as<arma::mat>(gammaBold_s);
			arma::mat B = trans(gammaBold) * ar;
			//Rprintf("line-c-before\n");
			F.submat((1+betaAdjust),(betaAdjust+ *tau+1), (betaAdjust+ *tau), (betaAdjust+ *tau+p)) = B;
			//Rprintf("line-c-after\n");
		}
		//Rprintf("line-d-before\n");
		F.submat((betaAdjust+ *tau+1),(betaAdjust+ *tau+1),(betaAdjust+ *tau+1),(betaAdjust+ *tau+p)) = ar;
		//Rprintf("line-d-after\n");
	} else {
		p = 0;
	}

	if(!Rf_isNull(ma_s)) {
		NumericMatrix ma_r(ma_s);
		arma::mat ma(ma_r.begin(), ma_r.nrow(), ma_r.ncol(), false);
		q = ma.n_cols;
		//Rprintf("one-before\n");
		F.submat(0,(betaAdjust+ *tau+p+1),0,(betaAdjust+ *tau+p+q)) = *alpha * ma;
		//Rprintf("one-after\n");
		if(betaAdjust == 1) {
			//Rprintf("two-before\n");
			F.submat(1,(betaAdjust+ *tau+p+1),1,(betaAdjust+ *tau+p+q)) = *beta * ma;
			///Rprintf("two-after\n");
		}
		if(*tau > 0) {
			//arma::mat gammaBold = as<arma::mat>(gammaBold_s);

			NumericMatrix gammaBold_r(gammaBold_s);
			arma::mat gammaBold(gammaBold_r.begin(), gammaBold_r.nrow(), gammaBold_r.ncol(), false);

			arma::mat C = trans(gammaBold) * ma;
			//Rprintf("three-before\n");
			F.submat((1+betaAdjust),(betaAdjust+ *tau+p+1), (betaAdjust+ *tau), (betaAdjust+ *tau+p+q)) = C;
			//Rprintf("three-after\n");
		}
		if(!Rf_isNull(ar_s)) {
			//Rprintf("four-before\n");
			F.submat((betaAdjust+ *tau+1), (betaAdjust+ *tau+p+1), (betaAdjust+ *tau+1), (betaAdjust+ *tau+p+q)) = ma;
			//Rprintf("four-after\n");
		}
	} else {
		q = 0;
	}

	return R_NilValue;

	END_RCPP

}

SEXP updateWtransposeMatrix(SEXP wTranspose_s, SEXP smallPhi_s, SEXP tau_s, SEXP arCoefs_s, SEXP maCoefs_s, SEXP p_s, SEXP q_s) {
	BEGIN_RCPP

	NumericMatrix wTranspose(wTranspose_s);

	double *arCoefs, *maCoefs;
	int *p, *q, *tau, adjBeta = 0;

	p = &INTEGER(p_s)[0];
	q = &INTEGER(q_s)[0];
	tau = &INTEGER(tau_s)[0];

	if(!Rf_isNull(smallPhi_s)) {
		adjBeta = 1;
		wTranspose(0,1) = REAL(smallPhi_s)[0];
	}

	if(*p > 0) {
		arCoefs = REAL(arCoefs_s);
		for(int i = 1; i <= *p; i++) {
			wTranspose(0,(adjBeta + *tau + i)) = arCoefs[(i - 1)];
		}
		if(*q > 0) {
			maCoefs = REAL(maCoefs_s);
			for(int i = 1; i <= *q; i++) {
				wTranspose(0,(adjBeta + *tau + *p + i)) = maCoefs[(i - 1)];
			}
		}
	} else if(*q > 0) {
		maCoefs = REAL(maCoefs_s);
		for(int i = 1; i <= *q; i++) {
			wTranspose(0,(adjBeta + *tau + i)) = maCoefs[(i - 1)];
		}
	}

	return R_NilValue;

	END_RCPP
}

SEXP updateGMatrix(SEXP g_s, SEXP gammaBold_s, SEXP alpha_s, SEXP beta_s, SEXP gammaVector_s, SEXP seasonalPeriods_s) {
	BEGIN_RCPP


	int adjBeta = 0, *seasonalPeriods;

	double *gammaVector;

	NumericMatrix g(g_s);


	g(0,0) = REAL(alpha_s)[0];
	if(!Rf_isNull(beta_s)) {
		g(1,0) = REAL(beta_s)[0];
		adjBeta = 1;
	}

	if((!Rf_isNull(gammaVector_s))&&(!Rf_isNull(seasonalPeriods_s))) {
		NumericMatrix gammaBold(gammaBold_s);
		seasonalPeriods = INTEGER(seasonalPeriods_s);
		gammaVector = REAL(gammaVector_s);
		int position = adjBeta + 1;
		int bPos = 0;
		gammaBold(0,bPos) = gammaVector[0];
		g(position, 0) = gammaVector[0];
		if(LENGTH(gammaVector_s) > 1) {
			for(R_len_t s = 0; s < (LENGTH(seasonalPeriods_s)-1); s++) {
				position = position + seasonalPeriods[s];
				bPos = bPos + seasonalPeriods[s];
				g(position, 0) = gammaVector[(s+1)];
			}


		}
	}

	return R_NilValue;

	END_RCPP
}