码迷,mamicode.com
首页 > 编程语言 > 详细

n维向量积(3维向量积的推广,向量算法)

时间:2015-01-30 07:54:19      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

在三维空间中,两个向量的乘积(向量积,外积,乘积,区别于两个向量的数乘:内积,点积)表示两个向量的扭矩,而三个向量的混合积A×B·C,则表示由三个向量A,B,C所构成的平行六面体的面积。而且在混合积中A,B,C的位置是可以互换的(这个很容易证明),这也符合我们的经验。那么问题来了?
1)3个或者N>3个三维向量相乘如何定义?A×B×C×D....因为A×B是有定义的,A×B是向量,那么只要继续乘就可以了,这也说明3维向量相乘,向量个数不是问题;
2)向量个数不是问题,那4维向量的两个向量相乘呢?

设A=(a1,a2,a3,a4),B=(b1,b2,b3,b4) A*B=(x1,x2,x3,x4)则满足如下方程组:
 ① A·(A*B)=0
 ② B·(A*B)=0
 ③ |A*B|=|A|*|B|sinθ。
这是一个4元二次方程组,但只有3个方程组,显然解不是一个。这说明A*B在4维空间,如果按垂直来定义,无法唯一确定,其结果是一个面。
类似的,扩展到n维空间,方程组还是只有3个。结果是n-2维体(面)。

下面推广n维向量的
n-1个向量积:A1*A2*...A(n-1)·;
混合积:A1*A2*...A(n-1)·An.


/// <summary>
        /// 向量积
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static TVector Mul(TVector A, TVector B)
        {
            if (A.Count == B.Count && B.Count == 3)
            {
                var theI = A[1] * B[2] - A[2] * B[1];
                var theJ = -(A[0] * B[2] - A[2] * B[0]);
                var theK = A[0] * B[1] - A[1] * B[0];
                TVector theV = new TVector(theI, theJ, theK);
                return theV;
            }
            else
            {
                throw new Exception("现仅支持3分量.");
            }
        }
        /// <summary>
        /// n维向量的n-1个向量的乘积,n>3
        /// </summary>
        /// <param name="Vectors">向量组</param>
        /// <returns></returns>
        public static TVector Mul2(params TVector[] Vectors)
        {
            if (Vectors==null || Vectors.Length < 2)
            {
                throw new Exception("参数错误必须是n维向量n-1个向量模式");
            }
            var theN = Vectors[0].Count;
            if (theN != Vectors.Length + 1)
            {
                throw new Exception("参数错误必须是n维向量n-1个向量模式");
            }
            var theA = new double[theN, theN];
            for (int i = 0; i < theN; i++)
            {
                theA[0, i] = 1; 
            }
            for (int i = 0; i < theN; i++)
            {
                for (int j = 0; j < Vectors.Length; j++)
                {
                    theA[j + 1, i] = Vectors[j][i];
                }
            }
            //按第一行展开求代数余子式,并计算行列式.(1,j)的代数余子式就是所求向量第j个分量的值.
            var theRetA = new double[theN];
            for (int j = 0; j < theN; j++)
            {
                var theA1 = LinearAlgebra.GetDeterminantMij(theA, 1, j + 1);
                var theSign = LinearAlgebra.CalcDeterMijSign(1, j + 1);
                theRetA[j] = theSign * LinearAlgebra.CalcDeterminant(theA1);
            }
            return new TVector(theRetA);
        }
        /// <summary>
        /// n维向量的n个向量的混合积A1×A2.....An-1·An
        /// </summary>
        /// <param name="Vectors">向量组</param>
        /// <returns></returns>
        public static double Mul3(params TVector[] Vectors)
        {
            if (Vectors == null || Vectors.Length < 2)
            {
                throw new Exception("参数错误必须是n维向量n-1个向量模式");
            }
            var theN = Vectors[0].Count;
            if (theN != Vectors.Length)
            {
                throw new Exception("参数错误必须是n维向量n个向量模式");
            }
            var theA = new double[theN, theN];
          
            for (int i = 0; i < theN; i++)
            {
                for (int j = 0; j < Vectors.Length; j++)
                {
                    theA[j, i] = Vectors[j][i];
                }
            }
            return LinearAlgebra.CalcDeterminant(theA);
        }
        /// <summary>
        /// 向量混合积A×B·C
        /// </summary>
        /// <param name="A"></param>
        /// <param name="B"></param>
        /// <returns></returns>
        public static double Mul(TVector A, TVector B, TVector C)
        {
            if (A.Count == B.Count && B.Count == 3)
            {
                double[,] theA = new double[3, 3];
                for (int i = 0; i < 2; i++)
                {
                    theA[0, i] = A[i];
                    theA[1, i] = B[i];
                    theA[2, i] = C[i];
                }
                return LinearAlgebra.CalcDeterminantAij(theA);
            }
            else
            {
                throw new Exception("现仅支持3分量.");
            }
        }

暂时研究到此,后面有时间继续研究。
注意:算法中用到的函数参见Mymathlib系列.??
??

n维向量积(3维向量积的推广,向量算法)

标签:

原文地址:http://blog.csdn.net/hawksoft/article/details/43166337

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!