golang 矩阵乘法、行列式、求逆矩阵

package matrix

import (
    "math"
    "github.com/astaxie/beego"
)

type Matrix4 struct {
    Elements [16]float64 `json:"elements"`
}

type SQ struct {
    //矩阵结构
    M,N int            //m是列数,n是行数
    Data [][]float64
}

//矩阵定义
func (this*SQ)Set(m int,n int,data []float64) {
    //m是列数,n是行数,data是矩阵数据(从左到右由上到下填充)
    this.M=m
    this.N=n
    if len(data)!=this.M*this.N {
        beego.Debug("矩阵定义失败")
        return
    }else {
        k := 0
        if this.M*this.N == len(data){
            for i := 0; i < this.N; i++ {
                var tmpArr []float64
                for j := 0; j < this.M; j++ {
                    tmpArr = append(tmpArr, data[k])
                    k++
                }
                this.Data = append(this.Data, tmpArr)
            }
        }else {
            beego.Debug("矩阵定义失败")
            return
        }
    }
}
//a的列数和b的行数相等
//矩阵乘法
func Mul(a SQ,b SQ) [][]float64{
    if a.M==b.M {
        res := [][]float64{}
        for i:=0;i<a.M;i++ {
            t := []float64{}
            for j:=0;j<b.M;j++ {
                r := float64(0)
                for k:=0;k<a.M;k++ {
                    r += a.Data[i][k]*b.Data[k][j]
                }
                t = append(t, r)
            }
            res = append(res,t)
        }
        return res
    }else {
        beego.Debug("两矩阵无法进行相乘运算")
        return [][]float64{}
    }
    /*一个应用的例子
    a := [][]int{
        {1,2},
        {3,4},
        {5,6},
    }
    b := [][]int{
        {1,2,3},
        {3,4,1},
    }
    A := SQ{
        2,3,
        a,
    }
    B := SQ{
        3,2,
        b,
    }

    res := mul(A,B)
     */
}

//计算n阶行列式(N=n-1)
func Det(Matrix [][]float64,N int) float64 {
    var T0,T1,T2,Cha int
    var Num float64
    var B [][]float64

    if N>0 {
        Cha=0
        for i := 0; i < N; i++ {
            var tmpArr []float64
            for j := 0; j < N; j++ {
                tmpArr = append(tmpArr, 0)
            }
            B = append(B, tmpArr)
        }
        Num=0
        for T0=0;T0<=N;T0++{    //T0循环
            for T1=1;T1<=N;T1++ {    //T1循环
                for T2=0;T2<=N-1;T2++ {    //T2循环
                    if T2==T0 {
                        Cha = 1
                    }
                    B[T1-1][T2]=Matrix[T1][T2+Cha]
                }//T2循环
                Cha=0
            }//T1循环
            Num=Num+Matrix[0][T0]*Det(B,N-1)*math.Pow(-1,float64(T0))
        }//T0循环
        return Num
    }else if N==0 {
        return Matrix[0][0]
    }
    return 0
}

//矩阵求逆(N=n-1)
func Inverse(Matrix [][]float64,N int) (MatrixC [][]float64) {
    var T0,T1,T2,T3 int
    var B [][]float64
    for i := 0; i < N; i++ {
        var tmpArr []float64
        for j := 0; j < N; j++ {
            tmpArr = append(tmpArr, 0)
        }
        B = append(B, tmpArr)
    }
    Chay := 0
    Chax := 0
    var add float64
    add = 1/Det(Matrix,N)
    for T0=0;T0<=N;T0++{
        for T3=0;T3<=N;T3++{
            for T1=0;T1<=N-1;T1++{
                if T1<T0 {
                    Chax = 0
                }else {
                    Chax = 1
                }
                for T2=0;T2<=N-1;T2++{
                    if T2<T3 {
                        Chay = 0
                    }else{
                        Chay = 1
                    }
                    B[T1][T2]=Matrix[T1+Chax][T2+Chay]
                }//T2循环
            }//T1循环
            Det(B,N-1)
            MatrixC[T3][T0]=Det(B,N-1)*add*(math.Pow(-1, float64(T0+T3)))
        }
    }
    return MatrixC
}

 

posted @ 2018-09-14 20:44  livalon1  阅读(3637)  评论(1编辑  收藏  举报