{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# A simple math problem for first grade" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "A = np.matrix('1 1 0; 0 1 1; 1 0 1')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[1, 1, 0],\n", " [0, 1, 1],\n", " [1, 0, 1]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "v1=np.matrix('6; 7; 9')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[6],\n", " [7],\n", " [9]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "v1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Inversion of the matrix gives the solution" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "invA = np.linalg.inv(A)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[ 0.5, -0.5, 0.5],\n", " [ 0.5, 0.5, -0.5],\n", " [-0.5, 0.5, 0.5]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invA" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution to the initial problem" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[ 4.],\n", " [ 2.],\n", " [ 5.]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invA.dot(v1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution to a more complicated problem" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "v2=np.matrix('15; 13; 18')" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "matrix([[ 10.],\n", " [ 5.],\n", " [ 8.]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "invA.dot(v2)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.6.8" } }, "nbformat": 4, "nbformat_minor": 4 }