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

📄 cmatrix.c

📁 大地测量专业计算软件
💻 C
📖 第 1 页 / 共 5 页
字号:
    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 != src->ncols || dst->ncols != 1 )
  {
    if( !MTX_Resize( dst, src->ncols, 1, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Resize returned FALSE." );
      return FALSE;
    }
  }

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

  return TRUE;
}

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

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

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

  // check that the submatrix doesn't exceed the bounds of the matrix
  if( dst_row + src->nrows > dst->nrows )
  {
    MTX_ERROR_MSG( "if( dst_row + src->nrows > dst->nrows )" );
    return FALSE;
  }
  if( dst_col + src->ncols > dst->ncols )
  {
    MTX_ERROR_MSG( "if( dst_col + src->ncols > dst->ncols )" );
    return FALSE;
  }

  if( !src->isReal && dst->isReal )
  {
    // convert the matrix to complex if the src matrix is complex
    if( !MTX_ConvertRealToComplex( dst ) )
    {
      MTX_ERROR_MSG( "MTX_ConvertRealToComplex returned FALSE." );
      return FALSE;
    }
  }

  // insert the submatrix
  if( dst->isReal )
  {
    for( j = 0; j < src->ncols; j++ )
    {
      memcpy( &(dst->data[dst_col+j][dst_row]), src->data[j], sizeof(double)*src->nrows );
    }
  }
  else
  {
    if( src->isReal )
    {
      for( j = 0; j < src->ncols; j++ )
      {
        for( i = 0; i < src->nrows; i++ )
        {
          dst->cplx[dst_col+j][dst_row+i].re = src->data[j][i];
          dst->cplx[dst_col+j][dst_row+i].im = 0.0;
        }
      }
    }
    else
    {
      for( j = 0; j < src->ncols; j++ )
      {
        memcpy( &(dst->cplx[dst_col+j][dst_row]), src->cplx[j], sizeof(stComplex)*src->nrows );
      }
    }
  }
  return TRUE;
}

BOOL MTX_ExtractSubMatrix( 
  const MTX* src,          //!< The source matrix.                        
  MTX* dst,                //!< The destination matrix to contain the submatrix.
  const unsigned from_row, //!< The zero-based index for the from row.
  const unsigned from_col, //!< The zero-based index for the from column.
  const unsigned to_row,   //!< The zero-based index for the to row.
  const unsigned to_col    //!< The zero-based index for the to column.
  )
{
  unsigned i;
  unsigned j;
  unsigned k;
  unsigned m;

  if( src == NULL )
  {
    MTX_ERROR_MSG( "NULL source matrix" );
    return FALSE;
  }
  if( dst == NULL )
  {
    MTX_ERROR_MSG( "NULL destination matrix" );
    return FALSE;
  }
  if( MTX_isNull( src ) )
  {
    MTX_ERROR_MSG( "NULL source matrix" );
    return FALSE;
  }

  if( to_row - from_row < 0 )
  {
    MTX_ERROR_MSG( "The destination matrix has invalid dimension. to_row - from_row < 0" );
    return FALSE;
  }
  if( to_col - from_col < 0 )
  {
    MTX_ERROR_MSG( "The destination matrix has invalid dimension. to_col - from_col < 0" );
    return FALSE;
  }

  if( from_row >= src->nrows )
  {
    MTX_ERROR_MSG( "from_row > number of source rows" );
    return FALSE;
  }
  if( from_col >= src->ncols )
  {
    MTX_ERROR_MSG( "from_col > number of source columns" );
    return FALSE;
  }
  if( to_row >= src->nrows )
  {
    MTX_ERROR_MSG( "to_row > number of source rows" );
    return FALSE;
  }
  if( to_col >= src->ncols )
  {
    MTX_ERROR_MSG( "to_col > number of source columns" );
    return FALSE;
  }

  if( !MTX_Malloc( dst, to_row-from_row+1, to_col-from_col+1, src->isReal ) )
  {
    MTX_ERROR_MSG( "MTX_Malloc returned FALSE." );
    return FALSE;
  }

  m = 0;
  for( j = from_col; j <= to_col; j++ )
  {
    k = 0;
    for( i = from_row; i <= to_row; i++ )
    {
      if( src->isReal )
      {
        dst->data[m][k] = src->data[j][i];
      }
      else
      {
        dst->cplx[m][k].re = src->cplx[j][i].re;
        dst->cplx[m][k].im = src->cplx[j][i].im;
      }
      k++;
    }
    m++;
  }

  return TRUE;
}

BOOL MTX_Zero( MTX *dst )
{
  unsigned i = 0;
  unsigned j = 0;

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

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

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

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

  if( col >= dst->ncols )
  {
    MTX_ERROR_MSG( "if( col >= dst->ncols )" );
    return FALSE;
  }

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

  return TRUE;
}

BOOL MTX_ZeroRow( MTX *dst, const unsigned row )
{
  return MTX_FillRow( dst, row, 0.0 );
}

BOOL MTX_Fill( MTX *dst, const double value )
{
  unsigned i = 0;
  unsigned j = 0;

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

  // use memcpy after the first column is set for efficiency and speed.
  if( dst->isReal )
  {
    j = 0;
    for( i = 0; i < dst->nrows; i++ )
    {
      dst->data[j][i] = value;          
    }
    for( j = 1; j < dst->ncols; j++ )
    {
      memcpy( dst->data[j], dst->data[j-1], sizeof(double)*dst->nrows );
    }
  }
  else
  {
    j = 0;
    for( i = 0; i < dst->nrows; i++ )
    {
      dst->cplx[j][i].re = value;
      dst->cplx[j][i].im = 0.0;
    }
    for( j = 1; j < dst->ncols; j++ )
    {
      memcpy( dst->cplx[j], dst->cplx[j-1], sizeof(stComplex)*dst->nrows );
    }
  }

  return TRUE;
}

