fact
stringlengths
5
7.52k
type
stringclasses
9 values
library
stringclasses
2 values
imports
listlengths
0
15
filename
stringclasses
121 values
symbolic_name
stringlengths
1
53
docstring
stringlengths
6
446
compileCargo (name : String) (manifestFile : FilePath) (cargo : FilePath := "cargo") (env : Array (String × Option String)) : LogIO Unit := do logInfo s!"Creating {name}" proc { env := env cmd := cargo.toString args := #["build", "--release", "--manifest-path", manifestFile.toString] }
def
root
[ "import Lake" ]
lakefile.lean
compileCargo
null
buildCargo (targetFile : FilePath) (manifestFile : FilePath) (targetDest : FilePath) (oFileJobs : Array (BuildJob FilePath)) (stopOnSuccess : Bool) : SpawnM (BuildJob FilePath) := let name := targetFile.fileName.getD targetFile.toString buildFileAfterDepArray targetFile oFileJobs fun _ => do let env := if stopOnSuccess...
def
root
[ "import Lake" ]
lakefile.lean
buildCargo
null
runEquivalenceTactic (mvarId : MVarId) (stx : Syntax) : TermElabM Unit := do runTransformationTactic TransformationGoal.Equivalence mvarId stx /-- Run equivalence tactic and return both the right-hand term (`q`) and the equivalence proof, of type `Equivalence p q`. -/
def
CvxLean
[ "import CvxLean.Lib.Equivalence", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Equivalence", "import CvxLean.Meta.TacticBuilder", "import CvxLean.Comman...
CvxLean/Command/Equivalence.lean
runEquivalenceTactic
/-- Run a transformation tactic indicating that an equivalence is expected. -/
elabEquivalenceProof (lhs : Expr) (rhsName : Name) (stx : Syntax) : TermElabM (Expr × Expr) := elabTransformationProof TransformationGoal.Equivalence lhs rhsName stx /-- Open an equivalence environment. -/ syntax (name := equivalence) "equivalence" ident "/" ident declSig ":=" Lean.Parser.Term.byTactic : command /-- Op...
def
CvxLean
[ "import CvxLean.Lib.Equivalence", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Equivalence", "import CvxLean.Meta.TacticBuilder", "import CvxLean.Comman...
CvxLean/Command/Equivalence.lean
elabEquivalenceProof
/-- Run equivalence tactic and return both the right-hand term (`q`) and the equivalence proof, of type `Equivalence p q`. -/
evalEquivalenceAux (probIdStx eqvIdStx : TSyntax `ident) (xs : Array (Syntax × Expr)) (lhsStx : Syntax) (proofStx : TSyntax `Lean.Parser.Term.byTactic) (bwdMap : Bool) : TermElabM Unit := do let D ← Meta.mkFreshTypeMVar let R ← Meta.mkFreshTypeMVar let lhsTy := mkApp2 (Lean.mkConst ``Minimization) D R let lhs ← elabTer...
def
CvxLean
[ "import CvxLean.Lib.Equivalence", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Equivalence", "import CvxLean.Meta.TacticBuilder", "import CvxLean.Comman...
CvxLean/Command/Equivalence.lean
evalEquivalenceAux
null
evalEquivalence : CommandElab := fun stx => match stx with | `(equivalence $eqvId / $probId $declSig := $proofStx) => do liftTermElabM do let (binders, lhsStx) := expandDeclSig declSig.raw elabBindersEx binders.getArgs fun xs => evalEquivalenceAux probId eqvId xs lhsStx proofStx false | _ => throwUnsupportedSyntax /-- ...
def
CvxLean
[ "import CvxLean.Lib.Equivalence", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Equivalence", "import CvxLean.Meta.TacticBuilder", "import CvxLean.Comman...
CvxLean/Command/Equivalence.lean
evalEquivalence
/-- Create `equivalence` command. -/
evalEquivalenceAndBwdMap : CommandElab := fun stx => match stx with | `(equivalence* $eqvId / $probId $declSig := $proofStx) => do liftTermElabM do let (binders, lhsStx) := expandDeclSig declSig.raw elabBindersEx binders.getArgs fun xs => evalEquivalenceAux probId eqvId xs lhsStx proofStx true | _ => throwUnsupportedSy...
def
CvxLean
[ "import CvxLean.Lib.Equivalence", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Equivalence", "import CvxLean.Meta.TacticBuilder", "import CvxLean.Comman...
CvxLean/Command/Equivalence.lean
evalEquivalenceAndBwdMap
/-- Same as `equivalence` but also adds the backward map to the environment. -/
runReductionTactic (mvarId : MVarId) (stx : Syntax) : TermElabM Unit := runTransformationTactic TransformationGoal.Reduction mvarId stx /-- Run reduction tactic and return both the right-hand term (`q`) and the reduction proof, of type `Reduction p q`. -/
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Reduction", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Reduction", "import CvxLean.Meta.TacticBuilder", "import C...
CvxLean/Command/Reduction.lean
runReductionTactic
/-- Run a transformation tactic indicating that a reduction is expected. -/
elabReductionProof (lhs : Expr) (rhsName : Name) (stx : Syntax) : TermElabM (Expr × Expr) := elabTransformationProof TransformationGoal.Reduction lhs rhsName stx /-- Open a reduction environment. -/ syntax (name := reduction) "reduction" ident "/" ident declSig ":=" Lean.Parser.Term.byTactic : command /-- Open a reduct...
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Reduction", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Reduction", "import CvxLean.Meta.TacticBuilder", "import C...
CvxLean/Command/Reduction.lean
elabReductionProof
/-- Run reduction tactic and return both the right-hand term (`q`) and the reduction proof, of type `Reduction p q`. -/
evalReductionAux (probIdStx redIdStx : TSyntax `ident) (xs : Array (Syntax × Expr)) (lhsStx : Syntax) (proofStx : TSyntax `Lean.Parser.Term.byTactic) (bwdMap : Bool) : TermElabM Unit := do let D ← Meta.mkFreshTypeMVar let R ← Meta.mkFreshTypeMVar let lhsTy := mkApp2 (Lean.mkConst ``Minimization) D R let lhs ← elabTermA...
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Reduction", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Reduction", "import CvxLean.Meta.TacticBuilder", "import C...
CvxLean/Command/Reduction.lean
evalReductionAux
null
evalReduction : CommandElab := fun stx => match stx with | `(reduction $eqvId / $probId $declSig := $proofStx) => do liftTermElabM do let (binders, lhsStx) := expandDeclSig declSig.raw elabBindersEx binders.getArgs fun xs => evalReductionAux probId eqvId xs lhsStx proofStx false | _ => throwUnsupportedSyntax /-- Same a...
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Reduction", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Reduction", "import CvxLean.Meta.TacticBuilder", "import C...
CvxLean/Command/Reduction.lean
evalReduction
/-- Create `reduction` command. It is similar to the `equivalence` command, but requires a `Reduction` instead of an `Equivalence`. -/
evalReductionAndBwdMap : CommandElab := fun stx => match stx with | `(reduction* $eqvId / $probId $declSig := $proofStx) => do liftTermElabM do let (binders, lhsStx) := expandDeclSig declSig.raw elabBindersEx binders.getArgs fun xs => evalReductionAux probId eqvId xs lhsStx proofStx true | _ => throwUnsupportedSyntax
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Reduction", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Util.Simp", "import CvxLean.Meta.Util.Error", "import CvxLean.Meta.Util.Debug", "import CvxLean.Meta.Reduction", "import CvxLean.Meta.TacticBuilder", "import C...
CvxLean/Command/Reduction.lean
evalReductionAndBwdMap
/-- Same as `reduction` but also adds the backward map to the environment. -/
runRelaxationTactic (mvarId : MVarId) (stx : Syntax) : TermElabM Unit := runTransformationTactic TransformationGoal.Relaxation mvarId stx /-- Run relaxation tactic and return both the right-hand term (`q`) and the relaxation proof, of type `Relaxation p q`. -/
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Relaxation", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Relaxation", "import CvxLean.Meta.TacticBuilder" ]
CvxLean/Command/Relaxation.lean
runRelaxationTactic
/-- Run a transformation tactic indicating that a relaxation is expected. -/
elabRelaxationProof (lhs : Expr) (rhsName : Name) (stx : Syntax) : TermElabM (Expr × Expr) := elabTransformationProof TransformationGoal.Relaxation lhs rhsName stx /-- Open a relaxation environment. -/ syntax (name := relaxation) "relaxation" ident "/" ident declSig ":=" Lean.Parser.Term.byTactic : command /-- Definiti...
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Relaxation", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Relaxation", "import CvxLean.Meta.TacticBuilder" ]
CvxLean/Command/Relaxation.lean
elabRelaxationProof
/-- Run relaxation tactic and return both the right-hand term (`q`) and the relaxation proof, of type `Relaxation p q`. -/
evalRelaxation : CommandElab := fun stx => match stx with | `(relaxation $relIdStx / $probIdStx $declSig := $proofStx) => do liftTermElabM do let (binders, lhsStx) := expandDeclSig declSig.raw elabBindersEx binders.getArgs fun xs => do let D ← Meta.mkFreshTypeMVar let R ← Meta.mkFreshTypeMVar let lhsTy := mkApp2 (Lean....
def
CvxLean
[ "import Lean", "import CvxLean.Lib.Relaxation", "import CvxLean.Syntax.Minimization", "import CvxLean.Meta.Util.Expr", "import CvxLean.Meta.Relaxation", "import CvxLean.Meta.TacticBuilder" ]
CvxLean/Command/Relaxation.lean
evalRelaxation
/-- Definition of the `relaxation` command. -/
getProblemName (stx : Syntax) : MetaM Name := do -- TODO: Full name with parameters? let idStx := match stx with | Syntax.ident _ _ _ _ => stx | Syntax.node _ _ args => args.getD 0 Syntax.missing | _ => Syntax.missing if ¬ idStx.getId.isStr then throwError "Invalid name for minimization problem: {idStx}." let currNames...
def
CvxLean
[ "import CvxLean.Tactic.DCP.AtomLibrary.All", "import CvxLean.Command.Solve.Conic" ]
CvxLean/Command/Solve.lean
getProblemName
/-- Get problem name. Used to add information about the solution to the environment. -/
getCanonizedProblemAndBwdMap (prob : Expr) : MetaM (MinimizationExpr × Expr) := do let ogProb ← MinimizationExpr.fromExpr prob let (canonProb, eqvProof) ← DCP.canonize ogProb let backwardMap ← mkAppM ``Minimization.Equivalence.psi #[eqvProof] return (canonProb, backwardMap) syntax (name := solve) "solve " term : comman...
def
CvxLean
[ "import CvxLean.Tactic.DCP.AtomLibrary.All", "import CvxLean.Command.Solve.Conic" ]
CvxLean/Command/Solve.lean
getCanonizedProblemAndBwdMap
/-- Call DCP and get the problem in conic form as well as `ψ`, the backward map from the equivalence. -/
evalSolve : CommandElab := fun stx => match stx with | `(solve $probInstance) => liftTermElabM <| do let probTerm ← elabTerm probInstance.raw none let probTerm ← whnf probTerm let probTerm ← instantiateMVars probTerm -- NOTE: Needed to solve the "OfNat" mvar bug. for mvarId in ← getMVars probTerm do try { let mvarVal ←...
def
CvxLean
[ "import CvxLean.Tactic.DCP.AtomLibrary.All", "import CvxLean.Command.Solve.Conic" ]
CvxLean/Command/Solve.lean
evalSolve
/-- The `solve` command. It works as follows: 1. Canonize optimization problem to conic form. 2. Extract problem data using `determineCoeffsFromExpr`. 3. Obtain a solution using `solutionDataFromProblemData`, which calls an external solver. 4. Store the result in the enviroment. -/
p₁ := optimization (x y : ℝ) maximize sqrt (x - y) subject to c1 : y = 2 * x - 3 c2 : x ^ (2 : ℝ) ≤ 2 c3 : 0 ≤ x - y #check p₁ -- Apply DCP transformation and call solver. solve p₁ #eval p₁.status #eval p₁.solution #eval p₁.value /- Second example. -/
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/LT2024.lean
p₁
null
p₂ := optimization (x : ℝ) minimize (x) subject to c1 : 1 / 1000 ≤ x c2 : 1 / sqrt x ≤ exp x -- solve p₂ -- set_option trace.Meta.debug true -- Equivalence mode. equivalence eqv₂/q₂ : p₂ := by pre_dcp solve q₂ #print q₂.conicForm #eval q₂.status #eval q₂.solution #eval q₂.value /- Third example (geometric programming)....
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/LT2024.lean
p₂
null
p₃ (Awall Aflr α β γ δ : ℝ) := optimization (h w d : ℝ) minimize (1 / (h * w * d)) subject to c1 : 0 < h c2 : 0 < w c3 : 0 < d c4 : 2 * (h * w + h * d) ≤ Awall c5 : w * d ≤ Aflr c6 : α ≤ h / w c7 : h / w ≤ β c8 : γ ≤ d / w c9 : d / w ≤ δ equivalence* eqv₃/q₃ : p₃ 100 10 0.5 2 0.5 2 := by change_of_variables! (h') (h ↦ ...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/LT2024.lean
p₃
/-- Maximizing the volume of a box. See: https://www.cvxpy.org/examples/dgp/max_volume_box.html -/
p₃.solution := eqv₃.backward_map q₃.solution #eval p₃.solution
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/LT2024.lean
p₃.solution
null
h := p₃.solution.1
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/LT2024.lean
h
null
w := p₃.solution.2.1
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/LT2024.lean
w
null
d := p₃.solution.2.2 #eval h * w * d #eval 2 * (h * w + h * d) ≤ 100 #eval w * d ≤ 10 #eval 0.5 ≤ h / w #eval h / w ≤ 2 #eval 0.5 ≤ d / w #eval d / w ≤ 2
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/LT2024.lean
d
null
p := optimization (x y : ℝ) minimize -2 * x subject to c₁ : 0 ≤ x c₂ : 1 < y c₃ : log (y - 1) ≤ 2 * sqrt x + 1 c₄ : 3 * x + 5 * y ≤ 10 -- Transform (proving `p ≡ q`). equivalence* eqv/q : p := by change_of_variables! (v) (y ↦ v + 1) change_of_variables! (w) (v ↦ exp w) remove_constr c₂ => field_simp; arith -- Make DCP-...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Demos/README.lean
p
null
covEstimation (n : ℕ) (N : ℕ) (α : ℝ) (y : Fin N → Fin n → ℝ) := optimization (R : Matrix (Fin n) (Fin n) ℝ) maximize (∏ i, gaussianPdf R (y i)) subject to c_pos_def : R.PosDef c_sparse : R⁻¹.abs.sum ≤ α reduction* red/covEstimationConvex (n : ℕ) (N : ℕ) (α : ℝ) (y : Fin N → Fin n → ℝ) : covEstimation n N α y := by -- ...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/CovarianceEstimation.lean
covEstimation
null
nₚ : ℕ := 2 @[optimization_param, reducible]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/CovarianceEstimation.lean
nₚ
null
Nₚ : ℕ := 4 @[optimization_param]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/CovarianceEstimation.lean
Nₚ
null
αₚ : ℝ := 1 @[optimization_param]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/CovarianceEstimation.lean
αₚ
null
yₚ : Fin Nₚ → Fin nₚ → ℝ := ![![0, 2], ![2, 0], ![-2, 0], ![0, -2]] solve covEstimationConvex nₚ Nₚ αₚ yₚ #print covEstimationConvex.conicForm -- minimize -- -(-(Nₚ • log (sqrt ((2 * π) ^ nₚ)) + Nₚ • (-Vec.sum t.0 / 2)) + -- -(↑Nₚ * trace ((covarianceMatrix fun i => yₚ i) * Rᵀ) / 2)) -- subject to -- _ : Real.nonnegOrt...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/CovarianceEstimation.lean
yₚ
null
Rₚ_opt := red.backward_map nₚ Nₚ αₚ.float yₚ.float covEstimationConvex.solution #eval Rₚ_opt -- !![2.000240, -0.000000; -0.000000, 2.000232]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/CovarianceEstimation.lean
Rₚ_opt
null
leastSquares {n : ℕ} (a : Fin n → ℝ) := optimization (x : ℝ) minimize (∑ i, ((a i - x) ^ 2) : ℝ) @[reducible]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
leastSquares
null
mean {n : ℕ} (a : Fin n → ℝ) : ℝ := (1 / n) * ∑ i, (a i) /-- It is useful to rewrite the sum of squares in the following way to prove `leastSquares_optimal_eq_mean`, following Marty Cohen's answer in https://math.stackexchange.com/questions/2554243. -/
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
mean
null
leastSquares_alt_objFun {n : ℕ} (hn : 0 < n) (a : Fin n → ℝ) (x : ℝ) : (∑ i, ((a i - x) ^ 2)) = n * ((x - mean a) ^ 2 + (mean (a ^ 2) - (mean a) ^ 2)) := by calc -- 1) Σ (aᵢ - x)² = Σ (aᵢ² - 2aᵢx + x²) _ = ∑ i, ((a i) ^ 2 - 2 * (a i) * x + (x ^ 2)) := by congr; funext i; simp; ring -- 2) ... = Σ aᵢ² - 2xΣ aᵢ + nx² _ = ...
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
leastSquares_alt_objFun
/-- It is useful to rewrite the sum of squares in the following way to prove `leastSquares_optimal_eq_mean`, following Marty Cohen's answer in https://math.stackexchange.com/questions/2554243. -/
leastSquares_optimal_eq_mean {n : ℕ} (hn : 0 < n) (a : Fin n → ℝ) (x : ℝ) (h : (leastSquares a).optimal x) : x = mean a := by simp [optimal, feasible, leastSquares] at h replace h : ∀ y, (x - mean a) ^ 2 ≤ (y - mean a) ^ 2 := by intros y have hy := h y have h_rw_x := leastSquares_alt_objFun hn a x have h_rw_y := leastS...
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
leastSquares_optimal_eq_mean
/-- Key result about least squares: `x* = mean a`. -/
leastSquaresVec {n : ℕ} (a : Fin n → ℝ) := optimization (x : ℝ) minimize (Vec.sum ((a - Vec.const n x) ^ 2) : ℝ) /-- Same as `leastSquares_optimal_eq_mean` in vector notation. -/
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
leastSquaresVec
null
leastSquaresVec_optimal_eq_mean {n : ℕ} (hn : 0 < n) (a : Fin n → ℝ) (x : ℝ) (h : (leastSquaresVec a).optimal x) : x = mean a := by apply leastSquares_optimal_eq_mean hn a simp [leastSquaresVec, leastSquares, optimal, feasible] at h ⊢ intros y simp only [Vec.sum, Pi.pow_apply, Pi.sub_apply, Vec.const, rpow_two] at h ex...
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
leastSquaresVec_optimal_eq_mean
/-- Same as `leastSquares_optimal_eq_mean` in vector notation. -/
fittingSphere := optimization (c : Fin n → ℝ) (r : ℝ) minimize (∑ i, (‖(x i) - c‖ ^ 2 - r ^ 2) ^ 2 : ℝ) subject to h₁ : 0 ≤ r -- Changes of variables ensuring bijection, which must also add the condition on `E` in the -- equivalence. TODO: Move to `CvxLean` core.
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
fittingSphere
null
ChangeOfVariablesBij {D E} (c : E → D) where c_inv : D → E cond_D : D → Prop cond_E : E → Prop prop_D : ∀ x, cond_D x → c (c_inv x) = x prop_E : ∀ y, cond_E y → c_inv (c y) = y @[equiv]
structure
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
ChangeOfVariablesBij
null
ChangeOfVariablesBij.toEquivalence {D E R} [Preorder R] {f : D → R} {cs : D → Prop} (c : E → D) (cov : ChangeOfVariablesBij c) (hD : ∀ x, cs x → cov.cond_D x) (hE : ∀ x, cs x → cov.cond_E (cov.c_inv x)) : ⟨f, cs⟩ ≡ ⟨fun y => f (c y), fun y => cs (c y) ∧ cov.cond_E y⟩ := Equivalence.ofStrongEquivalence <| { phi := fun x...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
ChangeOfVariablesBij.toEquivalence
null
covBij {n} : ChangeOfVariablesBij (fun ((c, t) : (Fin n → ℝ) × ℝ) => (c, sqrt (t + ‖c‖ ^ 2))) := { c_inv := fun (c, r) => (c, r ^ 2 - ‖c‖ ^ 2), cond_D := fun (_, r) => 0 ≤ r, cond_E := fun (c, t) => 0 ≤ t + ‖c‖ ^ 2, prop_D := fun (c, r) h => by simp [sqrt_sq h], prop_E := fun (c, t) h => by simp at h; simp [sq_sqrt h] ...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
covBij
null
fittingSphereConvex (n m : ℕ) (x : Fin m → Fin n → ℝ) := optimization (c : Fin n → ℝ) (t : ℝ) minimize (Vec.sum ((Vec.norm x ^ 2 - 2 * mulVec x c - Vec.const m t) ^ 2) : ℝ) /-- This tells us that solving the relaxed problem is sufficient (i.e., it is a valid reduction). -/
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
fittingSphereConvex
null
optimal_convex_implies_optimal_t (hm : 0 < m) (c : Fin n → ℝ) (t : ℝ) (h_opt : (fittingSphereConvex n m x).optimal (c, t)) : (fittingSphereT n m x).optimal (c, t) := by simp [fittingSphereT, fittingSphereConvex, optimal, feasible] at h_opt ⊢ constructor -- Feasibility. · let a := Vec.norm x ^ 2 - 2 * mulVec x c have h_...
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
optimal_convex_implies_optimal_t
/-- This tells us that solving the relaxed problem is sufficient (i.e., it is a valid reduction). -/
red (hm : 0 < m) : (fittingSphereT n m x) ≼ (fittingSphereConvex n m x) := { psi := id, psi_optimality := fun (c, t) h_opt => optimal_convex_implies_optimal_t n m x hm c t h_opt } #print fittingSphereConvex -- optimization (c : Fin n → ℝ) (t : ℝ) -- minimize Vec.sum ((Vec.norm x ^ 2 - 2 * mulVec x c - Vec.const m t) ^ ...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
red
/-- We show that we have a reduction via the identity map. -/
nₚ := 2 @[optimization_param]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
nₚ
null
mₚ := 10 @[optimization_param]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
mₚ
null
xₚ : Fin mₚ → Fin nₚ → ℝ := Matrix.transpose <| ![ ![1.824183228637652032e+00, 1.349093690455489103e+00, 6.966316403935147727e-01, 7.599387854623529392e-01, 2.388321695850912363e+00, 8.651370608981923116e-01, 1.863922545015865406e+00, 7.099743941474848663e-01, 6.005484882320809570e-01, 4.561429569892232472e-01], ![-9.6...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
xₚ
null
sol := eqv.backward_map nₚ mₚ xₚ.float fittingSphereConvex.solution
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
sol
null
cₚ_opt := sol.1
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
cₚ_opt
null
rₚ_opt := sol.2 #eval cₚ_opt -- ![1.664863, 0.031932] #eval rₚ_opt -- 1.159033
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/FittingSphere.lean
rₚ_opt
null
hypersonicShapeDesign := optimization (Δx : ℝ) minimize sqrt ((1 / Δx ^ 2) - 1) subject to h₁ : 10e-6 ≤ Δx h₂ : Δx ≤ 1 h₃ : a * (1 / Δx) - (1 - b) * sqrt (1 - Δx ^ 2) ≤ 0 equivalence* eqv₁/hypersonicShapeDesignConvex (a b : ℝ) (ha : 0 ≤ a) (hb₁ : 0 ≤ b) (hb₂ : b < 1) : hypersonicShapeDesign a b := by pre_dcp #print hyp...
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
hypersonicShapeDesign
null
aₚ : ℝ := 0.05 @[simp high]
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
aₚ
null
aₚ_nonneg : 0 ≤ aₚ := by unfold aₚ; norm_num @[optimization_param]
lemma
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
aₚ_nonneg
null
bₚ : ℝ := 0.65
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
bₚ
null
bₚ_nonneg : 0 ≤ bₚ := by unfold bₚ; norm_num
lemma
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
bₚ_nonneg
null
bₚ_lt_one : bₚ < 1 := by unfold bₚ; norm_num @[simp high]
lemma
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
bₚ_lt_one
null
one_sub_bₚ_nonneg : 0 ≤ 1 - bₚ := by unfold bₚ; norm_num time_cmd solve hypersonicShapeDesignConvex aₚ bₚ aₚ_nonneg bₚ_nonneg bₚ_lt_one #print hypersonicShapeDesignConvex.conicForm -- Final width of wedge.
lemma
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
one_sub_bₚ_nonneg
null
wₚ_opt := eqv₁.backward_map aₚ.float bₚ.float hypersonicShapeDesignConvex.solution #eval wₚ_opt -- 0.989524 #eval aₚ.float * (1 / wₚ_opt) - (1 - bₚ.float) * Float.sqrt (1 - wₚ_opt ^ 2) ≤ 0 #eval aₚ.float * (1 / wₚ_opt) - (1 - bₚ.float) * Float.sqrt (1 - wₚ_opt ^ 2) ≤ 0.000001 -- Final height of wedge.
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
wₚ_opt
null
hₚ_opt := Float.sqrt (1 - wₚ_opt ^ 2) #eval hₚ_opt -- 0.144368 -- Final L/D ratio.
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
hₚ_opt
null
ldRatioₚ := 1 / (Float.sqrt ((1 / wₚ_opt ^ 2) - 1)) #eval ldRatioₚ -- 6.854156 -- While the above is good enough, we simplify the problem further by performing a change of -- variables and simplifying appropriately. equivalence* eqv₂/hypersonicShapeDesignSimpler (a b : ℝ) (ha : 0 ≤ a) (hb₁ : 0 ≤ b) (hb₂ : b < 1) : hype...
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
ldRatioₚ
null
wₚ'_opt := eqv₁.backward_map aₚ.float bₚ.float <| eqv₂.backward_map aₚ.float bₚ.float hypersonicShapeDesignSimpler.solution #eval wₚ'_opt -- 0.989524 #eval aₚ.float * (1 / wₚ'_opt) - (1 - bₚ.float) * Float.sqrt (1 - wₚ'_opt ^ 2) ≤ 0 -- Final height of wedge.
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
wₚ'_opt
null
hₚ'_opt := Float.sqrt (1 - wₚ'_opt ^ 2) #eval hₚ'_opt -- 0.144371 -- Final L/D ratio.
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
hₚ'_opt
null
ldRatioₚ' := 1 / (Float.sqrt ((1 / wₚ'_opt ^ 2) - 1)) #eval ldRatioₚ' -- 6.854031
def
CvxLean
[ "import CvxLean", "import CvxLean.Command.Util.TimeCmd" ]
CvxLean/Examples/HypersonicShapeDesign.lean
ldRatioₚ'
null
trussDesign := optimization (h w r R : ℝ) with A := 2 * π * (R ^ 2 - r ^ 2) minimize 2 * A * sqrt (w ^ 2 + h ^ 2) subject to c_r : 0 < r c_F₁ : F₁ * sqrt (w ^ 2 + h ^ 2) / (2 * h) ≤ σ * A c_F₂ : F₂ * sqrt (w ^ 2 + h ^ 2) / (2 * w) ≤ σ * A c_hmin : hmin ≤ h c_hmax : h ≤ hmax c_wmin : wmin ≤ w c_wmax : w ≤ wmax c_R_lb : ...
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
trussDesign
null
hminₚ : ℝ := 1 @[optimization_param]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
hminₚ
null
hmaxₚ : ℝ := 100 @[simp high]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
hmaxₚ
null
hminₚ_pos : 0 < hminₚ := by unfold hminₚ; norm_num
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
hminₚ_pos
null
hminₚ_le_hmaxₚ : hminₚ ≤ hmaxₚ := by unfold hminₚ hmaxₚ; norm_num @[optimization_param]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
hminₚ_le_hmaxₚ
null
wminₚ : ℝ := 1 @[optimization_param]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
wminₚ
null
wmaxₚ : ℝ := 100 @[simp high]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
wmaxₚ
null
wminₚ_pos : 0 < wminₚ := by unfold wminₚ; norm_num
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
wminₚ_pos
null
wminₚ_le_wmaxₚ : wminₚ ≤ wmaxₚ := by unfold wminₚ wmaxₚ; norm_num @[optimization_param]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
wminₚ_le_wmaxₚ
null
Rmaxₚ : ℝ := 10 @[simp high]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
Rmaxₚ
null
Rmaxₚ_pos : 0 < Rmaxₚ := by unfold Rmaxₚ; norm_num @[optimization_param]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
Rmaxₚ_pos
null
σₚ : ℝ := 0.5 @[simp high]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
σₚ
null
σₚ_pos : 0 < σₚ := by unfold σₚ; norm_num @[optimization_param]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
σₚ_pos
null
F₁ₚ : ℝ := 10 @[simp high]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
F₁ₚ
null
F₁ₚ_pos : 0 < F₁ₚ := by unfold F₁ₚ; norm_num @[optimization_param]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
F₁ₚ_pos
null
F₂ₚ : ℝ := 20 @[simp high]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
F₂ₚ
null
F₂ₚ_pos : 0 < F₂ₚ := by unfold F₂ₚ; norm_num solve trussDesignDCP hminₚ hmaxₚ hminₚ_pos hminₚ_le_hmaxₚ wminₚ wmaxₚ wminₚ_pos wminₚ_le_wmaxₚ Rmaxₚ Rmaxₚ_pos σₚ σₚ_pos F₁ₚ F₁ₚ_pos F₂ₚ F₂ₚ_pos -- There are two non-trivial backward maps here, one from `eqv₁` and one from `eqv₂`, so we need to -- apply both of them.
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
F₂ₚ_pos
null
eqv₁.backward_mapₚ := eqv₁.backward_map hminₚ.float hmaxₚ.float wminₚ.float wmaxₚ.float Rmaxₚ.float σₚ.float F₁ₚ.float F₂ₚ.float
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
eqv₁.backward_mapₚ
null
eqv₂.backward_mapₚ := eqv₂.backward_map hminₚ.float hmaxₚ.float wminₚ.float wmaxₚ.float Rmaxₚ.float σₚ.float F₁ₚ.float F₂ₚ.float
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
eqv₂.backward_mapₚ
null
sol := eqv₁.backward_mapₚ (eqv₂.backward_mapₚ trussDesignDCP.solution) -- Finally, we obtain the optimal height, width, inner radius, and outer radius.
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
sol
null
hₚ_opt := sol.1
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
hₚ_opt
null
wₚ_opt := sol.2.1
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
wₚ_opt
null
rₚ_opt := sol.2.2.1
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
rₚ_opt
null
Rₚ_opt := sol.2.2.2 -- NOTE: These numbers may differ slighlty depending on the rewrites found by `pre_dcp`. #eval hₚ_opt -- 1.000000 #eval wₚ_opt -- 1.000517 #eval rₚ_opt -- 0.010162 #eval Rₚ_opt -- 2.121443
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
Rₚ_opt
null
valueₚ := let pi := 2 * Float.acos 0; let Aₚ_opt := 2 * pi * (Rₚ_opt ^ 2 - rₚ_opt ^ 2); 2 * Aₚ_opt * Float.sqrt (wₚ_opt ^ 2 + hₚ_opt ^ 2) #eval valueₚ -- 79.999976
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/TrussDesign.lean
valueₚ
null
vehSpeedSched [Fact (0 < n)] := optimization (s : Fin n → ℝ) minimize ∑ i, (d i / s i) * F (s i) subject to c_smin : ∀ i, smin i ≤ s i c_smax : ∀ i, s i ≤ smax i c_τmin : ∀ i, τmin i ≤ ∑ j in [[0, i]], d j / s j c_τmax : ∀ i, ∑ j in [[0, i]], d j / s j ≤ τmax i variable {n}
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
vehSpeedSched
null
simp_vec_fraction (h_d_pos : StrongLT 0 d) (s : Fin n → ℝ) (i : Fin n) : d i / (d i / s i) = s i := by have h_di_pos := h_d_pos i; simp at h_di_pos; have h_di_nonzero : d i ≠ 0 := by linarith rw [← div_mul, div_self h_di_nonzero, one_mul]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
simp_vec_fraction
null
fold_partial_sum [hn : Fact (0 < n)] (t : Fin n → ℝ) (i : Fin n) : ∑ j in [[0, i]], t j = Vec.cumsum t i := by simp [Vec.cumsum]; split_ifs · rfl · linarith [hn.out] equivalence* eqv₁/vehSpeedSchedConvex (n : ℕ) (d : Fin n → ℝ) (τmin τmax smin smax : Fin n → ℝ) (F : ℝ → ℝ) (h_n_pos : 0 < n) (h_d_pos : StrongLT 0 d) (h_...
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
fold_partial_sum
null
nₚ : ℕ := 10
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
nₚ
null
nₚ_pos : 0 < nₚ := by unfold nₚ; norm_num @[optimization_param, reducible]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
nₚ_pos
null
dₚ : Fin nₚ → ℝ := ![1.9501, 1.2311, 1.6068, 1.4860, 1.8913, 1.7621, 1.4565, 1.0185, 1.8214, 1.4447]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
dₚ
null
dₚ_pos : StrongLT 0 dₚ := by intro i; fin_cases i <;> (dsimp [dₚ]; norm_num) @[optimization_param, reducible]
lemma
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
dₚ_pos
null
τminₚ : Fin nₚ → ℝ := ![1.0809, 2.7265, 3.5118, 5.3038, 5.4516, 7.1648, 9.2674, 12.1543, 14.4058, 16.6258] @[optimization_param, reducible]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
τminₚ
null
τmaxₚ : Fin nₚ → ℝ := ![4.6528, 6.5147, 7.5178, 9.7478, 9.0641, 10.3891, 13.1540, 16.0878, 17.4352, 20.9539] @[optimization_param, reducible]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
τmaxₚ
null
sminₚ : Fin nₚ → ℝ := ![0.7828, 0.6235, 0.7155, 0.5340, 0.6329, 0.4259, 0.7798, 0.9604, 0.7298, 0.8405] @[optimization_param, reducible]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
sminₚ
null
smaxₚ : Fin nₚ → ℝ := ![1.9624, 1.6036, 1.6439, 1.5641, 1.7194, 1.9090, 1.3193, 1.3366, 1.9470, 2.8803]
def
CvxLean
[ "import CvxLean" ]
CvxLean/Examples/VehicleSpeedScheduling.lean
smaxₚ
null