nmatrix 0.1.0.rc4 → 0.1.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,143 @@
1
+ #--
2
+ # = NMatrix
3
+ #
4
+ # A linear algebra library for scientific computation in Ruby.
5
+ # NMatrix is part of SciRuby.
6
+ #
7
+ # NMatrix was originally inspired by and derived from NArray, by
8
+ # Masahiro Tanaka: http://narray.rubyforge.org
9
+ #
10
+ # == Copyright Information
11
+ #
12
+ # SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
13
+ # NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
14
+ #
15
+ # Please see LICENSE.txt for additional copyright notices.
16
+ #
17
+ # == Contributing
18
+ #
19
+ # By contributing source code to SciRuby, you agree to be bound by
20
+ # our Contributor Agreement:
21
+ #
22
+ # * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
23
+ #
24
+ # == homogeneous.rb
25
+ #
26
+ # This file contains optional shortcuts for generating homogeneous
27
+ # transformations.
28
+ #
29
+ #++
30
+
31
+ class NMatrix
32
+ class << self
33
+ #
34
+ # call-seq:
35
+ # x_rotation(angle_in_radians) -> NMatrix
36
+ # x_rotation(angle_in_radians, dtype: dtype) -> NMatrix
37
+ # y_rotation(angle_in_radians) -> NMatrix
38
+ # y_rotation(angle_in_radians, dtype: dtype) -> NMatrix
39
+ # z_rotation(angle_in_radians) -> NMatrix
40
+ # z_rotation(angle_in_radians, dtype: dtype) -> NMatrix
41
+ #
42
+ # Generate a 4x4 homogeneous transformation matrix representing a rotation
43
+ # about the x, y, or z axis respectively.
44
+ #
45
+ # * *Arguments* :
46
+ # - +angle_in_radians+ -> The angle of rotation in radians.
47
+ # - +dtype+ -> (optional) Default is +:float64+
48
+ # * *Returns* :
49
+ # - A homogeneous transformation matrix consisting of a single rotation.
50
+ #
51
+ # Examples:
52
+ #
53
+ # NMatrix.x_rotation(Math::PI.quo(6)) # =>
54
+ # 1.0 0.0 0.0 0.0
55
+ # 0.0 0.866025 -0.499999 0.0
56
+ # 0.0 0.499999 0.866025 0.0
57
+ # 0.0 0.0 0.0 1.0
58
+ #
59
+ #
60
+ # NMatrix.x_rotation(Math::PI.quo(6), dtype: :float32) # =>
61
+ # 1.0 0.0 0.0 0.0
62
+ # 0.0 0.866025 -0.5 0.0
63
+ # 0.0 0.5 0.866025 0.0
64
+ # 0.0 0.0 0.0 1.0
65
+ #
66
+ def x_rotation angle_in_radians, opts={}
67
+ c = Math.cos(angle_in_radians)
68
+ s = Math.sin(angle_in_radians)
69
+ NMatrix.new(4, [1.0, 0.0, 0.0, 0.0,
70
+ 0.0, c, -s, 0.0,
71
+ 0.0, s, c, 0.0,
72
+ 0.0, 0.0, 0.0, 1.0], {dtype: :float64}.merge(opts))
73
+ end
74
+
75
+ def y_rotation angle_in_radians, opts={}
76
+ c = Math.cos(angle_in_radians)
77
+ s = Math.sin(angle_in_radians)
78
+ NMatrix.new(4, [ c, 0.0, s, 0.0,
79
+ 0.0, 1.0, 0.0, 0.0,
80
+ -s, 0.0, c, 0.0,
81
+ 0.0, 0.0, 0.0, 1.0], {dtype: :float64}.merge(opts))
82
+ end
83
+
84
+ def z_rotation angle_in_radians, opts={}
85
+ c = Math.cos(angle_in_radians)
86
+ s = Math.sin(angle_in_radians)
87
+ NMatrix.new(4, [ c, -s, 0.0, 0.0,
88
+ s, c, 0.0, 0.0,
89
+ 0.0, 0.0, 1.0, 0.0,
90
+ 0.0, 0.0, 0.0, 1.0], {dtype: :float64}.merge(opts))
91
+ end
92
+
93
+
94
+ #
95
+ # call-seq:
96
+ # translation(x, y, z) -> NMatrix
97
+ # translation([x,y,z]) -> NMatrix
98
+ # translation(translation_matrix) -> NMatrix
99
+ # translation(translation_matrix) -> NMatrix
100
+ # translation(translation, dtype: dtype) -> NMatrix
101
+ # translation(x, y, z, dtype: dtype) -> NMatrix
102
+ #
103
+ # Generate a 4x4 homogeneous transformation matrix representing a translation.
104
+ #
105
+ # * *Returns* :
106
+ # - A homogeneous transformation matrix consisting of a translation.
107
+ #
108
+ # Examples:
109
+ #
110
+ # NMatrix.translation(4.0,5.0,6.0) # =>
111
+ # 1.0 0.0 0.0 4.0
112
+ # 0.0 1.0 0.0 5.0
113
+ # 0.0 0.0 1.0 6.0
114
+ # 0.0 0.0 0.0 1.0
115
+ #
116
+ # NMatrix.translation(4.0,5.0,6.0, dtype: :int64) # =>
117
+ # 1 0 0 4
118
+ # 0 1 0 5
119
+ # 0 0 1 6
120
+ # 0 0 0 1
121
+ # NMatrix.translation(4,5,6) # =>
122
+ # 1 0 0 4
123
+ # 0 1 0 5
124
+ # 0 0 1 6
125
+ # 0 0 0 1
126
+ #
127
+ def translation *args
128
+ xyz = args.shift if args.first.is_a?(NMatrix) || args.first.is_a?(Array)
129
+ default_dtype = xyz.respond_to?(:dtype) ? xyz.dtype : NMatrix.guess_dtype(xyz)
130
+ opts = {dtype: default_dtype}
131
+ opts = opts.merge(args.pop) if args.size > 0 && args.last.is_a?(Hash)
132
+ xyz ||= args
133
+
134
+ n = if args.size > 0
135
+ NMatrix.eye(4, opts)
136
+ else
137
+ NMatrix.eye(4, opts)
138
+ end
139
+ n[0..2,3] = xyz
140
+ n
141
+ end
142
+ end
143
+ end
@@ -820,6 +820,18 @@ class NMatrix
820
820
  end
821
821
 
822
822
 
823
+ #
824
+ # call-seq:
825
+ # inject -> symbol
826
+ #
827
+ # This overrides the inject function to use map_stored for yale matrices
828
+ #
829
+ def inject(sym)
830
+ return super(sym) unless self.yale?
831
+ return self.map_stored.inject(sym)
832
+ end
833
+
834
+
823
835
  #
824
836
  # call-seq:
825
837
  # clone_structure -> NMatrix
@@ -31,7 +31,7 @@ class NMatrix
31
31
  MAJOR = 0
32
32
  MINOR = 1
33
33
  TINY = 0
34
- PRE = "rc4"
34
+ PRE = "rc5"
35
35
 
36
36
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join(".")
37
37
  end
data/nmatrix.gemspec CHANGED
@@ -43,12 +43,12 @@ EOF
43
43
 
44
44
  gem.required_ruby_version = '>= 1.9'
45
45
 
46
- gem.add_dependency 'rdoc', '>=4.0.1'
47
- gem.add_development_dependency 'rake'
48
- gem.add_development_dependency 'bundler'
49
- gem.add_development_dependency 'rspec', '~>2.14.1'
50
- gem.add_development_dependency 'rspec-longrun', '~>1.0.1'
51
- gem.add_development_dependency 'pry'
52
- gem.add_development_dependency 'rake-compiler', '~>0.8.1'
46
+ gem.add_dependency 'rdoc', '~>4.0', '>=4.0.1'
47
+ gem.add_development_dependency 'rake', '~>10.3'
48
+ gem.add_development_dependency 'bundler', '~>1.6'
49
+ gem.add_development_dependency 'rspec', '~>2.14'
50
+ gem.add_development_dependency 'rspec-longrun', '~>1.0'
51
+ gem.add_development_dependency 'pry', '~>0.10'
52
+ gem.add_development_dependency 'rake-compiler', '~>0.8'
53
53
  end
54
54
 
@@ -562,4 +562,20 @@ describe 'NMatrix' do
562
562
  end
563
563
  end
564
564
  end
