igraph-help
[Top][All Lists]

## Re: [igraph] igraph_matrix_copy_to()

 From: Gábor Csárdi Subject: Re: [igraph] igraph_matrix_copy_to() Date: Wed, 30 Jun 2010 11:48:40 +0200

```Well, just for the record, this solution might work, but it is very
suboptimal, as it copies the whole thing twice. Also, no memory will
be freed until the function returns.

Personally I would suggest to keep everything in the igraph_matrix_t,
you can access its element just as quickly as the array of arrays
version, and it even has a couple of operations that might come handy
(searching for maximum/minimum, row/column-wise sums, etc.). You can
also extract a pointer to the first element, with &MATRIX(matrix,0,0),
and then pass this to BLAS or LAPACK routines.

Gabor

On Tue, Jun 29, 2010 at 2:49 PM, Fernando Martinez
> Hi Gábor and Tamas,
>
> I've solved it like this:
>
> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>
> #include <igraph.h>
>
> int main(){
>
>     igraph_neimode_t mode = IGRAPH_ALL;
>   igraph_matrix_t matriz;
>   igraph_vs_t from;
>
>
>   double distances_vector[numberofnodes*numberofnodes];
>   long distances[numberofnodes][numberofnodes];
>
>   igraph_vs_all(&from);
>
>
>   igraph_matrix_init(&matriz, numberofnodes, numberofnodes);
>
>   //get the shortest paths:
>   igraph_shortest_paths(&graph, &matriz, from, mode);
>
>
>   //copy the shortest paths to a 'double' single-dimensioned array:
>   igraph_matrix_copy_to(&matriz, distances_vector);
>
>
>
>
>   //transform the single-dimension array into matrix form for convenience:
>   for(j=0; j<numberofnodes; j++){
>     for(m=0; m<numberofnodes; m++){
>       distances[j][m]=(long)distances_vector[numberofnodes*j+m];
>     }
>   }
>
>
>
>   igraph_matrix_destroy(&matriz);
>   igraph_vs_destroy(&from);
>
>   return(0);
>
> }
>
>
> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>
> This works fine. Thanks very much for your inputs.
>
> Cheers,
> Fernando
>
>
>
>
>
> Gábor Csárdi wrote:
>>
>> Tamas,
>>
>> you are right, this is one thing. But even if it is igraph_real_t the
>> compiler gives a warning (gcc) or error (g++), because
>> igraph_matrix_copy_to expects a pointer to a real, and not a pointer
>> to an array. See e.g.
>> http://www.lysator.liu.se/c/c-faq/c-2.html#2-10
>>
>> Fernando, consider using a pointer to the first element of the array
>> instead of a pointer to an array. I think this can be done by a simple
>> casting, although I might miss something here, pointers to arrays are
>> tricky. Also, I don't know whether using g++ instead of gcc changes
>> anything here, except for getting an error instead of a warning.
>>
>> But actually, why copying the thing anyway? What is wrong with keeping
>> it in an igraph_matrix_t?
>>
>> Best,
>> Gabor
>>
>> On Mon, Jun 28, 2010 at 8:22 PM, Tamas Nepusz <address@hidden> wrote:
>>
>>>
>>> Hi Fernando,
>>>
>>> igraph matrices store real numbers, not integers, therefore long is not a
>>> suitable data type for igraph_matrix_copy_to. Use an array of igraph_real_t
>>>
>>> igraph_real_t distances[numberofnodes][numberofnodes];
>>>
>>> --
>>> Tamas
>>>
>>> On 2010.06.28., at 18:44, Fernando Martinez wrote:
>>>
>>>
>>>>
>>>> Dear All,
>>>>
>>>> I'm having problems when using the function igraph_matrix_copy_to()
>>>>
>>>> As I understand it, this function allows to copy an igraph matrix into a
>>>> normal C matrix. However, I get an error when compiling.
>>>>
>>>> My code is the following:
>>>>
>>>> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>>>>
>>>>
>>>>
>>>> #include <igraph.h>
>>>>
>>>> int main()
>>>> {
>>>>
>>>>
>>>>      igraph_matrix_t matriz;
>>>>      igraph_vector_t vec;
>>>>      igraph_vs_t from;
>>>>      igraph_neimode_t mode = IGRAPH_ALL;
>>>>
>>>>      long numberofnodes=512;
>>>>
>>>>      long distances[numberofnodes][numberofnodes];
>>>>
>>>>      igraph_vs_all(&from);
>>>>
>>>>      igraph_shortest_paths(&graph, &matriz, from, mode);
>>>>
>>>>      igraph_matrix_copy_to(&matriz, distances);
>>>>
>>>>      return(0);
>>>>
>>>> }
>>>>
>>>>
>>>> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
>>>>
>>>> I compile in Unbuntu 9.04
>>>>
>>>> g++ -I/igraph -L/lib w1024.cpp -ligraph -lgsl -lgslcblas -o w1024
>>>>
>>>> and gives error for the line of "igraph_matrix_copy_to(&matriz,
>>>> distances);":
>>>>
>>>> error: cannot convert `long int (*)[((long unsigned int)((long
>>>> int)numberofnodes))]' to `igraph_real_t*' for argument `2' to `int
>>>> igraph_matrix_copy_to(const igraph_matrix_t*, igraph_real_t*)'
>>>>
>>>> Any ideas on how to pass the data from the igraph matrix to a C matrix?
>>>>
>>>>
>>>> Cheers,
>>>> Fernando
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> igraph-help mailing list
>>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>>
>>>
>>> _______________________________________________
>>> igraph-help mailing list
>>> http://lists.nongnu.org/mailman/listinfo/igraph-help
>>>
>>>
>>
>>
>>
>>
>
>

--