GMatrix.cpp 29.5 KB
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 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
/***************************************************************************
 *                       GMatrix.cpp  -  matrix class                      *
 * ----------------------------------------------------------------------- *
 *  copyright            : (C) 2006 by Jurgen Knodlseder                   *
 * ----------------------------------------------------------------------- *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

/* __ Includes ___________________________________________________________ */
#include "GMatrix.hpp"

/* __ Namespaces _________________________________________________________ */
using namespace std;


/*==========================================================================
 =                                                                         =
 =                            Private functions                            =
 =                                                                         =
 ==========================================================================*/
void GMatrix::init_members(void)
{
  // Initialise GMatrix members
  m_rows       = 0;      // Number of rows
  m_cols       = 0;      // Number of columns
  m_elements   = 0;      // Logically used number of elements
  m_alloc      = 0;      // Allocated # of elements (>= m_elements)
  m_num_rowsel = 0;      // Number of selected rows (for comp. decomp.)
  m_num_colsel = 0;      // Number of selected columns (for comp. decomp.)
  m_data       = NULL;   // Matrix data
  m_colstart   = NULL;   // Column start indices (m_cols+1)
  m_rowsel     = NULL;   // Row selection (for compressed decomposition)
  m_colsel     = NULL;   // Column selection (for compressed decomposition)
  
  // Return
  return;
}
/***************************************************************************/
void GMatrix::copy_members(const GMatrix& m)
{
  // Copy matrix attributes
  m_rows       = m.m_rows;
  m_cols       = m.m_cols;
  m_elements   = m.m_elements;
  m_alloc      = m.m_alloc;
  m_num_rowsel = m.m_num_rowsel;
  m_num_colsel = m.m_num_colsel;

  // Allocate memory for column start array and copy content
  m_colstart = new int[m_cols+1];
  if (m_colstart == NULL)
    throw mem_alloc("GMatrix::copy_members(const Matrix&)", m_cols+1);
  for (int i = 0; i <= m_cols; ++i)
    m_colstart[i] = m.m_colstart[i];
  
  // Allocate memory for elements and copy them (only if there are elements)
  if (m.m_data != NULL && m_alloc > 0) {
    m_data = new double[m_alloc];
    if (m_data == NULL)
	  throw mem_alloc("GMatrix::copy_members(const Matrix&)", m_alloc);
    for (int i = 0; i < m_elements; ++i)
      m_data[i] = m.m_data[i];
  }
  
  // If there is a row selection then copy it
  if (m.m_rowsel != NULL && m_num_rowsel > 0) {
    m_rowsel = new int[m_num_rowsel];
    if (m_rowsel == NULL)
	  throw mem_alloc("GMatrix::copy_members(const Matrix&)", m_num_rowsel);
    for (int i = 0; i < m_num_rowsel; ++i)
      m_rowsel[i] = m.m_rowsel[i];
  }

  // If there is a column selection then copy it
  if (m.m_colsel != NULL && m_num_colsel > 0) {
    m_colsel = new int[m_num_colsel];
    if (m_colsel == NULL)
	  throw mem_alloc("GMatrix::copy_members(const Matrix&)", m_num_colsel);
    for (int i = 0; i < m_num_colsel; ++i)
      m_colsel[i] = m.m_colsel[i];
  }

  // Return
  return;
}
/***************************************************************************/
void GMatrix::free_members(void)
{
  // De-allocate only if memory has indeed been allocated
  if (m_colsel   != NULL) delete [] m_colsel;
  if (m_rowsel   != NULL) delete [] m_rowsel;
  if (m_data     != NULL) delete [] m_data;
  if (m_colstart != NULL) delete [] m_colstart;

  // Properly mark members as free
  m_colstart = NULL;
  m_data     = NULL;
  m_rowsel   = NULL;
  m_colsel   = NULL;
  
  // Return
  return;
}


/*==========================================================================
 =                                                                         =
 =                      GMatrix constructors/destructors                   =
 =                                                                         =
 ==========================================================================*/

/***************************************************************************
 *   GMatrix private constructor (used for derived class initialization)   *
 ***************************************************************************/
GMatrix::GMatrix(void)
{
  // Initialise private members for clean destruction
  init_members();

  // Return
  return;
}


/***************************************************************************
 *                           GMatrix constructor                           *
 ***************************************************************************/
