sparse_vector_class.py 文件源码

python
阅读 43 收藏 0 点赞 0 评论 0

项目:NLP.py 作者: PythonOptimizers 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号