⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmatrix.c

📁 大地测量专业计算软件
💻 C
📖 第 1 页 / 共 5 页
字号:
      MTX_ERROR_MSG( "malloc returned NULL." );
      return FALSE;
    }
  }
  else
  {
    cptr = (stComplex**)malloc( ncols*sizeof(stComplex*) );
    if( !cptr )
    {
      MTX_ERROR_MSG( "malloc returned NULL." );
      return FALSE;
    }
  }

  for( j = 0; j < ncols; j++ )
  {
    if( j < dst->ncols )
    {
      // keep the original data
      if( dst->isReal )
        dptr[j] = dst->data[j];
      else
        cptr[j] = dst->cplx[j];
    }
    else
    {
      // copy the new data
      if( dst->isReal )
        dptr[j] = (double*)malloc( dst->nrows*sizeof(double) );
      else
        cptr[j] = (stComplex*)malloc( dst->nrows*sizeof(stComplex) );
      if( dst->isReal )
      {
        if( !(dptr[j]) )
        {
          // this is most likely to occur if allocating more memory than available
          for( m = 0; m < j; m++ )
          {
            free( dptr[m] );
          }

          MTX_ERROR_MSG( "malloc returned NULL." );
          free( dptr );
          return FALSE;
        }
      }
      else
      {
        if( !(cptr[j]) )
        {
          // this is most likely to occur if allocating more memory than available
          for( m = 0; m < j; m++ )
          {
            free( cptr[m] );
          }
          free( cptr );
          MTX_ERROR_MSG( "malloc returned NULL." );
          return FALSE;
        }
      }
      // copy the src column vector
      for( i = 0; i < dst->nrows; i++ )
      {
        if( dst->isReal )
        {
          dptr[j][i] = src->data[j-dst->ncols][i];
        }
        else
        {
          if( src->isReal )
          {
            cptr[j][i].re = src->data[j-dst->ncols][i];
            cptr[j][i].im = 0;
          }
          else
          {
            cptr[j][i] = src->cplx[j-dst->ncols][i];
          }
        }
      }
    }
  }

  // free the old column array, and copy the new
  if( dst->isReal )
  {
    free( dst->data );
    dst->data = dptr;
  }
  else
  {
    free( dst->cplx );
    dst->cplx = cptr;
  }
  dst->ncols = ncols;

  return TRUE;
}



// A becomes A|0|0|0|.. etc
BOOL MTX_AddZeroValuedColumns( MTX *dst, const unsigned nr_new_cols )
{
  unsigned i = 0;
  unsigned j = 0;
  unsigned ncols;
  unsigned m = 0;
  double **dptr = NULL;
  stComplex **cptr = NULL;

  if( MTX_isNull( dst ) )
  {
    MTX_ERROR_MSG( "NULL Matrix" );
    return FALSE;
  }

  ncols = dst->ncols + nr_new_cols;

  // allocate a new array of column vectors
  if( dst->isReal )
  {
    dptr = (double**)malloc( ncols*sizeof(double*) );
    if( !dptr )
    {
      MTX_ERROR_MSG( "malloc returned NULL." );
      return FALSE;
    }
  }
  else
  {
    cptr = (stComplex**)malloc( ncols*sizeof(stComplex*) );
    if( !cptr )
    {
      MTX_ERROR_MSG( "malloc returned NULL." );
      return FALSE;
    }
  }

  for( j = 0; j < ncols; j++ )
  {
    if( j < dst->ncols )
    {
      // keep the original data
      if( dst->isReal )
        dptr[j] = dst->data[j];
      else
        cptr[j] = dst->cplx[j];
    }
    else
    {
      // copy the new data
      if( dst->isReal )
      {
        dptr[j] = (double*)calloc( dst->nrows, sizeof(double) );
      }
      else
      {
        cptr[j] = (stComplex*)calloc( dst->nrows, sizeof(stComplex) );
      }
      if( dst->isReal )
      {
        if( !(dptr[j]) )
        {
          // this is most likely to occur if allocating more memory than available
          for( m = 0; m < j; m++ )
          {
            free( dptr[m] );
          }
          free( dptr );

          MTX_ERROR_MSG( "calloc returned NULL." );
          return FALSE;
        }
      }
      else
      {
        if( !(cptr[j]) )
        {
          // this is most likely to occur if allocating more memory than available
          for( m = 0; m < j; m++ )
          {
            free( cptr[m] );
          }
          free( cptr );

          MTX_ERROR_MSG( "calloc returned NULL." );
          return FALSE;
        }
      }

    }
  }

  // free the old column array, and copy the new
  if( dst->isReal )
  {
    free( dst->data );
    dst->data = dptr;
  }
  else
  {
    free( dst->cplx );
    dst->cplx = cptr;
  }
  dst->ncols = ncols;

  return TRUE;
}

