My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
vector.cpp
Go to the documentation of this file.
1 /*
2  * vector.cpp - vector class implementation
3  *
4  * Copyright (C) 2003, 2004, 2005, 2006, 2007 Stefan Jahn <stefan@lkcc.org>
5  * Copyright (C) 2006, 2007 Gunther Kraut <gn.kraut@t-online.de>
6  *
7  * This is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this package; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  * $Id: vector.cpp 1825 2011-03-11 20:42:14Z ela $
23  *
24  */
25 
26 #if HAVE_CONFIG_H
27 # include <config.h>
28 #endif
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34 #include <float.h>
35 #include <assert.h>
36 
37 #include "complex.h"
38 #include "object.h"
39 #include "logging.h"
40 #include "strlist.h"
41 #include "vector.h"
42 #include "consts.h"
43 
44 // Constructor creates an unnamed instance of the vector class.
46  capacity = size = 0;
47  data = NULL;
48  dependencies = NULL;
49  origin = NULL;
50  requested = 0;
51 }
52 
53 /* Constructor creates an unnamed instance of the vector class with a
54  given initial size. */
55 vector::vector (int s) : object () {
56  assert (s >= 0);
57  capacity = size = s;
58  data = s > 0 ? (nr_complex_t *)
59  calloc (capacity, sizeof (nr_complex_t)) : NULL;
60  dependencies = NULL;
61  origin = NULL;
62  requested = 0;
63 }
64 
65 /* Constructor creates an unnamed instance of the vector class with a
66  given initial size and content. */
68  assert (s >= 0);
69  capacity = size = s;
70  data = s > 0 ? (nr_complex_t *)
71  calloc (capacity, sizeof (nr_complex_t)) : NULL;
72  for (int i = 0; i < s; i++) data[i] = val;
73  dependencies = NULL;
74  origin = NULL;
75  requested = 0;
76 }
77 
78 // Constructor creates an named instance of the vector class.
79 vector::vector (const char * n) : object (n) {
80  capacity = size = 0;
81  data = NULL;
82  dependencies = NULL;
83  origin = NULL;
84  requested = 0;
85 }
86 
87 /* This constructor creates a named instance of the vector class with
88  a given initial size. */
89 vector::vector (const char * n, int s) : object (n) {
90  assert (s >= 0);
91  capacity = size = s;
92  data = s > 0 ? (nr_complex_t *)
93  calloc (capacity, sizeof (nr_complex_t)) : NULL;
94  dependencies = NULL;
95  origin = NULL;
96  requested = 0;
97 }
98 
99 /* The copy constructor creates a new instance based on the given
100  vector object. */
101 vector::vector (const vector & v) : object (v) {
102  size = v.size;
103  capacity = v.capacity;
104  data = (nr_complex_t *) malloc (sizeof (nr_complex_t) * capacity);
105  memcpy (data, v.data, sizeof (nr_complex_t) * size);
106  dependencies = v.dependencies ? new strlist (*v.dependencies) : NULL;
107  origin = v.origin ? strdup (v.origin) : NULL;
108  requested = v.requested;
109 }
110 
111 /* The assignment copy constructor creates a new instance based on the
112  given vector object. It copies the data only and leaves any other
113  properties untouched. */
114 const vector& vector::operator=(const vector & v) {
115  if (&v != this) {
116  size = v.size;
117  capacity = v.capacity;
118  if (data) { free (data); data = NULL; }
119  if (capacity > 0) {
120  data = (nr_complex_t *) malloc (sizeof (nr_complex_t) * capacity);
121  if (size > 0) memcpy (data, v.data, sizeof (nr_complex_t) * size);
122  }
123  }
124  return *this;
125 }
126 
127 // Destructor deletes a vector object.
129  if (data) free (data);
130  if (dependencies) delete dependencies;
131  if (origin) free (origin);
132 }
133 
134 // Returns data dependencies.
136  return dependencies;
137 }
138 
139 // Sets the data dependencies.
141  if (dependencies) delete dependencies;
142  dependencies = s;
143 }
144 
145 /* The function appends a new complex data item to the end of the
146  vector and ensures that the vector can hold the increasing number
147  of data items. */
149  if (data == NULL) {
150  size = 0; capacity = 64;
151  data = (nr_complex_t *) malloc (sizeof (nr_complex_t) * capacity);
152  }
153  else if (size >= capacity) {
154  capacity *= 2;
155  data = (nr_complex_t *) realloc (data, sizeof (nr_complex_t) * capacity);
156  }
157  data[size++] = c;
158 }
159 
160 /* This function appends the given vector to the vector. */
161 void vector::add (vector * v) {
162  if (v != NULL) {
163  if (data == NULL) {
164  size = 0; capacity = v->getSize ();
165  data = (nr_complex_t *) malloc (sizeof (nr_complex_t) * capacity);
166  }
167  else if (size + v->getSize () > capacity) {
168  capacity += v->getSize ();
169  data = (nr_complex_t *) realloc (data, sizeof (nr_complex_t) * capacity);
170  }
171  for (int i = 0; i < v->getSize (); i++) data[size++] = v->get (i);
172  }
173 }
174 
175 // Returns the complex data item at the given position.
177  return data[i];
178 }
179 
180 void vector::set (nr_double_t d, int i) {
181  data[i] = nr_complex_t (d);
182 }
183 
184 void vector::set (const nr_complex_t z, int i) {
185  data[i] = nr_complex_t (z);
186 }
187 
188 // The function returns the current size of the vector.
189 int vector::getSize (void) {
190  return size;
191 }
192 
194  if (v1.getSize () != v2.getSize ()) {
195  logprint (LOG_ERROR, "vector '%s' and '%s' have different sizes\n",
196  v1.getName (), v2.getName ());
197  return 0;
198  }
199  return 1;
200 }
201 
202 // searches the maximum value of the vector elements.
203 // complex numbers in the 1. and 4. quadrant are counted as "abs(c)".
204 // complex numbers in the 2. and 3. quadrant are counted as "-abs(c)".
205 nr_double_t vector::maximum (void) {
206  nr_complex_t c;
207  nr_double_t d, max_D = -NR_MAX;
208  for (int i = 0; i < getSize (); i++) {
209  c = data[i];
210  d = fabs (arg (c)) < M_PI_2 ? abs (c) : -abs (c);
211  if (d > max_D) max_D = d;
212  }
213  return max_D;
214 }
215 
216 // searches the minimum value of the vector elements.
217 // complex numbers in the 1. and 4. quadrant are counted as "abs(c)".
218 // complex numbers in the 2. and 3. quadrant are counted as "-abs(c)".
219 nr_double_t vector::minimum (void) {
220  nr_complex_t c;
221  nr_double_t d, min_D = +NR_MAX;
222  for (int i = 0; i < getSize (); i++) {
223  c = data[i];
224  d = fabs (arg (c)) < M_PI_2 ? abs (c) : -abs (c);
225  if (d < min_D) min_D = d;
226  }
227  return min_D;
228 }
229 
230 /* Unwraps a phase vector in radians. Adds +/- 2*Pi if consecutive
231  values jump about |Pi|. */
232 vector unwrap (vector v, nr_double_t tol, nr_double_t step) {
233  vector result (v.getSize ());
234  nr_double_t add = 0;
235  result (0) = v (0);
236  for (int i = 1; i < v.getSize (); i++) {
237  nr_double_t diff = real (v (i) - v (i-1));
238  if (diff > +tol) {
239  add -= step;
240  } else if (diff < -tol) {
241  add += step;
242  }
243  result (i) = v (i) + add;
244  }
245  return result;
246 }
247 
249  nr_complex_t result (0.0);
250  for (int i = 0; i < v.getSize (); i++) result += v.get (i);
251  return result;
252 }
253 
255  nr_complex_t result (1.0);
256  for (int i = 0; i < v.getSize (); i++) result *= v.get (i);
257  return result;
258 }
259 
261  nr_complex_t result (0.0);
262  for (int i = 0; i < v.getSize (); i++) result += v.get (i);
263  return result / (nr_double_t) v.getSize ();
264 }
265 
267  vector result (v);
268  for (int i = 0; i < v.getSize (); i++) result.set (signum (v.get (i)), i);
269  return result;
270 }
271 
273  vector result (v);
274  for (int i = 0; i < v.getSize (); i++) result.set (sign (v.get (i)), i);
275  return result;
276 }
277 
279  vector result (v);
280  for (int i = 0; i < v.getSize (); i++) result.set (xhypot (v.get(i), z), i);
281  return result;
282 }
283 
284 vector xhypot (vector v, const nr_double_t d) {
285  vector result (v);
286  for (int i = 0; i < v.getSize (); i++) result.set (xhypot (v.get(i), d), i);
287  return result;
288 }
289 
291  vector result (v);
292  for (int i = 0; i < v.getSize (); i++) result.set (xhypot (z, v.get (i)), i);
293  return result;
294 }
295 
296 vector xhypot (const nr_double_t d, vector v) {
297  vector result (v);
298  for (int i = 0; i < v.getSize (); i++) result.set (xhypot (d, v.get (i)), i);
299  return result;
300 }
301 
303  int j, i, n, len, len1 = v1.getSize (), len2 = v2.getSize ();
304  if (len1 >= len2) {
305  assert (len1 % len2 == 0);
306  len = len1;
307  } else {
308  assert (len2 % len1 == 0);
309  len = len2;
310  }
311  vector res (len);
312  for (j = i = n = 0; n < len; n++) {
313  res (n) = xhypot (v1 (i), v2 (j));
314  if (++i >= len1) i = 0; if (++j >= len2) j = 0;
315  }
316  return res;
317 }
318 
320  vector result (v);
321  for (int i = 0; i < v.getSize (); i++) result.set (sinc (v.get (i)), i);
322  return result;
323 }
324 
326  vector result (v);
327  for (int i = 0; i < v.getSize (); i++) result.set (abs (v.get (i)), i);
328  return result;
329 }
330 
332  vector result (v);
333  for (int i = 0; i < v.getSize (); i++) result.set (norm (v.get (i)), i);
334  return result;
335 }
336 
338  vector result (v);
339  for (int i = 0; i < v.getSize (); i++) result.set (arg (v.get (i)), i);
340  return result;
341 }
342 
344  vector result (v);
345  for (int i = 0; i < v.getSize (); i++) result.set (real (v.get (i)), i);
346  return result;
347 }
348 
350  vector result (v);
351  for (int i = 0; i < v.getSize (); i++) result.set (imag (v.get (i)), i);
352  return result;
353 }
354 
356  vector result (v);
357  for (int i = 0; i < v.getSize (); i++) result.set (conj (v.get (i)), i);
358  return result;
359 }
360 
362  vector result (v);
363  for (int i = 0; i < v.getSize (); i++)
364  result.set (10.0 * log10 (norm (v.get (i))), i);
365  return result;
366 }
367 
369  vector result (v);
370  for (int i = 0; i < v.getSize (); i++) result.set (sqrt (v.get (i)), i);
371  return result;
372 }
373 
375  vector result (v);
376  for (int i = 0; i < v.getSize (); i++) result.set (exp (v.get (i)), i);
377  return result;
378 }
379 
381  vector result (v);
382  for (int i = 0; i < v.getSize (); i++) result.set (limexp (v.get (i)), i);
383  return result;
384 }
385 
387  vector result (v);
388  for (int i = 0; i < v.getSize (); i++) result.set (log (v.get (i)), i);
389  return result;
390 }
391 
393  vector result (v);
394  for (int i = 0; i < v.getSize (); i++) result.set (log10 (v.get (i)), i);
395  return result;
396 }
397 
399  vector result (v);
400  for (int i = 0; i < v.getSize (); i++) result.set (log2 (v.get (i)), i);
401  return result;
402 }
403 
405  vector result (v);
406  for (int i = 0; i < v.getSize (); i++) result.set (pow (v.get(i), z), i);
407  return result;
408 }
409 
410 vector pow (vector v, const nr_double_t d) {
411  vector result (v);
412  for (int i = 0; i < v.getSize (); i++) result.set (pow (v.get(i), d), i);
413  return result;
414 }
415 
417  vector result (v);
418  for (int i = 0; i < v.getSize (); i++) result.set (pow (z, v.get (i)), i);
419  return result;
420 }
421 
422 vector pow (const nr_double_t d, vector v) {
423  vector result (v);
424  for (int i = 0; i < v.getSize (); i++) result.set (pow (d, v.get (i)), i);
425  return result;
426 }
427 
429  int j, i, n, len, len1 = v1.getSize (), len2 = v2.getSize ();
430  if (len1 >= len2) {
431  assert (len1 % len2 == 0);
432  len = len1;
433  } else {
434  assert (len2 % len1 == 0);
435  len = len2;
436  }
437  vector res (len);
438  for (j = i = n = 0; n < len; n++) {
439  res (n) = pow (v1 (i), v2 (j));
440  if (++i >= len1) i = 0; if (++j >= len2) j = 0;
441  }
442  return res;
443 }
444 
446  vector result (v);
447  for (int i = 0; i < v.getSize (); i++) result.set (sin (v.get (i)), i);
448  return result;
449 }
450 
452  vector result (v);
453  for (int i = 0; i < v.getSize (); i++) result.set (asin (v.get (i)), i);
454  return result;
455 }
456 
458  vector result (v);
459  for (int i = 0; i < v.getSize (); i++) result.set (acos (v.get (i)), i);
460  return result;
461 }
462 
464  vector result (v);
465  for (int i = 0; i < v.getSize (); i++) result.set (cos (v.get (i)), i);
466  return result;
467 }
468 
470  vector result (v);
471  for (int i = 0; i < v.getSize (); i++) result.set (tan (v.get (i)), i);
472  return result;
473 }
474 
476  vector result (v);
477  for (int i = 0; i < v.getSize (); i++) result.set (atan (v.get (i)), i);
478  return result;
479 }
480 
482  vector result (v);
483  for (int i = 0; i < v.getSize (); i++) result.set (cot (v.get (i)), i);
484  return result;
485 }
486 
488  vector result (v);
489  for (int i = 0; i < v.getSize (); i++) result.set (acot (v.get (i)), i);
490  return result;
491 }
492 
494  vector result (v);
495  for (int i = 0; i < v.getSize (); i++) result.set (sinh (v.get (i)), i);
496  return result;
497 }
498 
500  vector result (v);
501  for (int i = 0; i < v.getSize (); i++) result.set (asinh (v.get (i)), i);
502  return result;
503 }
504 
506  vector result (v);
507  for (int i = 0; i < v.getSize (); i++) result.set (cosh (v.get (i)), i);
508  return result;
509 }
510 
512  vector result (v);
513  for (int i = 0; i < v.getSize (); i++) result.set (acosh (v.get (i)), i);
514  return result;
515 }
516 
518  vector result (v);
519  for (int i = 0; i < v.getSize (); i++) result.set (asech (v.get (i)), i);
520  return result;
521 }
522 
524  vector result (v);
525  for (int i = 0; i < v.getSize (); i++) result.set (tanh (v.get (i)), i);
526  return result;
527 }
528 
530  vector result (v);
531  for (int i = 0; i < v.getSize (); i++) result.set (atanh (v.get (i)), i);
532  return result;
533 }
534 
536  vector result (v);
537  for (int i = 0; i < v.getSize (); i++) result.set (coth (v.get (i)), i);
538  return result;
539 }
540 
542  vector result (v);
543  for (int i = 0; i < v.getSize (); i++) result.set (acoth (v.get (i)), i);
544  return result;
545 }
546 
547 // converts impedance to reflexion coefficient
549  vector result (v);
550  for (int i = 0; i < v.getSize (); i++) result (i) = ztor (v (i), zref);
551  return result;
552 }
553 
554 // converts admittance to reflexion coefficient
556  vector result (v);
557  for (int i = 0; i < v.getSize (); i++) result (i) = ytor (v (i), zref);
558  return result;
559 }
560 
561 // converts reflexion coefficient to impedance
563  vector result (v);
564  for (int i = 0; i < v.getSize (); i++) result (i) = rtoz (v (i), zref);
565  return result;
566 }
567 
568 // converts reflexion coefficient to admittance
570  vector result (v);
571  for (int i = 0; i < v.getSize (); i++) result (i) = rtoy (v (i), zref);
572  return result;
573 }
574 
575 // differentiates 'var' with respect to 'dep' exactly 'n' times
576 vector diff (vector var, vector dep, int n) {
577  int k, xi, yi, exchange = 0;
578  vector x, y;
579  // exchange dependent and independent variable if necessary
580  if (var.getSize () < dep.getSize ()) {
581  x = vector (var);
582  y = vector (dep);
583  exchange++;
584  }
585  else {
586  x = vector (dep);
587  y = vector (var);
588  }
589  assert (y.getSize () % x.getSize () == 0 && x.getSize () >= 2);
590  vector result (y);
591  nr_complex_t c;
592 
593  for (k = 0; k < n; k++) { // differentiate n times
594  for (yi = xi = 0; yi < y.getSize (); yi++, xi++) {
595  if (xi == x.getSize ()) xi = 0; // roll through independent vector
596  if (xi == 0) {
597  c = (y.get (yi + 1) - y.get (yi)) / (x.get (xi + 1) - x.get (xi));
598  } else if (xi == x.getSize () - 1) {
599  c = (y.get (yi) - y.get (yi - 1)) / (x.get (xi) - x.get (xi - 1));
600  }
601  else {
602  c =
603  ((y.get (yi) - y.get (yi - 1)) / (x.get (xi) - x.get (xi - 1)) +
604  (y.get (yi + 1) - y.get (yi)) / (x.get (xi + 1) - x.get (xi))) /
605  2.0;
606  }
607  result.set (exchange ? 1.0 / c : c, yi);
608  }
609  y = result;
610  }
611  return result;
612 }
613 
615  for (int i = 0; i < size; i++) data[i] = c;
616  return *this;
617 }
618 
619 vector vector::operator=(const nr_double_t d) {
620  for (int i = 0; i < size; i++) data[i] = d;
621  return *this;
622 }
623 
625  int i, n, len = v.getSize ();
626  assert (size % len == 0);
627  for (i = n = 0; i < size; i++) { data[i] += v (n); if (++n >= len) n = 0; }
628  return *this;
629 }
630 
632  for (int i = 0; i < size; i++) data[i] += c;
633  return *this;
634 }
635 
636 vector vector::operator+=(const nr_double_t d) {
637  for (int i = 0; i < size; i++) data[i] += d;
638  return *this;
639 }
640 
642  int len1 = v1.getSize (), len2 = v2.getSize ();
643  vector result;
644  if (len1 >= len2) {
645  result = v1;
646  result += v2;
647  } else {
648  result = v2;
649  result += v1;
650  }
651  return result;
652 }
653 
655  vector result (v);
656  result += c;
657  return result;
658 }
659 
661  return v + c;
662 }
663 
664 vector operator+(vector v, const nr_double_t d) {
665  vector result (v);
666  result += d;
667  return result;
668 }
669 
670 vector operator+(const nr_double_t d, vector v) {
671  return v + d;
672 }
673 
675  vector result (size);
676  for (int i = 0; i < size; i++) result (i) = -data[i];
677  return result;
678 }
679 
681  int i, n, len = v.getSize ();
682  assert (size % len == 0);
683  for (i = n = 0; i < size; i++) { data[i] -= v (n); if (++n >= len) n = 0; }
684  return *this;
685 }
686 
688  for (int i = 0; i < size; i++) data[i] -= c;
689  return *this;
690 }
691 
692 vector vector::operator-=(const nr_double_t d) {
693  for (int i = 0; i < size; i++) data[i] -= d;
694  return *this;
695 }
696 
698  int len1 = v1.getSize (), len2 = v2.getSize ();
699  vector result;
700  if (len1 >= len2) {
701  result = v1;
702  result -= v2;
703  } else {
704  result = -v2;
705  result += v1;
706  }
707  return result;
708 }
709 
711  vector result (v);
712  result -= c;
713  return result;
714 }
715 
716 vector operator-(vector v, const nr_double_t d) {
717  vector result (v);
718  result -= d;
719  return result;
720 }
721 
723  vector result (-v);
724  result += c;
725  return result;
726 }
727 
728 vector operator-(const nr_double_t d, vector v) {
729  vector result (-v);
730  result += d;
731  return result;
732 }
733 
735  int i, n, len = v.getSize ();
736  assert (size % len == 0);
737  for (i = n = 0; i < size; i++) { data[i] *= v (n); if (++n >= len) n = 0; }
738  return *this;
739 }
740 
742  for (int i = 0; i < size; i++) data[i] *= c;
743  return *this;
744 }
745 
746 vector vector::operator*=(const nr_double_t d) {
747  for (int i = 0; i < size; i++) data[i] *= d;
748  return *this;
749 }
750 
752  int len1 = v1.getSize (), len2 = v2.getSize ();
753  vector result;
754  if (len1 >= len2) {
755  result = v1;
756  result *= v2;
757  } else {
758  result = v2;
759  result *= v1;
760  }
761  return result;
762 }
763 
765  vector result (v);
766  result *= c;
767  return result;
768 }
769 
770 vector operator*(vector v, const nr_double_t d) {
771  vector result (v);
772  result *= d;
773  return result;
774 }
775 
777  return v * c;
778 }
779 
780 vector operator*(const nr_double_t d, vector v) {
781  return v * d;
782 }
783 
785  int i, n, len = v.getSize ();
786  assert (size % len == 0);
787  for (i = n = 0; i < size; i++) { data[i] /= v (n); if (++n >= len) n = 0; }
788  return *this;
789 }
790 
792  for (int i = 0; i < size; i++) data[i] /= c;
793  return *this;
794 }
795 
796 vector vector::operator/=(const nr_double_t d) {
797  for (int i = 0; i < size; i++) data[i] /= d;
798  return *this;
799 }
800 
802  int len1 = v1.getSize (), len2 = v2.getSize ();
803  vector result;
804  if (len1 >= len2) {
805  assert (len1 % len2 == 0);
806  result = v1;
807  result /= v2;
808  } else {
809  assert (len2 % len1 == 0);
810  result = 1 / v2;
811  result *= v1;
812  }
813  return result;
814 }
815 
817  vector result (v);
818  result /= c;
819  return result;
820 }
821 
822 vector operator/(vector v, const nr_double_t d) {
823  vector result (v);
824  result /= d;
825  return result;
826 }
827 
829  vector result (v);
830  result = c;
831  result /= v;
832  return result;
833 }
834 
835 vector operator/(const nr_double_t d, vector v) {
836  vector result (v);
837  result = d;
838  result /= v;
839  return result;
840 }
841 
843  int len = v.getSize ();
844  vector result (len);
845  for (int i = 0; i < len; i++) result (i) = v (i) % z;
846  return result;
847 }
848 
849 vector operator%(vector v, const nr_double_t d) {
850  int len = v.getSize ();
851  vector result (len);
852  for (int i = 0; i < len; i++) result (i) = v (i) % d;
853  return result;
854 }
855 
857  int len = v.getSize ();
858  vector result (len);
859  for (int i = 0; i < len; i++) result (i) = z % v (i);
860  return result;
861 }
862 
863 vector operator%(const nr_double_t d, vector v) {
864  int len = v.getSize ();
865  vector result (len);
866  for (int i = 0; i < len; i++) result (i) = d % v (i);
867  return result;
868 }
869 
871  int j, i, n, len, len1 = v1.getSize (), len2 = v2.getSize ();
872  if (len1 >= len2) {
873  assert (len1 % len2 == 0);
874  len = len1;
875  } else {
876  assert (len2 % len1 == 0);
877  len = len2;
878  }
879  vector res (len);
880  for (j = i = n = 0; n < len; n++) {
881  res (n) = v1 (i) % v2 (j);
882  if (++i >= len1) i = 0; if (++j >= len2) j = 0;
883  }
884  return res;
885 }
886 
887 /* This function reverses the order of the data list. */
888 void vector::reverse (void) {
890  malloc (sizeof (nr_complex_t) * size);
891  for (int i = 0; i < size; i++) buffer[i] = data[size - 1 - i];
892  free (data);
893  data = buffer;
894  capacity = size;
895 }
896 
897 // Sets the origin (the analysis) of the vector.
898 void vector::setOrigin (char * n) {
899  if (origin) free (origin);
900  origin = n ? strdup (n) : NULL;
901 }
902 
903 // Returns the origin (the analysis) of the vector.
904 char * vector::getOrigin (void) {
905  return origin;
906 }
907 
908 /* The function returns the number of entries with the given value
909  deviating no more than the given epsilon. */
910 int vector::contains (nr_complex_t val, nr_double_t eps) {
911  int count = 0;
912  for (int i = 0; i < size; i++) {
913  if (abs (data[i] - val) <= eps) count++;
914  }
915  return count;
916 }
917 
918 // Sorts the vector either in ascending or descending order.
919 void vector::sort (bool ascending) {
920  nr_complex_t t;
921  for (int i = 0; i < size; i++) {
922  for (int n = 0; n < size - 1; n++) {
923  if (ascending ? data[n] > data[n+1] : data[n] < data[n+1]) {
924  t = data[n];
925  data[n] = data[n+1];
926  data[n+1] = t;
927  }
928  }
929  }
930 }
931 
932 /* The function creates a linear stepped vector of values starting at
933  the given start value, ending with the given stop value and
934  containing points elements. */
935 vector linspace (nr_double_t start, nr_double_t stop, int points) {
936  vector result (points);
937  nr_double_t val, step = (stop - start) / (points - 1);
938  for (int i = 0; i < points; i++) {
939  val = start + (i * step);
940  if (i != 0 && fabs (val) < fabs (step) / 4 && fabs (val) < NR_EPSI)
941  val = 0.0;
942  result.set (val, i);
943  }
944  return result;
945 }
946 
947 /* The function creates a logarithmic stepped vector of values
948  starting at the given start value, ending with the given stop value
949  and containing points elements. */
950 vector logspace (nr_double_t start, nr_double_t stop, int points) {
951  assert (start * stop > 0);
952  vector result (points);
953  nr_double_t step, first, last, d;
954 
955  // ensure the last value being larger than the first
956  if (fabs (start) > fabs (stop)) {
957  first = fabs (stop);
958  last = fabs (start);
959  }
960  else {
961  first = fabs (start);
962  last = fabs (stop);
963  }
964  // check direction and sign of values
965  d = fabs (start) > fabs (stop) ? -1 : 1;
966  // compute logarithmic step size
967  step = (log (last) - log (first)) / (points - 1);
968  for (int i = 0, j = points - 1; i < points; i++, j--) {
969  if (d > 0)
970  result.set (start * exp (step * i), i);
971  else
972  result.set (stop * exp (step * i), j);
973  }
974  return result;
975 }
976 
978  vector result (v);
979  nr_complex_t val (0.0);
980  for (int i = 0; i < v.getSize (); i++) {
981  val += v.get (i);
982  result.set (val, i);
983  }
984  return result;
985 }
986 
988  vector result (v);
989  nr_complex_t val (0.0);
990  for (int i = 0; i < v.getSize (); i++) {
991  val = (val * (nr_double_t) i + v.get (i)) / (i + 1.0);
992  result.set (val, i);
993  }
994  return result;
995 }
996 
998  vector result (v);
999  nr_complex_t val (1.0);
1000  for (int i = 0; i < v.getSize (); i++) {
1001  val *= v.get (i);
1002  result.set (val, i);
1003  }
1004  return result;
1005 }
1006 
1008  vector result (v);
1009  for (int i = 0; i < v.getSize (); i++) result.set (ceil (v.get (i)), i);
1010  return result;
1011 }
1012 
1014  vector result (v);
1015  for (int i = 0; i < v.getSize (); i++) result.set (fix (v.get (i)), i);
1016  return result;
1017 }
1018 
1020  vector result (v);
1021  for (int i = 0; i < v.getSize (); i++) result.set (floor (v.get (i)), i);
1022  return result;
1023 }
1024 
1026  vector result (v);
1027  for (int i = 0; i < v.getSize (); i++) result.set (round (v.get (i)), i);
1028  return result;
1029 }
1030 
1032  vector result (v);
1033  for (int i = 0; i < v.getSize (); i++) result.set (sqr (v.get (i)), i);
1034  return result;
1035 }
1036 
1038  vector result (v);
1039  for (int i = 0; i < v.getSize (); i++) result.set (step (v.get (i)), i);
1040  return result;
1041 }
1042 
1043 static nr_double_t integrate_n (vector v) { /* using trapezoidal rule */
1044  nr_double_t result = 0.0;
1045  for (int i = 1; i < v.getSize () - 1; i++) result += norm (v.get (i));
1046  result += 0.5 * norm (v.get (0));
1047  result += 0.5 * norm (v.get (v.getSize () - 1));
1048  return result;
1049 }
1050 
1051 nr_double_t vector::rms (void) {
1052  nr_double_t result = sqrt (integrate_n (*this) / getSize ());
1053  return result;
1054 }
1055 
1056 nr_double_t vector::variance (void) {
1057  nr_double_t result = 0.0;
1058  nr_complex_t average = avg (*this);
1059  for (int i = 0; i < getSize (); i++) result += norm (get (i) - average);
1060  if (getSize () > 1)
1061  return result / (getSize () - 1);
1062  return 0.0;
1063 }
1064 
1065 nr_double_t vector::stddev (void) {
1066  return sqrt (variance ());
1067 }
1068 
1070  vector result (v);
1071  for (int i = 0; i < v.getSize (); i++) result.set (erf (v.get (i)), i);
1072  return result;
1073 }
1074 
1076  vector result (v);
1077  for (int i = 0; i < v.getSize (); i++) result.set (erfc (v.get (i)), i);
1078  return result;
1079 }
1080 
1082  vector result (v);
1083  for (int i = 0; i < v.getSize (); i++) result.set (erfinv (v.get (i)), i);
1084  return result;
1085 }
1086 
1088  vector result (v);
1089  for (int i = 0; i < v.getSize (); i++) result.set (erfcinv (v.get (i)), i);
1090  return result;
1091 }
1092 
1094  vector result (v);
1095  for (int i = 0; i < v.getSize (); i++) result.set (i0 (v.get (i)), i);
1096  return result;
1097 }
1098 
1099 vector jn (const int n, vector v) {
1100  vector result (v);
1101  for (int i = 0; i < v.getSize (); i++) result.set (jn (n, v.get (i)), i);
1102  return result;
1103 }
1104 
1105 vector yn (const int n, vector v) {
1106  vector result (v);
1107  for (int i = 0; i < v.getSize (); i++) result.set (yn (n, v.get (i)), i);
1108  return result;
1109 }
1110 
1112  vector result (v);
1113  for (int i = 0; i < v.getSize (); i++) result.set (polar (a, v.get (i)), i);
1114  return result;
1115 }
1116 
1118  vector result (v);
1119  for (int i = 0; i < v.getSize (); i++) result.set (polar (v.get (i), p), i);
1120  return result;
1121 }
1122 
1124  int j, i, n, len, len1 = a.getSize (), len2 = p.getSize ();
1125  if (len1 >= len2) {
1126  assert (len1 % len2 == 0);
1127  len = len1;
1128  } else {
1129  assert (len2 % len1 == 0);
1130  len = len2;
1131  }
1132  vector res (len);
1133  for (j = i = n = 0; n < len; n++) {
1134  res (n) = polar (a (i), p (j));
1135  if (++i >= len1) i = 0; if (++j >= len2) j = 0;
1136  }
1137  return res;
1138 }
1139 
1140 vector atan2 (const nr_double_t y, vector v) {
1141  vector result (v);
1142  for (int i = 0; i < v.getSize (); i++)
1143  result.set (atan2 (y, v.get (i)), i);
1144  return result;
1145 }
1146 
1147 vector atan2 (vector v, const nr_double_t x) {
1148  vector result (v);
1149  for (int i = 0; i < v.getSize (); i++)
1150  result.set (atan2 (v.get (i), x) , i);
1151  return result;
1152 }
1153 
1155  int j, i, n, len, len1 = y.getSize (), len2 = x.getSize ();
1156  if (len1 >= len2) {
1157  assert (len1 % len2 == 0);
1158  len = len1;
1159  } else {
1160  assert (len2 % len1 == 0);
1161  len = len2;
1162  }
1163  vector res (len);
1164  for (j = i = n = 0; n < len; n++) {
1165  res (n) = atan2 (y (i), x (j));
1166  if (++i >= len1) i = 0; if (++j >= len2) j = 0;
1167  }
1168  return res;
1169 }
1170 
1172  vector result (v);
1173  for (int i = 0; i < v.getSize (); i++)
1174  result.set (10.0 * log10 (v.get (i) / 0.001), i);
1175  return result;
1176 }
1177 
1179  vector result (v);
1180  for (int i = 0; i < v.getSize (); i++)
1181  result.set (0.001 * pow (10.0 , v.get (i) / 10.0), i);
1182  return result;
1183 }
1184 
1185 nr_double_t integrate (vector v, const nr_double_t h) {
1186  nr_double_t s = real (v.get (0) ) / 2;
1187  for (int i = 1; i < v.getSize () - 1; i++)
1188  s += real (v.get (i));
1189  return (s + real (v.get (v.getSize () - 1) ) / 2) * h;
1190 }
1191 
1193  nr_complex_t s;
1194  s = v.get (0) / 2.0;
1195  for (int i = 1; i < v.getSize () - 1; i++)
1196  s += v.get (i);
1197  return (s + v.get (v.getSize () - 1) / 2.0) * h;
1198 }
1199 
1201  vector result (v);
1202  for (int i = 0; i < v.getSize (); i++)
1203  result.set (10.0 * log10 (norm (v.get (i)) / conj (z) / 0.001), i);
1204  return result;
1205 }
1206 
1207 vector runavg (const nr_complex_t x, const int n) {
1208  vector result (n);
1209  for (int i = 0; i < n; i++) result.set (x, i);
1210  return result;
1211 }
1212 
1213 vector runavg (vector v, const int n) {
1214  nr_complex_t s (0.0), y;
1215  int len = v.getSize () - n + 1, i;
1216  vector result (len);
1217  for (i = 0; i < n; i++) s += v.get (i);
1218  y = s / (nr_double_t) n; // first running average value
1219  result.set (y, 0);
1220  for (i = 0; i < len - 1; i++) {
1221  y += (v.get (i + n) - v.get (i)) / (nr_double_t) n;
1222  result.set (y, i + 1);
1223  }
1224  return result;
1225 }
1226 
1227 #ifdef DEBUG
1228 // Debug function: Prints the vector object.
1229 void vector::print (void) {
1230  for (int r = 0; r < size; r++) {
1231  fprintf (stderr, "%+.2e%+.2ei\n", (double) real (get (r)),
1232  (double) imag (get (r)));
1233  }
1234 }
1235 #endif /* DEBUG */