{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Matrices\n", "\n", "In our last exercise, we explored vectors. In this exercises we will continue by representing vectors as matrices. A matrix is an array of numbers that can be arranged into rows and columns. A matrix is denoted as $A(mXn)$, the m rows are horizontal and the n columns are vertical. The individual items in an m×n matrix A, often denoted by a(i,j) where i and j usually vary from 1 to m and n are called its elements or entries. Lets watch the video below to understand Matrices and Matrices operations in detial. " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('DQUIj5XoDao', width=860, height=460)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Matrix multiplication\n", "\n", "You can add or subtract matrices of same size. \n", "\n", "The number of rows in a matrix M is called the row rank of M, and the number of coloumns in A is called the column rank of A. If row = coloumn then there is no reason to distinguish between row rank and column rank; the common value is simply called the rank of the matrix. \n", "\n", "In Matrix multiplication there are two cases to consider. Scaler multiplication and Matrix multiplication. Scaler multiplication is multiplying a matrix by a single number, and Matrix multiplication is multiplying a matrix by another matrix. Scalar multiplication is fairly straightforward. You just multiply each element of the matrix by the scalar multiplier. Dot product multiplication requires two matrices in which the number of rows in the first matrix is the same as the number of columns in the second matrix." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " Matrix M : [[1 2 3]\n", " [4 5 6]\n", " [7 8 0]]\n", "\n", " Matrix N : [[1 1 1]\n", " [2 2 2]\n", " [3 3 3]]\n", "\n", " Rank of M : 3\n", "\n", " Transpose of M : [[1 4 7]\n", " [2 5 8]\n", " [3 6 0]]\n", "\n", " Matrix Dot Product : [[14 14 14]\n", " [32 32 32]\n", " [23 23 23]]\n" ] } ], "source": [ "import numpy as np\n", "\n", "M = np.array([[1,2,3],[4,5,6],[7,8,0]])\n", "\n", "N = np.array([[1,1,1],[2,2,2],[3,3,3]])\n", "\n", "print(\"\\n Matrix M : \", M)\n", "\n", "print(\"\\n Matrix N : \", N)\n", "\n", "print(\"\\n Rank of M :\", np.linalg.matrix_rank(M))\n", "\n", "print(\"\\n Transpose of M :\", np.transpose(M))\n", "\n", "print (\"\\n Matrix Dot Product : \", np.matmul(M,N))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solving systems of equations with matrices\n", "\n", "One cool thing about Matrix multiplication is that we can use it to solve systems of equations. In the video below, we will discuss a system of linear equations, and how to solve for x and y." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('Vq1tSPVQcPs', width=860, height=460)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"\"\"\n", "3x +4y = 20\n", "\n", "-4x + 9y = 26\n", "\"\"\"\n", "\n", "import numpy as np\n", "\n", "A = np.array([[3,4],[-4, 9]])\n", "\n", "B = np.array([20, 26])\n", "\n", "X= np.linalg.inv(A).dot(B)\n", "\n", "print(X)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Eigenvalues and eigenvectors\n", "\n", "Eigenvalues and eigenvectors form the basics of computing and mathematics. They are heavily used by scientists reduce the dimension space. Eigenvector is a vector that does not change when a transformation is applied to it, except that it becomes a scaled version of the original vector. Eigenvectors can help us calculating an approximation of a large matrix as a smaller vector. Similarly Eigenvalue is the scalar that is used to transform (stretch) an Eigenvector. \n", "\n", "In Linear Algebra, a scalar λ is called an eigenvalue of matrix A if there exists a column vector v such that\n", "\n", "$Av = \\lambda v$\n", "\n", "and v is non-zero. Any vector satisfying the above relation is known as eigenvector of the matrix A corresponding to the eigen value. Lets understand this more deeply in the video below." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from IPython.display import YouTubeVideo\n", "YouTubeVideo('0Y1xztCb434', width=860, height=460)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "a = np.array([[10,5],[4,4]])\n", "\n", "w, v = np.linalg.eig(a)\n", "\n", "print(w)\n", "print(v)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Exercises" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Solve the following system of linear equations using Python:\n", "\n", "
\n", "$4x + 3y + 2z = 25$\n", "
\n", "
\n", "$-2x + 2y + 3z = -10$\n", "
\n", "
\n", "$3x -5y + 2z = -4$\n", "
\n", "\n", "2. Find all eigenvalues and corresponding eigenvectors for the matrix A if A =\n", "\n", "\t\\begin{bmatrix} \n", "\t3 & -2 & 1 \\\\ \n", "\t2 & -4 & 0 \\\\ \n", "\t1 & 0 & 3 \n", "\t\\end{bmatrix} \n", "\t.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Your code here" ] } ], "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.7.4" } }, "nbformat": 4, "nbformat_minor": 4 }