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 .
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
''' |