Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tvector.cpp
Go to the documentation of this file.
1 /*
2  * tvector.cpp - simple vector template class implementation
3  *
4  * Copyright (C) 2004, 2005, 2006, 2008 Stefan Jahn <stefan@lkcc.org>
5  *
6  * This is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * This software is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; see the file COPYING. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  * $Id$
22  *
23  */
24 
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <cmath>
30 
31 #include "compat.h"
32 #include "complex.h"
33 #include "tvector.h"
34 #include "precision.h"
35 
36 namespace qucs {
37 
38 // Constructor creates an unnamed instance of the tvector class.
39 template <class nr_type_t>
41  external = 0;
42  capacity = size = 0;
43  data = NULL;
44 }
45 
46 /* Constructor creates an unnamed instance of the tvector class with a
47  certain length. */
48 template <class nr_type_t>
50  external = 0;
51  capacity = size = s;
52  if (s > 0) {
53  data = new nr_type_t[s];
54  memset (data, 0, sizeof (nr_type_t) * s);
55  }
56  else data = NULL;
57 }
58 
59 /* The copy constructor creates a new instance based on the given
60  tvector object. */
61 template <class nr_type_t>
63  external = 0;
64  size = v.size;
65  capacity = v.capacity;
66  data = NULL;
67 
68  // copy tvector elements
69  if (size > 0) {
70  data = new nr_type_t[size];
71  memcpy (data, v.data, sizeof (nr_type_t) * size);
72  }
73 }
74 
75 /* The assignment copy constructor creates a new instance based on the
76  given tvector object. */
77 template <class nr_type_t>
78 const tvector<nr_type_t>&
80  if (&v != this) {
81  size = v.size;
82  capacity = v.capacity;
83  if (data && !external) { delete[] data; data = NULL; }
84  external = 0;
85  if (size > 0) {
86  data = new nr_type_t[size];
87  memcpy (data, v.data, sizeof (nr_type_t) * size);
88  }
89  }
90  return *this;
91 }
92 
93 // Destructor deletes a tvector object.
94 template <class nr_type_t>
96  if (data && !external) delete[] data;
97 }
98 
99 // Returns the tvector element at the given position.
100 template <class nr_type_t>
101 nr_type_t tvector<nr_type_t>::get (int i) {
102  assert (i >= 0 && i < size);
103  return data[i];
104 }
105 
106 // Sets the tvector element at the given position.
107 template <class nr_type_t>
108 void tvector<nr_type_t>::set (int i, nr_type_t z) {
109  assert (i >= 0 && i < size);
110  data[i] = z;
111 }
112 
113 // Sets all the tvector elements to the given value.
114 template <class nr_type_t>
115 void tvector<nr_type_t>::set (nr_type_t z) {
116  for (int i = 0; i < size; i++) data[i] = z;
117 }
118 
119 // Sets the specified tvector elements to the given value.
120 template <class nr_type_t>
121 void tvector<nr_type_t>::set (nr_type_t z, int start, int stop) {
122  for (int i = start; i < stop; i++) data[i] = z;
123 }
124 
125 // Appends the given value to the tvector.
126 template <class nr_type_t>
127 void tvector<nr_type_t>::add (nr_type_t z) {
128  if (size >= capacity) {
129  if (data) {
130  // double the vectors capacity
131  capacity *= 2;
132  data = (nr_type_t *) realloc (data, capacity * sizeof (nr_type_t));
133  }
134  else {
135  // initial capacity
136  capacity = 4;
137  data = (nr_type_t *) malloc (capacity * sizeof (nr_type_t));
138  }
139  }
140  data[size++] = z;
141 }
142 
143 // Rejects the given number of values from the start of the tvector.
144 template <class nr_type_t>
146  if (n < size) {
147  for (int i = 0; i < size - n; i++) data[i] = data[i + n];
148  size -= n;
149  }
150  else size = 0;
151 }
152 
153 // Rejects the given number of values from the end of the tvector.
154 template <class nr_type_t>
156  if (n < size) {
157  size -= n;
158  }
159  else size = 0;
160 }
161 
162 // Sets size to zero. Does not reduce the capacity.
163 template <class nr_type_t>
165  size = 0;
166 }
167 
168 /* The function returns the number of entries with the given value
169  deviating no more than the given epsilon. */
170 template <class nr_type_t>
171 int tvector<nr_type_t>::contains (nr_type_t val, nr_double_t eps) {
172  int count = 0;
173  for (int i = 0; i < size; i++) if (abs (data[i] - val) <= eps) count++;
174  return count;
175 }
176 
177 // Copies the specified elements from the given tvector.
178 template <class nr_type_t>
180  for (int i = start; i < stop; i++) data[i] = a.get (i);
181 }
182 
183 // Applies external data vector to the vector.
184 template <class nr_type_t>
185 void tvector<nr_type_t>::setData (nr_type_t * d, int len) {
186  if (data && !external) delete[] data;
187  external = 1;
188  data = d;
189  capacity = size = len;
190 }
191 
192 // The function swaps the given rows with each other.
193 template <class nr_type_t>
194 void tvector<nr_type_t>::exchangeRows (int r1, int r2) {
195  assert (r1 >= 0 && r2 >= 0 && r1 < size && r2 < size);
196  nr_type_t s = data[r1];
197  data[r1] = data[r2];
198  data[r2] = s;
199 }
200 
201 // Addition.
202 template <class nr_type_t>
203 tvector<nr_type_t> operator + (tvector<nr_type_t> a, tvector<nr_type_t> b) {
204  assert (a.getSize () == b.getSize ());
205  int n = a.getSize ();
206  tvector<nr_type_t> res (n);
207  for (int i = 0; i < n; i++) res.set (i, a.get (i) + b.get (i));
208  return res;
209 }
210 
211 // Intrinsic vector addition.
212 template <class nr_type_t>
214  assert (a.getSize () == size);
215  nr_type_t * src = a.getData ();
216  nr_type_t * dst = data;
217  for (int i = 0; i < size; i++) *dst++ += *src++;
218  return *this;
219 }
220 
221 // Subtraction.
222 template <class nr_type_t>
223 tvector<nr_type_t> operator - (tvector<nr_type_t> a, tvector<nr_type_t> b) {
224  assert (a.getSize () == b.getSize ());
225  int n = a.getSize ();
226  tvector<nr_type_t> res (n);
227  for (int i = 0; i < n; i++) res.set (i, a.get (i) - b.get (i));
228  return res;
229 }
230 
231 // Intrinsic vector substration.
232 template <class nr_type_t>
234  assert (a.getSize () == size);
235  nr_type_t * src = a.getData ();
236  nr_type_t * dst = data;
237  for (int i = 0; i < size; i++) *dst++ -= *src++;
238  return *this;
239 }
240 
241 // Intrinsic scalar multiplication.
242 template <class nr_type_t>
244  nr_type_t * dst = data;
245  for (int i = 0; i < size; i++) *dst++ *= s;
246  return *this;
247 }
248 
249 // Intrinsic scalar division.
250 template <class nr_type_t>
252  nr_type_t * dst = data;
253  for (int i = 0; i < size; i++) *dst++ /= s;
254  return *this;
255 }
256 
257 // Scalar multiplication.
258 template <class nr_type_t>
259 tvector<nr_type_t> operator * (nr_double_t s, tvector<nr_type_t> a) {
260  int n = a.getSize ();
261  tvector<nr_type_t> res (n);
262  for (int i = 0; i < n; i++) res.set (i, s * a.get (i));
263  return res;
264 }
265 
266 template <class nr_type_t>
267 tvector<nr_type_t> operator * (tvector<nr_type_t> a, nr_double_t s) {
268  return s * a;
269 }
270 
271 // Vector multiplication (element by element).
272 template <class nr_type_t>
273 tvector<nr_type_t> operator * (tvector<nr_type_t> a, tvector<nr_type_t> b) {
274  assert (a.getSize () == b.getSize ());
275  int n = a.getSize ();
276  tvector<nr_type_t> res (n);
277  for (int i = 0; i < n; i++) res.set (i, a.get (i) * b.get (i));
278  return res;
279 }
280 
281 // Computes the scalar product of two vectors.
282 template <class nr_type_t>
283 nr_type_t scalar (tvector<nr_type_t> a, tvector<nr_type_t> b) {
284  assert (a.getSize () == b.getSize ());
285  nr_type_t n = 0;
286  for (int i = 0; i < a.getSize (); i++) n += a.get (i) * b.get (i);
287  return n;
288 }
289 
290 // Constant assignment operation.
291 template <class nr_type_t>
293  for (int i = 0; i < size; i++) data[i] = val;
294  return *this;
295 }
296 
297 // Returns the sum of the vector elements.
298 template <class nr_type_t>
299 nr_type_t sum (tvector<nr_type_t> a) {
300  nr_type_t res = 0;
301  for (int i = 0; i < a.getSize (); i++) res += a.get (i);
302  return res;
303 }
304 
305 // Vector negation.
306 template <class nr_type_t>
307 tvector<nr_type_t> operator - (tvector<nr_type_t> a) {
308  int n = a.getSize ();
309  tvector<nr_type_t> res (n);
310  for (int i = 0; i < n; i++) res.set (i, -a.get (i));
311  return res;
312 }
313 
314 // Vector less comparison.
315 template <class nr_type_t>
316 bool operator < (tvector<nr_type_t> a, tvector<nr_type_t> b) {
317  assert (a.getSize () == b.getSize ());
318  int n = a.getSize ();
319  for (int i = 0; i < n; i++) if (a.get (i) >= b.get (i)) return false;
320  return true;
321 }
322 
323 // Vector greater comparison.
324 template <class nr_type_t>
325 bool operator > (tvector<nr_type_t> a, tvector<nr_type_t> b) {
326  assert (a.getSize () == b.getSize ());
327  int n = a.getSize ();
328  for (int i = 0; i < n; i++) if (a.get (i) <= b.get (i)) return false;
329  return true;
330 }
331 
332 // Scalar addition.
333 template <class nr_type_t>
334 tvector<nr_type_t> operator + (nr_type_t s, tvector<nr_type_t> a) {
335  int n = a.getSize ();
336  tvector<nr_type_t> res (n);
337  for (int i = 0; i < n; i++) res.set (i, s + a.get (i));
338  return res;
339 }
340 
341 template <class nr_type_t>
342 tvector<nr_type_t> operator + (tvector<nr_type_t> a, nr_type_t s) {
343  return s + a;
344 }
345 
346 // Mean square norm.
347 template <class nr_type_t>
348 nr_double_t norm (tvector<nr_type_t> a) {
349 #if 0
350  nr_double_t k = 0;
351  for (int i = 0; i < a.getSize (); i++) k += norm (a.get (i));
352  return n;
353 #else
354  nr_double_t scale = 0, n = 1, x, ax;
355  for (int i = 0; i < a.getSize (); i++) {
356  if ((x = real (a (i))) != 0) {
357  ax = fabs (x);
358  if (scale < ax) {
359  x = scale / ax;
360  n = 1 + n * x * x;
361  scale = ax;
362  }
363  else {
364  x = ax / scale;
365  n += x * x;
366  }
367  }
368  if ((x = imag (a (i))) != 0) {
369  ax = fabs (x);
370  if (scale < ax) {
371  x = scale / ax;
372  n = 1 + n * x * x;
373  scale = ax;
374  }
375  else {
376  x = ax / scale;
377  n += x * x;
378  }
379  }
380  }
381  return scale * scale * n;
382 #endif
383 }
384 
385 // Maximum norm.
386 template <class nr_type_t>
387 nr_double_t maxnorm (tvector<nr_type_t> a) {
388  nr_double_t nMax = 0, n;
389  for (int i = 0; i < a.getSize (); i++) {
390  n = norm (a.get (i));
391  if (n > nMax) nMax = n;
392  }
393  return nMax;
394 }
395 
396 // Conjugate vector.
397 template <class nr_type_t>
398 tvector<nr_type_t> conj (tvector<nr_type_t> a) {
399  int n = a.getSize ();
400  tvector<nr_type_t> res (n);
401  for (int i = 0; i < n; i++) res.set (i, conj (a.get (i)));
402  return res;
403 }
404 
405 // Checks validity of vector.
406 template <class nr_type_t>
408  for (int i = 0; i < size; i++)
409  if (!std::isfinite (real (data[i]))) return 0;
410  return 1;
411 }
412 
413 // The functions reorders the vector according to the given index array.
414 template <class nr_type_t>
415 void tvector<nr_type_t>::reorder (int * idx) {
416  tvector<nr_type_t> old = *this;
417  for (int i = 0; i < size; i++) data[i] = old.get (idx[i]);
418 }
419 
420 #ifdef DEBUG
421 // Debug function: Prints the vector object.
422 template <class nr_type_t>
423 void tvector<nr_type_t>::print (void) {
424  for (int r = 0; r < size; r++) {
425  fprintf (stderr, "%+.2e%+.2ei\n", (double) real (get (r)),
426  (double) imag (get (r)));
427  }
428 }
429 #endif /* DEBUG */
430 
431 } // namespace qucs
start
Definition: parse_zvr.y:126
void truncate(int)
Definition: tvector.cpp:155
int isFinite(void)
Definition: tvector.cpp:407
void setData(nr_type_t *, int)
Definition: tvector.cpp:185
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
matrix abs(matrix a)
Computes magnitude of each matrix element.
Definition: matrix.cpp:531
tvector operator-=(tvector)
Definition: tvector.cpp:233
void set(int, nr_type_t)
Definition: tvector.cpp:108
nr_type_t scalar(tvector< nr_type_t > a, tvector< nr_type_t > b)
Definition: tvector.cpp:283
size
Definition: parse_vcd.y:203
matrix operator*(matrix a, nr_complex_t z)
Matrix scaling complex version.
Definition: matrix.cpp:298
nr_complex_t sum(vector)
Definition: vector.cpp:251
n
Definition: parse_citi.y:147
tvector operator*=(nr_double_t)
Definition: tvector.cpp:243
r
Definition: parse_mdl.y:515
void exchangeRows(int, int)
Definition: tvector.cpp:194
bool operator>(const nr_complex_t z1, const nr_complex_t z2)
Superior.
Definition: complex.cpp:889
nr_double_t maxnorm(tvector< nr_type_t > a)
Definition: tvector.cpp:387
i
Definition: parse_mdl.y:516
matrix imag(matrix a)
Imaginary part matrix.
Definition: matrix.cpp:581
matrix operator-(matrix a, matrix b)
Matrix subtraction.
Definition: matrix.cpp:259
void add(nr_type_t)
Definition: tvector.cpp:127
stop
Definition: parse_zvr.y:127
tvector operator+=(tvector)
Definition: tvector.cpp:213
void drop(int)
Definition: tvector.cpp:145
nr_type_t get(int)
Definition: tvector.cpp:101
const tvector & operator=(const tvector &)
Definition: tvector.cpp:79
x
Definition: parse_mdl.y:498
Declaration sizeof(struct vcd_scope))
matrix operator+(matrix a, matrix b)
Matrix addition.
Definition: matrix.cpp:228
nr_type_t * data
Definition: tvector.h:141
tvector operator/=(nr_double_t)
Definition: tvector.cpp:251
v
Definition: parse_zvr.y:141
nr_double_t norm(const nr_complex_t z)
Compute euclidian norm of complex number.
Definition: complex.cpp:283
void print(void)
matrix conj(matrix a)
Conjugate complex matrix.
Definition: matrix.cpp:505
void clear(void)
Definition: tvector.cpp:164
vcd scale
Definition: parse_vcd.y:180
int getSize(void)
Definition: tvector.h:82
void reorder(int *)
Definition: tvector.cpp:415
int capacity
Definition: tvector.h:140
int contains(nr_type_t, nr_double_t eps=NR_EPSI)
Definition: tvector.cpp:171
nr_type_t * getData(void)
Definition: tvector.h:83
data
Definition: parse_citi.y:117