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;
        }
    }
}

你可能感兴趣的:(MyMathLib系列(线性空间))