diff --git a/gb.gsl/src/c_complex.c b/gb.gsl/src/c_complex.c index 569f03121..3f7e06763 100644 --- a/gb.gsl/src/c_complex.c +++ b/gb.gsl/src/c_complex.c @@ -50,6 +50,38 @@ CCOMPLEX *COMPLEX_push_complex(double value) return COMPLEX_create(gsl_complex_rect(0, value)); } +//---- Utility functions ---------------------------------------------------- + +int COMPLEX_get_value(GB_VALUE *value, double *x, gsl_complex *z) +{ + GB.Conv(value, value->_variant.value.type); + + if (value->type >= GB_T_OBJECT && GB.Is(value->_object.value, CLASS_Complex)) + { + CCOMPLEX *c = (CCOMPLEX *)(value->_object.value); + if (GB.CheckObject(c)) + return CGV_ERR; + *z = c->number; + if (GSL_IMAG(*z) == 0.0) + { + *x = GSL_REAL(*z); + return CGV_FLOAT; + } + else + return CGV_COMPLEX; + } + else + { + if (GB.Conv(value, GB_T_FLOAT)) + return CGV_ERR; + + *x = value->_float.value; + z->dat[0] = *x; + z->dat[1] = 0.0; + return CGV_FLOAT; + } +} + //---- Arithmetic operators ------------------------------------------------- static CCOMPLEX *_addf(CCOMPLEX *a, double f) diff --git a/gb.gsl/src/c_complex.h b/gb.gsl/src/c_complex.h index 5e0f7dd41..0f1642a37 100644 --- a/gb.gsl/src/c_complex.h +++ b/gb.gsl/src/c_complex.h @@ -42,12 +42,12 @@ typedef } CCOMPLEX; -/*typedef - struct { - GB_BASE ob; - GB_ARRAY_BASE array; - } - CCOMPLEXARRAY;*/ +enum +{ + CGV_ERR, + CGV_FLOAT, + CGV_COMPLEX +}; CCOMPLEX *COMPLEX_create(gsl_complex number); CCOMPLEX *COMPLEX_push_complex(double value); @@ -55,4 +55,6 @@ char *COMPLEX_to_string(gsl_complex number, bool local); #define COMPLEX_get(_c) ((_c) ? (_c)->number : COMPLEX_zero) +int COMPLEX_get_value(GB_VALUE *value, double *x, gsl_complex *z); + #endif /* __C_COMPLEX_H */ diff --git a/gb.gsl/src/c_polynomial.c b/gb.gsl/src/c_polynomial.c index e8c078256..150a96e34 100644 --- a/gb.gsl/src/c_polynomial.c +++ b/gb.gsl/src/c_polynomial.c @@ -116,7 +116,7 @@ static bool ensure_not_complex(CPOLYNOMIAL *_object) for (i = 0; i < size; i++) { - if (cd[i].dat[1] != 0.0) + if (GSL_IMAG(cd[i]) != 0.0) return TRUE; } @@ -133,43 +133,6 @@ static bool ensure_not_complex(CPOLYNOMIAL *_object) return FALSE; } -enum -{ - V_ERR, - V_FLOAT, - V_COMPLEX -}; - -static int get_value(GB_VALUE *value, double *x, gsl_complex *z) -{ - GB.Conv(value, value->_variant.value.type); - - if (value->type >= GB_T_OBJECT && GB.Is(value->_object.value, CLASS_Complex)) - { - CCOMPLEX *c = (CCOMPLEX *)(value->_object.value); - if (GB.CheckObject(c)) - return V_ERR; - *z = c->number; - if (GSL_IMAG(*z) == 0.0) - { - *x = GSL_REAL(*z); - return V_FLOAT; - } - else - return V_COMPLEX; - } - else - { - if (GB.Conv(value, GB_T_FLOAT)) - return V_ERR; - - *x = value->_float.value; - z->dat[0] = *x; - z->dat[1] = 0.0; - return V_FLOAT; - } -} - //---- Arithmetic operators ------------------------------------------------- #if 0 @@ -535,14 +498,14 @@ BEGIN_METHOD(Polynomial_put, GB_VARIANT value; GB_INTEGER index) return; } - type = get_value(value, &x, &z); + type = COMPLEX_get_value(value, &x, &z); - if (type == V_ERR) + if (type == CGV_ERR) return; ensure_size(THIS, index + 1); - if (type == V_COMPLEX) + if (type == CGV_COMPLEX) { ensure_complex(THIS); CDATA(THIS)[index] = z; @@ -565,8 +528,8 @@ BEGIN_METHOD(Polynomial_Eval, GB_VARIANT value) double x; gsl_complex z; - type = get_value(value, &x, &z); - if (type == V_ERR) + type = COMPLEX_get_value(value, &x, &z); + if (type == CGV_ERR) return; if (COMPLEX(THIS)) @@ -575,7 +538,7 @@ BEGIN_METHOD(Polynomial_Eval, GB_VARIANT value) } else { - if (type == V_COMPLEX) + if (type == CGV_COMPLEX) GB.ReturnObject(COMPLEX_create(gsl_poly_complex_eval(DATA(THIS), COUNT(THIS), z))); else GB.ReturnFloat(gsl_poly_eval(DATA(THIS), COUNT(THIS), x)); diff --git a/gb.gsl/src/c_vector.c b/gb.gsl/src/c_vector.c index 1cc3010c6..25391dbb2 100644 --- a/gb.gsl/src/c_vector.c +++ b/gb.gsl/src/c_vector.c @@ -28,12 +28,11 @@ #include "c_complex.h" #include "c_vector.h" -#define THIS ((GSLVECTOR *)_object) -#define VECTOR THIS->vector -#define VECTORC ((gsl_vector_complex *)VECTOR) - -#define SIZE(_ob) ((int)((GSLVECTOR *)_ob)->vector->size) - +#define THIS ((CVECTOR *)_object) +#define VEC(_v) ((gsl_vector *)(_v)->vector) +#define CVEC(_v) ((gsl_vector_complex *)(_v)->vector) +#define SIZE(_v) ((int)(VEC(_v)->size)) +#define COMPLEX(_v) ((_v)->complex) //---- Utility functions ---------------------------------------------- @@ -121,249 +120,90 @@ void gsl_vector_complex_negative(gsl_vector *a) //---- Vector creation ------------------------------------------------------ -static bool _do_not_init = FALSE; +//static bool _do_not_init = FALSE; -static GSLVECTOR *VECTOR_create(GB_TYPE type, int size, bool init) +static CVECTOR *VECTOR_create(int size, bool complex, bool init) { - GB_CLASS klass; - GSLVECTOR *v; - - if (type == GB_T_FLOAT) - klass = CLASS_FloatVector; - else if (type == CLASS_Complex) - klass = CLASS_ComplexVector; - else - return NULL; - - _do_not_init = TRUE; - v = (GSLVECTOR *)GB.New(klass, NULL, NULL); - - v->type = type; - - if (type == GB_T_FLOAT) - v->vector = init ? gsl_vector_calloc(size) : gsl_vector_alloc(size); - else - v->vector = (gsl_vector *)(init ? gsl_vector_complex_calloc(size) : gsl_vector_complex_alloc(size)); - - return v; + GB.Push(2, GB_T_INTEGER, size, GB_T_BOOLEAN, complex); + return (CVECTOR *)GB.New(CLASS_Vector, NULL, (void *)(intptr_t)2); } -static GSLVECTOR *VECTOR_copy(GSLVECTOR *_object) +static CVECTOR *VECTOR_copy(CVECTOR *_object) { - GSLVECTOR *copy = VECTOR_create(THIS->type, (int)VECTOR->size, FALSE); - if (THIS->type == GB_T_FLOAT) - gsl_vector_memcpy(copy->vector, VECTOR); + CVECTOR *copy = VECTOR_create(SIZE(THIS), COMPLEX(THIS), FALSE); + if (COMPLEX(THIS)) + gsl_vector_memcpy(VEC(copy), VEC(THIS)); else - gsl_vector_complex_memcpy((gsl_vector_complex *)copy->vector, VECTORC); + gsl_vector_complex_memcpy(CVEC(copy), CVEC(THIS)); return copy; } -static GSLVECTOR *VECTOR_convert_to_complex(GSLVECTOR *_object) +static CVECTOR *VECTOR_convert_to_complex(CVECTOR *_object) { - GSLVECTOR *v = VECTOR_create(CLASS_Complex, SIZE(THIS), FALSE); + CVECTOR *v = VECTOR_create(SIZE(THIS), TRUE, FALSE); int i; for (i = 0; i < SIZE(THIS); i++) - gsl_vector_complex_set((gsl_vector_complex *)v->vector, i, gsl_complex_rect(gsl_vector_get(VECTOR, i), 0)); + gsl_vector_complex_set((gsl_vector_complex *)v->vector, i, gsl_complex_rect(gsl_vector_get(VEC(THIS), i), 0)); return v; } -//---- Arithmetic operators ------------------------------------------------- - -#if 0 -static GSLVECTOR *_addf(GSLVECTOR *a, double f) -{ - GSLVECTOR *r = VECTOR_copy(a); - if (r->type == GB_T_FLOAT) - gsl_vector_add_constant(r->vector, f); - else - gsl_vector_complex_add_constant((gsl_vector_complex *)r->vector, gsl_complex_rect(f, 0)); - return r; -} - -static GSLVECTOR *_add(GSLVECTOR *a, GSLVECTOR *b) -{ - GSLVECTOR *r = VECTOR_copy(a); - - if (r->type == GB_T_FLOAT) - gsl_vector_add(r->vector, b->vector); - else - gsl_vector_complex_add((gsl_vector_complex *)r->vector, (gsl_vector_complex *)b->vector); - - return r; -} - -static GSLVECTOR *_subf(GSLVECTOR *a, double f) -{ - GSLVECTOR *r = VECTOR_copy(a); - if (r->type == GB_T_FLOAT) - gsl_vector_add_constant(r->vector, -f); - else - gsl_vector_complex_add_constant((gsl_vector_complex *)r->vector, gsl_complex_rect(-f, 0)); - return r; -} - -static GSLVECTOR *_sub(GSLVECTOR *a, GSLVECTOR *b) -{ - GSLVECTOR *r = VECTOR_copy(a); - - if (r->type == GB_T_FLOAT) - gsl_vector_sub(r->vector, b->vector); - else - gsl_vector_complex_sub((gsl_vector_complex *)r->vector, (gsl_vector_complex *)b->vector); - - return r; -} - -static GSLVECTOR *_mulf(GSLVECTOR *a, double f) -{ - GSLVECTOR *r = VECTOR_copy(a); - - if (r->type == GB_T_FLOAT) - gsl_vector_scale(r->vector, f); - else - gsl_vector_complex_scale((gsl_vector_complex *)r->vector, gsl_complex_rect(f, 0)); - - return r; -} - -static GSLVECTOR *_mul(GSLVECTOR *a, GSLVECTOR *b) -{ - GSLVECTOR *r = VECTOR_copy(a); - - if (r->type == GB_T_FLOAT) - gsl_vector_mul(r->vector, b->vector); - else - gsl_vector_complex_mul((gsl_vector_complex *)r->vector, (gsl_vector_complex *)b->vector); - - return r; -} - -static GSLVECTOR *_divf(GSLVECTOR *a, double f) -{ - GSLVECTOR *r; - - if (f == 0.0) - return NULL; - - r = VECTOR_copy(a); - if (r->type == GB_T_FLOAT) - gsl_vector_scale(r->vector, 1 / f); - else - gsl_vector_complex_scale((gsl_vector_complex *)r->vector, gsl_complex_rect(1 / f, 0)); - - return r; -} - -static GSLVECTOR *_idivf(GSLVECTOR *a, double f) -{ - GSLVECTOR *r; - - if (a->type == GB_T_FLOAT) - { - if (gsl_vector_has_zero(a->vector)) - return NULL; - } - else - { - if (gsl_vector_complex_has_zero((gsl_vector_complex *)a->vector)) - return NULL; - } - - r = VECTOR_copy(a); - - if (a->type == GB_T_FLOAT) - { - gsl_vector_inverse(r->vector); - gsl_vector_scale(r->vector, f); - } - else - { - gsl_vector_complex_inverse((gsl_vector_complext *)r->vector); - gsl_vector_complex_scale((gsl_vector_complex *)r->vector, gsl_complex_rect(f, 0)); - } - - return r; -} - -static GSLVECTOR *_div(GSLVECTOR *a, GSLVECTOR *b) -{ - GSLVECTOR *r; - - if (a->type == GB_T_FLOAT) - { - if (gsl_vector_has_zero(a->vector)) - return NULL; - } - else - { - if (gsl_vector_complex_has_zero((gsl_vector_complex *)a->vector)) - return NULL; - } - - r = VECTOR_copy(a); - gsl_vector_div(r->vector, b->vector); - return r; -} - -static int _equal(GSLVECTOR *a, GSLVECTOR *b) -{ - return gsl_vector_equal(a->vector, b->vector); -} - -static int _equalf(GSLVECTOR *a, double f) +static void ensure_complex(CVECTOR *_object) { + gsl_vector_complex *v; + int size = SIZE(THIS); int i; - int size = (int)a->vector->size; + + if (COMPLEX(THIS)) + return; + + v = gsl_vector_complex_alloc(size); + for (i = 0; i < size; i++) + gsl_vector_complex_set(v, i, gsl_complex_rect(gsl_vector_get(VEC(THIS), i), 0)); + + gsl_vector_free(VEC(THIS)); + THIS->vector = v; + THIS->complex = TRUE; +} + + +/*static bool ensure_not_complex(CVECTOR *_object) +{ + gsl_vector *v; + int size = SIZE(THIS); + int i; + gsl_complex c; + + if (!COMPLEX(THIS)) + return FALSE; for (i = 0; i < size; i++) { - if (gsl_vector_get(a->vector, i) != f) - return FALSE; + c = gsl_vector_complex_get(CVEC(THIS), i); + if (GSL_IMAG(c) != 0.0) + return TRUE; } - return TRUE; -} - -static GSLVECTOR *_neg(GSLVECTOR *a) -{ - GSLVECTOR *r = VECTOR_copy(a); - gsl_vector_negative(r->vector); - return r; -} - -static double _abs(GSLVECTOR *a) -{ - return gsl_blas_dnrm2(a->vector); -} - -static GB_OPERATOR_DESC _operators = -{ - add: (void *)_add, - addf: (void *)_addf, - sub: (void *)_sub, - subf: (void *)_subf, - mul: (void *)_mul, - mulf: (void *)_mulf, - div: (void *)_div, - divf: (void *)_divf, - idivf: (void *)_idivf, - equal: (void *)_equal, - equalf: (void *)_equalf, - abs: (void *)_abs, - neg: (void *)_neg -}; -#endif + v = gsl_vector_alloc(size); + + for (i = 0; i < size; i++) + gsl_vector_set(v, i, GSL_REAL(gsl_vector_complex_get(CVEC(THIS), i))); + + gsl_vector_complex_free(CVEC(THIS)); + THIS->vector = v; + THIS->complex = FALSE; + return FALSE; +}*/ //---- Conversions ---------------------------------------------------------- -static char *_to_string(GSLVECTOR *_object, bool local) +static char *_to_string(CVECTOR *_object, bool local) { char *result = NULL; int i; - int size = (int)THIS->vector->size; + int size = SIZE(THIS); char *str; int len; @@ -374,14 +214,14 @@ static char *_to_string(GSLVECTOR *_object, bool local) if (i) result = GB.AddChar(result, ' '); - if (THIS->type == GB_T_FLOAT) + if (!COMPLEX(THIS)) { - GB.NumberToString(local, gsl_vector_get(VECTOR, i), NULL, &str, &len); + GB.NumberToString(local, gsl_vector_get(VEC(THIS), i), NULL, &str, &len); result = GB.AddString(result, str, len); } else { - str = COMPLEX_to_string(gsl_vector_complex_get(VECTORC, i), local); + str = COMPLEX_to_string(gsl_vector_complex_get(CVEC(THIS), i), local); result = GB.AddString(result, str, GB.StringLength(str)); GB.FreeString(&str); } @@ -392,30 +232,30 @@ static char *_to_string(GSLVECTOR *_object, bool local) return result; } -static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) +static bool _convert(CVECTOR *_object, GB_TYPE type, GB_VALUE *conv) { if (THIS) { - if (THIS->type == GB_T_FLOAT) + if (!COMPLEX(THIS)) { switch (type) { case GB_T_FLOAT: - conv->_float.value = gsl_blas_dnrm2(VECTOR); + conv->_float.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_SINGLE: - conv->_single.value = gsl_blas_dnrm2(VECTOR); + conv->_single.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_INTEGER: case GB_T_SHORT: case GB_T_BYTE: - conv->_integer.value = gsl_blas_dnrm2(VECTOR); + conv->_integer.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_LONG: - conv->_long.value = gsl_blas_dnrm2(VECTOR); + conv->_long.value = gsl_blas_dnrm2(VEC(THIS)); return FALSE; case GB_T_STRING: @@ -426,13 +266,6 @@ static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) return FALSE; default: - - if (type == CLASS_ComplexVector) - { - conv->_object.value = VECTOR_convert_to_complex(THIS); - return FALSE; - } - return TRUE; } } @@ -441,21 +274,21 @@ static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) switch (type) { case GB_T_FLOAT: - conv->_float.value = gsl_blas_dznrm2(VECTORC); + conv->_float.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_SINGLE: - conv->_single.value = gsl_blas_dznrm2(VECTORC); + conv->_single.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_INTEGER: case GB_T_SHORT: case GB_T_BYTE: - conv->_integer.value = gsl_blas_dznrm2(VECTORC); + conv->_integer.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_LONG: - conv->_long.value = gsl_blas_dznrm2(VECTORC); + conv->_long.value = gsl_blas_dznrm2(CVEC(THIS)); return FALSE; case GB_T_STRING: @@ -476,7 +309,7 @@ static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) { GB_ARRAY array = (GB_ARRAY)conv->_object.value; int size = GB.Array.Count(array); - GSLVECTOR *v; + CVECTOR *v; int i; GB_VALUE temp; void *data; @@ -484,14 +317,35 @@ static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) if (atype > GB_T_BOOLEAN && atype <= GB_T_FLOAT) { - v = VECTOR_create(GB_T_FLOAT, size, FALSE); + v = VECTOR_create(size, FALSE, FALSE); for (i = 0; i < size; i++) { data = GB.Array.Get(array, i); GB.ReadValue(&temp, data, atype); GB.Conv(&temp, GB_T_FLOAT); - gsl_vector_set(v->vector, i, temp._float.value); + gsl_vector_set(VEC(v), i, temp._float.value); + } + + conv->_object.value = v; + return FALSE; + } + else if (atype == GB_T_VARIANT) + { + CCOMPLEX *c; + v = VECTOR_create(size, TRUE, FALSE); + + for (i = 0; i < size; i++) + { + GB.ReadValue(&temp, GB.Array.Get(array, i), atype); + GB.BorrowValue(&temp); + GB.Conv(&temp, CLASS_Complex); + c = temp._object.value; + if (c) + gsl_vector_complex_set(CVEC(v), i, c->number); + else + gsl_vector_complex_set(CVEC(v), i, COMPLEX_zero); + GB.ReleaseValue(&temp); } conv->_object.value = v; @@ -500,7 +354,7 @@ static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) else if (atype == CLASS_Complex) { CCOMPLEX *c; - v = VECTOR_create(atype, size, FALSE); + v = VECTOR_create(size, TRUE, FALSE); for (i = 0; i < size; i++) { @@ -518,8 +372,8 @@ static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) else if (type == CLASS_Complex) { CCOMPLEX *c = (CCOMPLEX *)conv->_object.value; - GSLVECTOR *v = VECTOR_create(type, 1, FALSE); - gsl_vector_complex_set((gsl_vector_complex *)v->vector, 0, c->number); + CVECTOR *v = VECTOR_create(1, TRUE, FALSE); + gsl_vector_complex_set(CVEC(v), 0, c->number); conv->_object.value = v; return FALSE; } @@ -530,27 +384,47 @@ static bool _convert(GSLVECTOR *_object, GB_TYPE type, GB_VALUE *conv) //--------------------------------------------------------------------------- +BEGIN_METHOD(Vector_new, GB_INTEGER size; GB_BOOLEAN complex) + + bool complex = VARGOPT(complex, FALSE); + int size = VARGOPT(size, 1); + + if (size < 1) size = 1; + + THIS->complex = complex; + + if (!complex) + THIS->vector = gsl_vector_calloc(size); + else + THIS->vector = gsl_vector_complex_calloc(size); + +END_METHOD + + BEGIN_METHOD_VOID(Vector_free) - if (THIS->type == GB_T_FLOAT) - gsl_vector_free(VECTOR); + if (!COMPLEX(THIS)) + gsl_vector_free(VEC(THIS)); else - gsl_vector_complex_free(VECTORC); + gsl_vector_complex_free(CVEC(THIS)); END_METHOD + BEGIN_PROPERTY(Vector_Count) GB.ReturnInteger(SIZE(THIS)); END_PROPERTY + BEGIN_METHOD_VOID(Vector_Copy) GB.ReturnObject(VECTOR_copy(THIS)); END_METHOD + BEGIN_METHOD(Vector_get, GB_INTEGER index) int size = SIZE(THIS); @@ -562,93 +436,100 @@ BEGIN_METHOD(Vector_get, GB_INTEGER index) return; } - if (THIS->type == GB_T_FLOAT) - GB.ReturnFloat(gsl_vector_get(VECTOR, index)); + if (!COMPLEX(THIS)) + GB.ReturnFloat(gsl_vector_get(VEC(THIS), index)); else - GB.ReturnObject(COMPLEX_create(gsl_vector_complex_get(VECTORC, index))); + GB.ReturnObject(COMPLEX_create(gsl_vector_complex_get(CVEC(THIS), index))); GB.ReturnConvVariant(); END_METHOD + BEGIN_METHOD(Vector_put, GB_VARIANT value; GB_INTEGER index) - int size = SIZE(THIS); int index = VARG(index); + int size = SIZE(THIS); + GB_VALUE *value = (GB_VALUE *)ARG(value); + int type; + gsl_complex z; + double x; - if (index < 0 || index >= size) + if (index < 0 || index > size) { GB.Error(GB_ERR_ARG); return; } - GB.Conv((GB_VALUE *)ARG(value), THIS->type); + type = COMPLEX_get_value(value, &x, &z); - if (THIS->type == GB_T_FLOAT) - gsl_vector_set(VECTOR, index, ((GB_FLOAT *)ARG(value))->value); + if (type == CGV_ERR) + return; + + if (type == CGV_COMPLEX) + { + ensure_complex(THIS); + gsl_vector_complex_set(CVEC(THIS), index, z); + } else { - CCOMPLEX *c = (CCOMPLEX *)((GB_OBJECT *)ARG(value))->value; - if (GB.CheckObject(c)) - gsl_vector_complex_set(VECTORC, index, gsl_complex_rect(0, 0)); + if (COMPLEX(THIS)) + gsl_vector_complex_set(CVEC(THIS), index, z); else - gsl_vector_complex_set(VECTORC, index, c->number); + gsl_vector_set(VEC(THIS), index, x); } - + END_METHOD + BEGIN_METHOD(Vector_Scale, GB_VALUE value) - GB_VALUE *value = ARG(value); - GSLVECTOR *v; + GB_VALUE *value = (GB_VALUE *)ARG(value); + int type; + gsl_complex z; + double x; - if (value->type == GB_T_VARIANT) - GB.Conv(value, value->_variant.value.type); + type = COMPLEX_get_value(value, &x, &z); - if (THIS->type == GB_T_FLOAT && value->type < GB_T_OBJECT) + if (type == CGV_ERR) + return; + + if (type == CGV_COMPLEX) { - GB.Conv(value, GB_T_FLOAT); - v = VECTOR_copy(THIS); - gsl_vector_scale(v->vector, value->_float.value); + ensure_complex(THIS); + gsl_vector_complex_scale(CVEC(THIS), z); } else { - CCOMPLEX *c; - - GB.Conv(value, CLASS_Complex); - c = (CCOMPLEX *)value->_object.value; - - if (THIS->type == GB_T_FLOAT) - v = VECTOR_convert_to_complex(THIS); + if (COMPLEX(THIS)) + gsl_vector_complex_scale(CVEC(THIS), z); else - v = VECTOR_copy(THIS); - - gsl_vector_complex_scale((gsl_vector_complex *)v->vector, c->number); + gsl_vector_scale(VEC(THIS), x); } - - GB.ReturnObject(v); + + GB.ReturnObject(THIS); END_METHOD -static void do_dot(GSLVECTOR *_object, GSLVECTOR *v, bool conj) +static void do_dot(CVECTOR *_object, CVECTOR *v, bool conj) { bool ca, cb; if (GB.CheckObject(v)) return; - ca = THIS->type == GB_T_FLOAT; - cb = v->type == GB_T_FLOAT; + ca = !COMPLEX(THIS); + cb = !COMPLEX(v); if (ca && cb) { double result; - gsl_blas_ddot(VECTOR, v->vector, &result); + gsl_blas_ddot(VEC(THIS), v->vector, &result); GB.ReturnFloat(result); } else { - GSLVECTOR *a, *b; + CVECTOR *a, *b; gsl_complex result; if (ca) @@ -662,9 +543,9 @@ static void do_dot(GSLVECTOR *_object, GSLVECTOR *v, bool conj) b = v; if (conj) - gsl_blas_zdotc((gsl_vector_complex *)a->vector, (gsl_vector_complex *)b->vector, &result); + gsl_blas_zdotc(CVEC(a), CVEC(b), &result); else - gsl_blas_zdotu((gsl_vector_complex *)a->vector, (gsl_vector_complex *)b->vector, &result); + gsl_blas_zdotu(CVEC(a), CVEC(b), &result); GB.ReturnObject(COMPLEX_create(result)); @@ -689,16 +570,16 @@ END_METHOD BEGIN_METHOD_VOID(Vector_Norm) - if (THIS->type == GB_T_FLOAT) - GB.ReturnFloat(gsl_blas_dnrm2(VECTOR)); + if (!COMPLEX(THIS)) + GB.ReturnFloat(gsl_blas_dnrm2(VEC(THIS))); else - GB.ReturnFloat(gsl_blas_dznrm2(VECTORC)); + GB.ReturnFloat(gsl_blas_dznrm2(CVEC(THIS))); END_METHOD BEGIN_METHOD(Vector_Equal, GB_OBJECT vector) - GSLVECTOR *v = VARG(vector); + CVECTOR *v = VARG(vector); bool ca, cb; if (GB.CheckObject(v)) @@ -710,16 +591,16 @@ BEGIN_METHOD(Vector_Equal, GB_OBJECT vector) return; } - ca = THIS->type == GB_T_FLOAT; - cb = v->type == GB_T_FLOAT; + ca = !COMPLEX(THIS); + cb = !COMPLEX(v); if (ca && cb) { - GB.ReturnBoolean(gsl_vector_equal(VECTOR, v->vector)); + GB.ReturnBoolean(gsl_vector_equal(VEC(THIS), VEC(v))); } else { - GSLVECTOR *a, *b; + CVECTOR *a, *b; if (ca) a = VECTOR_convert_to_complex(THIS); @@ -731,7 +612,7 @@ BEGIN_METHOD(Vector_Equal, GB_OBJECT vector) else b = v; - GB.ReturnBoolean(gsl_vector_complex_equal((gsl_vector_complex *)a->vector, (gsl_vector_complex *)b->vector)); + GB.ReturnBoolean(gsl_vector_complex_equal(CVEC(a), CVEC(b))); if (ca) GB.Unref(POINTER(&a)); if (cb) GB.Unref(POINTER(&b)); @@ -742,108 +623,16 @@ END_METHOD BEGIN_PROPERTY(Vector_Handle) - GB.ReturnPointer(VECTOR); + GB.ReturnPointer(THIS->vector); END_PROPERTY -//--------------------------------------------------------------------------- - -BEGIN_METHOD(FloatVector_new, GB_INTEGER size) - - if (_do_not_init) - _do_not_init = FALSE; - else - { - THIS->type = GB_T_FLOAT; - THIS->vector = gsl_vector_calloc(VARGOPT(size, 1)); - } - -END_METHOD - -BEGIN_METHOD(FloatVector_get, GB_INTEGER index) - - int size = SIZE(THIS); - int index = VARG(index); - - if (index < 0 || index >= size) - { - GB.Error(GB_ERR_ARG); - return; - } - - GB.ReturnFloat(gsl_vector_get(VECTOR, index)); - -END_METHOD - -/*BEGIN_METHOD(FloatVector_put, GB_FLOAT value; GB_INTEGER index) - - int size = SIZE(THIS); - int index = VARG(index); - - if (index < 0 || index >= size) - { - GB.Error(GB_ERR_ARG); - return; - } - - gsl_vector_set(VECTOR, index, VARG(value)); - -END_METHOD*/ - -//--------------------------------------------------------------------------- - -BEGIN_METHOD(ComplexVector_new, GB_INTEGER size) - - if (_do_not_init) - _do_not_init = FALSE; - else - { - THIS->type = CLASS_Complex; - THIS->vector = (gsl_vector *)gsl_vector_complex_calloc(VARGOPT(size, 1)); - } - -END_METHOD - -BEGIN_METHOD(ComplexVector_get, GB_INTEGER index) - - int size = SIZE(THIS); - int index = VARG(index); - - if (index < 0 || index >= size) - { - GB.Error(GB_ERR_ARG); - return; - } - - GB.ReturnObject(COMPLEX_create(gsl_vector_complex_get(VECTORC, index))); - -END_METHOD - -/*BEGIN_METHOD(ComplexVector_put, GB_OBJECT value; GB_INTEGER index) - - int size = SIZE(THIS); - int index = VARG(index); - CCOMPLEX *value; - - if (index < 0 || index >= size) - { - GB.Error(GB_ERR_ARG); - return; - } - - value = (CCOMPLEX *)VARG(value); - if (GB.CheckObject(value)) - return; - - gsl_vector_complex_set(VECTORC, index, value->number); - -END_METHOD*/ GB_DESC VectorDesc[] = { - GB_DECLARE("Vector", sizeof(GSLVECTOR)), GB_NOT_CREATABLE(), + GB_DECLARE("Vector", sizeof(CVECTOR)), - //GB_METHOD("_new", NULL, Vector_new, "[(Size)i]"), + GB_METHOD("_new", NULL, Vector_new, "[(Size)i(Complex)b]"), GB_METHOD("_free", NULL, Vector_free, NULL), //GB_STATIC_METHOD("_call", "Vector", Vector_call, "(Value)f."), GB_METHOD("Copy", "Vector", Vector_Copy, NULL), @@ -865,9 +654,9 @@ GB_DESC VectorDesc[] = GB_END_DECLARE }; -GB_DESC FloatVectorDesc[] = +/*GB_DESC FloatVectorDesc[] = { - GB_DECLARE("FloatVector", sizeof(GSLVECTOR)), + GB_DECLARE("FloatVector", sizeof(CVECTOR)), GB_INHERITS("Vector"), GB_METHOD("_new", NULL, FloatVector_new, "[(Size)i]"), @@ -880,7 +669,7 @@ GB_DESC FloatVectorDesc[] = GB_DESC ComplexVectorDesc[] = { - GB_DECLARE("ComplexVector", sizeof(GSLVECTOR)), + GB_DECLARE("ComplexVector", sizeof(CVECTOR)), GB_INHERITS("Vector"), GB_METHOD("_new", NULL, ComplexVector_new, "[(Size)i]"), @@ -889,4 +678,4 @@ GB_DESC ComplexVectorDesc[] = GB_METHOD("_put", NULL, Vector_put, "(Value)Complex(Index)i"), GB_END_DECLARE -}; +};*/ diff --git a/gb.gsl/src/c_vector.h b/gb.gsl/src/c_vector.h index 487ec75a5..b30edfa25 100644 --- a/gb.gsl/src/c_vector.h +++ b/gb.gsl/src/c_vector.h @@ -30,17 +30,15 @@ MA 02110-1301, USA. #ifndef __C_VECTOR_C extern GB_DESC VectorDesc[]; -extern GB_DESC FloatVectorDesc[]; -extern GB_DESC ComplexVectorDesc[]; #endif typedef struct { GB_BASE ob; - GB_TYPE type; - gsl_vector *vector; + void *vector; + bool complex; } - GSLVECTOR; + CVECTOR; #endif /* __C_VECTOR_H */ diff --git a/gb.gsl/src/main.c b/gb.gsl/src/main.c index bd66e7dbe..54d031c6f 100644 --- a/gb.gsl/src/main.c +++ b/gb.gsl/src/main.c @@ -43,8 +43,6 @@ GB_DESC *GB_CLASSES[] EXPORT = CGslDesc, /* The Elementary math functions */ ComplexDesc, VectorDesc, - FloatVectorDesc, - ComplexVectorDesc, PolynomialDesc, /* Other classes go here as completed */ NULL // Must have a null entry for the end of the structure @@ -52,8 +50,6 @@ GB_DESC *GB_CLASSES[] EXPORT = GB_CLASS CLASS_Complex; GB_CLASS CLASS_Vector; -GB_CLASS CLASS_FloatVector; -GB_CLASS CLASS_ComplexVector; GB_CLASS CLASS_Polynomial; static void error_handler(const char *reason, const char *file, int line, int gsl_errno) @@ -66,8 +62,6 @@ int EXPORT GB_INIT(void) { CLASS_Complex = GB.FindClass("Complex"); CLASS_Vector = GB.FindClass("Vector"); - CLASS_FloatVector = GB.FindClass("FloatVector"); - CLASS_ComplexVector = GB.FindClass("ComplexVector"); CLASS_Polynomial = GB.FindClass("Polynomial"); gsl_set_error_handler(error_handler); @@ -94,3 +88,4 @@ int EXPORT GB_INFO(const char *key, void **value) #ifdef _cpluscplus } #endif +