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
评论列表
文章目录