public inbox for gsl-discuss@sourceware.org
 help / color / mirror / Atom feed
* RE:  Naive question from novice C programmer
@ 2002-09-29 12:10 Marco Morais
  0 siblings, 0 replies; 3+ messages in thread
From: Marco Morais @ 2002-09-29 12:10 UTC (permalink / raw)
  To: gsl-discuss; +Cc: Alex Casti

>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));

Use the gsl_vector_view facility (section 8.3.5 of the
version 1.0 manual).
for example.....

size_t nsize = 10;
double * x = (double *)
malloc((nsize)*sizeof(double));
if ( x == NULL ) exit(1);
gsl_vector_view xvec = gsl_vector_view_array (x,
nsize);
// use some gsl functions requiring vector
if ( gsl_vector_reverse(&(xvec.vector)) ) exit(1);
if ( gsl_vector_fprintf(stdout, &(xvec.vector), "%f")
) exit(1);

The same type of facility exists for gsl_matrix.  See
the gsl_matrix_view_array() function (section 8.4.5).

marco

__________________________________________________
Do you Yahoo!?
New DSL Internet Access from SBC & Yahoo!
http://sbc.yahoo.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Naive question from novice C programmer
  2002-09-28 19:13 Alex Casti
@ 2002-09-28 19:19 ` Alan Aspuru-Guzik
  0 siblings, 0 replies; 3+ messages in thread
From: Alan Aspuru-Guzik @ 2002-09-28 19:19 UTC (permalink / raw)
  To: Alex Casti; +Cc: gsl-discuss

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.
> 
> 
> 

Enjoy!

-- 

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Naive question from novice C programmer
@ 2002-09-28 19:13 Alex Casti
  2002-09-28 19:19 ` Alan Aspuru-Guzik
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Casti @ 2002-09-28 19:13 UTC (permalink / raw)
  To: gsl-discuss


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));


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.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2002-09-29  2:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-29 12:10 Naive question from novice C programmer Marco Morais
  -- strict thread matches above, loose matches on Subject: below --
2002-09-28 19:13 Alex Casti
2002-09-28 19:19 ` Alan Aspuru-Guzik

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).