fast_matrix 0.2.0 → 0.3.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.
@@ -6,3 +6,6 @@
6
6
 
7
7
  #include "Vector/vector.c"
8
8
  #include "Vector/c_vector.c"
9
+
10
+ #include "LUPDecomposition/lup.c"
11
+ #include "LUPDecomposition/c_lup.c"
@@ -1,5 +1,8 @@
1
1
  #include "fast_matrix.h"
2
- #include <stdio.h>
2
+ #include "Helper/errors.h"
3
+ #include "Matrix/matrix.h"
4
+ #include "Vector/vector.h"
5
+ #include "LUPDecomposition/lup.h"
3
6
 
4
7
 
5
8
  void Init_fast_matrix()
@@ -7,4 +10,5 @@ void Init_fast_matrix()
7
10
  init_fm_errors();
8
11
  init_fm_matrix();
9
12
  init_fm_vector();
10
- }
13
+ init_fm_lup();
14
+ }
@@ -1,10 +1,6 @@
1
1
  #ifndef FAST_MATRIX_H
2
2
  #define FAST_MATRIX_H 1
3
3
 
4
- #include "Helper/errors.h"
5
- #include "Matrix/matrix.h"
6
- #include "Vector/vector.h"
7
-
8
4
  void Init_fast_matrix();
9
5
 
10
6
  #endif /* FAST_MATRIX_H */
data/lib/errors.rb CHANGED
@@ -5,5 +5,5 @@ module FastMatrix
5
5
 
6
6
  class Error < StandardError; end
7
7
  class NotSupportedError < NotImplementedError; end
8
-
8
+ class OperationNotDefinedError < Error; end
9
9
  end
data/lib/fast_matrix.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'fast_matrix/version'
2
2
  require 'vector/vector'
3
3
  require 'matrix/matrix'
4
+ require 'lup_decomposition/lup_decomposition'
4
5
  require 'scalar'
@@ -1,3 +1,3 @@
1
1
  module FastMatrix
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -0,0 +1,24 @@
1
+ require 'fast_matrix/fast_matrix'
2
+
3
+ module FastMatrix
4
+
5
+ class Matrix
6
+ # LUP decomposition for Matrix
7
+ class LUPDecomposition
8
+ #
9
+ # Returns L, U, P in an array
10
+ #
11
+ def to_ary
12
+ [l, u, p]
13
+ end
14
+ #
15
+ # alias for determinant method
16
+ #
17
+ alias determinant det
18
+ #
19
+ # alias for to_ary method
20
+ #
21
+ alias to_a to_ary
22
+ end
23
+ end
24
+ end
data/lib/matrix/matrix.rb CHANGED
@@ -13,12 +13,100 @@ module FastMatrix
13
13
  # Returns the number of columns.
14
14
  #
15
15
  alias column_size column_count
16
+ #
17
+ # Returns the inverse of the matrix.
18
+ #
19
+ alias inv inverse
20
+ alias entrywise_product hadamard_product
16
21
  #
17
22
  # Returns element (+i+,+j+) of the matrix. That is: row +i+, column +j+.
18
23
  #
19
24
  alias element []
20
25
  alias component []
21
26
 
27
+ alias lup_decomposition lup
28
+ alias t transpose
29
+ alias tr trace
30
+
31
+ alias skew_symmetric? antisymmetric?
32
+ #
33
+ # In real numbers is equivalent to checking for symmetry
34
+ #
35
+ alias hermitian? symmetric?
36
+
37
+ #
38
+ # Explicit conversion to a Matrix. Returns self
39
+ #
40
+ def to_matrix
41
+ self
42
+ end
43
+
44
+ def empty?
45
+ row_count * column_count == 0
46
+ end
47
+
48
+ #
49
+ # Returns true if this is a singular matrix.
50
+ #
51
+ def singular?
52
+ determinant == 0
53
+ end
54
+
55
+ #
56
+ # Returns true if this is a regular (i.e. non-singular) matrix.
57
+ #
58
+ def regular?
59
+ not singular?
60
+ end
61
+
62
+ #
63
+ # Returns true if this is a square matrix.
64
+ #
65
+ def square?
66
+ column_count == row_count
67
+ end
68
+
69
+ #
70
+ # Returns the imaginary part of the matrix.
71
+ # Always returns a zero matrix
72
+ #
73
+ def imaginary()
74
+ Matrix.zero(row_count, column_count)
75
+ end
76
+
77
+ alias imag imaginary
78
+
79
+ #
80
+ # Laplace expansion is equal to the determinant in the real numbers
81
+ #
82
+ def laplace_expansion(row: nil, column: nil)
83
+ determinant
84
+ end
85
+ alias cofactor_expansion laplace_expansion
86
+
87
+ #
88
+ # Returns an array containing matrices corresponding to the real and imaginary parts of the matrix
89
+ #
90
+ def rect
91
+ [real, imag]
92
+ end
93
+
94
+ alias rectangular rect
95
+
96
+ #
97
+ # Conjugate real matrix is equal to this matrix
98
+ #
99
+ alias conjugate to_matrix
100
+ alias conj conjugate
101
+ #
102
+ # Real method in real matrix is equal to this matrix
103
+ #
104
+ alias real to_matrix
105
+
106
+ def real?
107
+ true
108
+ end
109
+
22
110
  def collect
