码迷,mamicode.com
首页 > 其他好文 > 详细

矩阵及其运算(二):矩阵的运算

时间:2015-04-04 21:00:16      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:

1.矩阵的加法

 数学定义:设有两个m*n的矩阵AB,那么矩阵A与B的和记作A+B,规定其内的元素为A,B内对应元素的和.

 *矩阵加法满足交换律和结合律

 *矩阵减法:A-B=A+(-B)

  重载矩阵加法
    Public Overloads Shared Operator +(ByVal leftMatrix As Matrix, ByVal RightMatrix As Matrix) As Matrix
        Dim leftRow, leftCol, rightRow, rightCol As Integer
        leftRow = leftMatrix.Row : leftCol = leftMatrix.Col 被加矩阵的行与列长度
        rightRow = RightMatrix.Row : rightCol = RightMatrix.Col 相加矩阵的行与列长度
        If leftRow = rightRow And leftCol = rightCol Then
            Dim tempMatrix As New Matrix(leftRow, leftCol)
            For i = 1 To leftRow
                For j = 1 To leftCol
                    tempMatrix.Item(i, j) = leftMatrix.Item(i, j) + RightMatrix.Item(i, j) 对应元素相加
                Next
            Next
            Return tempMatrix
        Else
            Return Nothing
        End If
    End Operator
    重载矩阵减法
    Public Overloads Shared Operator -(ByVal leftMatrix As Matrix, ByVal RightMatrix As Matrix) As Matrix
        Dim leftRow, leftCol, rightRow, rightCol As Integer
        leftRow = leftMatrix.Row : leftCol = leftMatrix.Col 被减矩阵的行与列长度
        rightRow = RightMatrix.Row : rightCol = RightMatrix.Col 相减矩阵的行与列长度
        If leftRow = rightRow And leftCol = rightCol Then
            Dim tempMatrix As New Matrix(leftRow, leftCol)
            For i = 1 To leftRow
                For j = 1 To leftCol
                    tempMatrix.Item(i, j) = leftMatrix.Item(i, j) - RightMatrix.Item(i, j) 对应元素相减
                Next
            Next
            Return tempMatrix
        Else
            Return Nothing
        End If
    End Operator

 

2.数与矩阵相乘

数学定义:设数λ和矩阵A,那么λ与矩阵A的乘积记作λA,规定其内的元素为A,B内对应元素的乘积.

*数与矩阵相乘满足交换律,结合律和分配律

*矩阵加法与数乘矩阵统称为矩阵的线性运算

    重载数与矩阵相乘
    Public Overloads Shared Operator *(ByVal leftNum As Integer, ByVal RightMatrix As Matrix) As Matrix
        Dim rightRow, rightCol As Integer
        rightRow = RightMatrix.Row : rightCol = RightMatrix.Col 相加矩阵的行与列长度
        Dim tempMatrix As New Matrix(rightRow, rightCol)
        For i = 1 To rightRow
            For j = 1 To rightCol
                tempMatrix.Item(i, j) = RightMatrix.Item(i, j) * leftNum 对应元素相加
            Next
        Next
        Return tempMatrix
    End Operator
    重载矩阵与数相乘(交换律)
    Public Overloads Shared Operator *(ByVal RightMatrix As Matrix, ByVal leftNum As Integer) As Matrix
        Dim rightRow, rightCol As Integer
        rightRow = RightMatrix.Row : rightCol = RightMatrix.Col 相加矩阵的行与列长度
        Dim tempMatrix As New Matrix(rightRow, rightCol)
        For i = 1 To rightRow
            For j = 1 To rightCol
                tempMatrix.Item(i, j) = RightMatrix.Item(i, j) * leftNum 对应元素相加
            Next
        Next
        Return tempMatrix
    End Operator

 

3.矩阵与矩阵相乘

数学定义:设A是一个m*s的矩阵,B是一个s*n的矩阵,那么规定矩阵A与矩阵B的乘积是一个s*n的矩阵C,规定其i行j列的元素为A的i行与B的j列内对应元素的乘积和.

*矩阵与矩阵相乘满足结合律和分配律,但不满足交换律

 重载矩阵乘法
    Public Overloads Shared Operator *(ByVal leftMatrix As Matrix, ByVal RightMatrix As Matrix) As Matrix
        Dim leftRow, leftCol, rightRow, rightCol As Integer
        leftRow = leftMatrix.Row : leftCol = leftMatrix.Col 被乘矩阵的行与列长度
        rightRow = RightMatrix.Row : rightCol = RightMatrix.Col 相乘矩阵的行与列长度
        If leftCol = rightRow Then
            Dim tempMatrix As New Matrix(leftRow, rightCol)
            For i = 1 To leftRow
                For j = 1 To rightCol
                    For k = 1 To leftCol
                        tempMatrix.Item(i, j) = tempMatrix.Item(i, j) + leftMatrix.Item(i, k) * RightMatrix.Item(k, j) 对应元素相加
                    Next
                Next
            Next
            Return tempMatrix
        Else
            Return Nothing
        End If
    End Operator

 

矩阵及其运算(二):矩阵的运算

标签:

原文地址:http://www.cnblogs.com/experdot/p/4392817.html

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