简体   繁体   English

索引到CHOLMOD密集向量数组

[英]Indexing into CHOLMOD dense vector array

I have a cholmod_dense data structure: 我有一个cholmod_dense数据结构:

cholmod_dense* ex = cholmod_l_solve(CHOLMOD_A, L, B, &com);

and I want to extract the values and copy them to another variable. 我想提取值并将其复制到另一个变量。 This means I need to index into the double array and copy values over. 这意味着我需要索引到double数组中并复制值。

for (int k=0; k<ncols; k++) T_x[k]=((double*)ex->x)[k];   

which the compiler is ok with but I get a segmenation fault. 编译器可以,但是出现段错误。 Or I think I should be able to do: 或者我认为我应该能够做到:

double* e_x =(double*)ex->x;
for (int k=0; k<ncols; k++) T_x[k]=*e_x[k];

But the compiler really dislikes this: 但是编译器确实不喜欢这样:

error: invalid type argument of unary ‘*’ (have ‘double’)
for (int k=0; k<ncols; k++) T_x[k]= *e_x[k];

According to CHOLMOD userguide: 根据CHOLMOD用户指南:

  1. cholmod dense: A dense matrix, either real, complex or zomplex, in column-major order. cholmod密集:以列为主的密集实数矩阵,复数矩阵或zomplex矩阵。 This differs from the row-major convention used in C. A dense matrix X contains • X->x, a double array of size X->nzmax or twice that for the complex case. 这与C语言中使用的行优先约定不同。密集矩阵X包含•X-> x,大小为X-> nzmax的双精度数组,或者是复杂情况的两倍。 • X->z, a double array of size X->nzmax if X is zomplex. •X-> z,如果X是zomplex,则为大小为X-> nzmax的双精度数组。

So I should be able to simply grab ex->x and index into it as a double array, but I cannot do so without getting a segmentation fault. 因此,我应该能够简单地获取ex-> x并将其索引为双精度数组,但是如果没有分段错误,我将无法做到这一点。 Can anyone help me out? 谁能帮我吗?

The CHOLMOD library is written in C and the code that is linking to CHOLMOD library (the code snippet shown above) is c++. CHOLMOD库是用C编写的,链接到CHOLMOD库的代码(上面显示的代码段)是c ++。

Ok, so it looks likes I made a couple mistakes. 好的,看来我犯了一些错误。

First of all, I was running into a segmentation fault because I was using cholmod_l_zeros(); 首先,因为使用cholmod_l_zeros();所以我cholmod_l_zeros();了分段错误cholmod_l_zeros(); which assumes long integers . 假定long integers Instead, I should be using cholmod_zeros(); 相反,我应该使用cholmod_zeros(); since I am using doubles . 因为我正在使用doubles

After fixing this I ran into the error CHOLMOD error: invalid xtype right after my cholmod_solve(CHOLMOD_A, L, B, &com); 解决此问题后,我遇到了错误CHOLMOD error: invalid xtype我的cholmod_solve(CHOLMOD_A, L, B, &com);之后cholmod_solve(CHOLMOD_A, L, B, &com); CHOLMOD error: invalid xtype cholmod_solve(CHOLMOD_A, L, B, &com); statement. 声明。 This was because my cholmod_factor* L was defined out of scope. 这是因为我的cholmod_factor* L定义超出了范围。 After fixing both of these issues, the code successfully copies values from cholmod_dense ex->x double array over to my T_x double vector : 解决了这两个问题之后,代码成功地将值从cholmod_dense ex->x double array复制到我的T_x double vector

cholmod_dense* ex = cholmod_solve(CHOLMOD_A, L, B, &com);
double* e_x = (double*)ex->x;
for (int k=0; k<ncols; k++) T_x[k] = e_x[k];

I also was unaware that the [] operator automatically dereferences pointers. 我也没有意识到[]运算符会自动取消引用指针。 Good to know! 很高兴知道!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM