Qucs-core  0.0.18
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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$
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 <cmath>
33 #include <assert.h>
34 
35 #include "object.h"
36 #include "complex.h"
37 #include "vector.h"
38 #include "sweep.h"
39 
40 namespace qucs {
41 
42 // Constructor creates an unnamed instance of the sweep class.
45  data = NULL;
46  size = 0;
47  txt = NULL;
48  counter = 0;
49 }
50 
51 // Constructor creates a named instance of the sweep class.
52 sweep::sweep (const char * n) : object (n) {
54  data = NULL;
55  size = 0;
56  txt = NULL;
57  counter = 0;
58 }
59 
60 // Destructor deletes the sweep class object.
62  if (data) free (data);
63  if (txt) free (txt);
64 }
65 
66 /* The copy constructor creates a new instance of the sweep class
67  based on the given sweep object. */
69  type = s.type;
70  size = s.size;
71  counter = s.counter;
72  data = (nr_double_t *) malloc (sizeof (nr_double_t) * size);
73  if (s.data)
74  memcpy (data, s.data, sizeof (nr_double_t) * size);
75  else
76  memset (data, 0, sizeof (nr_double_t) * size);
77 }
78 
79 // The function returns the value at the given position.
80 nr_double_t sweep::get (int idx) {
81  assert (idx >= 0 && idx < size && data != NULL);
82  return data[idx];
83 }
84 
85 // The function sets the given value at the given position.
86 void sweep::set (int idx, nr_double_t val) {
87  assert (idx >= 0 && idx < size && data != NULL);
88  data[idx] = val;
89 }
90 
91 /* This function modifies the current size of the sweep. If the the
92  new number of points is larger than the current one it zeroes the
93  new elements. */
94 void sweep::setSize (int points) {
95  assert (points > 0);
96  if (data != NULL) {
97  data = (nr_double_t *) realloc (data, sizeof (nr_double_t) * points);
98  if (points > size)
99  memset (&data[size], 0, sizeof (nr_double_t) * (points - size));
100  }
101  else {
102  data = (nr_double_t *) malloc (sizeof (nr_double_t) * points);
103  memset (data, 0, sizeof (nr_double_t) * points);
104  }
105  size = points;
106  counter = 0;
107 }
108 
109 // The function creates a string representation of the sweep definition.
110 char * sweep::toString (void) {
111  if (txt) free (txt);
112  if (data == NULL || size == 0) return (char *) "";
113  int len = 3 + size - 1;
114  txt = (char *) malloc (len);
115  strcpy (txt, "[");
116  for (int i = 0; i < size; i++) {
117  static char str[256]; // enough for a real number
118  sprintf (str, "%g", (double) get (i));
119  txt = (char *) realloc (txt, len += strlen (str));
120  strcat (txt, str);
121  if (i != size - 1) strcat (txt, ";");
122  }
123  strcat (txt, "]");
124  return txt;
125 }
126 
127 /* The following function reverses the values order inside the sweep
128  definition. */
129 void sweep::reverse (void) {
130  if (data != NULL && size > 0) {
131  nr_double_t * buf = (nr_double_t *) malloc (sizeof (nr_double_t) * size);
132  for (int i = 0; i < size; i++) buf[i] = data[size - 1 - i];
133  free (data);
134  data = buf;
135  }
136 }
137 
138 /* The function returns the current sweep value and afterwards steps
139  one value forward. It wraps around at the end of sweep. */
140 nr_double_t sweep::next (void) {
141  nr_double_t res = data[counter];
142  if (++counter >= size) counter = 0;
143  if (size == 1)
144  return parent->getPropertyDouble ("Values");
145  return res;
146 }
147 
148 /* This function returns the sweep value before the current value and
149  thereby steps one value back. It wraps around at the beginning of
150  the sweep. */
151 nr_double_t sweep::prev (void) {
152  if (--counter < 0) counter = size - 1;
153  return data[counter];
154 }
155 
156 // Constructor creates an unnamed instance of the linsweep class.
158  type = SWEEP_LINEAR;
159 }
160 
161 // Constructor creates a named instance of the linsweep class.
162 linsweep::linsweep (const char * n) : sweep (n) {
163  type = SWEEP_LINEAR;
164 }
165 
166 /* This function creates a linear stepped vector of values starting at
167  the given start value, ending with the given stop value and
168  containing points elements. */
169 void linsweep::create (nr_double_t start, nr_double_t stop, int points) {
170  vector v = linspace (start, stop, points);
171  setSize (points);
172  for (int i = 0; i < points; i++) set (i, real (v.get (i)));
173 }
174 
175 // Destructor deletes the linsweep class object.
177 }
178 
179 // Constructor creates an unnamed instance of the logsweep class.
182 }
183 
184 // Constructor creates a named instance of the logsweep class.
185 logsweep::logsweep (const char * n) : sweep (n) {
187 }
188 
189 /* This function creates a logarithmic stepped vector of values
190  starting at the given start value, ending with the given stop value
191  and containing points elements. */
192 void logsweep::create (nr_double_t start, nr_double_t stop, int points) {
193  vector v = logspace (start, stop, points);
194  setSize (points);
195  for (int i = 0; i < points; i++) set (i, real (v.get (i)));
196 }
197 
198 // Destructor deletes the logsweep class object.
200 }
201 
202 // Constructor creates an unnamed instance of the consweep class.
205 }
206 
207 // Constructor creates a named instance of the consweep class.
208 consweep::consweep (const char * n) : sweep (n) {
210 }
211 
212 /* This function creates a constant value in a sweep containing one
213  element only. */
214 void consweep::create (nr_double_t val) {
215  setSize (1);
216  set (0, val);
217 }
218 
219 // Destructor deletes the consweep class object.
221 }
222 
223 // Constructor creates an unnamed instance of the lstsweep class.
225  type = SWEEP_LIST;
226 }
227 
228 // Constructor creates a named instance of the lstsweep class.
229 lstsweep::lstsweep (const char * n) : sweep (n) {
230  type = SWEEP_LIST;
231 }
232 
233 /* This function creates arbitrary values in a sweep containing points
234  elements. The actual values must be assigned using the set()
235  function. */
237  setSize (points);
238 }
239 
240 // Destructor deletes the lstsweep class object.
242 }
243 
244 } // namespace qucs
start
Definition: parse_zvr.y:126
matrix real(matrix a)
Real part matrix.
Definition: matrix.cpp:568
int size
Definition: sweep.h:65
void create(nr_double_t, nr_double_t, int)
Definition: sweep.cpp:169
nr_double_t getPropertyDouble(const char *)
Definition: object.cpp:176
int counter
Definition: sweep.h:67
qucs::vector logspace(nr_double_t, nr_double_t, int)
Definition: vector.cpp:965
n
Definition: parse_citi.y:147
i
Definition: parse_mdl.y:516
void create(nr_double_t)
Definition: sweep.cpp:214
stop
Definition: parse_zvr.y:127
points
Definition: parse_zvr.y:129
free($1)
qucs::vector linspace(nr_double_t, nr_double_t, int)
Definition: vector.cpp:950
Declaration sizeof(struct vcd_scope))
nr_double_t get(int)
Definition: sweep.cpp:80
generic object class.
Definition: object.h:52
void create(nr_double_t, nr_double_t, int)
Definition: sweep.cpp:192
int type
Definition: sweep.h:61
v
Definition: parse_zvr.y:141
nr_double_t * data
Definition: sweep.h:64
void create(int)
Definition: sweep.cpp:236
void set(int, nr_double_t)
Definition: sweep.cpp:86
void setSize(int)
Definition: sweep.cpp:94
nr_double_t prev(void)
Definition: sweep.cpp:151
nr_double_t next(void)
Definition: sweep.cpp:140
char * txt
Definition: sweep.h:66
nr_complex_t get(int)
Definition: vector.cpp:179
void reverse(void)
Definition: sweep.cpp:129
object * parent
Definition: sweep.h:68
char * toString(void)
Definition: sweep.cpp:110