My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
sweep.cpp
Go to the documentation of this file.
1 /*
2  * sweep.cpp - variable sweep class implementation
3  *
4  * Copyright (C) 2004, 2005, 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: sweep.cpp 1825 2011-03-11 20:42:14Z ela $
22  *
23  */
24 
25 #if HAVE_CONFIG_H
26 # include <config.h>
27 #endif
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33 #include <assert.h>
34 
35 #include "object.h"
36 #include "complex.h"
37 #include "vector.h"
38 #include "sweep.h"
39 
40 // Constructor creates an unnamed instance of the sweep class.
43  data = NULL;
44  size = 0;
45  txt = NULL;
46  counter = 0;
47 }
48 
49 // Constructor creates a named instance of the sweep class.
50 sweep::sweep (const char * n) : object (n) {
52  data = NULL;
53  size = 0;
54  txt = NULL;
55  counter = 0;
56 }
57 
58 // Destructor deletes the sweep class object.
60  if (data) free (data);
61  if (txt) free (txt);
62 }
63 
64 /* The copy constructor creates a new instance of the sweep class
65  based on the given sweep object. */
67  type = s.type;
68  size = s.size;
69  counter = s.counter;
70  data = (nr_double_t *) malloc (sizeof (nr_double_t) * size);
71  if (s.data)
72  memcpy (data, s.data, sizeof (nr_double_t) * size);
73  else
74  memset (data, 0, sizeof (nr_double_t) * size);
75 }
76 
77 // The function returns the value at the given position.
78 nr_double_t sweep::get (int idx) {
79  assert (idx >= 0 && idx < size && data != NULL);
80  return data[idx];
81 }
82 
83 // The function sets the given value at the given position.
84 void sweep::set (int idx, nr_double_t val) {
85  assert (idx >= 0 && idx < size && data != NULL);
86  data[idx] = val;
87 }
88 
89 /* This function modifies the current size of the sweep. If the the
90  new number of points is larger than the current one it zeroes the
91  new elements. */
92 void sweep::setSize (int points) {
93  assert (points > 0);
94  if (data != NULL) {
95  data = (nr_double_t *) realloc (data, sizeof (nr_double_t) * points);
96  if (points > size)
97  memset (&data[size], 0, sizeof (nr_double_t) * (points - size));
98  }
99  else {
100  data = (nr_double_t *) malloc (sizeof (nr_double_t) * points);
101  memset (data, 0, sizeof (nr_double_t) * points);
102  }
103  size = points;
104  counter = 0;
105 }
106 
107 // The function creates a string representation of the sweep definition.
108 char * sweep::toString (void) {
109  if (txt) free (txt);
110  if (data == NULL || size == 0) return (char *) "";
111  int len = 3 + size - 1;
112  txt = (char *) malloc (len);
113  strcpy (txt, "[");
114  for (int i = 0; i < size; i++) {
115  static char str[256]; // enough for a real number
116  sprintf (str, "%g", (double) get (i));
117  txt = (char *) realloc (txt, len += strlen (str));
118  strcat (txt, str);
119  if (i != size - 1) strcat (txt, ";");
120  }
121  strcat (txt, "]");
122  return txt;
123 }
124 
125 /* The following function reverses the values order inside the sweep
126  definition. */
127 void sweep::reverse (void) {
128  if (data != NULL && size > 0) {
129  nr_double_t * buf = (nr_double_t *) malloc (sizeof (nr_double_t) * size);
130  for (int i = 0; i < size; i++) buf[i] = data[size - 1 - i];
131  free (data);
132  data = buf;
133  }
134 }
135 
136 /* The function returns the current sweep value and afterwards steps
137  one value forward. It wraps around at the end of sweep. */
138 nr_double_t sweep::next (void) {
139  nr_double_t res = data[counter];
140  if (++counter >= size) counter = 0;
141  if (size == 1)
142  return parent->getPropertyDouble ("Values");
143  return res;
144 }
145 
146 /* This function returns the sweep value before the current value and
147  thereby steps one value back. It wraps around at the beginning of
148  the sweep. */
149 nr_double_t sweep::prev (void) {
150  if (--counter < 0) counter = size - 1;
151  return data[counter];
152 }
153 
154 // Constructor creates an unnamed instance of the linsweep class.
156  type = SWEEP_LINEAR;
157 }
158 
159 // Constructor creates a named instance of the linsweep class.
160 linsweep::linsweep (const char * n) : sweep (n) {
161  type = SWEEP_LINEAR;
162 }
163 
164 /* This function creates a linear stepped vector of values starting at
165  the given start value, ending with the given stop value and
166  containing points elements. */
167 void linsweep::create (nr_double_t start, nr_double_t stop, int points) {
168  vector v = linspace (start, stop, points);
169  setSize (points);
170  for (int i = 0; i < points; i++) set (i, real (v.get (i)));
171 }
172 
173 // Destructor deletes the linsweep class object.
175 }
176 
177 // Constructor creates an unnamed instance of the logsweep class.
180 }
181 
182 // Constructor creates a named instance of the logsweep class.
183 logsweep::logsweep (const char * n) : sweep (n) {
185 }
186 
187 /* This function creates a logarithmic stepped vector of values
188  starting at the given start value, ending with the given stop value
189  and containing points elements. */
190 void logsweep::create (nr_double_t start, nr_double_t stop, int points) {
191  vector v = logspace (start, stop, points);
192  setSize (points);
193  for (int i = 0; i < points; i++) set (i, real (v.get (i)));
194 }
195 
196 // Destructor deletes the logsweep class object.
198 }
199 
200 // Constructor creates an unnamed instance of the consweep class.
203 }
204 
205 // Constructor creates a named instance of the consweep class.
206 consweep::consweep (const char * n) : sweep (n) {
208 }
209 
210 /* This function creates a constant value in a sweep containing one
211  element only. */
212 void consweep::create (nr_double_t val) {
213  setSize (1);
214  set (0, val);
215 }
216 
217 // Destructor deletes the consweep class object.
219 }
220 
221 // Constructor creates an unnamed instance of the lstsweep class.
223  type = SWEEP_LIST;
224 }
225 
226 // Constructor creates a named instance of the lstsweep class.
227 lstsweep::lstsweep (const char * n) : sweep (n) {
228  type = SWEEP_LIST;
229 }
230 
231 /* This function creates arbitrary values in a sweep containing points
232  elements. The actual values must be assigned using the set()
233  function. */
234 void lstsweep::create (int points) {
235  setSize (points);
236 }
237 
238 // Destructor deletes the lstsweep class object.
240 }