23
111
  collected_rows = []
24
112
  rows.each do |i|
data/lib/scalar.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'fast_matrix/fast_matrix'
2
+ require 'errors'
2
3
 
3
4
  module FastMatrix
4
5
 
@@ -13,7 +14,7 @@ module FastMatrix
13
14
  def coerce(other)
14
15
  case other
15
16
  when Numeric
16
- return Scalar.new(other), self
17
+ [Scalar.new(other), self]
17
18
  else
18
19
  raise TypeError, "#{self.class} can't be coerced into #{other.class}"
19
20
  end
@@ -31,21 +32,38 @@ module FastMatrix
31
32
  def coerce(other)
32
33
  case other
33
34
  when Numeric
34
- return Scalar.new(other), self
35
+ [Scalar.new(other), self]
35
36
  else
36
37
  raise TypeError, "#{self.class} can't be coerced into #{other.class}"
37
38
  end
38
39
  end
39
40
  end
40
41
 
41
- private
42
-
43
42
  class Scalar < Numeric # :nodoc:
43
+ attr_reader :value
44
44
 
45
45
  def initialize(value)
46
46
  @value = value
47
47
  end
48
48
 
49
+ def +(other)
50
+ case other
51
+ when Vector, Matrix
52
+ raise OperationNotDefinedError, "#{@value.class}+#{other.class}"
53
+ else
54
+ Scalar.new(@value + other)
55
+ end
56
+ end
57
+
58
+ def -(other)
59
+ case other
60
+ when Vector, Matrix
61
+ raise OperationNotDefinedError, "#{@value.class}+#{other.class}"
62
+ else
63
+ Scalar.new(@value - other)
64
+ end
65
+ end
66
+
49
67
  def *(other)
50
68
  case other
51
69
  when Vector, Matrix
@@ -54,5 +72,38 @@ module FastMatrix
54
72
  Scalar.new(@value * other)
55
73
  end
56
74
  end
75
+
76
+ def /(other)
77
+ case other
78
+ when Vector
79
+ raise OperationNotDefinedError, "#{@value.class}/#{other.class}"
80
+ when Matrix
81
+ self * other.inverse
82
+ else
83
+ Scalar.new(@value / other)
84
+ end
85
+ end
86
+
87
+ def **(other)
88
+ case other
89
+ when Vector, Matrix
90
+ raise OperationNotDefinedError, "#{@value.class}**#{other.class}"
91
+ else
92
+ Scalar.new(@value**other)
93
+ end
94
+ end
95
+
96
+ def ==(other)
97
+ case other
98
+ when Scalar
99
+ other.value == @value
100
+ else
101
+ @value == other
102
+ end
103
+ end
104
+
105
+ def coerce(other)
106
+ @value.coerce(other)
107
+ end
57
108
  end
58
109
  end
data/lib/vector/vector.rb CHANGED
@@ -3,6 +3,11 @@ require 'vector/constructors'
3
3
  module FastMatrix
4
4
  class Vector
5
5
 
6
+ alias cross cross_product
7
+ alias dot inner_product
8
+ alias r magnitude
9
+ alias norm magnitude
10
+
6
11
  #
7
12
  # Create fast vector from standard vector
8
13
  #
@@ -31,7 +36,7 @@ module FastMatrix
31
36
  # Iterate over the elements of this vector
32
37
  #
33
38
  def each
34
- raise NotSupportedError unless block_given?
39
+ return to_enum :each unless block_given?
35
40
 
36
41
  (0...size).each do |i|
37
42
  yield self[i]
@@ -40,6 +45,8 @@ module FastMatrix
40
45
  end
41
46
 
42
47
  def each_with_index
48
+ return to_enum :each_with_index unless block_given?
49
+
43
50
  (0...size).each do |i|
44
51
  yield self[i], i
45
52
  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.2.0
4
+ version: 0.3.0
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-11-12 00:00:00.000000000 Z
11
+ date: 2019-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -101,6 +101,11 @@ files:
101
101
  - ext/fast_matrix/Helper/c_array_operations.h
102
102
  - ext/fast_matrix/Helper/errors.c
103
103
  - ext/fast_matrix/Helper/errors.h
104
+ - ext/fast_matrix/LUPDecomposition/c_lup.c
105
+ - ext/fast_matrix/LUPDecomposition/c_lup.h
106
+ - ext/fast_matrix/LUPDecomposition/helper.h
107
+ - ext/fast_matrix/LUPDecomposition/lup.c
108
+ - ext/fast_matrix/LUPDecomposition/lup.h
104
109
  - ext/fast_matrix/Matrix/c_matrix.c
105
110
  - ext/fast_matrix/Matrix/c_matrix.h
106
111
  - ext/fast_matrix/Matrix/errors.h
@@ -121,6 +126,7 @@ files:
121
126
  - lib/errors.rb
122
127
  - lib/fast_matrix.rb
123
128
  - lib/fast_matrix/version.rb
129
+ - lib/lup_decomposition/lup_decomposition.rb
124
130
  - lib/matrix/constructors.rb
125
131
  - lib/matrix/matrix.rb
126
132
  - lib/scalar.rb