~ubuntu-branches/ubuntu/trusty/gsl-ref-html/trusty

« back to all changes in this revision

Viewing changes to gsl-ref_8.html

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2006-04-12 19:46:32 UTC
  • mfrom: (1.3.1 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060412194632-c9lodpl075pv9si3
Tags: 1.8-1
* New upstream release 1.8
* As with previous releases, the sources were obtained from the FSF web 
  pages by means of a wget call (c.f. the debian/rules target 'upstream')

* debian/control: Standards-Version increased to 3.6.2
* debian/copyright: Updated FSF address
* debian/rules: Set DH_COMPAT=4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<HTML>
2
 
<HEAD>
3
 
<!-- This HTML file has been created by texi2html 1.54+ (gsl)
4
 
     from ../gsl-ref.texi -->
5
 
 
6
 
<TITLE>GNU Scientific Library -- Reference Manual - Vectors and Matrices</TITLE>
7
 
<!-- <LINK rel="stylesheet" title="Default Style Sheet" href="/css/texinfo.css" type="text/css"> -->
8
 
<link href="gsl-ref_9.html" rel=Next>
9
 
<link href="gsl-ref_7.html" rel=Previous>
10
 
<link href="gsl-ref_toc.html" rel=ToC>
11
 
 
12
 
</HEAD>
13
 
<BODY>
14
 
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_7.html">previous</A>, <A HREF="gsl-ref_9.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
15
 
<P><HR><P>
16
 
 
17
 
 
18
 
<H1><A NAME="SEC152" HREF="gsl-ref_toc.html#TOC152">Vectors and Matrices</A></H1>
19
 
<P>
20
 
<A NAME="IDX805"></A>
21
 
<A NAME="IDX806"></A>
22
 
<A NAME="IDX807"></A>
23
 
 
24
 
</P>
25
 
<P>
26
 
The functions described in this chapter provide a simple vector and
27
 
matrix interface to ordinary C arrays. The memory management of these
28
 
arrays is implemented using a single underlying type, known as a
29
 
block. By writing your functions in terms of vectors and matrices you
30
 
can pass a single structure containing both data and dimensions as an
31
 
argument without needing additional function parameters.  The structures
32
 
are compatible with the vector and matrix formats used by BLAS
33
 
routines.
34
 
 
35
 
</P>
36
 
 
37
 
 
38
 
 
39
 
<H2><A NAME="SEC153" HREF="gsl-ref_toc.html#TOC153">Data types</A></H2>
40
 
 
41
 
<P>
42
 
All the functions are available for each of the standard data-types.
43
 
The versions for <CODE>double</CODE> have the prefix <CODE>gsl_block</CODE>,
44
 
<CODE>gsl_vector</CODE> and <CODE>gsl_matrix</CODE>.  Similarly the versions for
45
 
single-precision <CODE>float</CODE> arrays have the prefix
46
 
<CODE>gsl_block_float</CODE>, <CODE>gsl_vector_float</CODE> and
47
 
<CODE>gsl_matrix_float</CODE>.  The full list of available types is given
48
 
below,
49
 
 
50
 
</P>
51
 
 
52
 
<PRE class="example">
53
 
gsl_block                       double         
54
 
gsl_block_float                 float         
55
 
gsl_block_long_double           long double   
56
 
gsl_block_int                   int           
57
 
gsl_block_uint                  unsigned int  
58
 
gsl_block_long                  long          
59
 
gsl_block_ulong                 unsigned long 
60
 
gsl_block_short                 short         
61
 
gsl_block_ushort                unsigned short
62
 
gsl_block_char                  char          
63
 
gsl_block_uchar                 unsigned char 
64
 
gsl_block_complex               complex double        
65
 
gsl_block_complex_float         complex float         
66
 
gsl_block_complex_long_double   complex long double   
67
 
</PRE>
68
 
 
69
 
<P>
70
 
Corresponding types exist for the <CODE>gsl_vector</CODE> and
71
 
<CODE>gsl_matrix</CODE> functions.
72
 
 
73
 
</P>
74
 
 
75
 
 
76
 
 
77
 
<H2><A NAME="SEC154" HREF="gsl-ref_toc.html#TOC154">Blocks</A></H2>
78
 
 
79
 
<P>
80
 
For consistency all memory is allocated through a <CODE>gsl_block</CODE>
81
 
structure.  The structure contains two components, the size of an area of
82
 
memory and a pointer to the memory.  The <CODE>gsl_block</CODE> structure looks
83
 
like this,
84
 
 
85
 
</P>
86
 
 
87
 
<PRE class="example">
88
 
typedef struct
89
 
{
90
 
  size_t size;
91
 
  double * data;
92
 
} gsl_block;
93
 
</PRE>
94
 
 
95
 
<P>
96
 
Vectors and matrices are made by <I>slicing</I> an underlying block. A
97
 
slice is a set of elements formed from an initial offset and a
98
 
combination of indices and step-sizes. In the case of a matrix the
99
 
step-size for the column index represents the row-length.  The step-size
100
 
for a vector is known as the <I>stride</I>.
101
 
 
102
 
</P>
103
 
<P>
104
 
The functions for allocating and deallocating blocks are defined in
105
 
<TT>'gsl_block.h'</TT>
106
 
 
107
 
</P>
108
 
 
109
 
 
110
 
 
111
 
<H3><A NAME="SEC155" HREF="gsl-ref_toc.html#TOC155">Block allocation</A></H3>
112
 
 
113
 
<P>
114
 
The functions for allocating memory to a block follow the style of
115
 
<CODE>malloc</CODE> and <CODE>free</CODE>.  In addition they also perform their own
116
 
error checking.  If there is insufficient memory available to allocate a
117
 
block then the functions call the GSL error handler (with an error
118
 
number of <CODE>GSL_ENOMEM</CODE>) in addition to returning a null
119
 
pointer.  Thus if you use the library error handler to abort your program
120
 
then it isn't necessary to check every <CODE>alloc</CODE>.
121
 
 
122
 
</P>
123
 
<P>
124
 
<DL>
125
 
<DT><U>Function:</U> gsl_block * <B>gsl_block_alloc</B> <I>(size_t <VAR>n</VAR>)</I>
126
 
<DD><A NAME="IDX808"></A>
127
 
This function allocates memory for a block of <VAR>n</VAR> double-precision
128
 
elements, returning a pointer to the block struct.  The block is not
129
 
initialized and so the values of its elements are undefined.  Use the
130
 
function <CODE>gsl_block_calloc</CODE> if you want to ensure that all the
131
 
elements are initialized to zero.
132
 
 
133
 
</P>
134
 
<P>
135
 
A null pointer is returned if insufficient memory is available to create
136
 
the block.
137
 
</DL>
138
 
 
139
 
</P>
140
 
<P>
141
 
<DL>
142
 
<DT><U>Function:</U> gsl_block * <B>gsl_block_calloc</B> <I>(size_t <VAR>n</VAR>)</I>
143
 
<DD><A NAME="IDX809"></A>
144
 
This function allocates memory for a block and initializes all the
145
 
elements of the block to zero.
146
 
</DL>
147
 
 
148
 
</P>
149
 
<P>
150
 
<DL>
151
 
<DT><U>Function:</U> void <B>gsl_block_free</B> <I>(gsl_block * <VAR>b</VAR>)</I>
152
 
<DD><A NAME="IDX810"></A>
153
 
This function frees the memory used by a block <VAR>b</VAR> previously
154
 
allocated with <CODE>gsl_block_alloc</CODE> or <CODE>gsl_block_calloc</CODE>.
155
 
</DL>
156
 
 
157
 
</P>
158
 
 
159
 
 
160
 
<H3><A NAME="SEC156" HREF="gsl-ref_toc.html#TOC156">Reading and writing blocks</A></H3>
161
 
 
162
 
<P>
163
 
The library provides functions for reading and writing blocks to a file
164
 
as binary data or formatted text.
165
 
 
166
 
</P>
167
 
<P>
168
 
<DL>
169
 
<DT><U>Function:</U> int <B>gsl_block_fwrite</B> <I>(FILE * <VAR>stream</VAR>, const gsl_block * <VAR>b</VAR>)</I>
170
 
<DD><A NAME="IDX811"></A>
171
 
This function writes the elements of the block <VAR>b</VAR> to the stream
172
 
<VAR>stream</VAR> in binary format.  The return value is 0 for success and
173
 
<CODE>GSL_EFAILED</CODE> if there was a problem writing to the file.  Since the
174
 
data is written in the native binary format it may not be portable
175
 
between different architectures.
176
 
</DL>
177
 
 
178
 
</P>
179
 
<P>
180
 
<DL>
181
 
<DT><U>Function:</U> int <B>gsl_block_fread</B> <I>(FILE * <VAR>stream</VAR>, gsl_block * <VAR>b</VAR>)</I>
182
 
<DD><A NAME="IDX812"></A>
183
 
This function reads into the block <VAR>b</VAR> from the open stream
184
 
<VAR>stream</VAR> in binary format.  The block <VAR>b</VAR> must be preallocated
185
 
with the correct length since the function uses the size of <VAR>b</VAR> to
186
 
determine how many bytes to read.  The return value is 0 for success and
187
 
<CODE>GSL_EFAILED</CODE> if there was a problem reading from the file.  The
188
 
data is assumed to have been written in the native binary format on the
189
 
same architecture.
190
 
</DL>
191
 
 
192
 
</P>
193
 
<P>
194
 
<DL>
195
 
<DT><U>Function:</U> int <B>gsl_block_fprintf</B> <I>(FILE * <VAR>stream</VAR>, const gsl_block * <VAR>b</VAR>, const char * <VAR>format</VAR>)</I>
196
 
<DD><A NAME="IDX813"></A>
197
 
This function writes the elements of the block <VAR>b</VAR> line-by-line to
198
 
the stream <VAR>stream</VAR> using the format specifier <VAR>format</VAR>, which
199
 
should be one of the <CODE>%g</CODE>, <CODE>%e</CODE> or <CODE>%f</CODE> formats for
200
 
floating point numbers and <CODE>%d</CODE> for integers.  The function returns
201
 
0 for success and <CODE>GSL_EFAILED</CODE> if there was a problem writing to
202
 
the file.
203
 
</DL>
204
 
 
205
 
</P>
206
 
<P>
207
 
<DL>
208
 
<DT><U>Function:</U> int <B>gsl_block_fscanf</B> <I>(FILE * <VAR>stream</VAR>, gsl_block * <VAR>b</VAR>)</I>
209
 
<DD><A NAME="IDX814"></A>
210
 
This function reads formatted data from the stream <VAR>stream</VAR> into the
211
 
block <VAR>b</VAR>.  The block <VAR>b</VAR> must be preallocated with the correct
212
 
length since the function uses the size of <VAR>b</VAR> to determine how many
213
 
numbers to read.  The function returns 0 for success and
214
 
<CODE>GSL_EFAILED</CODE> if there was a problem reading from the file.
215
 
</DL>
216
 
 
217
 
</P>
218
 
 
219
 
 
220
 
<H3><A NAME="SEC157" HREF="gsl-ref_toc.html#TOC157">Example programs for blocks</A></H3>
221
 
 
222
 
<P>
223
 
The following program shows how to allocate a block,
224
 
 
225
 
</P>
226
 
 
227
 
<PRE class="example">
228
 
#include &#60;stdio.h&#62;
229
 
#include &#60;gsl/gsl_block.h&#62;
230
 
 
231
 
int
232
 
main (void)
233
 
{
234
 
  gsl_block * b = gsl_block_alloc (100);
235
 
  
236
 
  printf ("length of block = %u\n", b-&#62;size);
237
 
  printf ("block data address = %#x\n", b-&#62;data);
238
 
 
239
 
  gsl_block_free (b);
240
 
  return 0;
241
 
}
242
 
</PRE>
243
 
 
244
 
<P>
245
 
Here is the output from the program,
246
 
 
247
 
</P>
248
 
 
249
 
<PRE class="example">
250
 
length of block = 100
251
 
block data address = 0x804b0d8
252
 
</PRE>
253
 
 
254
 
 
255
 
 
256
 
<H2><A NAME="SEC158" HREF="gsl-ref_toc.html#TOC158">Vectors</A></H2>
257
 
<P>
258
 
<A NAME="IDX815"></A>
259
 
<A NAME="IDX816"></A>
260
 
 
261
 
</P>
262
 
<P>
263
 
Vectors are defined by a <CODE>gsl_vector</CODE> structure which describes a
264
 
slice of a block.  Different vectors can be created which point to the
265
 
same block.  A vector slice is a set of equally-spaced elements of an
266
 
area of memory.
267
 
 
268
 
</P>
269
 
<P>
270
 
The <CODE>gsl_vector</CODE> structure contains five components, the
271
 
<I>size</I>, the <I>stride</I>, a pointer to the memory where the elements
272
 
are stored, <VAR>data</VAR>, a pointer to the block owned by the vector,
273
 
<VAR>block</VAR>, if any, and an ownership flag, <VAR>owner</VAR>.  The structure
274
 
is very simple and looks like this,
275
 
 
276
 
</P>
277
 
 
278
 
<PRE class="example">
279
 
typedef struct
280
 
{
281
 
  size_t size;
282
 
  size_t stride;
283
 
  double * data;
284
 
  gsl_block * block;
285
 
  int owner;
286
 
} gsl_vector;
287
 
</PRE>
288
 
 
289
 
<P>
290
 
The <VAR>size</VAR> is simply the number of vector elements.  The range of
291
 
valid indices runs from 0 to <CODE>size-1</CODE>.  The <VAR>stride</VAR> is the
292
 
step-size from one element to the next in physical memory, measured in
293
 
units of the appropriate datatype.  The pointer <VAR>data</VAR> gives the
294
 
location of the first element of the vector in memory.  The pointer
295
 
<VAR>block</VAR> stores the location of the memory block in which the vector
296
 
elements are located (if any).  If the vector owns this block then the
297
 
<VAR>owner</VAR> field is set to one and the block will be deallocated when the
298
 
vector is freed.  If the vector points to a block owned by another
299
 
object then the <VAR>owner</VAR> field is zero and any underlying block will not be
300
 
deallocated.
301
 
 
302
 
</P>
303
 
<P>
304
 
The functions for allocating and accessing vectors are defined in
305
 
<TT>'gsl_vector.h'</TT>
306
 
 
307
 
</P>
308
 
 
309
 
 
310
 
 
311
 
<H3><A NAME="SEC159" HREF="gsl-ref_toc.html#TOC159">Vector allocation</A></H3>
312
 
 
313
 
<P>
314
 
The functions for allocating memory to a vector follow the style of
315
 
<CODE>malloc</CODE> and <CODE>free</CODE>.  In addition they also perform their own
316
 
error checking.  If there is insufficient memory available to allocate a
317
 
vector then the functions call the GSL error handler (with an error
318
 
number of <CODE>GSL_ENOMEM</CODE>) in addition to returning a null
319
 
pointer.  Thus if you use the library error handler to abort your program
320
 
then it isn't necessary to check every <CODE>alloc</CODE>.
321
 
 
322
 
</P>
323
 
<P>
324
 
<DL>
325
 
<DT><U>Function:</U> gsl_vector * <B>gsl_vector_alloc</B> <I>(size_t <VAR>n</VAR>)</I>
326
 
<DD><A NAME="IDX817"></A>
327
 
This function creates a vector of length <VAR>n</VAR>, returning a pointer to
328
 
a newly initialized vector struct. A new block is allocated for the
329
 
elements of the vector, and stored in the <VAR>block</VAR> component of the
330
 
vector struct.  The block is "owned" by the vector, and will be
331
 
deallocated when the vector is deallocated.
332
 
</DL>
333
 
 
334
 
</P>
335
 
<P>
336
 
<DL>
337
 
<DT><U>Function:</U> gsl_vector * <B>gsl_vector_calloc</B> <I>(size_t <VAR>n</VAR>)</I>
338
 
<DD><A NAME="IDX818"></A>
339
 
This function allocates memory for a vector of length <VAR>n</VAR> and
340
 
initializes all the elements of the vector to zero.
341
 
</DL>
342
 
 
343
 
</P>
344
 
<P>
345
 
<DL>
346
 
<DT><U>Function:</U> void <B>gsl_vector_free</B> <I>(gsl_vector * <VAR>v</VAR>)</I>
347
 
<DD><A NAME="IDX819"></A>
348
 
This function frees a previously allocated vector <VAR>v</VAR>.  If the
349
 
vector was created using <CODE>gsl_vector_alloc</CODE> then the block
350
 
underlying the vector will also be deallocated.  If the vector has been
351
 
created from another object then the memory is still owned by that
352
 
object and will not be deallocated.
353
 
</DL>
354
 
 
355
 
</P>
356
 
 
357
 
 
358
 
<H3><A NAME="SEC160" HREF="gsl-ref_toc.html#TOC160">Accessing vector elements</A></H3>
359
 
<P>
360
 
<A NAME="IDX820"></A>
361
 
<A NAME="IDX821"></A>
362
 
<A NAME="IDX822"></A>
363
 
<A NAME="IDX823"></A>
364
 
<A NAME="IDX824"></A>
365
 
 
366
 
</P>
367
 
<P>
368
 
Unlike FORTRAN compilers, C compilers do not usually provide
369
 
support for range checking of vectors and matrices.  Range checking is
370
 
available in the GNU C Compiler extension <CODE>checkergcc</CODE> but it is
371
 
not available on every platform.  The functions <CODE>gsl_vector_get</CODE>
372
 
and <CODE>gsl_vector_set</CODE> can perform portable range checking for you
373
 
and report an error if you attempt to access elements outside the
374
 
allowed range.
375
 
 
376
 
</P>
377
 
<P>
378
 
The functions for accessing the elements of a vector or matrix are
379
 
defined in <TT>'gsl_vector.h'</TT> and declared <CODE>extern inline</CODE> to
380
 
eliminate function-call overhead.  You must compile your program with
381
 
the macro <CODE>HAVE_INLINE</CODE> defined to use these functions.  If
382
 
necessary you can turn off range checking completely without modifying
383
 
any source files by recompiling your program with the preprocessor
384
 
definition <CODE>GSL_RANGE_CHECK_OFF</CODE>.  Provided your compiler supports
385
 
inline functions the effect of turning off range checking is to replace
386
 
calls to <CODE>gsl_vector_get(v,i)</CODE> by <CODE>v-&#62;data[i*v-&#62;stride]</CODE> and
387
 
calls to <CODE>gsl_vector_set(v,i,x)</CODE> by <CODE>v-&#62;data[i*v-&#62;stride]=x</CODE>.
388
 
Thus there should be no performance penalty for using the range checking
389
 
functions when range checking is turned off.
390
 
 
391
 
</P>
392
 
<P>
393
 
<DL>
394
 
<DT><U>Function:</U> double <B>gsl_vector_get</B> <I>(const gsl_vector * <VAR>v</VAR>, size_t <VAR>i</VAR>)</I>
395
 
<DD><A NAME="IDX825"></A>
396
 
This function returns the <VAR>i</VAR>-th element of a vector <VAR>v</VAR>.  If
397
 
<VAR>i</VAR> lies outside the allowed range of 0 to <VAR>n-1</VAR> then the error
398
 
handler is invoked and 0 is returned.
399
 
</DL>
400
 
 
401
 
</P>
402
 
<P>
403
 
<DL>
404
 
<DT><U>Function:</U> void <B>gsl_vector_set</B> <I>(gsl_vector * <VAR>v</VAR>, size_t <VAR>i</VAR>, double <VAR>x</VAR>)</I>
405
 
<DD><A NAME="IDX826"></A>
406
 
This function sets the value of the <VAR>i</VAR>-th element of a vector
407
 
<VAR>v</VAR> to <VAR>x</VAR>.  If <VAR>i</VAR> lies outside the allowed range of 0 to
408
 
<VAR>n-1</VAR> then the error handler is invoked.
409
 
</DL>
410
 
 
411
 
</P>
412
 
<P>
413
 
<DL>
414
 
<DT><U>Function:</U> double * <B>gsl_vector_ptr</B> <I>(gsl_vector * <VAR>v</VAR>, size_t <VAR>i</VAR>)</I>
415
 
<DD><A NAME="IDX827"></A>
416
 
<DT><U>Function:</U> const double * <B>gsl_vector_const_ptr</B> <I>(const gsl_vector * <VAR>v</VAR>, size_t <VAR>i</VAR>)</I>
417
 
<DD><A NAME="IDX828"></A>
418
 
These functions return a pointer to the <VAR>i</VAR>-th element of a vector
419
 
<VAR>v</VAR>.  If <VAR>i</VAR> lies outside the allowed range of 0 to <VAR>n-1</VAR>
420
 
then the error handler is invoked and a null pointer is returned.
421
 
</DL>
422
 
 
423
 
</P>
424
 
 
425
 
 
426
 
<H3><A NAME="SEC161" HREF="gsl-ref_toc.html#TOC161">Initializing vector elements</A></H3>
427
 
<P>
428
 
<A NAME="IDX829"></A>
429
 
<A NAME="IDX830"></A>
430
 
 
431
 
</P>
432
 
<P>
433
 
<DL>
434
 
<DT><U>Function:</U> void <B>gsl_vector_set_all</B> <I>(gsl_vector * <VAR>v</VAR>, double <VAR>x</VAR>)</I>
435
 
<DD><A NAME="IDX831"></A>
436
 
This function sets all the elements of the vector <VAR>v</VAR> to the value
437
 
<VAR>x</VAR>.
438
 
</DL>
439
 
 
440
 
</P>
441
 
<P>
442
 
<DL>
443
 
<DT><U>Function:</U> void <B>gsl_vector_set_zero</B> <I>(gsl_vector * <VAR>v</VAR>)</I>
444
 
<DD><A NAME="IDX832"></A>
445
 
This function sets all the elements of the vector <VAR>v</VAR> to zero.
446
 
</DL>
447
 
 
448
 
</P>
449
 
<P>
450
 
<DL>
451
 
<DT><U>Function:</U> int <B>gsl_vector_set_basis</B> <I>(gsl_vector * <VAR>v</VAR>, size_t <VAR>i</VAR>)</I>
452
 
<DD><A NAME="IDX833"></A>
453
 
This function makes a basis vector by setting all the elements of the
454
 
vector <VAR>v</VAR> to zero except for the <VAR>i</VAR>-th element which is set to
455
 
one.
456
 
</DL>
457
 
 
458
 
</P>
459
 
 
460
 
 
461
 
<H3><A NAME="SEC162" HREF="gsl-ref_toc.html#TOC162">Reading and writing vectors</A></H3>
462
 
 
463
 
<P>
464
 
The library provides functions for reading and writing vectors to a file
465
 
as binary data or formatted text.
466
 
 
467
 
</P>
468
 
<P>
469
 
<DL>
470
 
<DT><U>Function:</U> int <B>gsl_vector_fwrite</B> <I>(FILE * <VAR>stream</VAR>, const gsl_vector * <VAR>v</VAR>)</I>
471
 
<DD><A NAME="IDX834"></A>
472
 
This function writes the elements of the vector <VAR>v</VAR> to the stream
473
 
<VAR>stream</VAR> in binary format.  The return value is 0 for success and
474
 
<CODE>GSL_EFAILED</CODE> if there was a problem writing to the file.  Since the
475
 
data is written in the native binary format it may not be portable
476
 
between different architectures.
477
 
</DL>
478
 
 
479
 
</P>
480
 
<P>
481
 
<DL>
482
 
<DT><U>Function:</U> int <B>gsl_vector_fread</B> <I>(FILE * <VAR>stream</VAR>, gsl_vector * <VAR>v</VAR>)</I>
483
 
<DD><A NAME="IDX835"></A>
484
 
This function reads into the vector <VAR>v</VAR> from the open stream
485
 
<VAR>stream</VAR> in binary format.  The vector <VAR>v</VAR> must be preallocated
486
 
with the correct length since the function uses the size of <VAR>v</VAR> to
487
 
determine how many bytes to read.  The return value is 0 for success and
488
 
<CODE>GSL_EFAILED</CODE> if there was a problem reading from the file.  The
489
 
data is assumed to have been written in the native binary format on the
490
 
same architecture.
491
 
</DL>
492
 
 
493
 
</P>
494
 
<P>
495
 
<DL>
496
 
<DT><U>Function:</U> int <B>gsl_vector_fprintf</B> <I>(FILE * <VAR>stream</VAR>, const gsl_vector * <VAR>v</VAR>, const char * <VAR>format</VAR>)</I>
497
 
<DD><A NAME="IDX836"></A>
498
 
This function writes the elements of the vector <VAR>v</VAR> line-by-line to
499
 
the stream <VAR>stream</VAR> using the format specifier <VAR>format</VAR>, which
500
 
should be one of the <CODE>%g</CODE>, <CODE>%e</CODE> or <CODE>%f</CODE> formats for
501
 
floating point numbers and <CODE>%d</CODE> for integers.  The function returns
502
 
0 for success and <CODE>GSL_EFAILED</CODE> if there was a problem writing to
503
 
the file.
504
 
</DL>
505
 
 
506
 
</P>
507
 
<P>
508
 
<DL>
509
 
<DT><U>Function:</U> int <B>gsl_vector_fscanf</B> <I>(FILE * <VAR>stream</VAR>, gsl_vector * <VAR>v</VAR>)</I>
510
 
<DD><A NAME="IDX837"></A>
511
 
This function reads formatted data from the stream <VAR>stream</VAR> into the
512
 
vector <VAR>v</VAR>.  The vector <VAR>v</VAR> must be preallocated with the correct
513
 
length since the function uses the size of <VAR>v</VAR> to determine how many
514
 
numbers to read.  The function returns 0 for success and
515
 
<CODE>GSL_EFAILED</CODE> if there was a problem reading from the file.
516
 
</DL>
517
 
 
518
 
</P>
519
 
 
520
 
 
521
 
<H3><A NAME="SEC163" HREF="gsl-ref_toc.html#TOC163">Vector views</A></H3>
522
 
 
523
 
<P>
524
 
In addition to creating vectors from slices of blocks it is also
525
 
possible to slice vectors and create vector views.  For example, a
526
 
subvector of another vector can be described with a view, or two views
527
 
can be made which provide access to the even and odd elements of a
528
 
vector.
529
 
 
530
 
</P>
531
 
<P>
532
 
A vector view is a temporary object, stored on the stack, which can be
533
 
used to operate on a subset of vector elements.  Vector views can be
534
 
defined for both constant and non-constant vectors, using separate types
535
 
that preserve constness.  A vector view has the type
536
 
<CODE>gsl_vector_view</CODE> and a constant vector view has the type
537
 
<CODE>gsl_vector_const_view</CODE>.  In both cases the elements of the view
538
 
can be accessed as a <CODE>gsl_vector</CODE> using the <CODE>vector</CODE> component
539
 
of the view object.  A pointer to a vector of type <CODE>gsl_vector *</CODE>
540
 
or <CODE>const gsl_vector *</CODE> can be obtained by taking the address of
541
 
this component with the <CODE>&#38;</CODE> operator.  
542
 
 
543
 
</P>
544
 
<P>
545
 
When using this pointer, it is important to ensure that the view itself
546
 
remains in scope--the simplest way to do so is by always writing the
547
 
pointer as <CODE>&#38;</CODE><VAR>view</VAR><CODE>.vector</CODE>, and not storing this value
548
 
in another pointer variable.  
549
 
 
550
 
</P>
551
 
<P>
552
 
<DL>
553
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_vector_subvector</B> <I>(gsl_vector *<VAR>v</VAR>, size_t <VAR>offset</VAR>, size_t <VAR>n</VAR>)</I>
554
 
<DD><A NAME="IDX838"></A>
555
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_vector_const_subvector</B> <I>(const gsl_vector * <VAR>v</VAR>, size_t <VAR>offset</VAR>, size_t <VAR>n</VAR>)</I>
556
 
<DD><A NAME="IDX839"></A>
557
 
These functions return a vector view of a subvector of another vector
558
 
<VAR>v</VAR>.  The start of the new vector is offset by <VAR>offset</VAR> elements
559
 
from the start of the original vector.  The new vector has <VAR>n</VAR>
560
 
elements.  Mathematically, the <VAR>i</VAR>-th element of the new vector
561
 
<VAR>v'</VAR> is given by,
562
 
 
563
 
</P>
564
 
 
565
 
<PRE class="example">
566
 
v'(i) = v-&#62;data[(offset + i)*v-&#62;stride]
567
 
</PRE>
568
 
 
569
 
<P>
570
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n-1</CODE>.
571
 
 
572
 
</P>
573
 
<P>
574
 
The <CODE>data</CODE> pointer of the returned vector struct is set to null if
575
 
the combined parameters (<VAR>offset</VAR>,<VAR>n</VAR>) overrun the end of the
576
 
original vector.
577
 
 
578
 
</P>
579
 
<P>
580
 
The new vector is only a view of the block underlying the original
581
 
vector, <VAR>v</VAR>.  The block containing the elements of <VAR>v</VAR> is not
582
 
owned by the new vector.  When the view goes out of scope the original
583
 
vector <VAR>v</VAR> and its block will continue to exist.  The original
584
 
memory can only be deallocated by freeing the original vector.  Of
585
 
course, the original vector should not be deallocated while the view is
586
 
still in use.
587
 
 
588
 
</P>
589
 
<P>
590
 
The function <CODE>gsl_vector_const_subvector</CODE> is equivalent to
591
 
<CODE>gsl_vector_subvector</CODE> but can be used for vectors which are
592
 
declared <CODE>const</CODE>.
593
 
</DL>
594
 
 
595
 
</P>
596
 
 
597
 
<P>
598
 
<DL>
599
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_vector_subvector_with_stride</B> <I>(gsl_vector *<VAR>v</VAR>, size_t <VAR>offset</VAR>, size_t <VAR>stride</VAR>, size_t <VAR>n</VAR>)</I>
600
 
<DD><A NAME="IDX840"></A>
601
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_vector_const_subvector_with_stride</B> <I>(const gsl_vector * <VAR>v</VAR>, size_t <VAR>offset</VAR>, size_t <VAR>stride</VAR>, size_t <VAR>n</VAR>)</I>
602
 
<DD><A NAME="IDX841"></A>
603
 
These functions return a vector view of a subvector of another vector
604
 
<VAR>v</VAR> with an additional stride argument. The subvector is formed in
605
 
the same way as for <CODE>gsl_vector_subvector</CODE> but the new vector has
606
 
<VAR>n</VAR> elements with a step-size of <VAR>stride</VAR> from one element to
607
 
the next in the original vector.  Mathematically, the <VAR>i</VAR>-th element
608
 
of the new vector <VAR>v'</VAR> is given by,
609
 
 
610
 
</P>
611
 
 
612
 
<PRE class="example">
613
 
v'(i) = v-&#62;data[(offset + i*stride)*v-&#62;stride]
614
 
</PRE>
615
 
 
616
 
<P>
617
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n-1</CODE>.
618
 
 
619
 
</P>
620
 
<P>
621
 
Note that subvector views give direct access to the underlying elements
622
 
of the original vector. For example, the following code will zero the
623
 
even elements of the vector <CODE>v</CODE> of length <CODE>n</CODE>, while leaving the
624
 
odd elements untouched,
625
 
 
626
 
</P>
627
 
 
628
 
<PRE class="example">
629
 
gsl_vector_view v_even 
630
 
  = gsl_vector_subvector_with_stride (v, 0, 2, n/2);
631
 
gsl_vector_set_zero (&#38;v_even.vector);
632
 
</PRE>
633
 
 
634
 
<P>
635
 
A vector view can be passed to any subroutine which takes a vector
636
 
argument just as a directly allocated vector would be, using
637
 
<CODE>&#38;</CODE><VAR>view</VAR><CODE>.vector</CODE>.  For example, the following code
638
 
computes the norm of odd elements of <CODE>v</CODE> using the BLAS
639
 
routine DNRM2,
640
 
 
641
 
</P>
642
 
 
643
 
<PRE class="example">
644
 
gsl_vector_view v_odd 
645
 
  = gsl_vector_subvector_with_stride (v, 1, 2, n/2);
646
 
double r = gsl_blas_dnrm2 (&#38;v_odd.vector);
647
 
</PRE>
648
 
 
649
 
<P>
650
 
The function <CODE>gsl_vector_const_subvector_with_stride</CODE> is equivalent
651
 
to <CODE>gsl_vector_subvector_with_stride</CODE> but can be used for vectors
652
 
which are declared <CODE>const</CODE>.
653
 
</DL>
654
 
 
655
 
</P>
656
 
<P>
657
 
<DL>
658
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_vector_complex_real</B> <I>(gsl_vector_complex *<VAR>v</VAR>)</I>
659
 
<DD><A NAME="IDX842"></A>
660
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_vector_complex_const_real</B> <I>(const gsl_vector_complex *<VAR>v</VAR>)</I>
661
 
<DD><A NAME="IDX843"></A>
662
 
These functions return a vector view of the real parts of the complex
663
 
vector <VAR>v</VAR>.
664
 
 
665
 
</P>
666
 
<P>
667
 
The function <CODE>gsl_vector_complex_const_real</CODE> is equivalent to
668
 
<CODE>gsl_vector_complex_real</CODE> but can be used for vectors which are
669
 
declared <CODE>const</CODE>.
670
 
</DL>
671
 
 
672
 
</P>
673
 
<P>
674
 
<DL>
675
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_vector_complex_imag</B> <I>(gsl_vector_complex *<VAR>v</VAR>)</I>
676
 
<DD><A NAME="IDX844"></A>
677
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_vector_complex_const_imag</B> <I>(const gsl_vector_complex *<VAR>v</VAR>)</I>
678
 
<DD><A NAME="IDX845"></A>
679
 
These functions return a vector view of the imaginary parts of the
680
 
complex vector <VAR>v</VAR>.
681
 
 
682
 
</P>
683
 
<P>
684
 
The function <CODE>gsl_vector_complex_const_imag</CODE> is equivalent to
685
 
<CODE>gsl_vector_complex_imag</CODE> but can be used for vectors which are
686
 
declared <CODE>const</CODE>.
687
 
</DL>
688
 
 
689
 
</P>
690
 
<P>
691
 
<DL>
692
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_vector_view_array</B> <I>(double *<VAR>base</VAR>, size_t <VAR>n</VAR>)</I>
693
 
<DD><A NAME="IDX846"></A>
694
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_vector_const_view_array</B> <I>(const double *<VAR>base</VAR>, size_t <VAR>n</VAR>)</I>
695
 
<DD><A NAME="IDX847"></A>
696
 
These functions return a vector view of an array.  The start of the new
697
 
vector is given by <VAR>base</VAR> and has <VAR>n</VAR> elements.  Mathematically,
698
 
the <VAR>i</VAR>-th element of the new vector <VAR>v'</VAR> is given by,
699
 
 
700
 
</P>
701
 
 
702
 
<PRE class="example">
703
 
v'(i) = base[i]
704
 
</PRE>
705
 
 
706
 
<P>
707
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n-1</CODE>.
708
 
 
709
 
</P>
710
 
<P>
711
 
The array containing the elements of <VAR>v</VAR> is not owned by the new
712
 
vector view.  When the view goes out of scope the original array will
713
 
continue to exist.  The original memory can only be deallocated by
714
 
freeing the original pointer <VAR>base</VAR>.  Of course, the original array
715
 
should not be deallocated while the view is still in use.
716
 
 
717
 
</P>
718
 
<P>
719
 
The function <CODE>gsl_vector_const_view_array</CODE> is equivalent to
720
 
<CODE>gsl_vector_view_array</CODE> but can be used for arrays which are
721
 
declared <CODE>const</CODE>.
722
 
</DL>
723
 
 
724
 
</P>
725
 
<P>
726
 
<DL>
727
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_vector_view_array_with_stride</B> <I>(double * <VAR>base</VAR>, size_t <VAR>stride</VAR>, size_t <VAR>n</VAR>)</I>
728
 
<DD><A NAME="IDX848"></A>
729
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_vector_const_view_array_with_stride</B> <I>(const double * <VAR>base</VAR>, size_t <VAR>stride</VAR>, size_t <VAR>n</VAR>)</I>
730
 
<DD><A NAME="IDX849"></A>
731
 
These functions return a vector view of an array <VAR>base</VAR> with an
732
 
additional stride argument. The subvector is formed in the same way as
733
 
for <CODE>gsl_vector_view_array</CODE> but the new vector has <VAR>n</VAR> elements
734
 
with a step-size of <VAR>stride</VAR> from one element to the next in the
735
 
original array.  Mathematically, the <VAR>i</VAR>-th element of the new
736
 
vector <VAR>v'</VAR> is given by,
737
 
 
738
 
</P>
739
 
 
740
 
<PRE class="example">
741
 
v'(i) = base[i*stride]
742
 
</PRE>
743
 
 
744
 
<P>
745
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n-1</CODE>.
746
 
 
747
 
</P>
748
 
<P>
749
 
Note that the view gives direct access to the underlying elements of the
750
 
original array.  A vector view can be passed to any subroutine which
751
 
takes a vector argument just as a directly allocated vector would be,
752
 
using <CODE>&#38;</CODE><VAR>view</VAR><CODE>.vector</CODE>.
753
 
 
754
 
</P>
755
 
<P>
756
 
The function <CODE>gsl_vector_const_view_array_with_stride</CODE> is
757
 
equivalent to <CODE>gsl_vector_view_array_with_stride</CODE> but can be used
758
 
for arrays which are declared <CODE>const</CODE>.
759
 
</DL>
760
 
 
761
 
</P>
762
 
 
763
 
 
764
 
 
765
 
<H3><A NAME="SEC164" HREF="gsl-ref_toc.html#TOC164">Copying vectors</A></H3>
766
 
 
767
 
<P>
768
 
Common operations on vectors such as addition and multiplication are
769
 
available in the BLAS part of the library (see section <A HREF="gsl-ref_12.html#SEC212">BLAS Support</A>).  However, it is useful to have a small number of utility
770
 
functions which do not require the full BLAS code.  The following
771
 
functions fall into this category.
772
 
 
773
 
</P>
774
 
<P>
775
 
<DL>
776
 
<DT><U>Function:</U> int <B>gsl_vector_memcpy</B> <I>(gsl_vector * <VAR>dest</VAR>, const gsl_vector * <VAR>src</VAR>)</I>
777
 
<DD><A NAME="IDX850"></A>
778
 
This function copies the elements of the vector <VAR>src</VAR> into the
779
 
vector <VAR>dest</VAR>.  The two vectors must have the same length.
780
 
</DL>
781
 
 
782
 
</P>
783
 
<P>
784
 
<DL>
785
 
<DT><U>Function:</U> int <B>gsl_vector_swap</B> <I>(gsl_vector * <VAR>v</VAR>, gsl_vector * <VAR>w</VAR>)</I>
786
 
<DD><A NAME="IDX851"></A>
787
 
This function exchanges the elements of the vectors <VAR>v</VAR> and <VAR>w</VAR>
788
 
by copying.  The two vectors must have the same length.
789
 
</DL>
790
 
 
791
 
</P>
792
 
 
793
 
 
794
 
 
795
 
<H3><A NAME="SEC165" HREF="gsl-ref_toc.html#TOC165">Exchanging elements</A></H3>
796
 
 
797
 
<P>
798
 
The following function can be used to exchange, or permute, the elements
799
 
of a vector.
800
 
 
801
 
</P>
802
 
<P>
803
 
<DL>
804
 
<DT><U>Function:</U> int <B>gsl_vector_swap_elements</B> <I>(gsl_vector * <VAR>v</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>)</I>
805
 
<DD><A NAME="IDX852"></A>
806
 
This function exchanges the <VAR>i</VAR>-th and <VAR>j</VAR>-th elements of the
807
 
vector <VAR>v</VAR> in-place.
808
 
</DL>
809
 
 
810
 
</P>
811
 
<P>
812
 
<DL>
813
 
<DT><U>Function:</U> int <B>gsl_vector_reverse</B> <I>(gsl_vector * <VAR>v</VAR>)</I>
814
 
<DD><A NAME="IDX853"></A>
815
 
This function reverses the order of the elements of the vector <VAR>v</VAR>.
816
 
</DL>
817
 
 
818
 
</P>
819
 
 
820
 
 
821
 
 
822
 
<H3><A NAME="SEC166" HREF="gsl-ref_toc.html#TOC166">Vector operations</A></H3>
823
 
 
824
 
<P>
825
 
The following operations are only defined for real vectors.
826
 
 
827
 
</P>
828
 
<P>
829
 
<DL>
830
 
<DT><U>Function:</U> int <B>gsl_vector_add</B> <I>(gsl_vector * <VAR>a</VAR>, const gsl_vector * <VAR>b</VAR>)</I>
831
 
<DD><A NAME="IDX854"></A>
832
 
This function adds the elements of vector <VAR>b</VAR> to the elements of
833
 
vector <VAR>a</VAR>, a'_i = a_i + b_i. The two vectors must have the
834
 
same length.
835
 
</DL>
836
 
 
837
 
</P>
838
 
<P>
839
 
<DL>
840
 
<DT><U>Function:</U> int <B>gsl_vector_sub</B> <I>(gsl_vector * <VAR>a</VAR>, const gsl_vector * <VAR>b</VAR>)</I>
841
 
<DD><A NAME="IDX855"></A>
842
 
This function subtracts the elements of vector <VAR>b</VAR> from the elements of
843
 
vector <VAR>a</VAR>, a'_i = a_i - b_i. The two vectors must have the
844
 
same length.
845
 
</DL>
846
 
 
847
 
</P>
848
 
<P>
849
 
<DL>
850
 
<DT><U>Function:</U> int <B>gsl_vector_mul</B> <I>(gsl_vector * <VAR>a</VAR>, const gsl_vector * <VAR>b</VAR>)</I>
851
 
<DD><A NAME="IDX856"></A>
852
 
This function multiplies the elements of vector <VAR>a</VAR> by the elements of
853
 
vector <VAR>b</VAR>, a'_i = a_i * b_i. The two vectors must have the
854
 
same length.
855
 
</DL>
856
 
 
857
 
</P>
858
 
<P>
859
 
<DL>
860
 
<DT><U>Function:</U> int <B>gsl_vector_div</B> <I>(gsl_vector * <VAR>a</VAR>, const gsl_vector * <VAR>b</VAR>)</I>
861
 
<DD><A NAME="IDX857"></A>
862
 
This function divides the elements of vector <VAR>a</VAR> by the elements of
863
 
vector <VAR>b</VAR>, a'_i = a_i / b_i. The two vectors must have the
864
 
same length.
865
 
</DL>
866
 
 
867
 
</P>
868
 
<P>
869
 
<DL>
870
 
<DT><U>Function:</U> int <B>gsl_vector_scale</B> <I>(gsl_vector * <VAR>a</VAR>, const double <VAR>x</VAR>)</I>
871
 
<DD><A NAME="IDX858"></A>
872
 
This function multiplies the elements of vector <VAR>a</VAR> by the constant
873
 
factor <VAR>x</VAR>, a'_i = x a_i.
874
 
</DL>
875
 
 
876
 
</P>
877
 
<P>
878
 
<DL>
879
 
<DT><U>Function:</U> int <B>gsl_vector_add_constant</B> <I>(gsl_vector * <VAR>a</VAR>, const double <VAR>x</VAR>)</I>
880
 
<DD><A NAME="IDX859"></A>
881
 
This function adds the constant value <VAR>x</VAR> to the elements of the
882
 
vector <VAR>a</VAR>, a'_i = a_i + x.
883
 
</DL>
884
 
 
885
 
</P>
886
 
 
887
 
 
888
 
<H3><A NAME="SEC167" HREF="gsl-ref_toc.html#TOC167">Finding maximum and minimum elements of vectors</A></H3>
889
 
 
890
 
<P>
891
 
<DL>
892
 
<DT><U>Function:</U> double <B>gsl_vector_max</B> <I>(const gsl_vector * <VAR>v</VAR>)</I>
893
 
<DD><A NAME="IDX860"></A>
894
 
This function returns the maximum value in the vector <VAR>v</VAR>.
895
 
</DL>
896
 
 
897
 
</P>
898
 
<P>
899
 
<DL>
900
 
<DT><U>Function:</U> double <B>gsl_vector_min</B> <I>(const gsl_vector * <VAR>v</VAR>)</I>
901
 
<DD><A NAME="IDX861"></A>
902
 
This function returns the minimum value in the vector <VAR>v</VAR>.
903
 
</DL>
904
 
 
905
 
</P>
906
 
<P>
907
 
<DL>
908
 
<DT><U>Function:</U> void <B>gsl_vector_minmax</B> <I>(const gsl_vector * <VAR>v</VAR>, double * <VAR>min_out</VAR>, double * <VAR>max_out</VAR>)</I>
909
 
<DD><A NAME="IDX862"></A>
910
 
This function returns the minimum and maximum values in the vector
911
 
<VAR>v</VAR>, storing them in <VAR>min_out</VAR> and <VAR>max_out</VAR>.
912
 
</DL>
913
 
 
914
 
</P>
915
 
<P>
916
 
<DL>
917
 
<DT><U>Function:</U> size_t <B>gsl_vector_max_index</B> <I>(const gsl_vector * <VAR>v</VAR>)</I>
918
 
<DD><A NAME="IDX863"></A>
919
 
This function returns the index of the maximum value in the vector <VAR>v</VAR>.
920
 
When there are several equal maximum elements then the lowest index is
921
 
returned.
922
 
</DL>
923
 
 
924
 
</P>
925
 
<P>
926
 
<DL>
927
 
<DT><U>Function:</U> size_t <B>gsl_vector_min_index</B> <I>(const gsl_vector * <VAR>v</VAR>)</I>
928
 
<DD><A NAME="IDX864"></A>
929
 
This function returns the index of the minimum value in the vector <VAR>v</VAR>.
930
 
When there are several equal minimum elements then the lowest index is
931
 
returned.
932
 
</DL>
933
 
 
934
 
</P>
935
 
<P>
936
 
<DL>
937
 
<DT><U>Function:</U> void <B>gsl_vector_minmax_index</B> <I>(const gsl_vector * <VAR>v</VAR>, size_t * <VAR>imin</VAR>, size_t * <VAR>imax</VAR>)</I>
938
 
<DD><A NAME="IDX865"></A>
939
 
This function returns the indices of the minimum and maximum values in
940
 
the vector <VAR>v</VAR>, storing them in <VAR>imin</VAR> and <VAR>imax</VAR>. When
941
 
there are several equal minimum or maximum elements then the lowest
942
 
indices are returned.
943
 
</DL>
944
 
 
945
 
</P>
946
 
 
947
 
 
948
 
<H3><A NAME="SEC168" HREF="gsl-ref_toc.html#TOC168">Vector properties</A></H3>
949
 
 
950
 
<P>
951
 
<DL>
952
 
<DT><U>Function:</U> int <B>gsl_vector_isnull</B> <I>(const gsl_vector * <VAR>v</VAR>)</I>
953
 
<DD><A NAME="IDX866"></A>
954
 
This function returns 1 if all the elements of the vector <VAR>v</VAR> are
955
 
zero, and 0 otherwise.
956
 
</DL>
957
 
 
958
 
</P>
959
 
 
960
 
 
961
 
<H3><A NAME="SEC169" HREF="gsl-ref_toc.html#TOC169">Example programs for vectors</A></H3>
962
 
 
963
 
<P>
964
 
This program shows how to allocate, initialize and read from a vector
965
 
using the functions <CODE>gsl_vector_alloc</CODE>, <CODE>gsl_vector_set</CODE> and
966
 
<CODE>gsl_vector_get</CODE>.
967
 
 
968
 
</P>
969
 
 
970
 
<PRE class="example">
971
 
#include &#60;stdio.h&#62;
972
 
#include &#60;gsl/gsl_vector.h&#62;
973
 
 
974
 
int
975
 
main (void)
976
 
{
977
 
  int i;
978
 
  gsl_vector * v = gsl_vector_alloc (3);
979
 
  
980
 
  for (i = 0; i &#60; 3; i++)
981
 
    {
982
 
      gsl_vector_set (v, i, 1.23 + i);
983
 
    }
984
 
  
985
 
  for (i = 0; i &#60; 100; i++)
986
 
    {
987
 
      printf ("v_%d = %g\n", i, gsl_vector_get (v, i));
988
 
    }
989
 
 
990
 
  return 0;
991
 
}
992
 
</PRE>
993
 
 
994
 
<P>
995
 
Here is the output from the program.  The final loop attempts to read
996
 
outside the range of the vector <CODE>v</CODE>, and the error is trapped by
997
 
the range-checking code in <CODE>gsl_vector_get</CODE>.
998
 
 
999
 
</P>
1000
 
 
1001
 
<PRE class="example">
1002
 
v_0 = 1.23
1003
 
v_1 = 2.23
1004
 
v_2 = 3.23
1005
 
gsl: vector_source.c:12: ERROR: index out of range
1006
 
IOT trap/Abort (core dumped)
1007
 
</PRE>
1008
 
 
1009
 
<P>
1010
 
The next program shows how to write a vector to a file.
1011
 
 
1012
 
</P>
1013
 
 
1014
 
<PRE class="example">
1015
 
#include &#60;stdio.h&#62;
1016
 
#include &#60;gsl/gsl_vector.h&#62;
1017
 
 
1018
 
int
1019
 
main (void)
1020
 
{
1021
 
  int i; 
1022
 
  gsl_vector * v = gsl_vector_alloc (100);
1023
 
  
1024
 
  for (i = 0; i &#60; 100; i++)
1025
 
    {
1026
 
      gsl_vector_set (v, i, 1.23 + i);
1027
 
    }
1028
 
 
1029
 
  {  
1030
 
     FILE * f = fopen ("test.dat", "w");
1031
 
     gsl_vector_fprintf (f, v, "%.5g");
1032
 
     fclose (f);
1033
 
  }
1034
 
  return 0;
1035
 
}
1036
 
</PRE>
1037
 
 
1038
 
<P>
1039
 
After running this program the file <TT>'test.dat'</TT> should contain the
1040
 
elements of <CODE>v</CODE>, written using the format specifier
1041
 
<CODE>%.5g</CODE>.  The vector could then be read back in using the function
1042
 
<CODE>gsl_vector_fscanf (f, v)</CODE> as follows:
1043
 
 
1044
 
</P>
1045
 
 
1046
 
<PRE class="example">
1047
 
#include &#60;stdio.h&#62;
1048
 
#include &#60;gsl/gsl_vector.h&#62;
1049
 
 
1050
 
int
1051
 
main (void)
1052
 
{
1053
 
  int i; 
1054
 
  gsl_vector * v = gsl_vector_alloc (10);
1055
 
 
1056
 
  {  
1057
 
     FILE * f = fopen ("test.dat", "r");
1058
 
     gsl_vector_fscanf (f, v);
1059
 
     fclose (f);
1060
 
  }
1061
 
 
1062
 
  for (i = 0; i &#60; 10; i++)
1063
 
    {
1064
 
      printf ("%g\n", gsl_vector_get(v, i));
1065
 
    }
1066
 
 
1067
 
  return 0;
1068
 
}
1069
 
</PRE>
1070
 
 
1071
 
 
1072
 
 
1073
 
<H2><A NAME="SEC170" HREF="gsl-ref_toc.html#TOC170">Matrices</A></H2>
1074
 
<P>
1075
 
<A NAME="IDX867"></A>
1076
 
<A NAME="IDX868"></A>
1077
 
<A NAME="IDX869"></A>
1078
 
<A NAME="IDX870"></A>
1079
 
 
1080
 
</P>
1081
 
<P>
1082
 
Matrices are defined by a <CODE>gsl_matrix</CODE> structure which describes a
1083
 
generalized slice of a block.  Like a vector it represents a set of
1084
 
elements in an area of memory, but uses two indices instead of one.
1085
 
 
1086
 
</P>
1087
 
<P>
1088
 
The <CODE>gsl_matrix</CODE> structure contains six components, the two
1089
 
dimensions of the matrix, a physical dimension, a pointer to the memory
1090
 
where the elements of the matrix are stored, <VAR>data</VAR>, a pointer to
1091
 
the block owned by the matrix <VAR>block</VAR>, if any, and an ownership
1092
 
flag, <VAR>owner</VAR>.  The physical dimension determines the memory layout
1093
 
and can differ from the matrix dimension to allow the use of
1094
 
submatrices.  The <CODE>gsl_matrix</CODE> structure is very simple and looks
1095
 
like this,
1096
 
 
1097
 
</P>
1098
 
 
1099
 
<PRE class="example">
1100
 
typedef struct
1101
 
{
1102
 
  size_t size1;
1103
 
  size_t size2;
1104
 
  size_t tda;
1105
 
  double * data;
1106
 
  gsl_block * block;
1107
 
  int owner;
1108
 
} gsl_matrix;
1109
 
</PRE>
1110
 
 
1111
 
<P>
1112
 
Matrices are stored in row-major order, meaning that each row of
1113
 
elements forms a contiguous block in memory.  This is the standard
1114
 
"C-language ordering" of two-dimensional arrays. Note that FORTRAN
1115
 
stores arrays in column-major order. The number of rows is <VAR>size1</VAR>.
1116
 
The range of valid row indices runs from 0 to <CODE>size1-1</CODE>.  Similarly
1117
 
<VAR>size2</VAR> is the number of columns.  The range of valid column indices
1118
 
runs from 0 to <CODE>size2-1</CODE>.  The physical row dimension <VAR>tda</VAR>, or
1119
 
<I>trailing dimension</I>, specifies the size of a row of the matrix as
1120
 
laid out in memory.
1121
 
 
1122
 
</P>
1123
 
<P>
1124
 
For example, in the following matrix <VAR>size1</VAR> is 3, <VAR>size2</VAR> is 4,
1125
 
and <VAR>tda</VAR> is 8.  The physical memory layout of the matrix begins in
1126
 
the top left hand-corner and proceeds from left to right along each row
1127
 
in turn.
1128
 
 
1129
 
</P>
1130
 
 
1131
 
<PRE class="example">
1132
 
00 01 02 03 XX XX XX XX
1133
 
10 11 12 13 XX XX XX XX
1134
 
20 21 22 23 XX XX XX XX
1135
 
</PRE>
1136
 
 
1137
 
<P>
1138
 
Each unused memory location is represented by "<CODE>XX</CODE>".  The
1139
 
pointer <VAR>data</VAR> gives the location of the first element of the matrix
1140
 
in memory.  The pointer <VAR>block</VAR> stores the location of the memory
1141
 
block in which the elements of the matrix are located (if any).  If the
1142
 
matrix owns this block then the <VAR>owner</VAR> field is set to one and the
1143
 
block will be deallocated when the matrix is freed.  If the matrix is
1144
 
only a slice of a block owned by another object then the <VAR>owner</VAR> field is
1145
 
zero and any underlying block will not be freed.
1146
 
 
1147
 
</P>
1148
 
<P>
1149
 
The functions for allocating and accessing matrices are defined in
1150
 
<TT>'gsl_matrix.h'</TT>
1151
 
 
1152
 
</P>
1153
 
 
1154
 
 
1155
 
 
1156
 
<H3><A NAME="SEC171" HREF="gsl-ref_toc.html#TOC171">Matrix allocation</A></H3>
1157
 
 
1158
 
<P>
1159
 
The functions for allocating memory to a matrix follow the style of
1160
 
<CODE>malloc</CODE> and <CODE>free</CODE>.  They also perform their own error
1161
 
checking.  If there is insufficient memory available to allocate a vector
1162
 
then the functions call the GSL error handler (with an error number of
1163
 
<CODE>GSL_ENOMEM</CODE>) in addition to returning a null pointer.  Thus if you
1164
 
use the library error handler to abort your program then it isn't
1165
 
necessary to check every <CODE>alloc</CODE>.
1166
 
 
1167
 
</P>
1168
 
<P>
1169
 
<DL>
1170
 
<DT><U>Function:</U> gsl_matrix * <B>gsl_matrix_alloc</B> <I>(size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1171
 
<DD><A NAME="IDX871"></A>
1172
 
This function creates a matrix of size <VAR>n1</VAR> rows by <VAR>n2</VAR>
1173
 
columns, returning a pointer to a newly initialized matrix struct. A new
1174
 
block is allocated for the elements of the matrix, and stored in the
1175
 
<VAR>block</VAR> component of the matrix struct.  The block is "owned" by the
1176
 
matrix, and will be deallocated when the matrix is deallocated.
1177
 
</DL>
1178
 
 
1179
 
</P>
1180
 
<P>
1181
 
<DL>
1182
 
<DT><U>Function:</U> gsl_matrix * <B>gsl_matrix_calloc</B> <I>(size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1183
 
<DD><A NAME="IDX872"></A>
1184
 
This function allocates memory for a matrix of size <VAR>n1</VAR> rows by
1185
 
<VAR>n2</VAR> columns and initializes all the elements of the matrix to zero.
1186
 
</DL>
1187
 
 
1188
 
</P>
1189
 
<P>
1190
 
<DL>
1191
 
<DT><U>Function:</U> void <B>gsl_matrix_free</B> <I>(gsl_matrix * <VAR>m</VAR>)</I>
1192
 
<DD><A NAME="IDX873"></A>
1193
 
This function frees a previously allocated matrix <VAR>m</VAR>.  If the
1194
 
matrix was created using <CODE>gsl_matrix_alloc</CODE> then the block
1195
 
underlying the matrix will also be deallocated.  If the matrix has been
1196
 
created from another object then the memory is still owned by that
1197
 
object and will not be deallocated.
1198
 
</DL>
1199
 
 
1200
 
</P>
1201
 
 
1202
 
 
1203
 
<H3><A NAME="SEC172" HREF="gsl-ref_toc.html#TOC172">Accessing matrix elements</A></H3>
1204
 
<P>
1205
 
<A NAME="IDX874"></A>
1206
 
<A NAME="IDX875"></A>
1207
 
 
1208
 
</P>
1209
 
<P>
1210
 
The functions for accessing the elements of a matrix use the same range
1211
 
checking system as vectors.  You can turn off range checking by recompiling
1212
 
your program with the preprocessor definition
1213
 
<CODE>GSL_RANGE_CHECK_OFF</CODE>.
1214
 
 
1215
 
</P>
1216
 
<P>
1217
 
The elements of the matrix are stored in "C-order", where the second
1218
 
index moves continuously through memory.  More precisely, the element
1219
 
accessed by the function <CODE>gsl_matrix_get(m,i,j)</CODE> and
1220
 
<CODE>gsl_matrix_set(m,i,j,x)</CODE> is 
1221
 
 
1222
 
</P>
1223
 
 
1224
 
<PRE class="example">
1225
 
m-&#62;data[i * m-&#62;tda + j]
1226
 
</PRE>
1227
 
 
1228
 
<P>
1229
 
where <VAR>tda</VAR> is the physical row-length of the matrix.
1230
 
 
1231
 
</P>
1232
 
<P>
1233
 
<DL>
1234
 
<DT><U>Function:</U> double <B>gsl_matrix_get</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>)</I>
1235
 
<DD><A NAME="IDX876"></A>
1236
 
This function returns the (i,j)-th element of a matrix
1237
 
<VAR>m</VAR>.  If <VAR>i</VAR> or <VAR>j</VAR> lie outside the allowed range of 0 to
1238
 
<VAR>n1-1</VAR> and 0 to <VAR>n2-1</VAR> then the error handler is invoked and 0
1239
 
is returned.
1240
 
</DL>
1241
 
 
1242
 
</P>
1243
 
<P>
1244
 
<DL>
1245
 
<DT><U>Function:</U> void <B>gsl_matrix_set</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>, double <VAR>x</VAR>)</I>
1246
 
<DD><A NAME="IDX877"></A>
1247
 
This function sets the value of the (i,j)-th element of a
1248
 
matrix <VAR>m</VAR> to <VAR>x</VAR>.  If <VAR>i</VAR> or <VAR>j</VAR> lies outside the
1249
 
allowed range of 0 to <VAR>n1-1</VAR> and 0 to <VAR>n2-1</VAR> then the error
1250
 
handler is invoked.
1251
 
</DL>
1252
 
 
1253
 
</P>
1254
 
<P>
1255
 
<DL>
1256
 
<DT><U>Function:</U> double * <B>gsl_matrix_ptr</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>)</I>
1257
 
<DD><A NAME="IDX878"></A>
1258
 
<DT><U>Function:</U> const double * <B>gsl_matrix_const_ptr</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>)</I>
1259
 
<DD><A NAME="IDX879"></A>
1260
 
These functions return a pointer to the (i,j)-th element of a
1261
 
matrix <VAR>m</VAR>.  If <VAR>i</VAR> or <VAR>j</VAR> lie outside the allowed range of
1262
 
0 to <VAR>n1-1</VAR> and 0 to <VAR>n2-1</VAR> then the error handler is invoked
1263
 
and a null pointer is returned.
1264
 
</DL>
1265
 
 
1266
 
</P>
1267
 
 
1268
 
 
1269
 
<H3><A NAME="SEC173" HREF="gsl-ref_toc.html#TOC173">Initializing matrix elements</A></H3>
1270
 
<P>
1271
 
<A NAME="IDX880"></A>
1272
 
<A NAME="IDX881"></A>
1273
 
<A NAME="IDX882"></A>
1274
 
<A NAME="IDX883"></A>
1275
 
<A NAME="IDX884"></A>
1276
 
<A NAME="IDX885"></A>
1277
 
<A NAME="IDX886"></A>
1278
 
<A NAME="IDX887"></A>
1279
 
 
1280
 
</P>
1281
 
<P>
1282
 
<DL>
1283
 
<DT><U>Function:</U> void <B>gsl_matrix_set_all</B> <I>(gsl_matrix * <VAR>m</VAR>, double <VAR>x</VAR>)</I>
1284
 
<DD><A NAME="IDX888"></A>
1285
 
This function sets all the elements of the matrix <VAR>m</VAR> to the value
1286
 
<VAR>x</VAR>.
1287
 
</DL>
1288
 
 
1289
 
</P>
1290
 
<P>
1291
 
<DL>
1292
 
<DT><U>Function:</U> void <B>gsl_matrix_set_zero</B> <I>(gsl_matrix * <VAR>m</VAR>)</I>
1293
 
<DD><A NAME="IDX889"></A>
1294
 
This function sets all the elements of the matrix <VAR>m</VAR> to zero.
1295
 
</DL>
1296
 
 
1297
 
</P>
1298
 
<P>
1299
 
<DL>
1300
 
<DT><U>Function:</U> void <B>gsl_matrix_set_identity</B> <I>(gsl_matrix * <VAR>m</VAR>)</I>
1301
 
<DD><A NAME="IDX890"></A>
1302
 
This function sets the elements of the matrix <VAR>m</VAR> to the
1303
 
corresponding elements of the identity matrix, m(i,j) =
1304
 
\delta(i,j), i.e. a unit diagonal with all off-diagonal elements zero.
1305
 
This applies to both square and rectangular matrices.
1306
 
</DL>
1307
 
 
1308
 
</P>
1309
 
 
1310
 
 
1311
 
<H3><A NAME="SEC174" HREF="gsl-ref_toc.html#TOC174">Reading and writing matrices</A></H3>
1312
 
 
1313
 
<P>
1314
 
The library provides functions for reading and writing matrices to a file
1315
 
as binary data or formatted text.
1316
 
 
1317
 
</P>
1318
 
<P>
1319
 
<DL>
1320
 
<DT><U>Function:</U> int <B>gsl_matrix_fwrite</B> <I>(FILE * <VAR>stream</VAR>, const gsl_matrix * <VAR>m</VAR>)</I>
1321
 
<DD><A NAME="IDX891"></A>
1322
 
This function writes the elements of the matrix <VAR>m</VAR> to the stream
1323
 
<VAR>stream</VAR> in binary format.  The return value is 0 for success and
1324
 
<CODE>GSL_EFAILED</CODE> if there was a problem writing to the file.  Since the
1325
 
data is written in the native binary format it may not be portable
1326
 
between different architectures.
1327
 
</DL>
1328
 
 
1329
 
</P>
1330
 
<P>
1331
 
<DL>
1332
 
<DT><U>Function:</U> int <B>gsl_matrix_fread</B> <I>(FILE * <VAR>stream</VAR>, gsl_matrix * <VAR>m</VAR>)</I>
1333
 
<DD><A NAME="IDX892"></A>
1334
 
This function reads into the matrix <VAR>m</VAR> from the open stream
1335
 
<VAR>stream</VAR> in binary format.  The matrix <VAR>m</VAR> must be preallocated
1336
 
with the correct dimensions since the function uses the size of <VAR>m</VAR> to
1337
 
determine how many bytes to read.  The return value is 0 for success and
1338
 
<CODE>GSL_EFAILED</CODE> if there was a problem reading from the file.  The
1339
 
data is assumed to have been written in the native binary format on the
1340
 
same architecture.
1341
 
</DL>
1342
 
 
1343
 
</P>
1344
 
<P>
1345
 
<DL>
1346
 
<DT><U>Function:</U> int <B>gsl_matrix_fprintf</B> <I>(FILE * <VAR>stream</VAR>, const gsl_matrix * <VAR>m</VAR>, const char * <VAR>format</VAR>)</I>
1347
 
<DD><A NAME="IDX893"></A>
1348
 
This function writes the elements of the matrix <VAR>m</VAR> line-by-line to
1349
 
the stream <VAR>stream</VAR> using the format specifier <VAR>format</VAR>, which
1350
 
should be one of the <CODE>%g</CODE>, <CODE>%e</CODE> or <CODE>%f</CODE> formats for
1351
 
floating point numbers and <CODE>%d</CODE> for integers.  The function returns
1352
 
0 for success and <CODE>GSL_EFAILED</CODE> if there was a problem writing to
1353
 
the file.
1354
 
</DL>
1355
 
 
1356
 
</P>
1357
 
<P>
1358
 
<DL>
1359
 
<DT><U>Function:</U> int <B>gsl_matrix_fscanf</B> <I>(FILE * <VAR>stream</VAR>, gsl_matrix * <VAR>m</VAR>)</I>
1360
 
<DD><A NAME="IDX894"></A>
1361
 
This function reads formatted data from the stream <VAR>stream</VAR> into the
1362
 
matrix <VAR>m</VAR>.  The matrix <VAR>m</VAR> must be preallocated with the correct
1363
 
dimensions since the function uses the size of <VAR>m</VAR> to determine how many
1364
 
numbers to read.  The function returns 0 for success and
1365
 
<CODE>GSL_EFAILED</CODE> if there was a problem reading from the file.
1366
 
</DL>
1367
 
 
1368
 
</P>
1369
 
 
1370
 
 
1371
 
<H3><A NAME="SEC175" HREF="gsl-ref_toc.html#TOC175">Matrix views</A></H3>
1372
 
 
1373
 
<P>
1374
 
A matrix view is a temporary object, stored on the stack, which can be
1375
 
used to operate on a subset of matrix elements.  Matrix views can be
1376
 
defined for both constant and non-constant matrices using separate types
1377
 
that preserve constness.  A matrix view has the type
1378
 
<CODE>gsl_matrix_view</CODE> and a constant matrix view has the type
1379
 
<CODE>gsl_matrix_const_view</CODE>.  In both cases the elements of the view
1380
 
can by accessed using the <CODE>matrix</CODE> component of the view object.  A
1381
 
pointer <CODE>gsl_matrix *</CODE> or <CODE>const gsl_matrix *</CODE> can be obtained
1382
 
by taking the address of the <CODE>matrix</CODE> component with the <CODE>&#38;</CODE>
1383
 
operator.  In addition to matrix views it is also possible to create
1384
 
vector views of a matrix, such as row or column views.
1385
 
 
1386
 
</P>
1387
 
<P>
1388
 
<DL>
1389
 
<DT><U>Function:</U> gsl_matrix_view <B>gsl_matrix_submatrix</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>k1</VAR>, size_t <VAR>k2</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1390
 
<DD><A NAME="IDX895"></A>
1391
 
<DT><U>Function:</U> gsl_matrix_const_view <B>gsl_matrix_const_submatrix</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t <VAR>k1</VAR>, size_t <VAR>k2</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1392
 
<DD><A NAME="IDX896"></A>
1393
 
These functions return a matrix view of a submatrix of the matrix
1394
 
<VAR>m</VAR>.  The upper-left element of the submatrix is the element
1395
 
(<VAR>k1</VAR>,<VAR>k2</VAR>) of the original matrix.  The submatrix has <VAR>n1</VAR>
1396
 
rows and <VAR>n2</VAR> columns.  The physical number of columns in memory
1397
 
given by <VAR>tda</VAR> is unchanged.  Mathematically, the
1398
 
(i,j)-th element of the new matrix is given by,
1399
 
 
1400
 
</P>
1401
 
 
1402
 
<PRE class="example">
1403
 
m'(i,j) = m-&#62;data[(k1*m-&#62;tda + k1) + i*m-&#62;tda + j]
1404
 
</PRE>
1405
 
 
1406
 
<P>
1407
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n1-1</CODE> and the index <VAR>j</VAR>
1408
 
runs from 0 to <CODE>n2-1</CODE>.
1409
 
 
1410
 
</P>
1411
 
<P>
1412
 
The <CODE>data</CODE> pointer of the returned matrix struct is set to null if
1413
 
the combined parameters (<VAR>i</VAR>,<VAR>j</VAR>,<VAR>n1</VAR>,<VAR>n2</VAR>,<VAR>tda</VAR>)
1414
 
overrun the ends of the original matrix.
1415
 
 
1416
 
</P>
1417
 
<P>
1418
 
The new matrix view is only a view of the block underlying the existing
1419
 
matrix, <VAR>m</VAR>.  The block containing the elements of <VAR>m</VAR> is not
1420
 
owned by the new matrix view.  When the view goes out of scope the
1421
 
original matrix <VAR>m</VAR> and its block will continue to exist.  The
1422
 
original memory can only be deallocated by freeing the original matrix.
1423
 
Of course, the original matrix should not be deallocated while the view
1424
 
is still in use.
1425
 
 
1426
 
</P>
1427
 
<P>
1428
 
The function <CODE>gsl_matrix_const_submatrix</CODE> is equivalent to
1429
 
<CODE>gsl_matrix_submatrix</CODE> but can be used for matrices which are
1430
 
declared <CODE>const</CODE>.
1431
 
</DL>
1432
 
 
1433
 
</P>
1434
 
 
1435
 
<P>
1436
 
<DL>
1437
 
<DT><U>Function:</U> gsl_matrix_view <B>gsl_matrix_view_array</B> <I>(double * <VAR>base</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1438
 
<DD><A NAME="IDX897"></A>
1439
 
<DT><U>Function:</U> gsl_matrix_const_view <B>gsl_matrix_const_view_array</B> <I>(const double * <VAR>base</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1440
 
<DD><A NAME="IDX898"></A>
1441
 
These functions return a matrix view of the array <VAR>base</VAR>.  The
1442
 
matrix has <VAR>n1</VAR> rows and <VAR>n2</VAR> columns.  The physical number of
1443
 
columns in memory is also given by <VAR>n2</VAR>.  Mathematically, the
1444
 
(i,j)-th element of the new matrix is given by,
1445
 
 
1446
 
</P>
1447
 
 
1448
 
<PRE class="example">
1449
 
m'(i,j) = base[i*n2 + j]
1450
 
</PRE>
1451
 
 
1452
 
<P>
1453
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n1-1</CODE> and the index <VAR>j</VAR>
1454
 
runs from 0 to <CODE>n2-1</CODE>.
1455
 
 
1456
 
</P>
1457
 
<P>
1458
 
The new matrix is only a view of the array <VAR>base</VAR>.  When the view
1459
 
goes out of scope the original array <VAR>base</VAR> will continue to exist.
1460
 
The original memory can only be deallocated by freeing the original
1461
 
array.  Of course, the original array should not be deallocated while
1462
 
the view is still in use.
1463
 
 
1464
 
</P>
1465
 
<P>
1466
 
The function <CODE>gsl_matrix_const_view_array</CODE> is equivalent to
1467
 
<CODE>gsl_matrix_view_array</CODE> but can be used for matrices which are
1468
 
declared <CODE>const</CODE>.
1469
 
</DL>
1470
 
 
1471
 
</P>
1472
 
 
1473
 
<P>
1474
 
<DL>
1475
 
<DT><U>Function:</U> gsl_matrix_view <B>gsl_matrix_view_array_with_tda</B> <I>(double * <VAR>base</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>, size_t <VAR>tda</VAR>)</I>
1476
 
<DD><A NAME="IDX899"></A>
1477
 
<DT><U>Function:</U> gsl_matrix_const_view <B>gsl_matrix_const_view_array_with_tda</B> <I>(const double * <VAR>base</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>, size_t <VAR>tda</VAR>)</I>
1478
 
<DD><A NAME="IDX900"></A>
1479
 
These functions return a matrix view of the array <VAR>base</VAR> with a
1480
 
physical number of columns <VAR>tda</VAR> which may differ from the corresponding
1481
 
dimension of the matrix.  The matrix has <VAR>n1</VAR> rows and <VAR>n2</VAR>
1482
 
columns, and the physical number of columns in memory is given by
1483
 
<VAR>tda</VAR>.  Mathematically, the (i,j)-th element of the new
1484
 
matrix is given by,
1485
 
 
1486
 
</P>
1487
 
 
1488
 
<PRE class="example">
1489
 
m'(i,j) = base[i*tda + j]
1490
 
</PRE>
1491
 
 
1492
 
<P>
1493
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n1-1</CODE> and the index <VAR>j</VAR>
1494
 
runs from 0 to <CODE>n2-1</CODE>.
1495
 
 
1496
 
</P>
1497
 
<P>
1498
 
The new matrix is only a view of the array <VAR>base</VAR>.  When the view
1499
 
goes out of scope the original array <VAR>base</VAR> will continue to exist.
1500
 
The original memory can only be deallocated by freeing the original
1501
 
array.  Of course, the original array should not be deallocated while
1502
 
the view is still in use.
1503
 
 
1504
 
</P>
1505
 
<P>
1506
 
The function <CODE>gsl_matrix_const_view_array_with_tda</CODE> is equivalent
1507
 
to <CODE>gsl_matrix_view_array_with_tda</CODE> but can be used for matrices
1508
 
which are declared <CODE>const</CODE>.
1509
 
</DL>
1510
 
 
1511
 
</P>
1512
 
<P>
1513
 
<DL>
1514
 
<DT><U>Function:</U> gsl_matrix_view <B>gsl_matrix_view_vector</B> <I>(gsl_vector * <VAR>v</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1515
 
<DD><A NAME="IDX901"></A>
1516
 
<DT><U>Function:</U> gsl_matrix_const_view <B>gsl_matrix_const_view_vector</B> <I>(const gsl_vector * <VAR>v</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>)</I>
1517
 
<DD><A NAME="IDX902"></A>
1518
 
These functions return a matrix view of the vector <VAR>v</VAR>.  The matrix
1519
 
has <VAR>n1</VAR> rows and <VAR>n2</VAR> columns. The vector must have unit
1520
 
stride. The physical number of columns in memory is also given by
1521
 
<VAR>n2</VAR>.  Mathematically, the (i,j)-th element of the new
1522
 
matrix is given by,
1523
 
 
1524
 
</P>
1525
 
 
1526
 
<PRE class="example">
1527
 
m'(i,j) = v-&#62;data[i*n2 + j]
1528
 
</PRE>
1529
 
 
1530
 
<P>
1531
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n1-1</CODE> and the index <VAR>j</VAR>
1532
 
runs from 0 to <CODE>n2-1</CODE>.
1533
 
 
1534
 
</P>
1535
 
<P>
1536
 
The new matrix is only a view of the vector <VAR>v</VAR>.  When the view
1537
 
goes out of scope the original vector <VAR>v</VAR> will continue to exist.
1538
 
The original memory can only be deallocated by freeing the original
1539
 
vector.  Of course, the original vector should not be deallocated while
1540
 
the view is still in use.
1541
 
 
1542
 
</P>
1543
 
<P>
1544
 
The function <CODE>gsl_matrix_const_view_vector</CODE> is equivalent to
1545
 
<CODE>gsl_matrix_view_vector</CODE> but can be used for matrices which are
1546
 
declared <CODE>const</CODE>.
1547
 
</DL>
1548
 
 
1549
 
</P>
1550
 
 
1551
 
<P>
1552
 
<DL>
1553
 
<DT><U>Function:</U> gsl_matrix_view <B>gsl_matrix_view_vector_with_tda</B> <I>(gsl_vector * <VAR>v</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>, size_t <VAR>tda</VAR>)</I>
1554
 
<DD><A NAME="IDX903"></A>
1555
 
<DT><U>Function:</U> gsl_matrix_const_view <B>gsl_matrix_const_view_vector_with_tda</B> <I>(const gsl_vector * <VAR>v</VAR>, size_t <VAR>n1</VAR>, size_t <VAR>n2</VAR>, size_t <VAR>tda</VAR>)</I>
1556
 
<DD><A NAME="IDX904"></A>
1557
 
These functions return a matrix view of the vector <VAR>v</VAR> with a
1558
 
physical number of columns <VAR>tda</VAR> which may differ from the
1559
 
corresponding matrix dimension.  The vector must have unit stride. The
1560
 
matrix has <VAR>n1</VAR> rows and <VAR>n2</VAR> columns, and the physical number
1561
 
of columns in memory is given by <VAR>tda</VAR>.  Mathematically, the
1562
 
(i,j)-th element of the new matrix is given by,
1563
 
 
1564
 
</P>
1565
 
 
1566
 
<PRE class="example">
1567
 
m'(i,j) = v-&#62;data[i*tda + j]
1568
 
</PRE>
1569
 
 
1570
 
<P>
1571
 
where the index <VAR>i</VAR> runs from 0 to <CODE>n1-1</CODE> and the index <VAR>j</VAR>
1572
 
runs from 0 to <CODE>n2-1</CODE>.
1573
 
 
1574
 
</P>
1575
 
<P>
1576
 
The new matrix is only a view of the vector <VAR>v</VAR>.  When the view
1577
 
goes out of scope the original vector <VAR>v</VAR> will continue to exist.
1578
 
The original memory can only be deallocated by freeing the original
1579
 
vector.  Of course, the original vector should not be deallocated while
1580
 
the view is still in use.
1581
 
 
1582
 
</P>
1583
 
<P>
1584
 
The function <CODE>gsl_matrix_const_view_vector_with_tda</CODE> is equivalent
1585
 
to <CODE>gsl_matrix_view_vector_with_tda</CODE> but can be used for matrices
1586
 
which are declared <CODE>const</CODE>.
1587
 
</DL>
1588
 
 
1589
 
</P>
1590
 
 
1591
 
 
1592
 
 
1593
 
<H3><A NAME="SEC176" HREF="gsl-ref_toc.html#TOC176">Creating row and column views</A></H3>
1594
 
 
1595
 
<P>
1596
 
In general there are two ways to access an object, by reference or by
1597
 
copying.  The functions described in this section create vector views
1598
 
which allow access to a row or column of a matrix by reference.
1599
 
Modifying elements of the view is equivalent to modifying the matrix,
1600
 
since both the vector view and the matrix point to the same memory
1601
 
block.
1602
 
 
1603
 
</P>
1604
 
<P>
1605
 
<DL>
1606
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_matrix_row</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>)</I>
1607
 
<DD><A NAME="IDX905"></A>
1608
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_matrix_const_row</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>)</I>
1609
 
<DD><A NAME="IDX906"></A>
1610
 
These functions return a vector view of the <VAR>i</VAR>-th row of the matrix
1611
 
<VAR>m</VAR>.  The <CODE>data</CODE> pointer of the new vector is set to null if
1612
 
<VAR>i</VAR> is out of range.
1613
 
 
1614
 
</P>
1615
 
<P>
1616
 
The function <CODE>gsl_vector_const_row</CODE> is equivalent to
1617
 
<CODE>gsl_matrix_row</CODE> but can be used for matrices which are declared
1618
 
<CODE>const</CODE>.
1619
 
</DL>
1620
 
 
1621
 
</P>
1622
 
<P>
1623
 
<DL>
1624
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_matrix_column</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>j</VAR>)</I>
1625
 
<DD><A NAME="IDX907"></A>
1626
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_matrix_const_column</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t <VAR>j</VAR>)</I>
1627
 
<DD><A NAME="IDX908"></A>
1628
 
These functions return a vector view of the <VAR>j</VAR>-th column of the
1629
 
matrix <VAR>m</VAR>.  The <CODE>data</CODE> pointer of the new vector is set to
1630
 
null if <VAR>j</VAR> is out of range.
1631
 
 
1632
 
</P>
1633
 
<P>
1634
 
The function <CODE>gsl_vector_const_column</CODE> equivalent to
1635
 
<CODE>gsl_matrix_column</CODE> but can be used for matrices which are declared
1636
 
<CODE>const</CODE>.
1637
 
</DL>
1638
 
 
1639
 
</P>
1640
 
<P>
1641
 
<A NAME="IDX909"></A>
1642
 
<A NAME="IDX910"></A>
1643
 
<DL>
1644
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_matrix_diagonal</B> <I>(gsl_matrix * <VAR>m</VAR>)</I>
1645
 
<DD><A NAME="IDX911"></A>
1646
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_matrix_const_diagonal</B> <I>(const gsl_matrix * <VAR>m</VAR>)</I>
1647
 
<DD><A NAME="IDX912"></A>
1648
 
These functions returns a vector view of the diagonal of the matrix
1649
 
<VAR>m</VAR>. The matrix <VAR>m</VAR> is not required to be square. For a
1650
 
rectangular matrix the length of the diagonal is the same as the smaller
1651
 
dimension of the matrix.
1652
 
 
1653
 
</P>
1654
 
<P>
1655
 
The function <CODE>gsl_matrix_const_diagonal</CODE> is equivalent to
1656
 
<CODE>gsl_matrix_diagonal</CODE> but can be used for matrices which are
1657
 
declared <CODE>const</CODE>.
1658
 
</DL>
1659
 
 
1660
 
</P>
1661
 
<P>
1662
 
<A NAME="IDX913"></A>
1663
 
<A NAME="IDX914"></A>
1664
 
<DL>
1665
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_matrix_subdiagonal</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>k</VAR>)</I>
1666
 
<DD><A NAME="IDX915"></A>
1667
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_matrix_const_subdiagonal</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t <VAR>k</VAR>)</I>
1668
 
<DD><A NAME="IDX916"></A>
1669
 
These functions return a vector view of the <VAR>k</VAR>-th subdiagonal of
1670
 
the matrix <VAR>m</VAR>. The matrix <VAR>m</VAR> is not required to be square.
1671
 
The diagonal of the matrix corresponds to k = 0.
1672
 
 
1673
 
</P>
1674
 
<P>
1675
 
The function <CODE>gsl_matrix_const_subdiagonal</CODE> is equivalent to
1676
 
<CODE>gsl_matrix_subdiagonal</CODE> but can be used for matrices which are
1677
 
declared <CODE>const</CODE>.
1678
 
</DL>
1679
 
 
1680
 
</P>
1681
 
<P>
1682
 
<A NAME="IDX917"></A>
1683
 
<A NAME="IDX918"></A>
1684
 
<DL>
1685
 
<DT><U>Function:</U> gsl_vector_view <B>gsl_matrix_superdiagonal</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>k</VAR>)</I>
1686
 
<DD><A NAME="IDX919"></A>
1687
 
<DT><U>Function:</U> gsl_vector_const_view <B>gsl_matrix_const_superdiagonal</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t <VAR>k</VAR>)</I>
1688
 
<DD><A NAME="IDX920"></A>
1689
 
These functions return a vector view of the <VAR>k</VAR>-th superdiagonal of
1690
 
the matrix <VAR>m</VAR>. The matrix <VAR>m</VAR> is not required to be square. The
1691
 
diagonal of the matrix corresponds to k = 0.
1692
 
 
1693
 
</P>
1694
 
<P>
1695
 
The function <CODE>gsl_matrix_const_superdiagonal</CODE> is equivalent to
1696
 
<CODE>gsl_matrix_superdiagonal</CODE> but can be used for matrices which are
1697
 
declared <CODE>const</CODE>.
1698
 
</DL>
1699
 
 
1700
 
</P>
1701
 
 
1702
 
 
1703
 
 
1704
 
<H3><A NAME="SEC177" HREF="gsl-ref_toc.html#TOC177">Copying matrices</A></H3>
1705
 
 
1706
 
<P>
1707
 
<DL>
1708
 
<DT><U>Function:</U> int <B>gsl_matrix_memcpy</B> <I>(gsl_matrix * <VAR>dest</VAR>, const gsl_matrix * <VAR>src</VAR>)</I>
1709
 
<DD><A NAME="IDX921"></A>
1710
 
This function copies the elements of the matrix <VAR>src</VAR> into the
1711
 
matrix <VAR>dest</VAR>.  The two matrices must have the same size.
1712
 
</DL>
1713
 
 
1714
 
</P>
1715
 
<P>
1716
 
<DL>
1717
 
<DT><U>Function:</U> int <B>gsl_matrix_swap</B> <I>(gsl_matrix * <VAR>m1</VAR>, gsl_matrix * <VAR>m2</VAR>)</I>
1718
 
<DD><A NAME="IDX922"></A>
1719
 
This function exchanges the elements of the matrices <VAR>m1</VAR> and
1720
 
<VAR>m2</VAR> by copying.  The two matrices must have the same size.
1721
 
</DL>
1722
 
 
1723
 
</P>
1724
 
 
1725
 
 
1726
 
<H3><A NAME="SEC178" HREF="gsl-ref_toc.html#TOC178">Copying rows and columns</A></H3>
1727
 
 
1728
 
<P>
1729
 
The functions described in this section copy a row or column of a matrix
1730
 
into a vector.  This allows the elements of the vector and the matrix to
1731
 
be modified independently.  Note that if the matrix and the vector point
1732
 
to overlapping regions of memory then the result will be undefined.  The
1733
 
same effect can be achieved with more generality using
1734
 
<CODE>gsl_vector_memcpy</CODE> with vector views of rows and columns.
1735
 
 
1736
 
</P>
1737
 
<P>
1738
 
<DL>
1739
 
<DT><U>Function:</U> int <B>gsl_matrix_get_row</B> <I>(gsl_vector * <VAR>v</VAR>, const gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>)</I>
1740
 
<DD><A NAME="IDX923"></A>
1741
 
This function copies the elements of the <VAR>i</VAR>-th row of the matrix
1742
 
<VAR>m</VAR> into the vector <VAR>v</VAR>.  The length of the vector must be the
1743
 
same as the length of the row.
1744
 
</DL>
1745
 
 
1746
 
</P>
1747
 
<P>
1748
 
<DL>
1749
 
<DT><U>Function:</U> int <B>gsl_matrix_get_col</B> <I>(gsl_vector * <VAR>v</VAR>, const gsl_matrix * <VAR>m</VAR>, size_t <VAR>j</VAR>)</I>
1750
 
<DD><A NAME="IDX924"></A>
1751
 
This function copies the elements of the <VAR>j</VAR>-th column of the matrix
1752
 
<VAR>m</VAR> into the vector <VAR>v</VAR>.  The length of the vector must be the
1753
 
same as the length of the column.
1754
 
</DL>
1755
 
 
1756
 
</P>
1757
 
<P>
1758
 
<DL>
1759
 
<DT><U>Function:</U> int <B>gsl_matrix_set_row</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, const gsl_vector * <VAR>v</VAR>)</I>
1760
 
<DD><A NAME="IDX925"></A>
1761
 
This function copies the elements of the vector <VAR>v</VAR> into the
1762
 
<VAR>i</VAR>-th row of the matrix <VAR>m</VAR>.  The length of the vector must be
1763
 
the same as the length of the row.
1764
 
</DL>
1765
 
 
1766
 
</P>
1767
 
<P>
1768
 
<DL>
1769
 
<DT><U>Function:</U> int <B>gsl_matrix_set_col</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>j</VAR>, const gsl_vector * <VAR>v</VAR>)</I>
1770
 
<DD><A NAME="IDX926"></A>
1771
 
This function copies the elements of the vector <VAR>v</VAR> into the
1772
 
<VAR>j</VAR>-th column of the matrix <VAR>m</VAR>.  The length of the vector must be
1773
 
the same as the length of the column.
1774
 
</DL>
1775
 
 
1776
 
</P>
1777
 
 
1778
 
 
1779
 
<H3><A NAME="SEC179" HREF="gsl-ref_toc.html#TOC179">Exchanging rows and columns</A></H3>
1780
 
 
1781
 
<P>
1782
 
The following functions can be used to exchange the rows and columns of
1783
 
a matrix.
1784
 
 
1785
 
</P>
1786
 
<P>
1787
 
<DL>
1788
 
<DT><U>Function:</U> int <B>gsl_matrix_swap_rows</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>)</I>
1789
 
<DD><A NAME="IDX927"></A>
1790
 
This function exchanges the <VAR>i</VAR>-th and <VAR>j</VAR>-th rows of the matrix
1791
 
<VAR>m</VAR> in-place.
1792
 
</DL>
1793
 
 
1794
 
</P>
1795
 
<P>
1796
 
<DL>
1797
 
<DT><U>Function:</U> int <B>gsl_matrix_swap_columns</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>)</I>
1798
 
<DD><A NAME="IDX928"></A>
1799
 
This function exchanges the <VAR>i</VAR>-th and <VAR>j</VAR>-th columns of the
1800
 
matrix <VAR>m</VAR> in-place.
1801
 
</DL>
1802
 
 
1803
 
</P>
1804
 
<P>
1805
 
<DL>
1806
 
<DT><U>Function:</U> int <B>gsl_matrix_swap_rowcol</B> <I>(gsl_matrix * <VAR>m</VAR>, size_t <VAR>i</VAR>, size_t <VAR>j</VAR>)</I>
1807
 
<DD><A NAME="IDX929"></A>
1808
 
This function exchanges the <VAR>i</VAR>-th row and <VAR>j</VAR>-th column of the
1809
 
matrix <VAR>m</VAR> in-place.  The matrix must be square for this operation to
1810
 
be possible.
1811
 
</DL>
1812
 
 
1813
 
</P>
1814
 
<P>
1815
 
<DL>
1816
 
<DT><U>Function:</U> int <B>gsl_matrix_transpose_memcpy</B> <I>(gsl_matrix * <VAR>dest</VAR>, const gsl_matrix * <VAR>src</VAR>)</I>
1817
 
<DD><A NAME="IDX930"></A>
1818
 
This function makes the matrix <VAR>dest</VAR> the transpose of the matrix
1819
 
<VAR>src</VAR> by copying the elements of <VAR>src</VAR> into <VAR>dest</VAR>.  This
1820
 
function works for all matrices provided that the dimensions of the matrix
1821
 
<VAR>dest</VAR> match the transposed dimensions of the matrix <VAR>src</VAR>.
1822
 
</DL>
1823
 
 
1824
 
</P>
1825
 
<P>
1826
 
<DL>
1827
 
<DT><U>Function:</U> int <B>gsl_matrix_transpose</B> <I>(gsl_matrix * <VAR>m</VAR>)</I>
1828
 
<DD><A NAME="IDX931"></A>
1829
 
This function replaces the matrix <VAR>m</VAR> by its transpose by copying
1830
 
the elements of the matrix in-place.  The matrix must be square for this
1831
 
operation to be possible.
1832
 
</DL>
1833
 
 
1834
 
</P>
1835
 
 
1836
 
 
1837
 
<H3><A NAME="SEC180" HREF="gsl-ref_toc.html#TOC180">Matrix operations</A></H3>
1838
 
 
1839
 
<P>
1840
 
The following operations are defined for real and complex matrices.
1841
 
 
1842
 
</P>
1843
 
<P>
1844
 
<DL>
1845
 
<DT><U>Function:</U> int <B>gsl_matrix_add</B> <I>(gsl_matrix * <VAR>a</VAR>, const gsl_matrix * <VAR>b</VAR>)</I>
1846
 
<DD><A NAME="IDX932"></A>
1847
 
This function adds the elements of matrix <VAR>b</VAR> to the elements of
1848
 
matrix <VAR>a</VAR>, a'(i,j) = a(i,j) + b(i,j). The two matrices must have the
1849
 
same dimensions.
1850
 
</DL>
1851
 
 
1852
 
</P>
1853
 
<P>
1854
 
<DL>
1855
 
<DT><U>Function:</U> int <B>gsl_matrix_sub</B> <I>(gsl_matrix * <VAR>a</VAR>, const gsl_matrix * <VAR>b</VAR>)</I>
1856
 
<DD><A NAME="IDX933"></A>
1857
 
This function subtracts the elements of matrix <VAR>b</VAR> from the elements of
1858
 
matrix <VAR>a</VAR>, a'(i,j) = a(i,j) - b(i,j). The two matrices must have the
1859
 
same dimensions.
1860
 
</DL>
1861
 
 
1862
 
</P>
1863
 
<P>
1864
 
<DL>
1865
 
<DT><U>Function:</U> int <B>gsl_matrix_mul_elements</B> <I>(gsl_matrix * <VAR>a</VAR>, const gsl_matrix * <VAR>b</VAR>)</I>
1866
 
<DD><A NAME="IDX934"></A>
1867
 