BOOL MTX_Redim( MTX *dst, const unsigned nrows, const unsigned ncols )
{
  unsigned i = 0;
  unsigned j = 0;
  unsigned nc;
  unsigned nr;
  MTX copy;
  double **dptr = NULL;
  stComplex **cptr = NULL;
  const BOOL isReal = dst->isReal;

  MTX_Init( &copy );

  if( !dst )
  {
    MTX_ERROR_MSG( "dst is a NULL pointer." );
    return FALSE;
  }

  if( nrows == 0 || ncols == 0 )
  {
    MTX_ERROR_MSG( "if( nrows == 0 || ncols == 0 )" );
    return FALSE;
  }

  // special case - calling Redim with a null matrix
  if( dst->ncols == 0 && dst->nrows == 0 )
    return MTX_Calloc( dst, nrows, ncols, dst->isReal );


  // check same size
  if( dst->nrows == nrows && dst->ncols == ncols )
    return TRUE;

  // special cases, adding or removing columns
  if( dst->nrows == nrows )
  {
    if( ncols < dst->ncols )
    {
      if( MTX_RemoveColumnsAfterIndex( dst, ncols-1 ) == FALSE )
      {
        MTX_ERROR_MSG( "MTX_RemoveColumnsAfterIndex returned FALSE." );
        return FALSE;
      }

      return TRUE;
    }
    else
    {
      // Add the extra columns
      if( !MTX_AddZeroValuedColumns( dst, ncols-dst->ncols ) )
      {
        MTX_ERROR_MSG( "MTX_AddZeroValuedColumns returned FALSE." );
        return FALSE;
      }
      return TRUE;
    }
  }

  // make a copy of the previous data
  if( !MTX_Malloc( &copy, dst->nrows, dst->ncols, isReal ) )
  {
    MTX_ERROR_MSG( "MTX_Malloc returned FALSE." );
    return FALSE;
  }
  if( !MTX_Copy( dst, &copy ) )
  {
    MTX_ERROR_MSG( "MTX_Copy returned FALSE." );
    MTX_Free( &copy );
    return FALSE;
  }

  // must reallocate the matrix
  MTX_Free( dst );

  if( !MTX_Calloc( dst, nrows, ncols, isReal ) )
  {
    MTX_ERROR_MSG( "MTX_Calloc returned FALSE." );
    MTX_Free( &copy );
    return FALSE;
  }

  // Copy the previous data.
  if( dst->ncols < copy.ncols )
    nc = dst->ncols;
  else
    nc = copy.ncols;

  if( dst->nrows < copy.nrows )
    nr = dst->nrows;
  else
    nr = copy.nrows;

  if( isReal )
  {
    for( j = 0; j < nc; j++ )
    {
      memcpy( dst->data[j], copy.data[j], sizeof(double)*nr );
    }
  }
  else
  {
    for( j = 0; j < nc; j++ )
    {
      memcpy( dst->cplx[j], copy.cplx[j], sizeof(stComplex)*nr );
    }
  }
  MTX_Free( &copy );
  return TRUE;
}

BOOL MTX_Resize( MTX *dst, const unsigned nrows, const unsigned ncols, const BOOL isReal )
{
  if( !dst )
  {
    MTX_ERROR_MSG( "dst is a NULL pointer." );
    return FALSE;
  }

  if( nrows == 0 || ncols == 0 )
  {
    MTX_ERROR_MSG( "if( nrows == 0 || ncols == 0 )" );
    return FALSE;
  }

  // MTX_Calloc is smart. It only re-allocates memory if it needs to
  // and always sets the data to zero.
  if( !MTX_Calloc( dst, nrows, ncols, isReal ) )
  {
    MTX_ERROR_MSG( "MTX_Calloc returned FALSE." );
    return FALSE;
  }

  return TRUE;
}

