程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> MyMathLib系列(向量及矩陣--准備工作)

MyMathLib系列(向量及矩陣--准備工作)

編輯:C++入門知識

MyMathLib系列(向量及矩陣--准備工作)


因為向量和矩陣的計算工作量比較大,為了更好的書寫代碼,這裡增加了幾個定義類,這些定義或者擴展方法將在以後的代碼中應用到:
1、公共枚舉類型

/*
   文件:PublicEnums.cs
 * 目的:定義公共枚舉類型.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMathLib
{
    /// 
    /// 初等變換類型(課本上是加,這裡是減,僅僅是個系數符號的區別.
    /// 
    public enum BasicTransMethod
    {
        /// 
        /// 交換i< == >j
        /// 
        Swap,
        /// 
        /// 乘以非零數:i=i*M
        /// 
        Multipler,
        /// 
        /// 第i行減去第j行乘以一個系數:i=i-j*M
        /// 
        CoPlus1,
        /// 
        /// 第i行乘以系數減去第j行乘以一個系數:i=i*M1 - j*M2
        /// 
        CoPlus2

    }
    /// 
    /// 行變換還是列變換.
    /// 
    public enum TransRowOrCol
    {
        /// 
        /// 行
        /// 
        Row,
        /// 
        /// 列
        /// 
        Col
    }

    public static class ConstDef
    {
        public const int Decimals = 15;
    }

    public enum SolutionType
    {
        /// 
        /// 無解
        /// 
        None,
        /// 
        /// 只有零解
        /// 
        OnlyZero,
        //僅有一個非零解
        OnlyOne,
        /// 
        /// 有很多解.
        /// 
        Many
    }
}

2、方程求解結果

/*
 * SolutionOfEquation.cs
 * Added by Hawksoft.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMathLib
{
    /// 
    /// 方程解結果,用於方程組求解後的結果存放.
    /// 
    public class SolutionOfEquation
    {
        /// 
        /// 解類型
        /// 
        public SolutionType SolutionType{ get; set; }
        /// 
        /// 解向量
        /// 
        public List SolutionVectors { get; set; }
        /// 
        /// 基本未知量
        /// 
        public List PrimaryUnknownVariations { get; set; }
        /// 
        /// 自由未知量.
        /// 
        public List FreeUnknownVariations { get; set; }

        public SolutionOfEquation()
        {
            SolutionVectors = new List();
            PrimaryUnknownVariations = new List();
            FreeUnknownVariations = new List();
        }
    }
}


3、初等變換記錄項

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyMathLib
{
    /// 
    /// 初等變換記錄項,用於矩陣初等變換的過程演示和矩陣求逆.
    /// 
    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.


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved