import%20marimo%0A%0A__generated_with%20%3D%20%220.13.15%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20math%0A%0A%20%20%20%20import%20cvxpy%20as%20cvx%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%20%23%20pick%20a%20bunch%20of%20random%20points%0A%20%20%20%20pos%20%3D%20np.random.randn(1000%2C%202)%0A%0A%20%20%20%20print(f%22cvxpy-base%20version%3A%20%7Bcvx.__version__%7D%22)%0A%20%20%20%20print(f%22numpy%20version%3A%20%7Bnp.__version__%7D%22)%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%23%23%20Location%20problem%0A%0A%20%20%20%20We%20want%20to%20find%20the%20smallest%20circle%20such%20that%20%24n%24%20points%20are%20all%20contained%20in%20it.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Create%20a%20scatter%20plot%20with%20plotly%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%20%20%20%20_fig.add_trace(go.Scatter(x%3Dpos%5B%3A%2C%200%5D%2C%20y%3Dpos%5B%3A%2C%201%5D%2C%20mode%3D%22markers%22%2C%20marker%3D%7B%22symbol%22%3A%20%22x%22%2C%20%22size%22%3A%2010%7D))%0A%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Random%20Points%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22y%22%2C%0A%20%20%20%20%20%20%20%20yaxis%3D%7B%22scaleanchor%22%3A%20%22x%22%2C%20%22scaleratio%22%3A%201%7D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig%0A%0A%20%20%20%20return%0A%0A%0A%40app.function%0Adef%20minimize(objective%2C%20constraints%3DNone)%3A%0A%20%20%20%20%22%22%22Minimizes%20a%20given%20objective%20function%20subject%20to%20optional%20constraints.%0A%0A%20%20%20%20This%20function%20takes%20an%20objective%20function%20and%20an%20optional%20list%20of%20constraints%2C%0A%20%20%20%20creates%20a%20convex%20optimization%20problem%2C%20and%20solves%20it%20to%20obtain%20the%20minimum%0A%20%20%20%20value%20of%20the%20objective.%0A%0A%20%20%20%20%3Aparam%20objective%3A%20The%20objective%20function%20to%20be%20minimized.%0A%20%20%20%20%3Atype%20objective%3A%20cvx.Expression%0A%20%20%20%20%3Aparam%20constraints%3A%20A%20list%20of%20constraints%20applied%20to%20the%20optimization%20problem%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20or%20None%20if%20there%20are%20no%20constraints.%0A%20%20%20%20%3Atype%20constraints%3A%20Optional%5BList%5Bcvx.Constraint%5D%5D%0A%20%20%20%20%3Areturn%3A%20The%20minimum%20value%20of%20the%20objective%20function%20achieved%20under%20the%20given%20constraints.%0A%20%20%20%20%3Artype%3A%20float%0A%20%20%20%20%22%22%22%0A%20%20%20%20return%20cvx.Problem(cvx.Minimize(objective)%2C%20constraints).solve()%0A%0A%0A%40app.function%0Adef%20location(pos)%3A%0A%20%20%20%20%22%22%22Computes%20the%20minimum%20enclosing%20circle%20(or%20sphere%20in%20higher%20dimensions)%20for%20a%20given%20set%20of%20points.%0A%0A%20%20%20%20The%20function%20minimizes%20the%20radius%20while%20ensuring%20that%20all%20points%20lie%20within%0A%20%20%20%20or%20on%20the%20boundary%20of%20the%20circle.%0A%0A%20%20%20%20%3Aparam%20pos%3A%20A%202D%20array-like%20structure%20containing%20coordinates%20of%20points%0A%20%20%20%20%20%20%20%20(e.g.%2C%20list%20of%20tuples%20or%20numpy%20array).%20Each%20row%20represents%20a%20point%0A%20%20%20%20%20%20%20%20in%20an%20n-dimensional%20Euclidean%20space%2C%20and%20the%20number%20of%20columns%20must%0A%20%20%20%20%20%20%20%20be%20consistent%20across%20all%20rows.%0A%20%20%20%20%3Atype%20pos%3A%20array-like%0A%20%20%20%20%3Areturn%3A%20A%20tuple%20containing%20two%20elements%3A%20the%20radius%20of%20the%20minimum%0A%20%20%20%20%20%20%20%20enclosing%20circle%20(as%20a%20float)%20and%20the%20center%20coordinates%20of%20the%20circle%0A%20%20%20%20%20%20%20%20(as%20a%201D%20array%20of%20floats).%0A%20%20%20%20%3Artype%3A%20tuple%5Bfloat%2C%20numpy.ndarray%5D%0A%20%20%20%20%22%22%22%0A%20%20%20%20r%2C%20x%20%3D%20cvx.Variable(1)%2C%20cvx.Variable(2)%0A%20%20%20%20minimize(objective%3Dr%2C%20constraints%3D%5Bcvx.norm(row%20-%20x%2C%202)%20%3C%3D%20r%20for%20row%20in%20pos%5D)%0A%20%20%20%20return%20r.value%2C%20x.value%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20print(location(pos))%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20%23%20Create%20a%20scatter%20plot%20with%20a%20circle%20overlay%20using%20plotly%0A%20%20%20%20_radius%2C%20_midpoint%20%3D%20location(pos)%0A%0A%20%20%20%20%23%20Generate%20points%20for%20the%20circle%0A%20%20%20%20_theta%20%3D%20np.linspace(0%2C%202%20*%20math.pi%2C%201000)%0A%20%20%20%20_circle_x%20%3D%20_radius%20*%20np.cos(_theta)%20%2B%20_midpoint%5B0%5D%0A%20%20%20%20_circle_y%20%3D%20_radius%20*%20np.sin(_theta)%20%2B%20_midpoint%5B1%5D%0A%0A%20%20%20%20%23%20Create%20the%20figure%0A%20%20%20%20_fig%20%3D%20go.Figure()%0A%0A%20%20%20%20%23%20Add%20the%20scatter%20points%0A%20%20%20%20_fig.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%3Dpos%5B%3A%2C%200%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3Dpos%5B%3A%2C%201%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%3D%7B%22symbol%22%3A%20%22x%22%2C%20%22size%22%3A%2010%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Points%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Add%20the%20circle%0A%20%20%20%20_fig.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_circle_x%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20y%3D_circle_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%3D%7B%22color%22%3A%20%22red%22%2C%20%22width%22%3A%202%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20name%3D%22Minimum%20Circle%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20)%0A%0A%20%20%20%20%23%20Update%20layout%0A%20%20%20%20_fig.update_layout(%0A%20%20%20%20%20%20%20%20title%3D%22Minimum%20Circle%20Containing%20All%20Points%22%2C%0A%20%20%20%20%20%20%20%20xaxis_title%3D%22x%22%2C%0A%20%20%20%20%20%20%20%20yaxis_title%3D%22y%22%2C%0A%20%20%20%20%20%20%20%20yaxis%3D%7B%22scaleanchor%22%3A%20%22x%22%2C%20%22scaleratio%22%3A%201%7D%2C%0A%20%20%20%20)%0A%0A%20%20%20%20_fig%0A%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(%0A%20%20%20%20%20%20%20%20r%22%22%22%0A%20%20%20%20%23%20Summary%0A%0A%20%20%20%20-%20Each%20constraint%20%24%5CrVert%7B%5Cmathbf%7Bx%7D-%5Cmathbf%7Bc%7D%7D%5ClVert_2%20%3C%20R%24%20represents%20a%20cone.%0A%20%20%20%20%20%20Feasible%20domain%20is%20the%20intersection%20of%20all%20cones.%0A%0A%20%20%20%20-%20It%20is%20trivial%20to%20generalize%20(but%20not%20to%20plot)%20for%20points%20in%20higher%20dimensional%20spaces.%0A%0A%20%20%20%20-%20However%2C%20all%20of%20this%20fails%20once%20we%20can%20construct%20multiple%20circles.%0A%20%20%20%20%22%22%22%0A%20%20%20%20)%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
d6af59001018e8f7fbce5ebcce4ffe5ede71df2d9c295d42ee0386573496ab2b