565
+
566
+ context "#inject" do
567
+ it "should sum columns of yale matrix correctly" do
568
+ n = NMatrix.new([4, 3], stype: :yale, default: 0)
569
+ n[0,0] = 1
570
+ n[1,1] = 2
571
+ n[2,2] = 4
572
+ n[3,2] = 8
573
+ column_sums = []
574
+ n.cols.times do |i|
575
+ column_sums << n.col(i).inject(:+)
576
+ end
577
+ expect(column_sums).to eq([1, 2, 12])
578
+ end
579
+ end
580
+
565
581
  end
@@ -0,0 +1,91 @@
1
+ # = NMatrix
2
+ #
3
+ # A linear algebra library for scientific computation in Ruby.
4
+ # NMatrix is part of SciRuby.
5
+ #
6
+ # NMatrix was originally inspired by and derived from NArray, by
7
+ # Masahiro Tanaka: http://narray.rubyforge.org
8
+ #
9
+ # == Copyright Information
10
+ #
11
+ # SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
12
+ # NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
13
+ #
14
+ # Please see LICENSE.txt for additional copyright notices.
15
+ #
16
+ # == Contributing
17
+ #
18
+ # By contributing source code to SciRuby, you agree to be bound by
19
+ # our Contributor Agreement:
20
+ #
21
+ # * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
22
+ #
23
+ # == homogeneous_spec.rb
24
+ #
25
+ # Specs for the homogeneous transformation matrix methods.
26
+ #
27
+
28
+ require 'spec_helper'
29
+ require "./lib/nmatrix/homogeneous.rb"
30
+
31
+ require 'pry'
32
+
33
+ describe 'NMatrix' do
34
+ context "#x_rotation" do
35
+ it "should generate a matrix representing a rotation about the x axis" do
36
+ x = NMatrix.x_rotation(Math::PI.quo(6))
37
+ expect(x).to be_within(1e-8).of(NMatrix.new([4,4], [1.0, 0.0, 0.0, 0.0,
38
+ 0.0, Math.cos(Math::PI.quo(6)), -0.5, 0.0,
39
+ 0.0, 0.5, Math.cos(Math::PI.quo(6)), 0.0,
40
+ 0.0, 0.0, 0.0, 1.0] ))
41
+ end
42
+ end
43
+
44
+
45
+ context "#y_rotation" do
46
+ it "should generate a matrix representing a rotation about the y axis" do
47
+ y = NMatrix.y_rotation(Math::PI.quo(6))
48
+ expect(y).to be_within(1e-8).of(NMatrix.new([4,4], [Math.cos(Math::PI.quo(6)), 0.0, 0.5, 0.0,
49
+ 0.0, 1.0, 0.0, 0.0,
50
+ -0.5, 0.0, Math.cos(Math::PI.quo(6)), 0.0,
51
+ 0.0, 0.0, 0.0, 1.0] ))
52
+ end
53
+ end
54
+
55
+ context "#z_rotation" do
56
+ it "should generate a matrix representing a rotation about the z axis" do
57
+ z = NMatrix.z_rotation(Math::PI.quo(6))
58
+ expect(z).to be_within(1e-8).of(NMatrix.new([4,4], [Math.cos(Math::PI.quo(6)), -0.5, 0.0, 0.0,
59
+ 0.5, Math.cos(Math::PI.quo(6)), 0.0, 0.0,
60
+ 0.0, 0.0, 1.0, 0.0,
61
+ 0.0, 0.0, 0.0, 1.0] ))
62
+ end
63
+ end
64
+
65
+ context "#translation" do
66
+ it "should generate a translation matrix from an Array" do
67
+ t = NMatrix.translation([4,5,6])
68
+ expect(t).to be_within(1e-8).of(NMatrix.new([4,4], [1, 0, 0, 4,
69
+ 0, 1, 0, 5,
70
+ 0, 0, 1, 6,
71
+ 0, 0, 0, 1] ))
72
+ end
73
+
74
+ it "should generate a translation matrix from x, y, and z values" do
75
+ t = NMatrix.translation(4,5,6)
76
+ expect(t).to be_within(1e-8).of(NMatrix.new([4,4], [1, 0, 0, 4,
77
+ 0, 1, 0, 5,
78
+ 0, 0, 1, 6,
79
+ 0, 0, 0, 1] ))
80
+ end
81
+
82
+ it "should generate a translation matrix from an NMatrix with correctly inferred dtype" do
83
+ t = NMatrix.translation(NMatrix.new([3,1], [4,5,6], dtype: :float64) )
84
+ expect(t).to be_within(1e-8).of(NMatrix.new([4,4], [1, 0, 0, 4,
85
+ 0, 1, 0, 5,
86
+ 0, 0, 1, 6,
87
+ 0, 0, 0, 1] ))
88
+ expect(t.dtype).to be(:float64)
89
+ end
90
+ end
91
+ end
data/spec/rspec_spec.rb CHANGED
@@ -28,7 +28,7 @@ require 'spec_helper'
28
28
 
