小编Woj*_*chR的帖子

避免日志溢出(cosh(x))

我的模拟需要实现

np.log(np.cosh(x))
Run Code Online (Sandbox Code Playgroud)

这会溢出 large x,即我收到RuntimeWarning: overflow encountered in cosh警告。原则上,随着对数减少所讨论的数字,在 的某个范围内xcosh应该溢出而不log(cosh())应该溢出。

在 NumPy 中是否有任何解决方案,例如在精神上与np.log1p()功能相似?

提供更多信息:我知道一个可能的解决方案可能是符号使用 SymPy https://github.com/sympy/sympy/issues/12671 但是模拟应该很快,并且符号计算 AFAIK 可能会显着减慢它的速度。

python numpy logarithm numeric hyperbolic-function

5
推荐指数
1
解决办法
904
查看次数

查找与给定相似的所有向量的快速方法

通过“相似向量”,我定义了一个向量,该向量在一个位置与给定的一个相差-1或1。但是,如果给定一个的元素为零,则仅相差1即可。例子:

similar_vectors(np.array([0,0,0]))

array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.]])


similar_vectors(np.array([1,0,2,3,0,0,1]))

array([[ 0.,  0.,  2.,  3.,  0.,  0.,  1.],
       [ 2.,  0.,  2.,  3.,  0.,  0.,  1.],
       [ 1.,  1.,  2.,  3.,  0.,  0.,  1.],
       [ 1.,  0.,  1.,  3.,  0.,  0.,  1.],
       [ 1.,  0.,  3.,  3.,  0.,  0.,  1.],
       [ 1.,  0.,  2.,  2.,  0.,  0.,  1.],
       [ 1.,  0.,  2.,  4.,  0.,  0.,  1.],
       [ 1.,  0.,  2.,  3.,  1.,  0.,  1.],
       [ …
Run Code Online (Sandbox Code Playgroud)

python optimization numpy numeric numerical-methods

4
推荐指数
1
解决办法
165
查看次数