OpenCascade Matrix

eryar@163.com

摘要Abstract:本文对矩阵作简要介绍,并结合代码说明OpenCascade矩阵计算类的使用方法。

关键字Key Words:OpenCascade、Matrix、C++

一、引言Introduction

矩阵的研究历史悠久,拉丁方阵和幻方在史前年代已有人研究。作为解决线性方程的工具,矩阵也有不短的历史。1693年,微积分的发现者之一莱布尼茨建立了行列式论(theory of determinants)。1750年,克拉默又定下了克拉默法则。1800年代,高斯和威廉若尔当建立了高斯-若尔当消去法。

从其发展历史可以看出,从行列式论的提出到克拉默法则总共历时50多年之久,而在我们《线性代数》的课本中,也就是一章的内容,学习时间可能只有一个星期。难怪这些概念这么抽象晦涩,也是情有可原的,哈哈。不过,当理解这些概念,也就掌握了一个强大的工具,因为它也使有些计算统一、简单。

矩阵是高等代数中的常见工具,也常见于统计分析等应用数学学科中。在物理学中,矩阵用于电路学、力学、光学和量子物理中都有应用;计算机科学中,三维变换就是使用了矩阵的线性变换。矩阵的运算是数值分析领域的重要问题。

在计算机图形相关的软件中,矩阵是必不可少的重要工具。如:OpenGL、DirectX中的变换都是用矩阵实现的;OpenCascade中就有矩阵计算类(math_Matrix);OpenSceneGraph中也有矩阵的计算类(osg::Matrix)等等。矩阵在计算中主要用来表达线性变换,统一三维空间中物体的位置和方向变换。

本文主要介绍OpenCascade中矩阵类的使用方法。

二、矩阵的运算 Matrix Calculation

矩阵的运算主要有矩阵的加法、数与矩阵相乘、矩阵与矩阵相乘、矩阵的转置、方阵的行列式、共轭矩阵、逆矩阵。还有通过矩阵来求解线性方程组的解问题。

1. 矩阵的加法 Matrix Addition

应该注意,只有当两个矩阵是同型矩阵时,才能进行加法运算。矩阵加法满足下列运算规律:设A、B、C都是m×n矩阵:

2. 数与矩阵相乘 Matrix Multiplication

数乘矩阵满足下列运算规律(设A、B为m×n矩阵,λ、μ为数):

矩阵相加与矩阵相乘合起来,统称为矩阵的线性运算。

3. 矩阵与矩阵相乘 Matrix Multiplication

并把此乘积记作:C = AB

按此定义,一个1×s行矩阵与一个s×1列矩阵的乘积是一个1阶方阵,也就是一个数:

由此表明乘积矩阵AB=C的(i,j)元cij就是A的第i行与B的第j列的乘积。必须注意:只有当每一个矩阵(左矩阵)的列数等于第二个矩阵(右矩阵)的行数时,两个矩阵才能相乘。

矩阵的乘法虽不满足交换律,但仍满足下列结合律和分配律(假设运算都是可行的):

4. 矩阵的转置 Matrix Transpose

矩阵转置也是一种运算,满足下列运算规律(假设运算都是可行的):

5. 方阵的行列式 Determinant of the Square Matrix

应该注意,方阵与行列式是两个不同的概念,n阶方阵是n×n个数按一定方式排列成的数表,而n阶行列式是这些数(也就是数表A)按一定的运算法则所确定的一个数。

由A确定|A|的这个运算满足下述运算规律(设A、B为n阶方阵,λ为数):

6. 逆矩阵 Inverse of Matrix

定义:对于n阶矩阵A,如果一个n阶矩阵B,使AB=BA=E,则说矩阵A是可逆的,并把矩阵B称为A的逆矩阵,简称逆阵。

方阵的逆矩阵满足下列运算规律:

7. 线性方程组的数值解法

在自然科学和工程中有很多问题的解决都归结为求解线性方程组或者非线性方程组的数学问题。例如,电学中的网络问题,用最小二乘法求实验数据的网线拟合问题,三次样条的插值问题,用有限元素法计算结构力学中的一些问题或用差分法解椭圆型偏微分方程的边值问题等等。

求解线性方程组的直接法主要有选主元高斯消去法、平方根法、追赶法等。如果计算过程中没有舍入误差,则此种方法经过有限步算术运算可求得方程组的精确解,但实际计算中由于舍入误差的存在和影响,这种方法也只能求得方程组的近似解。目前这种方法是计算机上解低阶稠密矩阵的有效方法。

高斯消去法是一个古老的求解线性方程组的方法,但由于它改进得到的选主元的高斯消去法则是目前计算机上常用的解低阶稠密矩阵方程组的有效方法。

用高斯消去法解方程组的基本思想是用矩阵行的初等变换将系数矩阵化为具有简单形式的矩阵,即三角形矩阵,再通过回代过程,求出方程组的解。

三、OpenCascade矩阵计算

在OpenCascade中,矩阵的计算类是math_Matrix,它可以有任意的行数和列数,实现了矩阵大部分的运算。以下通过程序实例来说明math_Matrix的使用方法:

/*
* Copyright (c) 2013 eryar All Rights Reserved.
*
* File : Main.cpp
* Author : eryar@163.com
* Date : 2013-07-17 21:46
* Version : 1.0v
*
* Description : Demonstrate how to use math_Matrix class.
* 题目来自《线性代数》同济 第四版
*
*/ #include <math_Matrix.hxx> #pragma comment(lib, "TKernel.lib")
#pragma comment(lib, "TKMath.lib") int main(int argc, char* argv[])
{
math_Matrix A(, , , );
math_Matrix B(, , , );
math_Matrix C(, , , ); // 1. Test Matrix Transpose and multiply.
// 题目来自P53习题二 第3题
// Set value of Matrix A
A.Init();
A(, ) = -;
A(, ) = -; // Set value of Matrix B
B(, ) = ; B(, ) = ; B(, ) = ;
B(, ) = -; B(, ) = -; B(, ) = ;
B(, ) = ; B(, ) = ; B(, ) = ; C.TMultiply(A, B); cout<<"Matrix A: "<<endl;
cout<<A<<endl;
cout<<"Matrix B: "<<endl;
cout<<B<<endl;
cout<<"Matrix C: "<<endl;
cout<<C<<endl; // 2. Test the Determinant of the Matrix.
// 题目来自P12 例7
math_Matrix D(, , , );
D(, ) = ; D(, ) = ; D(, ) = -; D(, ) = ;
D(, ) = -; D(, ) = ; D(, ) = ; D(, ) = -;
D(, ) = ; D(, ) = ; D(, ) = ; D(, ) = -;
D(, ) = ; D(, ) = -; D(, ) = ; D(, ) = -; cout<<"Matrix D: "<<endl;
cout<<D<<endl;
cout<<"Determinant of Matrix D: "<<D.Determinant()<<endl; // 3. Calculate Inverse of the Matrix.
// 题目来自P54 11题(3) 和P56 30题(1)
math_Matrix E(, , , ); E(, ) = ; E(, ) = ; E(, ) = -;
E(, ) = ; E(, ) = ; E(, ) = -;
E(, ) = ; E(, ) = -; E(, ) = ; cout<<"Inverse of the Matrix E: "<<endl;
cout<<E.Inverse()<<endl; // P56 30题(1)
math_Matrix F(, , , ); F(, ) = ; F(, ) = ;
F(, ) = ; F(, ) = ;
F(, ) = ; F(, ) = ;
F(, ) = ; F(, ) = ; cout<<"Inverse of the Matrix F: "<<endl;
cout<<F.Inverse()<<endl; return ;
}

计算结果为:

  Matrix A:
math_Matrix of RowNumber = and ColNumber =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) = Matrix B:
math_Matrix of RowNumber = and ColNumber =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = Matrix C:
math_Matrix of RowNumber = and ColNumber =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = Matrix D:
math_Matrix of RowNumber = and ColNumber =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) = - Determinant of Matrix D:
Inverse of the Matrix E:
math_Matrix of RowNumber = and ColNumber =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) = -8.88178e-017
math_Matrix ( , ) = -6.5
math_Matrix ( , ) =
math_Matrix ( , ) = -0.5
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) = - Inverse of the Matrix F:
math_Matrix of RowNumber = and ColNumber =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) = -
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) =
math_Matrix ( , ) = -
math_Matrix ( , ) = -
math_Matrix ( , ) = -
math_Matrix ( , ) = -
math_Matrix ( , ) = Press any key to continue . . .

线性方程组的计算方法请参考另一篇blog:

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

http://www.cppblog.com/eryar/archive/2012/06/21/179629.html

四、结论 Conclusion

OpenCascade的math_Matrix实现了矩阵的大部分运算,也可以作为一个小型的C++矩阵计算库来使用,在没有安装Matlab、Mathematica等软件的情况下,可以使用这个库进行一些计算。

另外,对《计算方法》中算法感兴趣的读者可以参考OpenCascade其中代码的实现,加深对相关算法的理解。

五、参考文献 Bibliography

1. 同济大学应用数学系 线性代数 高等教育出版社 2003

2. 易大义,沈云宝,李有法 计算方法 浙江大学出版社 2002

PDF Version: OpenCascade Matrix

OpenCascade Matrix的更多相关文章

  1. OpenCascade Eigenvalues and Eigenvectors of Square Matrix

    OpenCascade Eigenvalues and Eigenvectors of Square Matrix eryar@163.com Abstract. OpenCascade use th ...

  2. OpenGL Shader in OpenCASCADE

    OpenGL Shader in OpenCASCADE eryar@163.com Abstract. As implementation of one of the strategic steps ...

  3. OpenCASCADE General Transformation

    OpenCASCADE General Transformation eryar@163.com Abstract. OpenCASCADE provides a general transforma ...

  4. OpenCASCADE Coordinate Transforms

    OpenCASCADE Coordinate Transforms eryar@163.com Abstract. The purpose of the OpenGL graphics process ...

  5. OpenCASCADE Camera

    OpenCASCADE Camera eryar@163.com Abstract. OpenCASCADE introduce a new class Graphic3d_Camera for th ...

  6. OpenCASCADE Quaternion

    OpenCASCADE Quaternion eryar@163.com Abstract. The quaternions are members of a noncommutative divis ...

  7. OpenCascade B-Spline Basis Function

    OpenCascade B-Spline Basis Function eryar@163.com Abstract. B-splines are quite a bit more flexible ...

  8. Locations Section of OpenCascade BRep

    Locations Section of OpenCascade BRep eryar@163.com 摘要Abstract:本文结合OpenCascade的BRep格式描述文档和源程序,对BRep格 ...

  9. Opencascade术语笔记。

    1. chamfer 倒角 vs fillet  圆角: 2.boolean operatiron(布尔操作): common(相加),fuse(相交),cut(相减): 3.depressions( ...

随机推荐

  1. [置顶]PADS PCB功能使用技巧系列之NO.005- 如何正确使用Verify Design?

    有没有遇到过进行Verify Design通过后,回来的样板仍然出现短路或其它莫名其妙的问题?此情此景,你是否一度对PADS失去的希望?但,工具是没有问题的,看看怎么样正确有效地使用它吧.主要需要注意 ...

  2. VS2008 工程只生成dll不生成lib的解决方案

    http://topic.csdn.net/u/20081216/22/b12d1450-7585-4c9f-867a-7c181737c328.html 问题:vs2008版本的,不知道为什么只生成 ...

  3. selenium 富文本框处理

    selenium 富文本框处理, 网上有用API的解决方法1:参见:http://blog.csdn.net/xc5683/article/details/8963621 群里1位群友的解决方法2:参 ...

  4. 安装 webpack

    安装 webpack看好webpack 对自动压缩和文件名自动md5更名,可解决客户端缓存问题.我的安装环境为 centos linux,root用户 1.安装Node及NPM.到NodeJS官网安装 ...

  5. 转:给 C# 开发者的代码审查清单

      给 C# 开发者的代码审查清单   [感谢@L就是L 的热心翻译.如果其他朋友也有不错的原创或译文,可以尝试推荐给伯乐在线.] 这是为C#开发者准备的通用性代码审查清单,可以当做开发过程中的参考. ...

  6. JavaScript 学习笔记(一)

    1.javascript中,值包括原始值和对象,原始值包括布尔值.数字.字符串.null和undefined,其他的值为对象. 原始值的特点:(1)按值进行比较:3===3> true; 'ab ...

  7. Git 的origin和master分析

    首先要明确一点,对git的操作是围绕3个大的步骤来展开的(其实几乎所有的SCM都是这样) 1.     从git取数据(git clone) 2.     改动代码 3.     将改动传回git(g ...

  8. iOS 里面 NSTimer 防止 循环引用

    使用NSTimer的类 #import "TBTimerTestObject.h" #import "TBWeakTimerTarget.h" @interfa ...

  9. [ASE][Daily Scrum]12.15

    这两周事情好多~ 组里面的事情,出国的申请出国………… 不过整体来说我们sprint3并没有安排太多的工作,所以完成情况尚可. 大地图和AI花费了不少时间,

  10. 【Win10 UWP】QQ SDK(二):SDK的回调处理

    上一讲,我们介绍了QQ SDK的使用方法,请看<[Win10 UWP]QQ SDK(一):SDK基本使用方法> 一. 回调的基本形式 从前面的介绍中我们知道,我们的应用和QQ客户端之间需要 ...