ccmath 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 83d2fef732aa63e2e110ef2dacbfdad43e9367f0
4
+ data.tar.gz: f3b1d96ddfa548493c1f6172e26334d43af65ee6
5
+ SHA512:
6
+ metadata.gz: 5d97f22e2547750e60adf92c264683fa0733f67ed207bab79b7494b22a94a83fdf99ed89d307eadd50632e8f72a7e22ca346b8392f3488e1d0f628f3a06f52cf
7
+ data.tar.gz: 6babd532fb5e80db788d875ca051c08ebb33ca2247982594d3451db7a64ff3f0c05cca0d4b3e36723a6e7d007d1dd09ed379d1f075b87cd095b6d3772ebc17db
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ sudo: false
3
+ cache:
4
+ - bundler
5
+
6
+ bundler:
7
+ rvm:
8
+ - 2.2
9
+ # - 2.1
10
+ # - 2.0
11
+ - ruby-head
12
+ # - jruby-head
13
+ before_install: gem install bundler -v 1.10.3
14
+ notifications:
15
+ email: false
@@ -0,0 +1,13 @@
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/)
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ccmath.gemspec
4
+ gem 'pry'
5
+ gem 'benchmark-ips'
6
+ gemspec
@@ -0,0 +1,95 @@
1
+ # CMath implemented with C
2
+
3
+ [![Build Status](https://travis-ci.org/gogotanaka/ccmath.svg?branch=master)](https://travis-ci.org/gogotanaka/ccmath)
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.
15
+
16
+ ```ruby
17
+ CCMath.set = 'R' # equivalent to Math
18
+ CCMath.set = 'C' # equivalent to CMath
19
+ ```
20
+
21
+ ## Demo
22
+ ```
23
+ CCMath.cos(1)
24
+ #=> 0.5403023058681398
25
+
26
+
27
+ ```
28
+ Calculating -------------------------------------
29
+ CMath 31.262k i/100ms
30
+ CCMath 53.484k i/100ms
31
+ -------------------------------------------------
32
+ CMath 437.150k (± 7.1%) i/s - 2.188M
33
+ CCMath 1.044M (±28.3%) i/s - 4.600M
34
+
35
+ Comparison:
36
+ CCMath: 1044410.2 i/s
37
+ CMath: 437150.3 i/s - 2.39x slower
38
+
39
+ ```
40
+
41
+ ## Installation
42
+
43
+
44
+ ```ruby
45
+ gem 'ccmath'
46
+ ```
47
+
48
+ And then execute:
49
+
50
+ $ bundle
51
+
52
+ Or install it yourself as:
53
+
54
+ $ gem install ccmath
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies.
59
+
60
+ Then, run `bundle exec rake compile` to compile library and `bundle exec rake test` to run the tests.
61
+
62
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
63
+
64
+ To install this gem onto your local machine, run `bundle exec rake install`.
65
+
66
+ <!-- To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). -->
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ccmath. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
71
+
72
+
73
+ ## License
74
+
75
+ The MIT License (MIT)
76
+
77
+ Copyright (c) 2015 gogotanaka
78
+
79
+ Permission is hereby granted, free of charge, to any person obtaining a copy
80
+ of this software and associated documentation files (the "Software"), to deal
81
+ in the Software without restriction, including without limitation the rights
82
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
83
+ copies of the Software, and to permit persons to whom the Software is
84
+ furnished to do so, subject to the following conditions:
85
+
86
+ The above copyright notice and this permission notice shall be included in
87
+ all copies or substantial portions of the Software.
88
+
89
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
91
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
92
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
95
+ THE SOFTWARE.
@@ -0,0 +1,50 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ require "rake/extensiontask"
11
+
12
+ task :build => :compile
13
+
14
+ Rake::ExtensionTask.new("ccmath") do |ext|
15
+ ext.lib_dir = "lib/ccmath"
16
+ end
17
+
18
+ task :compile_and_test do
19
+ Rake::Task['compile'].invoke
20
+ Rake::Task['test'].invoke
21
+ end
22
+
23
+
24
+ task default: :compile_and_test
25
+
26
+
27
+ # Benchmark
28
+ #-----------------------------------------------
29
+ desc "Compare with stb-CMath"
30
+ task :benchmark do
31
+ require "benchmark/ips"
32
+ require 'cmath'
33
+ require 'ccmath'
34
+
35
+ Benchmark.ips do |x|
36
+ x.report('CMath') { CMath.cos(1+1i) }
37
+ x.report('CCMath') { CCMath.cos(1+1i) }
38
+
39
+ x.compare!
40
+ end
41
+
42
+ Benchmark.ips do |x|
43
+ x.report('Math') { CMath.cos(1) }
44
+ x.report('CMath') { CMath.cos(1) }
45
+ x.report('CCMath') { CCMath.cos(1) }
46
+
47
+ x.compare!
48
+ end
49
+ end
50
+ task bm: :benchmark
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ccmath"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ccmath/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ccmath"
8
+ spec.version = Ccmath::VERSION
9
+ spec.authors = ["gogotanaka"]
10
+ spec.email = ["mail@tanakakazuki.com"]
11
+
12
+ spec.summary = %q{ Implement CMath with C. }
13
+ spec.description = %q{ Implement CMath with C. }
14
+ spec.homepage = "https://github.com/gogotanaka/ccmath"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+ spec.extensions = ["ext/ccmath/extconf.rb"]
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.10"
32
+ spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rake-compiler"
34
+ spec.add_development_dependency "minitest"
35
+ end
@@ -0,0 +1,496 @@
1
+ #include "ccmath.h"
2
+
3
+ VALUE rb_mCCMath;
4
+ VALUE rb_eMathDomainError;
5
+
6
+ static ID id_real_p, id_codomain;
7
+
8
+ #define RB_BIGNUM_TYPE_P(x) RB_TYPE_P((x), T_BIGNUM)
9
+ #define BIGNUM_POSITIVE_P(b) (FIX2LONG(rb_big_cmp((b), INT2FIX(0))) >= 0)
10
+
11
+ #define fix2dbl_without_to_f(x) (double) FIX2LONG(x)
12
+ #define big2dbl_without_to_f(x) rb_big2dbl(x)
13
+ #define int2dbl_without_to_f(x) \
14
+ (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
15
+ #define rat2dbl_without_to_f(x) \
16
+ (int2dbl_without_to_f(rb_rational_num(x)) / int2dbl_without_to_f(rb_rational_den(x)))
17
+
18
+ static inline double
19
+ num2dbl_without_to_f(VALUE num)
20
+ {
21
+ if (SPECIAL_CONST_P(num)) {
22
+ if (FIXNUM_P(num)) {
23
+ return fix2dbl_without_to_f(num);
24
+ }
25
+ else if (FLONUM_P(num)) {
26
+ return RFLOAT_VALUE(num);
27
+ }
28
+ }
29
+ else {
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);
37
+ }
38
+ }
39
+ return RFLOAT_VALUE(rb_to_float(num));
40
+ }
41
+
42
+ #define NUM2DBL_F(x) num2dbl_without_to_f(x)
43
+
44
+ inline static VALUE
45
+ f_real_p(VALUE x)
46
+ {
47
+ return rb_funcall(x, id_real_p, 0);
48
+ }
49
+
50
+ inline static VALUE
51
+ DBLS2COMP(double real, double imag)
52
+ {
53
+ NEWOBJ_OF(obj, struct RComplex, rb_cComplex,
54
+ T_COMPLEX | (RGENGC_WB_PROTECTED_COMPLEX ? FL_WB_PROTECTED : 0));
55
+
56
+ RCOMPLEX_SET_REAL(obj, DBL2NUM(real));
57
+ RCOMPLEX_SET_IMAG(obj, DBL2NUM(imag));
58
+
59
+ return (VALUE)obj;
60
+ }
61
+
62
+ #define EXTRACT_DBLS_FROM_REAL(x) \
63
+ x##_real = NUM2DBL_F(x); \
64
+ x##_imag = 0.0;
65
+
66
+ #define EXTRACT_DBLS_FROM_COMP(z) \
67
+ struct RComplex* dat_##z; \
68
+ dat_##z = ((struct RComplex*)(z)); \
69
+ z##_real = NUM2DBL_F(dat_##z->real); \
70
+ z##_imag = NUM2DBL_F(dat_##z->imag);
71
+
72
+ #define EXTRACT_DBLS(z); \
73
+ double z##_real, z##_imag; \
74
+ do{ \
75
+ if (f_real_p(z)) { \
76
+ EXTRACT_DBLS_FROM_REAL(z) \
77
+ } else { \
78
+ EXTRACT_DBLS_FROM_COMP(z) \
79
+ } \
80
+ }while(0)
81
+
82
+ #define domain_error(msg) \
83
+ rb_raise(rb_eMathDomainError, "Numerical argument is out of domain - " #msg)
84
+
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
+ static VALUE
103
+ ccmath_sqrt(VALUE obj, VALUE z)
104
+ {
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
+ }
120
+ }
121
+ else {
122
+ double s = sqrt((hypot(z_real, z_imag) + z_real) / 2.0);
123
+ return DBLS2COMP(s, z_imag / (2 * s));
124
+ }
125
+ }
126
+ }
127
+
128
+ static VALUE
129
+ ccmath_exp(VALUE obj, VALUE z)
130
+ {
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);
137
+
138
+ double ere = exp(z_real);
139
+ return DBLS2COMP(ere * cos(z_imag), ere * sin(z_imag));
140
+ }
141
+ }
142
+
143
+ void
144
+ assing_numbits(VALUE x, double *p_d, size_t *p_numbits) {
145
+ if (RB_BIGNUM_TYPE_P(x) && BIGNUM_POSITIVE_P(x) && DBL_MAX_EXP <= (*p_numbits = rb_absint_numwords(x, 1, NULL))) {
146
+ *p_numbits -= DBL_MANT_DIG;
147
+ x = rb_big_rshift(x, SIZET2NUM(*p_numbits));
148
+ }
149
+ else {
150
+ *p_numbits = 0;
151
+ }
152
+
153
+ *p_d = NUM2DBL_F(x);
154
+ }
155
+
156
+ static double
157
+ internal_log(VALUE x)
158
+ {
159
+ double d;
160
+ size_t numbits;
161
+
162
+ assing_numbits(x, &d, &numbits);
163
+
164
+ if (d < 0.0) domain_error("log");
165
+ if (d == 0.0) return -INFINITY;
166
+
167
+ return log(d) + numbits * log(2); /* log(d * 2 ** numbits) */
168
+ }
169
+
170
+ static VALUE
171
+ ccmath_log(int argc, const VALUE* argv, VALUE obj)
172
+ {
173
+ VALUE z, base;
174
+ rb_scan_args(argc, argv, "11", &z, &base);
175
+
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
+ }
198
+ }
199
+ else {
200
+ return DBLS2COMP(log(r), m_atan2(z_imag, z_real));
201
+ }
202
+ }
203
+ }
204
+
205
+ static VALUE
206
+ ccmath_log2(VALUE obj, VALUE z)
207
+ {
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
+ }
224
+ }
225
+
226
+ static VALUE
227
+ ccmath_log10(VALUE obj, VALUE z)
228
+ {
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
+ }
245
+ }
246
+
247
+ static VALUE
248
+ ccmath_cos(VALUE obj, VALUE z)
249
+ {
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
+ }
259
+ }
260
+
261
+ static VALUE
262
+ ccmath_sin(VALUE obj, VALUE z)
263
+ {
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
+
274
+ }
275
+
276
+ static VALUE
277
+ ccmath_tan(VALUE obj, VALUE z)
278
+ {
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
+ }
285
+ }
286
+
287
+ static VALUE
288
+ ccmath_cosh(VALUE obj, VALUE z)
289
+ {
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
+ }
299
+ }
300
+
301
+ static VALUE
302
+ ccmath_sinh(VALUE obj, VALUE z)
303
+ {
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
+ }
313
+ }
314
+
315
+ static VALUE
316
+ ccmath_tanh(VALUE obj, VALUE z)
317
+ {
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
+ }
324
+ }
325
+
326
+ static VALUE
327
+ ccmath_asinh(VALUE obj, VALUE z)
328
+ {
329
+ if (codomain_check(z)) {
330
+ return DBL2NUM(asinh(NUM2DBL_F(z)));
331
+ }
332
+ else {
333
+ EXTRACT_DBLS(z);;
334
+
335
+ if (z_imag == 0.0) return DBLS2COMP(asinh(z_real), 0.0);
336
+
337
+ VALUE s1 = DBLS2COMP(1.0 + z_imag, -z_real);
338
+ VALUE s2 = DBLS2COMP(1.0 - z_imag, z_real);
339
+
340
+ s1 = ccmath_sqrt(obj, s1);
341
+ s2 = ccmath_sqrt(obj, s2);
342
+ EXTRACT_DBLS(s1);
343
+ EXTRACT_DBLS(s2);
344
+
345
+ return DBLS2COMP(asinh(s1_real * s2_imag - s2_real * s1_imag), atan2(z_imag, s1_real * s2_real - s1_imag * s2_imag));
346
+ }
347
+ }
348
+
349
+ static VALUE
350
+ ccmath_asin(VALUE obj, VALUE z)
351
+ {
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);;
359
+
360
+ if (z_imag == 0.0) return DBLS2COMP(asin(z_real), 0.0);
361
+
362
+ VALUE s = ccmath_asinh(obj, DBLS2COMP(-z_imag, z_real));
363
+ EXTRACT_DBLS(s);
364
+ return DBLS2COMP(s_imag, -s_real);
365
+ }
366
+ }
367
+
368
+ static VALUE
369
+ ccmath_acosh(VALUE obj, VALUE z)
370
+ {
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);;
378
+
379
+ if (z_imag == 0.0) return DBLS2COMP(acosh(z_real), 0.0);
380
+
381
+ VALUE s1 = DBLS2COMP(z_real - 1.0, z_imag);
382
+ VALUE s2 = DBLS2COMP(z_real + 1.0, z_imag);
383
+
384
+ s1 = ccmath_sqrt(obj, s1);
385
+ s2 = ccmath_sqrt(obj, s2);
386
+ EXTRACT_DBLS(s1);
387
+ EXTRACT_DBLS(s2);
388
+
389
+ return DBLS2COMP(asinh(s1_real * s2_real + s1_imag * s2_imag), 2.0 * atan2(s1_imag, s2_real));
390
+ }
391
+ }
392
+
393
+ static VALUE
394
+ ccmath_acos(VALUE obj, VALUE z)
395
+ {
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);;
403
+
404
+ if (z_imag == 0.0) return DBLS2COMP(acos(z_real), 0.0);
405
+
406
+ return rb_funcall(DBL2NUM(M_PI / 2.0), '-', 1, ccmath_asin(obj, z));
407
+ }
408
+ }
409
+
410
+ static VALUE
411
+ ccmath_atanh(VALUE obj, VALUE z)
412
+ {
413
+ if (codomain_check(z)) {
414
+ double d = NUM2DBL_F(z);
415
+
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);;
423
+
424
+ if (z_imag == 0.0) return DBLS2COMP(atanh(z_real), 0.0);
425
+
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
+ }
431
+ }
432
+
433
+ static VALUE
434
+ ccmath_atan(VALUE obj, VALUE z)
435
+ {
436
+ if (codomain_check(z)) {
437
+ return DBL2NUM(atan(NUM2DBL_F(z)));
438
+ }
439
+ else {
440
+ EXTRACT_DBLS(z);;
441
+
442
+ if (z_imag == 0.0) return DBLS2COMP(atan(z_real), 0.0);
443
+
444
+ VALUE s = ccmath_atanh(obj, DBLS2COMP(-z_imag, z_real));
445
+ EXTRACT_DBLS(s);
446
+ return DBLS2COMP(s_imag, -s_real);
447
+ }
448
+ }
449
+
450
+ static VALUE
451
+ ccmath_define_set(VALUE obj, VALUE str)
452
+ {
453
+ rb_ivar_set(obj, id_codomain, str);
454
+ return obj;
455
+ }
456
+
457
+ void Init_ccmath(void)
458
+ {
459
+ id_real_p = rb_intern("real?");
460
+ id_codomain = rb_intern("codomain");
461
+ rb_mCCMath = rb_define_module("CCMath");
462
+
463
+ rb_eMathDomainError = rb_define_class_under(rb_mCCMath, "DomainError", rb_eStandardError);
464
+ #ifdef M_PI
465
+ rb_define_const(rb_mCCMath, "PI", DBL2NUM(M_PI));
466
+ #else
467
+ rb_define_const(rb_mCCMath, "PI", DBL2NUM(atan(1.0)*4.0));
468
+ #endif
469
+
470
+ #ifdef M_E
471
+ rb_define_const(rb_mCCMath, "E", DBL2NUM(M_E));
472
+ #else
473
+ rb_define_const(rb_mCCMath, "E", DBL2NUM(exp(1.0)));
474
+ #endif
475
+
476
+ rb_define_module_function(rb_mCCMath, "sqrt", ccmath_sqrt, 1);
477
+ rb_define_module_function(rb_mCCMath, "exp", ccmath_exp, 1);
478
+ rb_define_module_function(rb_mCCMath, "log", ccmath_log, -1);
479
+ rb_define_module_function(rb_mCCMath, "log2", ccmath_log2, 1);
480
+ rb_define_module_function(rb_mCCMath, "log10", ccmath_log10, 1);
481
+ rb_define_module_function(rb_mCCMath, "cos", ccmath_cos, 1);
482
+ rb_define_module_function(rb_mCCMath, "sin", ccmath_sin, 1);
483
+ rb_define_module_function(rb_mCCMath, "tan", ccmath_tan, 1);
484
+ rb_define_module_function(rb_mCCMath, "cosh", ccmath_cosh, 1);
485
+ rb_define_module_function(rb_mCCMath, "sinh", ccmath_sinh, 1);
486
+ rb_define_module_function(rb_mCCMath, "tanh", ccmath_tanh, 1);
487
+ rb_define_module_function(rb_mCCMath, "asinh", ccmath_asinh, 1);
488
+ rb_define_module_function(rb_mCCMath, "asin", ccmath_asin, 1);
489
+ rb_define_module_function(rb_mCCMath, "acosh", ccmath_acosh, 1);
490
+ rb_define_module_function(rb_mCCMath, "acos", ccmath_acos, 1);
491
+ rb_define_module_function(rb_mCCMath, "atanh", ccmath_atanh, 1);
492
+ rb_define_module_function(rb_mCCMath, "atan", ccmath_atan, 1);
493
+ rb_define_module_function(rb_mCCMath, "set=", ccmath_define_set, 1);
494
+
495
+ rb_ivar_set(rb_mCCMath, id_codomain, rb_str_new2("R"));
496
+ }
@@ -0,0 +1,86 @@
1
+ #ifndef CCMATH_H
2
+ #define CCMATH_H 1
3
+
4
+ #include "ruby.h"
5
+
6
+ #ifndef M_PI
7
+ #define M_PI 3.14159265358979323846
8
+ #endif
9
+
10
+ #ifndef M_LN2
11
+ #define M_LN2 (0.6931471805599453094) /* log(2) */
12
+ #endif
13
+
14
+ #ifndef M_LN10
15
+ #define M_LN10 (2.302585092994045684) /* log(10) */
16
+ #endif
17
+
18
+ #ifndef DBL_MANT_DIG
19
+ #define DBL_MAX_EXP 1024
20
+ #endif
21
+
22
+ #ifndef DBL_MANT_DIG
23
+ #define DBL_MANT_DIG 53
24
+ #endif
25
+
26
+ #ifndef DBL_EPSILON
27
+ #define DBL_EPSILON 2.2204460492503131e-16
28
+ #endif
29
+
30
+ #define LARGE_DBL (DBL_MAX/4.)
31
+
32
+ struct RComplex {
33
+ struct RBasic basic;
34
+ const VALUE real;
35
+ const VALUE imag;
36
+ };
37
+
38
+ #define RCOMPLEX(obj) (R_CAST(RComplex)(obj))
39
+ #define RCOMPLEX_SET_REAL(cmp, r) RB_OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->real,(r))
40
+ #define RCOMPLEX_SET_IMAG(cmp, i) RB_OBJ_WRITE((cmp), &((struct RComplex *)(cmp))->imag,(i))
41
+
42
+ static inline double
43
+ m_atan2(double y, double x)
44
+ {
45
+ if (x == 0.0 && y == 0.0) {
46
+ if (!signbit(x))
47
+ return y;
48
+ if (!signbit(y))
49
+ return M_PI;
50
+ return -M_PI;
51
+ }
52
+ #ifndef ATAN2_INF_C99
53
+ if (isinf(x) && isinf(y)) {
54
+ /* optimization for FLONUM */
55
+ double z = (x < 0.0) ? (3.0 * M_PI / 4.0) : (M_PI / 4.0)
56
+ return (y < 0.0) ? -z : z;
57
+ }
58
+ #endif
59
+ return atan2(y, x);
60
+ }
61
+
62
+ static inline double
63
+ m_log1p(double x)
64
+ {
65
+ #ifdef HAVE_LOG1P
66
+ if (x == 0.0)
67
+ return x;
68
+
69
+ return log1p(x);
70
+ #else
71
+ if (fabs(x) < DBL_EPSILON/2.0)
72
+ return x;
73
+
74
+ return log(1.0+x);
75
+ #endif /* ifdef HAVE_LOG1P */
76
+ }
77
+
78
+ #ifndef HAVE_TANH
79
+ double
80
+ tanh(double x)
81
+ {
82
+ return sinh(x) / cosh(x);
83
+ }
84
+ #endif /* ifdef HAVE_TANH */
85
+
86
+ #endif /* CCMATH_H */
@@ -0,0 +1,3 @@
1
+ require "mkmf"
2
+
3
+ create_makefile("ccmath/ccmath")
@@ -0,0 +1,22 @@
1
+ require "ccmath/version"
2
+ require "ccmath/ccmath"
3
+
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
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Ccmath
2
+ VERSION = "0.2.7"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ccmath
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.7
5
+ platform: ruby
6
+ authors:
7
+ - gogotanaka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake-compiler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: " Implement CMath with C. "
70
+ email:
71
+ - mail@tanakakazuki.com
72
+ executables: []
73
+ extensions:
74
+ - ext/ccmath/extconf.rb
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - ccmath.gemspec
86
+ - ext/ccmath/ccmath.c
87
+ - ext/ccmath/ccmath.h
88
+ - ext/ccmath/extconf.rb
89
+ - lib/ccmath.rb
90
+ - lib/ccmath/version.rb
91
+ homepage: https://github.com/gogotanaka/ccmath
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.4.5
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Implement CMath with C.
115
+ test_files: []