- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 c 相当陌生,想根据维基百科的伪代码实现 cholesky 分解。需要动态分配内存。
我用以下示例矩阵尝试了我的代码:
4.000 2.000 0.000 0.000
2.000 5.000 2.000 0.000
0.000 2.000 10.000 3.000
0.000 0.000 3.000 2.000
这应该导致:
2.000 0.000 0.000 0.000
1.000 2.000 0.000 0.000
0.000 1.000 3.000 0.000
0.000 0.000 1.000 1.000
而是返回我。
4.000 2.000 0.000 0.000
0.000 5.000 2.000 0.000
0.000 0.000 10.000 3.000
0.000 0.000 0.000 2.000
我想我在使用指针时有一些误解。我尝试根据this link动态分配.
有人可以告诉我,为什么正确的值没有写入我的矩阵中?这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int Cholesky(int n, double **A){
double sum;
sum = 0.0f;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < i; j++)
{
for(int k = 0; k < j-1; k++)
{
sum = sum - A[i][k]*A[j][k];
}
if(i > j)
{
A[i][j] = sum / A[j][j];
} else
{
if(sum > 0)
{
A[i][i] = sqrt(sum);
} else {
printf("Die Matrix ist nicht symetrisch positiv\n");
return -1;
}
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%.5f ", A[i][j]);
printf("\n");
}
}
int main(){
int n = 4;
double ** matrix;
double test[4][4] = {{4.0f,2.0f,0.0f,0.0f},{2.0f,5.0f,2.0f,0.0f},{0.0f,2.0f,10.0f, 3.0f},{0.0f,0.0f,3.0f,2.0f}};
/* Speicher reservieren für die int-Zeiger (=zeile) */
matrix = malloc(n * sizeof(double *));
if(NULL == matrix) {
printf("Kein virtueller RAM mehr vorhanden ... !");
return -1;
}
/* jetzt noch Speicher reservieren für die einzelnen Spalten
* der i-ten Zeile */
for(int i = 0; i < n; i++) {
matrix[i] = malloc(n * sizeof(double));
if(NULL == matrix[i]) {
printf("Kein Speicher mehr fuer Zeile %d\n",i);
return -1;
}
}
/* mit beliebigen Werten initialisieren */
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = test[i][j];
/* Inhalt der Matrix entsprechend ausgeben */
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%.5f ", matrix[i][j]);
printf("\n");
}
Cholesky(n, matrix);
/* Spalten der i-ten Zeile freigeben */
for(int i = 0; i < n; i++)
free(matrix[i]);
/* Jetzt können die leeren Zeilen freigegeben werden. */
free(matrix);
int x;
scanf("%d", x);
return 0;
}
最佳答案
您的动态分配矩阵是正确的,并且指针的使用也是正确的。
正确的值没有写入您的矩阵,因为您用于胆汁分解的算法是错误的。您可以在这里找到正确的算法:
http://www2.denizyuret.com/bib/press/www.library.cornell.edu/nr/bookcpdf/c2-9.pdf
您的函数仅在此指令中修改矩阵:
if (i > j) A[i][j] = sum / A[j][j];
这意味着对角线下方的元素被归零,如您的帖子中所示。
4.000 2.000 0.000 0.000
0.000 5.000 2.000 0.000
0.000 0.000 10.000 3.000
0.000 0.000 0.000 2.000
关于c 中的 Cholesky 分解失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33612896/
我正在开发一种算法,可以求解 Ax= b,其中 A 和 b 已知。 有两种方法可以实现此目的:x= A-1 b 或使用 Cholesky。我知道矩阵始终是方正定矩阵,尽管 det(A) 可能为零。在极
我真的很想评估你们中是否有任何人可以指出我在 Cholesky 分解 方面最优化和计算速度最快的线性代数库。 到目前为止,我一直在使用 Apache Commons Math 库,但也许已经有更强大和
我对 c 相当陌生,想根据维基百科的伪代码实现 cholesky 分解。需要动态分配内存。 我用以下示例矩阵尝试了我的代码: 4.000 2.000 0.000 0.000 2.000 5
double *cholesky(double *A, int n) { double *L = (double*)calloc(n * n, sizeof(double)); if
我想知道是否可以在不需要临时数组的情况下通过 Cholesky 分解获得矩阵的逆。截至目前,我可以在不使用临时数组的情况下进行 cholesky 分解,但从那里我还没有想出一种方法来获得原始矩阵的逆矩
我正尝试在 C++ 中实现 Cholesky 分解,它以前在 lapack dpotrf_ 中实现过. Cholesky 分解:R' * R = A 代码: #include #include l
我正在使用 python 的 scikits.sparse.cholmod 来获取对称矩阵的 cholesky 分解。 我将 cholesky() 的结果与 matlab 的 chol() 进行了比较
我正在开发一些代码,可以从 HW 获取浮点或定点数据。目前我们将其作为 float 。 底层API都是定点的。所以我们必须将数据作为定点传回。我们使用的算法是 Cholesky。我想知道为什么我们必须
我使用 OpenCL 实现了以下 Cholesky 分解算法。该代码表现出随机行为。它仅在某些时候匹配 cpu 输出。有人可以帮我弄清楚我的实现有什么问题吗。 算法如下: procedure CHOL
我对大型稀疏矩阵的 Cholesky 分解很感兴趣。我遇到的问题是 Cholesky 因子不一定是稀疏的(就像两个稀疏矩阵的乘积不一定是稀疏的一样)。 例如,对于仅沿第一行、第一列和对角线具有非零值的
我正在尝试通过 pdpotrf() 进行 Cholesky 分解MKL-Intel 的库,它使用 ScaLAPACK。我正在读取主节点中的整个矩阵,然后像在这个 example 中一样分发它.当 SP
Cholesky Decomposition 有两种不同的形式: A = M * ctranspose (M) 和 LDL 形式 A = L * D * ctranspose (L) 其中 ctran
我想用 Sympy 找到一个符号 Cholesky 分解。矩阵 M(参见示例)是实对称的(因此是埃尔米特矩阵)。但 Sympy 引发 ValueError:矩阵必须是厄米特矩阵。 两个问题: 为什么会
我想使用 Eigen 来计算稀疏矩阵的 cholesky 分解。但是,结果不正确,我找不到原因。我如何获得正确答案? Eigen 中是否实现了特殊例程,利用稀疏矩阵的结构来提高性能(例如,对于下例中的
OpenCV 中是否有任何函数可用于对矩阵应用 Cholesky 分解? 最佳答案 如果仍然相关,OCV 3.0.0 直接通过 HAL 接口(interface)支持 Cholesky。参见 cv::
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow
我想获得 tf.cholesky 相对于其输入的梯度。目前,tf.cholesky 没有注册梯度: LookupError: No gradient defined for operation 'Ch
我想知道是否有人可以帮助我调试以下应该计算正定矩阵的上三角 cholesky 分解的 OCaml 代码。 我知道它不是很实用而且很笨重,所以我提前道歉。我在下面给出了一些原因。 不管怎样,这就过去了!
我正在尝试在 Halide 中实现 Cholesky 分解。部分常见算法(例如 crout)由对三角矩阵的迭代组成。在某种程度上,分解的对角线元素是通过从输入矩阵的对角线元素中减去部分列和来计算的。列
谁能给我指出一个库/代码,让我可以在 python (numpy) 中对 Cholesky 分解执行低阶更新?Matlab 将此功能作为称为“cholupdate”的函数提供。LINPACK 也有这个
我是一名优秀的程序员,十分优秀!