From 21dac9f92df618e0f9bf5f1d4b7be81f3203c794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Minisini?= Date: Tue, 14 Feb 2012 08:06:09 +0000 Subject: [PATCH] [GB.GSL] * BUG: Update Makefile.am with new source files. * NEW: Add a complex dynamic constructor. * NEW: Add a complex static constructor _call(). * NEW: Add Complex.Copy(). * NEW: Add Complex.ToString(). git-svn-id: svn://localhost/gambas/trunk@4478 867c0c6c-44f3-4631-809d-bfa615b0a4ec --- gb.gsl/src/Makefile.am | 2 +- gb.gsl/src/c_complex.c | 156 ++++++++++++++++++++++++++++++++--------- 2 files changed, 123 insertions(+), 35 deletions(-) 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 };