import%20marimo%0A%0A__generated_with%20%3D%20%220.23.3%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%0A%20%20%20%20%23%20Matrix%20Multiplication%20as%20Linear%20Transformations%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%20Matrix%20Multiplication%20as%20Linear%20Transformations%0A%0A%20%20%20%20In%20this%20interactive%20exploration%2C%20we'll%20understand%20how%20matrix%20multiplication%20relates%20to%20linear%20transformations.%0A%0A%20%20%20%20%23%23%20Key%20Concepts%0A%20%20%20%20-%20**Linear%20Transformation**%3A%20A%20function%20that%20maps%20vectors%20to%20vectors%20while%20preserving%20vector%20addition%20and%20scalar%20multiplication%0A%20%20%20%20-%20**Matrix%20Multiplication**%3A%20Represents%20the%20composition%20of%20linear%20transformations%0A%20%20%20%20-%20**Basis%20Vectors**%3A%20Standard%20vectors%20that%20span%20a%20vector%20space%0A%20%20%20%20-%20**Determinant**%3A%20Measures%20how%20much%20a%20transformation%20scales%20area%2Fvolume%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20%23%20Show%20basic%20matrix-vector%20multiplication%0A%20%20%20%20_A%20%3D%20np.array(%5B%5B2%2C%201%5D%2C%20%5B1%2C%202%5D%5D)%0A%20%20%20%20_x%20%3D%20np.array(%5B1%2C%200%5D)%0A%0A%20%20%20%20%23%20Apply%20transformation%0A%20%20%20%20_Ax%20%3D%20_A%20%40%20_x%0A%0A%20%20%20%20%23%20Create%20visualization%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(8%2C%206))%0A%0A%20%20%20%20%23%20Plot%20original%20basis%20vectors%0A%20%20%20%20_ax.arrow(0%2C%200%2C%201%2C%200%2C%20head_width%3D0.05%2C%20head_length%3D0.05%2C%20fc%3D%22blue%22%2C%20ec%3D%22blue%22%2C%20label%3D%22e%E2%82%81%22)%0A%20%20%20%20_ax.arrow(0%2C%200%2C%200%2C%201%2C%20head_width%3D0.05%2C%20head_length%3D0.05%2C%20fc%3D%22green%22%2C%20ec%3D%22green%22%2C%20label%3D%22e%E2%82%82%22)%0A%0A%20%20%20%20%23%20Plot%20transformed%20basis%20vectors%0A%20%20%20%20_ax.arrow(0%2C%200%2C%20_A%5B0%2C%200%5D%2C%20_A%5B1%2C%200%5D%2C%20head_width%3D0.05%2C%20head_length%3D0.05%2C%20fc%3D%22red%22%2C%20ec%3D%22red%22%2C%20label%3D%22A%C2%B7e%E2%82%81%22)%0A%20%20%20%20_ax.arrow(0%2C%200%2C%20_A%5B0%2C%201%5D%2C%20_A%5B1%2C%201%5D%2C%20head_width%3D0.05%2C%20head_length%3D0.05%2C%20fc%3D%22orange%22%2C%20ec%3D%22orange%22%2C%20label%3D%22A%C2%B7e%E2%82%82%22)%0A%0A%20%20%20%20%23%20Plot%20input%20vector%0A%20%20%20%20_ax.arrow(0%2C%200%2C%20_x%5B0%5D%2C%20_x%5B1%5D%2C%20head_width%3D0.05%2C%20head_length%3D0.05%2C%20fc%3D%22purple%22%2C%20ec%3D%22purple%22%2C%20label%3D%22x%22)%0A%0A%20%20%20%20%23%20Plot%20output%20vector%0A%20%20%20%20_ax.arrow(0%2C%200%2C%20_Ax%5B0%5D%2C%20_Ax%5B1%5D%2C%20head_width%3D0.05%2C%20head_length%3D0.05%2C%20fc%3D%22brown%22%2C%20ec%3D%22brown%22%2C%20label%3D%22Ax%22)%0A%0A%20%20%20%20_ax.set_xlim(-1%2C%204)%0A%20%20%20%20_ax.set_ylim(-1%2C%204)%0A%20%20%20%20_ax.grid(True)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20_ax.set_aspect(%22equal%22)%0A%20%20%20%20_ax.set_title(%22Linear%20Transformation%3A%20Matrix%20A%20applied%20to%20Vector%20x%22)%0A%20%20%20%20return%20mo%2C%20np%2C%20plt%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20%23%20Demonstrate%20matrix%20multiplication%20as%20transformation%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Understanding%20Linear%20Transformations%20Through%20Matrices%0A%0A%20%20%20%20Consider%20a%202%C3%972%20matrix%20%24A%24%3A%0A%20%20%20%20%24%24A%20%3D%20%08egin%7Bbmatrix%7D%202%20%26%201%20%5C%201%20%26%202%20%5Cend%7Bbmatrix%7D%24%24%0A%0A%20%20%20%20This%20matrix%20transforms%20the%20standard%20basis%20vectors%3A%0A%20%20%20%20-%20%24e_1%20%3D%20%08egin%7Bbmatrix%7D%201%20%5C%200%20%5Cend%7Bbmatrix%7D%24%20becomes%20%24A%20%5Ccdot%20e_1%20%3D%20%08egin%7Bbmatrix%7D%202%20%5C%201%20%5Cend%7Bbmatrix%7D%24%0A%20%20%20%20-%20%24e_2%20%3D%20%08egin%7Bbmatrix%7D%200%20%5C%201%20%5Cend%7Bbmatrix%7D%24%20becomes%20%24A%20%5Ccdot%20e_2%20%3D%20%08egin%7Bbmatrix%7D%201%20%5C%202%20%5Cend%7Bbmatrix%7D%24%0A%0A%20%20%20%20The%20columns%20of%20%24A%24%20represent%20where%20the%20basis%20vectors%20land%20after%20transformation.%0A%0A%20%20%20%20Let's%20see%20how%20this%20applies%20to%20a%20specific%20vector%20%24x%24%3A%0A%20%20%20%20%24%24x%20%3D%20%08egin%7Bbmatrix%7D%201%20%5C%200%20%5Cend%7Bbmatrix%7D%24%24%0A%0A%20%20%20%20Applying%20our%20transformation%3A%0A%20%20%20%20%24%24Ax%20%3D%20%08egin%7Bbmatrix%7D%202%20%26%201%20%5C%201%20%26%202%20%5Cend%7Bbmatrix%7D%20%08egin%7Bbmatrix%7D%201%20%5C%200%20%5Cend%7Bbmatrix%7D%20%3D%20%08egin%7Bbmatrix%7D%202%20%5C%201%20%5Cend%7Bbmatrix%7D%24%24%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20np)%3A%0A%20%20%20%20%23%20Interactive%20demonstration%20of%20composition%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Composition%20of%20Transformations%0A%0A%20%20%20%20Matrix%20multiplication%20corresponds%20to%20applying%20transformations%20in%20sequence.%0A%0A%20%20%20%20If%20we%20have%20two%20transformations%3A%0A%20%20%20%20-%20%24A%20%3D%20%5Cbegin%7Bbmatrix%7D%202%20%26%201%20%5C%5C%201%20%26%202%20%5Cend%7Bbmatrix%7D%24%0A%20%20%20%20-%20%24B%20%3D%20%5Cbegin%7Bbmatrix%7D%201%20%26%200%20%5C%5C%200%20%26%203%20%5Cend%7Bbmatrix%7D%24%0A%0A%20%20%20%20Applying%20%24B%24%20after%20%24A%24%20is%20equivalent%20to%20multiplying%20matrices%20%24BA%24%3A%0A%0A%20%20%20%20%24%24(BA)x%20%3D%20B(Ax)%24%24%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20%23%20Example%20of%20composition%0A%20%20%20%20_A%20%3D%20np.array(%5B%5B2%2C%201%5D%2C%20%5B1%2C%202%5D%5D)%0A%20%20%20%20_B%20%3D%20np.array(%5B%5B1%2C%200%5D%2C%20%5B0%2C%203%5D%5D)%0A%0A%20%20%20%20%23%20Calculate%20BA%0A%20%20%20%20_BA%20%3D%20_B%20%40%20_A%0A%0A%20%20%20%20%23%20Test%20with%20a%20vector%0A%20%20%20%20_x%20%3D%20np.array(%5B1%2C%201%5D)%0A%0A%20%20%20%20%23%20Apply%20transformations%20sequentially%0A%20%20%20%20_Ax%20%3D%20_A%20%40%20_x%0A%20%20%20%20_BAx%20%3D%20_BA%20%40%20_x%0A%0A%20%20%20%20%23%20Compare%20with%20direct%20multiplication%0A%20%20%20%20_result_direct%20%3D%20_B%20%40%20_Ax%0A%0A%20%20%20%20print(f%22Original%20vector%3A%20%7B_x%7D%22)%0A%20%20%20%20print(f%22A(x)%3A%20%7B_Ax%7D%22)%0A%20%20%20%20print(f%22B(A(x))%3A%20%7B_result_direct%7D%22)%0A%20%20%20%20print(f%22(BA)(x)%3A%20%7B_BAx%7D%22)%0A%20%20%20%20print(f%22Are%20they%20equal%3F%20%7Bnp.allclose(_result_direct%2C%20_BAx)%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20np)%3A%0A%20%20%20%20%23%20Introduce%20determinant%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Determinant%3A%20Measuring%20Scaling%20Factor%0A%0A%20%20%20%20The%20determinant%20of%20a%20transformation%20matrix%20tells%20us%20how%20much%20area%20(in%202D)%20or%20volume%20(in%203D)%20is%20scaled.%0A%0A%20%20%20%20For%20a%202%C3%972%20matrix%3A%0A%20%20%20%20%24%24%5Ctext%7Bdet%7D(A)%20%3D%20%5Cbegin%7Bvmatrix%7D%20a%20%26%20b%20%5C%5C%20c%20%26%20d%20%5Cend%7Bvmatrix%7D%20%3D%20ad%20-%20bc%24%24%0A%0A%20%20%20%20Example%20with%20our%20matrix%3A%0A%20%20%20%20%24%24A%20%3D%20%5Cbegin%7Bbmatrix%7D%202%20%26%201%20%5C%5C%201%20%26%202%20%5Cend%7Bbmatrix%7D%24%24%0A%0A%20%20%20%20%24%24%5Ctext%7Bdet%7D(A)%20%3D%202%20%5Ccdot%202%20-%201%20%5Ccdot%201%20%3D%204%20-%201%20%3D%203%24%24%0A%0A%20%20%20%20This%20means%20the%20transformation%20scales%20the%20area%20by%20a%20factor%20of%203.%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20%23%20Calculate%20determinant%0A%20%20%20%20_A%20%3D%20np.array(%5B%5B2%2C%201%5D%2C%20%5B1%2C%202%5D%5D)%0A%20%20%20%20_det_A%20%3D%20np.linalg.det(_A)%0A%0A%20%20%20%20print(f%22Matrix%20A%3A%20%7B_A%7D%22)%0A%20%20%20%20print(f%22Determinant%20of%20A%3A%20%7B_det_A%7D%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo%2C%20np%2C%20plt)%3A%0A%20%20%20%20%23%20Visualize%20how%20determinant%20affects%20area%0A%20%20%20%20mo.md(%22%22%22%0A%20%20%20%20%23%23%20Visualizing%20the%20Effect%20of%20Determinants%0A%0A%20%20%20%20The%20determinant%20tells%20us%20how%20the%20transformation%20changes%20the%20area%20of%20shapes.%0A%0A%20%20%20%20Let's%20consider%20a%20unit%20square%20with%20corners%20at%20(0%2C0)%2C%20(1%2C0)%2C%20(1%2C1)%2C%20(0%2C1).%0A%0A%20%20%20%20After%20applying%20transformation%20%24A%24%2C%20we%20get%20a%20parallelogram.%0A%0A%20%20%20%20With%20determinant%20%3D%203%2C%20the%20area%20is%20multiplied%20by%203.%0A%20%20%20%20%22%22%22)%0A%0A%20%20%20%20%23%20Define%20unit%20square%20corners%0A%20%20%20%20_square%20%3D%20np.array(%5B%5B0%2C%200%5D%2C%20%5B1%2C%200%5D%2C%20%5B1%2C%201%5D%2C%20%5B0%2C%201%5D%2C%20%5B0%2C%200%5D%5D)%20%20%23%20Closing%20the%20shape%0A%0A%20%20%20%20%23%20Apply%20transformation%0A%20%20%20%20_A%20%3D%20np.array(%5B%5B2%2C%201%5D%2C%20%5B1%2C%202%5D%5D)%0A%20%20%20%20_transformed_square%20%3D%20_square%20%40%20_A.T%0A%0A%20%20%20%20%23%20Plot%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(figsize%3D(8%2C%206))%0A%0A%20%20%20%20%23%20Plot%20original%20square%0A%20%20%20%20_ax.plot(_square%5B%3A%2C%200%5D%2C%20_square%5B%3A%2C%201%5D%2C%20%22b-%22%2C%20linewidth%3D2%2C%20label%3D%22Original%20Square%22)%0A%0A%20%20%20%20%23%20Plot%20transformed%20square%0A%20%20%20%20_ax.plot(_transformed_square%5B%3A%2C%200%5D%2C%20_transformed_square%5B%3A%2C%201%5D%2C%20%22r-%22%2C%20linewidth%3D2%2C%20label%3D%22Transformed%20Square%22)%0A%0A%20%20%20%20%23%20Add%20grid%20and%20labels%0A%20%20%20%20_ax.grid(True)%0A%20%20%20%20_ax.legend()%0A%20%20%20%20_ax.set_aspect(%22equal%22)%0A%20%20%20%20_ax.set_title(%22Effect%20of%20Linear%20Transformation%20on%20a%20Unit%20Square%22)%0A%20%20%20%20_ax.set_xlabel(%22X%22)%0A%20%20%20%20_ax.set_ylabel(%22Y%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%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
15d7e87338fe5141226560929c452f0f