fast_matrix 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,80 @@
1
+ require 'matrix/constructors'
2
+
3
+ module FastMatrix
4
+ # Matrix with fast implementations of + - * determinate in C
5
+ class Matrix
6
+
7
+ # Aliases just for compatibility with standard matrix
8
+ #
9
+ # Returns the number of rows.
10
+ #
11
+ alias row_size row_count
12
+ #
13
+ # Returns the number of columns.
14
+ #
15
+ alias column_size column_count
16
+ #
17
+ # Returns element (+i+,+j+) of the matrix. That is: row +i+, column +j+.
18
+ #
19
+ alias element []
20
+ alias component []
21
+
22
+ def to_s
23
+ convert.to_s
24
+ end
25
+
26
+ def inspect
27
+ convert.inspect
28
+ end
29
+
30
+ #
31
+ # Create fast matrix from standard matrix
32
+ #
33
+ def self.convert(matrix)
34
+ fast_matrix = Matrix.new(matrix.row_count, matrix.column_count)
35
+ (0...matrix.row_count).each do |i|
36
+ (0...matrix.column_count).each do |j|
37
+ fast_matrix[i, j] = matrix[i, j]
38
+ end
39
+ end
40
+ fast_matrix
41
+ end
42
+
43
+ def each_with_index
44
+ (0...row_count).each do |i|
45
+ (0...column_count).each do |j|
46
+ yield self[i, j], i, j
47
+ end
48
+ end
49
+ end
50
+
51
+ def each_with_index!
52
+ (0...row_count).each do |i|
53
+ (0...column_count).each do |j|
54
+ self[i, j] = yield self[i, j], i, j
55
+ end
56
+ end
57
+ self
58
+ end
59
+
60
+ #
61
+ # Convert to standard ruby matrix.
62
+ #
63
+ def convert
64
+ ::Matrix.build(row_size, column_size) { |i, j| self[i, j] }
65
+ end
66
+
67
+ # FIXME: for compare with standard matrix
68
+ def ==(other)
69
+ # TODO: check class and use fast compare from C if possibly
70
+ return false unless %i[row_size column_size \[\]].all? { |x| other.respond_to? x }
71
+ return false unless self.row_size == other.row_size && self.column_size == other.column_size
72
+
73
+ result = true
74
+ each_with_index do |elem, i, j|
75
+ result &&= elem == other[i, j].to_f
76
+ end
77
+ result
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,58 @@
1
+ require 'fast_matrix/fast_matrix'
2
+ require 'errors'
3
+
4
+ module FastMatrix
5
+ class Vector
6
+ #
7
+ # Creates a Vector from a list of elements.
8
+ # Vector[7, 4, ...]
9
+ #
10
+ def self.[](*elems)
11
+ vector = new(elems.size)
12
+ vector.each_with_index! { |_, idx| elems[idx] }
13
+ vector
14
+ end
15
+
16
+ #
17
+ # Creates a vector from an Array.
18
+ # The optional argument +copy+ exists only for compatibility with standard.
19
+ # The optional argument +copy+ cannot be false, unlike standard.
20
+ #
21
+ def self.elements(array, copy = true)
22
+ check_flag_copy(copy)
23
+ self[*array]
24
+ end
25
+
26
+ #
27
+ # Returns a standard basis +n+-vector, where k is the index.
28
+ #
29
+ # Vector.basis(size, index) # => Vector[0, 1, 0]
30
+ #
31
+ def self.basis(size, index)
32
+ result = zero(size)
33
+ result[index] = 1
34
+ result
35
+ end
36
+
37
+ #
38
+ # Return a zero vector.
39
+ #
40
+ # Vector.zero(3) => Vector[0, 0, 0]
41
+ #
42
+ def self.zero(size)
43
+ result = new(size)
44
+ result.each_with_index! { 0 }
45
+ result
46
+ end
47
+
48
+ class << Vector
49
+ private
50
+
51
+ def check_flag_copy(copy)
52
+ unless copy
53
+ raise NotSupportedError, "Can't create vector without copy elements"
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,20 @@
1
+ require 'vector/constructors'
2
+
3
+ module FastMatrix
4
+ class Vector
5
+
6
+ def each_with_index
7
+ (0...size).each do |i|
8
+ yield self[i], i
9
+ end
10
+ end
11
+
12
+ def each_with_index!
13
+ (0...size).each do |i|
14
+ self[i] = yield self[i], i
15
+ end
16
+ self
17
+ end
18
+
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_matrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmcs_ruby
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-24 00:00:00.000000000 Z
11
+ date: 2019-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -83,19 +83,25 @@ files:
83
83
  - LICENSE.txt
84
84
  - README.md
85
85
  - Rakefile
86
- - bin/console
87
- - bin/setup
88
86
  - ext/fast_matrix/c_array_opeartions.c
89
87
  - ext/fast_matrix/c_array_operations.h
88
+ - ext/fast_matrix/errors.c
89
+ - ext/fast_matrix/errors.h
90
90
  - ext/fast_matrix/extconf.rb
91
91
  - ext/fast_matrix/fast_matrix.c
92
92
  - ext/fast_matrix/fast_matrix.h
93
93
  - ext/fast_matrix/matrix.c
94
94
  - ext/fast_matrix/matrix.h
95
+ - ext/fast_matrix/vector.c
96
+ - ext/fast_matrix/vector.h
95
97
  - fast_matrix.gemspec
96
- - lib/constructors.rb
98
+ - lib/errors.rb
97
99
  - lib/fast_matrix.rb
98
100
  - lib/fast_matrix/version.rb
101
+ - lib/matrix/constructors.rb
102
+ - lib/matrix/matrix.rb
103
+ - lib/vector/constructors.rb
104
+ - lib/vector/vector.rb
99
105
  homepage: https://github.com/mmcs-ruby/fast_matrix
100
106
  licenses:
101
107
  - MIT
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "fast_matrix"
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(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/lib/constructors.rb DELETED
@@ -1,109 +0,0 @@
1
- require "fast_matrix/version"
2
-
3
- module FastMatrix
4
- class Matrix
5
-
6
- #
7
- # Creates a matrix of size +row_count+ x +column_count+.
8
- # It fills the values by calling the given block,
9
- # passing the current row and column.
10
- # Returns an enumerator if no block is given.
11
- #
12
- # m = Matrix.build(2, 4) {|row, col| col - row }
13
- # => Matrix[[0, 1, 2, 3], [-1, 0, 1, 2]]
14
- # m = Matrix.build(3) { rand }
15
- # => a 3x3 matrix with random elements
16
- #
17
- def self.build(row_count, column_count = row_count, &block)
18
- matrix = self.new(row_count, column_count)
19
- matrix.each_with_index! { |_, i, j| block.call(i, j) }
20
- end
21
- #
22
- # Creates a matrix where each argument is a row.
23
- # Matrix[ [25, 93], [-1, 66] ]
24
- # => 25 93
25
- # -1 66
26
- #
27
- def self.[](*rows)
28
- columns_count = rows[0].size
29
- rows_count = rows.size
30
- matrix = Matrix.new(rows_count, columns_count)
31
- matrix.each_with_index! { |_, i, j| rows[i][j] }
32
- matrix
33
- end
34
-
35
- #
36
- # Creates a single-column matrix where the values of that column are as given
37
- # in +column+.
38
- # Matrix.column_vector([4,5,6])
39
- # => 4
40
- # 5
41
- # 6
42
- #
43
- def self.column_vector(column)
44
- matrix = Matrix.new(column.size, 1)
45
- column.each_with_index { |elem, i| matrix[i, 0] = elem }
46
- matrix
47
- end
48
-
49
- #
50
- # Creates a single-row matrix where the values of that row are as given in
51
- # +row+.
52
- # Matrix.row_vector([4,5,6])
53
- # => 4 5 6
54
- #
55
- def self.row_vector(row)
56
- matrix = Matrix.new(1, row.size)
57
- row.each_with_index { |elem, j| matrix[0, j] = elem }
58
- matrix
59
- end
60
-
61
- #
62
- # Creates a matrix where the diagonal elements are composed of +values+.
63
- # Matrix.diagonal(9, 5, -3)
64
- # => 9 0 0
65
- # 0 5 0
66
- # 0 0 -3
67
- #
68
- def self.diagonal(*values)
69
- matrix = Matrix.new(values.size, values.size)
70
- matrix.each_with_index! { |_, i, j| i == j ? values[i] : 0 }
71
- matrix
72
- end
73
-
74
- #
75
- # Creates an +n+ by +n+ diagonal matrix where each diagonal element is
76
- # +value+.
77
- # Matrix.scalar(2, 5)
78
- # => 5 0
79
- # 0 5
80
- #
81
- def self.scalar(n, value)
82
- matrix = Matrix.new(n, n)
83
- matrix.each_with_index! { |_, i, j| i == j ? value : 0 }
84
- matrix
85
- end
86
-
87
- #
88
- # Creates an +n+ by +n+ identity matrix.
89
- # Matrix.identity(2)
90
- # => 1 0
91
- # 0 1
92
- #
93
- def self.identity(n)
94
- scalar(n, 1)
95
- end
96
-
97
- #
98
- # Creates a zero matrix +n+ by +n+.
99
- # Matrix.zero(2)
100
- # => 0 0
101
- # 0 0
102
- #
103
- def self.zero(n)
104
- matrix = Matrix.new(n, n)
105
- matrix.each_with_index! { |_, _, _| 0 }
106
- matrix
107
- end
108
- end
109
- end