quaternion 0.1.0

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: c189f435b49a8ec52111f49762b9f3401acff57c
4
+ data.tar.gz: 5114e0133f4e73eb7f50baa31036457863d40a26
5
+ SHA512:
6
+ metadata.gz: d7a653a9ec36bea1e216b58de34396c5c8fefdda9d1e000b7e557cbbd667a67b5a80c82f16effcb28576253a61687907ac4c7b16f1755b47549e26fd4b91775b
7
+ data.tar.gz: b496fde8478ffa2d2719fc63f1062016cb7efab8f4a50413f79f33a5f7d3210923b254067a721ed18c6bfb258d21b3ba72069cf347bb859ab3382f86fc8e5ff4
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /rdoc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in quaternion.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Hiroyuki Tanaka
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,28 @@
1
+ # Quaternion
2
+
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'quaternion'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install quaternion
19
+
20
+ ## Usage
21
+
22
+ ## Contributing
23
+
24
+ 1. Fork it ( https://github.com/tanahiro/quaternion/fork )
25
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
26
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
27
+ 4. Push to the branch (`git push origin my-new-feature`)
28
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ end
7
+
8
+ #task :default => :test
9
+
10
+ require 'rdoc/task'
11
+ RDoc::Task.new do |rdoc|
12
+ rdoc.main = "README.md"
13
+ rdoc.rdoc_files.include("README.md", "lib/**/*.rb")
14
+ rdoc.rdoc_dir = "rdoc"
15
+ rdoc.title = "Quaternion"
16
+ end
17
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "quaternion"
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
data/bin/setup ADDED
@@ -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
data/lib/quaternion.rb ADDED
@@ -0,0 +1,225 @@
1
+ root_dir = "#{__dir__}/.."
2
+
3
+ require 'matrix'
4
+ require "#{root_dir}/lib/quaternion/version"
5
+ require "#{root_dir}/lib/quaternion/numeric"
6
+
7
+ ##
8
+ # Class for Quaternion calculation.
9
+ class Quaternion
10
+ include Math
11
+
12
+ ##
13
+ # Returns an instance of Quaternion.
14
+ # The following code instanciate a quaternion of
15
+ # q0 + q1 _i_ + q2 _j_ + q3 _k_, where q0 = 1, q1 = 2, q2 = 3 and q3 = 4.
16
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0) # => Quaternion(1.0; Vector[2.0, 3.0, 4.0])
17
+ # q = Quaternion.new(1.0, [2.0, 3.0, 4.0]) # => Quaternion(1.0; Vector[2.0, 3.0, 4.0])
18
+ # q = Quaternion.new(1.0, Vector[2.0, 3.0, 4.0]) # => Quaternion(1.0; Vector[2.0, 3.0, 4.0])
19
+ def initialize *vars
20
+ case vars.size
21
+ when 2
22
+ @w = vars[0]
23
+
24
+ case vars[1]
25
+ when Vector
26
+ @v = vars[1].clone
27
+ when Array
28
+ @v = Vector.elements(vars[1], true)
29
+ else
30
+ raise ArgumentError, "Invalid type"
31
+ end
32
+ when 4
33
+ @w = vars[0]
34
+ @v = Vector.elements(vars[1..3], true)
35
+ end
36
+ end
37
+
38
+ ##
39
+ # Returns element +i+.
40
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
41
+ # q[0] # => 1.0
42
+ # q[:w] # => 1.0
43
+ # q[:x] # => 2.0
44
+ # q[:y] # => 3.0
45
+ # q[:z] # => 4.0
46
+ # q[:v] # => Vector[2.0, 3.0, 4.0]
47
+ def [] i
48
+ case i
49
+ when :w, 0
50
+ return @w
51
+ when :x, 1
52
+ return @v[0]
53
+ when :y, 2
54
+ return @v[1]
55
+ when :z, 3
56
+ return @v[2]
57
+ when :v
58
+ return @v
59
+ else
60
+ raise ArgumentError, "Invalid index"
61
+ end
62
+ end
63
+
64
+ ##
65
+ # Sets +var+ to element +i+
66
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
67
+ # q[0] = 0.0
68
+ # q # => Quaternion(0.0; Vector[2.0, 3.0, 4.0])
69
+ def []= i, var
70
+ case i
71
+ when :w, 0
72
+ @w = var
73
+ when :x, 1
74
+ @v = Vector.elements([var, @v[1], @v[2]])
75
+ when :y, 2
76
+ @v = Vector.elements([@v[0], var, @v[2]])
77
+ when :z, 3
78
+ @v = Vector.elements([@v[0], @v[1], var])
79
+ when :v
80
+ if var.is_a?(Array)
81
+ @v = Vector.elements(var, true)
82
+ elsif var.is_a?(Vector)
83
+ @v = var
84
+ else
85
+ raise ArgumentError, "Invalid type. Should be Array or Vector"
86
+ end
87
+ else
88
+ raise ArgumentError, "Invalid index"
89
+ end
90
+ end
91
+
92
+ ##
93
+ # Compares with other Quaternion
94
+ def == other
95
+ if other.is_a?(self.class)
96
+ if (@w == other[:w]) and (@v == other[:v])
97
+ return true
98
+ else
99
+ return false
100
+ end
101
+ else
102
+ return false
103
+ end
104
+ end
105
+
106
+ ##
107
+ # Returns result of addition.
108
+ # q1 = Quaternion.new(1.0, 2.0, 3.0, 4.0)
109
+ # q2 = Quaternion.new(5.0, 6.0, 7.0, 8.0)
110
+ # q1 + q2 # => Quaternion(6.0; Vector[8.0, 10.0, 12.0])
111
+ def + other
112
+ w = @w + other[:w]
113
+ v = @v + other[:v]
114
+
115
+ return Quaternion.new(w, v)
116
+ end
117
+
118
+ ##
119
+ # Returns result of subtraction
120
+ # q1 = Quaternion.new(1.0, 2.0, 3.0, 4.0)
121
+ # q2 = Quaternion.new(5.0, 4.0, 3.0, 2.0)
122
+ # q1 - q2 # => Quaternion(-4.0; Vector[-2.0, 0.0, 2.0])
123
+ def - other
124
+ w = @w - other[:w]
125
+ v = @v - other[:v]
126
+
127
+ return Quaternion.new(w, v)
128
+ end
129
+
130
+ ##
131
+ # Returns product with +other+.
132
+ # +other+ can be Quaternion or Numeric.
133
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
134
+ # q*2 # => Quarternion(2.0; Vector[4.0, 6.0, 8.0])
135
+ #
136
+ # q1 = Quaternion.new(1.0, 4.0, 5.0, 6.0)
137
+ # q*q1 # => Quaternion(-46.0, Vector[4.0, 12.0, 8.0])
138
+ def * other
139
+ case other
140
+ when self.class
141
+ w = @w*other[:w] - @v.inner_product(other[:v])
142
+ v = @w*other[:v] + @v*other[:w] + @v.cross_product(other[:v])
143
+
144
+ return self.class.new(w, v)
145
+ when Numeric
146
+ w = @w*other
147
+ v = @v*other
148
+
149
+ return self.class.new(w, v)
150
+ else
151
+ raise ArgumentError, "Invalide type"
152
+ end
153
+ end
154
+
155
+ ##
156
+ # Devision by a scalar +other+.
157
+ # Returns Quaternion with each element is divided by +other+.
158
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
159
+ # q/2 # => Quaternion(0.5; Vector[1.0, 1.5, 2.0])
160
+ def / other
161
+ case other
162
+ when Numeric
163
+ w = @w/other
164
+ v = @v/other
165
+
166
+ return self.class.new(w, v)
167
+ else
168
+ raise ArgumentError, "Invalid type"
169
+ end
170
+ end
171
+
172
+ ##
173
+ # Returns conjugate of +self+
174
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
175
+ # q.conjugate # => Quaternion(1.0; Vector[-2.0, -3.0, -4.0])
176
+ def conjugate
177
+ return self.class.new(@w, -@v)
178
+ end
179
+
180
+ ##
181
+ # Returns norm of +self+
182
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
183
+ # q.norm # => 5.477225575051661
184
+ def norm
185
+ return sqrt(@w**2 + @v.inner_product(@v))
186
+ end
187
+ alias magnitude norm
188
+
189
+ ##
190
+ # Returns inverse of +self+.
191
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
192
+ # q.inverse # => Quaternion(0.03333333333333333; Vector[-0.06666666666666667, -0.1, -0.13333333333333333])
193
+ def inverse
194
+ return (self.conjugate)/(self.norm**2)
195
+ end
196
+
197
+ ##
198
+ # Returns normailzed Quaternion
199
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
200
+ # q.normalize # => Quaternion(0.18257418583505536; Vector[0.3651483716701107, 0.5477225575051661, 0.7302967433402214])
201
+ def normalize
202
+ return self/self.magnitude
203
+ end
204
+
205
+ ##
206
+ # Destructive method of Quaternion#normalize
207
+ def normalize!
208
+ m = self.magnitude
209
+
210
+ @w /= m
211
+ @v /= m
212
+
213
+ return self
214
+ end
215
+
216
+ ##
217
+ # Returns string expression of self
218
+ # q = Quaternion.new(1.0, 2.0, 3.0, 4.0)
219
+ # q.inspect # => "Quaternion(1.0; Vector[2.0, 3.0, 4.0])"
220
+ def inspect
221
+ return "Quaternion(#{@w}; #{@v})"
222
+ end
223
+ alias to_s inspect
224
+ end
225
+
@@ -0,0 +1,24 @@
1
+
2
+ class Float # :nodoc:
3
+ alias_method :multi_org, :*
4
+
5
+ def * other
6
+ if other.is_a?(Quaternion)
7
+ other*self
8
+ else
9
+ self.multi_org(other)
10
+ end
11
+ end
12
+ end
13
+
14
+ class Fixnum # :nodoc:
15
+ alias_method :multi_org, :*
16
+
17
+ def * other
18
+ if other.is_a?(Quaternion)
19
+ other*self
20
+ else
21
+ self.multi_org(other)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ class Quaternion
2
+ VERSION = "0.1.0" # :nodoc:
3
+ end
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'quaternion/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "quaternion"
7
+ spec.version = Quaternion::VERSION
8
+ spec.authors = ["Hiroyuki Tanaka"]
9
+ spec.email = ["hryktnk@gmail.com"]
10
+
11
+ spec.summary = %q{Quaternion class}
12
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
13
+ #spec.homepage = "TODO: Put your gem's website or public repo URL here."
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ #if spec.respond_to?(:metadata)
22
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com' to prevent pushes to rubygems.org, or delete to allow pushes to any server."
23
+ #end
24
+
25
+ spec.required_ruby_version = '>= 2.2.1'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.9"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest"
30
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quaternion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiroyuki Tanaka
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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: minitest
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
+ description:
56
+ email:
57
+ - hryktnk@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bin/console
69
+ - bin/setup
70
+ - lib/quaternion.rb
71
+ - lib/quaternion/numeric.rb
72
+ - lib/quaternion/version.rb
73
+ - quaternion.gemspec
74
+ homepage:
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 2.2.1
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.6
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Quaternion class
98
+ test_files: []