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
|
/* Modified Cholesky Decomposition
*
* Copyright (C) 2016 Patrick Alken
*
* This 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, or (at your option) any
* later version.
*
* This source 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. See the GNU General Public License
* for more details.
*/
#include <config.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_permutation.h>
#include <gsl/gsl_permute_vector.h>
#include "cholesky_common.c"
/*
* This module contains routines related to the Modified Cholesky
* Decomposition, which factors a symmetric indefinite matrix A as
*
* P (A + E) P^T = L D L^T
*
* where:
* P: permutation matrix
* E: small, non-negative diagonal matrix
* L: unit lower triangular matrix
* D: strictly positive diagonal matrix
*
* These routines follow these works closely:
*
* [1] P. E. Gill, W. Murray, M. H. Wright, Practical Optimization,
* Academic Press, 1981.
*
* [2] Dennis and Schnabel, Numerical Methods for Unconstrained Optimization
* and Nonlinear Equations, SIAM, 1996
*/
static size_t mcholesky_maxabs(const gsl_vector * v, double *maxabs);
/*
gsl_linalg_mcholesky_decomp()
Perform Pivoted Modified Cholesky LDLT decomposition of a symmetric positive
indefinite matrix:
P (A + E) P^T = L D L^T
Inputs: A - (input) symmetric, positive indefinite matrix,
stored in lower triangle
(output) lower triangle contains L; diagonal contains D
p - (output) permutation matrix P
E - (output) perturbation matrix E
Return: success/error
Notes:
1) Based on algorithm 4.2.2 (Outer Product LDLT with Pivoting) of
Golub and Van Loan, Matrix Computations (4th ed), with modifications
described in [1] and [2]
2) E can be set to NULL if not required
*/
int
gsl_linalg_mcholesky_decomp (gsl_matrix * A, gsl_permutation * p,
gsl_vector * E)
{
const size_t N = A->size1;
if (N != A->size2)
{
GSL_ERROR("LDLT decomposition requires square matrix", GSL_ENOTSQR);
}
else if (p->size != N)
{
GSL_ERROR ("permutation length must match matrix size", GSL_EBADLEN);
}
else
{
const double delta = GSL_DBL_EPSILON;
double beta;
double gamma = 0.0;
double xi = 0.0;
gsl_vector_view diag = gsl_matrix_diagonal(A);
size_t i, j;
/* save a copy of A in upper triangle (for later rcond calculation) */
gsl_matrix_transpose_tricpy(CblasLower, CblasUnit, A, A);
gsl_permutation_init(p);
/* compute:
* gamma = max | A_{ii} |
* xi = max_{i \ne j} | A_{ij} |
*/
for (i = 0; i < N; ++i)
{
double aii = gsl_matrix_get(A, i, i);
gamma = GSL_MAX(gamma, fabs(aii));
for (j = 0; j < i; ++j)
{
double aij = gsl_matrix_get(A, i, j);
xi = GSL_MAX(xi, fabs(aij));
}
}
/* compute:
* beta = sqrt[ max { gamma, xi/nu, eps } ]
* with: nu = max{ sqrt(N^2 - 1), 1 }
*/
if (N == 1)
{
beta = GSL_MAX(GSL_MAX(gamma, xi), GSL_DBL_EPSILON);
}
else
{
double nu = sqrt(N*N - 1.0);
beta = GSL_MAX(GSL_MAX(gamma, xi / nu), GSL_DBL_EPSILON);
}
beta = sqrt(beta);
for (j = 0; j < N; ++j)
{
double ajj, thetaj, u, alpha, alphainv;
gsl_vector_view w;
size_t q;
/* compute q = max_idx { A_jj, ..., A_nn } */
w = gsl_vector_subvector(&diag.vector, j, N - j);
q = mcholesky_maxabs(&w.vector, NULL) + j;
gsl_permutation_swap(p, q, j);
cholesky_swap_rowcol(A, q, j);
/* theta_j = max_{j+1 <= i <= n} |A_{ij}| */
if (j < N - 1)
{
w = gsl_matrix_subcolumn(A, j, j + 1, N - j - 1);
mcholesky_maxabs(&w.vector, &thetaj);
}
else
{
thetaj = 0.0;
}
u = thetaj / beta;
/* compute alpha = d_j */
ajj = gsl_matrix_get(A, j, j);
alpha = GSL_MAX(GSL_MAX(delta, fabs(ajj)), u * u);
alphainv = 1.0 / alpha;
if (j < N - 1)
{
/* v = A(j+1:n, j) */
gsl_vector_view v = gsl_matrix_subcolumn(A, j, j + 1, N - j - 1);
/* m = A(j+1:n, j+1:n) */
gsl_matrix_view m = gsl_matrix_submatrix(A, j + 1, j + 1, N - j - 1, N - j - 1);
/* m = m - v v^T / alpha */
gsl_blas_dsyr(CblasLower, -alphainv, &v.vector, &m.matrix);
/* v = v / alpha */
gsl_vector_scale(&v.vector, alphainv);
}
if (E)
gsl_vector_set(E, j, alpha - ajj);
gsl_matrix_set(A, j, j, alpha);
}
if (E)
{
/* we currently have: P A P^T + E = L D L^T, permute E
* so that we have: P (A + E) P^T = L D L^T */
gsl_permute_vector_inverse(p, E);
}
return GSL_SUCCESS;
}
}
int
gsl_linalg_mcholesky_solve(const gsl_matrix * LDLT,
const gsl_permutation * p,
const gsl_vector * b,
gsl_vector * x)
{
int status = gsl_linalg_pcholesky_solve(LDLT, p, b, x);
return status;
}
int
gsl_linalg_mcholesky_svx(const gsl_matrix * LDLT,
const gsl_permutation * p,
gsl_vector * x)
{
int status = gsl_linalg_pcholesky_svx(LDLT, p, x);
return status;
}
int
gsl_linalg_mcholesky_rcond (const gsl_matrix * LDLT, const gsl_permutation * p,
double * rcond, gsl_vector * work)
{
int status = gsl_linalg_pcholesky_rcond(LDLT, p, rcond, work);
return status;
}
int
gsl_linalg_mcholesky_invert(const gsl_matrix * LDLT, const gsl_permutation * p,
gsl_matrix * Ainv)
{
int status = gsl_linalg_pcholesky_invert(LDLT, p, Ainv);
return status;
}
/*
mcholesky_maxabs()
Compute:
val = max_i |v_i|
Inputs: v - vector
maxabs - (output) max abs value
Return: index corresponding to max_i |v_i|
*/
static size_t
mcholesky_maxabs(const gsl_vector * v, double *maxabs)
{
const size_t n = v->size;
size_t i;
size_t idx = 0;
double max = gsl_vector_get(v, idx);
for (i = 1; i < n; ++i)
{
double vi = gsl_vector_get(v, i);
double absvi = fabs(vi);
if (absvi > max)
{
max = absvi;
idx = i;
}
}
if (maxabs)
*maxabs = max;
return idx;
}
|