📄 vectors.texi
字号:
@cindex blocks@cindex vectors@cindex matricesThe functions described in this chapter provide a simple vector andmatrix interface to ordinary C arrays. The memory management of thesearrays is implemented using a single underlying type, known as ablock. By writing your functions in terms of vectors and matrices youcan pass a single structure containing both data and dimensions as anargument without needing additional function parameters. The structuresare compatible with the vector and matrix formats used by @sc{blas}routines.@menu* Data types:: * Blocks:: * Vectors:: * Matrices:: * Vector and Matrix References and Further Reading:: @end menu@node Data types@section Data typesAll the functions are available for each of the standard data-types.The versions for @code{double} have the prefix @code{gsl_block},@code{gsl_vector} and @code{gsl_matrix}. Similarly the versions forsingle-precision @code{float} arrays have the prefix@code{gsl_block_float}, @code{gsl_vector_float} and@code{gsl_matrix_float}. The full list of available types is givenbelow,@examplegsl_block double gsl_block_float float gsl_block_long_double long double gsl_block_int int gsl_block_uint unsigned int gsl_block_long long gsl_block_ulong unsigned long gsl_block_short short gsl_block_ushort unsigned shortgsl_block_char char gsl_block_uchar unsigned char gsl_block_complex complex double gsl_block_complex_float complex float gsl_block_complex_long_double complex long double @end example@noindentCorresponding types exist for the @code{gsl_vector} and@code{gsl_matrix} functions.@node Blocks@section BlocksFor consistency all memory is allocated through a @code{gsl_block}structure. The structure contains two components, the size of an area ofmemory and a pointer to the memory. The @code{gsl_block} structure lookslike this,@exampletypedef struct@{ size_t size; double * data;@} gsl_block;@end example@comment@noindentVectors and matrices are made by @dfn{slicing} an underlying block. Aslice is a set of elements formed from an initial offset and acombination of indices and step-sizes. In the case of a matrix thestep-size for the column index represents the row-length. The step-sizefor a vector is known as the @dfn{stride}.The functions for allocating and deallocating blocks are defined in@file{gsl_block.h}@menu* Block allocation:: * Reading and writing blocks:: * Example programs for blocks:: @end menu@node Block allocation@subsection Block allocationThe functions for allocating memory to a block follow the style of@code{malloc} and @code{free}. In addition they also perform their ownerror checking. If there is insufficient memory available to allocate ablock then the functions call the GSL error handler (with an errornumber of @code{GSL_ENOMEM}) in addition to returning a nullpointer. Thus if you use the library error handler to abort your programthen it isn't necessary to check every @code{alloc}.@deftypefun {gsl_block *} gsl_block_alloc (size_t @var{n})This function allocates memory for a block of @var{n} double-precisionelements, returning a pointer to the block struct. The block is notinitialized and so the values of its elements are undefined. Use thefunction @code{gsl_block_calloc} if you want to ensure that all theelements are initialized to zero.A null pointer is returned if insufficient memory is available to createthe block.@end deftypefun@deftypefun {gsl_block *} gsl_block_calloc (size_t @var{n})This function allocates memory for a block and initializes all theelements of the block to zero.@end deftypefun@deftypefun void gsl_block_free (gsl_block * @var{b})This function frees the memory used by a block @var{b} previouslyallocated with @code{gsl_block_alloc} or @code{gsl_block_calloc}.@end deftypefun@node Reading and writing blocks@subsection Reading and writing blocksThe library provides functions for reading and writing blocks to a fileas binary data or formatted text.@deftypefun int gsl_block_fwrite (FILE * @var{stream}, const gsl_block * @var{b})This function writes the elements of the block @var{b} to the stream@var{stream} in binary format. The return value is 0 for success and@code{GSL_EFAILED} if there was a problem writing to the file. Since thedata is written in the native binary format it may not be portablebetween different architectures.@end deftypefun@deftypefun int gsl_block_fread (FILE * @var{stream}, gsl_block * @var{b})This function reads into the block @var{b} from the open stream@var{stream} in binary format. The block @var{b} must be preallocatedwith the correct length since the function uses the size of @var{b} todetermine how many bytes to read. The return value is 0 for success and@code{GSL_EFAILED} if there was a problem reading from the file. Thedata is assumed to have been written in the native binary format on thesame architecture.@end deftypefun@deftypefun int gsl_block_fprintf (FILE * @var{stream}, const gsl_block * @var{b}, const char * @var{format})This function writes the elements of the block @var{b} line-by-line tothe stream @var{stream} using the format specifier @var{format}, whichshould be one of the @code{%g}, @code{%e} or @code{%f} formats forfloating point numbers and @code{%d} for integers. The function returns0 for success and @code{GSL_EFAILED} if there was a problem writing tothe file.@end deftypefun@deftypefun int gsl_block_fscanf (FILE * @var{stream}, gsl_block * @var{b})This function reads formatted data from the stream @var{stream} into theblock @var{b}. The block @var{b} must be preallocated with the correctlength since the function uses the size of @var{b} to determine how manynumbers to read. The function returns 0 for success and@code{GSL_EFAILED} if there was a problem reading from the file.@end deftypefun@comment@node Example programs for blocks@subsection Example programs for blocksThe following program shows how to allocate a block,@example@verbatiminclude examples/block.c@end example@comment@noindentHere is the output from the program,@example@verbatiminclude examples/block.out@end example@comment@node Vectors@section Vectors@cindex vectors@cindex stride, of vector indexVectors are defined by a @code{gsl_vector} structure which describes aslice of a block. Different vectors can be created which point to thesame block. A vector slice is a set of equally-spaced elements of anarea of memory.The @code{gsl_vector} structure contains five components, the@dfn{size}, the @dfn{stride}, a pointer to the memory where the elementsare stored, @var{data}, a pointer to the block owned by the vector,@var{block}, if any, and an ownership flag, @var{owner}. The structureis very simple and looks like this,@exampletypedef struct@{ size_t size; size_t stride; double * data; gsl_block * block; int owner;@} gsl_vector;@end example@comment@noindentThe @var{size} is simply the number of vector elements. The range ofvalid indices runs from 0 to @code{size-1}. The @var{stride} is thestep-size from one element to the next in physical memory, measured inunits of the appropriate datatype. The pointer @var{data} gives thelocation of the first element of the vector in memory. The pointer@var{block} stores the location of the memory block in which the vectorelements are located (if any). If the vector owns this block then the@var{owner} field is set to one and the block will be deallocated when thevector is freed. If the vector points to a block owned by anotherobject then the @var{owner} field is zero and any underlying block will not bedeallocated.The functions for allocating and accessing vectors are defined in@file{gsl_vector.h}@menu* Vector allocation:: * Accessing vector elements:: * Initializing vector elements:: * Reading and writing vectors:: * Vector views:: * Copying vectors:: * Exchanging elements:: * Vector operations:: * Finding maximum and minimum elements of vectors:: * Vector properties:: * Example programs for vectors:: @end menu@node Vector allocation@subsection Vector allocationThe functions for allocating memory to a vector follow the style of@code{malloc} and @code{free}. In addition they also perform their ownerror checking. If there is insufficient memory available to allocate avector then the functions call the GSL error handler (with an errornumber of @code{GSL_ENOMEM}) in addition to returning a nullpointer. Thus if you use the library error handler to abort your programthen it isn't necessary to check every @code{alloc}.@deftypefun {gsl_vector *} gsl_vector_alloc (size_t @var{n})This function creates a vector of length @var{n}, returning a pointer toa newly initialized vector struct. A new block is allocated for theelements of the vector, and stored in the @var{block} component of thevector struct. The block is ``owned'' by the vector, and will bedeallocated when the vector is deallocated.@end deftypefun@deftypefun {gsl_vector *} gsl_vector_calloc (size_t @var{n})This function allocates memory for a vector of length @var{n} andinitializes all the elements of the vector to zero.@end deftypefun@deftypefun void gsl_vector_free (gsl_vector * @var{v})This function frees a previously allocated vector @var{v}. If thevector was created using @code{gsl_vector_alloc} then the blockunderlying the vector will also be deallocated. If the vector has beencreated from another object then the memory is still owned by thatobject and will not be deallocated.@end deftypefun@node Accessing vector elements@subsection Accessing vector elements@cindex vectors, range-checking@cindex range-checking for vectors@cindex Checkergcc@cindex gcc extensions, range-checking@cindex Fortran range checking, equivalent in gccUnlike @sc{fortran} compilers, C compilers do not usually providesupport for range checking of vectors and matrices. Range checking isavailable in the GNU C Compiler extension @code{checkergcc} but it isnot available on every platform. The functions @code{gsl_vector_get}and @code{gsl_vector_set} can perform portable range checking for youand report an error if you attempt to access elements outside theallowed range.The functions for accessing the elements of a vector or matrix aredefined in @file{gsl_vector.h} and declared @code{extern inline} toeliminate function-call overhead. If necessary you can turn off rangechecking completely without modifying any source files by recompilingyour program with the preprocessor definition@code{GSL_RANGE_CHECK_OFF}. Provided your compiler supports inlinefunctions the effect of turning off range checking is to replace callsto @code{gsl_vector_get(v,i)} by @code{v->data[i*v->stride]} andcalls to @code{gsl_vector_set(v,i,x)} by @code{v->data[i*v->stride]=x}.Thus there should be no performance penalty for using the range checkingfunctions when range checking is turned off.@deftypefun double gsl_vector_get (const gsl_vector * @var{v}, size_t @var{i})This function returns the @var{i}-th element of a vector @var{v}. If@var{i} lies outside the allowed range of 0 to @var{n-1} then the errorhandler is invoked and 0 is returned.@end deftypefun
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -