我意识到这np.power(a, b)比以前慢np.exp(b * np.log(a)):
import numpy as np
a, b = np.random.random((2, 100000))
%timeit np.power(a, b) # best of 3: 4.16 ms per loop
%timeit np.exp(b * np.log(a)) # best of 3: 1.74 ms per loop
Run Code Online (Sandbox Code Playgroud)
结果是相同的(具有1e-16阶的几个数值误差).
还做了哪些额外的工作np.power?此外,我如何自己找到这些问题的答案?
(我来自Python,这就是为什么我的问题具有Python风格)
我有一个矩阵x( str(x)-> num[1:1000,1:4]) 和一个向量y( str(y) -> num[1:4])。我想从x中 coreespoonding 条目的每一列中减去y。IE x_y[i] = x[,i]-y[i]。
我发现这样做的方法是t(t(x)-y),但在我看来这是一种相当神秘的方法。有没有其他对读者更友好的方法来做到这一点?
对于那些了解 python 的人来说:我本质上是在寻找一种与 中已知的类似的广播方式,它可以通过等numpy来塑造维度。np.newaxis
我有一组f_t具有多个根的函数(实际上是两个)。我想找到“第一个”根,并且在fsolve大多数情况下都可以正常工作。问题是,随着 t 趋于无穷大,两个根会收敛。(我的功能的一个简单例子是f_t(x) = x^2 - 1/t)。所以越大t,犯的错误就越多fsolve。是否有一个预定义的函数,类似于fsolve我可以告诉它应该只在给定范围内查找的函数(例如,始终在[0, inf)中查找根)。
该问题与https://mathematica.stackexchange.com/questions/91784/how-to-find-numerically-all-roots-of-a-function-in-a-given-range?noredirect=1&lq基本相同=1,但是有 Mathematica 的答案,我想要它们在 Python 中。
PS:我现在如何编写自己的算法,但由于这些算法往往比内置函数慢,我希望找到一个可以实现相同功能的内置函数。特别是我读过这篇文章Find root of a function in a given interval
我想找到微分方程的平衡点,并检查平衡点是否稳定.
这是一个最小的工作示例
import numpy as np
from scipy.optimize import fsolve
dim = 2
A = np.random.uniform(size = (dim,dim))
sol, infodict, ier, mesg = fsolve(lambda x: 1-np.dot(A,x),
np.ones(dim), full_output = True)
Run Code Online (Sandbox Code Playgroud)
为了找出解sol是否稳定,雅可比行列式的所有特征值必须具有负实部.然而,雅可比未保存在中infodict,而QR分解则保存在infodict.
如何从QR分解中获得Jacoian fsolve?
我能做的就像是
eigenvalues = np.linalg.eigvals(infodict["fjac"])*infodict["r"][ind]
Run Code Online (Sandbox Code Playgroud)
ind对角线条目在哪里r,但我怀疑这是最好的方式.
我想要一个易于读取访问多维numpy数组的某些部分.对于任何访问第一个维度的数组都是easy(b[index]).另一方面,访问第六维是"硬"(特别是阅读).
b[:,:,:,:,:,index] #the next person to read the code will have to count the :
Run Code Online (Sandbox Code Playgroud)
有一个更好的方法吗?特别是有一种方法,在编写程序时轴是不知道的吗?
编辑:索引维度不一定是最后一个维度
我想“创造性地”设置我的随机种子。这就像np.random.seed(42)读过《银河系漫游指南》的人会明白这个笑话。
不过,我希望能够用字符串(例如引文)开始随机种子。
这就像np.random.seed(str_to_int("I like cake")),为此我需要一个函数,将整数赋予字符串。
我愿意在我的字符串中只允许字母(如果必须是小写字母),如果这使任务更容易的话。该函数不一定是随机的,但除了常量 0 函数之外的其他函数就更好了。
ufunc文档指出:
在哪里
1.7 版本中的新功能。接受与操作数一起广播的布尔数组。True 值表示计算该位置的 ufunc,False 值表示将值保留在输出中。
out什么时候没有给出默认行为?
我观察到一些行为,这对我来说并没有真正意义:
import numpy as np
a,b = np.ones((2,2))
np.add(a,b,where = False) #returns 0
np.exp(a, where = False) #returns 1
np.sin(a, where = False) #returns 1
np.sign(a, where = False) #returns 0
np.reciprocal(a, where = False) #returns 0
Run Code Online (Sandbox Code Playgroud)
有谁知道根本原因/行为?特别np.reciprocal是没有意义,因为倒数永远不可能是 0
编辑:行为更加复杂:
a,b = np.ones(2)
np.add(a,b,where = False) #returns 6.0775647498958414e-316
a,b = 1,1
np.add(a,b, where = False) #returns 12301129,
#running this line several times doesn't give the same result …Run Code Online (Sandbox Code Playgroud) Given a two vectors x and y I can use scipy.interpolate.interp1d to compute the (quadratic) spline. However I'm not directly interested in the spline itself, but rather in the derivative of the spline.
I would prefer having an explicit solution rather than a numerical derivative.
但是我找不到多项式参数存储在哪里interp1d。我尝试过interp1d.__dict__,其中包含interp1d._spline但我没有找到该参数的定义是什么。