ccmath 0.2.7 → 0.2.71

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83d2fef732aa63e2e110ef2dacbfdad43e9367f0
4
- data.tar.gz: f3b1d96ddfa548493c1f6172e26334d43af65ee6
3
+ metadata.gz: c2fda0ca3c87789fe9fbba2ab15250a2e4cf7c6d
4
+ data.tar.gz: 73c23f5a3218c774c21035689ca9ded2d6d30b14
5
5
  SHA512:
6
- metadata.gz: 5d97f22e2547750e60adf92c264683fa0733f67ed207bab79b7494b22a94a83fdf99ed89d307eadd50632e8f72a7e22ca346b8392f3488e1d0f628f3a06f52cf
7
- data.tar.gz: 6babd532fb5e80db788d875ca051c08ebb33ca2247982594d3451db7a64ff3f0c05cca0d4b3e36723a6e7d007d1dd09ed379d1f075b87cd095b6d3772ebc17db
6
+ metadata.gz: eae6b0686f5e9bcc96a12d557ae39830badc4c3863c9ac80baf8d1a4385bd3ea8cb2819ccca506d60ada66b5520cbaac87ab9a2eadb7ddcbf583627070fd5576
7
+ data.tar.gz: 2792755a0be3d4849243eca410c013eb33b302d99ce0022191abc10f235b8bd002f1fb7ff49264e4b5f497a755dcc03637e98923b6616f74a4c1ee8f21a29914
@@ -0,0 +1,9 @@
1
+ ## CHANGELOG
2
+
3
+ ### v2.71
4
+
5
+ * `CCMath.gamma` can receive `Complex` object.
6
+
7
+ ### v2.7
8
+
9
+ * Initial release.
data/README.md CHANGED
@@ -1,28 +1,19 @@
1
- # CMath implemented with C
1
+ # Hopefully, CCMath is better than CMath... (・ω・;)
2
2
 
