{"cells": [{"cell_type": "code", "execution_count": 1, "metadata": {"school_cell_uuid": "03dae528c2b44fde84943eb24636aa35"}, "outputs": [{"data": {"text/plain": ["array([[1, 1, 0],\n", " [0, 1, 1],\n", " [1, 1, 1]])"]}, "execution_count": 1, "metadata": {}, "output_type": "execute_result"}], "source": ["import numpy as np\n", "\n", "A = np.array([[1, 1, 0], [0, 1, 1], [1, 1, 1]])\n", "A"]}, {"cell_type": "code", "execution_count": 2, "metadata": {"school_cell_uuid": "957d8ed805f44e91a5000657126af584"}, "outputs": [{"data": {"text/plain": ["array([[ 0., -1., 1.],\n", " [ 1., 1., -1.],\n", " [-1., 0., 1.]])"]}, "execution_count": 2, "metadata": {}, "output_type": "execute_result"}], "source": ["Ainv = np.linalg.inv(A)\n", "Ainv"]}, {"cell_type": "code", "execution_count": 3, "metadata": {"school_cell_uuid": "2a3cf7fc5356482a9b7f35f548e8a39f"}, "outputs": [{"data": {"text/plain": ["array([[2],\n", " [2],\n", " [3]])"]}, "execution_count": 3, "metadata": {}, "output_type": "execute_result"}], "source": ["b = np.array([[2], [2], [3]])\n", "b"]}, {"cell_type": "code", "execution_count": 4, "metadata": {"school_cell_uuid": "d3998c66a793467e9be557975195c796"}, "outputs": [{"data": {"text/plain": ["array([[1.],\n", " [1.],\n", " [1.]])"]}, "execution_count": 4, "metadata": {}, "output_type": "execute_result"}], "source": ["x = Ainv @ b\n", "x"]}, {"cell_type": "code", "execution_count": 5, "metadata": {"school_cell_uuid": "3f7280e955b44bae95a4b9c20b32c8ba"}, "outputs": [{"data": {"text/plain": ["array([[0.],\n", " [0.],\n", " [0.]])"]}, "execution_count": 5, "metadata": {}, "output_type": "execute_result"}], "source": ["A @ x - b"]}, {"cell_type": "code", "execution_count": 6, "metadata": {"school_cell_uuid": "28c7be2eab0e4df796af4d115b907a12"}, "outputs": [{"data": {"text/plain": ["array([[1.],\n", " [1.],\n", " [1.]])"]}, "execution_count": 6, "metadata": {}, "output_type": "execute_result"}], "source": ["x, resid, rank, s = np.linalg.lstsq(A, b)\n", "x"]}, {"cell_type": "code", "execution_count": 7, "metadata": {"school_cell_uuid": "03dae528c2b44fde84943eb24636aa35"}, "outputs": [{"data": {"text/plain": ["array([[1, 1, 0],\n", " [0, 1, 1],\n", " [1, 1, 1],\n", " [1, 1, 2]])"]}, "execution_count": 7, "metadata": {}, "output_type": "execute_result"}], "source": ["A = np.array([[1, 1, 0], [0, 1, 1], [1, 1, 1], [1, 1, 2]])\n", "A"]}, {"cell_type": "code", "execution_count": 8, "metadata": {"school_cell_uuid": "2a3cf7fc5356482a9b7f35f548e8a39f"}, "outputs": [{"data": {"text/plain": ["array([[2. ],\n", " [2. ],\n", " [3. ],\n", " [4.1]])"]}, "execution_count": 8, "metadata": {}, "output_type": "execute_result"}], "source": ["b = np.array([[2], [2], [3], [4.1]])\n", "b"]}, {"cell_type": "code", "execution_count": 9, "metadata": {"school_cell_uuid": "aa77a682007f47a2b816edf7a189f604"}, "outputs": [{"data": {"text/plain": ["array([[ 0.33333333, -1. , 0.33333333, 0.33333333],\n", " [ 0.5 , 1. , 0. , -0.5 ],\n", " [-0.5 , 0. , 0. , 0.5 ]])"]}, "execution_count": 9, "metadata": {}, "output_type": "execute_result"}], "source": ["Apinv = np.linalg.inv(A.T @ A) @ A.T\n", "Apinv"]}, {"cell_type": "code", "execution_count": 10, "metadata": {"school_cell_uuid": "a77798c4871040da9e036aea73b0d9c6"}, "outputs": [{"data": {"text/plain": ["array([[1.03333333],\n", " [0.95 ],\n", " [1.05 ]])"]}, "execution_count": 10, "metadata": {}, "output_type": "execute_result"}], "source": ["x = Apinv @ b\n", "x"]}, {"cell_type": "code", "execution_count": 11, "metadata": {"school_cell_uuid": "e434374930354029962fbf535fe31294"}, "outputs": [{"data": {"text/plain": ["array([[1.98333333],\n", " [2. ],\n", " [3.03333333],\n", " [4.08333333]])"]}, "execution_count": 11, "metadata": {}, "output_type": "execute_result"}], "source": ["A @ x"]}, {"cell_type": "code", "execution_count": 12, "metadata": {"school_cell_uuid": "4d1e378932d9463fa667104c42524208"}, "outputs": [{"data": {"text/plain": ["array([[1.03333333],\n", " [0.95 ],\n", " [1.05 ]])"]}, "execution_count": 12, "metadata": {}, "output_type": "execute_result"}], "source": ["x, resid, rank, s = np.linalg.lstsq(A, b)\n", "x"]}, {"cell_type": "code", "execution_count": 13, "metadata": {"school_cell_uuid": "74cc66e8dc654b18a87b633a6a691d3e"}, "outputs": [{"data": {"text/plain": ["(array([0.00166667]), 0.001666666666666655)"]}, "execution_count": 13, "metadata": {}, "output_type": "execute_result"}], "source": ["resid, np.linalg.norm(A @ x - b) ** 2"]}], "metadata": {"celltoolbar": "Edit 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.0"}, "toc": {"base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false}}, "nbformat": 4, "nbformat_minor": 4}