%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%22marimo%3E%3D0.23.9%22%2C%20%22numpy%3E%3D2.0%22%2C%20%22scipy%3E%3D1.14%22%2C%20%22ipython%22%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Matrix%0A%0A%20%20%20%20%3E%20Marcos%20Duarte%2C%20Renato%20Naville%20Watanabe%3Cbr%3E%0A%20%20%20%20%3E%20%5BLaboratory%20of%20Biomechanics%20and%20Motor%20Control%5D(https%3A%2F%2Fbmclab.pesquisa.ufabc.edu.br%2F)%3Cbr%3E%0A%20%20%20%20%3E%20Federal%20University%20of%20ABC%2C%20Brazil%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20How%20to%20use%20this%20guide%0A%0A%20%20%20%20A%20motion%20record%20is%20a%20table%20of%20numbers%2C%20but%20not%20every%20operation%20on%20that%20table%20has%20the%20same%20meaning.%20This%20guide%20moves%20from%20shapes%20and%20products%20to%20solving%20equations%2C%20then%20asks%20when%20a%20matrix%20can%20represent%20an%20orthonormal%20coordinate%20frame.%20You%20need%20basic%20vectors%20and%20algebra.%0A%0A%20%20%20%20Read%20the%20examples%20in%20order%2C%20predict%20each%20result%2C%20and%20then%20run%20the%20cell.%20Try%20the%20distributed%20Exercises%20before%20opening%20their%20worked%20solutions.%20Finish%20with%20the%20checkpoint%20questions%20and%20independent%20Problems%3B%20their%20panels%20contain%20final%20answers%20only.%0A%0A%20%20%20%20**Challenge%200.**%20A%20table%20has%20100%20rows%20(time%20samples)%20and%20three%20columns%20(coordinates).%20What%20shape%20must%20a%20coordinate%20transform%20have%2C%20and%20what%20shape%20should%20the%20transformed%20data%20retain%3F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Python%20setup%0A%0A%20%20%20%20NumPy%20provides%20arrays%20and%20linear%20algebra%3B%20SciPy%20supplies%20a%20second%20pseudoinverse%20implementation.%20IPython%20display%20renders%20example%20arrays.%20No%20external%20data%20are%20needed.%20The%20old%20matrix%20class%20is%20shown%20only%20for%20comparison%3B%20use%20arrays%20and%20%60%40%60%20in%20new%20work.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20A%20matrix%20is%20a%20square%20or%20rectangular%20array%20of%20numbers%20or%20symbols%20(termed%20elements)%2C%20arranged%20in%20rows%20and%20columns.%20For%20instance%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20a_%7B1%2C1%7D%20%26%20a_%7B1%2C2%7D%20%26%20a_%7B1%2C3%7D%20%5C%5C%0A%20%20%20%20a_%7B2%2C1%7D%20%26%20a_%7B2%2C2%7D%20%26%20a_%7B2%2C3%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20%26%202%20%26%203%20%5C%5C%0A%20%20%20%204%20%26%205%20%26%206%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20matrix%20%24%5Cmathbf%7BA%7D%24%20above%20has%20two%20rows%20and%20three%20columns%2C%20it%20is%20a%202x3%20matrix.%0A%0A%20%20%20%20In%20Numpy%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Import%20the%20necessary%20libraries%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20from%20IPython.display%20import%20display%0A%20%20%20%20np.set_printoptions(precision%3D4)%20%20%23%20number%20of%20digits%20of%20precision%20for%20floating%20point%0A%20%20%20%20return%20(np%2C)%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A%20%3D%20np.array(%5B%5B1%2C%202%2C%203%5D%2C%20%5B4%2C%205%2C%206%5D%5D)%0A%20%20%20%20A%0A%20%20%20%20return%20(A%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20get%20information%20about%20the%20number%20of%20elements%20and%20the%20structure%20of%20the%20matrix%20(in%20fact%2C%20a%20Numpy%20array)%2C%20we%20can%20use%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20np)%3A%0A%20%20%20%20print('A%3A%5Cn'%2C%20A)%0A%20%20%20%20print('len(A)%20%3D%20'%2C%20len(A))%0A%20%20%20%20print('np.size(A)%20%3D%20'%2C%20np.size(A))%0A%20%20%20%20print('np.shape(A)%20%3D%20'%2C%20np.shape(A))%0A%20%20%20%20print('np.ndim(A)%20%3D%20'%2C%20np.ndim(A))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20could%20also%20have%20accessed%20this%20information%20with%20the%20correspondent%20methods%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A)%3A%0A%20%20%20%20print('A.size%20%3D%20'%2C%20A.size)%0A%20%20%20%20print('A.shape%20%3D%20'%2C%20A.shape)%0A%20%20%20%20print('A.ndim%20%3D%20'%2C%20A.ndim)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20used%20the%20array%20function%20in%20Numpy%20to%20represent%20a%20matrix.%20A%20%5BNumpy%20array%20is%20in%20fact%20different%20than%20a%20matrix%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Fuser%2Fnumpy-for-matlab-users.html)%2C%20if%20we%20want%20to%20use%20explicit%20matrices%20in%20Numpy%2C%20we%20have%20to%20use%20the%20function%20%60asmatrix%60%20(called%20%60mat%60%20before%20NumPy%202)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20B%20%3D%20np.asmatrix(%5B%5B1%2C%202%2C%203%5D%2C%20%5B4%2C%205%2C%206%5D%5D)%0A%20%20%20%20B%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Both%20array%20and%20matrix%20types%20work%20in%20Numpy%2C%20but%20you%20should%20choose%20only%20one%20type%20and%20not%20mix%20them%3B%20the%20array%20is%20preferred%20because%20it%20is%20%5Bthe%20standard%20vector%2Fmatrix%2Ftensor%20type%20of%20Numpy%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Fuser%2Fnumpy-for-matlab-users.html).%20So%2C%20let's%20use%20the%20array%20type%20for%20the%20rest%20of%20this%20text.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%201.**%20An%20array%20has%20shape%20%60(2%2C%203)%60.%20Give%20the%20shape%20of%20its%20transpose%20and%20of%20%60A%20%40%20A.T%60.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%201%0A%20%20%20%20Transposing%20exchanges%20rows%20and%20columns%2C%20so%20%60A.T%60%20is%20%60(3%2C%202)%60.%20The%20inner%20dimensions%20match%20in%20%60(2%2C3)%20%40%20(3%2C2)%60%2C%20giving%20%60(2%2C2)%60.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Addition%20and%20multiplication%0A%0A%20%20%20%20The%20sum%20of%20two%20m-by-n%20matrices%20%24%5Cmathbf%7BA%7D%24%20and%20%24%5Cmathbf%7BB%7D%24%20is%20another%20m-by-n%20matrix%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20a_%7B1%2C1%7D%20%26%20a_%7B1%2C2%7D%20%26%20a_%7B1%2C3%7D%20%5C%5C%0A%20%20%20%20a_%7B2%2C1%7D%20%26%20a_%7B2%2C2%7D%20%26%20a_%7B2%2C3%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5C%3B%5C%3B%5C%3B%20%5Ctext%7Band%7D%20%5C%3B%5C%3B%5C%3B%0A%20%20%20%20%5Cmathbf%7BB%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20b_%7B1%2C1%7D%20%26%20b_%7B1%2C2%7D%20%26%20b_%7B1%2C3%7D%20%5C%5C%0A%20%20%20%20b_%7B2%2C1%7D%20%26%20b_%7B2%2C2%7D%20%26%20b_%7B2%2C3%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%2B%20%5Cmathbf%7BB%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20a_%7B1%2C1%7D%2Bb_%7B1%2C1%7D%20%26%20a_%7B1%2C2%7D%2Bb_%7B1%2C2%7D%20%26%20a_%7B1%2C3%7D%2Bb_%7B1%2C3%7D%20%5C%5C%0A%20%20%20%20a_%7B2%2C1%7D%2Bb_%7B2%2C1%7D%20%26%20a_%7B2%2C2%7D%2Bb_%7B2%2C2%7D%20%26%20a_%7B2%2C3%7D%2Bb_%7B2%2C3%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20Numpy%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_1%20%3D%20np.array(%5B%5B1%2C%202%2C%203%5D%2C%20%5B4%2C%205%2C%206%5D%5D)%0A%20%20%20%20B_1%20%3D%20np.array(%5B%5B7%2C%208%2C%209%5D%2C%20%5B10%2C%2011%2C%2012%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_1)%0A%20%20%20%20print('B%3A%5Cn'%2C%20B_1)%0A%20%20%20%20print('A%20%2B%20B%3A%5Cn'%2C%20A_1%20%2B%20B_1)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20multiplication%20of%20the%20m-by-n%20matrix%20%24%5Cmathbf%7BA%7D%24%20by%20the%20n-by-p%20matrix%20%24%5Cmathbf%7BB%7D%24%20is%20a%20m-by-p%20matrix%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20a_%7B1%2C1%7D%20%26%20a_%7B1%2C2%7D%20%5C%5C%0A%20%20%20%20a_%7B2%2C1%7D%20%26%20a_%7B2%2C2%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5C%3B%5C%3B%5C%3B%20%5Ctext%7Band%7D%20%5C%3B%5C%3B%5C%3B%0A%20%20%20%20%5Cmathbf%7BB%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20b_%7B1%2C1%7D%20%26%20b_%7B1%2C2%7D%20%26%20b_%7B1%2C3%7D%20%5C%5C%0A%20%20%20%20b_%7B2%2C1%7D%20%26%20b_%7B2%2C2%7D%20%26%20b_%7B2%2C3%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%5Cmathbf%7BB%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20a_%7B1%2C1%7Db_%7B1%2C1%7D%20%2B%20a_%7B1%2C2%7Db_%7B2%2C1%7D%20%26%20a_%7B1%2C1%7Db_%7B1%2C2%7D%20%2B%20a_%7B1%2C2%7Db_%7B2%2C2%7D%20%26%20a_%7B1%2C1%7Db_%7B1%2C3%7D%20%2B%20a_%7B1%2C2%7Db_%7B2%2C3%7D%20%5C%5C%0A%20%20%20%20a_%7B2%2C1%7Db_%7B1%2C1%7D%20%2B%20a_%7B2%2C2%7Db_%7B2%2C1%7D%20%26%20a_%7B2%2C1%7Db_%7B1%2C2%7D%20%2B%20a_%7B2%2C2%7Db_%7B2%2C2%7D%20%26%20a_%7B2%2C1%7Db_%7B1%2C3%7D%20%2B%20a_%7B2%2C2%7Db_%7B2%2C3%7D%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20Numpy%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_2%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%5D)%0A%20%20%20%20B_2%20%3D%20np.array(%5B%5B5%2C%206%2C%207%5D%2C%20%5B8%2C%209%2C%2010%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_2)%0A%20%20%20%20print('B%3A%5Cn'%2C%20B_2)%0A%20%20%20%20print('A%20x%20B%3A%5Cn'%2C%20np.dot(A_2%2C%20B_2))%0A%20%20%20%20return%20A_2%2C%20B_2%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20For%20two-dimensional%20arrays%2C%20%60A%20%40%20B%60%20(or%20%60np.dot(A%2C%20B)%60)%20performs%20matrix%20multiplication%3B%20%60A%20*%20B%60%20multiplies%20corresponding%20entries.%3Cbr%3E%0A%20%20%20%20We%20can%20use%20the%20matrix%20type%20to%20show%20the%20equivalent%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A_2%2C%20B_2%2C%20np)%3A%0A%20%20%20%20A_3%20%3D%20np.asmatrix(A_2)%0A%20%20%20%20B_3%20%3D%20np.asmatrix(B_2)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_3)%0A%20%20%20%20print('B%3A%5Cn'%2C%20B_3)%0A%20%20%20%20print('A%20x%20B%3A%5Cn'%2C%20A_3%20*%20B_3)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Same%20result%20as%20before.%0A%0A%20%20%20%20The%20order%20in%20multiplication%20matters%2C%20%24%5Cmathbf%7BAB%7D%20%5Cneq%20%5Cmathbf%7BBA%7D%24%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_4%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%5D)%0A%20%20%20%20B_4%20%3D%20np.array(%5B%5B5%2C%206%5D%2C%20%5B7%2C%208%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_4)%0A%20%20%20%20print('B%3A%5Cn'%2C%20B_4)%0A%20%20%20%20print('A%20x%20B%3A%5Cn'%2C%20np.dot(A_4%2C%20B_4))%0A%20%20%20%20print('B%20x%20A%3A%5Cn'%2C%20np.dot(B_4%2C%20A_4))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20addition%20or%20multiplication%20of%20a%20scalar%20(a%20single%20number)%20to%20a%20matrix%20is%20performed%20over%20all%20the%20elements%20of%20the%20matrix%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_5%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%5D)%0A%20%20%20%20c%20%3D%2010%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_5)%0A%20%20%20%20print('c%3A%5Cn'%2C%20c)%0A%20%20%20%20print('c%20%2B%20A%3A%5Cn'%2C%20c%20%2B%20A_5)%0A%20%20%20%20print('cA%3A%5Cn'%2C%20c%20*%20A_5)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%202.**%20For%20%24A%3D%5Cbegin%7Bbmatrix%7D1%262%5C%5C0%261%5Cend%7Bbmatrix%7D%24%20and%20%24B%3D%5Cbegin%7Bbmatrix%7D2%260%5C%5C1%263%5Cend%7Bbmatrix%7D%24%2C%20calculate%20%24AB%24%20and%20compare%20it%20with%20the%20elementwise%20product.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%202%0A%20%20%20%20Row-by-column%20multiplication%20gives%20%24AB%3D%5Cbegin%7Bbmatrix%7D4%266%5C%5C1%263%5Cend%7Bbmatrix%7D%24.%20Elementwise%20multiplication%20gives%20%24%5Cbegin%7Bbmatrix%7D2%260%5C%5C0%263%5Cend%7Bbmatrix%7D%24.%20Shape%20compatibility%20does%20not%20make%20the%20operations%20interchangeable.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Transposition%0A%0A%20%20%20%20The%20transpose%20of%20the%20matrix%20%24%5Cmathbf%7BA%7D%24%20is%20the%20matrix%20%24%5Cmathbf%7BA%5ET%7D%24%20turning%20all%20the%20rows%20of%20matrix%20%24%5Cmathbf%7BA%7D%24%20into%20columns%20(or%20columns%20into%20rows)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20a%20%26%20b%20%26%20c%20%5C%5C%0A%20%20%20%20d%20%26%20e%20%26%20f%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5C%3B%5C%3B%5C%3B%5C%3B%5C%3B%5C%3B%5Ciff%5C%3B%5C%3B%5C%3B%5C%3B%5C%3B%5C%3B%0A%20%20%20%20%5Cmathbf%7BA%5ET%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20a%20%26%20d%20%5C%5C%0A%20%20%20%20b%20%26%20e%20%5C%5C%0A%20%20%20%20c%20%26%20f%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20NumPy%2C%20the%20transpose%20operator%20can%20be%20used%20as%20a%20method%20or%20function%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_6%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_6)%0A%20%20%20%20print('A.T%3A%5Cn'%2C%20A_6.T)%0A%20%20%20%20print('np.transpose(A)%3A%5Cn'%2C%20np.transpose(A_6))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Determinant%0A%0A%20%20%20%20The%20determinant%20is%20a%20number%20associated%20with%20a%20square%20matrix.%0A%0A%20%20%20%20The%20determinant%20of%20the%20following%20matrix%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%5B%20%20%5Cbegin%7Barray%7D%7Bccc%7D%0A%20%20%20%20a%20%26%20b%20%26%20c%20%5C%5C%0A%20%20%20%20d%20%26%20e%20%26%20f%20%5C%5C%0A%20%20%20%20g%20%26%20h%20%26%20i%20%5Cend%7Barray%7D%20%5Cright%5D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20is%20written%20as%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cleft%7C%20%20%5Cbegin%7Barray%7D%7Bccc%7D%0A%20%20%20%20a%20%26%20b%20%26%20c%20%5C%5C%0A%20%20%20%20d%20%26%20e%20%26%20f%20%5C%5C%0A%20%20%20%20g%20%26%20h%20%26%20i%20%5Cend%7Barray%7D%20%5Cright%7C%0A%20%20%20%20%24%24%0A%0A%20%20%20%20And%20has%20the%20value%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20(aei%20%2B%20bfg%20%2B%20cdh)%20-%20(ceg%20%2B%20bdi%20%2B%20afh)%0A%20%20%20%20%24%24%0A%0A%20%20%20%20One%20way%20to%20manually%20calculate%20the%20determinant%20of%20a%20matrix%20is%20to%20use%20the%20%5Brule%20of%20Sarrus%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRule_of_Sarrus)%3A%20we%20repeat%20the%20first%20two%20columns%20in%20the%20right%20side%20of%20the%20matrix%20and%20calculate%20the%20sum%20of%20the%20products%20of%20three%20diagonal%20north-west%20to%20south-east%20lines%20of%20matrix%20elements%2C%20minus%20the%20sum%20of%20the%20products%20of%20three%20diagonal%20south-west%20to%20north-east%20lines%20of%20elements%20as%20illustrated%20in%20the%20following%20figure%3A%3Cbr%3E%0A%20%20%20%20%3Cbr%3E%0A%20%20%20%20%3Cfigure%20aria-describedby%3D%22figure-caption-1%22%3E%3Cimg%20src%3D'https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2F6%2F66%2FSarrus_rule.svg'%20width%3D300%20alt%3D'Rule%20of%20Sarrus'%2F%3E%3Ccenter%3E%3C%2Fcenter%3E%20%3C%2Ffigure%3E%0A%0A%20%20%20%20%3Cspan%20id%3D%22figure-caption-1%22%20class%3D%22figure-caption%22%20style%3D%22display%3Ablock%3Btext-align%3Acenter%3Bfont-style%3Aitalic%22%3EFigure.%20Rule%20of%20Sarrus%3A%20the%20sum%20of%20the%20products%20of%20the%20solid%20diagonals%20minus%20the%20sum%20of%20the%20products%20of%20the%20dashed%20diagonals%20(%3Ca%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FRule_of_Sarrus%22%3Eimage%20from%20Wikipedia%3C%2Fa%3E).%3C%2Fspan%3E%0A%0A%20%20%20%20In%20Numpy%2C%20the%20determinant%20is%20computed%20with%20the%20%60linalg.det%60%20function%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_7%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_7)%0A%20%20%20%20return%20(A_7%2C)%0A%0A%0A%40app.cell%0Adef%20_(A_7%2C%20np)%3A%0A%20%20%20%20print('Determinant%20of%20A%3A%5Cn'%2C%20np.linalg.det(A_7))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%203.**%20For%20%24A%3D%5Cbegin%7Bbmatrix%7D2%264%5C%5C1%262%5Cend%7Bbmatrix%7D%24%2C%20find%20the%20determinant%20and%20decide%20whether%20an%20inverse%20exists.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%203%0A%20%20%20%20The%20determinant%20is%20%242(2)-4(1)%3D0%24.%20The%20rows%20are%20dependent%2C%20so%20the%20transformation%20loses%20information%20and%20no%20inverse%20exists.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Identity%0A%0A%20%20%20%20The%20identity%20matrix%20%24%5Cmathbf%7BI%7D%24%20is%20a%20matrix%20with%20ones%20in%20the%20main%20diagonal%20and%20zeros%20otherwise.%20The%203x3%20identity%20matrix%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BI%7D%20%3D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20%26%200%20%26%200%20%5C%5C%0A%20%20%20%200%20%26%201%20%26%200%20%5C%5C%0A%20%20%20%200%20%26%200%20%26%201%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20Numpy%2C%20instead%20of%20manually%20creating%20this%20matrix%20we%20can%20use%20the%20function%20%60eye%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20np.eye(3)%20%20%23%20identity%203x3%20array%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Inverse%0A%0A%20%20%20%20The%20inverse%20of%20the%20matrix%20%24%5Cmathbf%7BA%7D%24%20is%20the%20matrix%20%24%5Cmathbf%7BA%5E%7B-1%7D%7D%24%20such%20that%20the%20product%20between%20these%20two%20matrices%20is%20the%20identity%20matrix%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%5Ccdot%5Cmathbf%7BA%5E%7B-1%7D%7D%20%3D%20%5Cmathbf%7BI%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20calculation%20of%20the%20inverse%20of%20a%20matrix%20is%20usually%20not%20simple%20(the%20inverse%20of%20the%20matrix%20%24%5Cmathbf%7BA%7D%24%20is%20not%20%241%2F%5Cmathbf%7BA%7D%24%3B%20there%20is%20no%20division%20operation%20between%20matrices).%20%20The%20Numpy%20function%20%60linalg.inv%60%20computes%20the%20inverse%20of%20a%20square%20matrix%3A%0A%0A%20%20%20%20%20%20%20%20numpy.linalg.inv(a)%0A%20%20%20%20%20%20%20%20Compute%20the%20(multiplicative)%20inverse%20of%20a%20matrix.%0A%20%20%20%20%20%20%20%20Given%20a%20square%20matrix%20a%2C%20return%20the%20matrix%20ainv%20satisfying%20dot(a%2C%20ainv)%20%3D%20dot(ainv%2C%20a)%20%3D%20eye(a.shape%5B0%5D).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_8%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_8)%0A%20%20%20%20_Ainv%20%3D%20np.linalg.inv(A_8)%0A%20%20%20%20print('Inverse%20of%20A%3A%5Cn'%2C%20_Ainv)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Pseudo-inverse%0A%0A%20%20%20%20For%20a%20non-square%20matrix%2C%20its%20inverse%20is%20not%20defined.%20However%2C%20we%20can%20calculate%20what%20it's%20known%20as%20the%20pseudo-inverse.%3Cbr%3E%0A%20%20%20%20Consider%20a%20non-square%20matrix%2C%20%24%5Cmathbf%7BA%7D%24.%20To%20calculate%20its%20inverse%2C%20note%20that%20the%20following%20manipulation%20results%20in%20the%20identity%20matrix%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%20%5Cmathbf%7BA%7D%5ET%20(%5Cmathbf%7BA%7D%5Cmathbf%7BA%7D%5ET)%5E%7B-1%7D%20%3D%20%5Cmathbf%7BI%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20%24%5Cmathbf%7BA%7D%20%5Cmathbf%7BA%7D%5ET%24%20is%20a%20square%20matrix%20and%20is%20invertible%20(also%20%5Bnonsingular%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FInvertible_matrix))%20if%20%24%5Cmathbf%7BA%7D%24%20has%20full%20row%20rank%20(linearly%20independent%20rows).%3Cbr%3E%0A%20%20%20%20The%20matrix%20%24%5Cmathbf%7BA%7D%5ET(%5Cmathbf%7BA%7D%5Cmathbf%7BA%7D%5ET)%5E%7B-1%7D%24%20is%20known%20as%20the%20%5Bgeneralized%20inverse%20or%20Moore%E2%80%93Penrose%20pseudoinverse%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FMoore%25E2%2580%2593Penrose_pseudoinverse)%20of%20the%20matrix%20%24%5Cmathbf%7BA%7D%24%2C%20a%20generalization%20of%20the%20inverse%20matrix.%0A%0A%20%20%20%20To%20compute%20the%20Moore%E2%80%93Penrose%20pseudoinverse%2C%20we%20could%20calculate%20it%20by%20a%20naive%20approach%20in%20Python%3A%0A%20%20%20%20%60%60%60python%0A%20%20%20%20from%20numpy.linalg%20import%20inv%0A%20%20%20%20Ainv%20%3D%20A.T%20%40%20inv(A%20%40%20A.T)%0A%20%20%20%20%60%60%60%0A%20%20%20%20But%20both%20Numpy%20and%20Scipy%20have%20functions%20to%20calculate%20the%20pseudoinverse%2C%20which%20might%20give%20greater%20numerical%20stability%20(but%20read%20%5BInverses%20and%20pseudoinverses.%20Numerical%20issues%2C%20speed%2C%20symmetry%5D(http%3A%2F%2Fvene.ro%2Fblog%2Finverses-pseudoinverses-numerical-issues-speed-symmetry.html)).%20Of%20note%2C%20%5Bnumpy.linalg.pinv%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Freference%2Fgenerated%2Fnumpy.linalg.pinv.html)%20calculates%20the%20pseudoinverse%20of%20a%20matrix%20using%20its%20singular-value%20decomposition%20(SVD)%20and%20including%20all%20large%20singular%20values%20(using%20the%20%5BLAPACK%20(Linear%20Algebra%20Package)%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FLAPACK)%20routine%20gesdd)%2C%20and%20%5Bscipy.linalg.pinv%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.linalg.pinv.html)%20also%20uses%20the%20SVD%20(it%20used%20a%20least-squares%20solver%20until%20SciPy%201.7%2C%20when%20the%20SVD-based%20%60scipy.linalg.pinv2%60%20was%20folded%20into%20it%3B%20%60pinv2%60%20was%20removed%20in%20SciPy%201.9).%0A%0A%20%20%20%20For%20example%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20from%20scipy.linalg%20import%20pinv%0A%20%20%20%20A_9%20%3D%20np.array(%5B%5B1%2C%200%2C%200%5D%2C%20%5B0%2C%201%2C%200%5D%5D)%0A%20%20%20%20Apinv%20%3D%20pinv(A_9)%0A%20%20%20%20print('Matrix%20A%3A%5Cn'%2C%20A_9)%0A%20%20%20%20print('Pseudo-inverse%20of%20A%3A%5Cn'%2C%20Apinv)%0A%20%20%20%20print('A%20x%20Apinv%3A%5Cn'%2C%20A_9%20%40%20Apinv)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Orthogonality%0A%0A%20%20%20%20A%20real%20square%20matrix%20%24Q%24%20is%20orthogonal%20when%20%24Q%5ETQ%3DI%24%3A%20its%20columns%20(and%20rows)%20are%20**mutually%20perpendicular%20unit%20vectors**.%20Linear%20independence%20and%20unit%20length%20alone%20are%20insufficient.%0A%0A%20%20%20%20As%20consequence%3A%0A%0A%20%20%20%201.%20Its%20determinant%20is%20equal%20to%201%20or%20-1.%0A%20%20%20%202.%20Its%20inverse%20is%20equal%20to%20its%20transpose.%0A%0A%20%20%20%20However%2C%20keep%20in%20mind%20that%20not%20all%20matrices%20with%20determinant%20equals%20to%20one%20are%20orthogonal%2C%20for%20example%2C%20the%20matrix%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%203%20%26%202%20%5C%5C%0A%20%20%20%204%20%26%203%0A%20%20%20%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Has%20determinant%20equals%20to%20one%20but%20it%20is%20not%20orthogonal%20(the%20columns%20or%20rows%20don't%20have%20norm%20equals%20to%20one).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%204.**%20The%20columns%20%24(1%2C0)%24%20and%20%24(1%2F%5Csqrt2%2C1%2F%5Csqrt2)%24%20both%20have%20length%20one.%20Are%20they%20an%20orthogonal%20matrix%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%204%0A%20%20%20%20No.%20Their%20dot%20product%20is%20%241%2F%5Csqrt2%24%2C%20not%20zero.%20Unit%20length%20and%20independence%20are%20necessary%20but%20do%20not%20ensure%20perpendicularity.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Linear%20equations%0A%0A%20%20%20%20%3E%20A%20linear%20equation%20is%20an%20algebraic%20equation%20in%20which%20each%20term%20is%20either%20a%20constant%20or%20the%20product%20of%20a%20constant%20and%20(the%20first%20power%20of)%20a%20single%20variable%20(%5BWikipedia%5D(https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FLinear_equation)).%0A%0A%20%20%20%20We%20are%20interested%20in%20solving%20a%20set%20of%20linear%20equations%20where%20two%20or%20more%20variables%20are%20unknown%2C%20for%20instance%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x%20%2B%202y%20%3D%204%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%203x%20%2B%204y%20%3D%2010%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Let's%20see%20how%20to%20employ%20the%20matrix%20formalism%20to%20solve%20these%20equations%20(even%20that%20we%20know%20the%20solution%20is%20%60x%3D2%60%20and%20%60y%3D1%60).%3Cbr%3E%0A%20%20%20%20Let's%20express%20this%20set%20of%20equations%20in%20matrix%20form%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20%26%202%20%5C%5C%0A%20%20%20%203%20%26%204%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20x%20%5C%5C%0A%20%20%20%20y%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%3D%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%204%20%5C%5C%0A%20%20%20%2010%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20And%20for%20the%20general%20case%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BAv%7D%20%3D%20%5Cmathbf%7Bc%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Where%20%24%5Cmathbf%7BA%2C%20v%2C%20c%7D%24%20are%20the%20matrices%20above%20and%20we%20want%20to%20find%20the%20values%20%60x%2Cy%60%20for%20the%20matrix%20%24%5Cmathbf%7Bv%7D%24.%3Cbr%3E%0A%20%20%20%20Because%20there%20is%20no%20division%20of%20matrices%2C%20we%20can%20use%20the%20inverse%20of%20%24%5Cmathbf%7BA%7D%24%20to%20solve%20for%20%24%5Cmathbf%7Bv%7D%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%5E%7B-1%7D%5Cmathbf%7BAv%7D%20%3D%20%5Cmathbf%7BA%7D%5E%7B-1%7D%5Cmathbf%7Bc%7D%20%5Cimplies%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bv%7D%20%3D%20%5Cmathbf%7BA%7D%5E%7B-1%7D%5Cmathbf%7Bc%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20As%20we%20know%20how%20to%20compute%20the%20inverse%20of%20%24%5Cmathbf%7BA%7D%24%2C%20the%20solution%20is%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_10%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%5D)%0A%20%20%20%20_Ainv%20%3D%20np.linalg.inv(A_10)%0A%20%20%20%20c_1%20%3D%20np.array(%5B4%2C%2010%5D)%0A%20%20%20%20_v%20%3D%20np.dot(_Ainv%2C%20c_1)%0A%20%20%20%20print('v%3A%5Cn'%2C%20_v)%0A%20%20%20%20return%20A_10%2C%20c_1%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20What%20we%20expected.%0A%0A%20%20%20%20However%2C%20the%20use%20of%20the%20inverse%20of%20a%20matrix%20to%20solve%20equations%20is%20computationally%20inefficient.%3Cbr%3E%0A%20%20%20%20Instead%2C%20we%20should%20use%20%60linalg.solve%60%20for%20a%20determined%20system%20(same%20number%20of%20equations%20and%20unknowns)%20or%20%60linalg.lstsq%60%20otherwise%3A%3Cbr%3E%0A%20%20%20%20From%20the%20help%20for%20%60solve%60%3A%0A%0A%20%20%20%20%20%20%20%20numpy.linalg.solve(a%2C%20b)%5Bsource%5D%0A%20%20%20%20%20%20%20%20Solve%20a%20linear%20matrix%20equation%2C%20or%20system%20of%20linear%20scalar%20equations.%0A%20%20%20%20%20%20%20%20Computes%20the%20%E2%80%9Cexact%E2%80%9D%20solution%2C%20x%2C%20of%20the%20well-determined%2C%20i.e.%2C%20full%20rank%2C%20linear%20matrix%20equation%20ax%20%3D%20b.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A_10%2C%20c_1%2C%20np)%3A%0A%20%20%20%20_v%20%3D%20np.linalg.solve(A_10%2C%20c_1)%0A%20%20%20%20print('Using%20solve%3A')%0A%20%20%20%20print('v%3A%5Cn'%2C%20_v)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20And%20from%20the%20help%20for%20%60lstsq%60%3A%0A%0A%20%20%20%20%20%20%20%20numpy.linalg.lstsq(a%2C%20b%2C%20rcond%3D-1)%5Bsource%5D%0A%20%20%20%20%20%20%20%20Return%20the%20least-squares%20solution%20to%20a%20linear%20matrix%20equation.%0A%20%20%20%20%20%20%20%20Solves%20the%20equation%20a%20x%20%3D%20b%20by%20computing%20a%20vector%20x%20that%20minimizes%20the%20Euclidean%202-norm%20%7C%7C%20b%20-%20a%20x%20%7C%7C%5E2.%20The%20equation%20may%20be%20under-%2C%20well-%2C%20or%20over-%20determined%20(i.e.%2C%20the%20number%20of%20linearly%20independent%20rows%20of%20a%20can%20be%20less%20than%2C%20equal%20to%2C%20or%20greater%20than%20its%20number%20of%20linearly%20independent%20columns).%20If%20a%20is%20square%20and%20of%20full%20rank%2C%20then%20x%20(but%20for%20round-off%20error)%20is%20the%20%E2%80%9Cexact%E2%80%9D%20solution%20of%20the%20equation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A_10%2C%20c_1%2C%20np)%3A%0A%20%20%20%20_v%20%3D%20np.linalg.lstsq(A_10%2C%20c_1)%5B0%5D%0A%20%20%20%20print('Using%20lstsq%3A')%0A%20%20%20%20print('v%3A%5Cn'%2C%20_v)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Same%20solutions%2C%20of%20course.%0A%0A%20%20%20%20When%20a%20system%20of%20equations%20has%20a%20unique%20solution%2C%20the%20determinant%20of%20the%20**square**%20matrix%20associated%20to%20this%20system%20of%20equations%20is%20nonzero.%3Cbr%3E%0A%20%20%20%20When%20the%20determinant%20is%20zero%20there%20are%20either%20no%20solutions%20or%20many%20solutions%20to%20the%20system%20of%20equations.%0A%0A%20%20%20%20But%20if%20we%20have%20an%20overdetermined%20system%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x%20%2B%202y%20%3D%204%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%203x%20%2B%204y%20%3D%2010%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%205x%20%2B%206y%20%3D%2015%0A%20%20%20%20%24%24%0A%0A%20%20%20%20(Note%20that%20the%20possible%20solution%20for%20this%20set%20of%20equations%20is%20not%20exact%20because%20the%20last%20equation%20should%20be%20equal%20to%2016.)%0A%0A%20%20%20%20Let's%20try%20to%20solve%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_11%20%3D%20np.array(%5B%5B1%2C%202%5D%2C%20%5B3%2C%204%5D%2C%20%5B5%2C%206%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_11)%0A%20%20%20%20c_2%20%3D%20np.array(%5B4%2C%2010%2C%2015%5D)%0A%20%20%20%20print('c%3A%5Cn'%2C%20c_2)%0A%20%20%20%20return%20A_11%2C%20c_2%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Because%20the%20matix%20%24%5Cmathbf%7BA%7D%24%20is%20not%20squared%2C%20we%20can%20calculate%20its%20pseudo-inverse%20or%20use%20the%20function%20%60linalg.lstsq%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A_11%2C%20c_2%2C%20np)%3A%0A%20%20%20%20_v%20%3D%20np.linalg.lstsq(A_11%2C%20c_2)%5B0%5D%0A%20%20%20%20print('Using%20lstsq%3A')%0A%20%20%20%20print('v%3A%5Cn'%2C%20_v)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20**Exercise%205.**%20For%20equations%20%24x%3D1%24%20and%20%24x%3D3%24%2C%20what%20does%20least%20squares%20return%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Solution%20to%20Exercise%205%0A%20%20%20%20The%20objective%20is%20%24(x-1)%5E2%2B(x-3)%5E2%24.%20Its%20derivative%20is%20%244x-8%24%2C%20so%20the%20minimum%20is%20%24x%3D2%24.%20Neither%20equation%20is%20exact%3B%20residuals%20are%20%24-1%24%20and%20%241%24.%0A%20%20%20%20%2F%2F%2F%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20functions%20%60inv%60%20and%20%60solve%60%20failed%20because%20the%20matrix%20%24%5Cmathbf%7BA%7D%24%20was%20not%20square%20(overdetermined%20system).%20The%20function%20%60lstsq%60%20not%20only%20was%20able%20to%20handle%20an%20overdetermined%20system%20but%20was%20also%20able%20to%20find%20the%20best%20approximate%20solution.%0A%0A%20%20%20%20And%20if%20the%20the%20set%20of%20equations%20was%20undetermined%2C%20%60lstsq%60%20would%20also%20work.%20For%20instance%2C%20consider%20the%20system%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20x%20%2B%202y%20%2B%202z%20%3D%2010%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%24%24%0A%20%20%20%203x%20%2B%204y%20%2B%20z%20%3D%2013%0A%20%20%20%20%24%24%0A%0A%20%20%20%20And%20in%20matrix%20form%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%201%20%26%202%20%26%202%20%5C%5C%0A%20%20%20%203%20%26%204%20%26%201%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%20x%20%5C%5C%0A%20%20%20%20y%20%5C%5C%0A%20%20%20%20z%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%3D%20%5Cbegin%7Bbmatrix%7D%0A%20%20%20%2010%20%5C%5C%0A%20%20%20%2013%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20A%20possible%20solution%20would%20be%20%60x%3D2%2Cy%3D1%2Cz%3D3%60%2C%20but%20other%20values%20would%20also%20satisfy%20this%20set%20of%20equations.%0A%0A%20%20%20%20Let's%20try%20to%20solve%20using%20%60lstsq%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20A_12%20%3D%20np.array(%5B%5B1%2C%202%2C%202%5D%2C%20%5B3%2C%204%2C%201%5D%5D)%0A%20%20%20%20print('A%3A%5Cn'%2C%20A_12)%0A%20%20%20%20c_3%20%3D%20np.array(%5B10%2C%2013%5D)%0A%20%20%20%20print('c%3A%5Cn'%2C%20c_3)%0A%20%20%20%20return%20A_12%2C%20c_3%0A%0A%0A%40app.cell%0Adef%20_(A_12%2C%20c_3%2C%20np)%3A%0A%20%20%20%20_v%20%3D%20np.linalg.lstsq(A_12%2C%20c_3)%5B0%5D%0A%20%20%20%20print('Using%20lstsq%3A')%0A%20%20%20%20print('v%3A%5Cn'%2C%20_v)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20For%20this%20consistent%20underdetermined%20system%20the%20residual%20can%20be%20zero.%20%60lstsq%60%20chooses%20the%20solution%20with%20minimum%20Euclidean%20norm%20among%20all%20least-squares%20solutions%3B%20the%20displayed%20coefficients%20need%20not%20be%20the%20particular%20solution%20given%20above.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Checkpoint%20questions%0A%0A%20%20%20%201.%20When%20is%20%60A%20*%20B%60%20a%20matrix%20product%20for%20NumPy%20arrays%3F%0A%20%20%20%202.%20Does%20determinant%20one%20prove%20orthogonality%3F%0A%20%20%20%203.%20Why%20prefer%20a%20solver%20over%20forming%20an%20inverse%3F%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Checkpoint%20answers%0A%20%20%20%201.%20It%20is%20elementwise%20multiplication%2C%20not%20a%20matrix%20product.%0A%20%20%20%202.%20No.%0A%20%20%20%203.%20A%20direct%20solver%20avoids%20an%20unnecessary%20inverse%20and%20usually%20has%20better%20numerical%20behavior.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Problems%0A%0A%20%20%20%201.%20Solve%20%24x%2B2y%3D4%24%20and%20%243x%2B4y%3D10%24.%0A%20%20%20%202.%20Give%20the%20determinant%20and%20inverse%20of%20%60diag(2%2C%204)%60.%0A%20%20%20%203.%20Give%20the%20minimum-norm%20solution%20of%20%24x%2By%3D2%24.%0A%20%20%20%204.%20A%20%60(100%2C3)%60%20position%20table%20is%20right-multiplied%20by%20a%20%60(3%2C3)%60%20transform.%20Give%20the%20result%20shape.%0A%0A%20%20%20%20%2F%2F%2F%20details%20%7C%20Final%20answers%0A%20%20%20%201.%20%24x%3D2%24%2C%20%24y%3D1%24.%0A%20%20%20%202.%20Determinant%208%3B%20inverse%20%60diag(0.5%2C%200.25)%60.%0A%20%20%20%203.%20%24(1%2C1)%24.%0A%20%20%20%204.%20%60(100%2C3)%60.%0A%20%20%20%20%2F%2F%2F%0A%0A%20%20%20%20%23%23%20Go%20deeper%0A%0A%20%20%20%20%5BNumPy%20for%20MATLAB%20users%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Fuser%2Fnumpy-for-matlab-users.html)%20%E2%80%94%20array%20shapes%20and%20matrix%20products.%20Continue%20with%20%5B3D%20transformations%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Ftransformation_3d.py).%0A%0A%20%20%20%20%23%23%23%20Video%20lectures%20on%20the%20Internet%0A%0A%20%20%20%20%5BMIT%20Linear%20Algebra%20video%20lectures%5D(https%3A%2F%2Focw.mit.edu%2Fcourses%2F18-06-linear-algebra-spring-2010%2Fvideo_galleries%2Fvideo-lectures%2F)%20%E2%80%94%20the%20lectures%20on%20elimination%2C%20orthogonality%2C%20and%20least%20squares%20develop%20the%20operations%20used%20here.%0A%0A%20%20%20%20%23%23%20References%0A%0A%20%20%20%20-%20%5BNumPy%20for%20MATLAB%20users%5D(https%3A%2F%2Fnumpy.org%2Fdoc%2Fstable%2Fuser%2Fnumpy-for-matlab-users.html)%20%E2%80%94%20array%20shapes%20and%20matrix%20products.%20Continue%20with%20%5B3D%20transformations%5D(https%3A%2F%2Fmolab.marimo.io%2Fgithub%2FBMClab%2FBMC%2Fblob%2Fmaster%2Fnotebooks%2Ftransformation_3d.py).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
074d638376aa72c5ec799349ba2494e0