00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <assert.h>
00033
00034 #include "sparse-matrix.h"
00035
00036
00037
00038
00039 I_Matrix_Row_Node newRowNode( int col, REAL_VALUE value ) {
00040
00041
00042
00043 I_Matrix_Row_Node new_node;
00044
00045 new_node = (I_Matrix_Row_Node ) malloc( sizeof( *new_node ));
00046 checkAllocatedPointer(new_node);
00047
00048 new_node->column = col;
00049 new_node->value = value;
00050 new_node->next = NULL;
00051
00052 return( new_node );
00053
00054 }
00055
00056 void destroyRow( I_Matrix_Row_Node row ) {
00057 I_Matrix_Row_Node temp;
00058
00059 while( row != NULL ) {
00060 temp = row;
00061 row = row->next;
00062 free( temp );
00063 }
00064 }
00065
00066 I_Matrix_Row_Node removeRowNode( I_Matrix_Row_Node row,
00067 int col, int *count ) {
00068
00069
00070
00071
00072
00073 I_Matrix_Row_Node temp_node, cur_node, trail_node;
00074
00075
00076 if( row == NULL )
00077 return( NULL );
00078
00079
00080 if( row->column == col ) {
00081 temp_node = row;
00082 row = row->next;
00083 free( temp_node );
00084 (*count)--;
00085 return( row );
00086 }
00087
00088 trail_node = row;
00089 cur_node = row->next;
00090 while( cur_node != NULL ) {
00091
00092
00093 if( cur_node->column == col ) {
00094 trail_node->next = cur_node->next;
00095 free( cur_node );
00096 (*count)--;
00097 return( row );
00098 }
00099
00100 trail_node = cur_node;
00101 cur_node = cur_node->next;
00102 }
00103
00104
00105
00106 return( row );
00107
00108 }
00109
00110 I_Matrix_Row_Node addEntryToRow( I_Matrix_Row_Node row,
00111 int col, REAL_VALUE value,
00112 int *count, int accumulate ) {
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128 I_Matrix_Row_Node new_node, cur_node, trail_node;
00129 trail_node = 0;
00130
00131
00132
00133
00134
00135 if( IS_ZERO( value ) ) {
00136 if ( accumulate )
00137 return ( row );
00138 else
00139 return( removeRowNode( row, col, count ));
00140 }
00141
00142
00143 if( row == NULL ) {
00144 new_node = newRowNode( col, value );
00145 (*count)++;
00146 return( new_node );
00147 }
00148
00149
00150 if( row->column > col ) {
00151 new_node = newRowNode( col, value );
00152 new_node->next = row;
00153 (*count)++;
00154 return( new_node );
00155 }
00156
00157 cur_node = row;
00158 while( cur_node != NULL ) {
00159
00160
00161
00162 if( cur_node->column == col ) {
00163
00164 if( accumulate )
00165 cur_node->value += value;
00166 else
00167 cur_node->value = value;
00168
00169 return( row );
00170 }
00171
00172
00173
00174 if( cur_node->column > col ) {
00175 new_node = newRowNode( col, value );
00176 trail_node->next = new_node;
00177 new_node->next = cur_node;
00178 (*count)++;
00179 return( row );
00180 }
00181
00182 trail_node = cur_node;
00183 cur_node = cur_node->next;
00184 }
00185
00186
00187
00188
00189
00190 new_node = newRowNode( col, value );
00191 trail_node->next = new_node;
00192 (*count)++;
00193 return( row );
00194
00195 }
00196
00197 void displayRow( I_Matrix_Row_Node row ) {
00198
00199 if( row == NULL )
00200 printf( "<empty>");
00201
00202 while( row != NULL ) {
00203 printf("[%d] %.3f ", row->column, row->value );
00204 row = row->next;
00205 }
00206
00207 printf( "\n");
00208 }
00209
00210
00211
00212
00213 I_Matrix newIMatrix( int num_rows ) {
00214 I_Matrix i_matrix;
00215
00216 i_matrix = (I_Matrix) malloc( sizeof( *i_matrix));
00217 checkAllocatedPointer(i_matrix);
00218
00219 i_matrix->num_rows = num_rows;
00220
00221 i_matrix->row = (I_Matrix_Row_Node *)
00222 calloc( num_rows, sizeof( *(i_matrix->row) ));
00223
00224 i_matrix->row_length = (int *) calloc( num_rows, sizeof( int ));
00225
00226 return( i_matrix );
00227 }
00228
00229 void destroyIMatrix( I_Matrix i_matrix ) {
00230 int i;
00231
00232 free( i_matrix->row_length );
00233
00234 for( i = 0; i < i_matrix->num_rows; i++ )
00235 destroyRow( i_matrix->row[i] );
00236 free( i_matrix->row );
00237
00238 free( i_matrix );
00239
00240 }
00241
00242 int addEntryToIMatrix( I_Matrix i_matrix, int row,
00243 int col, REAL_VALUE value ) {
00244
00245 assert(( i_matrix != NULL)
00246 && (row >=0) && ( row < i_matrix->num_rows ));
00247
00248 i_matrix->row[row] = addEntryToRow( i_matrix->row[row], col, value,
00249 &(i_matrix->row_length[row]), 0 );
00250
00251 return( 1 );
00252 }
00253
00254 int accumulateEntryInIMatrix( I_Matrix i_matrix, int row,
00255 int col, REAL_VALUE value ) {
00256
00257
00258
00259
00260
00261
00262 assert(( i_matrix != NULL)
00263 && (row >=0) && ( row < i_matrix->num_rows ));
00264
00265 i_matrix->row[row] = addEntryToRow( i_matrix->row[row], col, value,
00266 &(i_matrix->row_length[row]), 1 );
00267
00268 return( 1 );
00269 }
00270
00271 int countEntriesInIMatrix( I_Matrix i_matrix ) {
00272 int i;
00273 int total = 0;
00274
00275 for( i = 0; i < i_matrix->num_rows; i++ )
00276 total += i_matrix->row_length[i];
00277
00278 return( total );
00279 }
00280
00281 REAL_VALUE sumIMatrixRowValues( I_Matrix i_matrix, int row ) {
00282 REAL_VALUE sum = 0.0;
00283 I_Matrix_Row_Node list;
00284
00285 list = i_matrix->row[row];
00286
00287 while( list != NULL ) {
00288 sum += list->value;
00289 list = list->next;
00290 }
00291 return( sum );
00292 }
00293
00294 void displayIMatrix( I_Matrix i_matrix ) {
00295 int i;
00296
00297 for( i = 0; i < i_matrix->num_rows; i++ ) {
00298 printf( "(len=%d, sum =%.1f)Row=%d: ", i_matrix->row_length[i],
00299 sumIMatrixRowValues( i_matrix, i ), i );
00300 displayRow( i_matrix->row[i] );
00301
00302 }
00303
00304 }
00305
00306
00307
00308
00309 Matrix newMatrix( int num_rows, int num_non_zero ) {
00310 Matrix matrix;
00311
00312 matrix = (Matrix) malloc( sizeof( *matrix ));
00313 checkAllocatedPointer(matrix);
00314
00315 matrix->num_rows = num_rows;
00316 matrix->num_non_zero = num_non_zero;
00317
00318 matrix->mat_val = (REAL_VALUE *)
00319 calloc( num_non_zero, sizeof( REAL_VALUE ));
00320 matrix->col = (int *)
00321 calloc( num_non_zero, sizeof( int ));
00322 matrix->row_start = (int *)
00323 calloc( num_rows, sizeof( int ));
00324 matrix->row_length = (int *)
00325 calloc( num_rows, sizeof( int ));
00326
00327 return( matrix );
00328 }
00329
00330 void destroyMatrix( Matrix matrix ) {
00331
00332 if(matrix != NULL)
00333 {
00334 if(matrix->row_length != NULL)
00335 free( matrix->row_length );
00336
00337 if(matrix->row_start != NULL)
00338 free( matrix->row_start );
00339
00340 if(matrix->col != NULL)
00341 free( matrix->col );
00342
00343 if(matrix->mat_val != NULL)
00344 free( matrix->mat_val );
00345
00346 free( matrix );
00347 }
00348 }
00349
00350 Matrix transformIMatrix( I_Matrix i_matrix ) {
00351
00352
00353
00354
00355
00356 Matrix matrix;
00357 int row;
00358 I_Matrix_Row_Node cur_node;
00359 int index = 0;
00360
00361
00362 matrix = newMatrix( i_matrix->num_rows,
00363 countEntriesInIMatrix( i_matrix ));
00364
00365
00366 for( row = 0; row < i_matrix->num_rows; row++ ) {
00367
00368 matrix->row_start[row] = index;
00369 matrix->row_length[row] = i_matrix->row_length[row];
00370
00371 cur_node = i_matrix->row[row];
00372 while( cur_node != NULL ) {
00373
00374 matrix->col[index] = cur_node->column;
00375 matrix->mat_val[index] = cur_node->value;
00376 index++;
00377
00378 cur_node = cur_node->next;
00379 }
00380
00381 }
00382
00383 assert( index == matrix->num_non_zero );
00384
00385 return( matrix );
00386 }
00387
00388 REAL_VALUE sumRowValues( Matrix matrix, int row ) {
00389 REAL_VALUE sum = 0.0;
00390 int col;
00391
00392 for( col = matrix->row_start[row];
00393 col < matrix->row_start[row] + matrix->row_length[row];
00394 col++ )
00395 sum += matrix->mat_val[col];
00396
00397 return( sum );
00398 }
00399
00400 void displayMatrix( Matrix matrix ) {
00401 int i, j;
00402
00403 for( i = 0; i < matrix->num_rows; i++ ) {
00404 printf( "(len=%d, sum=%.1f)Row=%d: ", matrix->row_length[i],
00405 sumRowValues( matrix, i ), i );
00406
00407
00408 if( matrix->row_length[i] == 0 )
00409 printf( "<empty>");
00410
00411 for( j = matrix->row_start[i];
00412 j < matrix->row_start[i] + matrix->row_length[i];
00413 j++ )
00414 printf("[%d] %.3f ", matrix->col[j], matrix->mat_val[j] );
00415
00416 printf( "\n");
00417
00418 }
00419
00420 }
00421
00422 REAL_VALUE getEntryMatrix( Matrix matrix, int row, int col ) {
00423
00424
00425
00426
00427
00428 int j;
00429
00430 for( j = matrix->row_start[row];
00431 j < matrix->row_start[row] + matrix->row_length[row];
00432 j++ )
00433
00434 if( matrix->col[j] == col )
00435 return( matrix->mat_val[j] );
00436
00437 return( 0.0 );
00438 }
00439
00440
00441
00442