From 0ee5e3f32a5c36bc6c8bca766ddb4964b1db7aa9 Mon Sep 17 00:00:00 2001 From: Holger Vogt Date: Sun, 4 Jun 2023 09:46:13 +0200 Subject: [PATCH] Add a control language function cvector, to create a complex vector. Let vec1 = cvector(50) will generate a complex vector, with the real part values increasing from 0 to 49, the imaginary values are set to 0. Useful in ac loops to store result data. --- src/frontend/parse.c | 1 + src/include/ngspice/fteext.h | 1 + src/maths/cmaths/cmath2.c | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/frontend/parse.c b/src/frontend/parse.c index 16666a1bc..ebe22d309 100644 --- a/src/frontend/parse.c +++ b/src/frontend/parse.c @@ -360,6 +360,7 @@ struct func ft_funcs[] = { { "avg", cx_avg }, /* A.Roldan 03/06/05 incremental average new function */ { "group_delay", (cx_function_t*)(void *) cx_group_delay }, /* A.Roldan 10/06/05 group delay new function */ { "vector", cx_vector }, + { "cvector", cx_cvector }, { "unitvec", cx_unitvec }, { "length", cx_length }, { "vecmin", cx_min }, diff --git a/src/include/ngspice/fteext.h b/src/include/ngspice/fteext.h index 7c8a383b7..4f2ce0c35 100644 --- a/src/include/ngspice/fteext.h +++ b/src/include/ngspice/fteext.h @@ -93,6 +93,7 @@ extern void *cx_mean(void *, short int , int , int *, short int *); extern void *cx_stddev(void *, short int , int , int *, short int *); extern void *cx_length(void *, short int , int , int *, short int *); extern void *cx_vector(void *, short int , int , int *, short int *); +extern void *cx_cvector(void *, short int , int , int *, short int *); extern void *cx_unitvec(void *, short int , int , int *, short int *); extern void *cx_plus(void *, void *, short int , short int , int ); extern void *cx_minus(void *, void *, short int , short int , int ); diff --git a/src/maths/cmaths/cmath2.c b/src/maths/cmaths/cmath2.c index 888c15a44..6dc37b6b1 100644 --- a/src/maths/cmaths/cmath2.c +++ b/src/maths/cmaths/cmath2.c @@ -4,7 +4,7 @@ Author: 1985 Wayne A. Christopher, U. C. Berkeley CAD Group **********/ /** \file cmath2.c - \brief functions for the control language parser: norm, uminus, rnd, sunif, sgauss, poisson, exponential, mean, stddev, length, vector, unitvec, plus, minus, times, mod, max, min, d, avg, floor, ceil, nint + \brief functions for the control language parser: norm, uminus, rnd, sunif, sgauss, poisson, exponential, mean, stddev, length, vector, cvector, unitvec, plus, minus, times, mod, max, min, d, avg, floor, ceil, nint Routines to do complex mathematical functions. These routines require the -lm libraries. We sacrifice a lot of space to be able @@ -479,7 +479,7 @@ cx_length(void *data, short int type, int length, int *newlength, short int *new /* Return a vector from 0 to the magnitude of the argument. Length of the - * argument is irrelevent. + * argument is irrelevant. */ void * @@ -506,6 +506,38 @@ cx_vector(void *data, short int type, int length, int *newlength, short int *new return ((void *) d); } +/* Return a complex vector. Argument sets the length of the vector. +The real part ranges from 0 to the magnitude of the argument. The imaginary +part is set to 0. + */ + +void* +cx_cvector(void* data, short int type, int length, int* newlength, short int* newtype) +{ + ngcomplex_t* cc = (ngcomplex_t*)data; + double* dd = (double*)data; + int i, len; + ngcomplex_t* d; + + NG_IGNORE(length); + + if (type == VF_REAL) + len = (int)fabs(*dd); + else + len = (int)cmag(*cc); + if (len == 0) + len = 1; + d = alloc_c(len); + *newlength = len; + *newtype = VF_COMPLEX; + for (i = 0; i < len; i++) { + realpart(d[i]) = i; + imagpart(d[i]) = 0; + } + return ((void*)d); +} + + /* Create a vector of the given length composed of all ones. */