numo-pocketfft 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bddf2fdda35fb2a8308fabe55a365945282cec74
4
- data.tar.gz: 34d970a1a6a45a667fbd57f648cd7e0dde85bf6e
3
+ metadata.gz: 170eebc1e93586a62ba569d77a3a48bf8c1deb2a
4
+ data.tar.gz: a40006057f6538de447002968f1e5d17d7f72ab3
5
5
  SHA512:
6
- metadata.gz: ae9396b7f766a993efbb49ae837b684e8b3ab17b1728ec41798dbadb2954b30faf9bc0232e4b0c07faec2bea938e8f6cf02f5597771e79bbe9318cb2b56ec57c
7
- data.tar.gz: 53c4212c75ffc3ce9e6b876eb4ec194fc1a7f2ada7796992675c539b08f703c250a4308cc83fa5f7a33f029965d602cba66a70cd6ce1c0d1c71f778ddaeea9e9
6
+ metadata.gz: 2a93c018c1f75ad1c903d5235e874ed6e881cddf33523ac9be7ebff7c047e2767561db48204c2a5ec13f72d243cee1e49394bfa0362b63b20f395245801700d9
7
+ data.tar.gz: 4c23f877e13768d2641f69ddf000b54603038df559b250179a320f395c0920f31a162cfb76b2bde310add0ea1ce8f8b5540da8429dfaf2b93b9c64dc5ca8539c
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # 0.1.1
2
+ - Add input validation for empty array.
3
+ - Add input validation for non-Numo::NArray object.
4
+ - Fix to raise NoMemoryError when happened memory allocation error.
5
+
6
+ # 0.1.0
7
+ - First release.
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Numo::Pocketfft
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/numo-pocketfft.svg)](https://badge.fury.io/rb/numo-pocketfft)
3
4
  [![Build Status](https://travis-ci.org/yoshoku/numo-pocketfft.svg?branch=master)](https://travis-ci.org/yoshoku/numo-pocketfft)
4
5
  [![Coverage Status](https://coveralls.io/repos/github/yoshoku/numo-pocketfft/badge.svg?branch=master)](https://coveralls.io/github/yoshoku/numo-pocketfft?branch=master)
5
6
  [![BSD 3-Clause License](https://img.shields.io/badge/License-BSD%203--Clause-orange.svg)](https://github.com/yoshoku/numo-liblinear/blob/master/LICENSE.txt)
7
+ [![Documentation](http://img.shields.io/badge/docs-rdoc.info-blue.svg)](http://www.rubydoc.info/gems/numo-pocketfft/0.1.1)
6
8
 
7
9
  Numo::Pocketfft provides functions for performing descrete Fourier Transform with
8
10
  [Numo::NArray](https://github.com/ruby-numo/numo-narray) by using
@@ -12,6 +12,7 @@ VALUE numo_pocketfft_fft(VALUE x_val, int is_forward)
12
12
  int n_repeats;
13
13
  int i;
14
14
  int res;
15
+ int fail;
15
16
  double fct;
16
17
  VALUE z_val;
17
18
  double* z_pt;
@@ -30,22 +31,27 @@ VALUE numo_pocketfft_fft(VALUE x_val, int is_forward)
30
31
  length = NA_SHAPE(x_nary)[n_dims - 1];
31
32
  x_pt = (double*)na_get_pointer_for_read(x_val);
32
33
 
34
+ plan = make_cfft_plan(length);
35
+ if (!plan) {
36
+ rb_raise(rb_eNoMemError, "Failed to allocate memory for plan of pocketfft.");
37
+ return Qnil;
38
+ }
39
+
33
40
  z_val = nary_s_new_like(numo_cDComplex, x_val);
34
41
  z_pt = (double*)na_get_pointer_for_write(z_val);
35
42
  GetNArray(z_val, z_nary);
36
43
  for (i = 0; i < (int)(NA_SIZE(z_nary) * 2); z_pt[i++] = 0.0);
37
44
 
45
+ fail = 0;
38
46
  fct = is_forward == 1 ? 1.0 : 1.0 / length;
39
- plan = make_cfft_plan(length);
40
- if (!plan) {
41
- return Qnil;
42
- }
43
-
44
47
  n_repeats = (int)(NA_SIZE(x_nary)) / length;
45
48
  for (i = 0; i < n_repeats; i++) {
46
49
  memcpy(z_pt, x_pt, 2 * length * sizeof(double));
47
50
  res = is_forward == 1 ? cfft_forward(plan, z_pt, fct) : cfft_backward(plan, z_pt, fct);
48
- if (res != 0) { break; }
51
+ if (res != 0) {
52
+ fail = 1;
53
+ break;
54
+ }
49
55
  z_pt += length * 2;
50
56
  x_pt += length * 2;
51
57
  }
@@ -54,6 +60,12 @@ VALUE numo_pocketfft_fft(VALUE x_val, int is_forward)
54
60
  destroy_cfft_plan(plan);
55
61
  }
56
62
 
63
+ if (fail) {
64
+ rb_raise(rb_eNoMemError, "Failed to allocate memory in function of pocketfft.");
65
+ rb_funcall(z_val, rb_intern("free"), 0);
66
+ return Qnil;
67
+ }
68
+
57
69
  return z_val;
58
70
  }
59
71
 
@@ -84,6 +96,7 @@ static VALUE numo_pocketfft_rfft(VALUE self, VALUE x_val)
84
96
  size_t length;
85
97
  int n_repeats;
86
98
  int i;
99
+ int fail;
87
100
  size_t* z_shape;
88
101
  VALUE z_val;
89
102
  narray_t* z_nary;
@@ -105,6 +118,7 @@ static VALUE numo_pocketfft_rfft(VALUE self, VALUE x_val)
105
118
 
106
119
  plan = make_rfft_plan(length);
107
120
  if (!plan) {
121
+ rb_raise(rb_eNoMemError, "Failed to allocate memory for plan of pocketfft.");
108
122
  return Qnil;
109
123
  }
110
124
 
@@ -118,12 +132,16 @@ static VALUE numo_pocketfft_rfft(VALUE self, VALUE x_val)
118
132
  GetNArray(z_val, z_nary);
119
133
  for (i = 0; i < (int)(NA_SIZE(z_nary) * 2); z_pt[i++] = 0.0);
120
134
 
135
+ fail = 0;
121
136
  z_step = (int)(NA_SHAPE(z_nary)[n_dims - 1]) * 2;
122
137
  n_repeats = (int)(NA_SIZE(x_nary)) / length;
123
138
  for (i = 0; i < n_repeats; i++) {
124
139
  z_pt[z_step - 1] = 0.0;
125
140
  memcpy(z_pt + 1, x_pt, length * sizeof(double));
126
- if (rfft_forward(plan, z_pt + 1, 1.0) != 0) { break; }
141
+ if (rfft_forward(plan, z_pt + 1, 1.0) != 0) {
142
+ fail = 1;
143
+ break;
144
+ }
127
145
  z_pt[0] = z_pt[1];
128
146
  z_pt[1] = 0.0;
129
147
  z_pt += z_step;
@@ -134,6 +152,12 @@ static VALUE numo_pocketfft_rfft(VALUE self, VALUE x_val)
134
152
  destroy_rfft_plan(plan);
135
153
  }
136
154
 
155
+ if (fail) {
156
+ rb_raise(rb_eNoMemError, "Failed to allocate memory in function of pocketfft.");
157
+ rb_funcall(z_val, rb_intern("free"), 0);
158
+ return Qnil;
159
+ }
160
+
137
161
  return z_val;
138
162
  }
139
163
 
@@ -148,6 +172,7 @@ static VALUE numo_pocketfft_irfft(VALUE self, VALUE x_val)
148
172
  int n_dims;
149
173
  int n_repeats;
150
174
  int i;
175
+ int fail;
151
176
  double fct;
152
177
  size_t* z_shape;
153
178
  VALUE z_val;
@@ -166,10 +191,10 @@ static VALUE numo_pocketfft_irfft(VALUE self, VALUE x_val)
166
191
  n_dims = NA_NDIM(x_nary);
167
192
  length = NA_SHAPE(x_nary)[n_dims - 1];
168
193
  x_pt = (double*)na_get_pointer_for_read(x_val);
169
- fct = 1.0 / length;
170
194
 
171
195
  plan = make_rfft_plan(length);
172
196
  if (!plan) {
197
+ rb_raise(rb_eNoMemError, "Failed to allocate memory for plan of pocketfft.");
173
198
  return Qnil;
174
199
  }
175
200
 
@@ -183,11 +208,16 @@ static VALUE numo_pocketfft_irfft(VALUE self, VALUE x_val)
183
208
  GetNArray(z_val, z_nary);
184
209
  for (i = 0; i < (int)NA_SIZE(z_nary); z_pt[i++] = 0.0);
185
210
 
211
+ fail = 0;
212
+ fct = 1.0 / length;
186
213
  n_repeats = (int)(NA_SIZE(z_nary)) / length;
187
214
  for (i = 0; i < n_repeats; i++) {
188
215
  memcpy(z_pt + 1, x_pt + 2, (length - 1) * sizeof(double));
189
216
  z_pt[0] = x_pt[0];
190
- if (rfft_backward(plan, z_pt, fct) != 0) { break; }
217
+ if (rfft_backward(plan, z_pt, fct) != 0) {
218
+ fail = 1;
219
+ break;
220
+ }
191
221
  z_pt += length;
192
222
  x_pt += length * 2;
193
223
  }
@@ -196,6 +226,12 @@ static VALUE numo_pocketfft_irfft(VALUE self, VALUE x_val)
196
226
  destroy_rfft_plan(plan);
197
227
  }
198
228
 
229
+ if (fail) {
230
+ rb_raise(rb_eNoMemError, "Failed to allocate memory in function of pocketfft.");
231
+ rb_funcall(z_val, rb_intern("free"), 0);
232
+ return Qnil;
233
+ }
234
+
199
235
  return z_val;
200
236
  }
201
237
 
@@ -12,7 +12,10 @@ module Numo
12
12
  # @param a [Numo::DFloat/Numo::DComplex] Real or complex 1-dimensional input array.
13
13
  # @return [Numo::DComplex] Transformed data.
14
14
  def fft(a)
15
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
16
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
15
17
  raise ArgumentError, 'Expect input array to be one-dimensional.' unless a.ndim == 1
18
+
16
19
  raw_fft(a, 0, inverse: false, real: false)
17
20
  end
18
21
 
@@ -20,7 +23,10 @@ module Numo
20
23
  # @param a [Numo::DComplex] Complex 1-dimensional input array.
21
24
  # @return [Numo::DComplex] Inversed transformed data.
22
25
  def ifft(a)
26
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
27
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
23
28
  raise ArgumentError, 'Expect input array to be one-dimensional.' unless a.ndim == 1
29
+
24
30
  raw_fft(a, 0, inverse: true, real: false)
25
31
  end
26
32
 
@@ -28,7 +34,10 @@ module Numo
28
34
  # @param a [Numo::DFloat/Numo::DComplex] Real or complex 2-dimensional input array.
29
35
  # @return [Numo::DComplex] Transformed data.
30
36
  def fft2(a)
37
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
38
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
31
39
  raise ArgumentError, 'Expect input array to be two-dimensional.' unless a.ndim == 2
40
+
32
41
  fftn(a)
33
42
  end
34
43
 
@@ -36,7 +45,10 @@ module Numo
36
45
  # @param a [Numo::DComplex] Complex 2-dimensional input array.
37
46
  # @return [Numo::DComplex] Inversed transformed data.
38
47
  def ifft2(a)
48
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
49
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
39
50
  raise ArgumentError, 'Expect input array to be two-dimensional.' unless a.ndim == 2
51
+
40
52
  ifftn(a)
41
53
  end
42
54
 
@@ -44,6 +56,9 @@ module Numo
44
56
  # @param a [Numo::DFloat/Numo::DComplex] Real or complex input array with any-dimension.
45
57
  # @return [Numo::DComplex] Transformed data.
46
58
  def fftn(a)
59
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
60
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
61
+
47
62
  b = a.dup
48
63
  (0...b.ndim).to_a.reverse.each { |ax_id| b = raw_fft(b, ax_id, inverse: false, real: false) }
49
64
  b
@@ -53,6 +68,9 @@ module Numo
53
68
  # @param a [Numo::DComplex] Complex input array with any-dimension.
54
69
  # @return [Numo::DComplex] Inversed transformed data.
55
70
  def ifftn(a)
71
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
72
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
73
+
56
74
  b = a.dup
57
75
  (0...b.ndim).to_a.each { |ax_id| b = raw_fft(b, ax_id, inverse: true, real: false) }
58
76
  b
@@ -62,7 +80,10 @@ module Numo
62
80
  # @param a [Numo::DFloat] Real 1-dimensional input array.
63
81
  # @return [Numo::DComplex] Transformed data.
64
82
  def rfft(a)
83
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
84
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
65
85
  raise ArgumentError, 'Expect input array to be one-dimensional.' unless a.ndim == 1
86
+
66
87
  raw_fft(a, 0, inverse: false, real: true)
67
88
  end
68
89
 
@@ -70,7 +91,10 @@ module Numo
70
91
  # @param a [Numo::DComplex] Complex 1-dimensional input array.
71
92
  # @return [Numo::DFloat] Inverse transformed data.
72
93
  def irfft(a)
94
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
95
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
73
96
  raise ArgumentError, 'Expect input array to be one-dimensional.' unless a.ndim == 1
97
+
74
98
  raw_fft(a, 0, inverse: true, real: true)
75
99
  end
76
100
 
@@ -78,7 +102,10 @@ module Numo
78
102
  # @param a [Numo::DFloat] Real 2-dimensional input array.
79
103
  # @return [Numo::DComplex] Transformed data.
80
104
  def rfft2(a)
105
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
106
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
81
107
  raise ArgumentError, 'Expect input array to be two-dimensional.' unless a.ndim == 2
108
+
82
109
  rfftn(a)
83
110
  end
84
111
 
@@ -86,7 +113,10 @@ module Numo
86
113
  # @param a [Numo::DComplex] Complex 2-dimensional input array.
87
114
  # @return [Numo::DFloat] Inverse transformed data.
88
115
  def irfft2(a)
116
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
117
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
89
118
  raise ArgumentError, 'Expect input array to be two-dimensional.' unless a.ndim == 2
119
+
90
120
  irfftn(a)
91
121
  end
92
122
 
@@ -94,6 +124,9 @@ module Numo
94
124
  # @param a [Numo::DFloat] Real input array with any-dimension.
95
125
  # @return [Numo::DComplex] Transformed data.
96
126
  def rfftn(a)
127
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
128
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
129
+
97
130
  last_axis_id = a.ndim - 1
98
131
  b = raw_fft(a, last_axis_id, inverse: false, real: true)
99
132
  (0...last_axis_id).to_a.reverse.each { |ax_id| b = raw_fft(b, ax_id, inverse: false, real: false) }
@@ -104,6 +137,9 @@ module Numo
104
137
  # @param a [Numo::DComplex] Complex input array with any-dimension.
105
138
  # @return [Numo::DFloat] Inverse transformed data.
106
139
  def irfftn(a)
140
+ raise ArgumentError, 'Expect class of input array to be Numo::NArray.' unless a.is_a?(Numo::NArray)
141
+ raise ArgumentError, 'Expect input array to be non-empty.' if a.empty?
142
+
107
143
  last_axis_id = a.ndim - 1
108
144
  b = a.dup
109
145
  (0...last_axis_id).to_a.each { |ax_id| b = raw_fft(b, ax_id, inverse: true, real: false) }
@@ -5,6 +5,6 @@ module Numo
5
5
  # Numo::Pocketfft is the module that has functions for Fourier transform.
6
6
  module Pocketfft
7
7
  # The version of Numo::Pocketfft you are using.
8
- VERSION = '0.1.0'
8
+ VERSION = '0.1.1'
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numo-pocketfft
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - yoshoku
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-10 00:00:00.000000000 Z
11
+ date: 2019-08-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -110,6 +110,7 @@ files:
110
110
  - ".gitmodules"
111
111
  - ".rspec"
112
112
  - ".travis.yml"
113
+ - CHANGELOG.md
113
114
  - CODE_OF_CONDUCT.md
114
115
  - Gemfile
115
116
  - LICENSE.txt