Skip to content

Commit 668649e

Browse files
committed
feat: full solution to fitting sphere to data
1 parent 61b0cc9 commit 668649e

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

CvxLean/Command/Solve/Float/RealToFloat.lean

-3
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ partial def realToFloat (e : Expr) : MetaM Expr := do
2323
let translations ← discrTree.getMatch e
2424
for translation in translations do
2525
let (mvars, _, pattern) ← lambdaMetaTelescope translation.real
26-
trace[Meta.debug] (s!"`real-to-float` trying:\n{pattern}\n{e}")
27-
trace[Meta.debug] (s!"`real-to-float` mvars:\n{mvars}")
2826
if ← isDefEq pattern e then
2927
-- TODO: Search for conditions.
3028
let args ← mvars.mapM instantiateMVars
31-
trace[Meta.debug] (s!"`real-to-float` matched:\n{pattern}\n{e}")
3229
return mkAppNBeta translation.float args
3330
else
3431
trace[Meta.debug] "`real-to-float` error: no match for \n{pattern} \n{e}"

CvxLean/Examples/FittingSphere.lean

+4-2
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,11 @@ solve fittingSphereConvex nₚ mₚ xₚ
211211

212212
-- Finally, we recover the solution to the original problem.
213213

214-
#eval fittingSphereConvex.solution
214+
def sol := eqv.backward_map nₚ mₚ xₚ.float fittingSphereConvex.solution
215215

216-
def sol := eqv.backward_map
216+
#print eqv.backward_map
217+
218+
#eval sol -- (![1.664863, 0.031932], 1.159033)
217219

218220
end FittingSphere
219221

CvxLean/Examples/fitting_sphere.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import cvxpy as cp
3+
4+
n = 2
5+
6+
m = 10
7+
8+
x = np.array([[
9+
1.824183228637652032e+00, 1.349093690455489103e+00, 6.966316403935147727e-01,
10+
7.599387854623529392e-01, 2.388321695850912363e+00, 8.651370608981923116e-01,
11+
1.863922545015865406e+00, 7.099743941474848663e-01, 6.005484882320809570e-01,
12+
4.561429569892232472e-01], [
13+
-9.644136284187876385e-01, 1.069547315003422927e+00, 6.733229334437943470e-01,
14+
7.788072961810316164e-01, -9.467465278344706636e-01, -8.591303443863639311e-01,
15+
1.279527420871080956e+00, 5.314829019311283487e-01, 6.975676079749143499e-02,
16+
-4.641873429414754559e-01]]).T
17+
18+
c = cp.Variable((n))
19+
t = cp.Variable(1)
20+
21+
p = cp.Problem(
22+
cp.Minimize(
23+
cp.sum([((cp.norm(x[i]) ** 2) - 2 * (x[i] @ c) - t) ** 2 for i in range(m)])
24+
), [])
25+
p.solve(solver=cp.MOSEK, verbose=True)
26+
27+
# Backward map from change of variables.
28+
r = np.sqrt(t.value + (np.linalg.norm(c.value) ** 2))
29+
30+
print("t* = ", t.value)
31+
print("c* = ", c.value)
32+
print("r* = ", r)

0 commit comments

Comments
 (0)