让A是一个(N,M,M)矩阵(N非常大),我想计算scipy.linalg.expm(A[n,:,:])每个n in range(N)。我当然可以只使用for循环,但我想知道是否有一些技巧可以以更好的方式做到这一点(例如np.einsum)。
我对矩阵求逆等其他操作也有同样的问题(在评论中解决了求逆问题)。
我正在使用以下标准代码:
# importing required modules
import PyPDF2
def PDFmerge(pdfs, output):
# creating pdf file merger object
pdfMerger = PyPDF2.PdfFileMerger()
# appending pdfs one by one
for pdf in pdfs:
with open(pdf, 'rb') as f:
pdfMerger.append(f)
# writing combined pdf to output pdf file
with open(output, 'wb') as f:
pdfMerger.write(f)
def main():
# pdf files to merge
pdfs = ['example.pdf', 'rotated_example.pdf']
# output pdf file name
output = 'combined_example.pdf'
# calling pdf merge function
PDFmerge(pdfs = pdfs, output = output)
if …Run Code Online (Sandbox Code Playgroud) 我有以下最小示例:
a = np.zeros((5,5,5))
a[1,1,:] = [1,1,1,1,1]
print(a[1,:,range(4)])
Run Code Online (Sandbox Code Playgroud)
我期望输出一个 5 行 4 列的数组,其中第二行有一个数组。相反,它是一个 4 行 5 列的数组,其中第二列为 5 列。这里发生了什么,我该怎么做才能得到我期望的输出?
我有一个函数f(x),其中创建了许多局部变量。x是与这些局部变量之一同名的字符串,我想通过更改x. 什么是干净的方法来做到这一点?目前我使用了很多if/elif语句。
一些虚拟代码来代表我的问题:
def f(x):
a = [1,2,3]
b = [2,3,4]
c = [3,4,5]
if x == "a":
a[0] = 10
elif x == "b":
b[0] = 10
elif x == "c":
c[0] = 10
return a,b,c
Run Code Online (Sandbox Code Playgroud)
我想要正确的变量来改变值,但使用所有这些 if/elif 语句感觉有点多余。