narray-convolve 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/Gemfile +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +54 -0
- data/Rakefile +13 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/narray/convolve.rb +44 -0
- data/lib/narray/convolve/version.rb +7 -0
- data/narray-convolve.gemspec +27 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c96997545f196ef2e81ca16ead2bfcd79023e417bf37cd8afc52bee6a2375990
|
4
|
+
data.tar.gz: b9f17105c3accb3eebe697562b35eb81dae9053d958dc69277cdd3f8f93c191b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0024721187219f45ec1a87b0b182d978bd86e245bed7045b35de02c56289d56e734b6a0b5a0cf144dbc1c1744b21ab7e176fa8bcdd03b3dc5f68cf10957748dc'
|
7
|
+
data.tar.gz: 5be09f6ac1dfb5a5cef1b0e46681482cca2af24c5a803757e0409ea3fd0a4bfc8e78eeee42eb685b53988c6a1edcaecd8e6861bf54f71e5440d8d05e03dc547e
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 icm7216
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Narray::Convolve
|
2
|
+
|
3
|
+
Narray::Convolve is returns the discrete, linear convolution of two one-dimensional sequences.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'narray-convolve'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle install
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install narray-convolve
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Narray::Convolve is similar to numpy.convolve, but with a different order of second arguments.
|
24
|
+
```
|
25
|
+
irb(main):001:0> require "narray/convolve"
|
26
|
+
=> true
|
27
|
+
irb(main):002:0> n = Numo::DFloat[1,2,3]
|
28
|
+
=>
|
29
|
+
Numo::DFloat#shape=[3]
|
30
|
+
...
|
31
|
+
irb(main):003:0> m = Numo::DFloat[0,1,0.5].reverse
|
32
|
+
=>
|
33
|
+
Numo::DFloat(view)#shape=[3]
|
34
|
+
...
|
35
|
+
irb(main):004:0> Narray::Convolve.convolve(n, m, :same)
|
36
|
+
=>
|
37
|
+
Numo::DFloat(view)#shape=[3]
|
38
|
+
[1, 2.5, 4]
|
39
|
+
```
|
40
|
+
|
41
|
+
In case of Python
|
42
|
+
```
|
43
|
+
>>> import numpy as np
|
44
|
+
>>> np.convolve([1,2,3],[0,1,0.5], 'same')
|
45
|
+
array([1. , 2.5, 4. ])
|
46
|
+
>>>
|
47
|
+
```
|
48
|
+
see: [numpy.convolve — NumPy v1.20 Manual](https://numpy.org/doc/stable/reference/generated/numpy.convolve.html#numpy-convolve)
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
## License
|
53
|
+
|
54
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "rake/testtask"
|
5
|
+
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << "test"
|
8
|
+
t.libs << "lib"
|
9
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
10
|
+
t.options = "-v"
|
11
|
+
end
|
12
|
+
|
13
|
+
task default: :test
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "narray/convolve"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "convolve/version"
|
4
|
+
require "numo/narray"
|
5
|
+
|
6
|
+
module Narray
|
7
|
+
module Convolve
|
8
|
+
|
9
|
+
def convolve(n, m, mode = :full)
|
10
|
+
zero_size = m.size - 1
|
11
|
+
out_size = zero_size + n.size
|
12
|
+
n_end = out_size - 1
|
13
|
+
work_size = out_size + zero_size
|
14
|
+
|
15
|
+
out = Numo::DFloat.zeros(out_size)
|
16
|
+
work = Numo::DFloat.zeros(work_size)
|
17
|
+
work[zero_size..n_end].store(n[true])
|
18
|
+
|
19
|
+
work_size.times do |i|
|
20
|
+
w = i + zero_size
|
21
|
+
if w < work_size
|
22
|
+
mul = work[i..w] * m
|
23
|
+
out[i] = mul.sum
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
case mode
|
28
|
+
when :full # full: (N+M-1)
|
29
|
+
ans = out[true]
|
30
|
+
when :same # same: [M,N].max
|
31
|
+
s = (m.size / 2.0).round - 1
|
32
|
+
e = s + n.size
|
33
|
+
ans = out[s...e]
|
34
|
+
when :valid # valid: [M,N].max - [M,N].min + 1
|
35
|
+
s = zero_size
|
36
|
+
e = m.size
|
37
|
+
ans = out[s..-e]
|
38
|
+
end
|
39
|
+
ans
|
40
|
+
end
|
41
|
+
module_function :convolve
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/narray/convolve/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "narray-convolve"
|
7
|
+
spec.version = Narray::Convolve::VERSION
|
8
|
+
spec.authors = ["icm7216"]
|
9
|
+
spec.email = ["icm7216d@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "convolve for numo-narray"
|
12
|
+
spec.description = "This is a convolve method for numo-narray. Similar to numpy convolve method."
|
13
|
+
spec.homepage = "https://github.com/icm7216/narray-convolve"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "test-unit"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: narray-convolve
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- icm7216
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-02-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: test-unit
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: This is a convolve method for numo-narray. Similar to numpy convolve
|
28
|
+
method.
|
29
|
+
email:
|
30
|
+
- icm7216d@gmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".gitignore"
|
36
|
+
- Gemfile
|
37
|
+
- LICENSE.txt
|
38
|
+
- README.md
|
39
|
+
- Rakefile
|
40
|
+
- bin/console
|
41
|
+
- bin/setup
|
42
|
+
- lib/narray/convolve.rb
|
43
|
+
- lib/narray/convolve/version.rb
|
44
|
+
- narray-convolve.gemspec
|
45
|
+
homepage: https://github.com/icm7216/narray-convolve
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.4.0
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.1.4
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: convolve for numo-narray
|
68
|
+
test_files: []
|