def test_basic(self): import cvxopt a = cvxopt.matrix([1.0, 2.0, 3.0]) assert list(a) == [1.0, 2.0, 3.0] b = cvxopt.matrix([3.0, -2.0, -1.0]) c = cvxopt.spmatrix([1.0, -2.0, 3.0], [0, 2, 4], [1, 2, 4], (6, 5)) d = cvxopt.spmatrix([1.0, 2.0, 5.0], [0, 1, 2], [0, 0, 0], (3, 1)) e = cvxopt.mul(a, b) self.assertEqualLists(e, [3.0, -4.0, -3.0]) self.assertAlmostEqualLists(list(cvxopt.div(a, b)), [1.0 / 3.0, -1.0, -3.0]) self.assertAlmostEqual(cvxopt.div([1.0, 2.0, 0.25]), 2.0) self.assertEqualLists(list(cvxopt.min(a, b)), [1.0, -2.0, -1.0]) self.assertEqualLists(list(cvxopt.max(a, b)), [3.0, 2.0, 3.0]) self.assertEqual(cvxopt.max([1.0, 2.0]), 2.0) self.assertEqual(cvxopt.max(a), 3.0) self.assertEqual(cvxopt.max(c), 3.0) self.assertEqual(cvxopt.max(d), 5.0) self.assertEqual(cvxopt.min([1.0, 2.0]), 1.0) self.assertEqual(cvxopt.min(a), 1.0) self.assertEqual(cvxopt.min(c), -2.0) self.assertEqual(cvxopt.min(d), 1.0) self.assertEqual(len(c.imag()), 0) with self.assertRaises(OverflowError): cvxopt.matrix(1.0, (32780 * 4, 32780)) with self.assertRaises(OverflowError): cvxopt.spmatrix(1.0, (0, 32780 * 4), (0, 32780)) + 1
def test_basic(self): import cvxopt a = cvxopt.matrix([1.0,2.0,3.0]) b = cvxopt.matrix([3.0,-2.0,-1.0]) c = cvxopt.spmatrix([1.0,-2.0,3.0],[0,2,4],[1,2,4],(6,5)) d = cvxopt.spmatrix([1.0,2.0,5.0],[0,1,2],[0,0,0],(3,1)) self.assertEqualLists(list(cvxopt.mul(a,b)),[3.0,-4.0,-3.0]) self.assertAlmostEqualLists(list(cvxopt.div(a,b)),[1.0/3.0,-1.0,-3.0]) self.assertAlmostEqual(cvxopt.div([1.0,2.0,0.25]),2.0) self.assertEqualLists(list(cvxopt.min(a,b)),[1.0,-2.0,-1.0]) self.assertEqualLists(list(cvxopt.max(a,b)),[3.0,2.0,3.0]) self.assertEqual(cvxopt.max([1.0,2.0]),2.0) self.assertEqual(cvxopt.max(a),3.0) self.assertEqual(cvxopt.max(c),3.0) self.assertEqual(cvxopt.max(d),5.0) self.assertEqual(cvxopt.min([1.0,2.0]),1.0) self.assertEqual(cvxopt.min(a),1.0) self.assertEqual(cvxopt.min(c),-2.0) self.assertEqual(cvxopt.min(d),1.0)
def F(x=None, z=None): if x is None: x0 = cv.matrix([(1.-float(k)/n)/(n-1)]*n*n,(n*n,1)) for j in range(n): x0[j*n+j] = float(k)/n return (0,x0) if cv.min(x) < 0.: return None f = coef*x**h Df = cv.mul(h*coef,(x.T)**(h-1)) if z is None: return (f,Df) H = cv.spdiag(cv.mul(z[0]*h*(h-1)*coef.T,x**(h-2))) return (f,Df,H)
def test_basic(self): import cvxopt a = cvxopt.matrix([1.0,2.0,3.0]) b = cvxopt.matrix([3.0,-2.0,-1.0]) c = cvxopt.spmatrix([1.0,-2.0,3.0],[0,2,4],[1,2,4],(6,5)) d = cvxopt.spmatrix([1.0,2.0,5.0],[0,1,2],[0,0,0],(3,1)) self.assertEqualLists(list(cvxopt.mul(a,b)),[3.0,-4.0,-3.0]) self.assertAlmostEqualLists(list(cvxopt.div(a,b)),[1.0/3.0,-1.0,-3.0]) self.assertAlmostEqual(cvxopt.div([1.0,2.0,0.25]),2.0) self.assertEqualLists(list(cvxopt.min(a,b)),[1.0,-2.0,-1.0]) self.assertEqualLists(list(cvxopt.max(a,b)),[3.0,2.0,3.0]) self.assertEqual(cvxopt.max([1.0,2.0]),2.0) self.assertEqual(cvxopt.max(a),3.0) self.assertEqual(cvxopt.max(c),3.0) self.assertEqual(cvxopt.max(d),5.0) self.assertEqual(cvxopt.min([1.0,2.0]),1.0) self.assertEqual(cvxopt.min(a),1.0) self.assertEqual(cvxopt.min(c),-2.0) self.assertEqual(cvxopt.min(d),1.0) with self.assertRaises(OverflowError): cvxopt.matrix(1.0,(32780*4,32780)) with self.assertRaises(OverflowError): cvxopt.spmatrix(1.0,(0,32780*4),(0,32780))+1
def KL(ww=None, zz=None): if ww is None: return 0, cvx.matrix(1., (self.k, 1)) if cvx.min(ww) <= 0.: return None xx = cvx.matrix(x.T) HHww = HH * ww kl = sum( cvx.mul(xx, cvx.log(cvx.div(xx, HHww)) + HHww - xx)) Dkl = (ep.T - cvx.div(xx.T, HHww.T)) * HH if zz is None: return kl, Dkl else: D2kl = HH.T * cvx.spdiag( cvx.div(zz[0] * xx, HHww**2)) * HH return kl, Dkl, D2kl
def estimate_range(A, xl, xu): ''' Estimate the range of Ax, where xl <= x <= xu Mathematically, the lower and upper bounds are: ''' # Decide if A is sparse # if 'sp' in str(type(A)) or 'sparse' in str(type(A)): # pass # Efficient, exploit the sparsity of A Apos = copy(A) Aneg = copy(A) # For sparse matrices Apos.V = cvxopt.max(Apos.V, 0) Aneg.V = cvxopt.min(Aneg.V, 0) # pdb.set_trace() bl = Apos * xl + Aneg * xu bu = Apos * xu + Aneg * xl # Relatively efficient, but dense matrix means overhead # bl = cvxopt.min(A,0)*xu + cvxopt.max(A,0)*xl # bu = cvxopt.min(A,0)*xl + cvxopt.max(A,0)*xu # This version is less efficient # row, col = A.size # bl = matrix(10.0,size=(row,1)) # bu = matrix(10.0,size=(row,1)) # for i in range(row): # for j in range(col): # if A[i,j] > 0.0: # bl[i] += A[i,j]*xl[j] # bu[i] += A[i,j]*xu[j] # elif A[i] < 0.0: # print A.size, xl.size, xu.size, i,j # bl[i] += A[i,j]*xu[j] # bu[i] += A[i,j]*xl[j] return (bl, bu)