BOOL MTX_Copy( const MTX *src, MTX *dst )
{
  unsigned i = 0;
  unsigned j = 0;

  if( MTX_isNull( src ) )
  {
    MTX_ERROR_MSG( "NULL Matrix" );
    return FALSE;
  }
  if( !dst )
  {
    MTX_ERROR_MSG( "dst is a NULL pointer." );
    return FALSE;
  }

  // both must be real or both must be complex
  if( dst->isReal != src->isReal )
  {
    if( !MTX_Resize( dst, src->nrows, src->ncols, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }

  if( !MTX_isSameSize( src, dst ) )
  {
    if( !MTX_Resize( dst, src->nrows, src->ncols, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }
  

  if( src->isReal )
  {  
    for( j = 0; j < dst->ncols; j++ )
    {
      memcpy( dst->data[j], src->data[j], sizeof(double)*(dst->nrows) );
    }
  }
  else
  {
    for( j = 0; j < dst->ncols; j++ )
    {
      memcpy( dst->cplx[j], src->cplx[j], sizeof(stComplex)*(dst->nrows) );
    }
  }
  
  return TRUE;
}

BOOL MTX_CopyIntoColumnWiseVector( const MTX *src, MTX *dst )
{
  unsigned i = 0;
  unsigned j = 0;

  if( MTX_isNull( src ) )
  {
    MTX_ERROR_MSG( "NULL Matrix" );
    return FALSE;
  }
  if( !dst )
  {
    MTX_ERROR_MSG( "dst is a NULL pointer." );
    return FALSE;
  }

  // both must be real or both must be complex
  if( dst->isReal != src->isReal )
  {
    if( !MTX_Resize( dst, src->nrows*src->ncols, 1, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }

  if( dst->nrows != src->nrows*src->ncols )
  {
    if( !MTX_Resize( dst, src->nrows*src->ncols, 1, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }

  if( src->isReal )
  {
    for( j = 0; j < src->ncols; j++ )
    {
      memcpy( &(dst->data[0][j*src->nrows]), src->data[j], sizeof(double)*src->nrows );
    }
  }
  else
  {
    for( j = 0; j < src->ncols; j++ )
    {
      memcpy( &(dst->cplx[0][j*src->nrows]), src->cplx[j], sizeof(stComplex)*src->nrows );
    }
  }

  return TRUE;
}


BOOL MTX_SetFromStaticMatrix( MTX *dst, const double mat[], const unsigned nrows, const unsigned ncols )
{
  unsigned i = 0;
  unsigned j = 0;

  if( !dst )
  {
    MTX_ERROR_MSG( "dst is a NULL pointer." );
    return FALSE;
  }

  if( !mat )
  {
    MTX_ERROR_MSG( "mat is a NULL pointer." );
    return FALSE;
  }

  if( dst->nrows != nrows || dst->ncols != ncols )
  {
    if( !MTX_Resize( dst, nrows, ncols, TRUE ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }

  for( j = 0; j < ncols; j++ )
    for( i = 0; i < nrows; i++ )
      dst->data[j][i] = mat[i*ncols + j];

  return TRUE;
}

BOOL MTX_CopyColumn( const MTX *src, const unsigned col, MTX *dst )
{
  unsigned i = 0;

  if( MTX_isNull( src ) )
  {
    MTX_ERROR_MSG( "NULL Matrix" );
    return FALSE;
  }
  if( !dst )
  {
    MTX_ERROR_MSG( "dst is a NULL pointer." );
    return FALSE;
  }
  if( col >= src->ncols )
  {
    MTX_ERROR_MSG( "if( col >= src->ncols )" );
    return FALSE;
  }

  if( src->isReal != dst->isReal )
  {
    if( !MTX_Malloc( dst, src->nrows, 1, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }
  else if( dst->nrows != src->nrows || dst->ncols != 1 )
  {
    if( !MTX_Malloc( dst, src->nrows, 1, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }

  if( src->isReal )
  {
    memcpy( dst->data[0], src->data[col], sizeof(double)*(dst->nrows) );
  }
  else
  {
    memcpy( dst->cplx[0], src->cplx[col], sizeof(stComplex)*(dst->nrows) );
  }

  return TRUE;
}

BOOL MTX_CopyRow( const MTX *src, const unsigned row, MTX *dst )
{
  unsigned i = 0;

  if( MTX_isNull( src ) )
  {
    MTX_ERROR_MSG( "NULL Matrix" );
    return FALSE;
  }
  if( !dst )
  {
    MTX_ERROR_MSG( "dst is a NULL pointer." );
    return FALSE;
  }
  if( row >= src->nrows )
  {
    MTX_ERROR_MSG( "if( row >= src->nrows )" );
    return FALSE;
  }

  if( dst->nrows != 1 || dst->ncols != src->ncols )
  {
    if( !MTX_Resize( dst, 1, src->ncols, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }

  for( i = 0; i < dst->ncols; i++ )
  {
    if( src->isReal )
      dst->data[i][0] = src->data[i][row];
    else
      dst->cplx[i][0] = src->cplx[i][row];
  }

  return TRUE;
}

BOOL MTX_CopyRowIntoAColumnMatrix( const MTX *src, const unsigned row, MTX *dst )
{
  unsigned i = 0;

  if( MTX_isNull( src ) )
  {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -