import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%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%20pandas%20as%20pd%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%20%20%20%20from%20plotly.subplots%20import%20make_subplots%0A%0A%20%20%20%20return%20go%2C%20make_subplots%2C%20mo%2C%20np%2C%20pd%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%20Note%2005%3A%20Orthogonal%20Matrices%2C%20Isometries%2C%20and%20Rotary%20Embeddings%0A%0A%20%20%20%20%26larr%3B%20Previous%20Note%3A%20%5B04%20Rank-One%20Matrices%5D(04_rank_one_matrices.py)%20%7C%20Next%20Note%3A%20%5B06%20Moore-Penrose%20Pseudoinverse%5D(06_moore_penrose_inverse.py)%20%26rarr%3B%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%20%5Ba%5D%20Why%20do%20you%20need%20to%20know%20these%20concepts%3F%0A%0A%20%20%20%20An%20orthogonal%20matrix%20represents%20a%20linear%20transformation%20that%20preserves%20the%20fundamental%20geometry%20of%20Euclidean%20space.%20When%20an%20orthogonal%20operator%20acts%20on%20a%20vector%20space%2C%20it%20strictly%20preserves%20lengths%2C%20distances%2C%20and%20angles%20between%20vectors.%0A%0A%20%20%20%20These%20distance-preserving%20transformations%2C%20known%20as%20**isometries**%2C%20form%20the%20structural%20foundation%20of%3A%0A%20%20%20%201.%20**Rotary%20Position%20Embeddings%20(RoPE)**%3A%20Encoding%20relative%20token%20distances%20in%20modern%20LLMs%20(Llama%203%2C%20Mistral%2C%20Gemma)%20via%202D%20orthogonal%20rotations.%0A%20%20%20%202.%20**Orthogonal%20Weight%20Initialization**%3A%20Preserving%20gradient%20norm%20propagation%20across%20deep%20neural%20network%20layers%20without%20vanishing%20or%20exploding%20gradients.%0A%20%20%20%203.%20**QR%20Factorization%20and%20Stable%20Solvers**%3A%20Computing%20numerically%20stable%20matrix%20inversions%20and%20least-squares%20solutions%20without%20squaring%20condition%20numbers.%0A%20%20%20%204.%20**Coordinate%20Frame%20Transformations**%3A%20Rotating%20and%20reflecting%20feature%20spaces%20without%20introducing%20geometric%20distortion.%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---%0A%0A%20%20%20%20%23%23%20%5Bb%5D%20Concept%20explanation%20with%20their%20role%20in%20ML%2FAI%2FStats%3F%0A%0A%20%20%20%20%23%23%23%20Definition%3A%20Orthogonal%20Matrix%0A%0A%20%20%20%20A%20square%20matrix%20%24%5Cmathbf%7BQ%7D%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20n%7D%24%20is%20orthogonal%20if%20its%20transpose%20equals%20its%20inverse%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BQ%7D%5ET%20%5Cmathbf%7BQ%7D%20%3D%20%5Cmathbf%7BQ%7D%5Cmathbf%7BQ%7D%5ET%20%3D%20%5Cmathbf%7BI%7D_n%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20identity%20directly%20implies%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BQ%7D%5E%7B-1%7D%20%3D%20%5Cmathbf%7BQ%7D%5ET%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Writing%20%24%5Cmathbf%7BQ%7D%24%20in%20terms%20of%20its%20column%20vectors%20%24%5B%5Cmathbf%7Bq%7D_1%2C%20%5Cmathbf%7Bq%7D_2%2C%20%5Cdots%2C%20%5Cmathbf%7Bq%7D_n%5D%24%2C%20the%20condition%20%24%5Cmathbf%7BQ%7D%5ET%20%5Cmathbf%7BQ%7D%20%3D%20%5Cmathbf%7BI%7D_n%24%20states%20that%20the%20columns%20form%20an%20orthonormal%20basis%20of%20%24%5Cmathbb%7BR%7D%5En%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bq%7D_i%5ET%20%5Cmathbf%7Bq%7D_j%20%3D%20%5Cdelta_%7Bij%7D%20%3D%20%5Cbegin%7Bcases%7D%201%20%26%20%5Ctext%7Bif%20%7D%20i%20%3D%20j%20%5C%5C%200%20%26%20%5Ctext%7Bif%20%7D%20i%20%5Cneq%20j%20%5Cend%7Bcases%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Similarly%2C%20%24%5Cmathbf%7BQ%7D%5Cmathbf%7BQ%7D%5ET%20%3D%20%5Cmathbf%7BI%7D_n%24%20establishes%20that%20the%20rows%20of%20%24%5Cmathbf%7BQ%7D%24%20also%20form%20an%20orthonormal%20basis.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20The%20Isometry%20Property%3A%20Preservation%20of%20Lengths%20and%20Angles%0A%0A%20%20%20%20Let%20%24%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7By%7D%20%5Cin%20%5Cmathbb%7BR%7D%5En%24%20be%20arbitrary%20vectors%2C%20and%20let%20%24%5Ctilde%7B%5Cmathbf%7Bx%7D%7D%20%3D%20%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%24%20and%20%24%5Ctilde%7B%5Cmathbf%7By%7D%7D%20%3D%20%5Cmathbf%7BQ%7D%5Cmathbf%7By%7D%24%20denote%20their%20transformed%20counterparts.%0A%0A%20%20%20%20The%20inner%20product%20between%20the%20transformed%20vectors%20satisfies%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Clangle%20%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7BQ%7D%5Cmathbf%7By%7D%20%5Crangle%20%3D%20(%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D)%5ET%20(%5Cmathbf%7BQ%7D%5Cmathbf%7By%7D)%20%3D%20%5Cmathbf%7Bx%7D%5ET%20(%5Cmathbf%7BQ%7D%5ET%20%5Cmathbf%7BQ%7D)%20%5Cmathbf%7By%7D%20%3D%20%5Cmathbf%7Bx%7D%5ET%20%5Cmathbf%7BI%7D_n%20%5Cmathbf%7By%7D%20%3D%20%5Cmathbf%7Bx%7D%5ET%20%5Cmathbf%7By%7D%20%3D%20%5Clangle%20%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7By%7D%20%5Crangle%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Setting%20%24%5Cmathbf%7By%7D%20%3D%20%5Cmathbf%7Bx%7D%24%20demonstrates%20the%20preservation%20of%20Euclidean%20norm%20(%24L_2%24%20length)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5C%7C%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%5C%7C_2%5E2%20%3D%20%5Clangle%20%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%20%5Crangle%20%3D%20%5C%7C%5Cmathbf%7Bx%7D%5C%7C_2%5E2%20%5Cimplies%20%5C%7C%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%5C%7C_2%20%3D%20%5C%7C%5Cmathbf%7Bx%7D%5C%7C_2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Because%20both%20lengths%20and%20dot%20products%20remain%20invariant%2C%20the%20angle%20%24%5Ctheta%24%20between%20any%20pair%20of%20vectors%20is%20strictly%20preserved%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ccos(%5Ctheta)%20%3D%20%5Cfrac%7B%5Clangle%20%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7BQ%7D%5Cmathbf%7By%7D%20%5Crangle%7D%7B%5C%7C%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%5C%7C_2%20%5C%7C%5Cmathbf%7BQ%7D%5Cmathbf%7By%7D%5C%7C_2%7D%20%3D%20%5Cfrac%7B%5Clangle%20%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7By%7D%20%5Crangle%7D%7B%5C%7C%5Cmathbf%7Bx%7D%5C%7C_2%20%5C%7C%5Cmathbf%7By%7D%5C%7C_2%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Linear%20operators%20that%20preserve%20Euclidean%20distances%20are%20called%20**isometries**.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Determinant%20Dichotomy%3A%20Rotations%20vs%20Reflections%0A%0A%20%20%20%20Taking%20the%20determinant%20of%20both%20sides%20of%20%24%5Cmathbf%7BQ%7D%5ET%20%5Cmathbf%7BQ%7D%20%3D%20%5Cmathbf%7BI%7D_n%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cdet(%5Cmathbf%7BQ%7D%5ET%20%5Cmathbf%7BQ%7D)%20%3D%20%5Cdet(%5Cmathbf%7BQ%7D%5ET)%20%5Cdet(%5Cmathbf%7BQ%7D)%20%3D%20(%5Cdet(%5Cmathbf%7BQ%7D))%5E2%20%3D%20%5Cdet(%5Cmathbf%7BI%7D_n)%20%3D%201%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Therefore%2C%20every%20orthogonal%20matrix%20satisfies%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cdet(%5Cmathbf%7BQ%7D)%20%5Cin%20%5C%7B%2B1%2C%20-1%5C%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20separates%20orthogonal%20transformations%20into%20two%20distinct%20geometric%20classes%3A%0A%0A%20%20%20%20%23%23%23%23%20Rotations%20(%24%5Cdet(%5Cmathbf%7BQ%7D)%20%3D%20%2B1%24)%0A%0A%20%20%20%20Transformations%20belonging%20to%20the%20Special%20Orthogonal%20Group%20%24%5Ctext%7BSO%7D(n)%24%20that%20preserve%20spatial%20orientation.%20In%20two%20dimensions%2C%20a%20counter-clockwise%20rotation%20by%20angle%20%24%5Ctheta%24%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BR%7D_%5Ctheta%20%3D%20%5Cbegin%7Bbmatrix%7D%20%5Ccos%5Ctheta%20%26%20-%5Csin%5Ctheta%20%5C%5C%20%5Csin%5Ctheta%20%26%20%5Ccos%5Ctheta%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%20%5Cdet(%5Cmathbf%7BR%7D_%5Ctheta)%20%3D%20%5Ccos%5E2%5Ctheta%20%2B%20%5Csin%5E2%5Ctheta%20%3D%20%2B1%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%23%20Reflections%20(%24%5Cdet(%5Cmathbf%7BQ%7D)%20%3D%20-1%24)%0A%0A%20%20%20%20Transformations%20that%20reverse%20spatial%20orientation%20(mirror%20reflection).%20A%20reflection%20across%20a%20line%20oriented%20at%20angle%20%24%5Cphi%24%20relative%20to%20the%20positive%20x-axis%20is%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BH%7D_%5Cphi%20%3D%20%5Cbegin%7Bbmatrix%7D%20%5Ccos(2%5Cphi)%20%26%20%5Csin(2%5Cphi)%20%5C%5C%20%5Csin(2%5Cphi)%20%26%20-%5Ccos(2%5Cphi)%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%20%5Cdet(%5Cmathbf%7BH%7D_%5Cphi)%20%3D%20-%5Ccos%5E2(2%5Cphi)%20-%20%5Csin%5E2(2%5Cphi)%20%3D%20-1%0A%20%20%20%20%24%24%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Rotary%20Position%20Embedding%20(RoPE)%20in%20LLMs%0A%0A%20%20%20%20State-of-the-art%20transformer%20architectures%20(such%20as%20Llama%203%2C%20Mistral%2C%20and%20Gemma)%20replace%20additive%20absolute%20positional%20embeddings%20with%20Rotary%20Position%20Embeddings%20(RoPE).%0A%0A%20%20%20%20RoPE%20pairs%20consecutive%20channels%20in%20query%20and%20key%20vectors%20and%20applies%20a%202D%20orthogonal%20rotation%20matrix%20%24%5Cmathbf%7BR%7D_m%24%20corresponding%20to%20token%20position%20%24m%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ctilde%7B%5Cmathbf%7Bq%7D%7D_m%20%3D%20%5Cmathbf%7BR%7D_m%20%5Cmathbf%7Bq%7D_m%2C%20%5Cquad%20%5Ctilde%7B%5Cmathbf%7Bk%7D%7D_n%20%3D%20%5Cmathbf%7BR%7D_n%20%5Cmathbf%7Bk%7D_n%0A%20%20%20%20%24%24%0A%0A%20%20%20%20When%20computing%20the%20self-attention%20dot%20product%20between%20query%20token%20%24m%24%20and%20key%20token%20%24n%24%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Clangle%20%5Ctilde%7B%5Cmathbf%7Bq%7D%7D_m%2C%20%5Ctilde%7B%5Cmathbf%7Bk%7D%7D_n%20%5Crangle%20%3D%20(%5Cmathbf%7BR%7D_m%20%5Cmathbf%7Bq%7D_m)%5ET%20(%5Cmathbf%7BR%7D_n%20%5Cmathbf%7Bk%7D_n)%20%3D%20%5Cmathbf%7Bq%7D_m%5ET%20%5Cmathbf%7BR%7D_m%5ET%20%5Cmathbf%7BR%7D_n%20%5Cmathbf%7Bk%7D_n%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Because%20rotation%20matrices%20compose%20orthogonally%20(%24%5Cmathbf%7BR%7D_m%5ET%20%3D%20%5Cmathbf%7BR%7D_%7B-m%7D%24%20and%20%24%5Cmathbf%7BR%7D_%7B-m%7D%5Cmathbf%7BR%7D_n%20%3D%20%5Cmathbf%7BR%7D_%7Bn%20-%20m%7D%24)%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Clangle%20%5Ctilde%7B%5Cmathbf%7Bq%7D%7D_m%2C%20%5Ctilde%7B%5Cmathbf%7Bk%7D%7D_n%20%5Crangle%20%3D%20%5Cmathbf%7Bq%7D_m%5ET%20%5Cmathbf%7BR%7D_%7Bn%20-%20m%7D%20%5Cmathbf%7Bk%7D_n%0A%20%20%20%20%24%24%0A%0A%20%20%20%20The%20inner%20product%20depends%20solely%20on%20the%20relative%20displacement%20%24(n%20-%20m)%24%2C%20while%20the%20%24L_2%24%20norm%20of%20every%20individual%20query%20and%20key%20vector%20is%20strictly%20preserved.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Orthogonal%20Weight%20Initialization%20for%20Deep%20Networks%0A%0A%20%20%20%20In%20deep%20neural%20networks%20and%20recurrent%20models%2C%20vanishing%20or%20exploding%20gradients%20occur%20when%20signal%20magnitudes%20change%20exponentially%20across%20layers.%20Initializing%20weight%20matrices%20%24%5Cmathbf%7BW%7D%24%20as%20random%20orthogonal%20matrices%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BW%7D%5ET%20%5Cmathbf%7BW%7D%20%3D%20%5Cmathbf%7BI%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20ensures%20that%20every%20singular%20value%20equals%201%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Csigma_i(%5Cmathbf%7BW%7D)%20%3D%201%2C%20%5Cquad%20%5Cforall%20i%0A%20%20%20%20%24%24%0A%0A%20%20%20%20This%20guarantees%20that%20the%20Euclidean%20norm%20of%20forward%20activations%20and%20backward%20gradient%20vectors%20is%20preserved%20layer%20by%20layer%20at%20initialization%20(Saxe%20et%20al.%2C%202013).%0A%0A%20%20%20%20---%0A%0A%20%20%20%20%23%23%23%20Numerical%20Stability%20and%20QR%20Factorization%0A%0A%20%20%20%20In%20ordinary%20least%20squares%2C%20solving%20normal%20equations%20directly%20via%20%24(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%5E%7B-1%7D%20%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7By%7D%24%20squares%20the%20condition%20number%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Ckappa(%5Cmathbf%7BX%7D%5ET%20%5Cmathbf%7BX%7D)%20%3D%20(%5Ckappa(%5Cmathbf%7BX%7D))%5E2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20By%20using%20QR%20factorization%2C%20%24%5Cmathbf%7BX%7D%20%3D%20%5Cmathbf%7BQ%7D%5Cmathbf%7BR%7D%24%20with%20orthogonal%20%24%5Cmathbf%7BQ%7D%24%20and%20upper%20triangular%20%24%5Cmathbf%7BR%7D%24%2C%20the%20least-squares%20solution%20simplifies%20to%20back-substitution%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BR%7D%5Cmathbf%7Bw%7D%20%3D%20%5Cmathbf%7BQ%7D%5ET%20%5Cmathbf%7By%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Because%20%24%5Ckappa(%5Cmathbf%7BQ%7D)%20%3D%201%24%2C%20the%20condition%20number%20is%20not%20squared%2C%20avoiding%20severe%20loss%20of%20numerical%20precision%20in%20floating-point%20arithmetic.%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---%0A%0A%20%20%20%20%23%23%20%5Bc%5D%20Interactive%20Visualization%3A%20Rotation%20vs%20Reflection%0A%0A%20%20%20%20The%20interactive%20subplots%20below%20demonstrate%20the%20geometric%20effect%20of%202D%20orthogonal%20transformations%3A%0A%20%20%20%20*%20**Left%20Panel**%3A%20Pure%20rotation%20(%24%5Ctheta%20%3D%2060%5E%5Ccirc%24%2C%20%24%5Cdet(%5Cmathbf%7BQ%7D)%20%3D%20%2B1%24)%2C%20preserving%20basis%20orientation.%0A%20%20%20%20*%20**Right%20Panel**%3A%20Reflection%20across%20the%20%2430%5E%5Ccirc%24%20mirror%20line%20(%24%5Cdet(%5Cmathbf%7BQ%7D)%20%3D%20-1%24)%2C%20flipping%20orientation.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(go%2C%20make_subplots%2C%20np)%3A%0A%20%20%20%20%23%20Angles%20for%202D%20transformation%20comparison%0A%20%20%20%20rot_angle_deg%20%3D%2060.0%0A%20%20%20%20rot_angle_rad%20%3D%20np.radians(rot_angle_deg)%0A%20%20%20%20cos_r%20%3D%20np.cos(rot_angle_rad)%0A%20%20%20%20sin_r%20%3D%20np.sin(rot_angle_rad)%0A%20%20%20%20q_rot%20%3D%20np.array(%5B%5Bcos_r%2C%20-sin_r%5D%2C%20%5Bsin_r%2C%20cos_r%5D%5D)%0A%0A%20%20%20%20ref_angle_deg%20%3D%2030.0%0A%20%20%20%20ref_angle_rad%20%3D%20np.radians(ref_angle_deg)%0A%20%20%20%20cos_2f%20%3D%20np.cos(2%20*%20ref_angle_rad)%0A%20%20%20%20sin_2f%20%3D%20np.sin(2%20*%20ref_angle_rad)%0A%20%20%20%20q_ref%20%3D%20np.array(%5B%5Bcos_2f%2C%20sin_2f%5D%2C%20%5Bsin_2f%2C%20-cos_2f%5D%5D)%0A%0A%20%20%20%20%23%20Unit%20circle%20points%0A%20%20%20%20t%20%3D%20np.linspace(0%2C%202%20*%20np.pi%2C%20200)%0A%20%20%20%20circle_pts%20%3D%20np.vstack(%5Bnp.cos(t)%2C%20np.sin(t)%5D)%0A%0A%20%20%20%20%23%20Sample%20basis%20and%20test%20vectors%0A%20%20%20%20e1%20%3D%20np.array(%5B1.0%2C%200.0%5D)%0A%20%20%20%20e2%20%3D%20np.array(%5B0.0%2C%201.0%5D)%0A%20%20%20%20v_test%20%3D%20np.array(%5B1.2%2C%200.7%5D)%0A%0A%20%20%20%20rot_circle%20%3D%20q_rot%20%40%20circle_pts%0A%20%20%20%20rot_e1%20%3D%20q_rot%20%40%20e1%0A%20%20%20%20rot_e2%20%3D%20q_rot%20%40%20e2%0A%20%20%20%20rot_v%20%3D%20q_rot%20%40%20v_test%0A%0A%20%20%20%20ref_circle%20%3D%20q_ref%20%40%20circle_pts%0A%20%20%20%20ref_e1%20%3D%20q_ref%20%40%20e1%0A%20%20%20%20ref_e2%20%3D%20q_ref%20%40%20e2%0A%20%20%20%20ref_v%20%3D%20q_ref%20%40%20v_test%0A%0A%20%20%20%20det_rot%20%3D%20np.linalg.det(q_rot)%0A%20%20%20%20det_ref%20%3D%20np.linalg.det(q_ref)%0A%0A%20%20%20%20fig%20%3D%20make_subplots(%0A%20%20%20%20%20%20%20%20rows%3D1%2C%0A%20%20%20%20%20%20%20%20cols%3D2%2C%0A%20%20%20%20%20%20%20%20subplot_titles%3D%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Rotation%20(%7Brot_angle_deg%3A.0f%7D%C2%B0)%2C%20det(Q)%20%3D%20%7Bdet_rot%3A%2B.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22Reflection%20(across%20%7Bref_angle_deg%3A.0f%7D%C2%B0%20mirror%20line)%2C%20det(Q)%20%3D%20%7Bdet_ref%3A%2B.1f%7D%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Rotation%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dcircle_pts%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dcircle_pts%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23cbd5e1%22%2C%20dash%3D%22dash%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Unit%20Circle%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Drot_circle%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Drot_circle%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%233b82f6%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Rotated%20Circle%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20e1%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20e1%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2394a3b8%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Original%20e1%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20rot_e1%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20rot_e1%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%232563eb%22%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Rotated%20e1%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20rot_e2%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20rot_e2%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2306b6d4%22%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Rotated%20e2%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20v_test%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20v_test%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23d97706%22%2C%20dash%3D%22dot%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Original%20v%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20rot_v%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20rot_v%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ea580c%22%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Rotated%20v%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Right%3A%20Reflection%0A%20%20%20%20line_x%20%3D%20np.array(%5B-1.8%2C%201.8%5D)%0A%20%20%20%20line_y%20%3D%20line_x%20*%20np.tan(ref_angle_rad)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dline_x%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dline_y%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2394a3b8%22%2C%20dash%3D%22dashdot%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Mirror%20Line%20(%7Bref_angle_deg%3A.0f%7D%C2%B0)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dcircle_pts%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dcircle_pts%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23cbd5e1%22%2C%20dash%3D%22dash%22%2C%20width%3D1.5)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Dref_circle%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dref_circle%5B1%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ec4899%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Reflected%20Circle%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DTrue%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20e1%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20e1%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%2394a3b8%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20ref_e1%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20ref_e1%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23be185d%22%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Reflected%20e1%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20ref_e2%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20ref_e2%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%238b5cf6%22%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Reflected%20e2%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20v_test%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20v_test%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23d97706%22%2C%20dash%3D%22dot%22%2C%20width%3D2)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D6)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20showlegend%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Scatter(%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3D%5B0%2C%20ref_v%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%2C%20ref_v%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22lines%2Bmarkers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%239333ea%22%2C%20width%3D3)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(size%3D8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Reflected%20v%22%2C%0A%20%20%20%20%20%20%20%20)%2C%0A%20%20%20%20%20%20%20%20row%3D1%2C%0A%20%20%20%20%20%20%20%20col%3D2%2C%0A%20%20%20%20)%0A%0A%20%20%20%20axis_config%20%3D%20dict(%0A%20%20%20%20%20%20%20%20range%3D%5B-1.8%2C%201.8%5D%2C%0A%20%20%20%20%20%20%20%20zeroline%3DTrue%2C%0A%20%20%20%20%20%20%20%20zerolinecolor%3D%22%23e2e8f0%22%2C%0A%20%20%20%20%20%20%20%20gridcolor%3D%22%23f1f5f9%22%2C%0A%20%20%20%20%20%20%20%20scaleanchor%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20scaleratio%3D1%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20height%3D520%2C%0A%20%20%20%20%20%20%20%20margin%3Ddict(l%3D40%2C%20r%3D40%2C%20t%3D60%2C%20b%3D40)%2C%0A%20%20%20%20%20%20%20%20xaxis%3Daxis_config%2C%0A%20%20%20%20%20%20%20%20yaxis%3Daxis_config%2C%0A%20%20%20%20%20%20%20%20xaxis2%3Daxis_config%2C%0A%20%20%20%20%20%20%20%20yaxis2%3Daxis_config%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.25%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%20%20%20%20return%20(fig%2C)%0A%0A%0A%40app.cell%0Adef%20_(fig%2C%20mo)%3A%0A%20%20%20%20mo.ui.plotly(fig)%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---%0A%0A%20%20%20%20%23%23%20%5Bd%5D%20Code%20Examples%0A%0A%20%20%20%20%23%23%23%20Example%201%3A%20Numerical%20Verification%20of%20Orthogonality%20and%20Isometry%20Properties%0A%0A%20%20%20%20Below%2C%20we%20generate%20a%20random%20%244%20%5Ctimes%204%24%20orthogonal%20matrix%20using%20QR%20decomposition%20and%20verify%3A%0A%20%20%20%201.%20%24%5Cmathbf%7BQ%7D%5ET%20%5Cmathbf%7BQ%7D%20%3D%20%5Cmathbf%7BI%7D%24%0A%20%20%20%202.%20Length%20preservation%3A%20%24%5C%7C%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%5C%7C_2%20%3D%20%5C%7C%5Cmathbf%7Bx%7D%5C%7C_2%24%0A%20%20%20%203.%20Inner%20product%20preservation%3A%20%24%5Clangle%20%5Cmathbf%7BQ%7D%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7BQ%7D%5Cmathbf%7By%7D%5Crangle%20%3D%20%5Clangle%20%5Cmathbf%7Bx%7D%2C%20%5Cmathbf%7By%7D%5Crangle%24%0A%20%20%20%204.%20Eigenvalue%20magnitudes%3A%20%24%7C%5Clambda_i%7C%20%3D%201%24%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%20%23%20Seed%20for%20reproducibility%0A%20%20%20%20rng%20%3D%20np.random.default_rng(42)%0A%0A%20%20%20%20%23%20Generate%20a%20random%204x4%20matrix%20and%20obtain%20an%20orthogonal%20matrix%20via%20QR%0A%20%20%20%20a_mat%20%3D%20rng.standard_normal((4%2C%204))%0A%20%20%20%20q_mat%2C%20_%20%3D%20np.linalg.qr(a_mat)%0A%0A%20%20%20%20%23%201.%20Identity%20check%0A%20%20%20%20qt_q%20%3D%20q_mat.T%20%40%20q_mat%0A%20%20%20%20identity_error%20%3D%20float(np.max(np.abs(qt_q%20-%20np.eye(4))))%0A%0A%20%20%20%20%23%202.%20Vector%20length%20preservation%0A%20%20%20%20x_vec%20%3D%20rng.standard_normal(4)%0A%20%20%20%20qx_vec%20%3D%20q_mat%20%40%20x_vec%0A%20%20%20%20norm_x%20%3D%20float(np.linalg.norm(x_vec))%0A%20%20%20%20norm_qx%20%3D%20float(np.linalg.norm(qx_vec))%0A%20%20%20%20norm_diff%20%3D%20abs(norm_x%20-%20norm_qx)%0A%0A%20%20%20%20%23%203.%20Inner%20product%20preservation%0A%20%20%20%20y_vec%20%3D%20rng.standard_normal(4)%0A%20%20%20%20qy_vec%20%3D%20q_mat%20%40%20y_vec%0A%20%20%20%20dot_xy%20%3D%20float(np.dot(x_vec%2C%20y_vec))%0A%20%20%20%20dot_q%20%3D%20float(np.dot(qx_vec%2C%20qy_vec))%0A%20%20%20%20dot_diff%20%3D%20abs(dot_xy%20-%20dot_q)%0A%0A%20%20%20%20%23%204.%20Eigenvalue%20magnitudes%20on%20the%20unit%20circle%0A%20%20%20%20eigenvalues%20%3D%20np.linalg.eigvals(q_mat)%0A%20%20%20%20eigen_magnitudes%20%3D%20np.abs(eigenvalues)%0A%0A%20%20%20%20results_table%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Property%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Identity%20check%3A%20max%20%7CQ%5ET%20Q%20-%20I%7C%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Original%20norm%20%7C%7Cx%7C%7C_2%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Transformed%20norm%20%7C%7CQ%20x%7C%7C_2%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Norm%20difference%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Original%20dot%20product%20%3Cx%2C%20y%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Transformed%20dot%20product%20%3CQx%2C%20Qy%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Dot%20product%20difference%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Determinant%20det(Q)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22All%20eigenvalue%20magnitudes%20%7Clambda%7C%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22Value%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bidentity_error%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bnorm_x%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bnorm_qx%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bnorm_diff%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bdot_xy%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bdot_q%3A.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bdot_diff%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bnp.linalg.det(q_mat)%3A%2B.6f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22%2C%20%22.join(f%22%7Bm%3A.4f%7D%22%20for%20m%20in%20eigen_magnitudes)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20(results_table%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20pd%2C%20results_table)%3A%0A%20%20%20%20df_results%20%3D%20pd.DataFrame(results_table)%0A%20%20%20%20mo.ui.table(df_results)%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---%0A%0A%20%20%20%20%23%23%23%20Example%202%3A%202D%20Rotary%20Position%20Embedding%20(RoPE)%20Invariance%0A%0A%20%20%20%20We%20implement%20a%20minimal%202D%20RoPE%20rotation%20block.%20We%20demonstrate%20that%20when%20query%20%24%5Cmathbf%7Bq%7D%24%20is%20at%20position%20%24m%24%20and%20key%20%24%5Cmathbf%7Bk%7D%24%20is%20at%20position%20%24n%24%2C%20the%20attention%20inner%20product%20%24%5Clangle%20%5Cmathbf%7BR%7D_m%20%5Cmathbf%7Bq%7D%2C%20%5Cmathbf%7BR%7D_n%20%5Cmathbf%7Bk%7D%20%5Crangle%24%20depends%20solely%20on%20the%20relative%20offset%20%24(n%20-%20m)%24%2C%20regardless%20of%20absolute%20positions.%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%20def%20get_rope_rotation(pos%3A%20int%2C%20base_theta%3A%20float%20%3D%200.05)%20-%3E%20np.ndarray%3A%0A%20%20%20%20%20%20%20%20angle%20%3D%20pos%20*%20base_theta%0A%20%20%20%20%20%20%20%20cos_a%20%3D%20np.cos(angle)%0A%20%20%20%20%20%20%20%20sin_a%20%3D%20np.sin(angle)%0A%20%20%20%20%20%20%20%20return%20np.array(%5B%5Bcos_a%2C%20-sin_a%5D%2C%20%5Bsin_a%2C%20cos_a%5D%5D)%0A%0A%20%20%20%20%23%20Query%20and%20key%20vectors%0A%20%20%20%20q_vec%20%3D%20np.array(%5B1.5%2C%20-0.8%5D)%0A%20%20%20%20k_vec%20%3D%20np.array(%5B-0.4%2C%202.1%5D)%0A%0A%20%20%20%20%23%20Case%20A%3A%20Positions%20m%3D15%2C%20n%3D10%20(relative%20distance%20%3D%20-5)%0A%20%20%20%20r_m1%20%3D%20get_rope_rotation(pos%3D15)%0A%20%20%20%20r_n1%20%3D%20get_rope_rotation(pos%3D10)%0A%20%20%20%20q_rot1%20%3D%20r_m1%20%40%20q_vec%0A%20%20%20%20k_rot1%20%3D%20r_n1%20%40%20k_vec%0A%20%20%20%20dot_case_a%20%3D%20float(np.dot(q_rot1%2C%20k_rot1))%0A%0A%20%20%20%20%23%20Case%20B%3A%20Positions%20m%3D42%2C%20n%3D37%20(same%20relative%20distance%20%3D%20-5)%0A%20%20%20%20r_m2%20%3D%20get_rope_rotation(pos%3D42)%0A%20%20%20%20r_n2%20%3D%20get_rope_rotation(pos%3D37)%0A%20%20%20%20q_rot2%20%3D%20r_m2%20%40%20q_vec%0A%20%20%20%20k_rot2%20%3D%20r_n2%20%40%20k_vec%0A%20%20%20%20dot_case_b%20%3D%20float(np.dot(q_rot2%2C%20k_rot2))%0A%0A%20%20%20%20%23%20Direct%20relative%20formula%3A%20q%5ET%20R(n%20-%20m)%20k%0A%20%20%20%20r_rel%20%3D%20get_rope_rotation(pos%3D(10%20-%2015))%0A%20%20%20%20dot_relative_formula%20%3D%20float(q_vec%20%40%20r_rel%20%40%20k_vec)%0A%0A%20%20%20%20rope_summary%20%3D%20%7B%0A%20%20%20%20%20%20%20%20%22Configuration%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Case%20A%20(m%3D15%2C%20n%3D10%2C%20diff%3D-5)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Case%20B%20(m%3D42%2C%20n%3D37%2C%20diff%3D-5)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Relative%20formulation%20q%5ET%20R(-5)%20k%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Absolute%20discrepancy%20%7CCase%20A%20-%20Case%20B%7C%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Relative%20formula%20discrepancy%20%7CCase%20A%20-%20Formula%7C%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%22Attention%20Dot%20Product%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bdot_case_a%3A.8f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bdot_case_b%3A.8f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Bdot_relative_formula%3A.8f%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Babs(dot_case_a%20-%20dot_case_b)%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%7Babs(dot_case_a%20-%20dot_relative_formula)%3A.2e%7D%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%7D%0A%20%20%20%20return%20(rope_summary%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo%2C%20pd%2C%20rope_summary)%3A%0A%20%20%20%20df_rope%20%3D%20pd.DataFrame(rope_summary)%0A%20%20%20%20mo.ui.table(df_rope)%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
84022cf41febc4ef54076146f115350a