diff --git a/gb.gsl/src/Makefile.am b/gb.gsl/src/Makefile.am index e81740bb2..715f0bd5d 100644 --- a/gb.gsl/src/Makefile.am +++ b/gb.gsl/src/Makefile.am @@ -8,4 +8,4 @@ gblib_LTLIBRARIES = gb.gsl.la gb_gsl_la_LIBADD = @GSL_LIB@ gb_gsl_la_LDFLAGS = -module @LD_FLAGS@ @GSL_LDFLAGS@ -gb_gsl_la_SOURCES = main.c main.h c_gsl.c c_gsl.h +gb_gsl_la_SOURCES = main.c main.h c_gsl.c c_gsl.h c_complex.c c_complex.h diff --git a/gb.gsl/src/c_complex.c b/gb.gsl/src/c_complex.c index d244f598a..953a7408c 100644 --- a/gb.gsl/src/c_complex.c +++ b/gb.gsl/src/c_complex.c @@ -1,29 +1,28 @@ /*************************************************************************** - gsl.c + gsl.c - gb.gsl component + gb.gsl component - (c) 2012 Randall Morgan + (c) 2012 Randall Morgan - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 1, or (at your option) - any later version. + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 1, or (at your option) + any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - MA 02110-1301, USA. + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + MA 02110-1301, USA. ***************************************************************************/ -#ifndef __C_GSL_COMPLEX_C #define __C_GSL_COMPLEX_C #include "c_complex.h" @@ -35,8 +34,37 @@ #include #include #include -#endif +static GSLCOMPLEX *create_complex() +{ + return (GSLCOMPLEX *)GB.New(GB.FindClass("Complex"), NULL, NULL); +} + +BEGIN_METHOD(GslComplex_new, GB_FLOAT real; GB_FLOAT imag) + + THIS->number.dat[0] = VARGOPT(real, 0.0); + THIS->number.dat[1] = VARGOPT(imag, 0.0); + +END_METHOD + +BEGIN_METHOD(GslComplex_call, GB_FLOAT real; GB_FLOAT imag) + + GSLCOMPLEX *c = create_complex(); + + c->number.dat[0] = VARG(real); + c->number.dat[1] = VARG(imag); + GB.ReturnObject(c); + +END_METHOD + +BEGIN_METHOD_VOID(GslComplex_Copy) + + GSLCOMPLEX *c = create_complex(); + + c->number = THIS->number; + GB.ReturnObject(c); + +END_METHOD BEGIN_PROPERTY(GslComplex_Real) @@ -58,39 +86,99 @@ BEGIN_PROPERTY(GslComplex_Imagined) END_PROPERTY /************************************************** - Elemntary math functions for complex numbers + Elemntary math functions for complex numbers **************************************************/ -BEGIN_METHOD(GslComplex_Add, GB_OBJECT x;) - GSLCOMPLEX *p = VARG(x); - GSLCOMPLEX *obj; +BEGIN_METHOD(GslComplex_Add, GB_OBJECT x) + + GSLCOMPLEX *x = VARG(x); + GSLCOMPLEX *obj; - // Create new object - obj = (GSLCOMPLEX *) GB.New(GB.FindClass("Complex"), NULL, NULL); + if (GB.CheckObject(x)) + return; + + // Create new object + obj = create_complex(); - // Add two complex numbers - obj->number = gsl_complex_add(THIS->number, p->number); + // Add two complex numbers + obj->number = gsl_complex_add(THIS->number, x->number); - return GB.ReturnObject(obj); + GB.ReturnObject(obj); END_METHOD +BEGIN_METHOD_VOID(GslComplex_ToString) + + char buffer[64]; + char *p; + char *str; + int len; + double real, imag; + + real = THIS->number.dat[0]; + imag = THIS->number.dat[1]; + + if (real == 0.0 && imag == 0.0) + { + GB.ReturnConstZeroString("0"); + return; + } + + p = buffer; + + if (real != 0.0) + { + GB.NumberToString(FALSE, real, NULL, &str, &len); + strncpy(p, str, len); + p += len; + } + + if (imag != 0.0) + { + if (imag < 0.0) + { + *p++ = '-'; + imag = (-imag); + } + else if (p != buffer) + *p++ = '+'; + + if (imag != 1.0) + { + GB.NumberToString(FALSE, imag, NULL, &str, &len); + strncpy(p, str, len); + p += len; + } + *p++ = 'i'; + } + + GB.ReturnNewString(buffer, p - buffer); + +END_METHOD + /************************************************** - Describe Class properties and methods to Gambas + Describe Class properties and methods to Gambas **************************************************/ GB_DESC CGslComplexDesc[] = { - GB_DECLARE("Complex", sizeof(GSLCOMPLEX)), + GB_DECLARE("Complex", sizeof(GSLCOMPLEX)), + + GB_METHOD("_new", NULL, GslComplex_new, "[(Real)f(Imag)f]"), + GB_STATIC_METHOD("_call", "Complex", GslComplex_call, "[(Real)f(Imag)f]"), + GB_METHOD("Copy", "Complex", GslComplex_Copy, NULL), - GB_PROPERTY("REAL", "f", GslComplex_Real), - GB_PROPERTY("IMAG", "f", GslComplex_Imagined), + GB_PROPERTY("Real", "f", GslComplex_Real), + GB_PROPERTY("Imag", "f", GslComplex_Imagined), + GB_PROPERTY("X", "f", GslComplex_Real), + GB_PROPERTY("Y", "f", GslComplex_Imagined), - // Operations on gsl_complex - GB_STATIC_METHOD("Add", "Complex", GslComplex_Add, "(x)Complex"), - - GB_END_DECLARE + // Operations on gsl_complex + GB_METHOD("Add", "Complex", GslComplex_Add, "(X)Complex"), + GB_METHOD("ToString", "s", GslComplex_ToString, NULL), + + GB_END_DECLARE };