GMatrix::GMatrix(int rows, int cols)
{
  // Initialise private members for clean destruction
  init_members();

  // Determine number of elements to store in matrix
  int elements = rows*cols;

  // Throw exception if requested matrix size is zero
  if (elements == 0)
    throw empty("GMatrix constructor");
	
  // Allocate matrix array and column start index array. Throw an exception 
  // if allocation failed
  m_data     = new double[elements];
  m_colstart = new int[cols+1];
  if (m_data == NULL || m_colstart == NULL)
	throw mem_alloc("GMatrix constructor", elements);
	
  // Store matrix size (logical, storage, allocated)
  m_rows     = rows;
  m_cols     = cols;
  m_elements = elements;
  m_alloc    = elements;
  
  // Set-up column start indices
  m_colstart[0] = 0;
  for (int col = 1; col <= m_cols; ++col)
    m_colstart[col] = m_colstart[col-1] + m_rows;
	
  // Initialise matrix elements to 0.0
  for (int i = 0; i < m_elements; ++i)
    m_data[i] = 0.0;
	
  // Return
  return;
}


/***************************************************************************
 *                           GMatrix copy constructor                      *
 * ----------------------------------------------------------------------- *
 * The copy constructor is sufficiently general to provide the base        *
 * constructor for all derived classes, including sparse matrices.         *
 ***************************************************************************/
GMatrix::GMatrix(const GMatrix& m)
{
  // Initialise private members for clean destruction
  init_members();

  // Copy members
  copy_members(m);

  // Return
  return;
}


/***************************************************************************
 *                           GMatrix destructor                            *
 ***************************************************************************/
GMatrix::~GMatrix()
{
  // Free members
  free_members();

  // Return
  return;
}


/*==========================================================================
 =                                                                         =
 =                             GMatrix operators                           =
 =                                                                         =
 ==========================================================================*/

/***************************************************************************
 *                        GMatrix assignment operator                      *
 ***************************************************************************/
GMatrix& GMatrix::operator= (const GMatrix& m)
{
  // Execute only if object is not identical
  if (this != &m) { 

    // Free members
    free_members();
  
    // Initialise private members for clean destruction
    init_members();

    // Copy members
    copy_members(m);
	
  } // endif: object was not identical

  // Return this object
  return *this;
}


/***************************************************************************
 *                           Vector multiplication                         *
 ***************************************************************************/
GVector GMatrix::operator* (const GVector& v) const
{
  // Raise an exception if the matrix and vector dimensions are not compatible
  if (m_cols != v.m_num)
    throw matrix_vector_mismatch("GMatrix::operator* (const GVector&)", 
	                             v.m_num, m_rows, m_cols);

  // Perform vector multiplication
  GVector result(m_rows);
  for (int row = 0; row < m_rows; ++row) {
    double sum = 0.0;
	for (int col = 0; col < m_cols; ++col)
	  sum += (*this)(row,col) * v(col);
	result(row) = sum;
  }
  
  // Return result
  return result;
}


/***************************************************************************
 *                   GMatrix comparison (equality operator)                *
 ***************************************************************************/
int GMatrix::operator== (const GMatrix &m) const
{
  // Initalise the result to 'equal matrices'
  int result = 1;
  
  // Perform comparison (only if matrix dimensions and number of physical
  // elements are identical; otherwise set result to 'false')
  if (m_rows == m.m_rows && m_cols == m.m_cols && m_elements == m.m_elements) {
    for (int i = 0; i < m_elements; ++i) {
	  if (m_data[i] != m.m_data[i]) {
	    result = 0;
		break;
	  }
	}
  }
  else
    result = 0;

  // Return result
  return result;
}


/***************************************************************************
 *                 GMatrix comparison (non-equality operator)              *
 ***************************************************************************/
int GMatrix::operator!= (const GMatrix &m) const
{
  // Initalise the result to 'equal matrices'
  int result = 0;

  // Perform comparison (only if matrix dimensions and number of physical
  // elements are identical; otherwise set result to 'true')
  if (m_rows == m.m_rows && m_cols == m.m_cols && m_elements == m.m_elements) {
    for (int i = 0; i < m_elements; ++i) {
	  if (m_data[i] != m.m_data[i]) {
	    result = 1;
		break;
	  }
	}
  }
  else
    result = 1;
	
  // Return result
  return result;
}


