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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值