This is the mail archive of the mailing list for the GSL project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Naive question from novice C programmer

Dear Alex,
My suggestion is to use a 'view' object for each one of your arrays:

(From the manual):

---------- BEGIN EXTRACT FROM MANUAL ----------------------------
Function: gsl_vector_view gsl_vector_view_array (double *base, size_t n)
Function: gsl_vector_const_view gsl_vector_const_view_array (const double
*base, size_t n)
    These functions return a vector view of an array. The start of the new
vector is given by base and has n elements. Mathematically, the i-th
element of the new vector v' is given by,

v'(i) = base[i]

    where the index i runs from 0 to n-1.

    The array containing the elements of v is not owned by the new vector
view. When the view goes out of scope the original array will continue to
exist. The original memory can only be deallocated by freeing the original
pointer base. Of course, the original array should not be deallocated
while the view is still in use.

The function gsl_vector_const_view_array is equivalent to
gsl_vector_view_array but can be used for arrays which are declared const. 

-------------- END EXTRACT FROM MANUAL ---------------------------------
Example follows after your comments:

On Sat, 28 Sep 2002, Alex Casti wrote:

> I'm fairly new to both GSL and the C programming language.
> What I'm wondering is if there's a way to employ the GSL
> routines that take arrays as input, where the arrays are *not*
> defined the GSL way using "gsl_vector_alloc" and so forth,
> but rather the usual manual way with malloc, i.e.
> xarray = (double *) malloc((nsize)*sizeof(double));

gsl_vector_view X = gsl_vector_view_array(xarray,nsize);

gsl_vector_set_all(&X.vector, 10.0);

will accomplish what you ask for below.

A good example of all this is in the 'examples' section of the CBlas
section of the manual (applied to matrix multiplication).

> I would prefer to use my own array names and definitions
> because the GSL calling names are somewhat unwieldy and long.
> However, it doesn't appear that I can define "xarray" as
> above and then shove this into a GSL function like
> gsl_vector_set_all(xarray, 10.)
> When I try this I get an error complaining about incompatible
> pointer types.
> Am I correct in concluding that I *cannot* set my own array allocations
> and use the GSL routines?  Must I always use the GSL vector structs?
> Thanks for your help.



Alan Aspuru-Guzik                    Dios mueve al jugador, y éste, la pieza.
(510)642-5911 UC Berkeley           ¿Qué Dios detrás de Dios la trama empieza
(925)422-8739 LLNL                de polvo y tiempo y sueño y agonías? -Borges

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]