| 1 |
/* |
| 2 |
** cmath.c - Math module with complex numbers |
| 3 |
** |
| 4 |
** See Copyright Notice in mruby.h |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
** This `mruby-cmath` gem uses C99 _Complex features |
| 9 |
** You need C compiler that support C99+ |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <mruby.h> |
| 13 |
|
| 14 |
#ifdef MRB_NO_FLOAT |
| 15 |
# error CMath conflicts with 'MRB_NO_FLOAT' configuration |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <complex.h> |
| 19 |
|
| 20 |
mrb_value mrb_complex_new(mrb_state *mrb, mrb_float real, mrb_float imag); |
| 21 |
void mrb_complex_get(mrb_state *mrb, mrb_value cpx, mrb_float*, mrb_float*); |
| 22 |
|
| 23 |
static mrb_bool |
| 24 |
cmath_get_complex(mrb_state *mrb, mrb_value c, mrb_float *r, mrb_float *i) |
| 25 |
{ |
| 26 |
if (mrb_integer_p(c)) { |
| 27 |
*r = (mrb_float)mrb_integer(c); |
| 28 |
*i = 0; |
| 29 |
return FALSE; |
| 30 |
} |
| 31 |
else if (mrb_float_p(c)) { |
| 32 |
*r = mrb_float(c); |
| 33 |
*i = 0; |
| 34 |
return FALSE; |
| 35 |
} |
| 36 |
else if (mrb_type(c) == MRB_TT_COMPLEX) { |
| 37 |
mrb_complex_get(mrb, c, r, i); |
| 38 |
return TRUE; |
| 39 |
} |
| 40 |
else { |
| 41 |
mrb_raise(mrb, E_TYPE_ERROR, "Numeric required"); |
| 42 |
return FALSE; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
#ifdef MRB_USE_FLOAT32 |
| 47 |
#define F(x) x##f |
| 48 |
#else |
| 49 |
#define F(x) x |
| 50 |
#endif |
| 51 |
|
| 52 |
#if defined(_WIN32) && !defined(__MINGW32__) |
| 53 |
|
| 54 |
#ifdef MRB_USE_FLOAT32 |
| 55 |
typedef _Fcomplex mrb_complex; |
| 56 |
#define CX(r,i) _FCbuild(r,i) |
| 57 |
#else |
| 58 |
typedef _Dcomplex mrb_complex; |
| 59 |
#define CX(r,i) _Cbuild(r,i) |
| 60 |
#endif |
| 61 |
|
| 62 |
static mrb_complex |
| 63 |
CXDIVf(mrb_complex x, mrb_float y) |
| 64 |
{ |
| 65 |
return CX(creal(x)/y, cimag(x)/y); |
| 66 |
} |
| 67 |
|
| 68 |
static mrb_complex |
| 69 |
CXDIVc(mrb_complex a, mrb_complex b) |
| 70 |
{ |
| 71 |
mrb_float ratio, den; |
| 72 |
mrb_float abr, abi, cr, ci; |
| 73 |
|
| 74 |
if ((abr = creal(b)) < 0) |
| 75 |
abr = - abr; |
| 76 |
if ((abi = cimag(b)) < 0) |
| 77 |
abi = - abi; |
| 78 |
if (abr <= abi) { |
| 79 |
ratio = creal(b) / cimag(b); |
| 80 |
den = cimag(a) * (1 + ratio*ratio); |
| 81 |
cr = (creal(a)*ratio + cimag(a)) / den; |
| 82 |
ci = (cimag(a)*ratio - creal(a)) / den; |
| 83 |
} |
| 84 |
else { |
| 85 |
ratio = cimag(b) / creal(b); |
| 86 |
den = creal(a) * (1 + ratio*ratio); |
| 87 |
cr = (creal(a) + cimag(a)*ratio) / den; |
| 88 |
ci = (cimag(a) - creal(a)*ratio) / den; |
| 89 |
} |
| 90 |
return CX(cr, ci); |
| 91 |
} |
| 92 |
|
| 93 |
#else |
| 94 |
|
| 95 |
#if defined(__cplusplus) && (defined(__APPLE__) || (defined(__clang__) && (defined(__FreeBSD__) || defined(__OpenBSD__)))) |
| 96 |
|
| 97 |
#ifdef MRB_USE_FLOAT32 |
| 98 |
typedef std::complex<float> mrb_complex; |
| 99 |
#else |
| 100 |
typedef std::complex<double> mrb_complex; |
| 101 |
#endif /* MRB_USE_FLOAT32 */ |
| 102 |
|
| 103 |
#define CX(r,i) mrb_complex(r,i) |
| 104 |
#define creal(c) c.real() |
| 105 |
#define cimag(c) c.imag() |
| 106 |
#define FC(n) F(n) |
| 107 |
|
| 108 |
#else /* cpp */ |
| 109 |
|
| 110 |
#ifdef MRB_USE_FLOAT32 |
| 111 |
typedef float _Complex mrb_complex; |
| 112 |
#else |
| 113 |
typedef double _Complex mrb_complex; |
| 114 |
#endif /* MRB_USE_FLOAT32 */ |
| 115 |
|
| 116 |
#define CX(r,i) ((r)+(i)*_Complex_I) |
| 117 |
#endif |
| 118 |
|
| 119 |
#define CXDIVf(x,y) (x)/(y) |
| 120 |
#define CXDIVc(x,y) (x)/(y) |
| 121 |
|
| 122 |
#endif |
| 123 |
|
| 124 |
#ifndef FC |
| 125 |
#define FC(n) F(c ## n) |
| 126 |
#endif |
| 127 |
|
| 128 |
#define DEF_CMATH_METHOD(name) \ |
| 129 |
static mrb_value \ |
| 130 |
cmath_ ## name(mrb_state *mrb, mrb_value self)\ |
| 131 |
{\ |
| 132 |
mrb_value z = mrb_get_arg1(mrb);\ |
| 133 |
mrb_float real, imag;\ |
| 134 |
if (cmath_get_complex(mrb, z, &real, &imag)) {\ |
| 135 |
mrb_complex c = CX(real,imag);\ |
| 136 |
c = FC(name)(c);\ |
| 137 |
return mrb_complex_new(mrb, creal(c), cimag(c));\ |
| 138 |
}\ |
| 139 |
return mrb_float_value(mrb, F(name)(real));\ |
| 140 |
} |
| 141 |
|
| 142 |
/* exp(z): return the exponential of z */ |
| 143 |
DEF_CMATH_METHOD(exp) |
| 144 |
|
| 145 |
/* log(z): return the natural logarithm of z, with branch cut along the negative real axis */ |
| 146 |
static mrb_value |
| 147 |
cmath_log(mrb_state *mrb, mrb_value self) { |
| 148 |
mrb_value z; |
| 149 |
mrb_float base; |
| 150 |
mrb_float real, imag; |
| 151 |
|
| 152 |
mrb_int n = mrb_get_args(mrb, "o|f", &z, &base); |
| 153 |
|
| 154 |
#ifndef M_E |
| 155 |
#define M_E F(exp)(1.0) |
| 156 |
#endif |
| 157 |
|
| 158 |
if (n == 1) base = M_E; |
| 159 |
if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) { |
| 160 |
mrb_complex c = CX(real,imag); |
| 161 |
c = FC(log)(c); |
| 162 |
if (n == 2) c = CXDIVc(c, FC(log)(CX(base,0))); |
| 163 |
return mrb_complex_new(mrb, creal(c), cimag(c)); |
| 164 |
} |
| 165 |
if (n == 1) return mrb_float_value(mrb, F(log)(real)); |
| 166 |
return mrb_float_value(mrb, F(log)(real)/F(log)(base)); |
| 167 |
} |
| 168 |
|
| 169 |
/* log10(z): return the base-10 logarithm of z, with branch cut along the negative real axis */ |
| 170 |
static mrb_value |
| 171 |
cmath_log10(mrb_state *mrb, mrb_value self) { |
| 172 |
mrb_value z = mrb_get_arg1(mrb); |
| 173 |
mrb_float real, imag; |
| 174 |
if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) { |
| 175 |
mrb_complex c = CX(real,imag); |
| 176 |
c = CXDIVf(FC(log)(c),log(10)); |
| 177 |
return mrb_complex_new(mrb, creal(c), cimag(c)); |
| 178 |
} |
| 179 |
return mrb_float_value(mrb, F(log10)(real)); |
| 180 |
} |
| 181 |
|
| 182 |
/* log2(z): return the base-2 logarithm of z, with branch cut along the negative real axis */ |
| 183 |
static mrb_value |
| 184 |
cmath_log2(mrb_state *mrb, mrb_value self) { |
| 185 |
mrb_value z = mrb_get_arg1(mrb); |
| 186 |
mrb_float real, imag; |
| 187 |
if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) { |
| 188 |
mrb_complex c = CX(real,imag); |
| 189 |
c = CXDIVf(FC(log)(c),log(2.0)); |
| 190 |
return mrb_complex_new(mrb, creal(c), cimag(c)); |
| 191 |
} |
| 192 |
return mrb_float_value(mrb, F(log2)(real)); |
| 193 |
} |
| 194 |
|
| 195 |
/* sqrt(z): return square root of z */ |
| 196 |
static mrb_value |
| 197 |
cmath_sqrt(mrb_state *mrb, mrb_value self) { |
| 198 |
mrb_value z = mrb_get_arg1(mrb); |
| 199 |
mrb_float real, imag; |
| 200 |
if (cmath_get_complex(mrb, z, &real, &imag) || real < 0.0) { |
| 201 |
mrb_complex c = CX(real,imag); |
| 202 |
c = FC(sqrt)(c); |
| 203 |
return mrb_complex_new(mrb, creal(c), cimag(c)); |
| 204 |
} |
| 205 |
return mrb_float_value(mrb, F(sqrt)(real)); |
| 206 |
} |
| 207 |
|
| 208 |
/* sin(z): sine function */ |
| 209 |
DEF_CMATH_METHOD(sin) |
| 210 |
/* cos(z): cosine function */ |
| 211 |
DEF_CMATH_METHOD(cos) |
| 212 |
/* tan(z): tangent function */ |
| 213 |
DEF_CMATH_METHOD(tan) |
| 214 |
/* asin(z): arc sine function */ |
| 215 |
DEF_CMATH_METHOD(asin) |
| 216 |
/* acos(z): arc cosine function */ |
| 217 |
DEF_CMATH_METHOD(acos) |
| 218 |
/* atan(z): arg tangent function */ |
| 219 |
DEF_CMATH_METHOD(atan) |
| 220 |
/* sinh(z): hyperbolic sine function */ |
| 221 |
DEF_CMATH_METHOD(sinh) |
| 222 |
/* cosh(z): hyperbolic cosine function */ |
| 223 |
DEF_CMATH_METHOD(cosh) |
| 224 |
/* tanh(z): hyperbolic tangent function */ |
| 225 |
DEF_CMATH_METHOD(tanh) |
| 226 |
/* asinh(z): inverse hyperbolic sine function */ |
| 227 |
DEF_CMATH_METHOD(asinh) |
| 228 |
/* acosh(z): inverse hyperbolic cosine function */ |
| 229 |
DEF_CMATH_METHOD(acosh) |
| 230 |
/* atanh(z): inverse hyperbolic tangent function */ |
| 231 |
DEF_CMATH_METHOD(atanh) |
| 232 |
|
| 233 |
/* ------------------------------------------------------------------------*/ |
| 234 |
|
| 235 |
void |
| 236 |
mrb_mruby_cmath_gem_init(mrb_state* mrb) |
| 237 |
{ |
| 238 |
struct RClass *cmath; |
| 239 |
cmath = mrb_define_module(mrb, "CMath"); |
| 240 |
|
| 241 |
mrb_include_module(mrb, cmath, mrb_module_get(mrb, "Math")); |
| 242 |
|
| 243 |
mrb_define_module_function(mrb, cmath, "sin", cmath_sin, MRB_ARGS_REQ(1)); |
| 244 |
mrb_define_module_function(mrb, cmath, "cos", cmath_cos, MRB_ARGS_REQ(1)); |
| 245 |
mrb_define_module_function(mrb, cmath, "tan", cmath_tan, MRB_ARGS_REQ(1)); |
| 246 |
|
| 247 |
mrb_define_module_function(mrb, cmath, "asin", cmath_asin, MRB_ARGS_REQ(1)); |
| 248 |
mrb_define_module_function(mrb, cmath, "acos", cmath_acos, MRB_ARGS_REQ(1)); |
| 249 |
mrb_define_module_function(mrb, cmath, "atan", cmath_atan, MRB_ARGS_REQ(1)); |
| 250 |
|
| 251 |
mrb_define_module_function(mrb, cmath, "sinh", cmath_sinh, MRB_ARGS_REQ(1)); |
| 252 |
mrb_define_module_function(mrb, cmath, "cosh", cmath_cosh, MRB_ARGS_REQ(1)); |
| 253 |
mrb_define_module_function(mrb, cmath, "tanh", cmath_tanh, MRB_ARGS_REQ(1)); |
| 254 |
|
| 255 |
mrb_define_module_function(mrb, cmath, "asinh", cmath_asinh, MRB_ARGS_REQ(1)); |
| 256 |
mrb_define_module_function(mrb, cmath, "acosh", cmath_acosh, MRB_ARGS_REQ(1)); |
| 257 |
mrb_define_module_function(mrb, cmath, "atanh", cmath_atanh, MRB_ARGS_REQ(1)); |
| 258 |
|
| 259 |
mrb_define_module_function(mrb, cmath, "exp", cmath_exp, MRB_ARGS_REQ(1)); |
| 260 |
mrb_define_module_function(mrb, cmath, "log", cmath_log, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); |
| 261 |
mrb_define_module_function(mrb, cmath, "log2", cmath_log2, MRB_ARGS_REQ(1)); |
| 262 |
mrb_define_module_function(mrb, cmath, "log10", cmath_log10, MRB_ARGS_REQ(1)); |
| 263 |
mrb_define_module_function(mrb, cmath, "sqrt", cmath_sqrt, MRB_ARGS_REQ(1)); |
| 264 |
} |
| 265 |
|
| 266 |
void |
| 267 |
mrb_mruby_cmath_gem_final(mrb_state* mrb) |
| 268 |
{ |
| 269 |
} |