{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "pyZjL_rfVuyX" }, "source": [ "# Matrices" ], "id": "pyZjL_rfVuyX" }, { "cell_type": "markdown", "metadata": { "id": "jf51XtZRVuyc" }, "source": [ "Una matriz 4×5. \n", "Cuatro filas, cinco columnas. \n", "$$A_{(4×5)} =\n", "\\begin{pmatrix}\n", "a_{11} & a_{12} & a_{13} & a_{14} & a_{15} \\\\\n", "a_{21} & a_{22} & a_{23} & a_{24} & a_{25} \\\\\n", "a_{31} & a_{32} & a_{33} & a_{34} & a_{35} \\\\\n", "a_{41} & a_{42} & a_{43} & a_{44} & a_{45} \\\\\n", "\\end{pmatrix}\n", "$$" ], "id": "jf51XtZRVuyc" }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "KKV1-1faVuyh", "outputId": "3a24e667-183d-4df1-a334-990ff3f9a553", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "1 3 4 \n", "4 5 6 \n", "7 8 9 \n" ] } ], "source": [ "def imprimir(matriz):\n", " for f in range(len(matriz)):\n", " linea = \"\"\n", " for c in range(len(matriz[f])):\n", " linea += str(matriz[f][c]) + \" \"\n", " print(linea)\n", "\n", "if __name__ == \"__main__\":\n", " matriz = [\n", " [1,3,4],\n", " [4,5,6],\n", " [7,8,9]\n", " ]\n", " imprimir(matriz)" ], "id": "KKV1-1faVuyh" }, { "cell_type": "markdown", "metadata": { "id": "D8uQgs_QVuyl" }, "source": [ "**Ejercicio** \n", "En álgebra la [traza](https://es.wikipedia.org/wiki/Traza_(%C3%A1lgebra_lineal)) es la suma de la diagonal principal de una matriz. \n", "Generar una matriz de números aleatorios y calcular su traza." ], "id": "D8uQgs_QVuyl" }, { "cell_type": "markdown", "metadata": { "id": "FIcc9m-WVuyo" }, "source": [ "## Transponer una matriz" ], "id": "FIcc9m-WVuyo" }, { "cell_type": "markdown", "source": [ "### Usando `zip`" ], "metadata": { "id": "K40IzJqm_5fx" }, "id": "K40IzJqm_5fx" }, { "cell_type": "markdown", "metadata": { "id": "T3xKAXIXVuyp" }, "source": [ "Consideremos la siguiente matriz 3×4. \n", "$$A_{(3×4)} =\n", "\\begin{pmatrix}\n", "a_{11} & a_{12} & a_{13} & a_{14} \\\\\n", "a_{21} & a_{22} & a_{23} & a_{24} \\\\\n", "a_{31} & a_{32} & a_{33} & a_{34}\n", "\\end{pmatrix} =\n", "\\begin{pmatrix}\n", "1 & 2 & 3 & 4 \\\\\n", "5 & 6 & 7 & 8 \\\\\n", "9 & 10 & 11 & 12\n", "\\end{pmatrix}\n", "$$" ], "id": "T3xKAXIXVuyp" }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "G_EWYNc3Vuyr", "outputId": "259d829f-8359-4a5f-a5a1-d7808472bb7d", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[(1, 5, 9), (2, 6, 10), (3, 7, 11), (4, 8, 12)]\n", "\n", "1 5 9\n", "2 6 10\n", "3 7 11\n", "4 8 12\n" ] } ], "source": [ "matrix = [[1, 2, 3, 4],\n", " [5, 6, 7, 8],\n", " [9, 10, 11, 12]]\n", "\n", "m = list(zip(*matrix))\n", "\n", "print(m) # m es una lista de tuplas\n", "print()\n", "\n", "for i in range(len(m)):\n", " print(*m[i])" ], "id": "G_EWYNc3Vuyr" }, { "cell_type": "markdown", "metadata": { "id": "IW6UqHOmVuyt" }, "source": [ "Su transpuesta es $A^{t}$ de dimensión 4×3. \n", "$$A^{t} =\n", "\\begin{pmatrix}\n", "1 & 5 & 9 \\\\\n", "2 & 6 & 10 \\\\\n", "3 & 7 & 11 \\\\\n", "4 & 8 & 12\n", "\\end{pmatrix} \n", "$$" ], "id": "IW6UqHOmVuyt" }, { "cell_type": "markdown", "source": [ "### Transposición de una matriz permutando filas y columnas\n", "* Generamos la matriz m de números aleatorios, de dimensión 4x5\n", "* Son 4 filas y 5 columnas\n", "* Imprimimos m\n", "* Inicializamos la matriz transpuesta t de dimensión 5x4\n", "* Para crear t Invertimos las filas y columnas de m\n", "* Imprimimos la matriz transpuesta t" ], "metadata": { "id": "dPYDEuNRCEku" }, "id": "dPYDEuNRCEku" }, { "cell_type": "code", "source": [ "# Transponer matrices\n", "from random import seed, randint\n", "seed()\n", "\n", "# inicializamos la matriz m de dimensines 4x5\n", "m = [[None]*5 for _ in range(4)]\n", "\n", "# generamos aleatoriamente la matriz m\n", "for i in range(4):\n", " for j in range(5):\n", " m[i][j] = randint(1,9)\n", "\n", "# imprimimos la matriz m\n", "for i in range(4):\n", " for j in range(5):\n", " print(m[i][j],end=\" \")\n", " print()\n", "\n", "# inicializamos la matriz transpuesta t\n", "t = [[None]*len(m) for _ in range(len(m[0]))]\n", "\n", "for i in range(5):\n", " for j in range(4):\n", " t[i][j] = m[j][i]\n", "\n", "print()\n", "\n", "for i in range(5): # esta forma de imprimir una matriz\n", " print(*t[i]) # es más sencilla que la anterior" ], "metadata": { "id": "C-nqGaSqELJe", "outputId": "cb7b0107-3212-4daf-8026-3cb7a52f88fd", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "C-nqGaSqELJe", "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "6 9 2 9 2 \n", "6 8 9 4 1 \n", "9 1 4 9 9 \n", "3 1 8 7 4 \n", "\n", "6 6 9 3\n", "9 8 1 1\n", "2 9 4 8\n", "9 4 9 7\n", "2 1 9 4\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Copia de matriz 2D\n", "Podemos copiar una matriz de dos dimensiones sin necesidad de llamar a la librería copy para usar la función deepcopy." ], "metadata": { "id": "P-yqNyEtGkEA" }, "id": "P-yqNyEtGkEA" }, { "cell_type": "code", "source": [ "from random import seed, randint\n", "seed()\n", "\n", "# inicializamos la matriz m de dimensines 4x4\n", "m = [[None]*4 for _ in range(4)]\n", "\n", "# generamos aleatoriamente la matriz m\n", "for i in range(4):\n", " for j in range(4):\n", " m[i][j] = randint(1,9)\n", "\n", "# imprimimos la matriz m\n", "print(\"Matriz m\")\n", "for i in range(4):\n", " for j in range(4):\n", " print(m[i][j],end=\" \")\n", " print()\n", "print()\n", "\n", "# creamos la matriz cm que es una copia de m\n", "cm = [row[:] for row in m]\n", "\n", "# alteramos el primer valor de m\n", "m[0][0] = 0\n", "\n", "# imprimimos m con su alteración\n", "print(\"Matriz m con el primer valor alterado\")\n", "for i in range(4): # este sistema es mejor\n", " print(*m[i]) # para imprimir una matriz sin comas ni corchetes\n", "print()\n", "\n", "# imprimimos la copia y vemos que no se ha alterado\n", "print(\"La matriz copia de m es independiente, no tiene el valor alterado\")\n", "for i in range(4):\n", " print(*cm[i])" ], "metadata": { "id": "PGYM9Lk_GlUI", "outputId": "d0dfef90-d7b2-49cd-c74f-11f51cf3a7cc", "colab": { "base_uri": "https://localhost:8080/" } }, "id": "PGYM9Lk_GlUI", "execution_count": 4, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Matriz m\n", "7 8 3 5 \n", "2 7 5 3 \n", "6 8 5 9 \n", "3 8 2 8 \n", "\n", "Matriz m con el primer valor alterado\n", "0 8 3 5\n", "2 7 5 3\n", "6 8 5 9\n", "3 8 2 8\n", "\n", "La matriz copia de m es independiente, no tiene el valor alterado\n", "7 8 3 5\n", "2 7 5 3\n", "6 8 5 9\n", "3 8 2 8\n" ] } ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" }, "colab": { "name": "0590_matrices.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }