# Libjulia / c-api : how to unbox SparseMatrixCSC

**URL:** <https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 12, 2017, 10:44am UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620 "2017-03-12T10:44:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maxime\_Coorevits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxime_coorevits/32/730_2.png) [@Maxime\_Coorevits](https://discourse.julialang.org/u/Maxime_Coorevits)\
**Post date:** [March 12, 2017, 10:44am UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/1 "2017-03-12T10:44:46Z")

</div>

A new question about the libjulia c-api. My project is fully functional and I’m able to interact with the c-api in my VS2017 / C++ solution.

I work on a C++ wrapper of the ACME package that (intensively) use sparse matrix, but I can’t see how to “unbox” the SparseMatrixCSC with the c-api.

The package contains two major circuit functions that create : one “incidence” matrix (sparse), and two “topology” matrices (sparse). The first one return a SparseMatrixCSC of integers. The second return a tuple of two SparseMatrixCSC of Integer. The “topomat” function call the “incidence” function that is used to produce the two topologicals matrices.

My C/C++ method call the “topomat” function to get a tuple :

```
jl_value_t* topo = jl_eval_string((ACME_ID + ".topomat(" + id + ")").c_str());

```

I get a jl\_value\_t\* pointer that is a tuple - alright. As I know, the tuple contains two matrices :

```
size_t num_mat = jl_nfields(topo);

```

Return 2. OK.

```
jl_value_t* mat1 = jl_get_nth_field(topo, 0);
jl_value_t* mat2 = jl_get_nth_field(topo, 1);

```

Return 2 valid pointers where jl\_typeof\_str return =\> **SparseMatrixCSC**

But now, I’m confused. How to use thèses pointers?

A call to :

```
jl_is_mutable(mat1) 

```

return true.

If I call :

```
jl_value_t* eltype = (jl_value_t*)jl_array_eltype(mat1);

```

Return a value pointer that is now typed : **bitstype**

If I call :

```
jl_value_t *ty = jl_typeof(val1);
int sz = jl_datatype_size(ty);

```

I get a sz integer of =\> **20**

But I really don’t know how to work with a 2D array (matrix) with my pointers. The current Embedded documentation talk about the C-\>libjulia 1D array.

If someone can help me with a little example of code, I would be delighted.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 12, 2017, 11:43am UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/2 "2017-03-12T11:43:38Z")

</div>

I haven’t tested this, but you could get e.g. the colptr like this:

```nohighlight
jl_value_t* mat1 = jl_get_nth_field(topo, 0);
jl_array_t* colptr = (jl_array_t*)jl_get_nth_field(mat1, 0);
int_t* colptr_raw = (int_t*)jl_array_data(colptr);
size_t colptr_n = jl_array_len(colptr);

```

`colptr_raw` can then be used as a C array with length `colptr_n`. Same for the other spare matrix fields, except for the element types.

---

<div class="post-metadata">

**Author:** ![Maxime\_Coorevits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxime_coorevits/32/730_2.png) [@Maxime\_Coorevits](https://discourse.julialang.org/u/Maxime_Coorevits)\
**Post date:** [March 12, 2017, 12:05pm UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/3 "2017-03-12T12:05:53Z")

</div>

Thank you for you answer, barche, but this process doesn’t work.

```
jl_array_t* colptr = (jl_array_t*)jl_get_nth_field(mat1, 0);

```

return a bad structure :

```
jl_array_t* colptr (0x0402b0f8)
{
    data=0x00000009 
    length=67241042 
    flags={how=1 ndims=130 pooled=0 ...}
    elsize=0
    offset=67241170
    nrows=521
    maxsize=67240850
    ncols=67240850
}

```

The raw data pointer is a bad address : 0x00000009

The first matrix (mat1) is a sparse matrix with full size:

```
julia> full(tv)
9×16 Array{Int32,2}:
 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 -1 1 0 0 0 0 0 0 0 0 0 0 0
 0 0 0 -1 0 1 1 0 0 0 0 0 0 0 0 0
 1 0 0 -1 0 1 0 1 0 0 0 0 0 0 0 0
 0 0 0 -1 0 1 0 0 1 0 1 0 0 0 0 0
 1 0 0 -1 0 1 0 0 0 1 0 1 0 0 0 0
 0 0 0 1 0 -1 0 0 0 -1 0 0 -1 1 1 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1

```

The second matrix (mat2) :

```
julia> full(ti)
7×16 Array{Int32,2}:
 1 -1 -1 0 0 0 0 -1 0 0 0 -1 0 0 0 0
 0 0 0 1 1 0 1 1 0 0 1 1 0 0 -1 0
 0 0 0 0 0 1 -1 -1 0 0 -1 -1 0 0 1 0
 0 0 0 0 0 0 0 0 1 0 -1 0 0 0 0 0
 0 0 0 0 0 0 0 0 0 1 0 -1 0 0 1 0
 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 -1

```

Matrices come from the “birdie” circuit example of the ACME package.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [March 12, 2017, 12:23pm UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/4 "2017-03-12T12:23:34Z")

</div>

Oops, sorry, the colptr is the third field, not the first, I was confused by the printing output. Dumping the type yields:

```julia
SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseArray{Tv,Ti<:Integer,2}
  m::Int64
  n::Int64
  colptr::Array{Ti,1}
  rowval::Array{Ti,1}
  nzval::Array{Tv,1}

```

So it should be

```nohighlight
jl_array_t* colptr = (jl_array_t*)jl_get_nth_field(mat1, 2);

```

---

<div class="post-metadata">

**Author:** ![Maxime\_Coorevits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxime_coorevits/32/730_2.png) [@Maxime\_Coorevits](https://discourse.julialang.org/u/Maxime_Coorevits)\
**Post date:** [March 12, 2017, 12:50pm UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/5 "2017-03-12T12:50:12Z")

</div>

So simple 🙂

So, for someone who want to get the SparseMatrixCSC fields, the answer is :

```
int m = jl_unbox_int32(jl_get_nth_field(mat1, 0));
int n = jl_unbox_int32(jl_get_nth_field(mat1, 1));
jl_array_t* colptr = (jl_array_t*)jl_get_nth_field(mat1, 2);
jl_array_t* rowval = (jl_array_t*)jl_get_nth_field(mat1, 3);
jl_array_t* nzval = (jl_array_t*)jl_get_nth_field(mat1, 4);

```

In my case, m = 9, n = 16, colptr, rowval & nzval are valid jl\_array\_t pointers.

Thank’s to **barche**!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 12, 2017, 12:57pm UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/6 "2017-03-12T12:57:53Z")

</div>

This code will segfault. Be sure to root the variables.

Also, you don’t need to use any API, you can directly declare it as a C struct

```julia
struct jl_sparse_matrix_csc_t {
    int64_t m;
    int64_t n;
    jl_array_t *colptr;
    jl_array_t *rowval;
    jl_array_t *nzval;
};

```

And cast the pointer to this. You can do the same with the tuple too.

---

<div class="post-metadata">

**Author:** ![Maxime\_Coorevits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxime_coorevits/32/730_2.png) [@Maxime\_Coorevits](https://discourse.julialang.org/u/Maxime_Coorevits)\
**Post date:** [March 12, 2017, 1:09pm UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/7 "2017-03-12T13:09:17Z")

</div>

Thank you yuyichao. You’re right, but not “segfault” here, the code compile and run under VS2017 even if I use jl\_unbox\_int32 with int64 for input.

But you’re right, the better way is to use the jl\_sparse\_matrix\_csc\_t !

Thank you very much for your answer too !!!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 12, 2017, 2:06pm UTC](https://discourse.julialang.org/t/libjulia-c-api-how-to-unbox-sparsematrixcsc/2620/8 "2017-03-12T14:06:09Z")

</div>

Well, it won’t segfault every time. But it will once in a few thousand times when the GC decided to give you a bad day.