29
29
  describe "RSpec" do
30
30
  it "should permit #be_within to be used on a dense NMatrix" do
31
- expect(NMatrix.new(:dense, [4,1], 1.0, :complex128) / 10000.0).to be_within(0.00000001).of(NMatrix.new(:dense, [4,1], 0.0001, :float64))
32
- expect(NMatrix.new(:dense, [4,1], 1.0, :complex128) / 10000.0).not_to be_within(0.00000001).of(NMatrix.new(:dense, [4,1], 1.0, :float64))
31
+ expect(NMatrix.new([4,1], 1.0, dtype: :complex128, stype: :dense) / 10000.0).to be_within(0.00000001).of(NMatrix.new([4,1], 0.0001, dtype: :float64, stype: :dense))
32
+ expect(NMatrix.new([4,1], 1.0, dtype: :complex128, stype: :dense) / 10000.0).not_to be_within(0.00000001).of(NMatrix.new([4,1], 1.0, dtype: :float64, stype: :dense))
33
33
  end
34
34
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nmatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.rc4
4
+ version: 0.1.0.rc5
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Woods
@@ -10,106 +10,112 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-07-24 00:00:00.000000000 Z
13
+ date: 2014-08-01 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rdoc
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - '>='
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '4.0'
22
+ - - ">="
20
23
  - !ruby/object:Gem::Version
21
24
  version: 4.0.1
22
25
  type: :runtime
23
26
  prerelease: false
24
27
  version_requirements: !ruby/object:Gem::Requirement
25
28
  requirements:
26
- - - '>='
29
+ - - "~>"
30
+ - !ruby/object:Gem::Version
31
+ version: '4.0'
32
+ - - ">="
27
33
  - !ruby/object:Gem::Version
28
34
  version: 4.0.1
29
35
  - !ruby/object:Gem::Dependency
30
36
  name: rake
31
37
  requirement: !ruby/object:Gem::Requirement
32
38
  requirements:
33
- - - '>='
39
+ - - "~>"
34
40
  - !ruby/object:Gem::Version
35
- version: '0'
41
+ version: '10.3'
36
42
  type: :development
37
43
  prerelease: false
38
44
  version_requirements: !ruby/object:Gem::Requirement
39
45
  requirements:
40
- - - '>='
46
+ - - "~>"
41
47
  - !ruby/object:Gem::Version
42
- version: '0'
48
+ version: '10.3'
43
49
  - !ruby/object:Gem::Dependency
44
50
  name: bundler
45
51
  requirement: !ruby/object:Gem::Requirement
46
52
  requirements:
47
- - - '>='
53
+ - - "~>"
48
54
  - !ruby/object:Gem::Version
49
- version: '0'
55
+ version: '1.6'
50
56
  type: :development
51
57
  prerelease: false
52
58
  version_requirements: !ruby/object:Gem::Requirement
53
59
  requirements:
54
- - - '>='
60
+ - - "~>"
55
61
  - !ruby/object:Gem::Version
56
- version: '0'
62
+ version: '1.6'
57
63
  - !ruby/object:Gem::Dependency
58
64
  name: rspec
59
65
  requirement: !ruby/object:Gem::Requirement
60
66
  requirements:
61
- - - ~>
67
+ - - "~>"
62
68
  - !ruby/object:Gem::Version
63
- version: 2.14.1
69
+ version: '2.14'
64
70
  type: :development
65
71
  prerelease: false
66
72
  version_requirements: !ruby/object:Gem::Requirement
67
73
  requirements:
68
- - - ~>
74
+ - - "~>"
69
75
  - !ruby/object:Gem::Version
70
- version: 2.14.1
76
+ version: '2.14'
71
77
  - !ruby/object:Gem::Dependency
72
78
  name: rspec-longrun
73
79
  requirement: !ruby/object:Gem::Requirement
74
80
  requirements:
