1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 The GSL Team.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with the
Invariant Sections being "GNU General Public License" and "Free Software
Needs Free Documentation", the Front-Cover text being "A GNU Manual",
and with the Back-Cover Text being (a) (see below). A copy of the
license is included in the section entitled "GNU Free Documentation
License".
(a) The Back-Cover Text is: "You have the freedom to copy and modify this
GNU Manual." -->
<!-- Created by GNU Texinfo 5.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Scientific Library – Reference Manual: Vector views</title>
<meta name="description" content="GNU Scientific Library – Reference Manual: Vector views">
<meta name="keywords" content="GNU Scientific Library – Reference Manual: Vector views">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Function-Index.html#Function-Index" rel="index" title="Function Index">
<link href="Vectors.html#Vectors" rel="up" title="Vectors">
<link href="Copying-vectors.html#Copying-vectors" rel="next" title="Copying vectors">
<link href="Reading-and-writing-vectors.html#Reading-and-writing-vectors" rel="previous" title="Reading and writing vectors">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="Vector-views"></a>
<div class="header">
<p>
Next: <a href="Copying-vectors.html#Copying-vectors" accesskey="n" rel="next">Copying vectors</a>, Previous: <a href="Reading-and-writing-vectors.html#Reading-and-writing-vectors" accesskey="p" rel="previous">Reading and writing vectors</a>, Up: <a href="Vectors.html#Vectors" accesskey="u" rel="up">Vectors</a> [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="Vector-views-1"></a>
<h4 class="subsection">8.3.5 Vector views</h4>
<p>In addition to creating vectors from slices of blocks it is also
possible to slice vectors and create vector views. For example, a
subvector of another vector can be described with a view, or two views
can be made which provide access to the even and odd elements of a
vector.
</p>
<a name="index-gsl_005fvector_005fview"></a>
<a name="index-gsl_005fvector_005fconst_005fview"></a>
<p>A vector view is a temporary object, stored on the stack, which can be
used to operate on a subset of vector elements. Vector views can be
defined for both constant and non-constant vectors, using separate types
that preserve constness. A vector view has the type
<code>gsl_vector_view</code> and a constant vector view has the type
<code>gsl_vector_const_view</code>. In both cases the elements of the view
can be accessed as a <code>gsl_vector</code> using the <code>vector</code> component
of the view object. A pointer to a vector of type <code>gsl_vector *</code>
or <code>const gsl_vector *</code> can be obtained by taking the address of
this component with the <code>&</code> operator.
</p>
<p>When using this pointer it is important to ensure that the view itself
remains in scope—the simplest way to do so is by always writing the
pointer as <code>&</code><var>view</var><code>.vector</code>, and never storing this value
in another variable.
</p>
<dl>
<dt><a name="index-gsl_005fvector_005fsubvector"></a>Function: <em>gsl_vector_view</em> <strong>gsl_vector_subvector</strong> <em>(gsl_vector * <var>v</var>, size_t <var>offset</var>, size_t <var>n</var>)</em></dt>
<dt><a name="index-gsl_005fvector_005fconst_005fsubvector"></a>Function: <em>gsl_vector_const_view</em> <strong>gsl_vector_const_subvector</strong> <em>(const gsl_vector * <var>v</var>, size_t <var>offset</var>, size_t <var>n</var>)</em></dt>
<dd><p>These functions return a vector view of a subvector of another vector
<var>v</var>. The start of the new vector is offset by <var>offset</var> elements
from the start of the original vector. The new vector has <var>n</var>
elements. Mathematically, the <var>i</var>-th element of the new vector
<var>v’</var> is given by,
</p>
<div class="example">
<pre class="example">v'(i) = v->data[(offset + i)*v->stride]
</pre></div>
<p>where the index <var>i</var> runs from 0 to <code>n-1</code>.
</p>
<p>The <code>data</code> pointer of the returned vector struct is set to null if
the combined parameters (<var>offset</var>,<var>n</var>) overrun the end of the
original vector.
</p>
<p>The new vector is only a view of the block underlying the original
vector, <var>v</var>. The block containing the elements of <var>v</var> is not
owned by the new vector. When the view goes out of scope the original
vector <var>v</var> and its block will continue to exist. The original
memory can only be deallocated by freeing the original vector. Of
course, the original vector should not be deallocated while the view is
still in use.
</p>
<p>The function <code>gsl_vector_const_subvector</code> is equivalent to
<code>gsl_vector_subvector</code> but can be used for vectors which are
declared <code>const</code>.
</p></dd></dl>
<dl>
<dt><a name="index-gsl_005fvector_005fsubvector_005fwith_005fstride"></a>Function: <em>gsl_vector_view</em> <strong>gsl_vector_subvector_with_stride</strong> <em>(gsl_vector * <var>v</var>, size_t <var>offset</var>, size_t <var>stride</var>, size_t <var>n</var>)</em></dt>
<dt><a name="index-gsl_005fvector_005fconst_005fsubvector_005fwith_005fstride"></a>Function: <em>gsl_vector_const_view</em> <strong>gsl_vector_const_subvector_with_stride</strong> <em>(const gsl_vector * <var>v</var>, size_t <var>offset</var>, size_t <var>stride</var>, size_t <var>n</var>)</em></dt>
<dd><p>These functions return a vector view of a subvector of another vector
<var>v</var> with an additional stride argument. The subvector is formed in
the same way as for <code>gsl_vector_subvector</code> but the new vector has
<var>n</var> elements with a step-size of <var>stride</var> from one element to
the next in the original vector. Mathematically, the <var>i</var>-th element
of the new vector <var>v’</var> is given by,
</p>
<div class="example">
<pre class="example">v'(i) = v->data[(offset + i*stride)*v->stride]
</pre></div>
<p>where the index <var>i</var> runs from 0 to <code>n-1</code>.
</p>
<p>Note that subvector views give direct access to the underlying elements
of the original vector. For example, the following code will zero the
even elements of the vector <code>v</code> of length <code>n</code>, while leaving the
odd elements untouched,
</p>
<div class="example">
<pre class="example">gsl_vector_view v_even
= gsl_vector_subvector_with_stride (v, 0, 2, n/2);
gsl_vector_set_zero (&v_even.vector);
</pre></div>
<p>A vector view can be passed to any subroutine which takes a vector
argument just as a directly allocated vector would be, using
<code>&</code><var>view</var><code>.vector</code>. For example, the following code
computes the norm of the odd elements of <code>v</code> using the <small>BLAS</small>
routine <small>DNRM2</small>,
</p>
<div class="example">
<pre class="example">gsl_vector_view v_odd
= gsl_vector_subvector_with_stride (v, 1, 2, n/2);
double r = gsl_blas_dnrm2 (&v_odd.vector);
</pre></div>
<p>The function <code>gsl_vector_const_subvector_with_stride</code> is equivalent
to <code>gsl_vector_subvector_with_stride</code> but can be used for vectors
which are declared <code>const</code>.
</p></dd></dl>
<dl>
<dt><a name="index-gsl_005fvector_005fcomplex_005freal"></a>Function: <em>gsl_vector_view</em> <strong>gsl_vector_complex_real</strong> <em>(gsl_vector_complex * <var>v</var>)</em></dt>
<dt><a name="index-gsl_005fvector_005fcomplex_005fconst_005freal"></a>Function: <em>gsl_vector_const_view</em> <strong>gsl_vector_complex_const_real</strong> <em>(const gsl_vector_complex * <var>v</var>)</em></dt>
<dd><p>These functions return a vector view of the real parts of the complex
vector <var>v</var>.
</p>
<p>The function <code>gsl_vector_complex_const_real</code> is equivalent to
<code>gsl_vector_complex_real</code> but can be used for vectors which are
declared <code>const</code>.
</p></dd></dl>
<dl>
<dt><a name="index-gsl_005fvector_005fcomplex_005fimag"></a>Function: <em>gsl_vector_view</em> <strong>gsl_vector_complex_imag</strong> <em>(gsl_vector_complex * <var>v</var>)</em></dt>
<dt><a name="index-gsl_005fvector_005fcomplex_005fconst_005fimag"></a>Function: <em>gsl_vector_const_view</em> <strong>gsl_vector_complex_const_imag</strong> <em>(const gsl_vector_complex * <var>v</var>)</em></dt>
<dd><p>These functions return a vector view of the imaginary parts of the
complex vector <var>v</var>.
</p>
<p>The function <code>gsl_vector_complex_const_imag</code> is equivalent to
<code>gsl_vector_complex_imag</code> but can be used for vectors which are
declared <code>const</code>.
</p></dd></dl>
<dl>
<dt><a name="index-gsl_005fvector_005fview_005farray"></a>Function: <em>gsl_vector_view</em> <strong>gsl_vector_view_array</strong> <em>(double * <var>base</var>, size_t <var>n</var>) </em></dt>
<dt><a name="index-gsl_005fvector_005fconst_005fview_005farray"></a>Function: <em>gsl_vector_const_view</em> <strong>gsl_vector_const_view_array</strong> <em>(const double * <var>base</var>, size_t <var>n</var>)</em></dt>
<dd><p>These functions return a vector view of an array. The start of the new
vector is given by <var>base</var> and has <var>n</var> elements. Mathematically,
the <var>i</var>-th element of the new vector <var>v’</var> is given by,
</p>
<div class="example">
<pre class="example">v'(i) = base[i]
</pre></div>
<p>where the index <var>i</var> runs from 0 to <code>n-1</code>.
</p>
<p>The array containing the elements of <var>v</var> is not owned by the new
vector view. When the view goes out of scope the original array will
continue to exist. The original memory can only be deallocated by
freeing the original pointer <var>base</var>. Of course, the original array
should not be deallocated while the view is still in use.
</p>
<p>The function <code>gsl_vector_const_view_array</code> is equivalent to
<code>gsl_vector_view_array</code> but can be used for arrays which are
declared <code>const</code>.
</p></dd></dl>
<dl>
<dt><a name="index-gsl_005fvector_005fview_005farray_005fwith_005fstride"></a>Function: <em>gsl_vector_view</em> <strong>gsl_vector_view_array_with_stride</strong> <em>(double * <var>base</var>, size_t <var>stride</var>, size_t <var>n</var>)</em></dt>
<dt><a name="index-gsl_005fvector_005fconst_005fview_005farray_005fwith_005fstride"></a>Function: <em>gsl_vector_const_view</em> <strong>gsl_vector_const_view_array_with_stride</strong> <em>(const double * <var>base</var>, size_t <var>stride</var>, size_t <var>n</var>)</em></dt>
<dd><p>These functions return a vector view of an array <var>base</var> with an
additional stride argument. The subvector is formed in the same way as
for <code>gsl_vector_view_array</code> but the new vector has <var>n</var> elements
with a step-size of <var>stride</var> from one element to the next in the
original array. Mathematically, the <var>i</var>-th element of the new
vector <var>v’</var> is given by,
</p>
<div class="example">
<pre class="example">v'(i) = base[i*stride]
</pre></div>
<p>where the index <var>i</var> runs from 0 to <code>n-1</code>.
</p>
<p>Note that the view gives direct access to the underlying elements of the
original array. A vector view can be passed to any subroutine which
takes a vector argument just as a directly allocated vector would be,
using <code>&</code><var>view</var><code>.vector</code>.
</p>
<p>The function <code>gsl_vector_const_view_array_with_stride</code> is
equivalent to <code>gsl_vector_view_array_with_stride</code> but can be used
for arrays which are declared <code>const</code>.
</p></dd></dl>
<hr>
<div class="header">
<p>
Next: <a href="Copying-vectors.html#Copying-vectors" accesskey="n" rel="next">Copying vectors</a>, Previous: <a href="Reading-and-writing-vectors.html#Reading-and-writing-vectors" accesskey="p" rel="previous">Reading and writing vectors</a>, Up: <a href="Vectors.html#Vectors" accesskey="u" rel="up">Vectors</a> [<a href="Function-Index.html#Function-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|