2
<title>Blitz++ Class Reference: Vector<T></title>
5
<h1>Blitz++ Class Reference: Vector<T></h1>
7
<table border cellpadding=10 align=top>
11
<li><a href="#summary">Summary</a></li>
12
<li><a href="#synopsis">Synopsis</a></li>
13
<li><a href="#template">Template parameter</a></li>
14
<li><a href="#publictype">Public types</a></li>
15
<li><a href="#construct">Constructors</a></li>
16
<li><a href="#member">Member functions</a></li>
17
<li><a href="#memberops">Member operators</a></li>
18
<li><a href="#ops">Operators</a></li>
19
<li><a href="#global">Global math functions</a></li>
20
<li><a href="#global2">Other global functions</a></li>
21
<li><a href="#example">Example</a></li>
25
<h3>Related topics and classes</h3>
27
<li>The <a href="range.html">Range</a> class</li>
28
<!-- <li>Aliasing and the <a href="restrict.html">restrict</a> keyword</li> -->
29
<!-- <li>Data/view model</li> -->
30
<!-- <li>Expression templates</li> -->
31
<!-- <li>Type promotion</li> -->
32
<!-- <li>Performance benchmarks</li> -->
33
<!-- <li>where/elsewhere/endwhere</li> -->
34
<!-- <li>Matrix<T></li> -->
35
<!-- <li>FFTServer<T></li> -->
40
<h3>Inheritance diagram</h3>
41
MemoryBlockReference<T><br>
48
<!-- <h2>Summary</h2> -->
54
#include <blitz/vector.h>
55
using namespace blitz;
56
Vector<double> x(100);
60
<h2>Template parameter</h2>
63
The template parameter of Vector<T> is the element type of the
64
vector. This should an integral, floating point, or complex type.
65
These choices of <b>T</b> should work:
67
<li>bool, char, unsigned char, short int, short unsigned int, int, unsigned int,
68
long, unsigned long</li>
69
<li>float, double, long double</li>
70
<li>complex<float>, complex<double>, and
71
complex<long double></li>
74
may work provided they have the necessary numeric semantics.
80
<p>Vector<T> declares several publicly accessible
81
types. They may be accessed using the scope (::) operator: e.g.
83
Vector<double> x(50); // Create a vector of length 50
84
Vector<double>::T_iterator z = x.begin(); // Get an iterator for x
87
<table border cellpadding=2>
99
<td>The numeric type of the vector elements (e.g. double)</td>
104
<td>Complete type of the vector itself (e.g. Vector<double>)</td></tr>
107
<b>T_iterator</b></td>
108
<td>STL-like iterator to be used on this vector (see begin(), end())
109
** Not yet supported</td>
113
<b>T_constIterator</b></td>
114
<td>STL-like const iterator to be used on this vector (see begin(), end())
115
** Not yet supported</td>
120
<h2>Constructors</h2>
123
<li><b>Vector()</b><br><br>
124
Default constructor. No memory is allocated; the length of the vector is
126
vector may subsequently be resized using resize(), or used to refer to another
127
vector's data using reference().<br></li>
130
<b>Vector(size_t length)</b><br><br>
131
Create a new vector of the given length. Memory is allocated using
132
<b>new</b>. Elements are not initialized.<br></li>
135
<b>Vector(Vector<T_numtype>& x)</b><br><br>
136
Creates a reference (or alias) to the data of vector x. Both x and this
137
vector now refer to the same underlying data, so any changes made to the
138
data via x will be visible in this vector. (Note: T_numtype is a publicly
139
declared typedef for the template parameter).<br></li>
142
<b>Vector(Vector<T_numtype>& x, <a href="range.html">Range</a> r)</b><br><br>
143
Creates a reference (or alias) to a portion of the data of vector x.
144
The range object specifies an interval of the index set. For example,
146
Vector<double> x(50); // Create a vector of length 50
147
Vector<double> y(x, Range(10,20)); // y is now of length 11, and
148
// refers to elements 10-20 of x
149
y[5] = 3; // y[5] is aliased to x[15]
154
<b>Vector(size_t length, T_numtype initValue)</b><br><br>
155
Creates a new vector of the given length, and initializes all elements
156
to initValue. Memory is allocated using <b>new</b>.</li>
159
<b>Vector(size_t length, T_numtype firstValue, T_numtype inc)</b><br><br>
160
Creates a new vector of the given length, and initializes the elements
161
to [ firstValue, firstValue + inc, firstValue + 2 * inc, ...,
162
firstValue + (length-1) * inc ]. Memory is allocated using <b>new</b>.</li>
165
<b>Vector(<a href="range.html">Range</a> r)</b><br><br>
166
Creates a vector and initializes the elements to the range r. For
169
Vector<int> z(Range(0,5)); // z = [ 0, 1, 2, 3, 4, 5 ]
174
<b>Vector(size_t length, Random<P_distribution>& random)</b><br><br>
175
Creates a vector and initializes its elements with random numbers.
178
#include <blitz/rand-normal.h>
179
Random<Normal> gaussianNoise(0.0, 2.5); // Zero-mean, variance 2.5
180
Vector<double> x(50, gaussianNoise);
185
<b>Vector(size_t length, T_numtype* [restrict] data, int stride = 1)</b><br><br>
186
Creates a vector which refers to the given data. The <a href="restrict.html">
187
NCEG <b>restrict</b></a>
188
keyword can be used only with compilers which support it. The stride
189
parameter gives the spacing of elements: in the created vector, element
190
<b>i</b> will be <b>data[i * stride]</b>. When the vector object is
191
destroyed, the memory is not freed (i.e. the vector does not assume
192
ownership of the data).</li>
195
<b>Vector(<i>vector expression</i>)</b><br><br>
196
Creates a vector of appropriate length and stores the result of
197
<i>vector expression</i> in it. (See the explanation of expression
198
templates). Examples:
200
// Create a vector of length 16 containing a sampled cosine (full period)
201
Vector<double> x(cos(Range(0,15) * 2 * M_PI / 16.0));
203
// Create a new vector containing an approximation of the derivative
206
double delta = 1 / 16.; // Spacing between samples
207
Vector<double> y = (x(I) - x(I-1)) / delta;
216
<h2>Member functions</h2>
220
<b>T_iterator begin()</b> ** NOT YET SUPPORTED<br><br>
221
Returns an STL-compliant iterator positioned at the beginning of the vector's
222
data. T_iterator is a public type declared by Vector<T_numtype>, and
223
should be used to specify the type of the iterator:
225
Vector<double> x(50);
226
Vector<double>::T_iterator iter = x.begin();
228
Since T_iterator is STL-compliant, it may be used with the standard
229
template library routines. [GIVE EXAMPLES] See also end().
233
<b>T_constIterator begin() const</b> ** NOT YET SUPPORTED<br><br>
234
Returns an STL-compliant const iterator positioned at the beginning of
235
the vector's data. A const iterator has read-only access to the vector
236
elements. T_constIterator is a public type declared by Vector<T_numtype>,
237
and should be used to specify the type of the iterator:
239
Vector<int> z(50,0,1); // Creates the vector [ 0 1 2 ... 49 ]
240
const Vector<int>& zref = z; // Create a const reference to z
241
Vector<int>::T_constIterator iter = zref.begin();
243
Since T_constIterator is STL-compliant, it may be used with the
244
standard template library routines. [GIVE EXAMPLES] See also end().<br><br>
246
The begin() method is const-overloaded. By default, a non-const iterator
247
is returned. A const iterator is returned only when the vector itself
252
<b>T_vector copy() const</b><br><br>
253
Returns a copy of the vector. A new block of memory is allocated, and
254
the vector copy is guaranteed to have unit stride.
258
<b>T_iterator end()</b><br><br>
259
Returns an STL-compliant iterator positioned at the end of the
260
vector's data. See begin() above.
264
<b>T_constIterator end() const</b><br><br>
265
Returns an STL-compliant const iterator positioned at the end of the
266
vector's data. See begin() above.
270
<b>T_numtype * [<i>restrict</i>] data()<br>
271
const T_numtype * [<i>restrict</i>] data() const</b><br><br>
272
Obtain a pointer to the beginning of the vector data. This method
273
should be used with caution, since it has the potential to
274
cause data corruption and/or segment violations if used improperly.
275
The vector data is not necessarily stored contiguously; the
276
spacing between vector elements can be obtained via the stride()
277
member function. Here is an example:
279
Vector<float> x(10,0,1); // Creates [ 0 1 2 3 4 5 6 7 8 9 ]
280
float* xptr = x.data(); // Get a pointer to the vector data
281
int length = x.length(); // Obtain the length of the vector (10)
282
int stride = x.stride(); // Note that stride may be negative
284
for (int i=0; i < length; ++i)
285
xptr[i * stride] = 7; // Set each element to 7
287
// Now the vector contains [ 7 7 7 7 7 7 7 7 7 7 ]
288
// Note that "x = 7;" would accomplish the same result.
290
The <a href="restrict.html">NCEG restrict</a> keyword is applicable
291
only if the compiler supports it.<br><br>
293
The data() method is const-overloaded: if the vector is const, then
294
data() returns a const T_numtype* pointer.
298
<b>size_t length() const</b><br><br>
299
Returns the length of the vector.</li>
302
<b>void makeUnique()</b><br><br>
303
If the vector's elements are referenced (or aliased) by another vector,
304
a copy is made, and on return the vector refers to the new copy.
308
<b>void reference(T_vector& x)</b><br><br>
309
Makes the vector an alias for vector x. After this member function is
310
used, both the vector and x refer to the same underlying data.</li>
313
<b>void resize(size_t length)</b><br><br>
314
If length is different than the current length of the vector, the
315
vector is resized. The current contents are lost.
319
<b>void resizeAndPreserve(size_t newLength)</b><br><br>
320
Allocates a new vector of size newLength, and copies as much of
321
the current vector as will fit into the new vector. If the new
322
vector is larger than the previous vector, then the unused elements
323
are left uninitialized.</li>
326
<b>Vector<T_numtype> reverse()</b><br><br>
327
Returns a view of the vector in reverse order. This is done using
328
a negative stride.</li>
331
<b>int stride() const</b><br><br>
332
Returns the distance between vector elements in memory. This
333
will normally be 1 (referred to as <i>unit stride</i>).</li>
338
<h2>Member operators</h2>
341
T_numtype operator()(unsigned i) const
342
T_numtype& operator()(unsigned i)
343
T_numtype operator[](unsigned i) const
344
T_numtype& operator[](unsigned i)
345
T_vector operator()(Range r)
346
T_vector operator[](Range r)
347
T_pick operator[](Vector<int>) ** Currently broken
352
<h3>Iostream operators</h3>
355
ostream& operator>>(ostream& os, const <i>vector expression</i>&);
357
Formats a vector or vector expression for output. In future releases,
358
several output formats will be supported.
360
<h3>Arithmetic operators</h3>
362
Arithmetic operators are implemented using expression templates. These
363
assignment operators are supported:
366
operator=(<i>vector expression</i>)
367
operator+=(<i>vector expression</i>)
368
operator-=(<i>vector expression</i>)
369
operator*=(<i>vector expression</i>)
370
operator/=(<i>vector expression</i>)
371
operator%=(<i>vector expression</i>)
372
operator^=(<i>vector expression</i>)
373
operator&=(<i>vector expression</i>)
374
operator|=(<i>vector expression</i>)
375
operator>>=(<i>vector expression</i>)
376
operator=(<i>vector expression</i>)
380
A <i>vector expression</i> can be any combination of these operators
381
and operands, as well as use of the math functions listed later.
384
<h4>Vector expression operators</h4>
386
+ - * / % ^ & | >> <<
387
> < >= <= == != && ||
390
<h4>Vector expression operands</h4>
392
<li>Vector<T_numtype></li>
393
<li>Vector<T_numtype2> (A vector of a different type)</li>
394
<li>VectorPick<T_numtype></li>
395
<li><a href="range.html">Range</a></li>
396
<li>Random ** NOT YET FULLY SUPPORTED</li>
397
<li>Scalar constants (int,float,double,long double)</li>
398
<li>Complex constants (complex<float>, complex<double>,
399
complex<long double>)</li>
403
Arithmetic type promotion for vectors is identical to type promotion
404
for built-in types. For example, adding a double constant to a
405
Vector<int> will result in a Vector<double>; multiplying
406
a Vector<long double> by a Vector<float> will result in
407
a Vector<long double>. Generally, the result is promoted to
408
which ever type preserves the greatest precision.</p>
410
<p>Note that division and multiplication of integers may result in
411
truncation and/or wraparound, since the result remains an integer
412
type. The solution is to cast the elements of one of the vectors
413
as floating-point types; see <b>cast<T2></b> below.
414
** the cast() function is not yet implemented</p>
417
Vector<int> x(5), y(5);
418
Vector<double> z(5);
422
z = x / y; // Calculated using integer math, then cast as double
423
// Results in truncation
425
z = x / cast<double>(y); // Calculated using floating point
430
<!-- <h2>Global functions</h2> -->
432
<h3>Single-operand math functions</h3>
436
<li>Single operand math functions are evaluated using expression templates.</li>
437
<li>If used on an integer vector, most of the functions below will return
438
the answer type as a double.</li>
441
<table border cellpadding=2>
442
<tr align=center><th>Function</th><th>Description</th><th>Real vectors</th><th>Complex vectors</th><th>Availability</th></tr>
443
<tr><td>abs</td><td>Absolute value</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
444
<tr><td>acos</td><td>Inverse cosine. Elements must be in the range [-1,+1]. The resulting elements lie in [-Pi,+Pi].</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
445
<tr><td>acosh</td><td>Inverse hyperbolic cosine</td> <td>Y</td> <td></td> <td>all</td><td> </td></tr>
448
<td>Argument (phase/angle) of a complex vector. Result is a scalar vector whose elements lie in [-Pi, +Pi].</td>
451
<td>all<sup>b</sup></td></tr>
453
<tr><td>asin</td><td>Inverse sine. Elements must be in the range [-1,+1]. The resulting elements lie in [-Pi,+Pi].</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
454
<tr><td>asinh</td><td>Inverse hyperbolic sine</td> <td>Y</td> <td></td> <td>all</td><td> </td></tr>
455
<tr><td>atan</td><td>Inverse tangent. Resulting elements lie in [-Pi,+Pi].</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
456
<tr><td>atanh</td><td>Inverse hyperbolic tangent</td> <td>Y</td> <td></td> <td>all</td><td> </td></tr>
457
<tr><td>cast<T2></td><td>Cast vector elements to type T2<br>** NOT YET AVAILABLE</td><td>Y</td><td>Y</td><td>some<sup>e</sup></td></tr>
458
<tr><td>cbrt</td><td>Cubic root</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
459
<tr><td>ceil</td><td>Smallest floating integer not less than element</td> <td>Y</td> <td></td> <td>all</td><td> </td></tr>
460
<tr><td>class</td><td>Floating-point classification. Result is an integer
461
vector with elements taking values FP_PLUS_NORM,
462
FP_MINUS_NORM, FP_PLUS_ZERO, FP_MINUS_ZERO, FP_PLUS_INF, FP_MINUS_INF,
463
FP_PLUS_DENORM, FP_MINUS_DENORM, FP_SNAN, FP_QNAN as defined in <float.h>
465
<td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
468
<td>Complex conjugate</td>
471
<td>all<sup>b</sup></td></tr>
473
<tr><td>cos</td><td>Cosine. Resulting elements lie in [-1,+1].</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
474
<tr><td>cosh</td><td>Hyperbolic cosine</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
475
<tr><td>exp</td><td>Exponential.</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
476
<tr><td>expm1</td><td>Exponential minus one: exp(x)-1</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
477
<tr><td>erf</td><td>Error function</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
478
<tr><td>erfc</td><td>Complementary error function (1-erf(x)).</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
479
<tr><td>fabs</td><td>Same as <b>abs</b></td> <td>Y</td> <td></td> <td>all</td><td> </td></tr>
480
<tr><td>finite</td><td>Nonzero if finite. Result is integer.</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
481
<tr><td>floor</td><td>Largest floating int not greater than x</td> <td>Y</td> <td></td> <td>all</td><td> </td></tr>
482
<tr><td>ilogb</td><td>Integer unbiased exponent</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
485
<td>Imaginary portion of a complex vector. Result is a scalar vector which
486
may be used as an lvalue.</td>
489
<td>all<sup>b</sup></td></tr>
491
<tr><td>isnan</td><td>Nonzero if x is NaNS (Signalling Not a Number) or NaNQ (Quiet Not A Number). Result is integer.</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
492
<tr><td>itrunc</td><td>Truncate and convert to integer. Result is integer.</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
495
<td>Inverse of a complex number</td>
498
<td>all<sup>b</sup></td></tr>
500
<tr><td>j0</td><td>Bessel function first kind, order 0</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
501
<tr><td>j1</td><td>Bessel function first kind, order 1<br>
502
See also y0(x), y1(x), jn(x,y) and yn(x,y).
503
</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
504
<tr><td>lgamma</td><td>Log absolute gamma</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
505
<tr><td>log</td><td>Natural logarithm</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
506
<tr><td>logb</td><td>Unbiased exponent (IEEE)</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
507
<tr><td>log1p</td><td>Natural logarithm of (1+x)</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
508
<tr><td>log10</td><td>Logarithm base 10</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
509
<tr><td>nearest</td><td>Nearest floating point integer to x.</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
512
<td>Norm (magnitude) of a complex vector. Result is a scalar vector.</td>
515
<td>all<sup>b</sup></td></tr>
518
<td>Real portion of a complex vector. Result is a scalar vector which may
519
be used as an lvalue.</td>
522
<td>all<sup>b</sup></td></tr>
524
<tr><td>rint</td><td>Round to floating point integer, using the current
525
floating-point rounding mode. Rounding mode is read and set by
526
the functions fp_read_rnd() and fp_swap_rnd(). (See system man pages)</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
527
<tr><td>rsqrt</td><td>Reciprocal square root (i.e. 1.0/sqrt(x))</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td><td> </td></tr>
528
<tr><td>sin</td><td>Sine. Resulting elements lie in [-1,+1].</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
529
<tr><td>sinh</td><td>Hyperbolic sine</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
530
<tr><td>sqr</td><td>Square: equivalent to x*x</td> <td>Y</td> <td>Y</td> <td>all<sup>e</sup></td><td> </td></tr>
531
<tr><td>sqrt</td><td>Square root</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
532
<tr><td>tan</td><td>Tangent</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
533
<tr><td>tanh</td><td>Hyperbolic tangent</td> <td>Y</td> <td>Y</td> <td>all</td><td> </td></tr>
534
<tr><td>trunc</td><td>Nearest floating-point integer in the direction of zero</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
535
<tr><td>uitrunc</td><td>Truncate and convert to unsigned. Result is unsigned integer.</td> <td>Y</td> <td></td> <td>some<sup>d</sup></td> </tr>
536
<tr><td>y0</td><td>Bessel function 2nd kind, order 0</td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
537
<tr><td>y1</td><td>Bessel function 2nd kind, order 1
538
<br>See also yn(x,y) below</br></td> <td>Y</td> <td></td> <td>some<sup>c</sup></td><td> </td></tr>
541
<sup>a</sup>ANSI C math function<br>
542
<sup>b</sup>ANSI C++ math function<br>
543
<sup>c</sup>IEEE 754 standard required function<br>
544
<sup>d</sup>IEEE 754 recommended function<br>
545
<sup>e</sup>Nonstandard function specific to Blitz++<br>
548
<h3>Global math functions with two operands</h3>
550
** These functions are not yet implemented **
553
All the two operand math functions are provided in three forms:<br>
557
copysign(vector,double)
560
frexp(d, Vector<int>& e) NOT INCLUDED
562
jn(int, d) NOT INCLUDED
563
ldexp(d,i) NOT INCLUDED
564
modf(d, Vector<int>& e) NOT INCLUDED
568
polar(d,d) NOT INCLUDED
573
yn(int,d) NOT INCLUDED
577
<h2>Other global functions</h2>
579
(These functions <i>are</i> available)
587
<!-- innerProduct (same as dot) -->
589
<!-- inversePermute -->
601
<!-- outerProduct (same as kronecker) -->
611
<h2>The <i>where</i> function</h2>
614
The <i>where(X,Y,Z)</i> function provides the same functionality as
615
the operator X ? Y : Z. If X is logical true, then Y is returned;
616
otherwise, Z is returned.
620
The <i>where</i> function is implemented using expression templates.
621
The arguments X, Y, and Z can each be vectors, vector expressions,
622
<a href="range.html">Range</a>, or Vector picks.
628
Please see the files in the <a href="../examples/"><b>Blitz++/examples</b></a> directory.