/***************************************************************************
 *                  GMatrix negation (unary minus operator)                *
 ***************************************************************************/
GMatrix GMatrix::operator- ( ) const
{
  // Copy argument into result
  GMatrix result = *this;
  
  // Negate result
  for (int i = 0; i < m_elements; ++i)
    result.m_data[i] = -result.m_data[i];
	
  // Return result	
  return result;
}


/***************************************************************************
 *                  GMatrix addition (unary addition operator)             *
 ***************************************************************************/
GMatrix& GMatrix::operator+= (const GMatrix& m)
{
  // Raise an exception if the matrix dimensions are not compatible
  if (m_rows != m.m_rows || m_cols != m.m_cols)
    throw matrix_mismatch("GMatrix::operator+= (const GMatrix&)", 
						  m_rows, m_cols, m.m_rows, m.m_cols);

  // Add matrices
  for (int i = 0; i < m_elements; ++i)
    m_data[i] += m.m_data[i];

  // Return result
  return *this;
}


/***************************************************************************
 *             GMatrix subtraction (unary subtraction operator)            *
 ***************************************************************************/
GMatrix& GMatrix::operator-= (const GMatrix& m)
{
  // Raise an exception if the matrix dimensions are not compatible
  if (m_rows != m.m_rows || m_cols != m.m_cols)
    throw matrix_mismatch("GMatrix::operator-= (const GMatrix&)", 
						  m_rows, m_cols, m.m_rows, m.m_cols);

  // Subtract matrices
  for (int i = 0; i < m_elements; ++i)
    m_data[i] -= m.m_data[i];

  // Return result
  return *this;
}


/***************************************************************************
 *          Matrix multiplication (unary multiplication operator)          *
 * ----------------------------------------------------------------------- *
 * In case of rectangular matrices the result matrix does not change and   *
 * the operations can be performed 'quasi' inplace (except of a vector).   *
 * For the general case the result matrix changes the size so for          *
 * simplicity a new matrix is allocated to hold the results.               *
 ***************************************************************************/
GMatrix& GMatrix::operator*= (const GMatrix& m)
{
  // Raise an exception if the matrix dimensions are not compatible
  if (m_cols != m.m_rows)
    throw matrix_mismatch("GMatrix::operator*= (const GMatrix&)", 
						  m_rows, m_cols, m.m_rows, m.m_cols);

  // Case A: Matrices are rectangular, so perform 'inplace' multiplication
  if (m_rows == m_cols) {
    for (int row = 0; row < m_rows; ++row) {
      GVector v_row = extract_row(row);
      for (int col = 0; col < m_cols; ++col) {
	    double sum = 0.0;
	    for (int i = 0; i < m_cols; ++i)
	      sum += v_row(i) * m(i,col);
        (*this)(row,col) = sum;
      }
    }
  }
  
  // Case B: Matrices are not rectangular, so we cannot work inplace
  else {
  
    // Allocate result matrix
	GMatrix result(m_rows, m.m_cols);
	
	// Loop over all elements of result matrix
    for (int row = 0; row < m_rows; ++row) {
      for (int col = 0; col < m.m_cols; ++col) {
	    double sum = 0.0;
	    for (int i = 0; i < m_cols; ++i)
	      sum += (*this)(row,i) * m(i,col);
		result(row,col) = sum;
	  }
	}
	
	// Assign result
	*this = result;
	
  } // endelse: matrices were not rectangular

  // Return result
  return *this;
}


/***************************************************************************
 *  GMatrix scalar multiplication (unary scalar multiplication operator)   *
 ***************************************************************************/
GMatrix& GMatrix::operator*= (const double& d)
{
  // Case A: If multiplicator is 0.0 then set entire matrix to 0.0
  if (d == 0.0) {
    for (int i = 0; i < m_elements; ++i)
      m_data[i] = 0.0;
  }
  
  // Case B: If multiplicator is not +/- 1.0 then perform multiplication
  else if (fabs(d) != 1.0) {
    for (int i = 0; i < m_elements; ++i)
      m_data[i] *= d;
  }
  
  // Case C: If multiplication is -1.0 then negate
  else if (d == -1.0) {
    for (int i = 0; i < m_elements; ++i)
      m_data[i] = -m_data[i];
  }
  
  // Return result
  return *this;
}


/***************************************************************************
 *         Matrix scalar division (unary scalar division operator)         *
 ***************************************************************************/
GMatrix& GMatrix::operator/= (const double& d)
{
  // Case A: If divider is not +/- 1.0 then perform division
  if (fabs(d) != 1.0) {
    for (int i = 0; i < m_elements; ++i)
      m_data[i] /= d;
  }
  
  // Case B: If divider is -1.0 then negate
  else if (d == -1.0) {
    for (int i = 0; i < m_elements; ++i)
      m_data[i] = -m_data[i];
  }
  
  // Return result
  return *this;
}


/*==========================================================================
 =                                                                         =
 =                          GMatrix member functions                       =
 =                                                                         =
 ==========================================================================*/

/***************************************************************************
 *                       Set all matrix elements to zero                   *
 ***************************************************************************/
void GMatrix::clear()
{
  // Loop over all matrix elements
  for (int i = 0; i < m_elements; ++i)
    m_data[i] = 0.0;
  
  // Return
  return;
}


/***************************************************************************
 *                    Get minimum physical matrix element                  *
 ***************************************************************************/
double GMatrix::min() const
{
  // Initialise minimum with first element
  double result = m_data[0];
  
  // Search all elements for the smallest one
  for (int i = 1; i < m_elements; ++i) {
    if (m_data[i] < result)
	  result = m_data[i];
  }
  
  // Return result
  return result;
}


/***************************************************************************
 *                    Get maximum physical matrix element                  *
 ***************************************************************************/
double GMatrix::max() const
{
  // Initialise minimum with first element
  double result = m_data[0];
  
  // Search all elements for the largest one
  for (int i = 1; i < m_elements; ++i) {
    if (m_data[i] > result)
	  result = m_data[i];
  }

  // Return result
  return result;
}


/***************************************************************************
 *                              Get matrix sum                             *
 ***************************************************************************/
double GMatrix::sum() const
{
  // Initialise matrix sum
  double result = 0.0;
  
  // Add all elements  
  for (int i = 0; i < m_elements; ++i)
    result += m_data[i];
  
  // Return result
  return result;
}


/***************************************************************************
 *                         Inplace transpose of matrix                     *
 * ----------------------------------------------------------------------- *
 * The transpose operation exchanges the number of rows against the number *
 * of columns. The element transformation is done inplace.                 * 
 ***************************************************************************/
void GMatrix::transpose()
{
  // Case A: matrix is rectangular then simply swap the elements
  if (m_rows == m_cols) {
    double  swap;
    double* ptr_dst;
    double* ptr_src;
    for (int row = 0; row < m_rows; ++row) {
      for (int col = row; col < m_cols; ++col) {
	    ptr_dst  = m_data + m_cols*row + col;
	    ptr_src  = m_data + m_rows*col + row;
	    swap     = *ptr_dst;
	    *ptr_dst = *ptr_src;
	    *ptr_src = swap;
	  }
    }
  }
  
  // Case B: Dummy for non-rectangular transpose
  else {
    GMatrix result(m_cols, m_rows);
    for (int row = 0; row < m_rows; ++row) {
      for (int col = 0; col < m_cols; ++col)
	    result(col, row) = (*this)(row, col);
	}
	*this = result;
  }
  
}


/***************************************************************************
 *                     Extract row from matrix into vector                 *
 ***************************************************************************/
GVector GMatrix::extract_row(int row) const
{
  // Raise an exception if the row index is invalid
  #if defined(G_RANGE_CHECK)
  if (row >= m_rows)
    throw out_of_range("GMatrix::extract_row(int)", 
	                   row, 0, m_rows, m_cols);
  #endif
  
  // Create result vector
  GVector result(m_cols);
  
  // Extract row into vector
  for (int col = 0; col < m_cols; ++col)
    result(col) = (*this)(row,col);
	
  // Return vector
  return result;

}


/***************************************************************************
 *                   Extract column from matrix into vector                *
 ***************************************************************************/
GVector GMatrix::extract_col(int col) const
{
  // Raise an exception if the column index is invalid
  #if defined(G_RANGE_CHECK)
  if (col >= m_cols)
    throw out_of_range("GMatrix::extract_col(int)", 
	                   0, col, m_rows, m_cols);
  #endif
  
  // Create result vector
  GVector result(m_rows);
  
  // Extract column into vector
  for (int row = 0; row < m_rows; ++row)
    result(row) = (*this)(row,col);
	
  // Return vector
  return result;

}


/*==========================================================================
 =                                                                         =
 =                     GMatrix private member functions                    =
 =                                                                         =
 ==========================================================================*/

/***************************************************************************
 *                              select_non_zero                            *
 * ----------------------------------------------------------------------- *
 * Determines the non-zero rows and columns in matrix and set up index     *
 * arrays that points to these rows/columns. These arraya are used for     *
 * compressed matrix factorisations.                                       *
 ***************************************************************************/
void GMatrix::select_non_zero(void)
{
  // Free existing selection arrays
  if (m_rowsel != NULL) delete [] m_rowsel;
  if (m_colsel != NULL) delete [] m_colsel;
  
  // Allocate selection arrays
  m_rowsel = new int[m_rows];
  if (m_rowsel == NULL)
    throw mem_alloc("GMatrix::select_non_zero()", m_rows);
  m_colsel = new int[m_cols];
  if (m_colsel == NULL)
    throw mem_alloc("GMatrix::select_non_zero()", m_cols);

  // Initialise non-zero row and column counters
  m_num_rowsel = 0;
  m_num_colsel = 0;
  
  // Declare loop variables
  int row;
  int col;
  
  // Find all non-zero rows
  for (row = 0; row < m_rows; ++row) {
    for (col = 0; col < m_cols; ++col) {
	  if ((*this)(row,col) != 0.0)
	    break;
	}
	if (col < m_cols)      // found a non-zero element in row
      m_rowsel[m_num_rowsel++] = row;
  }

  // Find all non-zero columns
  for (col = 0; col < m_cols; ++col) {
    for (row = 0; row < m_rows; ++row) {
	  if ((*this)(row,col) != 0.0)
	    break;
	}
	if (row < m_rows)      // found a non-zero element in column
      m_colsel[m_num_colsel++] = col;
  }
  
  // Return
  return;
}


/*==========================================================================
 =                                                                         =
 =                             GMatrix friends                             =
 =                                                                         =
 ==========================================================================*/

/***************************************************************************
 *                             Output operator                             *
 ***************************************************************************/
ostream& operator<< (ostream& os, const GMatrix& m)
{
  // Put header in stream
  os << "=== GMatrix ===" << endl;
  if (m.m_rowsel != NULL)
    os << " Number of rows ............: " << m.m_rows << " (compressed " <<
	      m.m_num_rowsel << ")" << endl;
  else
    os << " Number of rows ............: " << m.m_rows << endl;
  if (m.m_colsel != NULL)
    os << " Number of columns .........: " << m.m_cols << " (compressed " <<
	      m.m_num_colsel << ")" << endl;
  else
    os << " Number of columns .........: " << m.m_cols << endl;
  os << " Number of elements ........: " << m.m_elements << endl;
  os << " Number of allocated cells .: " << m.m_alloc << endl;

  // Loop over all matrix elements
  for (int row = 0; row < m.m_rows; ++row) {
    os << " ";
    for (int col = 0; col < m.m_cols; ++col) {
      os << m(row,col);
	  if (col != m.m_cols-1)
	    os << ", ";
	}
	if (row != m.m_rows-1)
	  os << endl;
  }

  // If there is a row compression the show scheme
  if (m.m_rowsel != NULL) {
    os << endl << " Row selection ..:";
    for (int row = 0; row < m.m_num_rowsel; ++row)
      os << " " << m.m_rowsel[row];
  }

  // If there is a column compression the show scheme
  if (m.m_colsel != NULL) {
    os << endl << " Column selection:";
    for (int col = 0; col < m.m_num_colsel; ++col)
      os << " " << m.m_colsel[col];
  }
  
  // Return output stream
  return os;
}


/***************************************************************************
 *                          Matrix absolute values                         *
 ***************************************************************************/
GMatrix fabs(const GMatrix& m)
{
  // Define result matrix
  GMatrix result = m;
  
  // Convert all elements to absolute values  
  for (int i = 0; i < result.m_elements; ++i)
    result.m_data[i] = fabs(result.m_data[i]);
  
  // Return result
  return result;
}


/*==========================================================================
 =                                                                         =
 =                        GMatrix exception classes                        =
 =                                                                         =
 ==========================================================================*/

/***************************************************************************
 *                      Matrix row or column out of range                  *
 ***************************************************************************/
GMatrix::out_of_range::out_of_range(string origin, int row, int col, 
                                    int rows, int cols)
{
  m_origin = origin;
  ostringstream s_row;
  ostringstream s_col;
  ostringstream s_rows;
  ostringstream s_cols;
  s_row  << row;
  s_col  << col;
  s_rows << rows-1;
  s_cols << cols-1;
  m_message = "Matrix element (" + s_row.str() + "," + s_col.str() +
              ") out of range ([0," + s_rows.str() + "], [0," +
			  s_cols.str() + "])";
}


/***************************************************************************
 *                     Mismatch between matrix and vector                  *
 ***************************************************************************/
GMatrix::matrix_vector_mismatch::matrix_vector_mismatch(string origin, 
                                                int num, int rows, int cols)
{
  m_origin = origin;
  ostringstream s_num;
  ostringstream s_rows;
  ostringstream s_cols;
  s_num  << num;
  s_rows << rows;
  s_cols << cols;
  m_message = "Vector dimension [" + s_num.str() + 
              "] is incompatible with matrix size [" + 
			  s_rows.str() + "," + s_cols.str() + "]";
}


/***************************************************************************
 *                       Matrix mismatch in operation                      *
 ***************************************************************************/
GMatrix::matrix_mismatch::matrix_mismatch(string origin, int rows1, 
                                            int cols1, int rows2, int cols2)
{
  m_origin = origin;
  ostringstream s_rows1;
  ostringstream s_rows2;
  ostringstream s_cols1;
  ostringstream s_cols2;
  s_rows1 << rows1;
  s_cols1 << cols1;
  s_rows2 << rows2;
  s_cols2 << cols2;
  m_message = "Matrix mismatch: M1(" + s_rows1.str() + "," + s_cols1.str() +
              ") incompatible with M2(" + s_rows2.str() + "," + 
			  s_cols2.str() + ")";
}


/***************************************************************************
 *                           Matrix not rectangular                        *
 ***************************************************************************/
GMatrix::matrix_not_rect::matrix_not_rect(string origin, int rows1, 
                                            int cols1, int rows2, int cols2)
{
  m_origin = origin;
  ostringstream s_rows1;
  ostringstream s_rows2;
  ostringstream s_cols1;
  ostringstream s_cols2;
  s_rows1 << rows1;
  s_cols1 << cols1;
  s_rows2 << rows2;
  s_cols2 << cols2;
  m_message = "Operation only valid for rectangular matrices: M[" + 
              s_rows1.str() + "," + s_cols1.str() +
              "] *= M[" + s_rows2.str() + "," + s_cols2.str() + 
			  "] not allowed";
}


/***************************************************************************
 *                     Matrix is not positive definite                     *
 ***************************************************************************/
GMatrix::matrix_not_pos_definite::matrix_not_pos_definite(string origin, 
                                                        int row, double sum)
{
  m_origin = origin;
  ostringstream s_rows;
  ostringstream s_sum;
  s_rows << row;
  s_sum << scientific << sum;
  m_message = "Matrix is not positive definite (sum " + s_sum.str() + 
              " occured in row/column " + s_rows.str() + ")";
}


/***************************************************************************
 *                         Matrix is not symmetric                         *
 ***************************************************************************/
GMatrix::matrix_not_symmetric::matrix_not_symmetric(string origin, int cols, 
                                                                   int rows)
{
  m_origin = origin;
  ostringstream s_rows;
  ostringstream s_cols;
  s_rows << rows;
  s_cols << cols;
  m_message = "Matrix is not symmetric [" + s_rows.str() + "," +
			  s_cols.str() + "]";
}


/***************************************************************************
 *                       Matrix has not been factorised                    *
 ***************************************************************************/
GMatrix::matrix_not_factorised::matrix_not_factorised(string origin, string type)
{
  m_origin  = origin;
  m_message = "Matrix has not been factorised using " + type;
}


/***************************************************************************
 *                       All matrix elements are zero                      *
 ***************************************************************************/
GMatrix::matrix_zero::matrix_zero(string origin)
{
  m_origin = origin;
  m_message = "All matrix elements are zero";
}