|
Packit |
67cb25 |
********************
|
|
Packit |
67cb25 |
Vectors and Matrices
|
|
Packit |
67cb25 |
********************
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: include.rst
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions described in this chapter provide a simple vector and
|
|
Packit |
67cb25 |
matrix interface to ordinary C arrays. The memory management of these
|
|
Packit |
67cb25 |
arrays is implemented using a single underlying type, known as a
|
|
Packit |
67cb25 |
block. By writing your functions in terms of vectors and matrices you
|
|
Packit |
67cb25 |
can pass a single structure containing both data and dimensions as an
|
|
Packit |
67cb25 |
argument without needing additional function parameters. The structures
|
|
Packit |
67cb25 |
are compatible with the vector and matrix formats used by BLAS
|
|
Packit |
67cb25 |
routines.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Data types
|
|
Packit |
67cb25 |
==========
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
All the functions are available for each of the standard data-types.
|
|
Packit |
67cb25 |
The versions for :code:`double` have the prefix :code:`gsl_block`,
|
|
Packit |
67cb25 |
:code:`gsl_vector` and :code:`gsl_matrix`. Similarly the versions for
|
|
Packit |
67cb25 |
single-precision :code:`float` arrays have the prefix
|
|
Packit |
67cb25 |
:code:`gsl_block_float`, :code:`gsl_vector_float` and
|
|
Packit |
67cb25 |
:code:`gsl_matrix_float`. The full list of available types is given
|
|
Packit |
67cb25 |
below,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
=============================== ====================
|
|
Packit |
67cb25 |
Prefix Type
|
|
Packit |
67cb25 |
=============================== ====================
|
|
Packit |
67cb25 |
gsl_block double
|
|
Packit |
67cb25 |
gsl_block_float float
|
|
Packit |
67cb25 |
gsl_block_long_double long double
|
|
Packit |
67cb25 |
gsl_block_int int
|
|
Packit |
67cb25 |
gsl_block_uint unsigned int
|
|
Packit |
67cb25 |
gsl_block_long long
|
|
Packit |
67cb25 |
gsl_block_ulong unsigned long
|
|
Packit |
67cb25 |
gsl_block_short short
|
|
Packit |
67cb25 |
gsl_block_ushort unsigned short
|
|
Packit |
67cb25 |
gsl_block_char char
|
|
Packit |
67cb25 |
gsl_block_uchar unsigned char
|
|
Packit |
67cb25 |
gsl_block_complex complex double
|
|
Packit |
67cb25 |
gsl_block_complex_float complex float
|
|
Packit |
67cb25 |
gsl_block_complex_long_double complex long double
|
|
Packit |
67cb25 |
=============================== ====================
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Corresponding types exist for the :code:`gsl_vector` and
|
|
Packit |
67cb25 |
:code:`gsl_matrix` functions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index:: blocks
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Blocks
|
|
Packit |
67cb25 |
======
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
For consistency all memory is allocated through a :code:`gsl_block`
|
|
Packit |
67cb25 |
structure. The structure contains two components, the size of an area of
|
|
Packit |
67cb25 |
memory and a pointer to the memory. The :code:`gsl_block` structure looks
|
|
Packit |
67cb25 |
like this,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. type:: gsl_block
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
typedef struct
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
size_t size;
|
|
Packit |
67cb25 |
double * data;
|
|
Packit |
67cb25 |
} gsl_block;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Vectors and matrices are made by *slicing* an underlying block. A
|
|
Packit |
67cb25 |
slice is a set of elements formed from an initial offset and a
|
|
Packit |
67cb25 |
combination of indices and step-sizes. In the case of a matrix the
|
|
Packit |
67cb25 |
step-size for the column index represents the row-length. The step-size
|
|
Packit |
67cb25 |
for a vector is known as the *stride*.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for allocating and deallocating blocks are defined in
|
|
Packit |
67cb25 |
:file:`gsl_block.h`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Block allocation
|
|
Packit |
67cb25 |
----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for allocating memory to a block follow the style of
|
|
Packit |
67cb25 |
:code:`malloc` and :code:`free`. In addition they also perform their own
|
|
Packit |
67cb25 |
error checking. If there is insufficient memory available to allocate a
|
|
Packit |
67cb25 |
block then the functions call the GSL error handler (with an error
|
|
Packit |
67cb25 |
number of :macro:`GSL_ENOMEM`) in addition to returning a null
|
|
Packit |
67cb25 |
pointer. Thus if you use the library error handler to abort your program
|
|
Packit |
67cb25 |
then it isn't necessary to check every :code:`alloc`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_block * gsl_block_alloc (size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function allocates memory for a block of :data:`n` double-precision
|
|
Packit |
67cb25 |
elements, returning a pointer to the block struct. The block is not
|
|
Packit |
67cb25 |
initialized and so the values of its elements are undefined. Use the
|
|
Packit |
67cb25 |
function :func:`gsl_block_calloc` if you want to ensure that all the
|
|
Packit |
67cb25 |
elements are initialized to zero.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Zero-sized requests are valid and return a non-null result. A null pointer
|
|
Packit |
67cb25 |
is returned if insufficient memory is available to create the block.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_block * gsl_block_calloc (size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function allocates memory for a block and initializes all the
|
|
Packit |
67cb25 |
elements of the block to zero.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_block_free (gsl_block * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function frees the memory used by a block :data:`b` previously
|
|
Packit |
67cb25 |
allocated with :func:`gsl_block_alloc` or :func:`gsl_block_calloc`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Reading and writing blocks
|
|
Packit |
67cb25 |
--------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The library provides functions for reading and writing blocks to a file
|
|
Packit |
67cb25 |
as binary data or formatted text.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_block_fwrite (FILE * stream, const gsl_block * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function writes the elements of the block :data:`b` to the stream
|
|
Packit |
67cb25 |
:data:`stream` in binary format. The return value is 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem writing to the file. Since the
|
|
Packit |
67cb25 |
data is written in the native binary format it may not be portable
|
|
Packit |
67cb25 |
between different architectures.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_block_fread (FILE * stream, gsl_block * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function reads into the block :data:`b` from the open stream
|
|
Packit |
67cb25 |
:data:`stream` in binary format. The block :data:`b` must be preallocated
|
|
Packit |
67cb25 |
with the correct length since the function uses the size of :data:`b` to
|
|
Packit |
67cb25 |
determine how many bytes to read. The return value is 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem reading from the file. The
|
|
Packit |
67cb25 |
data is assumed to have been written in the native binary format on the
|
|
Packit |
67cb25 |
same architecture.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_block_fprintf (FILE * stream, const gsl_block * b, const char * format)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function writes the elements of the block :data:`b` line-by-line to
|
|
Packit |
67cb25 |
the stream :data:`stream` using the format specifier :data:`format`, which
|
|
Packit |
67cb25 |
should be one of the :code:`%g`, :code:`%e` or :code:`%f` formats for
|
|
Packit |
67cb25 |
floating point numbers and :code:`%d` for integers. The function returns
|
|
Packit |
67cb25 |
0 for success and :macro:`GSL_EFAILED` if there was a problem writing to
|
|
Packit |
67cb25 |
the file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_block_fscanf (FILE * stream, gsl_block * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function reads formatted data from the stream :data:`stream` into the
|
|
Packit |
67cb25 |
block :data:`b`. The block :data:`b` must be preallocated with the correct
|
|
Packit |
67cb25 |
length since the function uses the size of :data:`b` to determine how many
|
|
Packit |
67cb25 |
numbers to read. The function returns 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem reading from the file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Example programs for blocks
|
|
Packit |
67cb25 |
---------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following program shows how to allocate a block,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/block.c
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Here is the output from the program,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/block.txt
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: vectors
|
|
Packit |
67cb25 |
single: stride, of vector index
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Vectors
|
|
Packit |
67cb25 |
=======
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Vectors are defined by a :type:`gsl_vector` structure which describes a
|
|
Packit |
67cb25 |
slice of a block. Different vectors can be created which point to the
|
|
Packit |
67cb25 |
same block. A vector slice is a set of equally-spaced elements of an
|
|
Packit |
67cb25 |
area of memory.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The :type:`gsl_vector` structure contains five components, the
|
|
Packit |
67cb25 |
*size*, the *stride*, a pointer to the memory where the elements
|
|
Packit |
67cb25 |
are stored, :data:`data`, a pointer to the block owned by the vector,
|
|
Packit |
67cb25 |
:data:`block`, if any, and an ownership flag, :data:`owner`. The structure
|
|
Packit |
67cb25 |
is very simple and looks like this,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. type:: gsl_vector
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
typedef struct
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
size_t size;
|
|
Packit |
67cb25 |
size_t stride;
|
|
Packit |
67cb25 |
double * data;
|
|
Packit |
67cb25 |
gsl_block * block;
|
|
Packit |
67cb25 |
int owner;
|
|
Packit |
67cb25 |
} gsl_vector;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The :data:`size` is simply the number of vector elements. The range of
|
|
Packit |
67cb25 |
valid indices runs from 0 to :code:`size-1`. The :data:`stride` is the
|
|
Packit |
67cb25 |
step-size from one element to the next in physical memory, measured in
|
|
Packit |
67cb25 |
units of the appropriate datatype. The pointer :data:`data` gives the
|
|
Packit |
67cb25 |
location of the first element of the vector in memory. The pointer
|
|
Packit |
67cb25 |
:data:`block` stores the location of the memory block in which the vector
|
|
Packit |
67cb25 |
elements are located (if any). If the vector owns this block then the
|
|
Packit |
67cb25 |
:data:`owner` field is set to one and the block will be deallocated when the
|
|
Packit |
67cb25 |
vector is freed. If the vector points to a block owned by another
|
|
Packit |
67cb25 |
object then the :data:`owner` field is zero and any underlying block will not be
|
|
Packit |
67cb25 |
deallocated with the vector.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for allocating and accessing vectors are defined in
|
|
Packit |
67cb25 |
:file:`gsl_vector.h`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Vector allocation
|
|
Packit |
67cb25 |
-----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for allocating memory to a vector follow the style of
|
|
Packit |
67cb25 |
:code:`malloc` and :code:`free`. In addition they also perform their own
|
|
Packit |
67cb25 |
error checking. If there is insufficient memory available to allocate a
|
|
Packit |
67cb25 |
vector then the functions call the GSL error handler (with an error
|
|
Packit |
67cb25 |
number of :macro:`GSL_ENOMEM`) in addition to returning a null
|
|
Packit |
67cb25 |
pointer. Thus if you use the library error handler to abort your program
|
|
Packit |
67cb25 |
then it isn't necessary to check every :code:`alloc`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector * gsl_vector_alloc (size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function creates a vector of length n, returning a pointer to
|
|
Packit |
67cb25 |
a newly initialized vector struct. A new block is allocated for the
|
|
Packit |
67cb25 |
elements of the vector, and stored in the :data:`block` component of the
|
|
Packit |
67cb25 |
vector struct. The block is "owned" by the vector, and will be
|
|
Packit |
67cb25 |
deallocated when the vector is deallocated.
|
|
Packit |
67cb25 |
Zero-sized requests are valid and return a non-null result.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector * gsl_vector_calloc (size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function allocates memory for a vector of length :data:`n` and
|
|
Packit |
67cb25 |
initializes all the elements of the vector to zero.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_vector_free (gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function frees a previously allocated vector :data:`v`. If the
|
|
Packit |
67cb25 |
vector was created using :func:`gsl_vector_alloc` then the block
|
|
Packit |
67cb25 |
underlying the vector will also be deallocated. If the vector has
|
|
Packit |
67cb25 |
been created from another object then the memory is still owned by
|
|
Packit |
67cb25 |
that object and will not be deallocated.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: vectors, range-checking
|
|
Packit |
67cb25 |
single: range-checking for vectors
|
|
Packit |
67cb25 |
single: bounds checking, extension to GCC
|
|
Packit |
67cb25 |
single: gcc extensions, range-checking
|
|
Packit |
67cb25 |
single: Fortran range checking, equivalent in gcc
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Accessing vector elements
|
|
Packit |
67cb25 |
-------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Unlike Fortran compilers, C compilers do not usually provide
|
|
Packit |
67cb25 |
support for range checking of vectors and matrices. [#f1]_
|
|
Packit |
67cb25 |
The functions :func:`gsl_vector_get` and
|
|
Packit |
67cb25 |
:func:`gsl_vector_set` can perform portable range checking for you and
|
|
Packit |
67cb25 |
report an error if you attempt to access elements outside the allowed
|
|
Packit |
67cb25 |
range.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for accessing the elements of a vector or matrix are
|
|
Packit |
67cb25 |
defined in :file:`gsl_vector.h` and declared :code:`extern inline` to
|
|
Packit |
67cb25 |
eliminate function-call overhead. You must compile your program with
|
|
Packit |
67cb25 |
the preprocessor macro :macro:`HAVE_INLINE` defined to use these
|
|
Packit |
67cb25 |
functions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. macro:: GSL_RANGE_CHECK_OFF
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
If necessary you can turn off range checking completely without
|
|
Packit |
67cb25 |
modifying any source files by recompiling your program with the
|
|
Packit |
67cb25 |
preprocessor definition :macro:`GSL_RANGE_CHECK_OFF`. Provided your
|
|
Packit |
67cb25 |
compiler supports inline functions the effect of turning off range
|
|
Packit |
67cb25 |
checking is to replace calls to :code:`gsl_vector_get(v,i)` by
|
|
Packit |
67cb25 |
:code:`v->data[i*v->stride]` and calls to :code:`gsl_vector_set(v,i,x)` by
|
|
Packit |
67cb25 |
:code:`v->data[i*v->stride]=x`. Thus there should be no performance
|
|
Packit |
67cb25 |
penalty for using the range checking functions when range checking is
|
|
Packit |
67cb25 |
turned off.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. macro:: GSL_C99_INLINE
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
If you use a C99 compiler which requires inline functions in header
|
|
Packit |
67cb25 |
files to be declared :code:`inline` instead of :code:`extern inline`,
|
|
Packit |
67cb25 |
define the macro :macro:`GSL_C99_INLINE` (see :ref:`sec_inline-functions`).
|
|
Packit |
67cb25 |
With GCC this is selected automatically when compiling in C99 mode
|
|
Packit |
67cb25 |
(:code:`-std=c99`).
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. var:: gsl_check_range
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
If inline functions are not used, calls to the functions
|
|
Packit |
67cb25 |
:func:`gsl_vector_get` and :func:`gsl_vector_set` will link to the
|
|
Packit |
67cb25 |
compiled versions of these functions in the library itself. The range
|
|
Packit |
67cb25 |
checking in these functions is controlled by the global integer
|
|
Packit |
67cb25 |
variable :code:`gsl_check_range`. It is enabled by default---to
|
|
Packit |
67cb25 |
disable it, set :code:`gsl_check_range` to zero. Due to function-call
|
|
Packit |
67cb25 |
overhead, there is less benefit in disabling range checking here than
|
|
Packit |
67cb25 |
for inline functions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double gsl_vector_get (const gsl_vector * v, const size_t i)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the :data:`i`-th element of a vector :data:`v`. If
|
|
Packit |
67cb25 |
:data:`i` lies outside the allowed range of 0 to :code:`size - 1` then the error
|
|
Packit |
67cb25 |
handler is invoked and 0 is returned. |inlinefn|
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_vector_set (gsl_vector * v, const size_t i, double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function sets the value of the :data:`i`-th element of a vector
|
|
Packit |
67cb25 |
:data:`v` to :data:`x`. If :data:`i` lies outside the allowed range of 0 to
|
|
Packit |
67cb25 |
:code:`size - 1` then the error handler is invoked. |inlinefn|
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double * gsl_vector_ptr (gsl_vector * v, size_t i)
|
|
Packit |
67cb25 |
const double * gsl_vector_const_ptr (const gsl_vector * v, size_t i)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a pointer to the :data:`i`-th element of a vector
|
|
Packit |
67cb25 |
:data:`v`. If :data:`i` lies outside the allowed range of 0 to :code:`size - 1`
|
|
Packit |
67cb25 |
then the error handler is invoked and a null pointer is returned. |inlinefns|
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: vectors, initializing
|
|
Packit |
67cb25 |
single: initializing vectors
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Initializing vector elements
|
|
Packit |
67cb25 |
----------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_vector_set_all (gsl_vector * v, double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function sets all the elements of the vector :data:`v` to the value
|
|
Packit |
67cb25 |
:data:`x`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_vector_set_zero (gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function sets all the elements of the vector :data:`v` to zero.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_set_basis (gsl_vector * v, size_t i)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function makes a basis vector by setting all the elements of the
|
|
Packit |
67cb25 |
vector :data:`v` to zero except for the :data:`i`-th element which is set to
|
|
Packit |
67cb25 |
one.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Reading and writing vectors
|
|
Packit |
67cb25 |
---------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The library provides functions for reading and writing vectors to a file
|
|
Packit |
67cb25 |
as binary data or formatted text.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_fwrite (FILE * stream, const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function writes the elements of the vector :data:`v` to the stream
|
|
Packit |
67cb25 |
:data:`stream` in binary format. The return value is 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem writing to the file. Since the
|
|
Packit |
67cb25 |
data is written in the native binary format it may not be portable
|
|
Packit |
67cb25 |
between different architectures.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_fread (FILE * stream, gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function reads into the vector :data:`v` from the open stream
|
|
Packit |
67cb25 |
:data:`stream` in binary format. The vector :data:`v` must be preallocated
|
|
Packit |
67cb25 |
with the correct length since the function uses the size of :data:`v` to
|
|
Packit |
67cb25 |
determine how many bytes to read. The return value is 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem reading from the file. The
|
|
Packit |
67cb25 |
data is assumed to have been written in the native binary format on the
|
|
Packit |
67cb25 |
same architecture.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_fprintf (FILE * stream, const gsl_vector * v, const char * format)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function writes the elements of the vector :data:`v` line-by-line to
|
|
Packit |
67cb25 |
the stream :data:`stream` using the format specifier :data:`format`, which
|
|
Packit |
67cb25 |
should be one of the :code:`%g`, :code:`%e` or :code:`%f` formats for
|
|
Packit |
67cb25 |
floating point numbers and :code:`%d` for integers. The function returns
|
|
Packit |
67cb25 |
0 for success and :macro:`GSL_EFAILED` if there was a problem writing to
|
|
Packit |
67cb25 |
the file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_fscanf (FILE * stream, gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function reads formatted data from the stream :data:`stream` into the
|
|
Packit |
67cb25 |
vector :data:`v`. The vector :data:`v` must be preallocated with the correct
|
|
Packit |
67cb25 |
length since the function uses the size of :data:`v` to determine how many
|
|
Packit |
67cb25 |
numbers to read. The function returns 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem reading from the file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Vector views
|
|
Packit |
67cb25 |
------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
In addition to creating vectors from slices of blocks it is also
|
|
Packit |
67cb25 |
possible to slice vectors and create vector views. For example, a
|
|
Packit |
67cb25 |
subvector of another vector can be described with a view, or two views
|
|
Packit |
67cb25 |
can be made which provide access to the even and odd elements of a
|
|
Packit |
67cb25 |
vector.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. type:: gsl_vector_view
|
|
Packit |
67cb25 |
gsl_vector_const_view
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
A vector view is a temporary object, stored on the stack, which can be
|
|
Packit |
67cb25 |
used to operate on a subset of vector elements. Vector views can be
|
|
Packit |
67cb25 |
defined for both constant and non-constant vectors, using separate types
|
|
Packit |
67cb25 |
that preserve constness. A vector view has the type
|
|
Packit |
67cb25 |
:type:`gsl_vector_view` and a constant vector view has the type
|
|
Packit |
67cb25 |
:type:`gsl_vector_const_view`. In both cases the elements of the view
|
|
Packit |
67cb25 |
can be accessed as a :type:`gsl_vector` using the :code:`vector` component
|
|
Packit |
67cb25 |
of the view object. A pointer to a vector of type :code:`gsl_vector *`
|
|
Packit |
67cb25 |
or :code:`const gsl_vector *` can be obtained by taking the address of
|
|
Packit |
67cb25 |
this component with the :code:`&` operator.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
When using this pointer it is important to ensure that the view itself
|
|
Packit |
67cb25 |
remains in scope---the simplest way to do so is by always writing the
|
|
Packit |
67cb25 |
pointer as :code:`&view.vector`, and never storing this value
|
|
Packit |
67cb25 |
in another variable.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_vector_subvector (gsl_vector * v, size_t offset, size_t n)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_vector_const_subvector (const gsl_vector * v, size_t offset, size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of a subvector of another vector
|
|
Packit |
67cb25 |
:data:`v`. The start of the new vector is offset by :data:`offset` elements
|
|
Packit |
67cb25 |
from the start of the original vector. The new vector has :data:`n`
|
|
Packit |
67cb25 |
elements. Mathematically, the :data:`i`-th element of the new vector
|
|
Packit |
67cb25 |
:data:`v'` is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
v'(i) = v->data[(offset + i)*v->stride]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The :code:`data` pointer of the returned vector struct is set to null if
|
|
Packit |
67cb25 |
the combined parameters (:data:`offset`, :data:`n`) overrun the end of the
|
|
Packit |
67cb25 |
original vector.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The new vector is only a view of the block underlying the original
|
|
Packit |
67cb25 |
vector, :data:`v`. The block containing the elements of :data:`v` is not
|
|
Packit |
67cb25 |
owned by the new vector. When the view goes out of scope the original
|
|
Packit |
67cb25 |
vector :data:`v` and its block will continue to exist. The original
|
|
Packit |
67cb25 |
memory can only be deallocated by freeing the original vector. Of
|
|
Packit |
67cb25 |
course, the original vector should not be deallocated while the view is
|
|
Packit |
67cb25 |
still in use.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_vector_const_subvector` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_vector_subvector` but can be used for vectors which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_vector_subvector_with_stride (gsl_vector * v, size_t offset, size_t stride, size_t n)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_vector_const_subvector_with_stride (const gsl_vector * v, size_t offset, size_t stride, size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of a subvector of another vector
|
|
Packit |
67cb25 |
:data:`v` with an additional stride argument. The subvector is formed in
|
|
Packit |
67cb25 |
the same way as for :func:`gsl_vector_subvector` but the new vector has
|
|
Packit |
67cb25 |
:data:`n` elements with a step-size of :data:`stride` from one element to
|
|
Packit |
67cb25 |
the next in the original vector. Mathematically, the :data:`i`-th element
|
|
Packit |
67cb25 |
of the new vector :data:`v'` is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
v'(i) = v->data[(offset + i*stride)*v->stride]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Note that subvector views give direct access to the underlying elements
|
|
Packit |
67cb25 |
of the original vector. For example, the following code will zero the
|
|
Packit |
67cb25 |
even elements of the vector :data:`v` of length :code:`n`, while leaving the
|
|
Packit |
67cb25 |
odd elements untouched::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_vector_view v_even = gsl_vector_subvector_with_stride (v, 0, 2, n/2);
|
|
Packit |
67cb25 |
gsl_vector_set_zero (&v_even.vector);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
A vector view can be passed to any subroutine which takes a vector
|
|
Packit |
67cb25 |
argument just as a directly allocated vector would be, using
|
|
Packit |
67cb25 |
:code:`&view.vector`. For example, the following code
|
|
Packit |
67cb25 |
computes the norm of the odd elements of :data:`v` using the BLAS
|
|
Packit |
67cb25 |
routine :code:`dnrm2`::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_vector_view v_odd = gsl_vector_subvector_with_stride (v, 1, 2, n/2);
|
|
Packit |
67cb25 |
double r = gsl_blas_dnrm2 (&v_odd.vector);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_vector_const_subvector_with_stride` is equivalent
|
|
Packit |
67cb25 |
to :func:`gsl_vector_subvector_with_stride` but can be used for vectors
|
|
Packit |
67cb25 |
which are declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_vector_complex_real (gsl_vector_complex * v)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_vector_complex_const_real (const gsl_vector_complex * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the real parts of the complex
|
|
Packit |
67cb25 |
vector :data:`v`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_vector_complex_const_real` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_vector_complex_real` but can be used for vectors which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_vector_complex_imag (gsl_vector_complex * v)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_vector_complex_const_imag (const gsl_vector_complex * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the imaginary parts of the
|
|
Packit |
67cb25 |
complex vector :data:`v`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_vector_complex_const_imag` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_vector_complex_imag` but can be used for vectors which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_vector_view_array (double * base, size_t n)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_vector_const_view_array (const double * base, size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of an array. The start of the new
|
|
Packit |
67cb25 |
vector is given by :data:`base` and has :data:`n` elements. Mathematically,
|
|
Packit |
67cb25 |
the :data:`i`-th element of the new vector :data:`v'` is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
v'(i) = base[i]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The array containing the elements of :data:`v` is not owned by the new
|
|
Packit |
67cb25 |
vector view. When the view goes out of scope the original array will
|
|
Packit |
67cb25 |
continue to exist. The original memory can only be deallocated by
|
|
Packit |
67cb25 |
freeing the original pointer :data:`base`. Of course, the original array
|
|
Packit |
67cb25 |
should not be deallocated while the view is still in use.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_vector_const_view_array` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_vector_view_array` but can be used for arrays which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_vector_view_array_with_stride (double * base, size_t stride, size_t n)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_vector_const_view_array_with_stride (const double * base, size_t stride, size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of an array :data:`base` with an
|
|
Packit |
67cb25 |
additional stride argument. The subvector is formed in the same way as
|
|
Packit |
67cb25 |
for :func:`gsl_vector_view_array` but the new vector has :data:`n` elements
|
|
Packit |
67cb25 |
with a step-size of :data:`stride` from one element to the next in the
|
|
Packit |
67cb25 |
original array. Mathematically, the :data:`i`-th element of the new
|
|
Packit |
67cb25 |
vector :data:`v'` is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
v'(i) = base[i*stride]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Note that the view gives direct access to the underlying elements of the
|
|
Packit |
67cb25 |
original array. A vector view can be passed to any subroutine which
|
|
Packit |
67cb25 |
takes a vector argument just as a directly allocated vector would be,
|
|
Packit |
67cb25 |
using :code:`&view.vector`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_vector_const_view_array_with_stride` is
|
|
Packit |
67cb25 |
equivalent to :func:`gsl_vector_view_array_with_stride` but can be used
|
|
Packit |
67cb25 |
for arrays which are declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. @node Modifying subvector views
|
|
Packit |
67cb25 |
.. @subsection Modifying subvector views
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun int gsl_vector_view_from_vector (gsl_vector * v, gsl_vector * base, size_t offset, size_t n, size_t stride)
|
|
Packit |
67cb25 |
.. This function modifies and existing vector view :data:`v` to form a new
|
|
Packit |
67cb25 |
.. view of a vector :data:`base`, starting from element :data:`offset`. The
|
|
Packit |
67cb25 |
.. vector has :data:`n` elements separated by stride :data:`stride`. Any
|
|
Packit |
67cb25 |
.. existing view in :data:`v` will be lost as a result of this function.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun int gsl_vector_view_from_array (gsl_vector * v, double * base, size_t offset, size_t n, size_t stride)
|
|
Packit |
67cb25 |
.. This function modifies and existing vector view :data:`v` to form a new
|
|
Packit |
67cb25 |
.. view of an array :data:`base`, starting from element :data:`offset`. The
|
|
Packit |
67cb25 |
.. vector has :data:`n` elements separated by stride :data:`stride`. Any
|
|
Packit |
67cb25 |
.. existing view in :data:`v` will be lost as a result of this function.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. @deftypefun {gsl_vector *} gsl_vector_alloc_from_block (gsl_block * b, size_t offset, size_t n, size_t stride)
|
|
Packit |
67cb25 |
.. This function creates a vector as a slice of an existing block :data:`b`,
|
|
Packit |
67cb25 |
.. returning a pointer to a newly initialized vector struct. The start of
|
|
Packit |
67cb25 |
.. the vector is offset by :data:`offset` elements from the start of the
|
|
Packit |
67cb25 |
.. block. The vector has :data:`n` elements, with a step-size of :data:`stride`
|
|
Packit |
67cb25 |
.. from one element to the next. Mathematically, the :data:`i`-th element of
|
|
Packit |
67cb25 |
.. the vector is given by,
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @example
|
|
Packit |
67cb25 |
.. v(i) = b->data[offset + i*stride]
|
|
Packit |
67cb25 |
.. @end example
|
|
Packit |
67cb25 |
.. @noindent
|
|
Packit |
67cb25 |
.. where the index :data:`i` runs from 0 to :code:`n-1`.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. A null pointer is returned if the combined parameters
|
|
Packit |
67cb25 |
.. (:data:`offset`, :data:`n`, :data:`stride`) overrun the end of the block or if
|
|
Packit |
67cb25 |
.. insufficient memory is available to store the vector.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. The vector is only a view of the block :data:`b`, and the block is not
|
|
Packit |
67cb25 |
.. owned by the vector. When the vector is deallocated the block :data:`b`
|
|
Packit |
67cb25 |
.. will continue to exist. This memory can only be deallocated by freeing
|
|
Packit |
67cb25 |
.. the block itself. Of course, this block should not be deallocated while
|
|
Packit |
67cb25 |
.. the vector is still in use.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun {gsl_vector *} gsl_vector_alloc_from_vector (gsl_vector * :data:`v`, size_t offset, size_t n, size_t stride)
|
|
Packit |
67cb25 |
.. This function creates a vector as a slice of another vector :data:`v`,
|
|
Packit |
67cb25 |
.. returning a pointer to a newly initialized vector struct. The start of
|
|
Packit |
67cb25 |
.. the new vector is offset by :data:`offset` elements from the start of the
|
|
Packit |
67cb25 |
.. original vector. The new vector has :data:`n` elements, with a step-size
|
|
Packit |
67cb25 |
.. of :data:`stride` from one element to the next in the original vector.
|
|
Packit |
67cb25 |
.. Mathematically, the :data:`i`-th element of the new vector :data:`v'` is
|
|
Packit |
67cb25 |
.. given by,
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @example
|
|
Packit |
67cb25 |
.. v'(i) = v->data[(offset + i*stride)*v->stride]
|
|
Packit |
67cb25 |
.. @end example
|
|
Packit |
67cb25 |
.. @noindent
|
|
Packit |
67cb25 |
.. where the index :data:`i` runs from 0 to :code:`n-1`.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. A null pointer is returned if the combined parameters
|
|
Packit |
67cb25 |
.. (:data:`offset`, :data:`n`, :data:`stride`) overrun the end of the original
|
|
Packit |
67cb25 |
.. vector or if insufficient memory is available store the new vector.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. The new vector is only a view of the block underlying the original
|
|
Packit |
67cb25 |
.. vector, :data:`v`. The block is not owned by the new vector. When the new
|
|
Packit |
67cb25 |
.. vector is deallocated the original vector :data:`v` and its block will
|
|
Packit |
67cb25 |
.. continue to exist. The original memory can only be deallocated by
|
|
Packit |
67cb25 |
.. freeing the original vector. Of course, the original vector should not
|
|
Packit |
67cb25 |
.. be deallocated while the new vector is still in use.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Copying vectors
|
|
Packit |
67cb25 |
---------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Common operations on vectors such as addition and multiplication are
|
|
Packit |
67cb25 |
available in the BLAS part of the library (see :ref:`chap_blas-support`).
|
|
Packit |
67cb25 |
However, it is useful to have a small number of utility
|
|
Packit |
67cb25 |
functions which do not require the full BLAS code. The following
|
|
Packit |
67cb25 |
functions fall into this category.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_memcpy (gsl_vector * dest, const gsl_vector * src)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function copies the elements of the vector :data:`src` into the
|
|
Packit |
67cb25 |
vector :data:`dest`. The two vectors must have the same length.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_swap (gsl_vector * v, gsl_vector * w)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function exchanges the elements of the vectors :data:`v` and :data:`w`
|
|
Packit |
67cb25 |
by copying. The two vectors must have the same length.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Exchanging elements
|
|
Packit |
67cb25 |
-------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following functions can be used to exchange, or permute, the elements
|
|
Packit |
67cb25 |
of a vector.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_swap_elements (gsl_vector * v, size_t i, size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function exchanges the :data:`i`-th and :data:`j`-th elements of the
|
|
Packit |
67cb25 |
vector :data:`v` in-place.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_reverse (gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function reverses the order of the elements of the vector :data:`v`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Vector operations
|
|
Packit |
67cb25 |
-----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_add (gsl_vector * a, const gsl_vector * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function adds the elements of vector :data:`b` to the elements of
|
|
Packit |
67cb25 |
vector :data:`a`. The result :math:`a_i \leftarrow a_i + b_i` is stored
|
|
Packit |
67cb25 |
in :data:`a` and :data:`b` remains unchanged. The two vectors must have
|
|
Packit |
67cb25 |
the same length.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_sub (gsl_vector * a, const gsl_vector * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function subtracts the elements of vector :data:`b` from the elements of
|
|
Packit |
67cb25 |
vector :data:`a`. The result :math:`a_i \leftarrow a_i - b_i` is stored
|
|
Packit |
67cb25 |
in :data:`a` and :data:`b` remains unchanged. The two vectors must have the
|
|
Packit |
67cb25 |
same length.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_mul (gsl_vector * a, const gsl_vector * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function multiplies the elements of vector :data:`a` by the
|
|
Packit |
67cb25 |
elements of vector :data:`b`. The result :math:`a_i \leftarrow a_i * b_i`
|
|
Packit |
67cb25 |
is stored in :data:`a` and :data:`b` remains unchanged. The two
|
|
Packit |
67cb25 |
vectors must have the same length.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_div (gsl_vector * a, const gsl_vector * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function divides the elements of vector :data:`a` by the elements
|
|
Packit |
67cb25 |
of vector :data:`b`. The result :math:`a_i \leftarrow a_i / b_i` is
|
|
Packit |
67cb25 |
stored in :data:`a` and :data:`b` remains unchanged. The two vectors must
|
|
Packit |
67cb25 |
have the same length.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_scale (gsl_vector * a, const double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function multiplies the elements of vector :data:`a` by the
|
|
Packit |
67cb25 |
constant factor :data:`x`. The result :math:`a_i \leftarrow x a_i` is
|
|
Packit |
67cb25 |
stored in :data:`a`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_add_constant (gsl_vector * a, const double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function adds the constant value :data:`x` to the elements of the
|
|
Packit |
67cb25 |
vector :data:`a`. The result :math:`a_i \leftarrow a_i + x` is stored in
|
|
Packit |
67cb25 |
:data:`a`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Finding maximum and minimum elements of vectors
|
|
Packit |
67cb25 |
-----------------------------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following operations are only defined for real vectors.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double gsl_vector_max (const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the maximum value in the vector :data:`v`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double gsl_vector_min (const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the minimum value in the vector :data:`v`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_vector_minmax (const gsl_vector * v, double * min_out, double * max_out)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the minimum and maximum values in the vector
|
|
Packit |
67cb25 |
:data:`v`, storing them in :data:`min_out` and :data:`max_out`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: size_t gsl_vector_max_index (const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the index of the maximum value in the vector :data:`v`.
|
|
Packit |
67cb25 |
When there are several equal maximum elements then the lowest index is
|
|
Packit |
67cb25 |
returned.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: size_t gsl_vector_min_index (const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the index of the minimum value in the vector :data:`v`.
|
|
Packit |
67cb25 |
When there are several equal minimum elements then the lowest index is
|
|
Packit |
67cb25 |
returned.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_vector_minmax_index (const gsl_vector * v, size_t * imin, size_t * imax)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the indices of the minimum and maximum values in
|
|
Packit |
67cb25 |
the vector :data:`v`, storing them in :data:`imin` and :data:`imax`. When
|
|
Packit |
67cb25 |
there are several equal minimum or maximum elements then the lowest
|
|
Packit |
67cb25 |
indices are returned.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Vector properties
|
|
Packit |
67cb25 |
-----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following functions are defined for real and complex vectors. For
|
|
Packit |
67cb25 |
complex vectors both the real and imaginary parts must satisfy the
|
|
Packit |
67cb25 |
conditions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_isnull (const gsl_vector * v)
|
|
Packit |
67cb25 |
int gsl_vector_ispos (const gsl_vector * v)
|
|
Packit |
67cb25 |
int gsl_vector_isneg (const gsl_vector * v)
|
|
Packit |
67cb25 |
int gsl_vector_isnonneg (const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return 1 if all the elements of the vector :data:`v` are
|
|
Packit |
67cb25 |
zero, strictly positive, strictly negative, or non-negative
|
|
Packit |
67cb25 |
respectively, and 0 otherwise.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_vector_equal (const gsl_vector * u, const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns 1 if the vectors :data:`u` and :data:`v` are equal
|
|
Packit |
67cb25 |
(by comparison of element values) and 0 otherwise.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Example programs for vectors
|
|
Packit |
67cb25 |
----------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This program shows how to allocate, initialize and read from a vector
|
|
Packit |
67cb25 |
using the functions :func:`gsl_vector_alloc`, :func:`gsl_vector_set` and
|
|
Packit |
67cb25 |
:func:`gsl_vector_get`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/vector.c
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Here is the output from the program. The final loop attempts to read
|
|
Packit |
67cb25 |
outside the range of the vector :code:`v`, and the error is trapped by
|
|
Packit |
67cb25 |
the range-checking code in :func:`gsl_vector_get`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
$ ./a.out
|
|
Packit |
67cb25 |
v_0 = 1.23
|
|
Packit |
67cb25 |
v_1 = 2.23
|
|
Packit |
67cb25 |
v_2 = 3.23
|
|
Packit |
67cb25 |
gsl: vector_source.c:12: ERROR: index out of range
|
|
Packit |
67cb25 |
Default GSL error handler invoked.
|
|
Packit |
67cb25 |
Aborted (core dumped)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The next program shows how to write a vector to a file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/vectorw.c
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
After running this program the file :file:`test.dat` should contain the
|
|
Packit |
67cb25 |
elements of :code:`v`, written using the format specifier
|
|
Packit |
67cb25 |
:code:`%.5g`. The vector could then be read back in using the function
|
|
Packit |
67cb25 |
:code:`gsl_vector_fscanf (f, v)` as follows:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/vectorr.c
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: matrices
|
|
Packit |
67cb25 |
single: physical dimension, matrices
|
|
Packit |
67cb25 |
single: trailing dimension, matrices
|
|
Packit |
67cb25 |
single: leading dimension, matrices
|
|
Packit |
67cb25 |
single: ordering, matrix elements
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Matrices
|
|
Packit |
67cb25 |
========
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Matrices are defined by a :type:`gsl_matrix` structure which describes a
|
|
Packit |
67cb25 |
generalized slice of a block. Like a vector it represents a set of
|
|
Packit |
67cb25 |
elements in an area of memory, but uses two indices instead of one.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. type:: gsl_matrix
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The :type:`gsl_matrix` structure contains six components, the two
|
|
Packit |
67cb25 |
dimensions of the matrix, a physical dimension, a pointer to the memory
|
|
Packit |
67cb25 |
where the elements of the matrix are stored, :data:`data`, a pointer to
|
|
Packit |
67cb25 |
the block owned by the matrix :data:`block`, if any, and an ownership
|
|
Packit |
67cb25 |
flag, :data:`owner`. The physical dimension determines the memory layout
|
|
Packit |
67cb25 |
and can differ from the matrix dimension to allow the use of
|
|
Packit |
67cb25 |
submatrices. The :type:`gsl_matrix` structure is very simple and looks
|
|
Packit |
67cb25 |
like this::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
typedef struct
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
size_t size1;
|
|
Packit |
67cb25 |
size_t size2;
|
|
Packit |
67cb25 |
size_t tda;
|
|
Packit |
67cb25 |
double * data;
|
|
Packit |
67cb25 |
gsl_block * block;
|
|
Packit |
67cb25 |
int owner;
|
|
Packit |
67cb25 |
} gsl_matrix;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Matrices are stored in row-major order, meaning that each row of
|
|
Packit |
67cb25 |
elements forms a contiguous block in memory. This is the standard
|
|
Packit |
67cb25 |
"C-language ordering" of two-dimensional arrays. Note that Fortran
|
|
Packit |
67cb25 |
stores arrays in column-major order. The number of rows is :data:`size1`.
|
|
Packit |
67cb25 |
The range of valid row indices runs from 0 to :code:`size1 - 1`. Similarly
|
|
Packit |
67cb25 |
:data:`size2` is the number of columns. The range of valid column indices
|
|
Packit |
67cb25 |
runs from 0 to :code:`size2 - 1`. The physical row dimension :data:`tda`, or
|
|
Packit |
67cb25 |
*trailing dimension*, specifies the size of a row of the matrix as
|
|
Packit |
67cb25 |
laid out in memory.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
For example, in the following matrix :data:`size1` is 3, :data:`size2` is 4,
|
|
Packit |
67cb25 |
and :data:`tda` is 8. The physical memory layout of the matrix begins in
|
|
Packit |
67cb25 |
the top left hand-corner and proceeds from left to right along each row
|
|
Packit |
67cb25 |
in turn.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
00 01 02 03 XX XX XX XX
|
|
Packit |
67cb25 |
10 11 12 13 XX XX XX XX
|
|
Packit |
67cb25 |
20 21 22 23 XX XX XX XX
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Each unused memory location is represented by ":code:`XX`". The
|
|
Packit |
67cb25 |
pointer :data:`data` gives the location of the first element of the matrix
|
|
Packit |
67cb25 |
in memory. The pointer :data:`block` stores the location of the memory
|
|
Packit |
67cb25 |
block in which the elements of the matrix are located (if any). If the
|
|
Packit |
67cb25 |
matrix owns this block then the :data:`owner` field is set to one and the
|
|
Packit |
67cb25 |
block will be deallocated when the matrix is freed. If the matrix is
|
|
Packit |
67cb25 |
only a slice of a block owned by another object then the :data:`owner` field is
|
|
Packit |
67cb25 |
zero and any underlying block will not be freed.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for allocating and accessing matrices are defined in
|
|
Packit |
67cb25 |
:file:`gsl_matrix.h`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Matrix allocation
|
|
Packit |
67cb25 |
-----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for allocating memory to a matrix follow the style of
|
|
Packit |
67cb25 |
:code:`malloc` and :code:`free`. They also perform their own error
|
|
Packit |
67cb25 |
checking. If there is insufficient memory available to allocate a matrix
|
|
Packit |
67cb25 |
then the functions call the GSL error handler (with an error number of
|
|
Packit |
67cb25 |
:macro:`GSL_ENOMEM`) in addition to returning a null pointer. Thus if you
|
|
Packit |
67cb25 |
use the library error handler to abort your program then it isn't
|
|
Packit |
67cb25 |
necessary to check every :code:`alloc`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_matrix * gsl_matrix_alloc (size_t n1, size_t n2)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function creates a matrix of size :data:`n1` rows by :data:`n2` columns,
|
|
Packit |
67cb25 |
returning a pointer to a newly initialized matrix struct. A new block is
|
|
Packit |
67cb25 |
allocated for the elements of the matrix, and stored in the :data:`block`
|
|
Packit |
67cb25 |
component of the matrix struct. The block is "owned" by the matrix,
|
|
Packit |
67cb25 |
and will be deallocated when the matrix is deallocated. Requesting zero
|
|
Packit |
67cb25 |
for :data:`n1` or :data:`n2` is valid and returns a non-null result.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_matrix * gsl_matrix_calloc (size_t n1, size_t n2)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function allocates memory for a matrix of size :data:`n1` rows by
|
|
Packit |
67cb25 |
:data:`n2` columns and initializes all the elements of the matrix to zero.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_free (gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function frees a previously allocated matrix :data:`m`. If the
|
|
Packit |
67cb25 |
matrix was created using :func:`gsl_matrix_alloc` then the block
|
|
Packit |
67cb25 |
underlying the matrix will also be deallocated. If the matrix has
|
|
Packit |
67cb25 |
been created from another object then the memory is still owned by
|
|
Packit |
67cb25 |
that object and will not be deallocated.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: matrices, range-checking
|
|
Packit |
67cb25 |
single: range-checking for matrices
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Accessing matrix elements
|
|
Packit |
67cb25 |
-------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions for accessing the elements of a matrix use the same range
|
|
Packit |
67cb25 |
checking system as vectors. You can turn off range checking by recompiling
|
|
Packit |
67cb25 |
your program with the preprocessor definition
|
|
Packit |
67cb25 |
:macro:`GSL_RANGE_CHECK_OFF`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The elements of the matrix are stored in "C-order", where the second
|
|
Packit |
67cb25 |
index moves continuously through memory. More precisely, the element
|
|
Packit |
67cb25 |
accessed by the function :code:`gsl_matrix_get(m,i,j)` and
|
|
Packit |
67cb25 |
:code:`gsl_matrix_set(m,i,j,x)` is::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
m->data[i * m->tda + j]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where :data:`tda` is the physical row-length of the matrix.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double gsl_matrix_get (const gsl_matrix * m, const size_t i, const size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the :math:`(i,j)`-th element of a matrix
|
|
Packit |
67cb25 |
:data:`m`. If :data:`i` or :data:`j` lie outside the allowed range of 0 to
|
|
Packit |
67cb25 |
:code:`n1 - 1` and 0 to :code:`n2 - 1` then the error handler is invoked and 0
|
|
Packit |
67cb25 |
is returned. |inlinefn|
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_set (gsl_matrix * m, const size_t i, const size_t j, double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function sets the value of the :math:`(i,j)`-th element of a
|
|
Packit |
67cb25 |
matrix :data:`m` to :data:`x`. If :data:`i` or :data:`j` lies outside the
|
|
Packit |
67cb25 |
allowed range of 0 to :code:`n1 - 1` and 0 to :code:`n2 - 1` then the error
|
|
Packit |
67cb25 |
handler is invoked. |inlinefn|
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double * gsl_matrix_ptr (gsl_matrix * m, size_t i, size_t j)
|
|
Packit |
67cb25 |
const double * gsl_matrix_const_ptr (const gsl_matrix * m, size_t i, size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a pointer to the :math:`(i,j)`-th element of a
|
|
Packit |
67cb25 |
matrix :data:`m`. If :data:`i` or :data:`j` lie outside the allowed range of
|
|
Packit |
67cb25 |
0 to :code:`n1 - 1` and 0 to :code:`n2 - 1` then the error handler is invoked
|
|
Packit |
67cb25 |
and a null pointer is returned. |inlinefns|
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: matrices, initializing
|
|
Packit |
67cb25 |
single: initializing matrices
|
|
Packit |
67cb25 |
single: identity matrix
|
|
Packit |
67cb25 |
single: matrix, identity
|
|
Packit |
67cb25 |
single: zero matrix
|
|
Packit |
67cb25 |
single: matrix, zero
|
|
Packit |
67cb25 |
single: constant matrix
|
|
Packit |
67cb25 |
single: matrix, constant
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Initializing matrix elements
|
|
Packit |
67cb25 |
----------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_set_all (gsl_matrix * m, double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function sets all the elements of the matrix :data:`m` to the value
|
|
Packit |
67cb25 |
:data:`x`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_set_zero (gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function sets all the elements of the matrix :data:`m` to zero.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_set_identity (gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function sets the elements of the matrix :data:`m` to the
|
|
Packit |
67cb25 |
corresponding elements of the identity matrix, :math:`m(i,j) = \delta(i,j)`,
|
|
Packit |
67cb25 |
i.e. a unit diagonal with all off-diagonal elements zero.
|
|
Packit |
67cb25 |
This applies to both square and rectangular matrices.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Reading and writing matrices
|
|
Packit |
67cb25 |
----------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The library provides functions for reading and writing matrices to a file
|
|
Packit |
67cb25 |
as binary data or formatted text.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_fwrite (FILE * stream, const gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function writes the elements of the matrix :data:`m` to the stream
|
|
Packit |
67cb25 |
:data:`stream` in binary format. The return value is 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem writing to the file. Since the
|
|
Packit |
67cb25 |
data is written in the native binary format it may not be portable
|
|
Packit |
67cb25 |
between different architectures.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_fread (FILE * stream, gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function reads into the matrix :data:`m` from the open stream
|
|
Packit |
67cb25 |
:data:`stream` in binary format. The matrix :data:`m` must be preallocated
|
|
Packit |
67cb25 |
with the correct dimensions since the function uses the size of :data:`m` to
|
|
Packit |
67cb25 |
determine how many bytes to read. The return value is 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem reading from the file. The
|
|
Packit |
67cb25 |
data is assumed to have been written in the native binary format on the
|
|
Packit |
67cb25 |
same architecture.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_fprintf (FILE * stream, const gsl_matrix * m, const char * format)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function writes the elements of the matrix :data:`m` line-by-line to
|
|
Packit |
67cb25 |
the stream :data:`stream` using the format specifier :data:`format`, which
|
|
Packit |
67cb25 |
should be one of the :code:`%g`, :code:`%e` or :code:`%f` formats for
|
|
Packit |
67cb25 |
floating point numbers and :code:`%d` for integers. The function returns
|
|
Packit |
67cb25 |
0 for success and :macro:`GSL_EFAILED` if there was a problem writing to
|
|
Packit |
67cb25 |
the file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_fscanf (FILE * stream, gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function reads formatted data from the stream :data:`stream` into the
|
|
Packit |
67cb25 |
matrix :data:`m`. The matrix :data:`m` must be preallocated with the correct
|
|
Packit |
67cb25 |
dimensions since the function uses the size of :data:`m` to determine how many
|
|
Packit |
67cb25 |
numbers to read. The function returns 0 for success and
|
|
Packit |
67cb25 |
:macro:`GSL_EFAILED` if there was a problem reading from the file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Matrix views
|
|
Packit |
67cb25 |
------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. type:: gsl_matrix_view
|
|
Packit |
67cb25 |
gsl_matrix_const_view
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
A matrix view is a temporary object, stored on the stack, which can be
|
|
Packit |
67cb25 |
used to operate on a subset of matrix elements. Matrix views can be
|
|
Packit |
67cb25 |
defined for both constant and non-constant matrices using separate types
|
|
Packit |
67cb25 |
that preserve constness. A matrix view has the type
|
|
Packit |
67cb25 |
:type:`gsl_matrix_view` and a constant matrix view has the type
|
|
Packit |
67cb25 |
:type:`gsl_matrix_const_view`. In both cases the elements of the view
|
|
Packit |
67cb25 |
can by accessed using the :code:`matrix` component of the view object. A
|
|
Packit |
67cb25 |
pointer :code:`gsl_matrix *` or :code:`const gsl_matrix *` can be obtained
|
|
Packit |
67cb25 |
by taking the address of the :code:`matrix` component with the :code:`&`
|
|
Packit |
67cb25 |
operator. In addition to matrix views it is also possible to create
|
|
Packit |
67cb25 |
vector views of a matrix, such as row or column views.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_matrix_view gsl_matrix_submatrix (gsl_matrix * m, size_t k1, size_t k2, size_t n1, size_t n2)
|
|
Packit |
67cb25 |
gsl_matrix_const_view gsl_matrix_const_submatrix (const gsl_matrix * m, size_t k1, size_t k2, size_t n1, size_t n2)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a matrix view of a submatrix of the matrix
|
|
Packit |
67cb25 |
:data:`m`. The upper-left element of the submatrix is the element
|
|
Packit |
67cb25 |
(:data:`k1`, :data:`k2`) of the original matrix. The submatrix has :data:`n1`
|
|
Packit |
67cb25 |
rows and :data:`n2` columns. The physical number of columns in memory
|
|
Packit |
67cb25 |
given by :data:`tda` is unchanged. Mathematically, the
|
|
Packit |
67cb25 |
:math:`(i,j)`-th element of the new matrix is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
m'(i,j) = m->data[(k1*m->tda + k2) + i*m->tda + j]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n1 - 1` and the index :data:`j`
|
|
Packit |
67cb25 |
runs from 0 to :code:`n2 - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The :code:`data` pointer of the returned matrix struct is set to null if
|
|
Packit |
67cb25 |
the combined parameters (:data:`i`, :data:`j`, :data:`n1`, :data:`n2`, :data:`tda`)
|
|
Packit |
67cb25 |
overrun the ends of the original matrix.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The new matrix view is only a view of the block underlying the existing
|
|
Packit |
67cb25 |
matrix, :data:`m`. The block containing the elements of :data:`m` is not
|
|
Packit |
67cb25 |
owned by the new matrix view. When the view goes out of scope the
|
|
Packit |
67cb25 |
original matrix :data:`m` and its block will continue to exist. The
|
|
Packit |
67cb25 |
original memory can only be deallocated by freeing the original matrix.
|
|
Packit |
67cb25 |
Of course, the original matrix should not be deallocated while the view
|
|
Packit |
67cb25 |
is still in use.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_submatrix` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_submatrix` but can be used for matrices which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_matrix_view gsl_matrix_view_array (double * base, size_t n1, size_t n2)
|
|
Packit |
67cb25 |
gsl_matrix_const_view gsl_matrix_const_view_array (const double * base, size_t n1, size_t n2)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a matrix view of the array :data:`base`. The
|
|
Packit |
67cb25 |
matrix has :data:`n1` rows and :data:`n2` columns. The physical number of
|
|
Packit |
67cb25 |
columns in memory is also given by :data:`n2`. Mathematically, the
|
|
Packit |
67cb25 |
:math:`(i,j)`-th element of the new matrix is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
m'(i,j) = base[i*n2 + j]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n1 - 1` and the index :data:`j`
|
|
Packit |
67cb25 |
runs from 0 to :code:`n2 - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The new matrix is only a view of the array :data:`base`. When the view
|
|
Packit |
67cb25 |
goes out of scope the original array :data:`base` will continue to exist.
|
|
Packit |
67cb25 |
The original memory can only be deallocated by freeing the original
|
|
Packit |
67cb25 |
array. Of course, the original array should not be deallocated while
|
|
Packit |
67cb25 |
the view is still in use.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_view_array` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_view_array` but can be used for matrices which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_matrix_view gsl_matrix_view_array_with_tda (double * base, size_t n1, size_t n2, size_t tda)
|
|
Packit |
67cb25 |
gsl_matrix_const_view gsl_matrix_const_view_array_with_tda (const double * base, size_t n1, size_t n2, size_t tda)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a matrix view of the array :data:`base` with a
|
|
Packit |
67cb25 |
physical number of columns :data:`tda` which may differ from the corresponding
|
|
Packit |
67cb25 |
dimension of the matrix. The matrix has :data:`n1` rows and :data:`n2`
|
|
Packit |
67cb25 |
columns, and the physical number of columns in memory is given by
|
|
Packit |
67cb25 |
:data:`tda`. Mathematically, the :math:`(i,j)`-th element of the new
|
|
Packit |
67cb25 |
matrix is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
m'(i,j) = base[i*tda + j]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n1 - 1` and the index :data:`j`
|
|
Packit |
67cb25 |
runs from 0 to :code:`n2 - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The new matrix is only a view of the array :data:`base`. When the view
|
|
Packit |
67cb25 |
goes out of scope the original array :data:`base` will continue to exist.
|
|
Packit |
67cb25 |
The original memory can only be deallocated by freeing the original
|
|
Packit |
67cb25 |
array. Of course, the original array should not be deallocated while
|
|
Packit |
67cb25 |
the view is still in use.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_view_array_with_tda` is equivalent
|
|
Packit |
67cb25 |
to :func:`gsl_matrix_view_array_with_tda` but can be used for matrices
|
|
Packit |
67cb25 |
which are declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_matrix_view gsl_matrix_view_vector (gsl_vector * v, size_t n1, size_t n2)
|
|
Packit |
67cb25 |
gsl_matrix_const_view gsl_matrix_const_view_vector (const gsl_vector * v, size_t n1, size_t n2)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a matrix view of the vector :data:`v`. The matrix
|
|
Packit |
67cb25 |
has :data:`n1` rows and :data:`n2` columns. The vector must have unit
|
|
Packit |
67cb25 |
stride. The physical number of columns in memory is also given by
|
|
Packit |
67cb25 |
:data:`n2`. Mathematically, the :math:`(i,j)`-th element of the new
|
|
Packit |
67cb25 |
matrix is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
m'(i,j) = v->data[i*n2 + j]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n1 - 1` and the index :data:`j`
|
|
Packit |
67cb25 |
runs from 0 to :code:`n2 - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The new matrix is only a view of the vector :data:`v`. When the view
|
|
Packit |
67cb25 |
goes out of scope the original vector :data:`v` will continue to exist.
|
|
Packit |
67cb25 |
The original memory can only be deallocated by freeing the original
|
|
Packit |
67cb25 |
vector. Of course, the original vector should not be deallocated while
|
|
Packit |
67cb25 |
the view is still in use.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_view_vector` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_view_vector` but can be used for matrices which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_matrix_view gsl_matrix_view_vector_with_tda (gsl_vector * v, size_t n1, size_t n2, size_t tda)
|
|
Packit |
67cb25 |
gsl_matrix_const_view gsl_matrix_const_view_vector_with_tda (const gsl_vector * v, size_t n1, size_t n2, size_t tda)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a matrix view of the vector :data:`v` with a
|
|
Packit |
67cb25 |
physical number of columns :data:`tda` which may differ from the
|
|
Packit |
67cb25 |
corresponding matrix dimension. The vector must have unit stride. The
|
|
Packit |
67cb25 |
matrix has :data:`n1` rows and :data:`n2` columns, and the physical number
|
|
Packit |
67cb25 |
of columns in memory is given by :data:`tda`. Mathematically, the
|
|
Packit |
67cb25 |
:math:`(i,j)`-th element of the new matrix is given by::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
m'(i,j) = v->data[i*tda + j]
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
where the index :data:`i` runs from 0 to :code:`n1 - 1` and the index :data:`j`
|
|
Packit |
67cb25 |
runs from 0 to :code:`n2 - 1`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The new matrix is only a view of the vector :data:`v`. When the view
|
|
Packit |
67cb25 |
goes out of scope the original vector :data:`v` will continue to exist.
|
|
Packit |
67cb25 |
The original memory can only be deallocated by freeing the original
|
|
Packit |
67cb25 |
vector. Of course, the original vector should not be deallocated while
|
|
Packit |
67cb25 |
the view is still in use.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_view_vector_with_tda` is equivalent
|
|
Packit |
67cb25 |
to :func:`gsl_matrix_view_vector_with_tda` but can be used for matrices
|
|
Packit |
67cb25 |
which are declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. @node Modifying matrix views
|
|
Packit |
67cb25 |
.. @subsection Modifying matrix views
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun int gsl_matrix_view_from_matrix (gsl_matrix * m, gsl_matrix * mm, const size_t k1, const size_t k2, const size_t n1, const size_t n2)
|
|
Packit |
67cb25 |
.. This function modifies and existing matrix view :data:`m` to form a new
|
|
Packit |
67cb25 |
.. view of a matrix :data:`mm`, starting from element (:data:`k1`, :data:`k2`).
|
|
Packit |
67cb25 |
.. The matrix view has :data:`n1` rows and :data:`n2` columns. Any existing
|
|
Packit |
67cb25 |
.. view in :data:`m` will be lost as a result of this function.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun int gsl_matrix_view_from_vector (gsl_matrix * m, gsl_vector * v, const size_t offset, const size_t n1, const size_t n2)
|
|
Packit |
67cb25 |
.. This function modifies and existing matrix view :data:`m` to form a new
|
|
Packit |
67cb25 |
.. view of a vector :data:`v`, starting from element :data:`offset`. The
|
|
Packit |
67cb25 |
.. vector has :data:`n1` rows and :data:`n2` columns. Any
|
|
Packit |
67cb25 |
.. existing view in :data:`m` will be lost as a result of this function.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun int gsl_matrix_view_from_array (gsl_matrix * m, double * base, const size_t offset, const size_t n1, const size_t n2)
|
|
Packit |
67cb25 |
.. This function modifies and existing matrix view :data:`m` to form a new
|
|
Packit |
67cb25 |
.. view of an array :data:`base`, starting from element :data:`offset`. The
|
|
Packit |
67cb25 |
.. matrix has :data:`n1` rows and :data:`n2` columns. Any
|
|
Packit |
67cb25 |
.. existing view in :data:`m` will be lost as a result of this function.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun {gsl_matrix *} gsl_matrix_alloc_from_block (gsl_block * b, size_t offset, size_t n1, size_t n2, size_t tda)
|
|
Packit |
67cb25 |
.. This function creates a matrix as a slice of the block :data:`b`,
|
|
Packit |
67cb25 |
.. returning a pointer to a newly initialized matrix struct. The start of
|
|
Packit |
67cb25 |
.. the matrix is offset by :data:`offset` elements from the start of the
|
|
Packit |
67cb25 |
.. block. The matrix has :data:`n1` rows and :data:`n2` columns, with the
|
|
Packit |
67cb25 |
.. physical number of columns in memory given by :data:`tda`.
|
|
Packit |
67cb25 |
.. Mathematically, the (:data:`i`, :data:`j`)-th element of the matrix is given by,
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @example
|
|
Packit |
67cb25 |
.. m(i,j) = b->data[offset + i*tda + j]
|
|
Packit |
67cb25 |
.. @end example
|
|
Packit |
67cb25 |
.. @noindent
|
|
Packit |
67cb25 |
.. where the index :data:`i` runs from 0 to :code:`n1-1` and the index :data:`j`
|
|
Packit |
67cb25 |
.. runs from 0 to :code:`n2-1`.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. A null pointer is returned if the combined parameters
|
|
Packit |
67cb25 |
.. (:data:`offset`, :data:`n1`, :data:`n2`, :data:`tda`) overrun the end of the block
|
|
Packit |
67cb25 |
.. or if insufficient memory is available to store the matrix.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. The matrix is only a view of the block :data:`b`, and the block is not
|
|
Packit |
67cb25 |
.. owned by the matrix. When the matrix is deallocated the block :data:`b`
|
|
Packit |
67cb25 |
.. will continue to exist. This memory can only be deallocated by freeing
|
|
Packit |
67cb25 |
.. the block itself. Of course, this block should not be deallocated while
|
|
Packit |
67cb25 |
.. the matrix is still in use.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun {gsl_matrix *} gsl_matrix_alloc_from_matrix (gsl_matrix * m, size_t k1, size_t k2, size_t n1, size_t n2)
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. This function creates a matrix as a submatrix of the matrix :data:`m`,
|
|
Packit |
67cb25 |
.. returning a pointer to a newly initialized matrix struct. The upper-left
|
|
Packit |
67cb25 |
.. element of the submatrix is the element (:data:`k1`, :data:`k2`) of the
|
|
Packit |
67cb25 |
.. original matrix. The submatrix has :data:`n1` rows and :data:`n2` columns.
|
|
Packit |
67cb25 |
.. The physical number of columns in memory given by :data:`tda` is
|
|
Packit |
67cb25 |
.. unchanged. Mathematically, the (:data:`i`, :data:`j`)-th element of the
|
|
Packit |
67cb25 |
.. new matrix is given by,
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @example
|
|
Packit |
67cb25 |
.. m'(i,j) = m->data[(k1*m->tda + k2) + i*m->tda + j]
|
|
Packit |
67cb25 |
.. @end example
|
|
Packit |
67cb25 |
.. @noindent
|
|
Packit |
67cb25 |
.. where the index :data:`i` runs from 0 to :code:`n1-1` and the index :data:`j`
|
|
Packit |
67cb25 |
.. runs from 0 to :code:`n2-1`.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. A null pointer is returned if the combined parameters
|
|
Packit |
67cb25 |
.. (:data:`k1`, :data:`k2`, :data:`n1`, :data:`n2`, :data:`tda`) overrun the end of the
|
|
Packit |
67cb25 |
.. original matrix or if insufficient memory is available to store the matrix.
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. The new matrix is only a view of the block underlying the existing
|
|
Packit |
67cb25 |
.. matrix, :data:`m`. The block is not owned by the new matrix. When the new
|
|
Packit |
67cb25 |
.. matrix is deallocated the original matrix :data:`m` and its block will
|
|
Packit |
67cb25 |
.. continue to exist. The original memory can only be deallocated by
|
|
Packit |
67cb25 |
.. freeing the original matrix. Of course, the original matrix should not
|
|
Packit |
67cb25 |
.. be deallocated while the new matrix is still in use.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Creating row and column views
|
|
Packit |
67cb25 |
-----------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
In general there are two ways to access an object, by reference or by
|
|
Packit |
67cb25 |
copying. The functions described in this section create vector views
|
|
Packit |
67cb25 |
which allow access to a row or column of a matrix by reference.
|
|
Packit |
67cb25 |
Modifying elements of the view is equivalent to modifying the matrix,
|
|
Packit |
67cb25 |
since both the vector view and the matrix point to the same memory
|
|
Packit |
67cb25 |
block.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_matrix_row (gsl_matrix * m, size_t i)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_matrix_const_row (const gsl_matrix * m, size_t i)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the :data:`i`-th row of the matrix
|
|
Packit |
67cb25 |
:data:`m`. The :code:`data` pointer of the new vector is set to null if
|
|
Packit |
67cb25 |
:data:`i` is out of range.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_row` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_row` but can be used for matrices which are declared
|
|
Packit |
67cb25 |
:code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_matrix_column (gsl_matrix * m, size_t j)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_matrix_const_column (const gsl_matrix * m, size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the :data:`j`-th column of the
|
|
Packit |
67cb25 |
matrix :data:`m`. The :code:`data` pointer of the new vector is set to
|
|
Packit |
67cb25 |
null if :data:`j` is out of range.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_column` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_column` but can be used for matrices which are declared
|
|
Packit |
67cb25 |
:code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_matrix_subrow (gsl_matrix * m, size_t i, size_t offset, size_t n)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_matrix_const_subrow (const gsl_matrix * m, size_t i, size_t offset, size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the :data:`i`-th row of the matrix
|
|
Packit |
67cb25 |
:data:`m` beginning at :data:`offset` elements past the first column and
|
|
Packit |
67cb25 |
containing :data:`n` elements. The :code:`data` pointer of the new vector
|
|
Packit |
67cb25 |
is set to null if :data:`i`, :data:`offset`, or :data:`n` are out of range.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_subrow` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_subrow` but can be used for matrices which are declared
|
|
Packit |
67cb25 |
:code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_matrix_subcolumn (gsl_matrix * m, size_t j, size_t offset, size_t n)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_matrix_const_subcolumn (const gsl_matrix * m, size_t j, size_t offset, size_t n)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the :data:`j`-th column of the matrix
|
|
Packit |
67cb25 |
:data:`m` beginning at :data:`offset` elements past the first row and
|
|
Packit |
67cb25 |
containing :data:`n` elements. The :code:`data` pointer of the new vector
|
|
Packit |
67cb25 |
is set to null if :data:`j`, :data:`offset`, or :data:`n` are out of range.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_subcolumn` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_subcolumn` but can be used for matrices which are declared
|
|
Packit |
67cb25 |
:code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: matrix diagonal
|
|
Packit |
67cb25 |
single: diagonal, of a matrix
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_matrix_diagonal (gsl_matrix * m)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_matrix_const_diagonal (const gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the diagonal of the matrix
|
|
Packit |
67cb25 |
:data:`m`. The matrix :data:`m` is not required to be square. For a
|
|
Packit |
67cb25 |
rectangular matrix the length of the diagonal is the same as the smaller
|
|
Packit |
67cb25 |
dimension of the matrix.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_diagonal` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_diagonal` but can be used for matrices which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: matrix subdiagonal
|
|
Packit |
67cb25 |
single: subdiagonal, of a matrix
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_matrix_subdiagonal (gsl_matrix * m, size_t k)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_matrix_const_subdiagonal (const gsl_matrix * m, size_t k)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the :data:`k`-th subdiagonal of
|
|
Packit |
67cb25 |
the matrix :data:`m`. The matrix :data:`m` is not required to be square.
|
|
Packit |
67cb25 |
The diagonal of the matrix corresponds to :math:`k = 0`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_subdiagonal` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_subdiagonal` but can be used for matrices which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. index::
|
|
Packit |
67cb25 |
single: matrix superdiagonal
|
|
Packit |
67cb25 |
single: superdiagonal, matrix
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: gsl_vector_view gsl_matrix_superdiagonal (gsl_matrix * m, size_t k)
|
|
Packit |
67cb25 |
gsl_vector_const_view gsl_matrix_const_superdiagonal (const gsl_matrix * m, size_t k)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return a vector view of the :data:`k`-th superdiagonal of
|
|
Packit |
67cb25 |
the matrix :data:`m`. The matrix :data:`m` is not required to be square. The
|
|
Packit |
67cb25 |
diagonal of the matrix corresponds to :math:`k = 0`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The function :func:`gsl_matrix_const_superdiagonal` is equivalent to
|
|
Packit |
67cb25 |
:func:`gsl_matrix_superdiagonal` but can be used for matrices which are
|
|
Packit |
67cb25 |
declared :code:`const`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. @deftypefun {gsl_vector *} gsl_vector_alloc_row_from_matrix (gsl_matrix * m, size_t i)
|
|
Packit |
67cb25 |
.. This function allocates a new :type:`gsl_vector` struct which points to
|
|
Packit |
67cb25 |
.. the :data:`i`-th row of the matrix :data:`m`.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
..
|
|
Packit |
67cb25 |
.. @deftypefun {gsl_vector *} gsl_vector_alloc_col_from_matrix (gsl_matrix * m, size_t j)
|
|
Packit |
67cb25 |
.. This function allocates a new :type:`gsl_vector` struct which points to
|
|
Packit |
67cb25 |
.. the :data:`j`-th column of the matrix :data:`m`.
|
|
Packit |
67cb25 |
.. @end deftypefun
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Copying matrices
|
|
Packit |
67cb25 |
----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_memcpy (gsl_matrix * dest, const gsl_matrix * src)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function copies the elements of the matrix :data:`src` into the
|
|
Packit |
67cb25 |
matrix :data:`dest`. The two matrices must have the same size.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_swap (gsl_matrix * m1, gsl_matrix * m2)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function exchanges the elements of the matrices :data:`m1` and
|
|
Packit |
67cb25 |
:data:`m2` by copying. The two matrices must have the same size.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Copying rows and columns
|
|
Packit |
67cb25 |
------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The functions described in this section copy a row or column of a matrix
|
|
Packit |
67cb25 |
into a vector. This allows the elements of the vector and the matrix to
|
|
Packit |
67cb25 |
be modified independently. Note that if the matrix and the vector point
|
|
Packit |
67cb25 |
to overlapping regions of memory then the result will be undefined. The
|
|
Packit |
67cb25 |
same effect can be achieved with more generality using
|
|
Packit |
67cb25 |
:func:`gsl_vector_memcpy` with vector views of rows and columns.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_get_row (gsl_vector * v, const gsl_matrix * m, size_t i)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function copies the elements of the :data:`i`-th row of the matrix
|
|
Packit |
67cb25 |
:data:`m` into the vector :data:`v`. The length of the vector must be the
|
|
Packit |
67cb25 |
same as the length of the row.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_get_col (gsl_vector * v, const gsl_matrix * m, size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function copies the elements of the :data:`j`-th column of the matrix
|
|
Packit |
67cb25 |
:data:`m` into the vector :data:`v`. The length of the vector must be the
|
|
Packit |
67cb25 |
same as the length of the column.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_set_row (gsl_matrix * m, size_t i, const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function copies the elements of the vector :data:`v` into the
|
|
Packit |
67cb25 |
:data:`i`-th row of the matrix :data:`m`. The length of the vector must be
|
|
Packit |
67cb25 |
the same as the length of the row.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_set_col (gsl_matrix * m, size_t j, const gsl_vector * v)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function copies the elements of the vector :data:`v` into the
|
|
Packit |
67cb25 |
:data:`j`-th column of the matrix :data:`m`. The length of the vector must be
|
|
Packit |
67cb25 |
the same as the length of the column.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Exchanging rows and columns
|
|
Packit |
67cb25 |
---------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following functions can be used to exchange the rows and columns of
|
|
Packit |
67cb25 |
a matrix.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_swap_rows (gsl_matrix * m, size_t i, size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function exchanges the :data:`i`-th and :data:`j`-th rows of the matrix
|
|
Packit |
67cb25 |
:data:`m` in-place.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_swap_columns (gsl_matrix * m, size_t i, size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function exchanges the :data:`i`-th and :data:`j`-th columns of the
|
|
Packit |
67cb25 |
matrix :data:`m` in-place.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_swap_rowcol (gsl_matrix * m, size_t i, size_t j)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function exchanges the :data:`i`-th row and :data:`j`-th column of the
|
|
Packit |
67cb25 |
matrix :data:`m` in-place. The matrix must be square for this operation to
|
|
Packit |
67cb25 |
be possible.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_transpose_memcpy (gsl_matrix * dest, const gsl_matrix * src)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function makes the matrix :data:`dest` the transpose of the matrix
|
|
Packit |
67cb25 |
:data:`src` by copying the elements of :data:`src` into :data:`dest`. This
|
|
Packit |
67cb25 |
function works for all matrices provided that the dimensions of the matrix
|
|
Packit |
67cb25 |
:data:`dest` match the transposed dimensions of the matrix :data:`src`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_transpose (gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function replaces the matrix :data:`m` by its transpose by copying
|
|
Packit |
67cb25 |
the elements of the matrix in-place. The matrix must be square for this
|
|
Packit |
67cb25 |
operation to be possible.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Matrix operations
|
|
Packit |
67cb25 |
-----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following operations are defined for real and complex matrices.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_add (gsl_matrix * a, const gsl_matrix * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function adds the elements of matrix :data:`b` to the elements of
|
|
Packit |
67cb25 |
matrix :data:`a`. The result :math:`a(i,j) \leftarrow a(i,j) + b(i,j)`
|
|
Packit |
67cb25 |
is stored in :data:`a` and :data:`b` remains unchanged. The two matrices
|
|
Packit |
67cb25 |
must have the same dimensions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_sub (gsl_matrix * a, const gsl_matrix * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function subtracts the elements of matrix :data:`b` from the
|
|
Packit |
67cb25 |
elements of matrix :data:`a`. The result :math:`a(i,j) \leftarrow a(i,j) - b(i,j)`
|
|
Packit |
67cb25 |
is stored in :data:`a` and :data:`b` remains unchanged. The two
|
|
Packit |
67cb25 |
matrices must have the same dimensions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_mul_elements (gsl_matrix * a, const gsl_matrix * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function multiplies the elements of matrix :data:`a` by the
|
|
Packit |
67cb25 |
elements of matrix :data:`b`. The result :math:`a(i,j) \leftarrow a(i,j) * b(i,j)`
|
|
Packit |
67cb25 |
is stored in :data:`a` and :data:`b` remains unchanged. The two
|
|
Packit |
67cb25 |
matrices must have the same dimensions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_div_elements (gsl_matrix * a, const gsl_matrix * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function divides the elements of matrix :data:`a` by the elements
|
|
Packit |
67cb25 |
of matrix :data:`b`. The result :math:`a(i,j) \leftarrow a(i,j) / b(i,j)`
|
|
Packit |
67cb25 |
is stored in :data:`a` and :data:`b` remains unchanged. The two
|
|
Packit |
67cb25 |
matrices must have the same dimensions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_scale (gsl_matrix * a, const double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function multiplies the elements of matrix :data:`a` by the
|
|
Packit |
67cb25 |
constant factor :data:`x`. The result :math:`a(i,j) \leftarrow x a(i,j)`
|
|
Packit |
67cb25 |
is stored in :data:`a`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_add_constant (gsl_matrix * a, const double x)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function adds the constant value :data:`x` to the elements of the
|
|
Packit |
67cb25 |
matrix :data:`a`. The result :math:`a(i,j) \leftarrow a(i,j) + x` is
|
|
Packit |
67cb25 |
stored in :data:`a`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Finding maximum and minimum elements of matrices
|
|
Packit |
67cb25 |
------------------------------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following operations are only defined for real matrices.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double gsl_matrix_max (const gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the maximum value in the matrix :data:`m`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: double gsl_matrix_min (const gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the minimum value in the matrix :data:`m`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_minmax (const gsl_matrix * m, double * min_out, double * max_out)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the minimum and maximum values in the matrix
|
|
Packit |
67cb25 |
:data:`m`, storing them in :data:`min_out` and :data:`max_out`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_max_index (const gsl_matrix * m, size_t * imax, size_t * jmax)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the indices of the maximum value in the matrix
|
|
Packit |
67cb25 |
:data:`m`, storing them in :data:`imax` and :data:`jmax`. When there are
|
|
Packit |
67cb25 |
several equal maximum elements then the first element found is returned,
|
|
Packit |
67cb25 |
searching in row-major order.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_min_index (const gsl_matrix * m, size_t * imin, size_t * jmin)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the indices of the minimum value in the matrix
|
|
Packit |
67cb25 |
:data:`m`, storing them in :data:`imin` and :data:`jmin`. When there are
|
|
Packit |
67cb25 |
several equal minimum elements then the first element found is returned,
|
|
Packit |
67cb25 |
searching in row-major order.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: void gsl_matrix_minmax_index (const gsl_matrix * m, size_t * imin, size_t * jmin, size_t * imax, size_t * jmax)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns the indices of the minimum and maximum values in
|
|
Packit |
67cb25 |
the matrix :data:`m`, storing them in (:data:`imin`, :data:`jmin`) and
|
|
Packit |
67cb25 |
(:data:`imax`, :data:`jmax`). When there are several equal minimum or maximum
|
|
Packit |
67cb25 |
elements then the first elements found are returned, searching in
|
|
Packit |
67cb25 |
row-major order.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Matrix properties
|
|
Packit |
67cb25 |
-----------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following functions are defined for real and complex matrices.
|
|
Packit |
67cb25 |
For complex matrices both the real and imaginary parts must satisfy
|
|
Packit |
67cb25 |
the conditions.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_isnull (const gsl_matrix * m)
|
|
Packit |
67cb25 |
int gsl_matrix_ispos (const gsl_matrix * m)
|
|
Packit |
67cb25 |
int gsl_matrix_isneg (const gsl_matrix * m)
|
|
Packit |
67cb25 |
int gsl_matrix_isnonneg (const gsl_matrix * m)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
These functions return 1 if all the elements of the matrix :data:`m` are
|
|
Packit |
67cb25 |
zero, strictly positive, strictly negative, or non-negative
|
|
Packit |
67cb25 |
respectively, and 0 otherwise. To test whether a matrix is
|
|
Packit |
67cb25 |
positive-definite, use the :ref:`Cholesky decomposition <sec_cholesky-decomposition>`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. function:: int gsl_matrix_equal (const gsl_matrix * a, const gsl_matrix * b)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
This function returns 1 if the matrices :data:`a` and :data:`b` are equal
|
|
Packit |
67cb25 |
(by comparison of element values) and 0 otherwise.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Example programs for matrices
|
|
Packit |
67cb25 |
-----------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The program below shows how to allocate, initialize and read from a matrix
|
|
Packit |
67cb25 |
using the functions :func:`gsl_matrix_alloc`, :func:`gsl_matrix_set` and
|
|
Packit |
67cb25 |
:func:`gsl_matrix_get`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/matrix.c
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Here is the output from the program. The final loop attempts to read
|
|
Packit |
67cb25 |
outside the range of the matrix :code:`m`, and the error is trapped by
|
|
Packit |
67cb25 |
the range-checking code in :func:`gsl_matrix_get`.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
$ ./a.out
|
|
Packit |
67cb25 |
m(0,0) = 0.23
|
|
Packit |
67cb25 |
m(0,1) = 1.23
|
|
Packit |
67cb25 |
m(0,2) = 2.23
|
|
Packit |
67cb25 |
m(1,0) = 100.23
|
|
Packit |
67cb25 |
m(1,1) = 101.23
|
|
Packit |
67cb25 |
m(1,2) = 102.23
|
|
Packit |
67cb25 |
...
|
|
Packit |
67cb25 |
m(9,2) = 902.23
|
|
Packit |
67cb25 |
gsl: matrix_source.c:13: ERROR: first index out of range
|
|
Packit |
67cb25 |
Default GSL error handler invoked.
|
|
Packit |
67cb25 |
Aborted (core dumped)
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The next program shows how to write a matrix to a file.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/matrixw.c
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
After running this program the file :file:`test.dat` should contain the
|
|
Packit |
67cb25 |
elements of :code:`m`, written in binary format. The matrix which is read
|
|
Packit |
67cb25 |
back in using the function :func:`gsl_matrix_fread` should be exactly
|
|
Packit |
67cb25 |
equal to the original matrix.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The following program demonstrates the use of vector views. The program
|
|
Packit |
67cb25 |
computes the column norms of a matrix.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/vectorview.c
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
Here is the output of the program,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. include:: examples/vectorview.txt
|
|
Packit |
67cb25 |
:code:
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The results can be confirmed using |octave|::
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
$ octave
|
|
Packit |
67cb25 |
GNU Octave, version 2.0.16.92
|
|
Packit |
67cb25 |
octave> m = sin(0:9)' * ones(1,10)
|
|
Packit |
67cb25 |
+ ones(10,1) * cos(0:9);
|
|
Packit |
67cb25 |
octave> sqrt(sum(m.^2))
|
|
Packit |
67cb25 |
ans =
|
|
Packit |
67cb25 |
4.3146 3.1205 2.1932 3.2611 2.5342 2.5728
|
|
Packit |
67cb25 |
4.2047 3.6520 2.0852 3.0731
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
References and Further Reading
|
|
Packit |
67cb25 |
------------------------------
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
The block, vector and matrix objects in GSL follow the :code:`valarray`
|
|
Packit |
67cb25 |
model of C++. A description of this model can be found in the following
|
|
Packit |
67cb25 |
reference,
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
* B. Stroustrup, The C++ Programming Language (3rd Ed),
|
|
Packit |
67cb25 |
Section 22.4 Vector Arithmetic. Addison-Wesley 1997, ISBN 0-201-88954-4.
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. rubric:: Footnotes
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
.. [#f1] Range checking is available in the GNU C Compiler bounds-checking extension,
|
|
Packit |
67cb25 |
but it is not part of the default installation of GCC. Memory accesses
|
|
Packit |
67cb25 |
can also be checked with Valgrind or the :code:`gcc -fmudflap`
|
|
Packit |
67cb25 |
memory protection option.
|