+{"cells":[{"attachments":{},"cell_type":"markdown","metadata":{},"source":["# Introduction to numpy"]},{"cell_type":"code","execution_count":null,"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","execution":{"iopub.execute_input":"2022-11-24T03:57:54.697727Z","iopub.status.busy":"2022-11-24T03:57:54.697183Z","iopub.status.idle":"2022-11-24T03:57:54.725184Z","shell.execute_reply":"2022-11-24T03:57:54.723863Z","shell.execute_reply.started":"2022-11-24T03:57:54.697619Z"},"trusted":true},"outputs":[],"source":["import numpy as np"]},{"cell_type":"markdown","metadata":{},"source":["### Creating arrays"]},{"cell_type":"code","execution_count":2,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.728667Z","iopub.status.busy":"2022-11-24T03:57:54.727638Z","iopub.status.idle":"2022-11-24T03:57:54.739089Z","shell.execute_reply":"2022-11-24T03:57:54.737712Z","shell.execute_reply.started":"2022-11-24T03:57:54.728627Z"},"trusted":true},"outputs":[{"data":{"text/plain":["(2, 3)"]},"execution_count":2,"metadata":{},"output_type":"execute_result"}],"source":["a = np.array([(1.5, 2, 3), (4, 5, 6)])\n","a.shape"]},{"cell_type":"code","execution_count":3,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.741996Z","iopub.status.busy":"2022-11-24T03:57:54.741154Z","iopub.status.idle":"2022-11-24T03:57:54.749998Z","shell.execute_reply":"2022-11-24T03:57:54.748867Z","shell.execute_reply.started":"2022-11-24T03:57:54.741947Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["[[1.5 2. 3. ]\n"," [4. 5. 6. ]]\n"]}],"source":["print(a)"]},{"cell_type":"code","execution_count":4,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.753226Z","iopub.status.busy":"2022-11-24T03:57:54.752306Z","iopub.status.idle":"2022-11-24T03:57:54.762943Z","shell.execute_reply":"2022-11-24T03:57:54.761683Z","shell.execute_reply.started":"2022-11-24T03:57:54.753181Z"},"trusted":true},"outputs":[{"data":{"text/plain":["(6, 1)"]},"execution_count":4,"metadata":{},"output_type":"execute_result"}],"source":["a.reshape(6,1).shape"]},{"cell_type":"code","execution_count":5,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.765628Z","iopub.status.busy":"2022-11-24T03:57:54.764766Z","iopub.status.idle":"2022-11-24T03:57:54.775310Z","shell.execute_reply":"2022-11-24T03:57:54.774182Z","shell.execute_reply.started":"2022-11-24T03:57:54.765594Z"},"trusted":true},"outputs":[{"data":{"text/plain":["(2, 3)"]},"execution_count":5,"metadata":{},"output_type":"execute_result"}],"source":["b = np.arange(6).reshape(2, 3)\n","b.shape"]},{"cell_type":"code","execution_count":6,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.778829Z","iopub.status.busy":"2022-11-24T03:57:54.777791Z","iopub.status.idle":"2022-11-24T03:57:54.788853Z","shell.execute_reply":"2022-11-24T03:57:54.787316Z","shell.execute_reply.started":"2022-11-24T03:57:54.778790Z"},"trusted":true},"outputs":[{"data":{"text/plain":["(1, 2, 3)"]},"execution_count":6,"metadata":{},"output_type":"execute_result"}],"source":["a[None,:,:].shape"]},{"cell_type":"code","execution_count":7,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.792527Z","iopub.status.busy":"2022-11-24T03:57:54.791700Z","iopub.status.idle":"2022-11-24T03:57:54.801865Z","shell.execute_reply":"2022-11-24T03:57:54.800555Z","shell.execute_reply.started":"2022-11-24T03:57:54.792481Z"},"trusted":true},"outputs":[{"data":{"text/plain":["(2, 2, 3)"]},"execution_count":7,"metadata":{},"output_type":"execute_result"}],"source":["c = np.array([a, b])\n","c.shape"]},{"cell_type":"code","execution_count":8,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.803815Z","iopub.status.busy":"2022-11-24T03:57:54.803409Z","iopub.status.idle":"2022-11-24T03:57:54.815204Z","shell.execute_reply":"2022-11-24T03:57:54.814310Z","shell.execute_reply.started":"2022-11-24T03:57:54.803772Z"},"trusted":true},"outputs":[{"data":{"text/plain":["array([[[1.5, 2. , 3. ],\n"," [4. , 5. , 6. ]],\n","\n"," [[0. , 1. , 2. ],\n"," [3. , 4. , 5. ]]])"]},"execution_count":8,"metadata":{},"output_type":"execute_result"}],"source":["c"]},{"cell_type":"markdown","metadata":{},"source":["### Numpy sequences"]},{"cell_type":"code","execution_count":9,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.817527Z","iopub.status.busy":"2022-11-24T03:57:54.817148Z","iopub.status.idle":"2022-11-24T03:57:54.827732Z","shell.execute_reply":"2022-11-24T03:57:54.826336Z","shell.execute_reply.started":"2022-11-24T03:57:54.817495Z"},"trusted":true},"outputs":[{"data":{"text/plain":["array([0. , 0.26179939, 0.52359878, 0.78539816, 1.04719755,\n"," 1.30899694, 1.57079633])"]},"execution_count":9,"metadata":{},"output_type":"execute_result"}],"source":["from numpy import pi\n","x = np.linspace(0, pi/2, 7)\n","x"]},{"cell_type":"code","execution_count":10,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.830311Z","iopub.status.busy":"2022-11-24T03:57:54.829895Z","iopub.status.idle":"2022-11-24T03:57:54.841986Z","shell.execute_reply":"2022-11-24T03:57:54.840613Z","shell.execute_reply.started":"2022-11-24T03:57:54.830276Z"},"trusted":true},"outputs":[{"data":{"text/plain":["array([1.00000000e+00, 9.65925826e-01, 8.66025404e-01, 7.07106781e-01,\n"," 5.00000000e-01, 2.58819045e-01, 6.12323400e-17])"]},"execution_count":10,"metadata":{},"output_type":"execute_result"}],"source":["f = np.cos(x)\n","f"]},{"cell_type":"markdown","metadata":{},"source":["### Some linear Algebra"]},{"cell_type":"code","execution_count":11,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.845335Z","iopub.status.busy":"2022-11-24T03:57:54.844383Z","iopub.status.idle":"2022-11-24T03:57:54.856063Z","shell.execute_reply":"2022-11-24T03:57:54.854739Z","shell.execute_reply.started":"2022-11-24T03:57:54.845287Z"},"trusted":true},"outputs":[{"data":{"text/plain":["array([[0, 1, 2],\n"," [3, 4, 5],\n"," [6, 7, 8]])"]},"execution_count":11,"metadata":{},"output_type":"execute_result"}],"source":["from numpy import linalg as LA\n","A = np.arange(9).reshape(3, 3)\n","A"]},{"cell_type":"code","execution_count":12,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.859074Z","iopub.status.busy":"2022-11-24T03:57:54.858144Z","iopub.status.idle":"2022-11-24T03:57:54.876461Z","shell.execute_reply":"2022-11-24T03:57:54.874969Z","shell.execute_reply.started":"2022-11-24T03:57:54.859038Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["0.0\n","2\n"]}],"source":["print(np.linalg.det(A))\n","print(LA.matrix_rank(A))"]},{"cell_type":"code","execution_count":13,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.880380Z","iopub.status.busy":"2022-11-24T03:57:54.879443Z","iopub.status.idle":"2022-11-24T03:57:54.896817Z","shell.execute_reply":"2022-11-24T03:57:54.895481Z","shell.execute_reply.started":"2022-11-24T03:57:54.880307Z"},"trusted":true},"outputs":[{"data":{"text/plain":["array([ 1.33484692e+01, -1.34846923e+00, -2.48477279e-16])"]},"execution_count":13,"metadata":{},"output_type":"execute_result"}],"source":["w, v = LA.eig(A)\n","w"]},{"cell_type":"code","execution_count":14,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.899494Z","iopub.status.busy":"2022-11-24T03:57:54.898526Z","iopub.status.idle":"2022-11-24T03:57:54.906821Z","shell.execute_reply":"2022-11-24T03:57:54.905635Z","shell.execute_reply.started":"2022-11-24T03:57:54.899453Z"},"trusted":true},"outputs":[{"name":"stdout","output_type":"stream","text":["3\n"]}],"source":["A = A + np.eye(3, 3)\n","print(LA.matrix_rank(A)) # Full rank, hence invertible"]},{"cell_type":"code","execution_count":15,"metadata":{"execution":{"iopub.execute_input":"2022-11-24T03:57:54.909301Z","iopub.status.busy":"2022-11-24T03:57:54.908092Z","iopub.status.idle":"2022-11-24T03:57:54.926773Z","shell.execute_reply":"2022-11-24T03:57:54.925447Z","shell.execute_reply.started":"2022-11-24T03:57:54.909252Z"},"trusted":true},"outputs":[{"data":{"text/plain":["array([1., 2., 5.])"]},"execution_count":15,"metadata":{},"output_type":"execute_result"}],"source":["b = np.array([1, 2, 5])\n","x = np.linalg.solve(A, b) # A^{-1}b\n","\n","np.matmul(A, x) # Should give back b"]}],"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.12"}},"nbformat":4,"nbformat_minor":4}
0 commit comments