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%20plotly.graph_objects%20as%20go%0A%0A%20%20%20%20return%20go%2C%20mo%2C%20np%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%2000%3A%20Systems%20of%20Linear%20Equations%20and%20Vector%20Foundations%0A%0A%20%20%20%20Next%20Note%3A%20%5B01%20Inner%20Products%5D(01_inner_product.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%20Linear%20equations%20form%20the%20computational%20bedrock%20of%20data%20science%2C%20machine%20learning%2C%20and%20statistical%20modeling.%20Real-world%20phenomena%20frequently%20involve%20multiple%20interdependent%20variables%20constrained%20by%20simultaneous%20observations.%0A%0A%20%20%20%20Understanding%20how%20to%20express%20these%20relationships%20with%20vectors%20and%20matrices%20allows%20you%20to%3A%0A%0A%20%20%20%201.%20Formulate%20complex%20multidimensional%20problems%20in%20a%20compact%20mathematical%20language.%0A%20%20%20%202.%20Determine%20whether%20a%20unique%20solution%20exists%2C%20whether%20multiple%20solutions%20exist%2C%20or%20whether%20the%20system%20is%20inconsistent.%0A%20%20%20%203.%20Compute%20unknown%20parameters%20efficiently%20using%20standard%20linear%20algebra%20routines.%0A%20%20%20%204.%20Gain%20geometric%20intuition%20for%20how%20data%20points%2C%20hyperplanes%2C%20and%20transformations%20behave%20in%20vector%20spaces.%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%20Vector%20Representation%20of%20Quantities%0A%0A%20%20%20%20Consider%20a%20simple%202D%20coordinate%20system%20tracking%20two%20distinct%20quantities%3A%0A%20%20%20%20*%20%24x%24%3A%20Number%20of%20apples%0A%20%20%20%20*%20%24y%24%3A%20Number%20of%20bananas%0A%0A%20%20%20%20Using%20vector%20notation%2C%20we%20represent%20individual%20units%20along%20coordinate%20axes%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bapple%7D%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%201%20%5C%5C%200%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bbanana%7D%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%200%20%5C%5C%201%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bcombo%7D%7D%20%3D%20%5Cbegin%7Bbmatrix%7D%201%20%5C%5C%201%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20When%20a%20vendor%20packages%20goods%20in%20fixed%20proportions%2C%20say%20two%20apples%20for%20every%20one%20banana%2C%20the%20combo%20follows%20a%20directional%20ray%20through%20the%20origin%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bv%7D_%7B%5Ctext%7Bcombo%7D%7D%20%3D%20%5Calpha%20%5Cbegin%7Bbmatrix%7D%202%20%5C%5C%201%20%5Cend%7Bbmatrix%7D%2C%20%5Cquad%20%5Calpha%20%5Cin%20%5Cmathbb%7BR%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%20Pricing%20and%20Matrix%20Representation%0A%0A%20%20%20%20While%20quantities%20count%20discrete%20items%2C%20prices%20provide%20a%20scalar%20weighting%20of%20value.%20Let%3A%0A%20%20%20%20*%20%24x_1%24%3A%20Price%20per%20apple%0A%20%20%20%20*%20%24x_2%24%3A%20Price%20per%20banana%0A%0A%20%20%20%20For%20a%20bundle%20containing%20%24a_1%24%20apples%20and%20%24b_1%24%20bananas%2C%20the%20total%20cost%20%24c_1%24%20is%20a%20linear%20combination%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20c_1%20%3D%20a_1%20x_1%20%2B%20b_1%20x_2%0A%20%20%20%20%24%24%0A%0A%20%20%20%20When%20we%20observe%20two%20distinct%20purchase%20scenarios%3A%0A%20%20%20%201.%20%24a_1%24%20apples%20and%20%24b_1%24%20bananas%20sold%20for%20total%20cost%20%24c_1%24%0A%20%20%20%202.%20%24a_2%24%20apples%20and%20%24b_2%24%20bananas%20sold%20for%20total%20cost%20%24c_2%24%0A%0A%20%20%20%20We%20can%20stack%20these%20relationships%20into%20a%20single%20matrix%20equation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%20x_1%20%26%20x_2%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%5Cbegin%7Bbmatrix%7D%20a_1%20%26%20a_2%20%5C%5C%20b_1%20%26%20b_2%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%3D%20%5Cbegin%7Bbmatrix%7D%20c_1%20%26%20c_2%20%5Cend%7Bbmatrix%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20In%20row-vector%20notation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7Bx%7D_%7B%5Ctext%7Brow%7D%7D%20%5Cmathbf%7BA%7D%20%3D%20%5Cmathbf%7Bc%7D_%7B%5Ctext%7Brow%7D%7D%0A%20%20%20%20%24%24%0A%0A%20%20%20%20Taking%20the%20transpose%20yields%20the%20conventional%20column-vector%20formulation%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cmathbf%7BA%7D%5ET%20%5Cmathbf%7Bx%7D%20%3D%20%5Cmathbf%7Bc%7D%5ET%0A%20%20%20%20%24%24%0A%0A%20%20%20%20%23%23%23%20Solvability%20and%20Determinants%0A%0A%20%20%20%20A%20square%20linear%20system%20has%20a%20unique%20solution%20if%20and%20only%20if%20the%20coefficient%20matrix%20is%20invertible%2C%20which%20requires%20a%20non-zero%20determinant%3A%0A%0A%20%20%20%20%24%24%0A%20%20%20%20%5Cdet(%5Cmathbf%7BA%7D)%20%5Cneq%200%0A%20%20%20%20%24%24%0A%0A%20%20%20%20If%20%24%5Cdet(%5Cmathbf%7BA%7D)%20%3D%200%24%2C%20the%20rows%20(or%20columns)%20are%20linearly%20dependent.%20In%20that%20case%3A%0A%20%20%20%20*%20The%20system%20has%20infinitely%20many%20solutions%20if%20the%20target%20vector%20lies%20within%20the%20column%20span.%0A%20%20%20%20*%20The%20system%20has%20no%20solution%20if%20the%20target%20vector%20lies%20outside%20the%20column%20span.%0A%0A%20%20%20%20%23%23%23%20Role%20in%20ML%2C%20AI%2C%20and%20Statistics%0A%0A%20%20%20%20*%20**Parameter%20Estimation**%3A%20In%20supervised%20learning%2C%20linear%20models%20define%20predictions%20as%20linear%20combinations%20of%20feature%20vectors.%20Solving%20for%20optimal%20weights%20involves%20solving%20or%20approximating%20a%20system%20of%20linear%20equations.%0A%20%20%20%20*%20**Coordinate%20Transformations**%3A%20Every%20linear%20neural%20network%20layer%20performs%20a%20matrix-vector%20product%2C%20transforming%20representations%20from%20one%20coordinate%20basis%20to%20another.%0A%20%20%20%20*%20**Solvability%20and%20Multicollinearity**%3A%20In%20regression%20analysis%2C%20highly%20correlated%20predictors%20lead%20to%20near-singular%20matrices%20with%20determinants%20close%20to%20zero%2C%20causing%20numerical%20instability.%20Understanding%20linear%20independence%20is%20essential%20to%20diagnosing%20and%20resolving%20multicollinearity.%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%20Code%20examples%0A%0A%20%20%20%20The%20following%20code%20sets%20up%20the%20fruit%20pricing%20problem%2C%20validates%20invertibility%2C%20computes%20the%20unknown%20prices%2C%20and%20visualizes%20the%20intersection%20of%20the%20constraints%20using%20Plotly.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20c%2C%20np)%3A%0A%20%20%20%20%23%20Ensure%20matrix%20A%20is%20invertible%20by%20checking%20its%20determinant%0A%20%20%20%20det_A%20%3D%20np.linalg.det(A)%0A%20%20%20%20if%20det_A%20%3D%3D%200%3A%0A%20%20%20%20%20%20%20%20raise%20ValueError(%22Matrix%20A%20is%20singular%20and%20cannot%20be%20inverted%22)%0A%0A%20%20%20%20%23%20Solve%20the%20system%20of%20linear%20equations%3A%20x_row%20%40%20A%20%3D%20c_row%20%3C%3D%3E%20A.T%20%40%20x%20%3D%20c%0A%20%20%20%20%23%20Using%20np.linalg.solve%20on%20the%20transposed%20system%20is%20numerically%20preferred%0A%20%20%20%20x_solution%20%3D%20np.linalg.solve(A.T%2C%20c)%0A%0A%20%20%20%20%23%20Verification%20via%20matrix%20inversion%0A%20%20%20%20x_inv%20%3D%20c%20%40%20np.linalg.inv(A)%0A%0A%20%20%20%20%7B%22determinant%22%3A%20float(det_A)%2C%20%22solution_solve%22%3A%20x_solution.tolist()%2C%20%22solution_inv%22%3A%20x_inv.tolist()%7D%0A%20%20%20%20return%20(x_solution%2C)%0A%0A%0A%40app.cell%0Adef%20_(np)%3A%0A%20%20%20%20%23%20Coefficient%20matrix%20A%20(quantity%20matrix)%0A%20%20%20%20%23%20Row%201%3A%20scenario%201%20(40%20apples%2C%2020%20bananas)%0A%20%20%20%20%23%20Row%202%3A%20scenario%202%20(6%20apples%2C%208%20bananas)%0A%20%20%20%20A%20%3D%20np.array(%5B%5B40.0%2C%2020.0%5D%2C%20%5B6.0%2C%208.0%5D%5D)%0A%0A%20%20%20%20%23%20Target%20vector%20c%20(total%20costs%20for%20each%20scenario)%0A%20%20%20%20c%20%3D%20np.array(%5B100.0%2C%2080.0%5D)%0A%20%20%20%20return%20A%2C%20c%0A%0A%0A%40app.cell%0Adef%20_(A%2C%20c%2C%20go%2C%20np%2C%20x_solution)%3A%0A%20%20%20%20%23%20Compute%20lines%20for%20visualization%0A%20%20%20%20%23%20Equation%201%3A%20A%5B0%2C%200%5D*x1%20%2B%20A%5B1%2C%200%5D*x2%20%3D%20c%5B0%5D%20%3D%3E%2040*x1%20%2B%206*x2%20%3D%20100%0A%20%20%20%20%23%20Equation%202%3A%20A%5B0%2C%201%5D*x1%20%2B%20A%5B1%2C%201%5D*x2%20%3D%20c%5B1%5D%20%3D%3E%2020*x1%20%2B%208*x2%20%3D%2080%0A%20%20%20%20x1_range%20%3D%20np.linspace(0%2C%204%2C%20200)%0A%20%20%20%20x2_eq1%20%3D%20(c%5B0%5D%20-%20A%5B0%2C%200%5D%20*%20x1_range)%20%2F%20A%5B1%2C%200%5D%0A%20%20%20%20x2_eq2%20%3D%20(c%5B1%5D%20-%20A%5B0%2C%201%5D%20*%20x1_range)%20%2F%20A%5B1%2C%201%5D%0A%0A%20%20%20%20fig%20%3D%20go.Figure()%0A%0A%20%20%20%20%23%20Scenario%201%20constraint%20line%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%3Dx1_range%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx2_eq1%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%20name%3D%22Scenario%201%3A%2040x%E2%82%81%20%2B%206x%E2%82%82%20%3D%20100%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%231f77b4%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Scenario%202%20constraint%20line%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%3Dx1_range%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dx2_eq2%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%20name%3D%22Scenario%202%3A%2020x%E2%82%81%20%2B%208x%E2%82%82%20%3D%2080%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20line%3Ddict(color%3D%22%23ff7f0e%22%2C%20width%3D2.5)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Solution%20point%20(intersection)%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%5Bx_solution%5B0%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D%5Bx_solution%5B1%5D%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20mode%3D%22markers%2Btext%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3Df%22Solution%3A%20(%7Bx_solution%5B0%5D%3A.2f%7D%2C%20%7Bx_solution%5B1%5D%3A.2f%7D)%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20text%3D%5Bf%22%20%20Solution%20(%7Bx_solution%5B0%5D%3A.2f%7D%2C%20%7Bx_solution%5B1%5D%3A.2f%7D)%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20textposition%3D%22top%20right%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20marker%3Ddict(color%3D%22%23d62728%22%2C%20size%3D11%2C%20symbol%3D%22circle%22)%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3Ddict(text%3D%22Systems%20of%20Linear%20Equations%3A%20Constraint%20Lines%20and%20Unique%20Solution%22%2C%20font%3Ddict(size%3D14))%2C%0A%20%20%20%20%20%20%20%20xaxis%3Ddict(title%3D%22Price%20per%20Apple%20(x%E2%82%81)%22%2C%20range%3D%5B0%2C%203.5%5D%2C%20zeroline%3DTrue%2C%20gridcolor%3D%22%23e5e5e5%22)%2C%0A%20%20%20%20%20%20%20%20yaxis%3Ddict(title%3D%22Price%20per%20Banana%20(x%E2%82%82)%22%2C%20range%3D%5B0%2C%2012%5D%2C%20zeroline%3DTrue%2C%20gridcolor%3D%22%23e5e5e5%22)%2C%0A%20%20%20%20%20%20%20%20template%3D%22plotly_white%22%2C%0A%20%20%20%20%20%20%20%20legend%3Ddict(x%3D0.55%2C%20y%3D0.98)%2C%0A%20%20%20%20%20%20%20%20width%3D720%2C%0A%20%20%20%20%20%20%20%20height%3D480%2C%0A%20%20%20%20)%0A%0A%20%20%20%20fig%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%20Takeaway%0A%0A%20%20%20%20*%20**Geometric%20Meaning**%3A%20Each%20linear%20equation%20represents%20a%20hyperplane%20of%20constraints.%20Solving%20the%20system%20corresponds%20to%20finding%20the%20intersection%20point%20satisfying%20all%20constraints%20simultaneously.%0A%20%20%20%20*%20**Matrix%20Formulation**%3A%20Grouping%20quantities%20and%20observations%20into%20matrices%20turns%20a%20set%20of%20simultaneous%20equations%20into%20a%20compact%20matrix%20multiplication%20problem.%0A%20%20%20%20*%20**Invertibility%20and%20Determinants**%3A%20A%20unique%20solution%20exists%20if%20and%20only%20if%20the%20matrix%20determinant%20is%20non-zero%2C%20indicating%20that%20the%20equations%20are%20linearly%20independent.%0A%20%20%20%20*%20**Computational%20Practice**%3A%20While%20matrix%20inversion%20(%24%5Cmathbf%7BA%7D%5E%7B-1%7D%24)%20is%20theoretically%20convenient%2C%20numerical%20solvers%20like%20%60np.linalg.solve%60%20should%20always%20be%20preferred%20in%20practice%20for%20speed%20and%20stability.%0A%0A%20%20%20%20---%0A%0A%20%20%20%20Next%20Note%3A%20%5B01%20Inner%20Products%5D(01_inner_product.py)%20%26rarr%3B%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%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
5d27210847d91c880f1228f754a52a9b