eryar

PipeCAD - Plant Piping Design Software.
RvmTranslator - Translate AVEVA RVM to OBJ, glTF, etc.
posts - 603, comments - 590, trackbacks - 0, articles - 0

使用OpenCASCADE的Math功能解线性方程组

Posted on 2012-06-21 18:15 eryar 阅读(3035) 评论(5)  编辑 收藏 引用 所属分类: 2.OpenCASCADE

Use Math Utilities in the OpenCASCADE

OpenCASCADE由七个模块组成,分别如下:

  • Module FoundationClasses; 基础类;
  • Module ModelingData; 造型数据;
  • Module ModelingAlgorihtms; 造型算法;
  • Module Visualization; 可视化;
  • Module ApplicationFramework; 程序框架;
  • Module DataExchange; 数据交换;
  • Module Draw;

其中基础类模块有三个工具箱:

  • Toolkit TKAdvTools; 高级工具箱;
  • Toolkit TKMath; 数学工具箱;
  • Toolkit TKernel; 核心工具箱;

根据OpenCASCADEReference文档,查到math_Gauss类,描述如下:

This class implements the Gauss LU decomposition (Crout algorithm) with partial pivoting (rows interchange) of a square matrix and the different possible derived calculation :

  • Solution of a set of linear equations.
  • Inverse of a matrix.
  • Determinant of a matrix.

主要根据矩阵的三角分解中的LU分解方法,可作如下计算:

  • 线性方程组的求解;
  • 矩阵的逆;
  • 矩阵的行列式;

为简单起见,先做个简单的测试,求以下方程组的解:

Equation

OpenCASCADEC++代码如下:(新建一个控制台程序,需要包含TKernel.lib;TKMath.lib两个库;)

   1:  //------------------------------------------------------------------------------
   2:  //    Copyright (c) 2012 eryar All Rights Reserved.
   3:  //
   4:  //        File    : Main.cpp
   5:  //        Author  : eryar@163.com
   6:  //        Date    : 2012-6-20 20:06
   7:  //        Version : 1.0v
   8:  //
   9:  //    Description : Learn to use OpenCASCADE Math Utilities.
  10:  //
  11:  //==============================================================================
  12:   
  13:  #include <math_Gauss.hxx>
  14:   
  15:  int main(int argc, char* argv[])
  16:  {
  17:      math_Matrix aMatrix(1, 3, 1, 3);
  18:      math_Vector b1(1, 3);
  22:   
  23:      //aMatrix, b1 and b2 are set here to the appropriate values
  24:      aMatrix(1, 1)   = 10;
  25:      aMatrix(2, 2)   = 2;
  26:      aMatrix(3, 3)   = 8;
  27:   
  28:      b1.Init(1);
  29:   
  30:      // Use Gause method
  31:      math_Gauss  sol(aMatrix);
  32:   
  33:      // LU decomposition of A
  34:      if (sol.IsDone())
  35:      {
  36:          sol.Solve(b1, x1);
  37:      }
  38:      else
  39:      {
  40:          // Fix up
  41:          try 
  42:          {
  43:              sol.Solve(b1, x1);
  44:          }
  45:          catch (Standard_Failure)
  46:          {
  47:              Handle(Standard_Failure) error = Standard_Failure::Caught();
  48:              cout<<error<<endl;
  49:          }
  50:      }
  51:   
  52:      cout<<aMatrix;
  53:      cout<<b1;
  54:      cout<<x1;
  55:   
  56:      return 0;
  57:  }

输出结果如下所示:

   1:  math_Matrix of RowNumber = 3 and ColNumber = 3
   2:  math_Matrix ( 1, 1 ) = 10
   3:  math_Matrix ( 1, 2 ) = 0
   4:  math_Matrix ( 1, 3 ) = 0
   5:  math_Matrix ( 2, 1 ) = 0
   6:  math_Matrix ( 2, 2 ) = 2
   7:  math_Matrix ( 2, 3 ) = 0
   8:  math_Matrix ( 3, 1 ) = 0
   9:  math_Matrix ( 3, 2 ) = 0
  10:  math_Matrix ( 3, 3 ) = 8
  11:  math_Vector of Length = 3
  12:  math_Vector(1) = 1
  13:  math_Vector(2) = 1
  14:  math_Vector(3) = 1
  15:  math_Vector of Length = 3
  16:  math_Vector(1) = 0.1
  17:  math_Vector(2) = 0.5
  18:  math_Vector(3) = 0.125
  19:  Press any key to continue . . .

结论

通过对基础类模块中数学工具箱的使用,对OpenCASCADE慢慢进行了解。

Feedback

# re: 使用OpenCASCADE的Math功能解线性方程组  回复  更多评论   

2012-11-19 14:05 by liangquan
谢谢您的文章

# re: 使用OpenCASCADE的Math功能解线性方程组  回复  更多评论   

2012-11-19 17:35 by eryar
@liangquan
客气了。。。

# re: 使用OpenCASCADE的Math功能解线性方程组  回复  更多评论   

2014-03-19 20:56 by near
X1是什么类型,也是VECTOR么?怎么定义了X1结果也不对。。。

# re: 使用OpenCASCADE的Math功能解线性方程组  回复  更多评论   

2014-03-19 20:58 by eryar
嗯,x1和b1的一样,也是个Vector。
是不是你的aMatrix没有初始化,用aMatrix.Init(0.0)初始化来看看……@near

# re: 使用OpenCASCADE的Math功能解线性方程组  回复  更多评论   

2014-03-19 21:22 by near
问题解决了,果然如此...@eryar

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理