Processing math: 100%

Friday, March 14, 2025

Semidefinite o-pi-timality

Happy \pi Day! If other methods fail, you can always compute \pi with the MOSEK semidefinite optimizer. We leave the details as an interesting exercise for the curious readers. Some hints are hidden in our Modeling Cookbook .

from mosek.fusion import *
import mosek.fusion.pythonic
import numpy as np
def showmepi(d, m):
A = np.eye(m, k = 1) - np.eye(m)
with Model() as M:
S = M.variable(Domain.inPSDCone(m))
M.constraint(Expr.dot(S, np.eye(m)) == 1)
M.objective(ObjectiveSense.Maximize, Expr.dot(S, A + A.T))
M.solve()
print(f'{np.sqrt(-M.primalObjValue())*(m+1):.{d}f}')
for d, m in enumerate([5, 20, 200, 400, 600, 1600]):
showmepi(d + 1, m)
'''
output:
3.1
3.14
3.142
3.1416
3.14159
3.141592
'''
view raw showmepi.py hosted with ❤ by GitHub