75
- - - ~>
81
+ - - "~>"
76
82
  - !ruby/object:Gem::Version
77
- version: 1.0.1
83
+ version: '1.0'
78
84
  type: :development
79
85
  prerelease: false
80
86
  version_requirements: !ruby/object:Gem::Requirement
81
87
  requirements:
82
- - - ~>
88
+ - - "~>"
83
89
  - !ruby/object:Gem::Version
84
- version: 1.0.1
90
+ version: '1.0'
85
91
  - !ruby/object:Gem::Dependency
86
92
  name: pry
87
93
  requirement: !ruby/object:Gem::Requirement
88
94
  requirements:
89
- - - '>='
95
+ - - "~>"
90
96
  - !ruby/object:Gem::Version
91
- version: '0'
97
+ version: '0.10'
92
98
  type: :development
93
99
  prerelease: false
94
100
  version_requirements: !ruby/object:Gem::Requirement
95
101
  requirements:
96
- - - '>='
102
+ - - "~>"
97
103
  - !ruby/object:Gem::Version
98
- version: '0'
104
+ version: '0.10'
99
105
  - !ruby/object:Gem::Dependency
100
106
  name: rake-compiler
101
107
  requirement: !ruby/object:Gem::Requirement
102
108
  requirements:
103
- - - ~>
109
+ - - "~>"
104
110
  - !ruby/object:Gem::Version
105
- version: 0.8.1
111
+ version: '0.8'
106
112
  type: :development
107
113
  prerelease: false
108
114
  version_requirements: !ruby/object:Gem::Requirement
109
115
  requirements:
110
- - - ~>
116
+ - - "~>"
111
117
  - !ruby/object:Gem::Version
112
- version: 0.8.1
118
+ version: '0.8'
113
119
  description: NMatrix is an experimental linear algebra library for Ruby, written mostly
114
120
  in C.
115
121
  email:
@@ -119,9 +125,9 @@ extensions:
119
125
  - ext/nmatrix/extconf.rb
120
126
  extra_rdoc_files: []
121
127
  files:
122
- - .gitignore
123
- - .rspec
124
- - .travis.yml
128
+ - ".gitignore"
129
+ - ".rspec"
130
+ - ".travis.yml"
125
131
  - CONTRIBUTING.md
126
132
  - Gemfile
127
133
  - History.txt
@@ -195,6 +201,7 @@ files:
195
201
  - lib/nmatrix.rb
196
202
  - lib/nmatrix/blas.rb
197
203
  - lib/nmatrix/enumerate.rb
204
+ - lib/nmatrix/homogeneous.rb
198
205
  - lib/nmatrix/io/market.rb
199
206
  - lib/nmatrix/io/mat5_reader.rb
200
207
  - lib/nmatrix/io/mat_reader.rb
@@ -218,6 +225,7 @@ files:
218
225
  - spec/4x5_dense.mat
219
226
  - spec/blas_spec.rb
220
227
  - spec/elementwise_spec.rb
228
+ - spec/homogeneous_spec.rb
221
229
  - spec/io_spec.rb
222
230
  - spec/lapack_spec.rb
223
231
  - spec/leakcheck.rb
@@ -262,17 +270,17 @@ require_paths:
262
270
  - lib
263
271
  required_ruby_version: !ruby/object:Gem::Requirement
264
272
  requirements:
265
- - - '>='
273
+ - - ">="
266
274
  - !ruby/object:Gem::Version
267
275
  version: '1.9'
268
276
  required_rubygems_version: !ruby/object:Gem::Requirement
269
277
  requirements:
270
- - - '>'
278
+ - - ">"
271
279
  - !ruby/object:Gem::Version
272
280
  version: 1.3.1
273
281
  requirements: []
274
282
  rubyforge_project:
275
- rubygems_version: 2.0.3
283
+ rubygems_version: 2.2.0
276
284
  signing_key:
277
285
  specification_version: 4
278
286
  summary: NMatrix is an experimental linear algebra library for Ruby, written mostly
@@ -286,6 +294,7 @@ test_files:
286
294
  - spec/4x5_dense.mat
287
295
  - spec/blas_spec.rb
288
296
  - spec/elementwise_spec.rb
297
+ - spec/homogeneous_spec.rb
289
298
  - spec/io_spec.rb
290
299
  - spec/lapack_spec.rb
291
300
  - spec/leakcheck.rb