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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//- Class: CubitSparseMatrix<--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
//-
//- Description: This file defines the CubitSparseMatrix class which is a
//- sparse NxM Matrix.
//-
//- Author: Matt Staten
//- Data: 4/15/2011
//- Checked by:

#include "CubitSparseMatrix.hpp"
#include "CubitMessage.hpp"
#include "CubitDefines.h"
#include "CubitFile.hpp"

#include <vector>
#include <set>
#include <stdexcept>
#include <stdio.h>

using std::vector;
using std::set;
using std::map;

//==============================================================================
// Description:  Constructor
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
CubitSparseMatrix::CubitSparseMatrix
(
  int num_rows,
  int num_cols,
  vector<int> &is,
  vector<int> &js,
  vector<double> &es
)
{
  numRows = numCols = 0;
  reset( num_rows, num_cols, is, js, es );
}

//==============================================================================
// Description: Constructor
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
CubitSparseMatrix::CubitSparseMatrix()
{
  numRows = numCols = 0;
}

//==============================================================================
// Description: 
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::reset
(
  int num_rows,
  int num_cols,
  vector<int> &is,
  vector<int> &js,
  vector<double> &es
)
{

  if( is.size() != js.size() ||
      is.size() != es.size() )
  {
    throw std::invalid_argument("The sizes of is, js, and es must be the same" );
  }

  delete_data();

  int num_data = is.size();

  numRows = num_rows;
  numCols = num_cols;
  for ( int i = 0; i < num_data; i++ )
  {
    add( is[i], js[i], es[i] );
  }
}

//==============================================================================
// Description: Delete all data, free all memory, and set size to 0.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::clear()
{
  numRows = 0;
  numCols = 0;
  delete_data();
}

//==============================================================================
// Description: 
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::add( int row, int col, double data )
{
  if ( row+1 > numRows ||
       col+1 > numCols )
  {
    throw std::invalid_argument( "The dimensions of the 2 matrices must be the same." );
  }

  map<int,double> *submap = NULL;
  map<int,map<int,double>*>::iterator iter = matrixData.find( row );
  if ( iter == matrixData.end() )
  {
    submap = new map<int,double>;
    matrixData.insert( std::pair<int, map<int,double>*>(row, submap) );
  }
  else
  {
    submap = iter->second;
    map<int,double>::iterator iter2 = submap->find( col );
    if ( iter2 != submap->end() )
    {
      iter2->second += data;
      return;
    }
  }
  submap->insert( std::pair<int, double>( col, data ) );
}

//==============================================================================
// Description:  destructor
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
CubitSparseMatrix::~CubitSparseMatrix()
{
  delete_data();
}

//==============================================================================
// Description:  Delete all data in this matrix.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::delete_data()
{
  while ( !matrixData.empty() )
  {
    map<int,double> *submap = matrixData.begin()->second;
    matrixData.begin()->second = NULL;
    matrixData.erase( matrixData.begin() );
    delete submap;
  }
}
  
  // Create a matrix containing the rows and cols of this that are true in
  // rows_to_include and cols_to_include.
//==============================================================================
// Description:  retrieve specified portions of this matrix.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::sub_matrix
(
  const vector<bool> &rows_to_include,
  const vector<bool> &cols_to_include,
  CubitSparseMatrix &submatrix
)
{
  if ( (int) rows_to_include.size() != numRows )
  {
    throw std::invalid_argument( "The length of rows_to_include must the the same as the number of rows in the matrix." );
  }
  if ( (int) cols_to_include.size() != numCols )
  {
    throw std::invalid_argument( "The length of cols_to_include must the the same as the number of colums in the matrix." );
  }

  vector<int> is;
  vector<int> js;
  vector<double> es;
  int i;

  int *row_indices = new int[numRows];
  int *col_indices = new int[numCols];
  int num_rows = 0;
  int num_cols = 0;
  for ( i = 0; i < numRows; i++ )
  {
    if ( rows_to_include[i] )
    {
      row_indices[i] = num_rows;
      num_rows++;
    }
    else
    {
      row_indices[i] = 0;
    }
  }

  for ( i = 0; i < numCols; i++ )
  {
    if ( cols_to_include[i] )
    {
      col_indices[i] = num_cols;
      num_cols++;
    }
    else
    {
      col_indices[i] = 0;
    }
  }

  map<int, map<int,double>*>::iterator iter1 = matrixData.begin();
  while ( iter1 != matrixData.end() )
  {
    int row = iter1->first;
    map<int,double>*submap = iter1->second;
    iter1++;<--- Prefer prefix ++/-- operators for non-primitive types.

    if ( !rows_to_include[row] )
      continue;

    map<int,double>::iterator iter2 = submap->begin();
    while ( iter2 != submap->end() )
    {
      int col = iter2->first;
      double data = iter2->second;
      iter2++;<--- Prefer prefix ++/-- operators for non-primitive types.
      if ( !cols_to_include[col] )
        continue;

      is.push_back( row_indices[row] );
      js.push_back( col_indices[col] );
      es.push_back( data );
    }
  }

  delete [] col_indices;
  delete [] row_indices;

  submatrix.reset( num_rows, num_cols, is, js, es );
}

//==============================================================================
// Description:  Multiply this matrix by a vector.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
vector<double> CubitSparseMatrix::operator* (const vector<double> &vec ) const
{
  if ( (int) vec.size() != numCols )
  {
    throw std::invalid_argument( "The length of the input vector must be the same as the number of colums in the matrix." );
  }

  vector<double> ans( numRows );

  int i;
  for ( i = 0; i < numRows; i++ )
  {
    ans[i] = 0.0;
  }

  map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin();
  while ( iter1 != matrixData.end() )
  {
    int row = iter1->first;
    map<int,double>*submap = iter1->second;
    iter1++;<--- Prefer prefix ++/-- operators for non-primitive types.

    map<int,double>::iterator iter2 = submap->begin();
    while ( iter2 != submap->end() )
    {
      int col = iter2->first;
      double data = iter2->second;
      iter2++;<--- Prefer prefix ++/-- operators for non-primitive types.

      ans[row] += data * vec[col];
    }
  }
  return ans;
}

//==============================================================================
// Description:  retrieve a row of this matrix.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
const map<int,double> *CubitSparseMatrix::get_row( int row ) const<--- The function 'get_row' is never used.
{
  if ( row < 0 || row >= numRows )
  {
    throw std::invalid_argument( "The row index must be greater than or equal to 0, and less than the number of rows in the matrix." );
  }

  map<int, map<int,double>*>::const_iterator iter = matrixData.find( row );
  if ( iter == matrixData.end() )
    return NULL;
  return iter->second;
}

//==============================================================================
// Description: debug print of this matrix.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::print( char *filename ) const
{
  printf( "CubitSparseMatrix::numRows: %d\n", numRows );
  printf( "CubitSparseMatrix::numCols: %d\n", numCols );
  int min, max;
  double ave;
  num_non_zeros_per_row( ave, max, min );

  printf( "CubitSparseMatrix::Average # Non Zeros per row: %f\n", ave );
  printf( "CubitSparseMatrix::Minimum # Non Zeros per row: %d\n", min );
  printf( "CubitSparseMatrix::Maximum # Non Zeros per row: %d\n", max );

  CubitFile fp;
  if ( filename )
    fp.open( filename, "w" );

  map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin();
  while ( iter1 != matrixData.end() )
  {
    int row = iter1->first;
    map<int,double>*submap = iter1->second;
    iter1++;<--- Prefer prefix ++/-- operators for non-primitive types.

    map<int,double>::iterator iter2 = submap->begin();
    while ( iter2 != submap->end() )
    {
      int col = iter2->first;
      double data = iter2->second;
      iter2++;<--- Prefer prefix ++/-- operators for non-primitive types.

      if ( fp.file() )
      {
        fprintf( fp.file(), "%d %d %f\n",
                row, col, data );
      }
      else
      {
      printf( "CubitSparseMatrix::(%d,%d) : %f\n",
              row, col, data );
    }
  }
  }
}

//==============================================================================
// Description: debug print of this matrix.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::num_non_zeros_per_row
(
  double &ave,
  int &max,
  int &min
) const
{
  max = 0;
  min = numCols;
  ave = 0.0;
  
  map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin();
  while ( iter1 != matrixData.end() )
  {
    //iter1->first;
    map<int,double>*submap = iter1->second;
    iter1++;<--- Prefer prefix ++/-- operators for non-primitive types.

    int nz = submap->size();
    ave += nz;

    if ( nz > max ) max = nz;
    if ( nz < min ) min = nz;
  }
  if ( numCols > 0 )
    ave /= numCols;
}

//==============================================================================
// Description: Add an identity matrix to this matrix.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
void CubitSparseMatrix::plus_identity()
{
  for ( int i = 0; i < numRows; i++ )
  {
    if ( i > numCols ) break;
    add( i, i, 1.0 );
  }
}

//==============================================================================
// Description: return the number of non-zeros in this matrix.
// Notes:  
// Author: mlstate
// Date: 4/19/2011
//==============================================================================
int CubitSparseMatrix::num_non_zeros( void ) const<--- The function 'num_non_zeros' is never used.
{
  int num_nz = 0;
  map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin();
  while ( iter1 != matrixData.end() )
  {
    //iter1->first;
    map<int,double>*submap = iter1->second;
    if ( submap ) num_nz += submap->size();
    iter1++;<--- Prefer prefix ++/-- operators for non-primitive types.
  }
  return num_nz;
}