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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
/******************************************************************
* *
* File : dense.c *
* Programmers : Scott D. Cohen and Alan C. Hindmarsh @ LLNL *
* Last Modified : 1 September 1994 *
*----------------------------------------------------------------*
* This is the implementation file for a generic DENSE linear *
* solver package. *
* *
******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "dense.h"
#include "llnltyps.h"
#include "vector.h"
#include "llnlmath.h"
#include "ggets.h"
#define ZERO RCONST(0.0)
#define ONE RCONST(1.0)
/* Implementation */
DenseMat DenseAllocMat(integer N)
{
DenseMat A;
if (N <= 0) return(NULL);
A = (DenseMat) malloc(sizeof *A);
if (A==NULL) return (NULL);
A->data = denalloc(N);
if (A->data == NULL) {
free(A);
return(NULL);
}
A->size = N;
return(A);
}
integer *DenseAllocPiv(integer N)
{
if (N <= 0) return(NULL);
return((integer *) malloc(N * sizeof(integer)));
}
integer DenseFactor(DenseMat A, integer *p)
{
return(gefa(A->data, A->size, p));
}
void DenseBacksolve(DenseMat A, integer *p, N_Vector b)
{
gesl(A->data, A->size, p, N_VDATA(b));
}
void DenseZero(DenseMat A)
{
denzero(A->data, A->size);
}
void DenseCopy(DenseMat A, DenseMat B)
{
dencopy(A->data, B->data, A->size);
}
void DenseScale(real c, DenseMat A)
{
denscale(c, A->data, A->size);
}
void DenseAddI(DenseMat A)
{
denaddI(A->data, A->size);
}
void DenseFreeMat(DenseMat A)
{
denfree(A->data);
free(A);
}
void DenseFreePiv(integer *p)
{
free(p);
}
void DensePrint(DenseMat A)
{
denprint(A->data, A->size);
}
real **denalloc(integer n)
{
integer j;
real **a;
if (n <= 0) return(NULL);
a = (real **) malloc(n * sizeof(real *));
if (a == NULL) return(NULL);
a[0] = (real *) malloc(n * n * sizeof(real));
if (a[0] == NULL) {
free(a);
return(NULL);
}
for (j=1; j < n; j++) a[j] = a[0] + j * n;
return(a);
}
integer *denallocpiv(integer n)
{
if (n <= 0) return(NULL);
return((integer *) malloc(n * sizeof(integer)));
}
integer gefa(real **a, integer n, integer *p)
{
integer i, j, k, l;
real *col_j, *col_k, *diag_k;
real temp, mult, a_kj;
bool swap;
/* k = elimination step number */
for (k=0; k < n-1; k++, p++) {
col_k = a[k];
diag_k = col_k + k;
/* find l = pivot row number */
l=k;
for (i=k+1; i < n; i++)
if (ABS(col_k[i]) > ABS(col_k[l])) l=i;
*p = l;
/* check for zero pivot element */
if (col_k[l] == ZERO) return(k+1);
/* swap a(l,k) and a(k,k) if necessary */
if ((swap = (l != k))) {
temp = col_k[l];
col_k[l] = *diag_k;
*diag_k = temp;
}
/* Scale the elements below the diagonal in */
/* column k by -1.0 / a(k,k). After the above swap, */
/* a(k,k) holds the pivot element. This scaling */
/* stores the pivot row multipliers -a(i,k)/a(k,k) */
/* in a(i,k), i=k+1, ..., n-1. */
mult = -ONE / (*diag_k);
for(i=k+1; i < n; i++)
col_k[i] *= mult;
/* row_i = row_i - [a(i,k)/a(k,k)] row_k, i=k+1, ..., n-1 */
/* row k is the pivot row after swapping with row l. */
/* The computation is done one column at a time, */
/* column j=k+1, ..., n-1. */
for (j=k+1; j < n; j++) {
col_j = a[j];
a_kj = col_j[l];
/* Swap the elements a(k,j) and a(k,l) if l!=k. */
if (swap) {
col_j[l] = col_j[k];
col_j[k] = a_kj;
}
/* a(i,j) = a(i,j) - [a(i,k)/a(k,k)]*a(k,j) */
/* a_kj = a(k,j), col_k[i] = - a(i,k)/a(k,k) */
if (a_kj != ZERO) {
for (i=k+1; i < n; i++)
col_j[i] += a_kj * col_k[i];
}
}
}
/* set the last pivot row to be n-1 and check for a zero pivot */
*p = n-1;
if (a[n-1][n-1] == ZERO) return(n);
/* return 0 to indicate success */
return(0);
}
void gesl(real **a, integer n, integer *p, real *b)
{
integer k, l, i;
real mult, *col_k;
/* Solve Ly = Pb, store solution y in b */
for (k=0; k < n-1; k++) {
l = p[k];
mult = b[l];
if (l != k) {
b[l] = b[k];
b[k] = mult;
}
col_k = a[k];
for (i=k+1; i < n; i++)
b[i] += mult*col_k[i];
}
/* Solve Ux = y, store solution x in b */
for (k=n-1; k >= 0; k--) {
col_k = a[k];
b[k] /= col_k[k];
mult = -b[k];
for (i=0; i < k; i++)
b[i] += mult*col_k[i];
}
}
void denzero(real **a, integer n)
{
integer i, j;
real *col_j;
for (j=0; j < n; j++) {
col_j = a[j];
for (i=0; i < n; i++)
col_j[i] = ZERO;
}
}
void dencopy(real **a, real **b, integer n)
{
integer i, j;
real *a_col_j, *b_col_j;
for (j=0; j < n; j++) {
a_col_j = a[j];
b_col_j = b[j];
for (i=0; i < n; i++)
b_col_j[i] = a_col_j[i];
}
}
void denscale(real c, real **a, integer n)
{
integer i, j;
real *col_j;
for (j=0; j < n; j++) {
col_j = a[j];
for (i=0; i < n; i++)
col_j[i] *= c;
}
}
void denaddI(real **a, integer n)
{
integer i;
for (i=0; i < n; i++) a[i][i] += ONE;
}
void denfreepiv(integer *p)
{
free(p);
}
void denfree(real **a)
{
free(a[0]);
free(a);
}
void denprint(real **a, integer n)
{
integer i, j;
plintf("\n");
for (i=0; i < n; i++) {
for (j=0; j < n; j++) {
plintf("%10g", a[j][i]);
}
plintf("\n");
}
plintf("\n");
}
|