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
|
/**************************************************************************
**
** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
**
** Meschach Library
**
** This Meschach Library is provided "as is" without any express
** or implied warranty of any kind with respect to this software.
** In particular the authors shall not be liable for any direct,
** indirect, special, incidental or consequential damages arising
** in any way from use of the software.
**
** Everyone is granted permission to copy, modify and redistribute this
** Meschach Library, provided:
** 1. All copies contain this copyright notice.
** 2. All modified copies shall carry a notice stating who
** made the last modification and the date of such modification.
** 3. No charge is made for this software or works derived from it.
** This clause shall not be construed as constraining other software
** distributed on the same medium as this software, nor is a
** distribution fee considered a charge.
**
***************************************************************************/
/*
Matrix factorisation routines to work with the other matrix files.
*/
/* CHfactor.c 1.2 11/25/87 */
static char rcsid[] = "$Id: chfactor.c,v 1.2 1994/01/13 05:36:36 des Exp $";
#include <stdio.h>
#include "matrix.h"
#include "matrix2.h"
#include <math.h>
/* Most matrix factorisation routines are in-situ unless otherwise specified */
/* CHfactor -- Cholesky L.L' factorisation of A in-situ */
MAT *CHfactor(A)
MAT *A;
{
u_int i, j, k, n;
Real **A_ent, *A_piv, *A_row, sum, tmp;
if ( A==(MAT *)NULL )
error(E_NULL,"CHfactor");
if ( A->m != A->n )
error(E_SQUARE,"CHfactor");
n = A->n; A_ent = A->me;
for ( k=0; k<n; k++ )
{
/* do diagonal element */
sum = A_ent[k][k];
A_piv = A_ent[k];
for ( j=0; j<k; j++ )
{
/* tmp = A_ent[k][j]; */
tmp = *A_piv++;
sum -= tmp*tmp;
}
if ( sum <= 0.0 )
error(E_POSDEF,"CHfactor");
A_ent[k][k] = sqrt(sum);
/* set values of column k */
for ( i=k+1; i<n; i++ )
{
sum = A_ent[i][k];
A_piv = A_ent[k];
A_row = A_ent[i];
sum -= __ip__(A_row,A_piv,(int)k);
/************************************************
for ( j=0; j<k; j++ )
sum -= A_ent[i][j]*A_ent[k][j];
sum -= (*A_row++)*(*A_piv++);
************************************************/
A_ent[j][i] = A_ent[i][j] = sum/A_ent[k][k];
}
}
return (A);
}
/* CHsolve -- given a CHolesky factorisation in A, solve A.x=b */
VEC *CHsolve(A,b,x)
MAT *A;
VEC *b,*x;
{
if ( A==(MAT *)NULL || b==(VEC *)NULL )
error(E_NULL,"CHsolve");
if ( A->m != A->n || A->n != b->dim )
error(E_SIZES,"CHsolve");
x = v_resize(x,b->dim);
Lsolve(A,b,x,0.0);
Usolve(A,x,x,0.0);
return (x);
}
/* LDLfactor -- L.D.L' factorisation of A in-situ */
MAT *LDLfactor(A)
MAT *A;
{
u_int i, k, n, p;
Real **A_ent;
Real d, sum;
static VEC *r = VNULL;
if ( ! A )
error(E_NULL,"LDLfactor");
if ( A->m != A->n )
error(E_SQUARE,"LDLfactor");
n = A->n; A_ent = A->me;
r = v_resize(r,n);
MEM_STAT_REG(r,TYPE_VEC);
for ( k = 0; k < n; k++ )
{
sum = 0.0;
for ( p = 0; p < k; p++ )
{
r->ve[p] = A_ent[p][p]*A_ent[k][p];
sum += r->ve[p]*A_ent[k][p];
}
d = A_ent[k][k] -= sum;
if ( d == 0.0 )
error(E_SING,"LDLfactor");
for ( i = k+1; i < n; i++ )
{
sum = __ip__(A_ent[i],r->ve,(int)k);
/****************************************
sum = 0.0;
for ( p = 0; p < k; p++ )
sum += A_ent[i][p]*r->ve[p];
****************************************/
A_ent[i][k] = (A_ent[i][k] - sum)/d;
}
}
return A;
}
VEC *LDLsolve(LDL,b,x)
MAT *LDL;
VEC *b, *x;
{
if ( ! LDL || ! b )
error(E_NULL,"LDLsolve");
if ( LDL->m != LDL->n )
error(E_SQUARE,"LDLsolve");
if ( LDL->m != b->dim )
error(E_SIZES,"LDLsolve");
x = v_resize(x,b->dim);
Lsolve(LDL,b,x,1.0);
Dsolve(LDL,x,x);
LTsolve(LDL,x,x,1.0);
return x;
}
/* MCHfactor -- Modified Cholesky L.L' factorisation of A in-situ */
MAT *MCHfactor(A,tol)
MAT *A;
double tol;
{
u_int i, j, k, n;
Real **A_ent, *A_piv, *A_row, sum, tmp;
if ( A==(MAT *)NULL )
error(E_NULL,"MCHfactor");
if ( A->m != A->n )
error(E_SQUARE,"MCHfactor");
if ( tol <= 0.0 )
error(E_RANGE,"MCHfactor");
n = A->n; A_ent = A->me;
for ( k=0; k<n; k++ )
{
/* do diagonal element */
sum = A_ent[k][k];
A_piv = A_ent[k];
for ( j=0; j<k; j++ )
{
/* tmp = A_ent[k][j]; */
tmp = *A_piv++;
sum -= tmp*tmp;
}
if ( sum <= tol )
sum = tol;
A_ent[k][k] = sqrt(sum);
/* set values of column k */
for ( i=k+1; i<n; i++ )
{
sum = A_ent[i][k];
A_piv = A_ent[k];
A_row = A_ent[i];
sum -= __ip__(A_row,A_piv,(int)k);
/************************************************
for ( j=0; j<k; j++ )
sum -= A_ent[i][j]*A_ent[k][j];
sum -= (*A_row++)*(*A_piv++);
************************************************/
A_ent[j][i] = A_ent[i][j] = sum/A_ent[k][k];
}
}
return (A);
}
|