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 272 273 274 275 276 277 278 279 280
|
/* ----------------------------------------------------------------------------
@COPYRIGHT :
Copyright 1993,1994,1995 David MacDonald,
McConnell Brain Imaging Centre,
Montreal Neurological Institute, McGill University.
Permission to use, copy, modify, and distribute this
software and its documentation for any purpose and without
fee is hereby granted, provided that the above copyright
notice appear in all copies. The author and McGill University
make no representations about the suitability of this
software for any purpose. It is provided "as is" without
express or implied warranty.
---------------------------------------------------------------------------- */
#include <internal_volume_io.h>
/* ----------------------------- MNI Header -----------------------------------
@NAME : scaled_maximal_pivoting_gaussian_elimination
@INPUT : n size of matrix, n by n
a matrix
n_values number of values to solve for
@OUTPUT : row permutation array filled in by this function
solution on input, the values, on output the solution,
size n by n_values
@RETURNS : TRUE if successful
@DESCRIPTION: Performs scaled maximal pivoting gaussian elimination as a
numerically robust method to solve systems of linear equations.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 10, 1995 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static BOOLEAN scaled_maximal_pivoting_gaussian_elimination(
int n,
int row[],
Real **a,
int n_values,
Real **solution )
{
int i, j, k, p, v, tmp;
Real *s, val, best_val, m, scale_factor;
BOOLEAN success;
ALLOC( s, n );
for_less( i, 0, n )
row[i] = i;
for_less( i, 0, n )
{
s[i] = FABS( a[i][0] );
for_less( j, 1, n )
{
if( FABS(a[i][j]) > s[i] )
s[i] = FABS(a[i][j]);
}
if( s[i] == 0.0 )
{
FREE( s );
return( FALSE );
}
}
success = TRUE;
for_less( i, 0, n-1 )
{
p = i;
best_val = a[row[i]][i] / s[row[i]];
best_val = FABS( best_val );
for_less( j, i+1, n )
{
val = a[row[j]][i] / s[row[j]];
val = FABS( val );
if( val > best_val )
{
best_val = val;
p = j;
}
}
if( a[row[p]][i] == 0.0 )
{
success = FALSE;
break;
}
if( i != p )
{
tmp = row[i];
row[i] = row[p];
row[p] = tmp;
}
for_less( j, i+1, n )
{
if( a[row[i]][i] == 0.0 )
{
success = FALSE;
break;
}
m = a[row[j]][i] / a[row[i]][i];
for_less( k, i+1, n )
a[row[j]][k] -= m * a[row[i]][k];
for_less( v, 0, n_values )
solution[row[j]][v] -= m * solution[row[i]][v];
}
if( !success )
break;
}
if( success && a[row[n-1]][n-1] == 0.0 )
success = FALSE;
if( success )
{
for( i = n-1; i >= 0; --i )
{
for_less( j, i+1, n )
{
scale_factor = a[row[i]][j];
for_less( v, 0, n_values )
solution[row[i]][v] -= scale_factor * solution[row[j]][v];
}
for_less( v, 0, n_values )
solution[row[i]][v] /= a[row[i]][i];
}
}
FREE( s );
return( success );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : scaled_maximal_pivoting_gaussian_elimination_real
@INPUT : n
coefs
n_values
values
@OUTPUT : values has the solution on output
@RETURNS : TRUE if successful
@DESCRIPTION: Performs gaussian elimination on a type-Real matrix, first
copying it into temporary storage, which is modified as
the gaussian elimination is performed.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 10, 1995 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
static BOOLEAN scaled_maximal_pivoting_gaussian_elimination_real(
int n,
Real **coefs,
int n_values,
Real **values )
{
int i, j, v, *row;
Real **a, **solution;
BOOLEAN success;
ALLOC( row, n );
ALLOC2D( a, n, n );
ALLOC2D( solution, n, n_values );
for_less( i, 0, n )
{
for_less( j, 0, n )
a[i][j] = coefs[i][j];
for_less( v, 0, n_values )
solution[i][v] = values[v][i];
}
success = scaled_maximal_pivoting_gaussian_elimination( n, row, a, n_values,
solution );
if( success )
{
for_less( i, 0, n )
{
for_less( v, 0, n_values )
values[v][i] = solution[row[i]][v];
}
}
FREE2D( a );
FREE2D( solution );
FREE( row );
return( success );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : solve_linear_system
@INPUT : n
coefs - n by n matrix
values - size n list
@OUTPUT : solution - size n list
@RETURNS : TRUE if successful
@DESCRIPTION: Solves a linear system of equations, finding the solution
t t
vector that satisfies [coefs] * [solution] = [values]
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 10, 1995 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI BOOLEAN solve_linear_system(
int n,
Real **coefs,
Real values[],
Real solution[] )
{
int i;
for_less( i, 0, n )
solution[i] = values[i];
return( scaled_maximal_pivoting_gaussian_elimination_real( n, coefs, 1,
&solution ) );
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : invert_square_matrix
@INPUT : n
matrix - n by n matrix
@OUTPUT : inverse
@RETURNS : TRUE if successful
@DESCRIPTION: Computes the inverse of a square matrix.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 10, 1995 David MacDonald
@MODIFIED :
---------------------------------------------------------------------------- */
VIOAPI BOOLEAN invert_square_matrix(
int n,
Real **matrix,
Real **inverse )
{
Real tmp;
BOOLEAN success;
int i, j;
for_less( i, 0, n )
{
for_less( j, 0, n )
inverse[i][j] = 0.0;
inverse[i][i] = 1.0;
}
success = scaled_maximal_pivoting_gaussian_elimination_real( n, matrix,
n, inverse );
if( success )
{
for_less( i, 0, n-1 )
{
for_less( j, i+1, n )
{
tmp = inverse[i][j];
inverse[i][j] = inverse[j][i];
inverse[j][i] = tmp;
}
}
}
return( success );
}
|