This function multiplies the elements of matrix <VAR>a</VAR> by the elements of
1868
 
matrix <VAR>b</VAR>, a'(i,j) = a(i,j) * b(i,j). The two matrices must have the
1869
 
same dimensions.
1870
 
</DL>
1871
 
 
1872
 
</P>
1873
 
<P>
1874
 
<DL>
1875
 
<DT><U>Function:</U> int <B>gsl_matrix_div_elements</B> <I>(gsl_matrix * <VAR>a</VAR>, const gsl_matrix * <VAR>b</VAR>)</I>
1876
 
<DD><A NAME="IDX935"></A>
1877
 
This function divides the elements of matrix <VAR>a</VAR> by the elements of
1878
 
matrix <VAR>b</VAR>, a'(i,j) = a(i,j) / b(i,j). The two matrices must have the
1879
 
same dimensions.
1880
 
</DL>
1881
 
 
1882
 
</P>
1883
 
<P>
1884
 
<DL>
1885
 
<DT><U>Function:</U> int <B>gsl_matrix_scale</B> <I>(gsl_matrix * <VAR>a</VAR>, const double <VAR>x</VAR>)</I>
1886
 
<DD><A NAME="IDX936"></A>
1887
 
This function multiplies the elements of matrix <VAR>a</VAR> by the constant
1888
 
