How to use the nlp.tools.sparse_vector_class.SparseVector function in nlp

To help you get started, we’ve selected a few nlp examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
def zeros(n):
    """Return a zero vector of length n."""
    return SparseVector(n, {})
github PythonOptimizers / NLP.py / nlp / model / amplmodel.py View on Github external
def cost(self):
        """Evaluate sparse cost vector.

        Useful when problem is a linear program.
        Return a sparse vector. This method changes the sign of the cost vector
        if the problem is a maximization problem.
        """
        sc = sv.SparseVector(self.n, self.model.eval_cost())
        if self.scale_obj:
            sc *= self.scale_obj
        if not self.minimize:
            sc *= -1
        return sc
github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
def __pow__(self, other):
        """Raise each element of sparse vector to a power.

        If power is another sparse vector, compute elementwise power.
        In this latter case, by convention, 0^0 = 0.
        """
        if not isSparseVector(self):
            raise TypeError("Argument must be a SparseVector")
        if isSparseVector(other):
            rv = SparseVector(max(self.n, other.n), {})
            for k in self.values.keys():
                rv[k] = self[k]**other[k]
            return rv
        if not isinstance(other, types.IntType) and \
           not isinstance(other, types.LongType) and \
           not isinstance(other, types.FloatType):
                raise TypeError("Power must be numeric or a sparse vector")
        rv = SparseVector(self.n, {})
        for k in self.values.keys():
            rv[k] = math.pow(self[k], other)
        return rv
github PythonOptimizers / NLP.py / nlp / model / amplmodel.py View on Github external
def sigrad(self, i, x):
        """Evaluate sparse gradient of i-th constraint at x.

        Returns a sparse vector representing the sparse gradient
        in coordinate format.
        """
        sci = sv.SparseVector(self.n, self.model.eval_sgi(i, x))
        if isinstance(self.scale_con, np.ndarray):
            sci *= self.scale_con[i]
        return sci
github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
def __neg__(self):
        """Element by element opposite."""
        rv = SparseVector(self.n, {})
        for k in self.values.keys():
            rv[k] = -self[k]
        return rv
github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
def cos(a):
    """Elementwise cosine."""
    if not isSparseVector(a):
        raise TypeError("Argument must be a SparseVector")
    rv = SparseVector(a.n, {})
    for k in a.values.keys():
        rv.values[k] = math.cos(a.values[k])
    return rv
github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
# rv = SparseVector(max(self.n, other.shape[0]), {})
            rv = numpy.zeros(max(self.n, other.shape[0]), 'd')
            for k in range(other.shape[0]):
                rv[k] = other[k]
            for k in self.values.keys():
                rv[k] += self[k]
            return rv
        elif isSparseVector(other):
            rv = SparseVector(max(self.n, other.n), {})
            for k in self.values.keys():
                rv[k] += self[k]
            for k in other.values.keys():
                rv[k] += other[k]
            return rv
        elif operator.isNumberType(other):
            rv = SparseVector(self.n, {})
            for k in self.values.keys():
                rv[k] = self[k] + other
            return rv
        else:
            raise TypeError("Cannot add with SparseVector")
github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
def log10(a):
    """log10 of each element of a."""
    if not isSparseVector(a):
        raise TypeError("Argument must be a SparseVector")
    rv = SparseVector(a.n, {})
    for k in a.values.keys():
        rv.values[k] = math.log10(a.values[k])
    return rv
github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
def isSparseVector(x):
    """Determine if the argument is a SparseVector object."""
    return hasattr(x, '__class__') and x.__class__ is SparseVector
github PythonOptimizers / NLP.py / nlp / tools / sparse_vector_class.py View on Github external
def __rpow__(self, other):
        """Use each element of sparse vector as power of base."""
        if not isSparseVector(self):
            raise TypeError("Argument must be a SparseVector")
        if not isinstance(other, types.IntType) and \
           not isinstance(other, types.LongType) and \
           not isinstance(other, types.FloatType):
                raise TypeError("Power must be numeric")
        rv = SparseVector(self.n, {})
        for k in self.values.keys():
            rv[k] = math.pow(other, self[k])
        return rv