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

📄 matrix.cpp

📁 矩阵类
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    else
    {
      return 0.0;
    }
  }

  double Matrix::imag(const unsigned index)
  {
    unsigned row = 0;
    unsigned col = 0;

    if( IndexCheck(index) )
    {
      if( m_Matrix.ncols == 1 )
      {
        row = index;
      }
      else if( m_Matrix.nrows == 1 )
      {
        col = index;
      }
      else
      {
        // access the matrix as a singular column array
        col = index / m_Matrix.nrows;
        row = index - col*m_Matrix.nrows;
      }
      if( m_Matrix.isReal )
      {
        return 0.0;
      }
      else
      {
        return m_Matrix.cplx[col][row].im;
      }
    }
    else
    {
      return 0.0;
    }
  }

  bool Matrix::isStoredAsComplex()
  {
    if( m_Matrix.isReal )
      return false;
    else
      return true;
  }

  /// Is this a real matrix for accessing by (row,col) operator? e.g. double d = A(0,4).
  bool Matrix::isReal()
  {
    BOOL isItReal = 0;
    
    // not checking return value
    MTX_isReal(&m_Matrix,&isItReal);

    if( isItReal )
      return true;
    else
      return false;    
  }

  /// Is this a complex matrix for accessing by [row][col] operators? e.g. stComplex d = A[0][4].
  bool Matrix::isComplex()
  {
    return !isReal();
  }

  bool Matrix::isVector()
  {
    if( m_Matrix.nrows == 1 )
      return true;
    if( m_Matrix.ncols == 1 )
      return true;

    // otherwise
    return false;
  }

  bool Matrix::ReadFromFile( const char *path )
  {
    if( MTX_ReadFromFile( &m_Matrix, path ) )
      return true;
    else 
      return false;
  }


  bool Matrix::ReadFromFile( std::string path )
  {
    return ReadFromFile( path.c_str() );
  }


  bool Matrix::Copy( Matrix& src )
  {
    if( MTX_Copy( &src.m_Matrix, &m_Matrix ) )
      return true;
    else
      return false;
  }

  bool Matrix::Copy( const double& value )
  {
    if( !MTX_Malloc( &m_Matrix, 1, 1, true ) )
      return false;

    if( MTX_SetValue( &m_Matrix, 0, 0, value ) )
      return true;
    else
      return false;
  }

  bool Matrix::Copy( const std::complex<double>& cplx )
  {
    if( !MTX_Malloc( &m_Matrix, 1, 1, false ) )
      return false;

    if( MTX_SetComplexValue( &m_Matrix, 0, 0, cplx.real(), cplx.imag() ) )
      return true;
    else
      return false;
  }

  bool Matrix::Save( const char* path )
  {
    if( MTX_SaveCompressed( &m_Matrix, path ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Save( std::string path )
  {
    return Save( path.c_str() );
  }

  bool Matrix::Print( const char *path, const unsigned precision, bool append )
  {
    if( MTX_PrintAutoWidth( &m_Matrix, path, precision, append ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Print( std::string path, const unsigned precision, bool append ) 
  {
    return Print( path.c_str(), precision );
  }

  bool Matrix::PrintStdout( const unsigned precision )
  {
    if( MTX_PrintStdoutAutoWidth( &m_Matrix, precision ) )
      return true;
    else
      return false;
  }

  bool Matrix::PrintToBuffer( char* buffer, const unsigned maxlength, const unsigned precision )
  {
    if( MTX_PrintAutoWidth_ToBuffer( &m_Matrix, buffer, maxlength, precision ) )
      return true;
    else 
      return false;
  }

  bool Matrix::PrintFixedWidth( const char* path, const unsigned width, const unsigned precision, bool append )
  {
    if( MTX_Print( &m_Matrix, path, width, precision, append ) )
      return true;
    else 
      return false;
  }

  bool Matrix::PrintFixedWidth( std::string path, const unsigned width, const unsigned precision, bool append )
  {
    return PrintFixedWidth( path.c_str(), width, precision, append );
  }

  bool Matrix::PrintFixedWidthToBuffer( char* buffer, const unsigned maxlength, const unsigned width, const unsigned precision )
  {
    if( MTX_Print_ToBuffer( &m_Matrix, buffer, maxlength, width, precision ) )
      return true;
    else 
      return false;
  }

  bool Matrix::PrintDelimited( const char *path, const unsigned precision, const char delimiter, bool append )
  {
    if( MTX_PrintDelimited( &m_Matrix, path, precision, delimiter, append ) )
      return true;
    else 
      return false;
  }

  bool Matrix::PrintDelimited( std::string path, const unsigned precision, const char delimiter, bool append )
  {
    return PrintDelimited( path.c_str(), precision, delimiter, append );
  }
    

  bool Matrix::PrintDelimitedToBuffer( char *buffer, const unsigned maxlength, const unsigned precision, const char delimiter )
  {
    if( MTX_PrintDelimited_ToBuffer( &m_Matrix, buffer, maxlength, precision, delimiter ) )
      return true;
    else 
      return false;
  }

  bool Matrix::PrintRowToString( const unsigned row, char *buffer, const unsigned maxlength, const int width, const int precision )
  {
    if( MTX_PrintRowToString( &m_Matrix, row, buffer, maxlength, width, precision ) )
      return true;
    else
      return false;
  }


  bool Matrix::RemoveColumn( const unsigned col )
  {
    if( MTX_RemoveColumn( &m_Matrix, col ) )
      return true;
    else 
      return false;
  }

  bool Matrix::RemoveColumnsAfterIndex( const unsigned col )
  {
    if( MTX_RemoveColumnsAfterIndex( &m_Matrix, col ) )
      return true;
    else 
      return false;
  }

  bool Matrix::RemoveRowsAndColumns( const unsigned nrows, const unsigned rows[], const unsigned ncols, const unsigned cols[] )
  {
    if( MTX_RemoveRowsAndColumns( &m_Matrix, nrows, rows, ncols, cols ) )
      return true;
    else
      return false;
  }

  bool Matrix::InsertColumn( const Matrix &src, const unsigned dst_col, const unsigned src_col )
  {
    if( MTX_InsertColumn( &m_Matrix, &src.m_Matrix, dst_col, src_col ) )
      return true;
    else 
      return false;
  }

  bool Matrix::AddColumn( const Matrix &src, const unsigned src_col )
  {
    if( MTX_AddColumn( &m_Matrix, &src.m_Matrix, src_col ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Concatonate( const Matrix &src )
  {
    if( MTX_Concatonate( &m_Matrix, &src.m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Redim( const unsigned nrows, const unsigned ncols )
  {
    if( MTX_Redim( &m_Matrix, nrows, ncols ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Resize( const unsigned nrows, const unsigned ncols )
  {
    if( MTX_Resize( &m_Matrix, nrows, ncols, m_Matrix.isReal ) )
      return true;
    else 
      return false;
  }


  bool Matrix::SetFromStaticMatrix( const double mat[], const unsigned nrows, const unsigned ncols )
  {
    if( MTX_SetFromStaticMatrix( &m_Matrix, mat, nrows, ncols ) )
      return true;
    else 
      return false;
  }

  bool Matrix::SetFromMatrixString(const char* strMatrix)
  {
    if( MTX_SetFromMatrixString( &m_Matrix, strMatrix ) )
      return true;
    else
      return false;
  }

  bool Matrix::CopyColumn( const unsigned src_col, Matrix &dst )
  {
    if( MTX_CopyColumn( &m_Matrix, src_col, &dst.m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::InsertSubMatrix( const Matrix &src, const unsigned dst_row, const unsigned dst_col )
  {
    if( MTX_InsertSubMatrix( &m_Matrix, &src.m_Matrix, dst_row, dst_col ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Zero()
  {
    if( MTX_Zero( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::ZeroColumn( const unsigned col )
  {
    if( MTX_ZeroColumn( &m_Matrix, col ) )
      return true;
    else 
      return false;
  }

  bool Matrix::ZeroRow( const unsigned row )
  {
    if( MTX_ZeroRow( &m_Matrix, row ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Fill( const double value )
  {
    if( MTX_Fill( &m_Matrix, value ) )
      return true;
    else 
      return false;
  }

  bool Matrix::FillColumn( const unsigned col, const double value )
  {
    if( MTX_FillColumn( &m_Matrix, col, value ) )
      return true;
    else 
      return false;
  }

  bool Matrix::FillRow( const unsigned row, const double value )
  {
    if( MTX_FillRow( &m_Matrix, row, value ) )
      return true;
    else 
      return false;
  }

  bool Matrix::FlipColumn( const unsigned col )
  {
    if( MTX_FlipColumn( &m_Matrix, col ) )
      return true;
    else 
      return false;
  }

  bool Matrix::FlipRow( const unsigned row )
  {
    if( MTX_FlipRow( &m_Matrix, row ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Identity()
  {
    if( MTX_Identity( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Identity(const unsigned dimension)
  {
    if( MTX_Malloc( &m_Matrix, dimension, dimension, true ) )
    {
      if( MTX_Identity( &m_Matrix ) )
        return true;
      else
        return false;
    }
    else
    {
      return false;
    }
  }

  bool Matrix::Inplace_Transpose()
  {
    if( MTX_TransposeInplace( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_Round( const unsigned precision )
  {
    if( MTX_Round( &m_Matrix, precision ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_Floor()
  {
    if( MTX_Floor( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_Ceil()
  {
    if( MTX_Ceil( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_Fix()
  {
    if( MTX_Fix( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_AddScalar( const double scalar )
  {
    if( MTX_Add_Scalar( &m_Matrix, scalar ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_SubtractScalar( const double scalar )
  {
    if( MTX_Subtract_Scalar( &m_Matrix, scalar ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_MultiplyScalar( const double scalar )
  {
    if( MTX_Multiply_Scalar( &m_Matrix, scalar ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_DivideScalar( const double scalar )
  {
    if( MTX_Divide_Scalar( &m_Matrix, scalar ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_PowerScalar( const double scalar )
  {
    if( MTX_PowInplace( &m_Matrix, scalar, 0.0 ) ) 
      return true;
    else
      return false;
  }

  bool Matrix::Inplace_AddScalarComplex( const std::complex<double> cplx )
  {
    if( MTX_Add_ScalarComplex( &m_Matrix, cplx.real(), cplx.imag() ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_SubtractScalarComplex( const std::complex<double> cplx )
  {
    if( MTX_Subtract_ScalarComplex( &m_Matrix, cplx.real(), cplx.imag() ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_MultiplyScalarComplex( const std::complex<double> cplx )
  {
    if( MTX_Multiply_ScalarComplex( &m_Matrix, cplx.real(), cplx.imag() ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_DivideScalarComplex( const std::complex<double> cplx )
  {
    if( MTX_Divide_ScalarComplex( &m_Matrix,  cplx.real(), cplx.imag() ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_PowerScalarComplex( const std::complex<double> cplx )
  {
    if( MTX_PowInplace( &m_Matrix, cplx.real(), cplx.imag() ) ) 
      return true;
    else
      return false;
  }

  bool Matrix::Inplace_Abs()
  {
    if( MTX_Abs( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_acos()
  {
    if( MTX_acos( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_acosd()
  {
    if( MTX_acos( &m_Matrix ) )
    {
      if( MTX_Multiply_Scalar( &m_Matrix, RAD2DEG ) )
        return true;
      else
        return false;
    }
    else 
    {
      return false;
    }
  }

  bool Matrix::Inplace_acosh()
  {
    if( MTX_acosh( &m_Matrix ) )
      return true;
    else 
      return false;
  }

  bool Matrix::Inplace_angle()
  {
    if( MTX_angle( &m_Matrix ) )
      return true;
    else

⌨️ 快捷键说明

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