factor <VAR>x</VAR>, a'(i,j) = x a(i,j).
1889
 
</DL>
1890
 
 
1891
 
</P>
1892
 
<P>
1893
 
<DL>
1894
 
<DT><U>Function:</U> int <B>gsl_matrix_add_constant</B> <I>(gsl_matrix * <VAR>a</VAR>, const double <VAR>x</VAR>)</I>
1895
 
<DD><A NAME="IDX937"></A>
1896
 
This function adds the constant value <VAR>x</VAR> to the elements of the
1897
 
matrix <VAR>a</VAR>, a'(i,j) = a(i,j) + x.
1898
 
</DL>
1899
 
 
1900
 
</P>
1901
 
 
1902
 
 
1903
 
<H3><A NAME="SEC181" HREF="gsl-ref_toc.html#TOC181">Finding maximum and minimum elements of matrices</A></H3>
1904
 
 
1905
 
<P>
1906
 
The following operations are only defined for real matrices.
1907
 
 
1908
 
</P>
1909
 
<P>
1910
 
<DL>
1911
 
<DT><U>Function:</U> double <B>gsl_matrix_max</B> <I>(const gsl_matrix * <VAR>m</VAR>)</I>
1912
 
<DD><A NAME="IDX938"></A>
1913
 
This function returns the maximum value in the matrix <VAR>m</VAR>.
1914
 
