{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "cdfe7257", "metadata": { "id": "cdfe7257" }, "source": [ "# NumPy [8] Álgebra lineal" ] }, { "cell_type": "code", "execution_count": null, "id": "fc0934ae", "metadata": { "id": "fc0934ae", "outputId": "1f983451-ec4d-43ed-a1f9-de57da5ca870" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[ 1 2 3]\n", " [-2 0 1]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "m = np.array([[1,2,3], [-2,0,1]])\n", "print(m)" ] }, { "cell_type": "code", "execution_count": null, "id": "6736da85", "metadata": { "id": "6736da85", "outputId": "9a95994e-9607-48da-c384-23c39257d223" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1]\n", " [2]\n", " [3]]\n" ] } ], "source": [ "b = np.array([[1], [2], [3]])\n", "print(b)" ] }, { "cell_type": "markdown", "id": "9d74d825", "metadata": { "id": "9d74d825" }, "source": [ "## Transposición matricial" ] }, { "cell_type": "code", "execution_count": null, "id": "63fa5b73", "metadata": { "id": "63fa5b73", "outputId": "efe0d4d7-54b9-4f6d-eec6-2af684b123a6" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 2 3]]\n" ] } ], "source": [ "a = np.transpose(b) # cambiamos filas por columnas\n", "print(a)" ] }, { "cell_type": "code", "execution_count": null, "id": "bb407acc", "metadata": { "id": "bb407acc", "outputId": "ee5ed72a-45a9-4f81-a39b-c2974d155b98" }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, -2],\n", " [ 2, 0],\n", " [ 3, 1]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.transpose(m)" ] }, { "cell_type": "code", "execution_count": null, "id": "5f3ea381", "metadata": { "id": "5f3ea381", "outputId": "c3c3ea33-4964-45a9-915e-a1e68b033a86" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1]\n", " [2]\n", " [3]]\n" ] } ], "source": [ "print(np.transpose(a))" ] }, { "cell_type": "markdown", "id": "0f1aa5ef", "metadata": { "id": "0f1aa5ef" }, "source": [ "#### Comprobación\n", "Es necesario usar dobles corchetes." ] }, { "cell_type": "code", "execution_count": null, "id": "2f039e9a", "metadata": { "id": "2f039e9a", "outputId": "b8618b1e-e39f-404a-f312-fef0bc340af4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1]\n", " [2]\n", " [3]]\n" ] } ], "source": [ "print(np.transpose([[1, 2, 3]]))" ] }, { "cell_type": "markdown", "id": "156384c9", "metadata": { "id": "156384c9" }, "source": [ "## Resolución de sistemas lineales\n", "$$Ax=b$$\n", "Nuestro objetivo es calcular $x$.\n", "\n", "$$A=\n", "\\begin{pmatrix}\n", "1 & -3 & 2\\\\\n", "5 & 6 & -1\\\\\n", "4 & -1 & 3\n", "\\end{pmatrix}$$.\n", "\n", "$$b=\n", "\\begin{pmatrix}\n", "-3 \\\\\n", "13 \\\\\n", "8\n", "\\end{pmatrix}$$\n", "\n", "Solución:\n", "$$x=A^{-1}b=\n", "\\begin{pmatrix}\n", "-2 \\\\\n", "5 \\\\\n", "7\n", "\\end{pmatrix}$$\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ff090b85", "metadata": { "id": "ff090b85", "outputId": "d6dd49e2-dbc3-43ac-aa6c-9c2439e774c6" }, "outputs": [ { "data": { "text/plain": [ "array([[ 1, -3, 2],\n", " [ 5, 6, -1],\n", " [ 4, -1, 3]])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.array([[1,-3,2], [5,6,-1], [4,-1,3]])\n", "print(A)" ] }, { "cell_type": "code", "execution_count": null, "id": "75c77ada", "metadata": { "id": "75c77ada", "outputId": "9ecdbc67-4389-4dbb-e8da-0555205e0ad0" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-3]\n", " [13]\n", " [ 8]]\n" ] } ], "source": [ "b = np.array([[-3], [13], [8]])\n", "print(b)" ] }, { "cell_type": "code", "execution_count": null, "id": "0ab4fa44", "metadata": { "id": "0ab4fa44" }, "outputs": [], "source": [ "x = np.linalg.solve(A, b)" ] }, { "cell_type": "code", "execution_count": null, "id": "283a3535", "metadata": { "id": "283a3535", "outputId": "ccbf5511-5a7f-45e5-f9df-962d66eee9fa" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[-2.]\n", " [ 5.]\n", " [ 7.]]\n" ] } ], "source": [ "print(x)" ] }, { "cell_type": "markdown", "id": "b46c78de", "metadata": { "id": "b46c78de" }, "source": [ "#### Comprobación\n", "La función allclose nos permite comprobar que los valores, aunque no sean exactamente iguales, si son muy cercanos." ] }, { "cell_type": "code", "execution_count": null, "id": "1028a3a8", "metadata": { "id": "1028a3a8", "outputId": "6c2b6293-19b8-4449-a01b-cef11f1d4162" }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.allclose(np.dot(A,x),b)" ] }, { "cell_type": "code", "execution_count": null, "id": "07264f90", "metadata": { "id": "07264f90", "outputId": "f4b2a47b-e04b-4e14-91d8-8def9352bb61" }, "outputs": [ { "data": { "text/plain": [ "array([[-3.],\n", " [13.],\n", " [ 8.]])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(A, x)" ] }, { "cell_type": "code", "execution_count": null, "id": "81524cfc", "metadata": { "id": "81524cfc", "outputId": "6ace7639-049e-4df4-dee7-9890b12a3cf0" }, "outputs": [ { "data": { "text/plain": [ "array([[-3],\n", " [13],\n", " [ 8]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": null, "id": "38ba4c93", "metadata": { "id": "38ba4c93", "outputId": "8b93efa5-fd99-4308-d546-f6c4e62aaa79" }, "outputs": [ { "data": { "text/plain": [ "array([[-3.55271368e-15],\n", " [ 0.00000000e+00],\n", " [ 3.55271368e-15]])" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.dot(A,x) - b" ] }, { "cell_type": "markdown", "id": "f7b0fc8c", "metadata": { "id": "f7b0fc8c" }, "source": [ "### Otra forma de cambiar las dimensiones de un vector\n", "En la práctica equivale a transponer el vector." ] }, { "cell_type": "code", "execution_count": null, "id": "2dd1de85", "metadata": { "scrolled": true, "id": "2dd1de85", "outputId": "2fe21248-b782-4cbf-c92a-90e4f1a875c4" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]\n" ] } ], "source": [ "c = np.array([1,2,3])\n", "print(c)" ] }, { "cell_type": "code", "execution_count": null, "id": "485e7306", "metadata": { "id": "485e7306", "outputId": "546c2dd6-f566-4436-d23f-2429815bca22" }, "outputs": [ { "data": { "text/plain": [ "array([[1],\n", " [2],\n", " [3]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.shape = (3, 1)\n", "c" ] }, { "cell_type": "code", "execution_count": null, "id": "37006158", "metadata": { "id": "37006158", "outputId": "73e90158-0730-41ca-daa7-561d57855e8d" }, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3]])" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c.shape = (1, 3)\n", "c" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11" }, "colab": { "name": "0100_numpy8_algebra_lineal.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }