Example: Matrix multiplication

Published 2008-12-15 | Author: Alain Matthes

Illustration of how to compute the product of two matrices.

Source:Altermundus.com

Download as: [PDF] [TEX]  •  [Open in writeLaTeX]

Matrix multiplication

Do you have a question regarding this example, TikZ or LaTeX in general? Just ask in the LaTeX Forum.
Oder frag auf Deutsch auf TeXwelt.de.

% Author : Alain Matthes
% Source : http://altermundus.com/pages/examples.html
\documentclass[]{article}

\usepackage[utf8]{inputenc}
\usepackage[upright]{fourier}
\usepackage{tikz}
\usetikzlibrary{matrix,arrows,decorations.pathmorphing}
\begin{document}

% l' unite
\newcommand{\myunit}{1 cm}
\tikzset{
    node style sp/.style={draw,circle,minimum size=\myunit},
    node style ge/.style={circle,minimum size=\myunit},
    arrow style mul/.style={draw,sloped,midway,fill=white},
    arrow style plus/.style={midway,sloped,fill=white},
}

\begin{tikzpicture}[>=latex]
% les matrices
\matrix (A) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (0,0)
{%
  a_{11} & a_{12} & \ldots & a_{1p}  \\
  \node[node style sp] {a_{21}};%
         & \node[node style sp] {a_{22}};%
                  & \ldots%
                           & \node[node style sp] {a_{2p}}; \\
  \vdots & \vdots & \ddots & \vdots  \\
  a_{n1} & a_{n2} & \ldots & a_{np}  \\
};
\node [draw,below=10pt] at (A.south) 
    { $A$ : \textcolor{red}{$n$ rows} $p$ columns};

\matrix (B) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter =)] at (6*\myunit,6*\myunit)
{%
  b_{11} & \node[node style sp] {b_{12}};%
                  & \ldots & b_{1q}  \\
  b_{21} & \node[node style sp] {b_{22}};%
                  & \ldots & b_{2q}  \\
  \vdots & \vdots & \ddots & \vdots  \\
  b_{p1} & \node[node style sp] {b_{p2}};%
                  & \ldots & b_{pq}  \\
};
\node [draw,above=10pt] at (B.north) 
    { $B$ : $p$ rows \textcolor{red}{$q$ columns}};
% matrice résultat
\matrix (C) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (6*\myunit,0)
{%
  c_{11} & c_{12} & \ldots & c_{1q} \\
  c_{21} & \node[node style sp,red] {c_{22}};%
                  & \ldots & c_{2q} \\
  \vdots & \vdots & \ddots & \vdots \\
  c_{n1} & c_{n2} & \ldots & c_{nq} \\
};
% les fleches
\draw[blue] (A-2-1.north) -- (C-2-2.north);
\draw[blue] (A-2-1.south) -- (C-2-2.south);
\draw[blue] (B-1-2.west)  -- (C-2-2.west);
\draw[blue] (B-1-2.east)  -- (C-2-2.east);
\draw[<->,red](A-2-1) to[in=180,out=90]
	node[arrow style mul] (x) {$a_{21}\times b_{12}$} (B-1-2);
\draw[<->,red](A-2-2) to[in=180,out=90]
	node[arrow style mul] (y) {$a_{22}\times b_{22}$} (B-2-2);
\draw[<->,red](A-2-4) to[in=180,out=90]
	node[arrow style mul] (z) {$a_{2p}\times b_{p2}$} (B-4-2);
\draw[red,->] (x) to node[arrow style plus] {$+$} (y)%
                  to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (z)%
                  to (C-2-2.north west);


\node [draw,below=10pt] at (C.south) 
    {$ C=A\times B$ : \textcolor{red}{$n$ rows}  \textcolor{red}{$q$ columns}};

\end{tikzpicture}

\begin{tikzpicture}[>=latex]
% unit
% defintion of matrices
\matrix (A) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (0,0)
{%
  a_{11} &\ldots & a_{1k} & \ldots & a_{1p}  \\
    \vdots & \ddots & \vdots & \vdots & \vdots \\
  \node[node style sp] {a_{i1}};& \ldots%
         & \node[node style sp] {a_{ik}};%
                  & \ldots%
                           & \node[node style sp] {a_{ip}}; \\
  \vdots & \vdots& \vdots & \ddots & \vdots  \\
  a_{n1}& \ldots & a_{nk} & \ldots & a_{np}  \\
};
\node [draw,below] at (A.south) { $A$ : \textcolor{red}{$n$ rows} $p$ columns};
\matrix (B) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter =)] at (7*\myunit,7*\myunit)
{%
  b_{11} &  \ldots& \node[node style sp] {b_{1j}};%
                  & \ldots & b_{1q}  \\
  \vdots& \ddots & \vdots & \vdots & \vdots \\
  b_{k1} &  \ldots& \node[node style sp] {b_{kj}};%
                  & \ldots & b_{kq}  \\
  \vdots& \vdots & \vdots & \ddots & \vdots \\
  b_{p1} &  \ldots& \node[node style sp] {b_{pj}};%
                  & \ldots & b_{pq}  \\
};
\node [draw,above] at (B.north) { $B$ : $p$ rows \textcolor{red}{$q$ columns}};
% matrice resultat
\matrix (C) [matrix of math nodes,%
             nodes = {node style ge},%
             left delimiter  = (,%
             right delimiter = )] at (7*\myunit,0)
{%
  c_{11} & \ldots& c_{1j} & \ldots & c_{1q} \\
  \vdots& \ddots & \vdots & \vdots & \vdots \\
    c_{i1}& \ldots & \node[node style sp,red] {c_{ij}};%
                  & \ldots & c_{iq} \\
  \vdots& \vdots & \vdots & \ddots & \vdots \\
  c_{n1}& \ldots & c_{nk} & \ldots & c_{nq} \\
};
\node [draw,below] at (C.south) 
    {$ C=A\times B$ : \textcolor{red}{$n$ rows}  \textcolor{red}{$q$ columns}};
% arrows
\draw[blue] (A-3-1.north) -- (C-3-3.north);
\draw[blue] (A-3-1.south) -- (C-3-3.south);
\draw[blue] (B-1-3.west)  -- (C-3-3.west);
\draw[blue] (B-1-3.east)  -- (C-3-3.east);
\draw[<->,red](A-3-1) to[in=180,out=90] 
    node[arrow style mul] (x) {$a_{i1}\times b_{1j}$} (B-1-3);
\draw[<->,red](A-3-3) to[in=180,out=90] 
    node[arrow style mul] (y) {$a_{ik}\times b_{kj}$}(B-3-3);
\draw[<->,red](A-3-5) to[in=180,out=90] 
    node[arrow style mul] (z) {$a_{ip}\times b_{pj}$}(B-5-3);
\draw[red,->] (x) to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (y)%
                  to node[arrow style plus] {$+\raisebox{.5ex}{\ldots}+$} (z);
                  %
                  % to (C-3-3.north west);
\draw[->,red,decorate,decoration=zigzag] (z) -- (C-3-3.north west);
\end{tikzpicture}
\end{document}

% encoding : utf8
% format   : pdfLaTeX
% author   : Alain Matthes

Comments

  • #1 Nick Andersen, December 16, 2008 at 4:03 p.m.

    This is a very nice depicturement of the matrix multiplication... Simply love it...

    The only thing i would change is the three lines: \vdots & \vdots & \vdots & \vdots \ I would change them to: \vdots & \vdots & \ddots & \vdots \

    Great!

  • #2 Kjell Magne Fauske, December 17, 2008 at 9:11 p.m.

    Thanks Nick for the comment. Alain Matthes sent me an updated version of the example as well as a variant of the same illustration. I have now updated the example with the changes.

  • #3 Rasmus, January 29, 2009 at 9:46 a.m.

    Really impressive example and a nice illustration of matrix multiplication. Thanks!

  • #4 raritet, January 4, 2012 at 3:41 a.m.

    Купим электро, радио компоненты и другие комплектующие с хранения

    КОНДЕНСАТОРКСОТ-2Г 500В 000750.000 ПФ 5% ППБ-50Д-47 ОМ-5% К10-17 820пФ 50В 5% NPO "Б" "имп" РЕЗИСТОР СП2-2 1010.000КОМВС2- 25ОЖО46805 Генератор кварц.48 MHz SMD KXO-97 13ЛО37И 92г 1533ИР38 К1012ГП1 86г КОНДЕНСАТОРК53-521 10В000015.000МКФ 30% Т РЕЗИСТОР С2-29В-0,5-271ОМ+-0,5%-1,0-А ОЖ0.467.099ТУ ПВ10 РОЗЕТКА ОС РС50АТВ С КОЖУХОМ БРО.364.045ТУ К73 – 11 – 160 х 0,039 мкф. М/сх. BA3823LS ДИОД 2Д213А ПУСКАТЕЛЬ Э/МАГНИТНЫЙ ПМЕ-111/380В Манометр ЭКМВ-1У (-1)..3кгс/см2 Резистор С2-29В-0,125145 кОм 1% СП5-3 330 5% 90г "9" "В" Винт М3х16кр К10-17 0,068мкФ 50В Y5V "Б" "имп" ВЕНТИЛЬ ПОВ-1 2Т505Б Амперметр Э365-1 1,5 0..100А 120х120х50 РОЗЕТКА ГРПМ2-46ГПЛ2-В КЕО.364.002ТУ К52-1БВ-4,7м-63в-20% ОС УВ76А 88,89г "5" РЕЗИСТОР С5-16МВ-5-0,47ОМ+-1% ОЖ0.467.513ТУ ТРАНЗИСТОРЫ БИПОЛЯРНЫЕ*** МИКРОСХЕМА К555ИЕ7 БК0.348.289-03ТУ Транзистор NPN1500V,16A BUH1215 P=200VA, H=7, F=12MHZ TO-218 ПСБ-В Конденсатор 0805 150 nF Y5V 50V +-20% КОНДЕНСАТОРМБГЧ 1-2А 250В 000000.500МКФ 1 Резистор 0,5W 6,8 Ом CF-50 5% 142ЕН5А 91г Au "5" В81-1,0 Вольтметр PNP, 0,006A, 5V П27А транзистор P=0,03BA, H=20, F=1MHZ 32 10 2Д204В 91г ДРОССЕЛЬ Д10Н ОЮО.475.000ТУ Контакт состояния КС-47 (сигнальный)на DIN-рейку (ИЭК) КОНДЕНСАТОР БГТ-1000В-0,25МКФ+-10% ОЖ0.462.046ТУ Диодный мост 3A, 200V KBPC102 BR-3(D46) 16x16x7mm прово MIC С2-23 47к 10% 0,25Вт ВИЛКА МРН22-2 ДРОССЕЛЬ Д53 ОЮ0.475.000ТУ РЭС49 2РМ22Б4Ш3В1В "5" 1988г 2У101Ж РПКМ3-67/67Г1-В с кожухом

    ICQ 586-179-503
    tel: +38 (066) 239-82-50
    Руслан

  • #5 Kien, March 26, 2012 at 1:36 a.m.

    Hi, I wanted to use your adapation of this matrix multiplication to demonstrate the Nearest Neighbour Algorithm, but I am having troubles with drawing the blue lines in my LaTeX document. I was wondering if you could offer any assistance? Many thanks.

  • #6 Nestor Valles, January 27, 2013 at 6:30 a.m.

    This is called the Falk's scheme

Adding comments is currently not enabled.

There are currently six comments on this entry.

Features
Tags
Scientific and technical areas

Creative Commons License