3
3
  [![Build Status](https://travis-ci.org/gogotanaka/ccmath.svg?branch=master)](https://travis-ci.org/gogotanaka/ccmath)
4
4
 
5
- Ruby has two official libraries related with mathematics.
6
-
7
- * **Math** ...Implemented with C. Only for Real number.
8
- * **CMath**...Implemented with Ruby. Accept Complex number.
9
-
10
- And now,
11
-
12
- * **CCMath**...Implemented with C. Accept Complex number.
13
-
14
- You can declare codomain.
5
+ CCMath is CMath implemented with C-lang.
15
6
 
7
+ ## Demo
16
8
  ```ruby
17
- CCMath.set = 'R' # equivalent to Math
18
- CCMath.set = 'C' # equivalent to CMath
19
- ```
9
+ CCMath.cos(1i)
10
+ #=> (1.5430806348152437+0.0i)
20
11
 
21
- ## Demo
22
- ```
23
12
  CCMath.cos(1)
24
- #=> 0.5403023058681398
13
+ #=> (0.5403023058681398+0.0i)
14
+ ```
25
15
 
16
+ ## Bench
26
17
 
27
18
  ```
28
19
  Calculating -------------------------------------
@@ -93,3 +84,17 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93
84
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94
85
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
95
86
  THE SOFTWARE.
87
+
88
+ # Contributor Code of Conduct
89
+
90
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
91
+
92
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
93
+
94
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
95
+
96
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
97
+
98
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
99
+
100
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
@@ -3,7 +3,7 @@
3
3
  VALUE rb_mCCMath;
4
4
  VALUE rb_eMathDomainError;
5
5
 
6
- static ID id_real_p, id_codomain;
6
+ static ID id_real_p, id_set, id_power;
7
7
 
8
8
  #define RB_BIGNUM_TYPE_P(x) RB_TYPE_P((x), T_BIGNUM)
9
9
  #define BIGNUM_POSITIVE_P(b) (FIX2LONG(rb_big_cmp((b), INT2FIX(0))) >= 0)
@@ -28,12 +28,9 @@ num2dbl_without_to_f(VALUE num)
28
28
  }
29
29
  else {
30
30
  switch (BUILTIN_TYPE(num)) {
31
- case T_FLOAT:
32
- return RFLOAT_VALUE(num);
33
- case T_BIGNUM:
34
- return big2dbl_without_to_f(num);
35
- case T_RATIONAL:
36
- return rat2dbl_without_to_f(num);
31
+ case T_FLOAT: return RFLOAT_VALUE(num);
32
+ case T_BIGNUM: return big2dbl_without_to_f(num);
33
+ case T_RATIONAL: return rat2dbl_without_to_f(num);
37
34
  }
38
35
  }
39
36
  return RFLOAT_VALUE(rb_to_float(num));
@@ -47,6 +44,19 @@ f_real_p(VALUE x)
47
44
  return rb_funcall(x, id_real_p, 0);
48
45
  }
49
46
 
47
+ #define binop(n,op) \
48
+ inline static VALUE \
49
+ f_##n(VALUE x, VALUE y)\
50
+ {\
51
+ return rb_funcall(x, (op), 1, y);\
52
+ }
53
+
54
+ binop(add, '+')
55
+ binop(div, '/')
56
+ binop(sub, '-')
57
+ binop(mul, '*')
58
+ binop(pow, id_power)
59
+
50
60
  inline static VALUE
51
61
  DBLS2COMP(double real, double imag)
52
62
  {
@@ -71,73 +81,43 @@ DBLS2COMP(double real, double imag)
71
81
 
72
82
  #define EXTRACT_DBLS(z); \
73
83
  double z##_real, z##_imag; \
74
- do{ \
84
+ do { \
75
85
  if (f_real_p(z)) { \
76
86
  EXTRACT_DBLS_FROM_REAL(z) \
77
87
  } else { \
78
88
  EXTRACT_DBLS_FROM_COMP(z) \
79
89
  } \
80
- }while(0)
90
+ } while(0)
81
91
 
82
92
  #define domain_error(msg) \
83
93
  rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
84
94
 
85
- static inline int
86
- codomain_check(VALUE z) {
87
- if (!rb_respond_to(z, id_real_p)) rb_raise(rb_eTypeError, "Numeric Number required");
88
-
89
- char codomain = *(RSTRING_PTR(rb_ivar_get(rb_mCCMath, id_codomain)));
90
- if (codomain == 'R') {
91
- if (!f_real_p(z)) rb_raise(rb_eMathDomainError, "Real Number required");
92
- return 1;
93
- }
94
- else if (codomain == 'C') {
95
- return 0;
96
- }
97
- else {
98
- domain_error("wow");
99
- }
100
- }
101
-
102
95
  static VALUE
103
96
  ccmath_sqrt(VALUE obj, VALUE z)
104
97
  {
105
- if (codomain_check(z)) {
106
- double d = NUM2DBL_F(z);
107
- if (d < 0.0) domain_error("sqrt");
108
- if (d == 0.0) return DBL2NUM(0.0);
109
- return DBL2NUM(sqrt(d));
110
- }
111
- else {
112
- EXTRACT_DBLS(z);
113
- if (z_imag == 0.0) {
114
- if (z_real < 0.0) {
115
- return DBLS2COMP(0.0, sqrt(fabs(z_real)));
116
- }
117
- else {
118
- return DBLS2COMP(sqrt(z_real), 0.0);
119
- }
98
+ EXTRACT_DBLS(z);
99
+ if (z_imag == 0.0) {
100
+ if (z_real < 0.0) {
101
+ return DBLS2COMP(0.0, sqrt(fabs(z_real)));
120
102
  }
121
103
  else {
122
- double s = sqrt((hypot(z_real, z_imag) + z_real) / 2.0);
123
- return DBLS2COMP(s, z_imag / (2 * s));
104
+ return DBLS2COMP(sqrt(z_real), 0.0);
124
105
  }
125
106
  }
107
+ else {
108
+ double s = sqrt((hypot(z_real, z_imag) + z_real) / 2.0);
109
+ return DBLS2COMP(s, z_imag / (2 * s));
110
+ }
126
111
  }
127
112
 
128
113
  static VALUE
129
114
  ccmath_exp(VALUE obj, VALUE z)
130
115
  {
131
- if (codomain_check(z)) {
132
- return DBL2NUM(exp(NUM2DBL_F(z)));
133
- }
134
- else {
135
- EXTRACT_DBLS(z);
136
- if (z_imag == 0.0) return DBLS2COMP(exp(z_real), 0.0);
116
+ EXTRACT_DBLS(z);
117
+ if (z_imag == 0.0) return DBLS2COMP(exp(z_real), 0.0);
137
118
 
138
- double ere = exp(z_real);
139
- return DBLS2COMP(ere * cos(z_imag), ere * sin(z_imag));
140
- }
119
+ double ere = exp(z_real);
120
+ return DBLS2COMP(ere * cos(z_imag), ere * sin(z_imag));
141
121
  }
142
122
 
143
123
  void
@@ -173,291 +153,229 @@ ccmath_log(int argc, const VALUE* argv, VALUE obj)
173
153
  VALUE z, base;
174
154
  rb_scan_args(argc, argv, "11", &z, &base);
175
155
 
176
- if (codomain_check(z)) {
177
- double d = internal_log(z);
178
- if (argc == 2) {
179
- d /= internal_log(base);
180
- }
181
- return DBL2NUM(d);
182
- }
183
- else {
184
- EXTRACT_DBLS(z);;
185
- float r = hypot(z_real, z_imag);
186
- if (argc == 2) {
187
- if (!rb_respond_to(base, id_real_p)) rb_raise(rb_eTypeError, "Numeric Number required");
188
- EXTRACT_DBLS(base);
189
- if (base_imag != 0.0) domain_error("log");
190
- if (base_real > 0.0) {
191
- double ln_base = log(base_real);
192
- return DBLS2COMP(log(r) / ln_base, m_atan2(z_imag, z_real) / ln_base);
193
- }
194
- else {
195
- VALUE ln_base = DBLS2COMP(log(fabs(base_real)), M_PI);
196
- return rb_funcall(DBLS2COMP(log(r), m_atan2(z_imag, z_real)), '/', 1, ln_base);
197
- }
156
+ EXTRACT_DBLS(z);
157
+ float r = hypot(z_real, z_imag);
158
+ if (argc == 2) {
159
+ if (!rb_respond_to(base, id_real_p)) rb_raise(rb_eTypeError, "Numeric Number required");
160
+ EXTRACT_DBLS(base);
161
+ if (base_imag != 0.0) domain_error("log");
162
+ if (base_real > 0.0) {
163
+ double ln_base = log(base_real);
164
+ return DBLS2COMP(log(r) / ln_base, m_atan2(z_imag, z_real) / ln_base);
198
165
  }
199
166
  else {
200
- return DBLS2COMP(log(r), m_atan2(z_imag, z_real));
167
+ VALUE ln_base = DBLS2COMP(log(fabs(base_real)), M_PI);
168
+ return f_div(DBLS2COMP(log(r), m_atan2(z_imag, z_real)), ln_base);
201
169
  }
202
170
  }
171
+ else {
172
+ return DBLS2COMP(log(r), m_atan2(z_imag, z_real));
173
+ }
203
174
  }
204
175
 
205
176
  static VALUE
206
177
  ccmath_log2(VALUE obj, VALUE z)
207
178
  {
208
- if (codomain_check(z)) {
209
- double d;
210
- size_t numbits;
211
-
212
- assing_numbits(z, &d, &numbits);
213
-
214
- if (d < 0.0) domain_error("log2");
215
- if (d == 0.0) return DBL2NUM(-INFINITY);
216
-
217
- return DBL2NUM(log2(d) + numbits);
218
- }
219
- else {
220
- EXTRACT_DBLS(z);;
221
- float r = hypot(z_real, z_imag);
222
- return DBLS2COMP(log(r) / M_LN2, m_atan2(z_imag, z_real) / M_LN2);
223
- }
179
+ EXTRACT_DBLS(z);
180
+ float r = hypot(z_real, z_imag);
181
+ return DBLS2COMP(log(r) / M_LN2, m_atan2(z_imag, z_real) / M_LN2);
224
182
  }
225
183
 
226
184
  static VALUE
227
185
  ccmath_log10(VALUE obj, VALUE z)
228
186
  {
229
- if (codomain_check(z)) {
230
- double d;
231
- size_t numbits;
232
-
233
- assing_numbits(z, &d, &numbits);
234
-
235
- if (d < 0.0) domain_error("log10");
236
- if (d == 0.0) return DBL2NUM(-INFINITY);
237
-
238
- return DBL2NUM(log10(d) + numbits * log10(2));
239
- }
240
- else {
241
- EXTRACT_DBLS(z);;
242
- float r = hypot(z_real, z_imag);
243
- return DBLS2COMP(log(r) / M_LN10, m_atan2(z_imag, z_real) / M_LN10);
244
- }
187
+ EXTRACT_DBLS(z);
188
+ float r = hypot(z_real, z_imag);
189
+ return DBLS2COMP(log(r) / M_LN10, m_atan2(z_imag, z_real) / M_LN10);
245
190
  }
246
191
 
247
192
  static VALUE
248
193
  ccmath_cos(VALUE obj, VALUE z)
249
194
  {
250
- if (codomain_check(z)) {
251
- return DBL2NUM(cos(NUM2DBL_F(z)));
252
- }
253
- else {
254
- EXTRACT_DBLS(z);;
255
- if (z_real == 0.0) return DBLS2COMP(cosh(z_imag), 0.0);
256
- if (z_imag == 0.0) return DBLS2COMP(cos(z_real), 0.0);
257
- return DBLS2COMP(cos(z_real) * cosh(z_imag), -sin(z_real) * sinh(z_imag));
258
- }
195
+ EXTRACT_DBLS(z);
196
+ if (z_real == 0.0) return DBLS2COMP(cosh(z_imag), 0.0);
197
+ if (z_imag == 0.0) return DBLS2COMP(cos(z_real), 0.0);
198
+ return DBLS2COMP(cos(z_real) * cosh(z_imag), -sin(z_real) * sinh(z_imag));
259
199
  }
260
200
 
261
201
  static VALUE
262
202
  ccmath_sin(VALUE obj, VALUE z)
263
203
  {
264
- if (codomain_check(z)) {
265
- return DBL2NUM(sin(NUM2DBL_F(z)));
266
- }
267
- else {
268
- EXTRACT_DBLS(z);;
269
- if (z_real == 0.0) return DBLS2COMP(0.0, sinh(z_imag));
270
- if (z_imag == 0.0) return DBLS2COMP(sin(z_real), 0.0);
271
- return DBLS2COMP(sin(z_real) * cosh(z_imag), cos(z_real) * sinh(z_imag));
272
- }
273
-
204
+ EXTRACT_DBLS(z);
205
+ if (z_real == 0.0) return DBLS2COMP(0.0, sinh(z_imag));
206
+ if (z_imag == 0.0) return DBLS2COMP(sin(z_real), 0.0);
207
+ return DBLS2COMP(sin(z_real) * cosh(z_imag), cos(z_real) * sinh(z_imag));
274
208
  }
275
209
 
276
210
  static VALUE
277
211
  ccmath_tan(VALUE obj, VALUE z)
278
212
  {
279
- if (codomain_check(z)) {
280
- return DBL2NUM(tan(NUM2DBL_F(z)));
281
- }
282
- else {
283
- return rb_funcall(ccmath_sin(obj, z), '/', 1, ccmath_cos(obj, z));
284
- }
213
+ return f_div(ccmath_sin(obj, z), ccmath_cos(obj, z));
285
214
  }
286
215
 
287
216
  static VALUE
288
217
  ccmath_cosh(VALUE obj, VALUE z)
289
218
  {
290
- if (codomain_check(z)) {
291
- return DBL2NUM(cosh(NUM2DBL_F(z)));
292
- }
293
- else {
294
- EXTRACT_DBLS(z);;
295
- if (z_real == 0.0) return DBLS2COMP(cos(z_imag), 0.0);
296
- if (z_imag == 0.0) return DBLS2COMP(cosh(z_real), 0.0);
297
- return DBLS2COMP(cosh(z_real) * cos(z_imag), sinh(z_real) * sin(z_imag));
298
- }
219
+ EXTRACT_DBLS(z);
220
+ if (z_real == 0.0) return DBLS2COMP(cos(z_imag), 0.0);
221
+ if (z_imag == 0.0) return DBLS2COMP(cosh(z_real), 0.0);
222
+ return DBLS2COMP(cosh(z_real) * cos(z_imag), sinh(z_real) * sin(z_imag));
299
223
  }
300
224
 
301
225
  static VALUE
302
226
  ccmath_sinh(VALUE obj, VALUE z)
303
227
  {
304
- if (codomain_check(z)) {
305
- return DBL2NUM(sinh(NUM2DBL_F(z)));
306
- }
307
- else {
308
- EXTRACT_DBLS(z);;
309
- if (z_real == 0.0) return DBLS2COMP(0.0, sin(z_imag));
310
- if (z_imag == 0.0) return DBLS2COMP(sinh(z_real), 0.0);
311
- return DBLS2COMP(sinh(z_real) * cos(z_imag), cosh(z_real) * sin(z_imag));
312
- }
228
+ EXTRACT_DBLS(z);
229
+ if (z_real == 0.0) return DBLS2COMP(0.0, sin(z_imag));
230
+ if (z_imag == 0.0) return DBLS2COMP(sinh(z_real), 0.0);
231
+ return DBLS2COMP(sinh(z_real) * cos(z_imag), cosh(z_real) * sin(z_imag));
313
232
  }
314
233
 
315
234
  static VALUE
316
235
  ccmath_tanh(VALUE obj, VALUE z)
317
236
  {
318
- if (codomain_check(z)) {
319
- return DBL2NUM(tanh(NUM2DBL_F(z)));
320
- }
321
- else {
322
- return rb_funcall(ccmath_sinh(obj, z), '/', 1, ccmath_cosh(obj, z));
323
- }
237
+ return f_div(ccmath_sinh(obj, z), ccmath_cosh(obj, z));
324
238
  }
325
239
 
326
240
  static VALUE
327
241
  ccmath_asinh(VALUE obj, VALUE z)
328
242
  {
329
- if (codomain_check(z)) {
330
- return DBL2NUM(asinh(NUM2DBL_F(z)));
331
- }
332
- else {
333
- EXTRACT_DBLS(z);;
243
+ EXTRACT_DBLS(z);
334
244
 
335
- if (z_imag == 0.0) return DBLS2COMP(asinh(z_real), 0.0);
245
+ if (z_imag == 0.0) return DBLS2COMP(asinh(z_real), 0.0);
336
246
 
337
- VALUE s1 = DBLS2COMP(1.0 + z_imag, -z_real);
338
- VALUE s2 = DBLS2COMP(1.0 - z_imag, z_real);
247
+ VALUE s1 = DBLS2COMP(1.0 + z_imag, -z_real);
248
+ VALUE s2 = DBLS2COMP(1.0 - z_imag, z_real);
339
249
 
340
- s1 = ccmath_sqrt(obj, s1);
341
- s2 = ccmath_sqrt(obj, s2);
342
- EXTRACT_DBLS(s1);
343
- EXTRACT_DBLS(s2);
250
+ s1 = ccmath_sqrt(obj, s1);
251
+ s2 = ccmath_sqrt(obj, s2);
252
+ EXTRACT_DBLS(s1);
253
+ EXTRACT_DBLS(s2);
344
254
 
345
- return DBLS2COMP(asinh(s1_real * s2_imag - s2_real * s1_imag), atan2(z_imag, s1_real * s2_real - s1_imag * s2_imag));
346
- }
255
+ return DBLS2COMP(asinh(s1_real * s2_imag - s2_real * s1_imag), atan2(z_imag, s1_real * s2_real - s1_imag * s2_imag));
347
256
  }
348
257
 
349
258
  static VALUE
350
259
  ccmath_asin(VALUE obj, VALUE z)
351
260
  {
352
- if (codomain_check(z)) {
353
- double d = NUM2DBL_F(z);
354
- if (d < -1.0 || 1.0 < d) domain_error("asin");
355
- return DBL2NUM(asin(d));
356
- }
357
- else {
358
- EXTRACT_DBLS(z);;
261
+ EXTRACT_DBLS(z);
359
262
 
360
- if (z_imag == 0.0) return DBLS2COMP(asin(z_real), 0.0);
263
+ if (z_imag == 0.0) return DBLS2COMP(asin(z_real), 0.0);
361
264
 
362
- VALUE s = ccmath_asinh(obj, DBLS2COMP(-z_imag, z_real));
363
- EXTRACT_DBLS(s);
364
- return DBLS2COMP(s_imag, -s_real);
365
- }
265
+ VALUE s = ccmath_asinh(obj, DBLS2COMP(-z_imag, z_real));
266
+ EXTRACT_DBLS(s);
267
+ return DBLS2COMP(s_imag, -s_real);
366
268
  }
367
269
 
368
270
  static VALUE
369
271
  ccmath_acosh(VALUE obj, VALUE z)
370
272
  {
371
- if (codomain_check(z)) {
372
- double d = NUM2DBL_F(z);
373
- if (d < 1.0) domain_error("acosh");
374
- return DBL2NUM(acosh(d));
375
- }
376
- else {
377
- EXTRACT_DBLS(z);;
273
+ EXTRACT_DBLS(z);
378
274
 
379
- if (z_imag == 0.0) return DBLS2COMP(acosh(z_real), 0.0);
275
+ if (z_imag == 0.0) return DBLS2COMP(acosh(z_real), 0.0);
380
276
 
381
- VALUE s1 = DBLS2COMP(z_real - 1.0, z_imag);
382
- VALUE s2 = DBLS2COMP(z_real + 1.0, z_imag);
277
+ VALUE s1 = DBLS2COMP(z_real - 1.0, z_imag);
278
+ VALUE s2 = DBLS2COMP(z_real + 1.0, z_imag);
383
279
 
384
- s1 = ccmath_sqrt(obj, s1);
385
- s2 = ccmath_sqrt(obj, s2);
386
- EXTRACT_DBLS(s1);
387
- EXTRACT_DBLS(s2);
280
+ s1 = ccmath_sqrt(obj, s1);
281
+ s2 = ccmath_sqrt(obj, s2);
282
+ EXTRACT_DBLS(s1);
283
+ EXTRACT_DBLS(s2);
388
284
 
389
- return DBLS2COMP(asinh(s1_real * s2_real + s1_imag * s2_imag), 2.0 * atan2(s1_imag, s2_real));
390
- }
285
+ return DBLS2COMP(asinh(s1_real * s2_real + s1_imag * s2_imag), 2.0 * atan2(s1_imag, s2_real));
391
286
  }
392
287
 
393
288
  static VALUE
394
289
  ccmath_acos(VALUE obj, VALUE z)
395
290
  {
396
- if (codomain_check(z)) {
397
- double d = NUM2DBL_F(z);
398
- if (d < -1.0 || 1.0 < d) domain_error("acos");
399
- return DBL2NUM(acos(d));
400
- }
401
- else {
402
- EXTRACT_DBLS(z);;
291
+ EXTRACT_DBLS(z);
403
292
 
404
- if (z_imag == 0.0) return DBLS2COMP(acos(z_real), 0.0);
293
+ if (z_imag == 0.0) return DBLS2COMP(acos(z_real), 0.0);
405
294
 
406
- return rb_funcall(DBL2NUM(M_PI / 2.0), '-', 1, ccmath_asin(obj, z));
407
- }
295
+ return f_sub(DBL2NUM(M_PI / 2.0), ccmath_asin(obj, z));
408
296
  }
409
297
 
410
298
  static VALUE
411
299
  ccmath_atanh(VALUE obj, VALUE z)
412
300
  {
413
- if (codomain_check(z)) {
414
- double d = NUM2DBL_F(z);
301
+ EXTRACT_DBLS(z);
415
302
 
416
- if (d < -1.0 || +1.0 < d) domain_error("atanh");
417
- if (d == -1.0) return DBL2NUM(-INFINITY);
418
- if (d == +1.0) return DBL2NUM(+INFINITY);
419
- return DBL2NUM(atanh(d));
420
- }
421
- else {
422
- EXTRACT_DBLS(z);;
303
+ if (z_imag == 0.0) return DBLS2COMP(atanh(z_real), 0.0);
423
304
 
424
- if (z_imag == 0.0) return DBLS2COMP(atanh(z_real), 0.0);
305
+ double sq_imag = z_imag * z_imag;
425
306
 
426
- double sq_imag = z_imag * z_imag;
427
-
428
- return DBLS2COMP(m_log1p(4.0 * z_real / ((1.0 - z_real) * (1.0 - z_real) + sq_imag)) / 4.0,
429
- -m_atan2(-2.0 * z_imag, (1.0 - z_real) * (1.0 + z_real) - sq_imag) / 2.0);
430
- }
307
+ return DBLS2COMP(m_log1p(4.0 * z_real / ((1.0 - z_real) * (1.0 - z_real) + sq_imag)) / 4.0,
308
+ -m_atan2(-2.0 * z_imag, (1.0 - z_real) * (1.0 + z_real) - sq_imag) / 2.0);
431
309
  }
432
310
 
433
311
  static VALUE
434
312
  ccmath_atan(VALUE obj, VALUE z)
435
313
  {
436
- if (codomain_check(z)) {
437
- return DBL2NUM(atan(NUM2DBL_F(z)));
438
- }
439
- else {
440
- EXTRACT_DBLS(z);;
314
+ EXTRACT_DBLS(z);
441
315
 
442
- if (z_imag == 0.0) return DBLS2COMP(atan(z_real), 0.0);
316
+ if (z_imag == 0.0) return DBLS2COMP(atan(z_real), 0.0);
443
317
 
444
- VALUE s = ccmath_atanh(obj, DBLS2COMP(-z_imag, z_real));
445
- EXTRACT_DBLS(s);
446
- return DBLS2COMP(s_imag, -s_real);
447
- }
318
+ VALUE s = ccmath_atanh(obj, DBLS2COMP(-z_imag, z_real));
319
+ EXTRACT_DBLS(s);
320
+ return DBLS2COMP(s_imag, -s_real);
448
321
  }
449
322
 
450
323
  static VALUE
451
- ccmath_define_set(VALUE obj, VALUE str)
324
+ ccmath_gamma(VALUE obj, VALUE z)
452
325
  {
453
- rb_ivar_set(obj, id_codomain, str);
454
- return obj;
326
+ EXTRACT_DBLS(z);
327
+ if (z_real < 0.5) {
328
+ VALUE s1, s2;
329
+ s1 = ccmath_gamma(obj, f_sub(DBL2NUM(1), z));
330
+ s2 = ccmath_sin(obj, f_mul(DBL2NUM(M_PI), z));
331
+ return f_div(DBL2NUM(M_PI), f_mul(s1, s2));
332
+ }
333
+ else {
334
+ static const double lanczos_coef[] = {
335
+ 0.99999999999980993,
336
+ 676.5203681218851,
337
+ -1259.1392167224028,
338
+ 771.32342877765313,
339
+ -176.61502916214059,
340
+ 12.507343278686905,
341
+ -0.13857109526572012,
342
+ 9.9843695780195716e-6,
343
+ 1.5056327351493116e-7
344
+ };
345
+ int i;
346
+ double g, s, x_real, x_imag;
347
+
348
+ g = 7.0;
349
+ z_real -= 1;
350
+ x_real = lanczos_coef[0];
351
+ x_imag = 0.0;
352
+ for(i=1; i<g+2; i++) {
353
+ s = lanczos_coef[i] / ((z_real+i) * (z_real+i) + (z_imag * z_imag));
354
+ x_real += s * (z_real+i);
355
+ x_imag -= s * z_imag;
356
+ }
357
+ VALUE x = DBLS2COMP(x_real, x_imag);
358
+ VALUE sqrt_2_pi = DBL2NUM(sqrt(2 * M_PI));
359
+ VALUE t = DBLS2COMP(z_real + g + 0.5, z_imag);
360
+ return f_mul(sqrt_2_pi,
361
+ f_mul(f_pow(t, DBLS2COMP(z_real+0.5, z_imag)),
362
+ f_mul(f_div(DBL2NUM(1), ccmath_exp(obj,t)), x)));
363
+
364
+ }
455
365
  }
456
366
 
367
+ // static VALUE
368
+ // ccmath_define_set(VALUE obj, VALUE str)
369
+ // {
370
+ // rb_ivar_set(obj, id_set, str);
371
+ // return obj;
372
+ // }
373
+
457
374
  void Init_ccmath(void)
458
375
  {
459
376
  id_real_p = rb_intern("real?");
460
- id_codomain = rb_intern("codomain");
377
+ id_power = rb_intern("**");
378
+ id_set = rb_intern("set");
461
379
  rb_mCCMath = rb_define_module("CCMath");
462
380
 
463
381
  rb_eMathDomainError = rb_define_class_under(rb_mCCMath, "DomainError", rb_eStandardError);
@@ -490,7 +408,8 @@ void Init_ccmath(void)
490
408
  rb_define_module_function(rb_mCCMath, "acos", ccmath_acos, 1);
491
409
  rb_define_module_function(rb_mCCMath, "atanh", ccmath_atanh, 1);
492
410
  rb_define_module_function(rb_mCCMath, "atan", ccmath_atan, 1);
493
- rb_define_module_function(rb_mCCMath, "set=", ccmath_define_set, 1);
411
+ rb_define_module_function(rb_mCCMath, "gamma", ccmath_gamma, 1);
412
+ // rb_define_module_function(rb_mCCMath, "set=", ccmath_define_set, 1);
494
413
 
495
- rb_ivar_set(rb_mCCMath, id_codomain, rb_str_new2("R"));
414
+ // rb_ivar_set(rb_mCCMath, id_set, rb_str_new2("R"));
496
415
  }
@@ -2,21 +2,37 @@ require "ccmath/version"
2
2
  require "ccmath/ccmath"
3
3
 
4
4
  module CCMath
5
- class << self
6
- %w[
7
- atan2
8
- cbrt
9
- frexp
10
- ldexp
11
- hypot
12
- erf
13
- erfc
14
- gamma
15
- lgamma
16
- ].each do |meth|
17
- define_method(meth) do |*args, &blk|
18
- Math.send(meth, *args, &blk)
19
- end
5
+ def cbrt(z)
6
+ z ** (1.0/3)
7
+ end
8
+
9
+ def atan2(y,x)
10
+ if y.real? and x.real?
11
+ atan2!(y,x)
12
+ else
13
+ (-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
14
+ end
15
+ end
16
+
17
+ %w[
18
+ frexp
19
+ ldexp
20
+ hypot
21
+ erf
22
+ erfc
23
+ lgamma
24
+ ].each do |meth|
25
+ define_method(meth) do |*args, &blk|
26
+ Math.send(meth, *args, &blk)
20
27
  end
21
28
  end
29
+
30
+ module_function :cbrt
31
+ module_function :atan2
32
+ module_function :frexp
33
+ module_function :ldexp
34
+ module_function :hypot
35
+ module_function :erf
36
+ module_function :erfc
37
+ module_function :lgamma
22
38
  end
@@ -1,3 +1,3 @@
1
1
  module Ccmath
2
- VERSION = "0.2.7"
2
+ VERSION = "0.2.71"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ccmath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.71
5
5
  platform: ruby
6
6
  authors:
7
7
  - gogotanaka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-05 00:00:00.000000000 Z
11
+ date: 2015-08-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -76,7 +76,7 @@ extra_rdoc_files: []
76
76
  files:
77
77
  - ".gitignore"
78
78
  - ".travis.yml"
79
- - CODE_OF_CONDUCT.md
79
+ - CHANGELOG.md
80
80
  - Gemfile
81
81
  - README.md
82
82
  - Rakefile
@@ -1,13 +0,0 @@
1
- # Contributor Code of Conduct
2
-
3
- As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
-
5
- We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
-
7
- Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
-
9
- Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
-
11
- Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
-
13
- This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)