MyMathLib系列(線性空間),mymathlib線性
線性空間的演算法不是特別多,這裡是代碼:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace MyMathLib{ /// <summary> /// 線性空間 /// </summary> public class LinearSpace { /// <summary> /// 求向量Vector在基底BasisOfLinearSpace下的座標向量 /// </summary> /// <param name="BasisOfLinearSpace">線性空間的基</param> /// <param name="Vector">目標向量</param> /// <returns>座標向量.</returns> public static double[] CalcVectorCoordinate(List<double[]> BasisOfLinearSpace,double[] Vector) { if (BasisOfLinearSpace == null && BasisOfLinearSpace.Count <= 0) { throw new Exception("基底不可為空!"); } var theCoeffs = BasisOfLinearSpace.To2DArrayT(); var theRet = LinearAlgebra.LinearEquationsEM2(theCoeffs, Vector); if (theRet.SolutionType == SolutionType.OnlyOne) { return theRet.SolutionVectors[0]; } throw new Exception("參數錯誤!"); } /// <summary> /// 求向量Vector在另一個基底下的座標向量 /// </summary> /// <param name="P">過渡矩陣</param> /// <param name="Vector">向量</param> /// <returns>座標向量.</returns> public static double[] CalcVectorY(double[,] P, double[] Vector) { if (P == null || P.GetLength(0) <= 0 || P.GetLength(1) <= 0) { throw new Exception("過渡矩陣不可為空!"); } var theP = new TMatrix(P); var theP1 = TMatrix.GetInverseMatrix(theP); double[][] theVectors = new double[1][]; theVectors[0] = Vector; var theAt = TMatrix.Transposition(new TMatrix(theVectors)); var theRet = (theP1 * theAt); return theRet.Elements.GetVectorCol(0); } /// <summary> /// 擷取T在此基底下的座標矩陣A. /// </summary> /// <param name="P">變換矩陣</param> /// <param name="BasisVectors">基底</param> /// <returns>座標矩陣A</returns> public static TMatrix CalcCoordinateMatrixOfT(double[,] P, List<double[]> BasisVectors) { if (BasisVectors ==null || P == null || P.GetLength(0) <= 0 || P.GetLength(1) <= 0 || P.GetLength(0)!=P.GetLength(1) || P.GetLength(0)!=BasisVectors.Count || BasisVectors.Count <=0 || BasisVectors[0].Length != BasisVectors.Count) { throw new Exception("參數有誤!"); } var theBasis = BasisVectors.To2DArrayT(); var theBasisM = new TMatrix(theBasis); var thePM = new TMatrix(P); var theBasisM_1 = TMatrix.GetInverseMatrix(theBasisM); return theBasisM_1 * thePM * theBasisM; } /// <summary> /// 擷取T在此BasisVectors基底下的關係矩陣P. /// </summary> /// <param name="A">T在基底下的座標矩陣A</param> /// <param name="BasisVectors">基底矩陣</param> /// <returns>座標矩陣A</returns> public static TMatrix CalcRelationMatrixOfT(double[,] A, List<double[]> BasisVectors) { if (BasisVectors == null || A == null || A.GetLength(0) <= 0 || A.GetLength(1) <= 0 || A.GetLength(0) != A.GetLength(1) || A.GetLength(0) != BasisVectors.Count || BasisVectors.Count <= 0 || BasisVectors[0].Length != BasisVectors.Count) { throw new Exception("參數有誤!"); } var theBasis = BasisVectors.To2DArrayT(); var theBasisM = new TMatrix(theBasis); var thePM = new TMatrix(A); var theBasisM_1 = TMatrix.GetInverseMatrix(theBasisM); return theBasisM * thePM * theBasisM_1; } }}