- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在这里定义了计算矩阵行列式的函数。但有时我得到错误的标志。我从 this answer 为我的函数建模.
from scipy.linalg.cython_lapack cimport dgetrf
cpdef double det_c(double[:, ::1] A, double[:, ::1] work, double[::1] ipiv):
'''obtain determinant of float type square matrix A
Notes
-----
As is, this function is not yet computing the sign of the determinant
correctly, help!
Parameters
----------
A : memoryview (numpy array)
n x n array to compute determinant of
work : memoryview (numpy array)
n x n array to use within function
ipiv : memoryview (numpy array)
length n vector use within function
Returns
-------
detval : float
determinant of matrix A
'''
cdef int n = A.shape[0], info
work[...] = A
dgetrf(&n, &n, &work[0,0], &n, &ipiv[0], &info)
cdef double detval = 1.
cdef int j
for j in range(n):
if j != ipiv[j]:
detval = -detval*work[j, j]
else:
detval = detval*work[j, j]
return detval
当我测试此功能并将其与 np.linalg.det
进行比较时,有时我会得到错误的符号。
>>> a = np.array([[1,2],[3,5.]])
>>> np.linalg.det(a)
>>> -1.0000000000000004
>>> det_c(a, np.zeros((2, 2)), np.zeros(2, dtype=np.int32))
>>> 1
其他时候,正确的标志。
>>> b = np.array([[1,2,3],[1,2,1],[5,6,1.]])
>>> np.linalg.det(b)
>>> -7.999999999999998
>>> det_c(a, np.zeros((3, 3)), np.zeros(3, dtype=np.int32))
>>> -8.0
最佳答案
dgetrf
是 Fortran 子程序,Fortran 使用从 1 开始的索引,所以 ipiv
中的值在 1 到 n
之间(包括).为了解决这个问题,将循环中的测试从
if j != ipiv[j]:
到
if j != ipiv[j] - 1:
关于python - 在 Cython 中使用 LAPACK 包装器估计行列式进行 LU 分解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50284132/
我正在尝试主要基于 LU decomposition with partial pivoting Matlab 来处理我的 lu 分解 function [L,U,P] = lup(A) n = le
我试图通过使用 LU 分解来求解 Ax=b,但不知何故我无法通过乘以 L*U 来得到 A。这是代码和结果; A = array([2,3,5,4]).reshape(2,2) b = array([4
设 A 为以下矩阵: 1 3 2 4 如果我调用 lu( ) 函数并像这样保存返回值: [L, U] = lu(A); MATLAB 返回 L, U 使得 L * U = A: >> L * U an
我在 cython 中编写了以下 Matrix 类 用于矩阵求逆和其他一些线性代数运算。我尝试使用LU 分解 来计算矩阵的逆。代码速度很好。我试图实现 this code在 cython 中。我已经检
以下函数不使用行透视进行 LU 分解。 R 中是否有一个现有的函数可以使用行数据进行 LU 分解? > require(Matrix) > expand(lu(matrix(rnorm(16),4,4
已结束。此问题正在寻求书籍、工具、软件库等的推荐。它不满足Stack Overflow guidelines 。目前不接受答案。 我们不允许提出寻求书籍、工具、软件库等推荐的问题。您可以编辑问题,以便
我按照算法写了代码,但是结果不对。根据算法,我们必须指明矩阵的维数,并手动填写主矩阵A和 vector B。我们需要生成一个LU矩阵。它已生成,但编号错误。最后我们必须得到带有解的 vector X。
我正在做 LU decom,我在 googel 上找到了这段代码,但想通过输出“pvt”和“a”来理解它,但它似乎是我的 pvt 不正确,所以我得到了一些不同的东西所以请问谁能纠正我 ..谢谢 这是我
给定 L 和 U LU decomposition和常数 b 的向量使得 LU*x=b ,是否有找到 x 的内置函数?意思是 - X = functionName(L,U,b) 请注意,在 L 和 U
lu 在这种情况下是什么意思: size_t size = 10lu; 我一无所获。 谢谢! 最佳答案 简化:这意味着 unsigned long。 另请引用this. 关于c++ - 变量定义中 "
我在 c# 中偶然发现了这个正则表达式,我想移植到 javascript,但我不明白以下内容: [-.\p{Lu}\p{Ll}0-9]+ 我很难理解的部分当然是\p{Lu}。我访问过的所有正则表达式网
应该如何解释这一规则 fragment LETTER_UPPERCASE : [\p{Lu}] ; 在 https://github.com/okellogg/ada_antlr_grammar/b
应该如何解释这一规则 fragment LETTER_UPPERCASE : [\p{Lu}] ; 在 https://github.com/okellogg/ada_antlr_grammar/b
Lu 应该代表 Letter, Uppercase,但是我得到意想不到的结果 PS > 'Hello World' -replace '\p{Lu}', 'Z' ZZZZZ ZZZZZ 可以看出,Lu
我知道用 %lu 打印一个无符号长整数是不好的做法。在我正在从事的一个项目中,当我尝试以 snprint 格式用 %lu 打印 11 时,我得到了一个很大的数字。(旧代码)我使用的是 gcc 4.9.
我对下面的代码有问题,经过一些研究我在单独的一行中挑出了这个问题,但现在不确定如何解决它。 typedef double ComplexType; typedef std::complex Compl
我正在使用 Armadillo C++ 库求解中/大型线性系统(1000-5000 个方程)。 因为我必须解决不同的线性系统 AX=b 其中 A 始终相同而 B 发生变化,我只想对 A 进行 LU 分
我正在使用 C++ 中的 Eigen 库来求解稀疏线性方程:Ax=b 其中,A 是一个稀疏方阵,b 是一个矩形稀疏矩阵。我有多个 A 矩阵实例,每个实例都有多个右侧 b。因此,我想对所有 A 矩阵进行
更新:这是解决方案,我向每一行添加了一个标量以控制下溢和上溢。感谢大家的帮助。 我一直在研究 C++ 中的 LU 分解,希望有一天能分解并求解大型稀疏矩阵。我找到了一些代码并对其进行了修改以供自己使用
我目前在将数组分解为上 (u) 和下 (l) 数组时遇到一些问题。 我正在使用杜立特方法 我的代码: #include using namespace std; int main(){ do
我是一名优秀的程序员,十分优秀!