Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def zeros(n):
"""Return a zero vector of length n."""
return SparseVector(n, {})
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
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
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
def __neg__(self):
"""Element by element opposite."""
rv = SparseVector(self.n, {})
for k in self.values.keys():
rv[k] = -self[k]
return rv
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
# 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")
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
def isSparseVector(x):
"""Determine if the argument is a SparseVector object."""
return hasattr(x, '__class__') and x.__class__ is SparseVector
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