首页 > 代码库 > MyMathLib系列(向量及矩阵--准备工作)
MyMathLib系列(向量及矩阵--准备工作)
因为向量和矩阵的计算工作量比较大,为了更好的书写代码,这里增加了几个定义类,这些定义或者扩展方法将在以后的代码中应用到:
1、公共枚举类型
/* 文件:PublicEnums.cs * 目的:定义公共枚举类型. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyMathLib { /// <summary> /// 初等变换类型(课本上是加,这里是减,仅仅是个系数符号的区别. /// </summary> public enum BasicTransMethod { /// <summary> /// 交换i< == >j /// </summary> Swap, /// <summary> /// 乘以非零数:i=i*M /// </summary> Multipler, /// <summary> /// 第i行减去第j行乘以一个系数:i=i-j*M /// </summary> CoPlus1, /// <summary> /// 第i行乘以系数减去第j行乘以一个系数:i=i*M1 - j*M2 /// </summary> CoPlus2 } /// <summary> /// 行变换还是列变换. /// </summary> public enum TransRowOrCol { /// <summary> /// 行 /// </summary> Row, /// <summary> /// 列 /// </summary> Col } public static class ConstDef { public const int Decimals = 15; } public enum SolutionType { /// <summary> /// 无解 /// </summary> None, /// <summary> /// 只有零解 /// </summary> OnlyZero, //仅有一个非零解 OnlyOne, /// <summary> /// 有很多解. /// </summary> Many } }
2、方程求解结果
/* * SolutionOfEquation.cs * Added by Hawksoft. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyMathLib { /// <summary> /// 方程解结果,用于方程组求解后的结果存放. /// </summary> public class SolutionOfEquation { /// <summary> /// 解类型 /// </summary> public SolutionType SolutionType{ get; set; } /// <summary> /// 解向量 /// </summary> public List<double[]> SolutionVectors { get; set; } /// <summary> /// 基本未知量 /// </summary> public List<int> PrimaryUnknownVariations { get; set; } /// <summary> /// 自由未知量. /// </summary> public List<int> FreeUnknownVariations { get; set; } public SolutionOfEquation() { SolutionVectors = new List<double[]>(); PrimaryUnknownVariations = new List<int>(); FreeUnknownVariations = new List<int>(); } } }
3、初等变换记录项
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyMathLib { /// <summary> /// 初等变换记录项,用于矩阵初等变换的过程演示和矩阵求逆. /// </summary> public class TransformItem { public BasicTransMethod TransMethod { get; set; } public int i { get; set; } public int j { get; set; } public double M1 { get; set; } public double M2 { get; set; } public TransRowOrCol RowOrCol { get; set; } public static TransformItem CreateEleTransRow1(int i, int j) { return new TransformItem() { i = i, j = j, M1 = 1, TransMethod = BasicTransMethod.Swap, RowOrCol = TransRowOrCol.Row }; } public static TransformItem CreateEleTransRow2(int i,double Multipler) { return new TransformItem() { i = i, j = i, M1 = Multipler, M2 = Multipler, TransMethod = BasicTransMethod.Multipler, RowOrCol = TransRowOrCol.Row }; } public static TransformItem CreateEleTransRow3(int i, int j,double Multipler) { return new TransformItem() { i = i, j = j, M1 = Multipler, M2 = Multipler, TransMethod = BasicTransMethod.CoPlus1, RowOrCol = TransRowOrCol.Row }; } public static TransformItem CreateEleTransRow4(int i, int j, double M1,double M2) { return new TransformItem() { i = i, j = j, M1 = M1, M2 = M2, TransMethod = BasicTransMethod.CoPlus2, RowOrCol = TransRowOrCol.Row }; } public static TransformItem CreateEleTransCol1(int i, int j) { return new TransformItem() { i = i, j = j, M1 = 1, M2 = 1, TransMethod = BasicTransMethod.Swap, RowOrCol = TransRowOrCol.Row }; } public static TransformItem CreateEleTransCol2(int i, double Multipler) { return new TransformItem() { i = i, j = i, M1 = Multipler, M2 = Multipler, TransMethod = BasicTransMethod.Multipler, RowOrCol = TransRowOrCol.Row }; } public static TransformItem CreateEleTransCol3(int i, int j, double Multipler) { return new TransformItem() { i = i, j = j, M1 = 1, M2 = Multipler, TransMethod = BasicTransMethod.CoPlus1, RowOrCol = TransRowOrCol.Row }; } public static TransformItem CreateEleTransCol4(int i, int j, double M1,double M2) { return new TransformItem() { i = i, j = j, M1 = M1, M2 = M2, TransMethod = BasicTransMethod.CoPlus2, RowOrCol = TransRowOrCol.Row }; } }
另外说明一下:原来一直用decimal来计算,但在矩阵计算测试中发现,decimal很容易爆仓,所以改用double类型,其实decimal的位数比double多很多,但由于其特殊性,其表达的范围要比double小,所以后面改成了double.
MyMathLib系列(向量及矩阵--准备工作)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。