⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleatoms.py

📁 CVXMOD is a Python-based tool for expressing and solving convex optimization problems.
💻 PY
📖 第 1 页 / 共 2 页
字号:
        self.cols = c    def getparams(self):        return getparams(self.args)    def epiorhypo(self):        # doesn't handle matrices at the moment.        t = dummyvar(size(self))                s = stdstruct()        for x in self.args:            if equiv(size(x), size(self)):                s += (x >= ones(size(x))*t).split()            else:                # jem: look into whether this is quite what we want?                s += (x*ones(size(self)) >= ones(size(x))*t).split()        return (t, s)    def _getvalue(self):        return min(*value(self.args))    value = property(_getvalue)    def _getnegative(self):        for x in self.args:            if ispos(x):                return True        else:            return False    negative = property(_getnegative)    def _getpositive(self):        for x in self.args:            if not ispos(x):                return True        else:            return False    positive = property(_getpositive)    def _getconcave(self):        for x in self.args:            if not isconcave(x):                return False        else:            return True    concave = property(_getconcave)    def _getincreasing(self):        for x in self.args:            if not isincreasing(x):                return False        else:            return True    increasing = property(_getincreasing)    def _getdecreasing(self):        for x in self.args:            if not isdecreasing(x):                break        else:            return True    decreasing = property(_getdecreasing)def min(*args):    #"""Maximum element of a vector."""    # need to upgrade this to handle things like min(matrix((1,2)), matrix((2,3))).    if len(args) > 1:        if not getoptvars(args) and not getparams(args):            r = __builtin__.min([rows(x) for x in args])            c = __builtin__.min([cols(x) for x in args])            if (r,c) == (1,1):                return __builtin__.min(args)            args = list(args)            for i in xrange(len(args)):                if size(args[i]) == (1,1):                    args[i] = args[i]*matrix(ones(r,c))                elif size(args[i]) != (r,c):                    raise AtomArgsError('incompatible arguments to min')            z = zeros(r, c)            for i in range(r*c):                z[i] = __builtin__.min([x[i] for x in args])            return z        else:            return multiargminfunction(args)    else:        arg = args[0]        if iterable(arg):            return min(*arg)        elif isinstance(arg, (int, float)):            return arg        elif not getoptvars(arg) and not getparams(arg):            return __builtin__.min(arg)        elif is1x1(arg):            return arg        else:            return singleargminfunction(arg)        class norm1function(function, convex, positive):    name = 'norm1'    def __init__(self, arg):        self.arg = arg        self.rows = 1        self.cols = 1    def _getvalue(self):        return sum(abs(value(self.arg)))    value = property(_getvalue)    def epiorhypo(self):        t = dummyvar(1, 1)        (t2, s) = abs(self.arg).epiorhypo()        s += (t == sum(t2)).split()        return (t, s)    def _getinc(obj):        if ispos(obj.arg): # function is inc.            return isincreasing(obj.arg)        elif isneg(obj.arg): # function is dec.            return isdecreasing(obj.arg)        else:            return False    inc = property(_getinc)    def _getdec(obj):        if ispos(obj.arg): # function is inc.            return isdecreasing(obj.arg)        elif isneg(obj.arg): # function is dec.            return isincreasing(obj.arg)        else:            return False    dec = property(_getdec)    def cvx(self):        return 'norm(%s, 1)' % str(self.arg)def norm1(obj):    if isinstance(obj, (int, float, matrix, spmatrix)):        return sum(abs(obj))    else:        # jem. this (and possibly other situations) will be problematic with        # params. need some kind of conditional comparison / put on stack        # for later assertion.        if cols(obj) > 1:            raise DimError('norm1 arg must be a column vector')        else:            return norm1function(obj)class norminffunction(function, convex, positive):    name = 'norminf'    def __init__(self, arg):        self.arg = arg        self.rows = 1        self.cols = 1    def _getvalue(self):        return max(abs(value(self.arg)))    value = property(_getvalue)    def epiorhypo(self):        t = dummyvar(1, 1)        (abst, s) = abs(self.arg).epiorhypo()        (maxt, s2) = max(abst).epiorhypo()        s += s2        s += (t >= maxt).split()        return (t, s)    def _getinc(obj):        if ispos(obj.arg): # function is inc.            return isincreasing(obj.arg)        elif isneg(obj.arg): # function is dec.            return isdecreasing(obj.arg)        else:            return False    inc = property(_getinc)    def _getdec(obj):        if ispos(obj.arg): # function is inc.            return isdecreasing(obj.arg)        elif isneg(obj.arg): # function is dec.            return isincreasing(obj.arg)        else:            return False    dec = property(_getdec)    def cvx(self):        return 'norm(%s, inf)' % str(self.arg)def norminf(obj):    if isinstance(obj, (int, float, matrix, spmatrix)):        return max(abs(obj))    else:        # jem. this (and possibly other situations) will be problematic with        # params. need some kind of conditional comparison / put on stack        # for later assertion.        if cols(obj) > 1:            raise DimError('norminf arg must be a column vector')        else:            return norminffunction(obj)class posfunction(function, elementwise, convex, increasing, positive):    """The positive component of an argument."""    name ='pos'    def __init__(self, arg):        self.arg = arg        self.rows = rows(arg)        self.cols = cols(arg)    def getparams(self):        return getparams(self.arg)    def epiorhypo(self):        # doesn't handle matrices at the moment.        t = dummyvar(size(self.arg))        s = stdstruct()        s += (t >= 0).split()        s += (t >= self.arg).split()        return (t, s)    def _getvalue(self):        return pos(value(self.arg))    value = property(_getvalue)def pos(obj):    if isinstance(obj, (int, float)):        return __builtin__.max(obj, 0)    elif isinstance(obj, (matrix, spmatrix)):        (r, c) = size(obj)        z = zeros(r, c, full=True)        for i in xrange(r*c):            z[i] = __builtin__.max(obj[i], 0)        return z    elif isneg(obj):        return zeros(size(obj))    elif ispos(obj):        return obj    else:        return posfunction(obj)class negfunction(function, elementwise, concave, increasing, negative):    """The negative component of an argument."""    name ='neg'    def __init__(self, arg):        self.arg = arg        self.rows = rows(arg)        self.cols = cols(arg)    def epiorhypo(self):        # doesn't handle matrices at the moment.        t = dummyvar(size(self.arg))        s = stdstruct()        s += (t <= 0).split()        s += (t <= self.arg).split()        return (t, s)    def _getvalue(self):        return neg(value(self.arg))    value = property(_getvalue)def neg(obj):    if isinstance(obj, (int, float)):        return __builtin__.min(obj, 0)    elif isinstance(obj, (matrix, spmatrix)):        (r, c) = size(obj)        z = zeros(r, c)        for i in xrange(r*c):            z[i] = __builtin__.min(obj[i], 0)        return z    elif isneg(obj):        return obj    elif ispos(obj):        return zeros(size(obj))    else:        return negfunction(obj)class maxeigfunction(matrixfunction, convex, increasing):    """The maximum eigenvalue of a matrix."""    # jem: do know some stuff about positivity / negativity here.    name ='maxeig'    def __init__(self, arg):        if not equiv(rows(arg), cols(arg)):            raise DimError("cannot find maximum eigenvalue of non-square object")        self.arg = arg        self.rows = 1        self.cols = 1    def epiorhypo(self):        # doesn't handle matrices at the moment.        t = dummyvar()        s = stdstruct()        if not issymm(self.arg):            xs = dummyvar(size(self.arg), symm=True)            s += (xs == self.arg).split()            s += (xs |lt| t*eye(rows(self.arg), True)).split()        else:            s += (self.arg |lt| t*eye(rows(self.arg), True)).split()        return (t, s)    def _getvalue(self):        return maxeig(value(self.arg))    value = property(_getvalue)    def cvx(self):        return "lambda_max(%s)" % str(self.arg)def maxeig(obj):    if isinstance(obj, (int, float, matrix, spmatrix)):        return _maxeignum(obj)    else:        return maxeigfunction(obj)class mineigfunction(matrixfunction, concave, increasing):    """The minimum eigenvalue of a matrix."""    # jem: do know some stuff about positivity / negativity here.    name ='mineig'    def __init__(self, arg):        if not equiv(rows(arg), cols(arg)):            raise DimError("cannot find minimum eigenvalue of non-square object")        self.arg = arg        self.rows = 1        self.cols = 1    def epiorhypo(self):        # doesn't handle matrices at the moment.        t = dummyvar()        s = stdstruct()        if not issymm(self.arg):            xs = dummyvar(size(self.arg), symm=True)            s += (xs == self.arg).split()            s += (xs |gt| t*eye(rows(self.arg), True)).split()        else:            s += (self.arg |gt| t*eye(rows(self.arg), True)).split()        return (t, s)    def _getvalue(self):        return mineig(value(self.arg))    value = property(_getvalue)    def cvx(self):        return "lambda_min(%s)" % str(self.arg)def mineig(obj):    if isinstance(obj, (int, float, matrix, spmatrix)):        return _mineignum(obj)    else:        return mineigfunction(obj)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -