Multiplicación de matrices cuadradas

Hola programadores, bueno ahora algo más para este blog y un poco de teoria de Wikipedia:

Multiplicación de una matriz por otra matriz

Dadas dos matrices A y B, tales que el número de columnas de la matriz A es igual al número de filas de la matriz B; es decir:
A:=(a_{ij})_{m \times n} y B:=(b_{ij})_{n \times p}
la multiplicación de A por B, que se denota A \cdot B, \; A \times B, \; A \circ B o simplemente AB, el resultado del producto es una nueva matriz C:
C=AB:=(c_{ij})_{m \times p}
donde cada elemento ci,j está definido por:
c_{ij} = \sum_{r=1}^n a_{ir}b_{rj}
es decir:


C = AB_{}^{} =  \begin{pmatrix}  a_{1 1} & \cdots & a_{1 n} \\  \vdots & \ddots & \vdots \\  a_{m 1} & \cdots & a_{m n}  \end{pmatrix} \cdot  \begin{pmatrix}  b_{1 1} & \cdots & b_{1 p} \\  \vdots & \ddots & \vdots \\  b_{n 1} & \cdots & b_{n p}  \end{pmatrix} = 










 \begin{pmatrix}  a_{11}b_{11}+ \cdots +a_{1n}b_{n1} & \cdots & a_{11}b_{1p}+ \cdots +a_{1n}b_{np} \\  \vdots & \ddots & \vdots \\  a_{m1}b_{11}+ \cdots +a_{mn}b_{n1} & \cdots & a_{m1}b_{1p}+ \cdots +a_{mn}b_{np}  \end{pmatrix} 
                                                               
Con esa introducción  un ejemplo con valores:
El producto de dos matrices generalmente no es conmutativo, es decir, AB ≠ BA.
AB_{}^{} =  \begin{pmatrix}  1 & 1 \\  0 & 1  \end{pmatrix} \cdot  \begin{pmatrix}  1 & 0 \\  1 & 1  \end{pmatrix} =  \begin{pmatrix}  2 & 1 \\  1 & 1  \end{pmatrix} y por el contrario BA_{}^{} =  \begin{pmatrix}  1 & 0 \\  1 & 1  \end{pmatrix} \cdot  \begin{pmatrix}  1 & 1 \\  0 & 1  \end{pmatrix} =




 \begin{pmatrix}  1 & 1 \\  1 & 2  \end{pmatrix}
Y la Implementacion en VB2010 en Consola:

Module Module1
    Dim A(500, 500) As Double
    Dim B(500, 500) As Double
    Dim C(500, 500) As Double
    Dim Aux(500, 500), Baux(500, 500) As Double
    Dim n As Integer ' Contienen filas y columnas respectivamente
    Sub Main()
        Console.BackgroundColor = ConsoleColor.White
        Console.Clear()
        Console.ForegroundColor = ConsoleColor.Black
        Console.Title = "Producto de Matrices"
        Console.Write(vbLf) 'Da un enter al titulo
        Console.Write("" & vbTab & " Presentacion de matrices m x n")
        Console.Write(vbLf)
        Console.Write(vbLf)
        Call Ingresar()
        Call PMM(A, B)
        Call Imprimir()
    End Sub
    Sub Ingresar()
        ' Ingresa el tamano de la matrix n x m
        Console.Write("" & vbTab & "Ingrese el orden de la matriz (n) : ", n)
        n = Integer.Parse(Console.ReadLine())

        '----------------------------------------------------------
        ' Lee los datos ingresados por teclado
        Console.WriteLine("" & vbLf & " * Ingrese los elementos de la Matriz  A * " & vbLf & "")
        For i As Integer = 1 To n
            For j As Integer = 1 To n
                Console.Write("" & vbTab & "Elemento [{0},{1}] : ", i, j)
                A(i, j) = Double.Parse(Console.ReadLine()) ' Lectura de datos
            Next
        Next
        '---------------------------------------------------
        ' Lee los datos ingresados por teclado
        Console.WriteLine("" & vbLf & " * Ingrese los elementos de la Matriz  B * " & vbLf & "")
        For i As Integer = 1 To n
            For j As Integer = 1 To n
                Console.Write("" & vbTab & "Elemento [{0},{1}] : ", i, j)
                B(i, j) = Double.Parse(Console.ReadLine()) ' Lectura de datos
            Next
        Next
    End Sub
    Sub Presentar()
        '----------------------------------------------------------
        'Presentacion de la matriz A

        Console.WriteLine("" & vbLf & " * La Matriz A ingresada es : *" & vbLf & "")
        For i As Integer = 1 To n
            For j As Integer = 1 To n
                Console.Write("" & vbTab & "{0,2:f4}", A(i, j))
            Next
            Console.Write("" & vbLf & "")

        Next
        '----------------------------------------------------------
        'Presentacion de la matriz B

        Console.WriteLine("" & vbLf & " * La Matriz A ingresada es : *" & vbLf & "")
        For i As Integer = 1 To n
            For j As Integer = 1 To n
                Console.Write("" & vbTab & "{0,2:f4}", B(i, j))
            Next
            Console.Write("" & vbLf & "")
        Next
    End Sub
    Function PMM(ByVal Aux(,) As Double, ByVal Baux(,) As Double) As Double
        Console.WriteLine("" & vbLf & " * El Producto (A * B) es: *" & vbLf & "")
        For i As Integer = 1 To n
            For j As Integer = 1 To n
                For k As Integer = 1 To n
                    C(i, j) = C(i, j) + (Aux(i, k) * Baux(k, j))
                Next
            Next
        Next
    End Function
    Sub Imprimir()
        For i As Integer = 1 To n 'to n
            For j As Integer = 1 To n 'to m
                Console.Write("" & vbTab & "{0,2:f2}", C(i, j))
            Next
            Console.Write("" & vbLf & "")
        Next
        Console.ReadLine()
    End Sub
End Module


Y mis capturas:(de acuerdo al ejemplo arriba)

para A*B
 
y para B*A

Se demuestra la propiedad no conmutativa. Bueno amigos con esto me despido hasta otra entrega!

Comentarios

Entradas populares de este blog

DevComponents DotnetBar 12.5.0.2

Visual Studio 2012

Guardar Datagridview en base de datos Access