BOOL MTX_FillComplex( MTX *dst, const double re, const double im )
{
  unsigned i = 0;
  unsigned j = 0;

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

  if( dst->isReal )
  {
    if( !MTX_ConvertRealToComplex( dst ) )
    {
      MTX_ERROR_MSG( "MTX_ConvertRealToComplex returned FALSE." );
      return FALSE;
    }
  }

  for( j = 0; j < dst->ncols; j++ )
  {
    if( j == 0 )
    {
      for( i = 0; i < dst->nrows; i++ )
      {
        dst->cplx[j][i].re = re;
        dst->cplx[j][i].im = im;
      }
    }
    else
    {
      memcpy( dst->cplx[j], dst->cplx[j-1], sizeof(stComplex)*dst->nrows );
    }
  }
  return TRUE;
}

BOOL MTX_FillColumn( MTX *dst, const unsigned col, const double value )
{
  unsigned i = 0;
  unsigned j = 0;

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

  if( col >= dst->ncols )
  {
    MTX_ERROR_MSG( "if( col >= dst->ncols )" );
    return FALSE;
  }

  if( dst->isReal )
  {
    for( i = 0; i < dst->nrows; i++ )
    {
      dst->data[col][i] = value;
    }
  }
  else
  {
    for( i = 0; i < dst->nrows; i++ )
    {
      dst->cplx[col][i].re = value;
      dst->cplx[col][i].im = 0;
    }
  }

  return TRUE;
}

BOOL MTX_FillColumnComplex( MTX *dst, const unsigned col, const double re, const double im )
{
  unsigned i = 0;
  unsigned j = 0;

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

  if( col >= dst->ncols )
  {
    MTX_ERROR_MSG( "if( col >= dst->ncols )" );
    return FALSE;
  }

  if( dst->isReal )
  {
    if( !MTX_ConvertRealToComplex( dst ) )
    {
      MTX_ERROR_MSG( "MTX_ConvertRealToComplex returned FALSE." );
      return FALSE;
    }
  }

  for( i = 0; i < dst->nrows; i++ )
  {
    dst->cplx[col][i].re = re;
    dst->cplx[col][i].im = im;
  }

  return TRUE;
}

BOOL MTX_FillRow( MTX *dst, const unsigned row, const double value )
{
  unsigned j = 0;

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

  if( row >= dst->nrows )
  {
    MTX_ERROR_MSG( "if( row >= dst->nrows )" );
    return FALSE;
  }

  for( j = 0; j < dst->ncols; j++ )
  {
    if( dst->isReal )
    {
      dst->data[j][row] = value;
    }
    else
    {
      dst->cplx[j][row].re = value;
      dst->cplx[j][row].im = value;
    }
  }

  return TRUE;
}

BOOL MTX_FillRowComplex( MTX *dst, const unsigned row, const double re, const double im )
{
  unsigned j = 0;

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

  if( row >= dst->nrows )
  {
    MTX_ERROR_MSG( "if( row >= dst->nrows )" );
    return FALSE;
  }

  if( dst->isReal )
  {
    if( !MTX_ConvertRealToComplex( dst ) )
    {
      MTX_ERROR_MSG( "MTX_ConvertRealToComplex returned FALSE." );
      return FALSE;
    }
  }

  for( j = 0; j < dst->ncols; j++ )
  {
    dst->cplx[j][row].re = re;
    dst->cplx[j][row].im = im;
  }

  return TRUE;
}


// set the matrix to an identity
BOOL MTX_Identity( MTX *dst )
{
  unsigned j = 0;

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

  if( !MTX_isSquare( dst ) )
  {
    MTX_ERROR_MSG( "MTX_isSquare returned FALSE." );
    return FALSE;
  }

  if( !dst->isReal )
  {
    if( !MTX_Calloc( dst, dst->nrows, dst->ncols, TRUE ) )
    {
      MTX_ERROR_MSG( "MTX_Calloc returned FALSE." );
      return FALSE;
    }
  }
  else
  {
    if( !MTX_Zero( dst ) )
    {
      MTX_ERROR_MSG( "MTX_Zero returned FALSE." );
      return FALSE;
    }
  }

  for( j = 0; j < dst->ncols; j++ )
  {
    dst->data[j][j] = 1.0;
  }
  return TRUE;
}


// Transpose the matrix src into the matrix dst.
BOOL MTX_Transpose( const MTX *src, MTX *dst )
{
  unsigned i = 0;
  unsigned j = 0;

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

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

  // special case inplace transpose
  if( dst == src )
  {
    return MTX_TransposeInplace( dst );
  }

  // complex/real mixed cases
  if( !src->isReal && dst->isReal )
  {
    MTX_Free( dst );

    if( !MTX_Malloc( dst, src->ncols, src->nrows, src->isReal ) )
    {
      MTX_ERROR_MSG( "MTX_Malloc returned FALSE." );
      return FALSE;
    }
  }
  else if( src->isReal && !dst->isReal )
  {
    MTX_Free( dst );

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


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

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

⌨️ 快捷键说明

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