src/Matrix/i_matrix.cc
/* These functions allow for simple mathematical operations between a identity
   matrix and another matrix of unspecified type. Note that these functions
   return a pointer to the resulting array. That array must have it referencing
   done in the main matrix class or there will be memory leakage.

   Function    Result                   Details
   --------   --------   ---------------------------------------------------
     add       imx+mx    Adjusts to mx type, sums only over active elements
   subtract    imx-mx    Adjusts to mx type, subtact only over active elems.
   multiply    imx*mx    Reduced calculation using only nonzero imx elements
   multiply    imx*z     Fast calculation only over imx stored elements

*/
These functions allow for simple mathematical operations between a identity
matrix and another matrix of unspecified type. Note that these functions
return a pointer to the resulting array. That array must have it referencing
done in the main matrix class or there will be memory leakage.

Function    Result                   Details

add       imx+mx    Adjusts to mx type, sums only over active elements
subtract    imx-mx    Adjusts to mx type, subtact only over active elems.
multiply    imx*mx    Reduced calculation using only nonzero imx elements
multiply    imx*z     Fast calculation only over imx stored elements
These functions allow for simple mathematical operations between a identity matrix and another matrix of unspecified type . Note that these functions return a pointer to the resulting array . That array must have it referencing done in the main matrix class or there will be memory leakage . Function Result Details add imx + mx Adjusts to mx type sums only over active elements subtract imx - mx Adjusts to mx type subtact only over active elems . multiply imx * mx Reduced calculation using only nonzero imx elements multiply imx * z Fast calculation only over imx stored elements
_matrix* i_matrix::add(_matrix* mx)
  {
  if(!CheckDims(mx, 1)) IMxfatal(20);		
  switch (mx->stored_type())
    {
      case n_matrix_type:			
	{ 
	n_matrix* sum =				
                 new n_matrix(*(n_matrix*)mx);	
	for(int i=0; i<cols_; i++)		
          ((n_matrix*)sum)->data[i*cols_+i]
                                      += complex1;
	return sum;
	}
	break;
      case h_matrix_type:			
	{
	h_matrix* sum				
                = new h_matrix(*(h_matrix*)mx);
	for(int i=0; i<cols_; i++)		
	  sum->data[i*cols_-(i*(i-1))/2]	
                                      += complex1;
	return sum;
	}
      case d_matrix_type:			
	{	
	d_matrix* sum				
                = new d_matrix(*(d_matrix*)mx);
	for(int i=0; i<cols_; i++)		
	  sum->data[i] += complex1;		
	return sum;
	}
	break;
      case i_matrix_type:			
	return new d_matrix(cols_,		
                             cols_,complex(2));	
	break;					
      default:					
	{ 
	n_matrix* sum = new n_matrix(cols_,cols_);
	int pos = 0;
	for(int i=0; i<cols_; i++)
	  for(int j=0; j<cols_; j++,pos++)
	    if(i==j)
	      sum->data[pos]=(*mx)(i,j)+complex1;
	    else
	      sum->data[pos]=(*mx)(i,j);
	return sum;
	}
    }
  }
_matrix * i_matrix :: add ( _matrix * mx ) { if ( ! CheckDims ( mx , 1 ) ) IMxfatal ( 20 ) ; switch ( mx -> stored_type ( ) ) { case n_matrix_type : { n_matrix * sum = new n_matrix ( * ( n_matrix * ) mx ) ; for ( int i = 0 ; i < cols_ ; i ++ ) ( ( n_matrix * ) sum ) -> data [ i * cols_ + i ] += complex1 ; return sum ; } break ; case h_matrix_type : { h_matrix * sum = new h_matrix ( * ( h_matrix * ) mx ) ; for ( int i = 0 ; i < cols_ ; i ++ ) sum -> data [ i * cols_ - ( i * ( i - 1 ) ) / 2 ] += complex1 ; return sum ; } case d_matrix_type : { d_matrix * sum = new d_matrix ( * ( d_matrix * ) mx ) ; for ( int i = 0 ; i < cols_ ; i ++ ) sum -> data [ i ] += complex1 ; return sum ; } break ; case i_matrix_type : return new d_matrix ( cols_ , cols_ , complex ( 2 ) ) ; break ; default : { n_matrix * sum = new n_matrix ( cols_ , cols_ ) ; int pos = 0 ; for ( int i = 0 ; i < cols_ ; i ++ ) for ( int j = 0 ; j < cols_ ; j ++ , pos ++ ) if ( i == j ) sum -> data [ pos ] = ( * mx ) ( i , j ) + complex1 ; else sum -> data [ pos ] = ( * mx ) ( i , j ) ; return sum ; } } }
These functions allow for simple mathematical operations between a identity
matrix and another matrix of unspecified type.
These functions allow for simple mathematical operations between a identity matrix and another matrix of unspecified type .
// Quit if mismatch // Add a n_matrix to an i_matrix // Construct new n_matrix equal to mx // <i|sum|j> = <i|mx|j> // <i|sum|i> += <i|imx|i> = 1 // Add a h_matrix to an i_matrix // Construct a new h_matrix equal to mx // <i|sum|j> = <i|hmx|j> // <i|sum|i> += <i|imx|i> = 1 // Add a d_matrix to an i_matrix // Construct new d_matrix equal to mx // <i|sum|j> = <i|dmx|j> // <i|sum|i> += <i|imx|i> = 1 // Add two i_matrices // Return is a diagonal array // <i|sum|j> = del   * 2.0 //                i,j // Add a generic matrix to an i_matrix