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

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 Double) 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

 

你可能感兴趣的:(运算)