import%20marimo%0A%0A__generated_with%20%3D%20%220.24.0%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%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%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%5B%E2%86%90%2042%20Multiclass%20Classification%5D(42_multiclass_classification.py)%20%7C%20%5BIndex%5D(..%2Findex.html)%20%7C%20%5B44%20Logistic%20Regression%20%E2%86%92%5D(44_logistic_regression.py)%0A%0A%20%20%20%20%23%20Matrix%20Energy%20and%20Quadratic%20Forms%3A%20Curvature%2C%20Definiteness%2C%20and%20Optimization%20Landscapes%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%20In%20numerical%20optimization%2C%20machine%20learning%20loss%20landscapes%2C%20physics%2C%20and%20statistical%20estimation%2C%20multi-dimensional%20functions%20%24f(%5Ctheta)%24%20are%20analyzed%20through%20second-order%20Taylor%20series%20approximations%20around%20a%20stationary%20point%20%24%5Ctheta%5E*%24%3A%0A%0A%20%20%20%20%24%24f(%5Ctheta%5E*%20%2B%20%5CDelta%20%5Ctheta)%20%5Capprox%20f(%5Ctheta%5E*)%20%2B%20%5Cnabla%20f(%5Ctheta%5E*)%5E%5Ctop%20%5CDelta%20%5Ctheta%20%2B%20%5Cfrac%7B1%7D%7B2%7D%20%5CDelta%20%5Ctheta%5E%5Ctop%20H(%5Ctheta%5E*)%20%5CDelta%20%5Ctheta%24%24%0A%0A%20%20%20%20At%20stationary%20points%20where%20the%20gradient%20vanishes%20(%24%5Cnabla%20f(%5Ctheta%5E*)%20%3D%200%24)%2C%20the%20local%20geometry%20and%20whether%20%24%5Ctheta%5E*%24%20is%20a%20local%20minimum%2C%20local%20maximum%2C%20or%20unstable%20saddle%20point%20is%20**governed%20entirely%20by%20the%20quadratic%20form%20(energy)**%20induced%20by%20the%20Hessian%20matrix%20%24H(%5Ctheta%5E*)%24.%0A%0A%20%20%20%20%23%23%23%23%20The%20Physical%20Intuition%20of%20%22Energy%22%0A%20%20%20%20The%20term%20%22energy%22%20originates%20in%20classical%20mechanics%20and%20continuum%20physics.%20The%20potential%20energy%20stored%20in%20an%20elastic%20system%20under%20displacement%20%24x%24%20is%20given%20by%20the%20quadratic%20form%20%24E%20%3D%20%5Cfrac%7B1%7D%7B2%7D%20x%5E%5Ctop%20K%20x%24%2C%20where%20%24K%24%20is%20the%20structural%20stiffness%20matrix.%20For%20a%20physical%20structure%20to%20remain%20stable%20under%20any%20perturbation%2C%20any%20non-zero%20displacement%20%24x%20%5Cneq%200%24%20must%20require%20positive%20work%20(%24E%20%3E%200%24).%20If%20%24x%5E%5Ctop%20K%20x%20%3C%200%24%20along%20any%20direction%2C%20the%20system%20releases%20energy%20upon%20displacement%2C%20resulting%20in%20structural%20collapse.%0A%0A%20%20%20%20%23%23%23%23%20Role%20in%20Deep%20Learning%20and%20Convex%20Optimization%0A%20%20%20%20-%20**Positive%20Definite%20Hessian%20(%24H%20%5Csucc%200%24)**%3A%20The%20loss%20landscape%20forms%20a%20strictly%20convex%20multidimensional%20bowl.%20Gradient%20descent%2C%20Newton-Raphson%2C%20and%20quasi-Newton%20(BFGS)%20algorithms%20exhibit%20rapid%2C%20guaranteed%20convergence%20toward%20the%20unique%20local%20minimum.%0A%20%20%20%20-%20**Indefinite%20Hessian%20(Mixed%20Eigenvalues)**%3A%20The%20landscape%20forms%20a%20hyperbolic%20saddle.%20Along%20positive%20eigenvector%20directions%2C%20the%20loss%20rises%3B%20along%20negative%20eigenvector%20directions%2C%20the%20loss%20falls.%20First-order%20optimizers%20can%20stall%20near%20saddle%20points%20where%20gradient%20norms%20become%20infinitesimally%20small%20despite%20high%20residual%20error.%0A%20%20%20%20-%20**Positive%20Semi-Definite%20Hessian%20(%24H%20%5Csucceq%200%24)**%3A%20Occurs%20in%20overparameterized%20neural%20networks%20where%20entire%20manifolds%20of%20parameter%20weights%20produce%20identical%20zero-loss%20predictions%20(flat%20valleys%20or%20troughs).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20%5Bb%5D%20Mathematical%20Foundations%20and%20Spectral%20Mechanics%0A%0A%20%20%20%20%23%23%23%201.%20Quadratic%20Forms%20and%20Symmetrization%0A%0A%20%20%20%20For%20an%20arbitrary%20square%20matrix%20%24M%20%5Cin%20%5Cmathbb%7BR%7D%5E%7Bn%20%5Ctimes%20n%7D%24%20and%20a%20vector%20%24x%20%5Cin%20%5Cmathbb%7BR%7D%5En%24%2C%20the%20quadratic%20form%20is%3A%0A%0A%20%20%20%20%24%24Q(x)%20%3D%20x%5E%5Ctop%20M%20x%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20%5Csum_%7Bj%3D1%7D%5En%20M_%7Bij%7D%20x_i%20x_j%24%24%0A%0A%20%20%20%20Any%20square%20matrix%20decomposes%20uniquely%20into%20symmetric%20and%20skew-symmetric%20parts%3A%0A%0A%20%20%20%20%24%24M%20%3D%20%5Cfrac%7BM%20%2B%20M%5E%5Ctop%7D%7B2%7D%20%2B%20%5Cfrac%7BM%20-%20M%5E%5Ctop%7D%7B2%7D%20%3D%20A%20%2B%20S%24%24%0A%0A%20%20%20%20Because%20%24x%5E%5Ctop%20S%20x%20%5Cequiv%200%24%20for%20any%20skew-symmetric%20matrix%20%24S%20%3D%20-S%5E%5Ctop%24%2C%20the%20skew-symmetric%20component%20contributes%20nothing%20to%20the%20quadratic%20form%3A%0A%0A%20%20%20%20%24%24x%5E%5Ctop%20M%20x%20%3D%20x%5E%5Ctop%20A%20x%2C%20%5Cquad%20%5Ctext%7Bwhere%20%7D%20A%20%3D%20%5Cfrac%7BM%20%2B%20M%5E%5Ctop%7D%7B2%7D%24%24%0A%0A%20%20%20%20Consequently%2C%20in%20the%20study%20of%20quadratic%20forms%20and%20energy%20landscapes%2C%20**%24A%24%20is%20always%20assumed%20to%20be%20symmetric%20(%24A%20%3D%20A%5E%5Ctop%24)%20without%20loss%20of%20generality**.%0A%0A%20%20%20%20%23%23%23%202.%20Spectral%20Decoupling%20and%20Principal%20Curvatures%0A%0A%20%20%20%20By%20the%20Spectral%20Theorem%20for%20symmetric%20matrices%2C%20there%20exists%20an%20orthogonal%20matrix%20%24Q%20%3D%20%5Bq_1%2C%20%5Cdots%2C%20q_n%5D%24%20of%20eigenvectors%20and%20a%20real%20diagonal%20matrix%20%24%5CLambda%20%3D%20%5Coperatorname%7Bdiag%7D(%5Clambda_1%2C%20%5Cdots%2C%20%5Clambda_n)%24%20of%20eigenvalues%20such%20that%3A%0A%0A%20%20%20%20%24%24A%20%3D%20Q%20%5CLambda%20Q%5E%5Ctop%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20%5Clambda_i%20q_i%20q_i%5E%5Ctop%24%24%0A%0A%20%20%20%20Applying%20the%20orthogonal%20change%20of%20basis%20%24y%20%3D%20Q%5E%5Ctop%20x%24%20(so%20%24x%20%3D%20Q%20y%24)%3A%0A%0A%20%20%20%20%24%24Q(x)%20%3D%20x%5E%5Ctop%20(Q%20%5CLambda%20Q%5E%5Ctop)%20x%20%3D%20(Q%5E%5Ctop%20x)%5E%5Ctop%20%5CLambda%20(Q%5E%5Ctop%20x)%20%3D%20y%5E%5Ctop%20%5CLambda%20y%20%3D%20%5Csum_%7Bi%3D1%7D%5En%20%5Clambda_i%20y_i%5E2%24%24%0A%0A%20%20%20%20The%20spectral%20transformation%20decouples%20the%20coupled%20quadratic%20cross-terms%20into%20an%20independent%20sum%20of%20squares.%20The%20eigenvalues%20%24%5Clambda_i%24%20represent%20the%20**principal%20curvatures**%20along%20the%20orthogonal%20principal%20axes%20%24q_i%24.%0A%0A%20%20%20%20%23%23%23%203.%20Classification%20of%20Matrix%20Definiteness%0A%0A%20%20%20%20The%20sign%20of%20the%20energy%20%24Q(x)%20%3D%20x%5E%5Ctop%20A%20x%24%20for%20non-zero%20vectors%20%24x%20%5Cneq%200%24%20defines%20the%20definiteness%20of%20%24A%24%3A%0A%0A%20%20%20%201.%20**Positive%20Definite%20(%24A%20%5Csucc%200%24)**%3A%0A%0A%20%20%20%20%24%24x%5E%5Ctop%20A%20x%20%3E%200%20%5Cquad%20%5Cforall%20x%20%5Cneq%200%20%5Ciff%20%5Clambda_i%20%3E%200%20%5Cquad%20%5Cforall%20i%20%5Cin%20%5C%7B1%2C%20%5Cdots%2C%20n%5C%7D%24%24%0A%0A%20%20%20%20*Geometry*%3A%20Strictly%20convex%20elliptic%20bowl%20opening%20upwards.%20Unique%20global%20minimum%20at%20%24x%20%3D%200%24.%0A%0A%20%20%20%202.%20**Negative%20Definite%20(%24A%20%5Cprec%200%24)**%3A%0A%0A%20%20%20%20%24%24x%5E%5Ctop%20A%20x%20%3C%200%20%5Cquad%20%5Cforall%20x%20%5Cneq%200%20%5Ciff%20%5Clambda_i%20%3C%200%20%5Cquad%20%5Cforall%20i%20%5Cin%20%5C%7B1%2C%20%5Cdots%2C%20n%5C%7D%24%24%0A%0A%20%20%20%20*Geometry*%3A%20Strictly%20concave%20dome%20opening%20downwards.%20Unique%20global%20maximum%20at%20%24x%20%3D%200%24.%0A%0A%20%20%20%203.%20**Positive%20Semi-Definite%20(%24A%20%5Csucceq%200%24)**%3A%0A%0A%20%20%20%20%24%24x%5E%5Ctop%20A%20x%20%5Cge%200%20%5Cquad%20%5Cforall%20x%20%5Ciff%20%5Clambda_i%20%5Cge%200%20%5Cquad%20%5Cforall%20i%20%5Cin%20%5C%7B1%2C%20%5Cdots%2C%20n%5C%7D%24%24%0A%0A%20%20%20%20*Geometry*%3A%20Convex%20parabolic%20trough.%20Flat%20zero-energy%20valley%20along%20the%20null%20space%20of%20%24A%24.%0A%0A%20%20%20%204.%20**Negative%20Semi-Definite%20(%24A%20%5Cpreceq%200%24)**%3A%0A%0A%20%20%20%20%24%24x%5E%5Ctop%20A%20x%20%5Cle%200%20%5Cquad%20%5Cforall%20x%20%5Ciff%20%5Clambda_i%20%5Cle%200%20%5Cquad%20%5Cforall%20i%20%5Cin%20%5C%7B1%2C%20%5Cdots%2C%20n%5C%7D%24%24%0A%0A%20%20%20%20*Geometry*%3A%20Concave%20trough%20with%20flat%20zero-energy%20directions.%0A%0A%20%20%20%205.%20**Indefinite**%3A%0A%0A%20%20%20%20%24%24x%5E%5Ctop%20A%20x%20%5Ctext%7B%20assumes%20both%20positive%20and%20negative%20values%7D%20%5Ciff%20%5Cexists%20%5Clambda_j%20%3E%200%20%5Ctext%7B%20and%20%7D%20%5Clambda_k%20%3C%200%24%24%0A%0A%20%20%20%20*Geometry*%3A%20Hyperbolic%20saddle%20surface%20(pringle).%20The%20origin%20%24x%20%3D%200%24%20is%20a%20saddle%20point.%0A%0A%20%20%20%20%23%23%23%204.%20The%20Rayleigh%20Quotient%20and%20Extreme%20Curvatures%0A%0A%20%20%20%20For%20any%20non-zero%20vector%20%24x%20%5Cin%20%5Cmathbb%7BR%7D%5En%24%2C%20the%20**Rayleigh%20Quotient**%20is%20defined%20as%3A%0A%0A%20%20%20%20%24%24R(A%2C%20x)%20%3D%20%5Cfrac%7Bx%5E%5Ctop%20A%20x%7D%7Bx%5E%5Ctop%20x%7D%24%24%0A%0A%20%20%20%20By%20the%20Courant-Fischer%20Minimax%20Theorem%2C%20the%20Rayleigh%20quotient%20is%20bounded%20strictly%20between%20the%20minimal%20and%20maximal%20eigenvalues%20of%20%24A%24%3A%0A%0A%20%20%20%20%24%24%5Clambda_%7B%5Cmin%7D(A)%20%5Cle%20%5Cfrac%7Bx%5E%5Ctop%20A%20x%7D%7Bx%5E%5Ctop%20x%7D%20%5Cle%20%5Clambda_%7B%5Cmax%7D(A)%24%24%0A%0A%20%20%20%20-%20The%20minimum%20is%20attained%20when%20%24x%24%20aligns%20with%20the%20eigenvector%20%24q_%7B%5Cmin%7D%24%20corresponding%20to%20%24%5Clambda_%7B%5Cmin%7D%24.%0A%20%20%20%20-%20The%20maximum%20is%20attained%20when%20%24x%24%20aligns%20with%20the%20eigenvector%20%24q_%7B%5Cmax%7D%24%20corresponding%20to%20%24%5Clambda_%7B%5Cmax%7D%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%20Create%20coordinate%20grid%20for%202D%20quadratic%20form%20visualizations%0A%20%20%20%20grid_lim%20%3D%202.5%0A%20%20%20%20n_pts%20%3D%2060%0A%20%20%20%20u_vals%20%3D%20np.linspace(-grid_lim%2C%20grid_lim%2C%20n_pts)%0A%20%20%20%20v_vals%20%3D%20np.linspace(-grid_lim%2C%20grid_lim%2C%20n_pts)%0A%20%20%20%20uu%2C%20vv%20%3D%20np.meshgrid(u_vals%2C%20v_vals)%0A%20%20%20%20pts_2d%20%3D%20np.column_stack(%5Buu.ravel()%2C%20vv.ravel()%5D)%0A%0A%20%20%20%20%23%20Matrix%20Archetypes%0A%20%20%20%20%23%201.%20Positive%20Definite%3A%20Strictly%20convex%20bowl%0A%20%20%20%20a_pos_def%20%3D%20np.array(%5B%5B3.0%2C%200.8%5D%2C%20%5B0.8%2C%202.0%5D%5D)%0A%20%20%20%20evals_pd%2C%20evecs_pd%20%3D%20np.linalg.eigh(a_pos_def)%0A%0A%20%20%20%20%23%202.%20Indefinite%3A%20Hyperbolic%20saddle%0A%20%20%20%20a_indef%20%3D%20np.array(%5B%5B2.5%2C%200.5%5D%2C%20%5B0.5%2C%20-2.0%5D%5D)%0A%20%20%20%20evals_indef%2C%20evecs_indef%20%3D%20np.linalg.eigh(a_indef)%0A%0A%20%20%20%20%23%203.%20Positive%20Semi-Definite%3A%20Valley%20trough%20with%20zero%20eigenvalue%0A%20%20%20%20a_semi_def%20%3D%20np.array(%5B%5B2.0%2C%201.0%5D%2C%20%5B1.0%2C%200.5%5D%5D)%0A%20%20%20%20evals_psd%2C%20evecs_psd%20%3D%20np.linalg.eigh(a_semi_def)%0A%0A%20%20%20%20%23%20Compute%20energy%20surfaces%3A%20Z%20%3D%20%5Bx1%2C%20x2%5D%20A%20%5Bx1%2C%20x2%5D%5ET%0A%20%20%20%20def%20eval_energy(mat)%3A%0A%20%20%20%20%20%20%20%20%23%20Vectorized%20quadratic%20form%3A%20sum_j%20sum_k%20X_j%20A_jk%20X_k%0A%20%20%20%20%20%20%20%20return%20np.sum(pts_2d%20%40%20mat%20*%20pts_2d%2C%20axis%3D1).reshape(uu.shape)%0A%0A%20%20%20%20z_pd%20%3D%20eval_energy(a_pos_def)%0A%20%20%20%20z_indef%20%3D%20eval_energy(a_indef)%0A%20%20%20%20z_psd%20%3D%20eval_energy(a_semi_def)%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20a_indef%2C%0A%20%20%20%20%20%20%20%20a_pos_def%2C%0A%20%20%20%20%20%20%20%20a_semi_def%2C%0A%20%20%20%20%20%20%20%20evals_indef%2C%0A%20%20%20%20%20%20%20%20evals_pd%2C%0A%20%20%20%20%20%20%20%20u_vals%2C%0A%20%20%20%20%20%20%20%20v_vals%2C%0A%20%20%20%20%20%20%20%20z_indef%2C%0A%20%20%20%20%20%20%20%20z_pd%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20evals_indef%2C%0A%20%20%20%20evals_pd%2C%0A%20%20%20%20go%2C%0A%20%20%20%20make_subplots%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20np%2C%0A%20%20%20%20u_vals%2C%0A%20%20%20%20v_vals%2C%0A%20%20%20%20z_indef%2C%0A%20%20%20%20z_pd%2C%0A)%3A%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%22%3Cb%3EPositive%20Definite%20Landscape%20(lambda%20%3D%20%7Bevals_pd%5B0%5D%3A.2f%7D%2C%20%7Bevals_pd%5B1%5D%3A.2f%7D)%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20f%22%3Cb%3EIndefinite%20Saddle%20Landscape%20(lambda%20%3D%20%7Bevals_indef%5B0%5D%3A.2f%7D%2C%20%7Bevals_indef%5B1%5D%3A.2f%7D)%3C%2Fb%3E%22%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20horizontal_spacing%3D0.14%2C%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Left%3A%20Contour%20of%20Positive%20Definite%20(Ellipses)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Contour(%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3Dz_pd%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Du_vals%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dv_vals%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%22Teal%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20contours%3Ddict(start%3D0.5%2C%20end%3D25%2C%20size%3D2.5%2C%20showlabels%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorbar%3Ddict(title%3D%22Energy%20Q(x)%22%2C%20x%3D0.44%2C%20len%3D0.8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Positive%20Definite%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%20Mark%20global%20minimum%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%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%23DC2626%22%2C%20size%3D10%2C%20symbol%3D%22x%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Global%20Minimum%20(x%3D0)%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%20Contour%20of%20Indefinite%20Matrix%20(Hyperbolas%20%26%20Zero%20Crossing%20Lines)%0A%20%20%20%20fig.add_trace(%0A%20%20%20%20%20%20%20%20go.Contour(%0A%20%20%20%20%20%20%20%20%20%20%20%20z%3Dz_indef%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20x%3Du_vals%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dv_vals%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorscale%3D%22RdBu_r%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20contours%3Ddict(start%3D-15%2C%20end%3D15%2C%20size%3D3.0%2C%20showlabels%3DTrue)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20colorbar%3Ddict(title%3D%22Energy%20Q(x)%22%2C%20x%3D1.02%2C%20len%3D0.8)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Indefinite%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%20%23%20Mark%20saddle%20point%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%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5B0%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%23F59E0B%22%2C%20size%3D10%2C%20symbol%3D%22diamond%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Saddle%20Point%20(x%3D0)%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%20%23%20Highlight%20zero-energy%20asymptotic%20directions%20on%20saddle%20plot%0A%20%20%20%20x_asymp%20%3D%20np.linspace(-2.5%2C%202.5%2C%2050)%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%3Dx_asymp%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx_asymp%20*%200.95%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%23111827%22%2C%20width%3D1.5%2C%20dash%3D%22dash%22)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Zero%20Energy%20Boundary%20Q(x)%3D0%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%20fig.update_xaxes(title_text%3D%22x_1%20Coordinate%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_yaxes(title_text%3D%22x_2%20Coordinate%22%2C%20row%3D1%2C%20col%3D1)%0A%20%20%20%20fig.update_xaxes(title_text%3D%22x_1%20Coordinate%22%2C%20row%3D1%2C%20col%3D2)%0A%20%20%20%20fig.update_yaxes(title_text%3D%22x_2%20Coordinate%22%2C%20row%3D1%2C%20col%3D2)%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%3D70%2C%20b%3D50)%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(orientation%3D%22h%22%2C%20yanchor%3D%22bottom%22%2C%20y%3D-0.28%2C%20xanchor%3D%22center%22%2C%20x%3D0.5)%2C%0A%20%20%20%20)%0A%0A%20%20%20%20viz%20%3D%20mo.ui.plotly(fig)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(a_indef%2C%20a_pos_def%2C%20a_semi_def%2C%20evals_pd%2C%20mo%2C%20np%2C%20pd)%3A%0A%20%20%20%20%23%20Example%201%3A%20Pure%20NumPy%20Definiteness%20Classification%20Engine%0A%20%20%20%20def%20classify_matrix_definiteness(mat)%3A%0A%20%20%20%20%20%20%20%20%23%20Symmetrize%0A%20%20%20%20%20%20%20%20sym_mat%20%3D%20(mat%20%2B%20mat.T)%20%2F%202.0%0A%20%20%20%20%20%20%20%20eigvals%20%3D%20np.linalg.eigvalsh(sym_mat)%0A%20%20%20%20%20%20%20%20tol%20%3D%201e-10%0A%0A%20%20%20%20%20%20%20%20if%20np.all(eigvals%20%3E%20tol)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20status%20%3D%20%22Positive%20Definite%20(A%20%3E%200)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20geometry%20%3D%20%22Strictly%20convex%20bowl%3B%20unique%20global%20minimum%22%0A%20%20%20%20%20%20%20%20elif%20np.all(eigvals%20%3C%20-tol)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20status%20%3D%20%22Negative%20Definite%20(A%20%3C%200)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20geometry%20%3D%20%22Strictly%20concave%20dome%3B%20unique%20global%20maximum%22%0A%20%20%20%20%20%20%20%20elif%20np.all(eigvals%20%3E%3D%20-tol)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20status%20%3D%20%22Positive%20Semi-Definite%20(A%20%3E%3D%200)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20geometry%20%3D%20%22Convex%20valley%20trough%3B%20flat%20null%20space%20directions%22%0A%20%20%20%20%20%20%20%20elif%20np.all(eigvals%20%3C%3D%20tol)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20status%20%3D%20%22Negative%20Semi-Definite%20(A%20%3C%3D%200)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20geometry%20%3D%20%22Concave%20valley%20ridge%3B%20flat%20null%20space%20directions%22%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20status%20%3D%20%22Indefinite%20(Mixed%20Signs)%22%0A%20%20%20%20%20%20%20%20%20%20%20%20geometry%20%3D%20%22Hyperbolic%20saddle%20point%3B%20positive%20and%20negative%20directions%22%0A%0A%20%20%20%20%20%20%20%20return%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Eigenvalues%22%3A%20np.round(eigvals%2C%204)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Status%22%3A%20status%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Geometry%22%3A%20geometry%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Rayleigh_Range%22%3A%20f%22%5B%7Beigvals.min()%3A.3f%7D%2C%20%7Beigvals.max()%3A.3f%7D%5D%22%2C%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20test_matrices%20%3D%20%5B%0A%20%20%20%20%20%20%20%20(%22A_PosDef%22%2C%20a_pos_def)%2C%0A%20%20%20%20%20%20%20%20(%22A_Indef%22%2C%20a_indef)%2C%0A%20%20%20%20%20%20%20%20(%22A_SemiDef%22%2C%20a_semi_def)%2C%0A%20%20%20%20%20%20%20%20(%22A_NegDef%22%2C%20-a_pos_def)%2C%0A%20%20%20%20%5D%0A%0A%20%20%20%20records%20%3D%20%5B%5D%0A%20%20%20%20for%20name%2C%20m%20in%20test_matrices%3A%0A%20%20%20%20%20%20%20%20res%20%3D%20classify_matrix_definiteness(m)%0A%20%20%20%20%20%20%20%20records.append(%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Matrix_Name%22%3A%20name%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Matrix_Values%22%3A%20str(m.tolist())%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Eigenvalues%22%3A%20str(res%5B%22Eigenvalues%22%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Definiteness_Classification%22%3A%20res%5B%22Status%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Rayleigh_Curvature_Bounds%22%3A%20res%5B%22Rayleigh_Range%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20df_classification%20%3D%20pd.DataFrame(records)%0A%0A%20%20%20%20%23%20Example%202%3A%20Rayleigh%20Quotient%20Numerical%20Extremization%20on%20Unit%20Circle%0A%20%20%20%20angles%20%3D%20np.linspace(0%2C%202%20*%20np.pi%2C%20360)%0A%20%20%20%20circle_pts%20%3D%20np.column_stack(%5Bnp.cos(angles)%2C%20np.sin(angles)%5D)%0A%20%20%20%20%23%20Compute%20R(A%2C%20x)%20for%20a_pos_def%0A%20%20%20%20rayleigh_vals%20%3D%20np.sum(circle_pts%20%40%20a_pos_def%20*%20circle_pts%2C%20axis%3D1)%0A%0A%20%20%20%20df_rayleigh%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Quantity%22%3A%20%22Minimum%20Rayleigh%20Quotient%20min%20R(A%2C%20x)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Numerical_Grid_Search%22%3A%20round(np.min(rayleigh_vals)%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical_Eigenvalue%20(lambda_min)%22%3A%20round(evals_pd%5B0%5D%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Absolute_Difference%22%3A%20round(abs(np.min(rayleigh_vals)%20-%20evals_pd%5B0%5D)%2C%208)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Quantity%22%3A%20%22Maximum%20Rayleigh%20Quotient%20max%20R(A%2C%20x)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Numerical_Grid_Search%22%3A%20round(np.max(rayleigh_vals)%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Theoretical_Eigenvalue%20(lambda_max)%22%3A%20round(evals_pd%5B1%5D%2C%205)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Absolute_Difference%22%3A%20round(abs(np.max(rayleigh_vals)%20-%20evals_pd%5B1%5D)%2C%208)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Example%203%3A%20Optimization%20Landscape%20Diagnostics%0A%20%20%20%20%23%20Test%20function%3A%20Rosenbrock-like%20or%20multi-extrema%20Hessian%20at%20distinct%20points%0A%20%20%20%20df_opt%20%3D%20pd.DataFrame(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Stationary_Point_Type%22%3A%20%22Local%20Minimum%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hessian_Definiteness%22%3A%20%22Positive%20Definite%20(H%20%3E%200)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Newton_Step_Direction%22%3A%20%22-H%5E(-1)%20grad%20is%20valid%20descent%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Optimizer_Behavior%22%3A%20%22Fast%20quadratic%20convergence%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Stationary_Point_Type%22%3A%20%22Saddle%20Point%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hessian_Definiteness%22%3A%20%22Indefinite%20(Mixed%20signs)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Newton_Step_Direction%22%3A%20%22-H%5E(-1)%20grad%20can%20ascend%20to%20saddle%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Optimizer_Behavior%22%3A%20%22Stalls%3B%20requires%20Hessian-free%20escape%20or%20cubic%20regularization%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Stationary_Point_Type%22%3A%20%22Flat%20Valley%20%2F%20Degenerate%20Minimum%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Hessian_Definiteness%22%3A%20%22Positive%20Semi-Definite%20(lambda_min%20%3D%200)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Newton_Step_Direction%22%3A%20%22H%20is%20singular%3B%20inverse%20does%20not%20exist%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22Optimizer_Behavior%22%3A%20%22Requires%20Moore-Penrose%20pseudoinverse%20or%20damping%20(Levenberg-Marquardt)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20table_class%20%3D%20mo.ui.table(df_classification)%0A%20%20%20%20table_ray%20%3D%20mo.ui.table(df_rayleigh)%0A%20%20%20%20table_opt%20%3D%20mo.ui.table(df_opt)%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
888a6e4294456fcef4a9f87bab69e016