Beispiel #1
0
    def __call__(self, *args):
        while type(args[0]) is list: args = args[0]
        assert len(args) == 2
        
        p = args[0]
        x = args[1]


  
        if p == 0 or p == 1:
            return p
        elif 0 < p < 1:
            if isinstance(x, expr):
                return expr(self, [scalar(p), x])
            else:
                if x<1:
                    raise ValueError('power_pos called with exponent %f < 1' %x)
                return pow(max(p,0), x)
        elif p > 1:
            if isinstance(x, expr):
                return expr(self, [scalar(p), x])
            else:
                if x<1:
                    raise ValueError('power_pos called with exponent %f < 1' %x)
                return pow(max(0,p), x)
        else:
            raise ValueError('power called with negative base %f' %p)
Beispiel #2
0
    def __call__(self, *args):
        while type(args[0]) is list:
            args = args[0]
        assert len(args) == 2

        p = args[0]
        x = args[1]

        if p == 0 or p == 1:
            return p
        elif 0 < p < 1:
            if isinstance(x, expr):
                return expr(self, [scalar(p), x])
            else:
                if x < 1:
                    raise ValueError('power_pos called with exponent %f < 1' %
                                     x)
                return pow(max(p, 0), x)
        elif p > 1:
            if isinstance(x, expr):
                return expr(self, [scalar(p), x])
            else:
                if x < 1:
                    raise ValueError('power_pos called with exponent %f < 1' %
                                     x)
                return pow(max(0, p), x)
        else:
            raise ValueError('power called with negative base %f' % p)
 def __call__(self, *args):
     while type(args[0]) is list: args = args[0]
     assert len(args) == 2
     
     x = args[0]
     y = args[1]
     if isNumber(x) and isNumber(y):
         if y==0: raise ValueError('Division by zero in quadratic over linear')
         return x*x/y
     if isNumber(x): x = scalar(x)
     if isNumber(y): y = scalar(y)
     return expr(self, [x, y])
Beispiel #4
0
 def __init__(self, lhs, relop, rhs):
     assert relop in [EQ, LT, GT]
     if isNumber(lhs): lhs = scalar(lhs)
     if isNumber(rhs): rhs = scalar(rhs)
     if relop == EQ:
         assert lhs.is_affine() and rhs.is_affine()
     if relop == LT:
         assert lhs.is_convex() and rhs.is_concave()
     if relop == GT:
         assert lhs.is_concave() and rhs.is_convex()
     self.lhs = lhs
     self.rhs = rhs
     self.relop = relop
Beispiel #5
0
 def __call__(self, *args):
     while type(args[0]) is list: args = args[0]
     assert len(args) == 2
     
     x = args[0]
     y = args[1]
     
     if isNumber(x) and isNumber(y):
         if x<=0 or y<=0:
             raise ValueError('relative entropy called with negative arguments x = %f , y = %f' % (x,y))
         print (x,y)
         return x*math.log(float(x)/y)
     if isNumber(x): x = scalar(x)
     if isNumber(y): y = scalar(y)
     return expr(self,[x,y])
Beispiel #6
0
 def __init__(self, lhs, relop, rhs):
     assert relop in [EQ, LT, GT]
     if isNumber(lhs):
         lhs = scalar(lhs)
     if isNumber(rhs):
         rhs = scalar(rhs)
     if relop == EQ:
         assert lhs.is_affine() and rhs.is_affine()
     if relop == LT:
         assert lhs.is_convex() and rhs.is_concave()
     if relop == GT:
         assert lhs.is_concave() and rhs.is_convex()
     self.lhs = lhs
     self.rhs = rhs
     self.relop = relop
Beispiel #7
0
    def __call__(self, *args):
        while type(args[0]) is list:
            args = args[0]

        x = args
        n = float(len(args))

        flag = False
        for xi in x:
            if isinstance(xi, expr):
                flag = True
                break
        if not flag:
            for xi in x:
                if xi == 0: return 0
                if xi < 0:
                    raise ValueError('geometric mean called with'
                                     'a vector containing a negative entry')

                return math.exp(sum([math.log(xi) / n for xi in x]))
        y = []
        for i in range(len(x)):
            if isNumber(x[i]): y.append(scalar(x[i]))
            else: y.append(x[i])
        return expr(self, y)
