STIR 6.4.0
NumericVectorWithOffset.inl
Go to the documentation of this file.
1//
2//
3/*
4 Copyright (C) 2000 PARAPET partners
5 Copyright (C) 2000 - 2005-06-03, Hammersmith Imanet Ltd
6 Copyright (C) 2011-07-01 - 2012, Kris Thielemans
7 Copyright (C) 2013, 2020, 2023, 2025 University College London
8 This file is part of STIR.
9
10 SPDX-License-Identifier: Apache-2.0 AND License-ref-PARAPET-license
11
12 See STIR/LICENSE.txt for details
13*/
23
24// include for min,max definitions
25#include <algorithm>
26#include "stir/error.h"
27#include "stir/assign.h"
28
29START_NAMESPACE_STIR
30
31template <class T, class NUMBER>
35
36template <class T, class NUMBER>
39{
40 swap(*this, other);
41}
42
43// assignment
44template <class T, class NUMBER>
45NumericVectorWithOffset<T, NUMBER>&
51
52// addition
53template <class T, class NUMBER>
56{
57 this->check_state();
58 NumericVectorWithOffset retval(*this);
59 return retval += v;
60}
61
62// subtraction
63template <class T, class NUMBER>
66{
67 this->check_state();
68 NumericVectorWithOffset retval(*this);
69 return retval -= v;
70}
71
72// elem by elem multiplication
73template <class T, class NUMBER>
76{
77 this->check_state();
79 return retval *= v;
80}
81
82// elem by elem division
83template <class T, class NUMBER>
87 this->check_state();
88 NumericVectorWithOffset retval(*this);
89 return retval /= v;
90}
91
92// Add a constant to every element
93template <class T, class NUMBER>
96{
97 this->check_state();
98 NumericVectorWithOffset retval(*this);
99 return retval += v;
100}
101
102// Subtract a constant from every element
103template <class T, class NUMBER>
106{
107 this->check_state();
108 NumericVectorWithOffset retval(*this);
109 return retval -= v;
110}
111
112// Multiply every element by a constant
113template <class T, class NUMBER>
116{
117 this->check_state();
119 return retval *= v;
120}
122// Divide every element by a constant
123template <class T, class NUMBER>
126{
127 this->check_state();
128 NumericVectorWithOffset retval(*this);
129 return retval /= v;
130}
131
135template <class T, class NUMBER>
138{
139 this->check_state();
140 // first check if *this is empty
141 if (this->get_length() == 0)
142 {
143 return *this = v;
145 this->grow(std::min(this->get_min_index(), v.get_min_index()), std::max(this->get_max_index(), v.get_max_index()));
146 for (int i = v.get_min_index(); i <= v.get_max_index(); i++)
147 this->num[i] += v.num[i];
148 this->check_state();
149 return *this;
150}
153template <class T, class NUMBER>
156{
157 this->check_state();
158 // first check if *this is empty
159 if (this->get_length() == 0)
160 {
161 if (v.get_length() == 0)
162 return *this;
163 // else
164 // Note: I cannot implement things like v.num[i] * (-1), as NUMBER can be a
165 // BasicCoordinate, an unsigned int or whatever
166 error("NumericVectorWithOffset-= called with rhs that is larger in size than lhs. This is no longer supported.");
167 }
168 this->grow(std::min(this->get_min_index(), v.get_min_index()), std::max(this->get_max_index(), v.get_max_index()));
169 for (int i = v.get_min_index(); i <= v.get_max_index(); i++)
170 this->num[i] -= v.num[i];
171 this->check_state();
172 return *this;
173}
174
176template <class T, class NUMBER>
179{
180 this->check_state();
181 // first check if *this is empty
182 if (this->get_length() == 0)
183 {
184 if (v.get_length() == 0)
185 return *this;
186 // else
187 // we have to return an object of the same dimensions as v, but filled with 0.
188 *this = v;
189 assign(*this, 0);
190 return *this;
191 }
192 this->grow(std::min(this->get_min_index(), v.get_min_index()), std::max(this->get_max_index(), v.get_max_index()));
193 for (int i = v.get_min_index(); i <= v.get_max_index(); i++)
194 this->num[i] *= v.num[i];
195 this->check_state();
196 return *this;
197}
198
200template <class T, class NUMBER>
203{
204 this->check_state();
205 // first check if *this is empty
206 if (this->get_length() == 0)
207 {
208 if (v.get_length() == 0)
209 return *this;
210 // else
211 // we have to return an object of the same dimensions as v, but filled with 0.
212 *this = v;
213 assign(*this, 0);
214 return *this;
215 }
216 this->grow(std::min(this->get_min_index(), v.get_min_index()), std::max(this->get_max_index(), v.get_max_index()));
217 for (int i = v.get_min_index(); i <= v.get_max_index(); i++)
218 this->num[i] /= v.num[i];
219 this->check_state();
220 return *this;
221}
222
223template <class T, class NUMBER>
226{
227 this->check_state();
228 for (int i = this->get_min_index(); i <= this->get_max_index(); i++)
229 this->num[i] += v;
230 this->check_state();
231 return *this;
232}
233
234template <class T, class NUMBER>
235inline NumericVectorWithOffset<T, NUMBER>&
237{
238 this->check_state();
239 for (int i = this->get_min_index(); i <= this->get_max_index(); i++)
240 this->num[i] -= v;
241 this->check_state();
242 return *this;
243}
244
245template <class T, class NUMBER>
248{
249 this->check_state();
250 for (int i = this->get_min_index(); i <= this->get_max_index(); i++)
251 this->num[i] *= v;
252 this->check_state();
253 return *this;
254}
255
256template <class T, class NUMBER>
259{
260 this->check_state();
261 for (int i = this->get_min_index(); i <= this->get_max_index(); i++)
262 this->num[i] /= v;
263 this->check_state();
264 return *this;
265}
266
267template <class T, class NUMBER>
268template <class NUMBER2>
269inline void
272 const NUMBER2 b,
274{
276}
277
278template <class T, class NUMBER>
279inline void
281 const NUMBER a,
283 const NUMBER b)
284{
285 this->check_state();
286 if ((this->get_min_index() != x.get_min_index()) || (this->get_min_index() != y.get_min_index())
287 || (this->get_max_index() != x.get_max_index()) || (this->get_max_index() != y.get_max_index()))
288 error("NumericVectorWithOffset::xapyb: index ranges don't match");
289
290 typename NumericVectorWithOffset::iterator this_iter = this->begin();
291 typename NumericVectorWithOffset::const_iterator x_iter = x.begin();
292 typename NumericVectorWithOffset::const_iterator y_iter = y.begin();
293 while (this_iter != this->end())
294 {
295 *this_iter++ = (*x_iter++) * a + (*y_iter++) * b;
296 }
297}
298
299template <class T, class NUMBER>
300inline void
305{
306 this->check_state();
307 if ((this->get_min_index() != x.get_min_index()) || (this->get_min_index() != y.get_min_index())
308 || (this->get_min_index() != a.get_min_index()) || (this->get_min_index() != b.get_min_index())
309 || (this->get_max_index() != x.get_max_index()) || (this->get_max_index() != y.get_max_index())
310 || (this->get_max_index() != a.get_max_index()) || (this->get_max_index() != b.get_max_index()))
311 error("NumericVectorWithOffset::xapyb: index ranges don't match");
312
313 typename NumericVectorWithOffset::iterator this_iter = this->begin();
314 typename NumericVectorWithOffset::const_iterator x_iter = x.begin();
315 typename NumericVectorWithOffset::const_iterator y_iter = y.begin();
316 typename NumericVectorWithOffset::const_iterator a_iter = a.begin();
317 typename NumericVectorWithOffset::const_iterator b_iter = b.begin();
318
319 while (this_iter != this->end())
320 {
321 *this_iter++ = (*x_iter++) * (*a_iter++) + (*y_iter++) * (*b_iter++);
322 }
323}
324
325template <class T, class NUMBER>
326template <class T2>
327inline void
329{
330 this->xapyb(*this, a, y, b);
331}
332
333END_NAMESPACE_STIR
defines the stir::assign function to assign values to different data types
like VectorWithOffset, but with changes in various numeric operators
Definition NumericVectorWithOffset.h:48
NumericVectorWithOffset & operator=(const NumericVectorWithOffset &other)
assignment
Definition NumericVectorWithOffset.inl:46
NumericVectorWithOffset operator+(const NumericVectorWithOffset &v) const
adding vectors, element by element
Definition NumericVectorWithOffset.inl:55
NumericVectorWithOffset operator/(const NumericVectorWithOffset &v) const
dividing vectors, element by element
Definition NumericVectorWithOffset.inl:85
void xapyb(const NumericVectorWithOffset &x, const elemT a, const NumericVectorWithOffset &y, const elemT b)
set values of the array to x*a+y*b, where a and b are scalar
NumericVectorWithOffset operator*(const NumericVectorWithOffset &v) const
multiplying vectors, element by element
Definition NumericVectorWithOffset.inl:75
NumericVectorWithOffset & operator+=(const NumericVectorWithOffset &v)
adding elements of v to the current vector
Definition NumericVectorWithOffset.inl:137
NumericVectorWithOffset operator-(const NumericVectorWithOffset &v) const
subtracting vectors, element by element
Definition NumericVectorWithOffset.inl:65
NumericVectorWithOffset & operator/=(const NumericVectorWithOffset &v)
dividing all elements of the current vector by elements of v
Definition NumericVectorWithOffset.inl:202
void sapyb(const T2 &a, const NumericVectorWithOffset &y, const T2 &b)
set the values of the array to self*a+y*b, where a and b are scalar or vectors
Definition NumericVectorWithOffset.inl:328
NumericVectorWithOffset(const VectorWithOffset< T > &t)
Constructor from an object of this class' base_type.
Definition NumericVectorWithOffset.inl:32
NumericVectorWithOffset & operator-=(const NumericVectorWithOffset &v)
subtracting elements of v from the current vector
Definition NumericVectorWithOffset.inl:155
STIR_DEPRECATED void axpby(const elemT2 a, const NumericVectorWithOffset &x, const elemT2 b, const NumericVectorWithOffset &y)
NumericVectorWithOffset & operator*=(const NumericVectorWithOffset &v)
multiplying elements of the current vector with elements of v
Definition NumericVectorWithOffset.inl:178
iterator begin()
use to initialise an iterator to the first element of the vector
Definition VectorWithOffset.inl:190
VectorWithOffset & operator=(const VectorWithOffset &il)
assignment operator with another vector
Definition VectorWithOffset.inl:505
void check_state() const
Called internally to see if all variables are consistent.
Definition VectorWithOffset.inl:87
int get_max_index() const
get value of last valid index
Definition VectorWithOffset.inl:131
int get_min_index() const
get value of first valid index
Definition VectorWithOffset.inl:124
virtual void grow(const int min_index, const int max_index)
grow the range of the vector, new elements are set to T()
Definition VectorWithOffset.inl:491
int get_length() const
return number of elements in this vector
Definition VectorWithOffset.inl:538
iterator end()
iterator 'past' the last element of the vector
Definition VectorWithOffset.inl:206
VectorWithOffset()
Default constructor: creates a vector of length 0.
Definition VectorWithOffset.inl:253
T * num
pointer to (*this)[0] (taking get_min_index() into account that is).
Definition VectorWithOffset.h:370
Declaration of stir::error()
void error(const char *const s,...)
Print error with format string a la printf and throw exception.
Definition error.cxx:42