</DL>
1915
 
 
1916
 
</P>
1917
 
<P>
1918
 
<DL>
1919
 
<DT><U>Function:</U> double <B>gsl_matrix_min</B> <I>(const gsl_matrix * <VAR>m</VAR>)</I>
1920
 
<DD><A NAME="IDX939"></A>
1921
 
This function returns the minimum value in the matrix <VAR>m</VAR>.
1922
 
</DL>
1923
 
 
1924
 
</P>
1925
 
<P>
1926
 
<DL>
1927
 
<DT><U>Function:</U> void <B>gsl_matrix_minmax</B> <I>(const gsl_matrix * <VAR>m</VAR>, double * <VAR>min_out</VAR>, double * <VAR>max_out</VAR>)</I>
1928
 
<DD><A NAME="IDX940"></A>
1929
 
This function returns the minimum and maximum values in the matrix
1930
 
<VAR>m</VAR>, storing them in <VAR>min_out</VAR> and <VAR>max_out</VAR>.
1931
 
</DL>
1932
 
 
1933
 
</P>
1934
 
<P>
1935
 
<DL>
1936
 
<DT><U>Function:</U> void <B>gsl_matrix_max_index</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t * <VAR>imax</VAR>, size_t * <VAR>jmax</VAR>)</I>
1937
 
<DD><A NAME="IDX941"></A>
1938
 
This function returns the indices of the maximum value in the matrix
1939
 
<VAR>m</VAR>, storing them in <VAR>imax</VAR> and <VAR>jmax</VAR>.  When there are
1940
 
several equal maximum elements then the first element found is returned,
1941
 
searching in row-major order.
1942
 
</DL>
1943
 
 
1944
 
</P>
1945
 
<P>
1946
 
<DL>
1947
 
<DT><U>Function:</U> void <B>gsl_matrix_min_index</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t * <VAR>imax</VAR>, size_t * <VAR>jmax</VAR>)</I>
1948
 
<DD><A NAME="IDX942"></A>
1949
 
This function returns the indices of the minimum value in the matrix
1950
 
<VAR>m</VAR>, storing them in <VAR>imax</VAR> and <VAR>jmax</VAR>.  When there are
1951
 
several equal minimum elements then the first element found is returned,
1952
 
searching in row-major order.
1953
 
</DL>
1954
 
 
1955
 
</P>
1956
 
<P>
1957
 
<DL>
1958
 
<DT><U>Function:</U> void <B>gsl_matrix_minmax_index</B> <I>(const gsl_matrix * <VAR>m</VAR>, size_t * <VAR>imin</VAR>, size_t * <VAR>imax</VAR>)</I>
1959
 
<DD><A NAME="IDX943"></A>
1960
 
This function returns the indices of the minimum and maximum values in
1961
 
the matrix <VAR>m</VAR>, storing them in (<VAR>imin</VAR>,<VAR>jmin</VAR>) and
1962
 
(<VAR>imax</VAR>,<VAR>jmax</VAR>). When there are several equal minimum or maximum
1963
 
elements then the first elements found are returned, searching in
1964
 
row-major order.
1965
 
</DL>
1966
 
 
1967
 
</P>
1968
 
 
1969
 
 
1970
 
<H3><A NAME="SEC182" HREF="gsl-ref_toc.html#TOC182">Matrix properties</A></H3>
1971
 
 
1972
 
<P>
1973
 
<DL>
1974
 
<DT><U>Function:</U> int <B>gsl_matrix_isnull</B> <I>(const gsl_matrix * <VAR>m</VAR>)</I>
1975
 
<DD><A NAME="IDX944"></A>
1976
 
This function returns 1 if all the elements of the matrix <VAR>m</VAR> are
1977
 
zero, and 0 otherwise.
1978
 
</DL>
1979
 
 
1980
 
</P>
1981
 
 
1982
 
 
1983
 
<H3><A NAME="SEC183" HREF="gsl-ref_toc.html#TOC183">Example programs for matrices</A></H3>
1984
 
 
1985
 
<P>
1986
 
The program below shows how to allocate, initialize and read from a matrix
1987
 
using the functions <CODE>gsl_matrix_alloc</CODE>, <CODE>gsl_matrix_set</CODE> and
1988
 
<CODE>gsl_matrix_get</CODE>.
1989
 
 
1990
 
</P>
1991
 
 
1992
 
<PRE class="example">
1993
 
#include &#60;stdio.h&#62;
1994
 
#include &#60;gsl/gsl_matrix.h&#62;
1995
 
 
1996
 
int
1997
 
main (void)
1998
 
{
1999
 
  int i, j; 
2000
 
  gsl_matrix * m = gsl_matrix_alloc (10, 3);
2001
 
  
2002
 
  for (i = 0; i &#60; 10; i++)
2003
 
    for (j = 0; j &#60; 3; j++)
2004
 
      gsl_matrix_set (m, i, j, 0.23 + 100*i + j);
2005
 
  
2006
 
  for (i = 0; i &#60; 10; i++)
2007
 
    for (j = 0; j &#60; 3; j++)
2008
 
      printf ("m(%d,%d) = %g\n", i, j, 
2009
 
              gsl_matrix_get (m, i, j));
2010
 
 
2011
 
  return 0;
2012
 
}
2013
 
</PRE>
2014
 
 
2015
 
<P>
2016
 
Here is the output from the program.  The final loop attempts to read
2017
 
outside the range of the matrix <CODE>m</CODE>, and the error is trapped by
2018
 
the range-checking code in <CODE>gsl_matrix_get</CODE>.
2019
 
 
2020
 
</P>
2021
 
 
2022
 
<PRE class="example">
2023
 
m(0,0) = 0.23
2024
 
m(0,1) = 1.23
2025
 
m(0,2) = 2.23
2026
 
m(1,0) = 100.23
2027
 
m(1,1) = 101.23
2028
 
m(1,2) = 102.23
2029
 
...
2030
 
m(9,2) = 902.23
2031
 
gsl: matrix_source.c:13: ERROR: first index out of range
2032
 
IOT trap/Abort (core dumped)
2033
 
</PRE>
2034
 
 
2035
 
<P>
2036
 
The next program shows how to write a matrix to a file.
2037
 
 
2038
 
</P>
2039
 
 
2040
 
<PRE class="example">
2041
 
#include &#60;stdio.h&#62;
2042
 
#include &#60;gsl/gsl_matrix.h&#62;
2043
 
 
2044
 
int
2045
 
main (void)
2046
 
{
2047
 
  int i, j, k = 0; 
2048
 
  gsl_matrix * m = gsl_matrix_alloc (100, 100);
2049
 
  gsl_matrix * a = gsl_matrix_alloc (100, 100);
2050
 
  
2051
 
  for (i = 0; i &#60; 100; i++)
2052
 
    for (j = 0; j &#60; 100; j++)
2053
 
      gsl_matrix_set (m, i, j, 0.23 + i + j);
2054
 
 
2055
 
  {  
2056
 
     FILE * f = fopen ("test.dat", "wb");
2057
 
     gsl_matrix_fwrite (f, m);
2058
 
     fclose (f);
2059
 
  }
2060
 
 
2061
 
  {  
2062
 
     FILE * f = fopen ("test.dat", "rb");
2063
 
     gsl_matrix_fread (f, a);
2064
 
     fclose (f);
2065
 
  }
2066
 
 
2067
 
  for (i = 0; i &#60; 100; i++)
2068
 
    for (j = 0; j &#60; 100; j++)
2069
 
      {
2070
 
        double mij = gsl_matrix_get (m, i, j);
2071
 
        double aij = gsl_matrix_get (a, i, j);
2072
 
        if (mij != aij) k++;
2073
 
      }
2074
 
 
2075
 
  printf ("differences = %d (should be zero)\n", k);
2076
 
  return (k &#62; 0);
2077
 
}
2078
 
</PRE>
2079
 
 
2080
 
<P>
2081
 
After running this program the file <TT>'test.dat'</TT> should contain the
2082
 
elements of <CODE>m</CODE>, written in binary format.  The matrix which is read
2083
 
back in using the function <CODE>gsl_matrix_fread</CODE> should be exactly
2084
 
equal to the original matrix.
2085
 
 
2086
 
</P>
2087
 
<P>
2088
 
The following program demonstrates the use of vector views.  The program
2089
 
computes the column-norms of a matrix.
2090
 
 
2091
 
</P>
2092
 
 
2093
 
<PRE class="example">
2094
 
#include &#60;math.h&#62;
2095
 
#include &#60;stdio.h&#62;
2096
 
#include &#60;gsl/gsl_matrix.h&#62;
2097
 
#include &#60;gsl/gsl_blas.h&#62;
2098
 
 
2099
 
int
2100
 
main (void)
2101
 
{
2102
 
  size_t i,j;
2103
 
 
2104
 
  gsl_matrix *m = gsl_matrix_alloc (10, 10);
2105
 
 
2106
 
  for (i = 0; i &#60; 10; i++)
2107
 
    for (j = 0; j &#60; 10; j++)
2108
 
      gsl_matrix_set (m, i, j, sin (i) + cos (j));
2109
 
 
2110
 
  for (j = 0; j &#60; 10; j++)
2111
 
    {
2112
 
      gsl_vector_view column = gsl_matrix_column (m, j);
2113
 
      double d;
2114
 
 
2115
 
      d = gsl_blas_dnrm2 (&#38;column.vector);
2116
 
 
2117
 
      printf ("matrix column %d, norm = %g\n", j, d);
2118
 
    }
2119
 
 
2120
 
  gsl_matrix_free (m);
2121
 
 
2122
 
  return 0;
2123
 
}
2124
 
</PRE>
2125
 
 
2126
 
<P>
2127
 
Here is the output of the program, which can be confirmed using GNU
2128
 
OCTAVE,
2129
 
 
2130
 
</P>
2131
 
 
2132
 
<PRE class="example">
2133
 
$ ./a.out
2134
 
matrix column 0, norm = 4.31461
2135
 
matrix column 1, norm = 3.1205
2136
 
matrix column 2, norm = 2.19316
2137
 
matrix column 3, norm = 3.26114
2138
 
matrix column 4, norm = 2.53416
2139
 
matrix column 5, norm = 2.57281
2140
 
matrix column 6, norm = 4.20469
2141
 
matrix column 7, norm = 3.65202
2142
 
matrix column 8, norm = 2.08524
2143
 
matrix column 9, norm = 3.07313
2144
 
</PRE>
2145
 
 
2146
 
 
2147
 
<PRE class="example">
2148
 
octave&#62; m = sin(0:9)' * ones(1,10) 
2149
 
               + ones(10,1) * cos(0:9); 
2150
 
octave&#62; sqrt(sum(m.^2))
2151
 
ans =
2152
 
 
2153
 
  4.3146  3.1205  2.1932  3.2611  2.5342  2.5728
2154
 
  4.2047  3.6520  2.0852  3.0731
2155
 
</PRE>
2156
 
 
2157
 
 
2158
 
 
2159
 
<H2><A NAME="SEC184" HREF="gsl-ref_toc.html#TOC184">References and Further Reading</A></H2>
2160
 
 
2161
 
<P>
2162
 
The block, vector and matrix objects in GSL follow the <CODE>valarray</CODE>
2163
 
model of C++.  A description of this model can be found in the following
2164
 
reference,
2165
 
 
2166
 
</P>
2167
 
 
2168
 
<UL class="itemize">
2169
 
<LI>
2170
 
 
2171
 
B. Stroustrup,
2172
 
<CITE>The C++ Programming Language</CITE> (3rd Ed), 
2173
 
Section 22.4 Vector Arithmetic.
2174
 
Addison-Wesley 1997, ISBN 0-201-88954-4.
2175
 
</UL>
2176
 
 
2177
 
<P><HR><P>
2178
 
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_7.html">previous</A>, <A HREF="gsl-ref_9.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
2179
 
</BODY>
2180
 
</HTML>