Beispiel #8
0
    def __call__(self, *args):
        while type(args[0]) is list:
            args = args[0]
        assert len(args) == 2

        x = args[0]
        y = args[1]

        if isNumber(x) and isNumber(y):
            if x <= 0 or y <= 0:
                raise ValueError(
                    'relative entropy called with negative arguments x = %f , y = %f'
                    % (x, y))
            print(x, y)
            return x * math.log(float(x) / y)
        if isNumber(x): x = scalar(x)
        if isNumber(y): y = scalar(y)
        return expr(self, [x, y])
Beispiel #9
0
 def __call__(self, *args):
     while type(args[0]) is list: args = args[0]
     assert len(args) == 1 or len(args) == 2
     if len(args) == 1: args.append(1.0)        
     assert isNumber(args[1]) and args[1] >= 0
     
     x = args[0]
     M = args[1]
     if isinstance(x, expr):
         return expr(self, [x, scalar(M)])
     elif math.fabs(x) <= M:
         return math.fabs(x)
     else:
         return (x*x+M*M)/(2.0*M)
Beispiel #10
0
 def __call__(self, *args):
     while type(args[0]) is list: args = args[0]
     
     x = args
     flag = False
     for xi in x:
         if isinstance(xi, expr):
             flag = True
             break
     if not flag: return __builtin__.max(x)
     y = []
     for i in range(len(x)):
         if isNumber(x[i]): y.append(scalar(x[i]))
         else: y.append(x[i])
     return expr(self, y)
Beispiel #11
0
    def __call__(self, *args):
        while type(args[0]) is list:
            args = args[0]
        assert len(args) == 1 or len(args) == 2
        if len(args) == 1: args.append(1.0)
        assert isNumber(args[1]) and args[1] >= 0

        x = args[0]
        M = args[1]
        if isinstance(x, expr):
            return expr(self, [x, scalar(M)])
        elif math.fabs(x) <= M:
            return math.fabs(x)
        else:
            return (x * x + M * M) / (2.0 * M)
 def __call__(self, *args):
     while type(args[0]) is list: args = args[0]
     
     x = args
     flag = False
     for xi in x:
         if isinstance(xi, expr):
             flag = True
             break
     if not flag: return math.log(sum([math.exp(xi) for xi in x]))
     y = []
     for i in range(len(x)):
         if isNumber(x[i]): y.append(scalar(x[i]))
         else: y.append(x[i])
     return expr(self, y)
Beispiel #13
0
 def __call__(self,*args):
     while type(args[0]) is list: args = args[0]
     assert len(args) == 2
     
     x = args[0]
     p = args[1]
     
     if p == 0: return 1;
     if p == 1: return x;
     
     if isinstance(x,expr):
         return expr(self,[x, scalar(p)])
     else:
         if p<1:
             raise ValueError('pow_pos called with exponent %f <1' %p)
         else:
             return pow(max(x,0),p)
Beispiel #14
0
    def __call__(self, *args):
        while type(args[0]) is list:
            args = args[0]
        assert len(args) == 2

        x = args[0]
        p = args[1]

        if p == 0: return 1
        if p == 1: return x

        if isinstance(x, expr):
            return expr(self, [x, scalar(p)])
        else:
            if p < 1:
                raise ValueError('pow_pos called with exponent %f <1' % p)
            else:
                return pow(max(x, 0), p)
Beispiel #15
0
 def __call__(self,*args):
     while type(args[0]) is list: args = args[0]
     
     x = args
     n = float(len(args))
     
     flag = False
     for xi in x:
         if isinstance(xi, expr):
             flag = True 
             break
     if not flag: 
         for xi in x:
             if xi == 0: return 0
             if xi < 0: raise ValueError('geometric mean called with'
                                         'a vector containing a negative entry')
         
             return math.exp(sum([math.log(xi)/n for xi in x]))
     y = []
     for i in range(len(x)):
         if isNumber(x[i]): y.append(scalar(x[i]))
         else: y.append(x[i])
     return expr(self, y)