[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
This commit is contained in:
parent
d0757716c7
commit
21dac9f92d
2 changed files with 123 additions and 35 deletions
|
@ -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
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
|
||||
***************************************************************************/
|
||||
|
||||
#ifndef __C_GSL_COMPLEX_C
|
||||
#define __C_GSL_COMPLEX_C
|
||||
|
||||
#include "c_complex.h"
|
||||
|
@ -35,8 +34,37 @@
|
|||
#include <gsl/gsl_complex_math.h>
|
||||
#include <gsl/gsl_sf.h>
|
||||
#include <stdio.h>
|
||||
#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)
|
||||
|
||||
|
@ -61,21 +89,74 @@ END_PROPERTY
|
|||
Elemntary math functions for complex numbers
|
||||
**************************************************/
|
||||
|
||||
BEGIN_METHOD(GslComplex_Add, GB_OBJECT x;)
|
||||
GSLCOMPLEX *p = VARG(x);
|
||||
BEGIN_METHOD(GslComplex_Add, GB_OBJECT x)
|
||||
|
||||
GSLCOMPLEX *x = VARG(x);
|
||||
GSLCOMPLEX *obj;
|
||||
|
||||
if (GB.CheckObject(x))
|
||||
return;
|
||||
|
||||
// Create new object
|
||||
obj = (GSLCOMPLEX *) GB.New(GB.FindClass("Complex"), NULL, NULL);
|
||||
obj = create_complex();
|
||||
|
||||
// Add two complex numbers
|
||||
obj->number = gsl_complex_add(THIS->number, p->number);
|
||||
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
|
||||
|
@ -84,11 +165,18 @@ GB_DESC CGslComplexDesc[] =
|
|||
{
|
||||
GB_DECLARE("Complex", sizeof(GSLCOMPLEX)),
|
||||
|
||||
GB_PROPERTY("REAL", "f", GslComplex_Real),
|
||||
GB_PROPERTY("IMAG", "f", GslComplex_Imagined),
|
||||
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("X", "f", GslComplex_Real),
|
||||
GB_PROPERTY("Y", "f", GslComplex_Imagined),
|
||||
|
||||
// Operations on gsl_complex
|
||||
GB_STATIC_METHOD("Add", "Complex", GslComplex_Add, "(x)Complex"),
|
||||
GB_METHOD("Add", "Complex", GslComplex_Add, "(X)Complex"),
|
||||
GB_METHOD("ToString", "s", GslComplex_ToString, NULL),
|
||||
|
||||
GB_END_DECLARE
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue