vectorops 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 38db41b3c73af01fc46878a9175fcc8474fd4beb
4
+ data.tar.gz: 3b952bfff17bbebd5bf85e59ddb6af6e512d5004
5
+ SHA512:
6
+ metadata.gz: 85eb175b0ad2ad2370c6ea4278c095549fafeb92192475da29bd367210f55b32ba5a3c365e249c5cd7a0d465dcef89a5edc37b4ae3e0acbeda433ba170ffb3c6
7
+ data.tar.gz: cbd217dc043552fe6362f360e7ea5563cfa4a91b2f691156b58c364b6cfdadc87e592f6c6c3cbe1b8e5168cfa686d4cd2aa4cbc1f5107d1ee976dd0bc20d0225
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vectorops.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Peter Sorowka
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ vectorops
2
+ ==========
3
+
4
+ **Advanced Vector Operations for Ruby.**
5
+
6
+ READ THIS
7
+ ----------
8
+ This is work in progress and only very few operations are implemented. Feel free to contribute by adding more vector operations
9
+ like multiplication and so on.
10
+
11
+
12
+ Basic
13
+ -----
14
+
15
+ When you want to perform vector operations like summation in ruby, you cannot use the array class as it behaves like this:
16
+
17
+ v1 = [1,1,1,0,0,0]
18
+ v2 = [1,1,1,1,1,1]
19
+
20
+ v1 + v2
21
+ # -> [1,1,1,0,0,0,1,1,1,1,1,1]
22
+
23
+ For this purpose, there is a Vector class, defined in Ruby's stdlib. With Vector objects it's possible to do the following:
24
+
25
+ require 'matrix.rb'
26
+
27
+ v1 = Vector[1,1,1,0,0,0]
28
+ v2 = Vector[1,1,1,1,1,1]
29
+
30
+ v1 + v2
31
+ # -> Vector[2,2,2,1,1,1]
32
+
33
+ which is exactly what you want. Unfortunately, as of now, the libary seems not to be quite mature, as neither of the following will work by default:
34
+
35
+ v1 = Vector[1,1,1,0,0,0]
36
+ v2 = Vector[1,1,1,1,1,1]
37
+
38
+ # assigning values to a vector is impossible
39
+ # as no []= method is defined in the lib
40
+ v1 += v2
41
+
42
+ # adding up subsets of the vectors is not possible, as
43
+ # the [] accessor returns an array rather than a Vector
44
+ v1[0..3] + v2[0..3]
45
+
46
+ # adding a scalar to a vector doesn't work as expected as
47
+ # the + method doesn't handle this case
48
+ v1 + 2
49
+
50
+
51
+ Using the vectorops Gem, the following operations work like expected
52
+
53
+ v1 = Vector[1,1,1,0,0,0]
54
+ v2 = Vector[1,1,1,1,1,1]
55
+
56
+ v1[0..3]
57
+ # -> Vector[1,1,1]
58
+
59
+ v1 += v2
60
+ # -> v1 == Vector[2,2,2,1,1,1]
61
+
62
+ v1[0..3] += v2[0..3]
63
+ # -> v1 == Vector[2,2,2,0,0,0]
64
+
65
+ v1 + 2
66
+ # -> Vector[3,3,3,1,1,1]
67
+
68
+
69
+ Usage
70
+ -----
71
+
72
+ Install vectorops using the command
73
+
74
+ gem install vectorops
75
+
76
+ Or include the vectorops gem into your Gemfile
77
+
78
+ gem 'vectorops'
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require 'rspec/core/rake_task'
2
+ require 'bundler/gem_tasks'
3
+
4
+ task :default => :spec
@@ -0,0 +1,52 @@
1
+ require 'matrix'
2
+
3
+ class Vector
4
+
5
+ def []=(pos, value)
6
+ @elements[pos] = value.to_a
7
+ end
8
+
9
+ def [](*i)
10
+ i.length == 1 and i[0].is_a?(Numeric) ? @elements[i[0]] : Vector.elements(@elements[*i])
11
+ end
12
+
13
+ def +(v)
14
+ case v
15
+ when Vector
16
+ Vector.Raise ErrDimensionMismatch if size != v.size
17
+ els = collect2(v) {|v1, v2|
18
+ v1 + v2
19
+ }
20
+ Vector.elements(els, false)
21
+ when Matrix
22
+ Matrix.column_vector(self) + v
23
+ when Array
24
+ Vector.Raise ErrDimensionMismatch if size != v.length
25
+ Vector.elements(v) + self
26
+ when Numeric
27
+ Vector.elements([v] * size) + self
28
+ else
29
+ apply_through_coercion(v, __method__)
30
+ end
31
+ end
32
+
33
+ def -(v)
34
+ case v
35
+ when Vector
36
+ Vector.Raise ErrDimensionMismatch if size != v.size
37
+ els = collect2(v) {|v1, v2|
38
+ v1 - v2
39
+ }
40
+ Vector.elements(els, false)
41
+ when Matrix
42
+ Matrix.column_vector(self) - v
43
+ when Array
44
+ Vector.Raise ErrDimensionMismatch if size != v.length
45
+ Vector.elements(v) - self
46
+ when Numeric
47
+ Vector.elements([v] * size) - self
48
+ else
49
+ apply_through_coercion(v, __method__)
50
+ end
51
+ end
52
+ end
data/lib/vectorops.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "vectorops/version"
2
+
3
+ require 'core_ext/matrix'
@@ -0,0 +1,3 @@
1
+ module Vectorops
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,9 @@
1
+ require 'vectorops'
2
+
3
+ RSpec.configure do |config|
4
+ config.expect_with :rspec do |c|
5
+ c.syntax = :expect
6
+ end
7
+ config.color = true
8
+
9
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Vector do
4
+
5
+ it "works to add two equal length vectors" do
6
+
7
+ v1 = Vector[1,1,1,2,2,2]
8
+ v2 = Vector[2,2,2,3,3,3]
9
+
10
+ expect(v1 + v2).to eql(Vector[3,3,3,5,5,5])
11
+ end
12
+
13
+ it "refuses to add two unequal length vectors" do
14
+
15
+ v1 = Vector[1,2,3]
16
+ v2 = Vector[1,2,3,4,5]
17
+
18
+ expect {v1 + v2}.to raise_error
19
+ end
20
+
21
+ it "returns a vector when selecting a subset of vectors" do
22
+
23
+ v = Vector[1,2,3,4,5,6,7,8]
24
+ expect(v[2..4]).to eql(Vector[3,4,5])
25
+ end
26
+
27
+ it "works to use the += operator on the whole vector" do
28
+
29
+ v1 = Vector[1,1,1,2,2,2]
30
+ v2 = Vector[2,2,2,3,3,3]
31
+
32
+ v1 += v2
33
+
34
+ expect(v1).to eql(Vector[3,3,3,5,5,5])
35
+ end
36
+
37
+
38
+ it "works to use the += operator on a subset of a vector" do
39
+
40
+ v1 = Vector[1,1,1,2,2,2]
41
+ v2 = Vector[2,2,2,3,3,3]
42
+
43
+ v1[0..2] += v2[3..5]
44
+
45
+ expect(v1).to eql(Vector[4,4,4,2,2,2])
46
+ end
47
+
48
+ it "works to add a scalar to a vector" do
49
+
50
+ v = Vector[0,0,0]
51
+
52
+ expect(v + 2).to eql(Vector[2,2,2])
53
+ end
54
+ end
data/vectorops.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vectorops/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vectorops"
8
+ spec.version = Vectorops::VERSION
9
+ spec.authors = ["Peter Sorowka"]
10
+ spec.email = ["psorowka@freilicht.net"]
11
+ spec.description = %q{Advanced operations for Ruby Vector class}
12
+ spec.summary = %q{Adds additional operations to the basic Ruby Vector class}
13
+ spec.homepage = "https://github.com/psorowka/vectorops"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 3.0.0.beta2"
24
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vectorops
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Peter Sorowka
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-23 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 3.0.0.beta2
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 3.0.0.beta2
55
+ description: Advanced operations for Ruby Vector class
56
+ email:
57
+ - psorowka@freilicht.net
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - lib/core_ext/matrix.rb
68
+ - lib/vectorops.rb
69
+ - lib/vectorops/version.rb
70
+ - spec/spec_helper.rb
71
+ - spec/vector-ops.rb
72
+ - vectorops.gemspec
73
+ homepage: https://github.com/psorowka/vectorops
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.14
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Adds additional operations to the basic Ruby Vector class
97
+ test_files:
98
+ - spec/spec_helper.rb
99
+ - spec/vector-ops.rb
100
+ has_rdoc: