使用CSparse库在C中表示稀疏矩阵 [英] Represent a sparse matrix in C using the CSparse Library

查看:153
本文介绍了使用CSparse库在C中表示稀疏矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白如何使用CSparese库轻松地用C表示稀疏矩阵.

I can't understand how can easily represent a sparse matrix in C using the CSparese Library.

那就是我想要的

    | 6.0 0.0 2.0 |
A = | 3.0 8.0 0.0 |
    | 6.0 0.0 1.0 |
with

    | 40.0 |
b = | 50.0 |
    | 30.0 |

csparse的cs struct是这个

The cs Struct of the csparse is this

typedef struct cs_sparse    /* matrix in compressed-column or triplet form */
{
    csi nzmax ;     /* maximum number of entries */
    csi m ;         /* number of rows */
    csi n ;         /* number of columns */
    csi *p ;        /* column pointers (size n+1) or col indices (size nzmax) */
    csi *i ;        /* row indices, size nzmax */
    double *x ;     /* numerical values, size nzmax */
    csi nz ;        /* # of entries in triplet matrix, -1 for compressed-col */
} cs ;

那就是我要做的

int main(int argc, const char * argv[])
{

    cs A;
    int  N = 3;
    double b[]={1,2,3};
    double data[]={1,1,1};
    csi columnIndices[]={0,1,2};
    csi rowIndices[]={0,1,2};
    A.nzmax =3;
    A.m = N;
    A.n = N;
    A.p = &columnIndices[0];
    A.i = &rowIndices[0];
    A.x = &data[0];
    A.nz = 3;

    cs *B = cs_compress(&A);
    int status =  cs_cholsol(0,B,&b[0]);



    printf("status=%d",status);   // status always returns 0, which means error
    return 0;

我要问的是如何用我的数据填充矩阵以及必须使用哪种方法来解决它.

What I ask, is how can I populate my matrix with my data and which method I must use to solve it.

谢谢

推荐答案

您可以使用cs_load从文件中读取矩阵. (每行一个条目,LINE COLUMN DOUBLE,您可以看到此示例)

You can either use cs_load which read a matrix from a file. (one entry per line, LINE COLUMN DOUBLE, you can see this example)

或使用cs_entry设置矩阵的值:cs_entry (matrix, i, j, 0.42);

Or use cs_entry to set a value of the matrix : cs_entry (matrix, i, j, 0.42);

您可能想看这个完整示例,和

数据结构A不应包含有关b的任何信息.整个数据结构是A的稀疏表示.而且,您不应该自己初始化它,而要让cs_spalloc进行工作. (例如cs_spalloc (0, 0, 1, 1, 1)). 然后使用cs_entry设置值.

The data structure A should not contain any information about b. The whole data structure is a sparse representation of A. Moreover, you should not initialize it yourself, but let cs_spalloc do the work. (by example cs_spalloc (0, 0, 1, 1, 1)). And then use cs_entry to set the values.

对于要求解的方程式的右手部分(Ax = b),如果b应该是稠密的,则应使用简单的C数组:

For the right-hand part of the equation you want to solve (Ax = b), then if b is supposed to be dense, the you should use a simple C array :

简单地:double b[]={10.0,20.0,30.0};

最后您可以拨打cs_lsolve(A, b).

这篇关于使用CSparse库在C中表示稀疏矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