-
Notifications
You must be signed in to change notification settings - Fork 3
/
CholeskyUpdateReal.c
199 lines (171 loc) · 5.68 KB
/
CholeskyUpdateReal.c
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
// Compute the update of an upper Cholesky decomposition.
//
// This function is intended to be used as an (almost) drop-in replacement
// for MATLAB's `cholupdate` if code generation is required.
//
//
// `cholesky_update_real` is based on LINPACK subroutine `DCHUD`.
// However, the (z,y,rho)-update is not implemented.
//
// `givens_rotation_real` is based on LAPACK/BLAS subroutine `DROTG`.
//
// Important: This program requires C99! See the compilers note below.
//
//
// Copyright (c) 2011 University of Tennessee
//
// Copyright (c) 2011 University of California Berkeley
//
// Copyright (c) 2011 University of Colorado Denver
//
// Copyright (c) 2011 NAG Ltd.
//
// Copyright (c) 2011 Sven Hammarling,
// NAG Ltd.
//
// Copyright (c) 1978, G. W. Stewart,
// University of Maryland, Argonne National Lab.
//
// Copyright (c) 2016-2017, Mikhail Pak,
// Technical University of Munich.
//
// Notice on compilers:
// Microsoft Windows SDK 7.1 does not support C99.
// As a result, a lot of required functions are missing in `math.h`.
//
// Following compilers have been tested:
// - MinGW 4.9.2 C/C++ (TDM-GCC)
// - Microsoft Visual C++ 2015 Professional
//
// Compile this function by typing `mex CholeskyUpdateReal.c`
#include "matrix.h" // mwIndex, mwSize
#include "mex.h" // MEX functions and types
#include <math.h> // sqrt, fabs, hypot, copysign
// Define functions
// Rank-1 Cholesky update
int cholesky_update_real(const mwSize n, double *R, double *x);
// Real Givens rotation
void givens_rotation_real(double *a, double *b, double *c, double *s);
// Entry point for the MEX interface
void mexFunction(int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs) {
// Check if the number of input arguments is OK
if (nrhs != 2) {
mexErrMsgIdAndTxt(
"CholeskyUpdateReal:InvalidInput",
"Invalid number of input arguments. Please supply R and x.");
return;
}
// Check the number of output arguments
if (nlhs != 1) {
mexErrMsgIdAndTxt(
"CholeskyUpdateReal:InvalidCall",
"Invalid number of output arguments: only 1 or 2 allowed.");
return;
}
// Perform a deep copy of the first input (matrix `R`).
// Unfortunately, in-place update of `R` is not possible due to the
// copy-on-write nature of MATLAB. We have to copy `R` and modified the
// copied array in-place.
plhs[0] = mxDuplicateArray(prhs[0]);
// Target the `R_upd` pointer to the freshly created output value (updated Cholesky factor)
double *R_upd = mxGetPr(plhs[0]);
// Target the `x` pointer to the second input argument (vector `x`)
const double *x = mxGetPr(prhs[1]);
// Get size of the inputs*/
const mwSize *size_R = mxGetDimensions(prhs[0]);
const mwSize *size_x = mxGetDimensions(prhs[1]);
// Check if R is square
if (size_R[0] != size_R[1]) {
mexErrMsgIdAndTxt("CholeskyUpdateReal:InvalidInput",
"R must be a square matrix.");
return;
}
// Check if x is a column vector
if (size_x[1] != 1) {
mexErrMsgIdAndTxt("CholeskyUpdateReal:InvalidInput",
"x must be a column vector.");
return;
}
// Check if dimensions of the R and x are consistent
if (size_R[1] != size_x[0]) {
mexErrMsgIdAndTxt("CholeskyUpdateReal:InvalidInput",
"R and x must have the same size.");
return;
}
// Call the Cholesky update
int retval = cholesky_update_real(size_R[0], R_upd, x);
if (retval == 0) {
// Everything ok
return;
} else if (retval == 1) {
mexErrMsgIdAndTxt("CholeskyUpdateReal:AllocateError",
"Could not allocate memory.");
return;
} else {
mexErrMsgIdAndTxt("CholeskyUpdateReal:UnknownError",
"Unknown error occurred.");
return;
}
}
int cholesky_update_real(const mwSize n, double *R, const double *x) {
// Allocate memory for the vectors with the cosines of transforming rotations
double *c = mxMalloc(n * sizeof(double));
if (c == NULL) {
return 1;
}
// Allocate memory for the vectors with the sines of transforming rotations
double *s = mxMalloc(n * sizeof(double));
if (s == NULL) {
return 1;
}
// Update `R`
for (mwIndex j = 0; j < n; j++) {
double xj = x[j];
// Apply the previous rotations
for (mwIndex i = 0; i < j; i++) {
// Target R_ij to the matrix entry R(i + 1, j + 1)
// IMPORTANT:
// R is in the column major notation!
// (since it was copied from MATLAB)
double *R_ij = R + j * n + i;
const double t = (*R_ij) * c[i] + xj * s[i];
xj = xj * c[i] - (*R_ij) * s[i];
*R_ij = t;
}
// Compute the next rotation
givens_rotation_real((R + j * n + j), &xj, (c + j), (s + j));
}
// Free memory
mxFree(c);
mxFree(s);
return 0;
}
void givens_rotation_real(double *a, double *b, double *c, double *s) {
double r, z, roe;
if (fabs(*a) > fabs(*b)) {
roe = *a;
} else {
roe = *b;
}
const double scale = fabs(*a) + fabs(*b);
if (scale == 0.0) {
*c = 1.0;
*s = 0.0;
r = 0.0;
z = 0.0;
} else {
r = scale * hypot((*a / scale), (*b / scale));
r = copysign(r, roe);
*c = (*a) / r;
*s = (*b) / r;
z = 1.0;
if (fabs(*a) > fabs(*b)) {
z = *s;
}
if ((fabs(*a) >= fabs(*b)) && (*c != 0.0)) {
z = 1.0 / (*c);
}
}
*a = r;
*b = z;
}