Qucs-core
0.0.19
|
00001 /* 00002 * sweep.cpp - variable sweep class implementation 00003 * 00004 * Copyright (C) 2004, 2005, 2008 Stefan Jahn <stefan@lkcc.org> 00005 * 00006 * This is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU General Public License as published by 00008 * the Free Software Foundation; either version 2, or (at your option) 00009 * any later version. 00010 * 00011 * This software is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this package; see the file COPYING. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 * $Id$ 00022 * 00023 */ 00024 00025 #if HAVE_CONFIG_H 00026 # include <config.h> 00027 #endif 00028 00029 #include <stdio.h> 00030 #include <stdlib.h> 00031 #include <string.h> 00032 #include <cmath> 00033 #include <assert.h> 00034 00035 #include "object.h" 00036 #include "complex.h" 00037 #include "vector.h" 00038 #include "sweep.h" 00039 00040 namespace qucs { 00041 00042 // Constructor creates an unnamed instance of the sweep class. 00043 sweep::sweep () : object () { 00044 type = SWEEP_UNKNOWN; 00045 data = NULL; 00046 size = 0; 00047 txt = NULL; 00048 counter = 0; 00049 } 00050 00051 // Constructor creates a named instance of the sweep class. 00052 sweep::sweep (const std::string &n) : object (n) { 00053 type = SWEEP_UNKNOWN; 00054 data = NULL; 00055 size = 0; 00056 txt = NULL; 00057 counter = 0; 00058 } 00059 00060 // Destructor deletes the sweep class object. 00061 sweep::~sweep () { 00062 free (data); 00063 free (txt); 00064 } 00065 00066 /* The copy constructor creates a new instance of the sweep class 00067 based on the given sweep object. */ 00068 sweep::sweep (sweep & s) : object (s) { 00069 type = s.type; 00070 size = s.size; 00071 counter = s.counter; 00072 data = (nr_double_t *) malloc (sizeof (nr_double_t) * size); 00073 if (s.data) 00074 memcpy (data, s.data, sizeof (nr_double_t) * size); 00075 else 00076 memset (data, 0, sizeof (nr_double_t) * size); 00077 } 00078 00079 // The function returns the value at the given position. 00080 nr_double_t sweep::get (int idx) { 00081 assert (idx >= 0 && idx < size && data != NULL); 00082 return data[idx]; 00083 } 00084 00085 // The function sets the given value at the given position. 00086 void sweep::set (int idx, nr_double_t val) { 00087 assert (idx >= 0 && idx < size && data != NULL); 00088 data[idx] = val; 00089 } 00090 00091 /* This function modifies the current size of the sweep. If the the 00092 new number of points is larger than the current one it zeroes the 00093 new elements. */ 00094 void sweep::setSize (int points) { 00095 assert (points > 0); 00096 if (data != NULL) { 00097 data = (nr_double_t *) realloc (data, sizeof (nr_double_t) * points); 00098 if (points > size) 00099 memset (&data[size], 0, sizeof (nr_double_t) * (points - size)); 00100 } 00101 else { 00102 data = (nr_double_t *) malloc (sizeof (nr_double_t) * points); 00103 memset (data, 0, sizeof (nr_double_t) * points); 00104 } 00105 size = points; 00106 counter = 0; 00107 } 00108 00109 // The function creates a string representation of the sweep definition. 00110 char * sweep::toString (void) { 00111 free (txt); 00112 if (data == NULL || size == 0) return (char *) ""; 00113 int len = 3 + size - 1; 00114 txt = (char *) malloc (len); 00115 strcpy (txt, "["); 00116 for (int i = 0; i < size; i++) { 00117 static char str[256]; // enough for a real number 00118 sprintf (str, "%g", (double) get (i)); 00119 txt = (char *) realloc (txt, len += strlen (str)); 00120 strcat (txt, str); 00121 if (i != size - 1) strcat (txt, ";"); 00122 } 00123 strcat (txt, "]"); 00124 return txt; 00125 } 00126 00127 /* The following function reverses the values order inside the sweep 00128 definition. */ 00129 void sweep::reverse (void) { 00130 if (data != NULL && size > 0) { 00131 nr_double_t * buf = (nr_double_t *) malloc (sizeof (nr_double_t) * size); 00132 for (int i = 0; i < size; i++) buf[i] = data[size - 1 - i]; 00133 free (data); 00134 data = buf; 00135 } 00136 } 00137 00138 /* The function returns the current sweep value and afterwards steps 00139 one value forward. It wraps around at the end of sweep. */ 00140 nr_double_t sweep::next (void) { 00141 nr_double_t res = data[counter]; 00142 if (++counter >= size) counter = 0; 00143 if (size == 1) 00144 return parent->getPropertyDouble ("Values"); 00145 return res; 00146 } 00147 00148 /* This function returns the sweep value before the current value and 00149 thereby steps one value back. It wraps around at the beginning of 00150 the sweep. */ 00151 nr_double_t sweep::prev (void) { 00152 if (--counter < 0) counter = size - 1; 00153 return data[counter]; 00154 } 00155 00156 // Constructor creates an unnamed instance of the linsweep class. 00157 linsweep::linsweep () : sweep () { 00158 type = SWEEP_LINEAR; 00159 } 00160 00161 // Constructor creates a named instance of the linsweep class. 00162 linsweep::linsweep (const std::string &n) : sweep (n) { 00163 type = SWEEP_LINEAR; 00164 } 00165 00166 /* This function creates a linear stepped vector of values starting at 00167 the given start value, ending with the given stop value and 00168 containing points elements. */ 00169 void linsweep::create (nr_double_t start, nr_double_t stop, int points) { 00170 vector v = linspace (start, stop, points); 00171 setSize (points); 00172 for (int i = 0; i < points; i++) set (i, real (v.get (i))); 00173 } 00174 00175 // Destructor deletes the linsweep class object. 00176 linsweep::~linsweep () { 00177 } 00178 00179 // Constructor creates an unnamed instance of the logsweep class. 00180 logsweep::logsweep () : sweep () { 00181 type = SWEEP_LOGARITHMIC; 00182 } 00183 00184 // Constructor creates a named instance of the logsweep class. 00185 logsweep::logsweep (const std::string &n) : sweep (n) { 00186 type = SWEEP_LOGARITHMIC; 00187 } 00188 00189 /* This function creates a logarithmic stepped vector of values 00190 starting at the given start value, ending with the given stop value 00191 and containing points elements. */ 00192 void logsweep::create (nr_double_t start, nr_double_t stop, int points) { 00193 vector v = logspace (start, stop, points); 00194 setSize (points); 00195 for (int i = 0; i < points; i++) set (i, real (v.get (i))); 00196 } 00197 00198 // Destructor deletes the logsweep class object. 00199 logsweep::~logsweep () { 00200 } 00201 00202 // Constructor creates an unnamed instance of the consweep class. 00203 consweep::consweep () : sweep () { 00204 type = SWEEP_CONSTANT; 00205 } 00206 00207 // Constructor creates a named instance of the consweep class. 00208 consweep::consweep (const std::string &n) : sweep (n) { 00209 type = SWEEP_CONSTANT; 00210 } 00211 00212 /* This function creates a constant value in a sweep containing one 00213 element only. */ 00214 void consweep::create (nr_double_t val) { 00215 setSize (1); 00216 set (0, val); 00217 } 00218 00219 // Destructor deletes the consweep class object. 00220 consweep::~consweep () { 00221 } 00222 00223 // Constructor creates an unnamed instance of the lstsweep class. 00224 lstsweep::lstsweep () : sweep () { 00225 type = SWEEP_LIST; 00226 } 00227 00228 // Constructor creates a named instance of the lstsweep class. 00229 lstsweep::lstsweep (const std::string& n) : sweep (n) { 00230 type = SWEEP_LIST; 00231 } 00232 00233 /* This function creates arbitrary values in a sweep containing points 00234 elements. The actual values must be assigned using the set() 00235 function. */ 00236 void lstsweep::create (int points) { 00237 setSize (points); 00238 } 00239 00240 // Destructor deletes the lstsweep class object. 00241 lstsweep::~lstsweep () { 00242 } 00243 00244 } // namespace qucs