Examples

Example Code 1

from polynomial import *

#create instance from matrix and vector/list
A = np.array([[1, 1, 1, 1],[0, 2, 4, 2],[0, 4, 2, 2]])
b = [1, 1, 1, -3]
p1 = Polynomial(A,b)

#create instance from string, string can e.g. be taken from symbolic expression in Matlab or sympy
p2 = Polynomial(str(8*x[0]**6 + 6*x[1]**6 + 4*x[2]**6+2*x[3]**6 -3*x[0]**3*x[1]**2 + \
8*x[0]**2*x[1]*x[2]*x[3] - 9*x[1]*x[3]**4 + 2*x[0]**2*x[1]*x[3] - 3*x[1]*x[3]**2 + 1))

#in strings you can use round brackets, squares brackets or no brackets at all
p2 = Polynomial('8*x(0)**6 + 6*x(1)**6 + 4*x(2)**6+2*x(3)**6 -3*x(0)**3*x(1)**2 +  \
8*x(0)**2*x(1)*x(2)*x(3) - 9*x(1)*x(3)**4 + 2*x(0)**2*x(1)*x(3) - 3*x(1)*x(3)**2 + 1')

#create new random instance: shape, variables, degree, terms
p3 = Polynomial('standard_simplex',30, 60, 100, seed = 0)
p4 = Polynomial('simplex',10, 24, 56, seed = 2)
#general shape has additional input: minimum number of interior points
p5 = Polynomial('general',4,8,8,3,seed = 0)

#run chosen method
p1.sage_opt_python()
p1.sonc_opt_python()
#p1.opt(language = 'matlab') #not tested in a while

#display time and optimum
#should only take a few seconds on a modern machine
p1.local_min(method = 'all')
p1.print_solutions()
print('Optimality gap is %s' % str(p1.gap()))
        

Example Code 2

from polynomial import *

#create instance in 6 variables, degree 8, 50 terms from string
p7 = Polynomial('4.8606528 + 4.98007931*x[5]**8 - 0.60823598*x[4]**2 + 0.64040126*x[4]**2*x[5]**2 - \
1.74887251*x[4]**4*x[5]**4 + 0.56891707*x[4]**6*x[5]**2 + 5.09923373*x[4]**8 + \
8.91133172*x[3]**4*x[4]**2*x[5]**2 + 19.59928099*x[2]**2*x[5]**6 - 0.4999312*x[2]**2*x[4]**2 - \
0.60123437*x[2]**2*x[4]**4 - 0.81918723*x[2]**2*x[3]**2*x[5]**2 + \
1.78938101*x[2]**2*x[3]**2*x[5]**4 + 6.93298735*x[2]**2*x[3]**4 + 0.40554091*x[2]**4*x[4]**2 + \
0.72247756*x[2]**4*x[4]**4 + 10.82830079*x[2]**4*x[3]**2*x[4]**2 + 5.76145671*x[2]**6 + \
3.05858596*x[2]**6*x[3]**2 + 0.73146121*x[1]**2*x[4]**2*x[5]**2 - \
0.14006601*x[1]**2*x[4]**2*x[5]**4 + 10.06180744*x[1]**2*x[3]**2*x[4]**2 + \
4.45514164*x[1]**2*x[3]**2*x[4]**4 + 2.38069177*x[1]**2*x[2]**2*x[4]**2 + \
2.0609944*x[1]**2*x[2]**2*x[3]**2*x[5]**2 - 0.82451163*x[1]**4*x[4]**2*x[5]**2 + \
5.01527598*x[1]**4*x[2]**2*x[5]**2 + 1.81152102*x[1]**6*x[5]**2 - 0.10641543*x[0]**2*x[4]**4 - \
0.3266257*x[0]**2*x[3]**2*x[5]**2 + 0.19438246*x[0]**2*x[3]**2*x[4]**2 + \
0.71140485*x[0]**2*x[2]**2*x[4]**2 - 0.87110082*x[0]**2*x[2]**2*x[4]**4 + \
1.78143307*x[0]**2*x[2]**4*x[3]**2 - 0.75089195*x[0]**2*x[1]**2*x[4]**2 - \
0.02685681*x[0]**2*x[1]**2*x[3]**2 + 0.77251537*x[0]**2*x[1]**4*x[5]**2 + \
3.80814932*x[0]**2*x[1]**4*x[3]**2 + 0.04414057*x[0]**4 - 0.66146295*x[0]**4*x[4]**2*x[5]**2 + \
0.97435634*x[0]**4*x[3]**2 - 1.59452749*x[0]**4*x[3]**2*x[5]**2 + 2.82933911*x[0]**4*x[3]**4 + \
7.85764346*x[0]**4*x[2]**4 - 0.50829871*x[0]**4*x[1]**2 - 0.17537673*x[0]**4*x[1]**2*x[4]**2 + \
8.208759*x[0]**4*x[1]**4 + 0.4513008*x[0]**6 - 1.35222538*x[0]**6*x[1]**2 + 3.33034839*x[0]**8')

#solve via SOS
#this may take >1 minute
p7.sos_opt_matlab()

#solve via SONC
#should not take more than a few seconds
p7.sonc_opt_python()

#compute upper bound on optimum
p7.local_min(method = 'sonc')

#display time and solution
p7.print_solutions()
print('Optimality gap is %s' % str(p7.gap()))
        

Example Code 3

        from polynomial import *

#create instance in 20 variables, degree 6, 100 terms from string
p6 = Polynomial('1.0274876905315602 + 4.010101897906409*x(17)^4*x(19)^2 + \
7.765042386523891*x(16)^4*x(18)^2 + 0.4217442170306943*x(15)^2 + 3.82728337945768*x(15)^4*x(18)^2 + \
1.9519976244589623*x(13)^2*x(15)^2*x(19)^2 + 0.6678700958294899*x(13)^2*x(14)^2 + \
6.686501678333187*x(12)^2*x(17)^2*x(18)^2 + 6.412501712061847*x(12)^2*x(13)^2 + \
1.5002497239270731*x(11)^2*x(17)^2*x(19)^2 + 1.9119387644440253*x(11)^2*x(16)^2*x(18)^2 + \
4.875692984906463*x(11)^2*x(12)^2*x(16)^2 + 1.7494675848013919*x(11)^6 + \
2.0882219421451618*x(10)^2*x(16)^2 + 2.0178479384217027*x(10)^2*x(14)^2*x(17)^2 + \
0.13502755465342828*x(9)^2*x(19)^4 + 2.854844247228621*x(9)^2*x(10)^2*x(11)^2 + \
7.845950122517287*x(8)^2*x(11)^2*x(18)^2 + 7.010016680800461*x(8)^2*x(11)^4 + \
4.710857493597293*x(8)^2*x(10)^2 + 0.595690197183314*x(8)^2*x(9)^2*x(10)^2 + \
3.7826505745121723*x(8)^4*x(13)^2 + 7.4224692424986705*x(7)^2*x(16)^2*x(18)^2 + \
3.2673801241868357*x(7)^2*x(10)^2*x(16)^2 + 9.40494558093421*x(7)^2*x(9)^2 + \
4.614152146494045*x(7)^2*x(8)^2*x(13)^2 + 3.7886760793462333*x(7)^2*x(8)^2*x(9)^2 + \
7.678056936883737*x(6)^2*x(17)^4 + 6.597275205498519*x(6)^2*x(9)^2*x(14)^2 + \
1.7087592891300343*x(5)^2*x(17)^2 + 7.343200726713821*x(5)^2*x(14)^2*x(19)^2 + \
2.60794956901849*x(5)^2*x(13)^2*x(14)^2 + 1.5295702340837878*x(5)^2*x(12)^2 + \
10.300164002489936*x(5)^2*x(12)^2*x(15)^2 + 9.183718990796676*x(5)^2*x(9)^2*x(12)^2 + \
2.9614507699256407*x(5)^2*x(6)^2*x(19)^2 + 3.2372519829832775*x(5)^2*x(6)^2*x(7)^2 + \
6.6939652568967665*x(4)^2*x(15)^2*x(18)^2 + 3.708743652251333*x(4)^2*x(11)^2*x(18)^2 + \
4.574324010902654*x(4)^2*x(5)^2*x(11)^2 + 4.219747843829404*x(4)^4*x(15)^2 + \
4.915700834236318*x(4)^4*x(12)^2 + 2.6385591241462842*x(4)^4*x(10)^2 + \
5.006404471924069*x(3)^2*x(18)^2*x(19)^2 + 2.3382091333977035*x(3)^2*x(11)^2*x(18)^2 + \
8.361894509718216*x(3)^2*x(8)^2*x(9)^2 + 4.838206203859331*x(3)^2*x(6)^2 + \
0.48826072694261474*x(3)^2*x(5)^4 + 1.3745462467297949*x(3)^4*x(15)^2 + 2.544915817402945*x(3)^6 + \
11.406549089469664*x(2)^2*x(19)^4 + 9.110758749753877*x(2)^2*x(10)^2*x(13)^2 + \
4.218473105525957*x(2)^2*x(9)^2*x(19)^2 + 3.626867410922485*x(2)^2*x(6)^2*x(11)^2 + \
6.667013578131863*x(2)^2*x(6)^2*x(9)^2 + 2.8158330782883407*x(2)^2*x(6)^4 + \
9.231384183504932*x(2)^2*x(5)^2*x(19)^2 + 3.245065587786261*x(2)^2*x(3)^2*x(16)^2 + \
9.570452616524584*x(2)^2*x(3)^2*x(9)^2 + 4.348223199867024*x(2)^4*x(4)^2 + \
5.289927947676239*x(1)^2*x(16)^2*x(17)^2 - 0.485617687780263*x(1)^2*x(15)^2 + \
2.517456967171004*x(1)^2*x(14)^2*x(18)^2 + 3.956721208627563*x(1)^2*x(11)^2*x(15)^2 + \
2.153392403957681*x(1)^2*x(11)^2*x(14)^2 + 1.1707832253567307*x(1)^2*x(10)^2 + \
1.8470636950204162*x(1)^2*x(9)^2 + 0.9527245760972043*x(1)^2*x(9)^2*x(19)^2 + \
1.1753058002393444*x(1)^2*x(8)^2*x(14)^2 + 3.628088813303432*x(1)^2*x(6)^2 + \
0.831164416730436*x(1)^2*x(5)^2*x(18)^2 - 1.725449703149502*x(1)^2*x(4)^2*x(15)^2 + \
12.487993413479614*x(1)^2*x(4)^2*x(5)^2 + 5.573096100684647*x(1)^2*x(3)^2*x(17)^2 + \
5.456530621819992*x(1)^4*x(15)^2 + 0.3128075878695594*x(1)^4*x(11)^2 + \
0.6624221483017674*x(1)^4*x(5)^2 + 2.8954837054038935*x(1)^4*x(4)^2 + \
5.017548777611953*x(0)^2*x(17)^4 + 5.7048332599223714*x(0)^2*x(13)^2*x(19)^2 + \
9.200653124232273*x(0)^2*x(12)^2*x(16)^2 + 2.274641041498022*x(0)^2*x(11)^2*x(12)^2 + \
2.767877114180972*x(0)^2*x(11)^4 + 10.516007485623257*x(0)^2*x(10)^2*x(17)^2 + \
3.762515884283517*x(0)^2*x(9)^2*x(16)^2 + 0.9375126216071634*x(0)^2*x(8)^2 + \
1.3720113118183557*x(0)^2*x(6)^2*x(18)^2 + 0.17027241346247876*x(0)^2*x(6)^2*x(16)^2 + \
1.5442580633713991*x(0)^2*x(5)^2 + 0.8986071007410801*x(0)^2*x(5)^2*x(6)^2 + \
2.669557933521314*x(0)^2*x(5)^4 + 9.253211671212693*x(0)^2*x(4)^2*x(12)^2 + \
0.5681493678493919*x(0)^2*x(2)^2 + 2.1712413475008656*x(0)^2*x(2)^2*x(19)^2 + \
3.4737368700335995*x(0)^2*x(2)^2*x(18)^2 + 2.3990695035532856*x(0)^2*x(1)^2*x(11)^2 + \
4.65510964231864*x(0)^2*x(1)^2*x(10)^2 + 12.862788969217956*x(0)^4*x(16)^2 + \
2.058729571429864*x(0)^4*x(6)^2 + 2.2993487525883327*x(0)^4*x(5)^2')

#attempt to solve via SOS
#this may take >15 minutes on a modern machine or not be computable at all
p6.sos_opt_matlab()

#attempt to solve via SONC
#should not take more than a few seconds
p6.sonc_opt_python()

#compute upper bound on optimum
p6.local_min(method = 'sonc')

#display time and solution
p6.print_solutions()
print('Optimality gap is %s' % str(p6.gap()))