matrix 0.3.0 → 0.3.1
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 +4 -4
- data/README.md +2 -2
- data/lib/matrix.rb +43 -16
- data/lib/matrix/version.rb +1 -1
- data/matrix.gemspec +1 -1
- metadata +7 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bfd415fd47ead13b790454f50a6959b76bdc68da569efefd53bf11424ad9c93f
|
4
|
+
data.tar.gz: 78f73f4aed336e4903b48dc1de5a8835dbe019015f9f6d534bd89b380d0d6d16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efe3e7cb0fcce4e68c58530a8f290df1b0ea4bfe94d92b0a0310e537b8bfa4c19f468128e2b505ad33e10a522ed366a996b384c4f7528f111ac5b8982693c99b
|
7
|
+
data.tar.gz: cd798bc7bac9fdaa7586b25691bb2e69402b70a36bf66c821dc7ff406dd2b2f5e7d221535b7516ae809c29ea960cd04bd31072549db8150aebba33d3ef10f95d
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Matrix [](https://badge.fury.io/rb/matrix) [](https://stdgems.org/matrix/) [](https://badge.fury.io/rb/matrix) [](https://stdgems.org/matrix/) [](https://github.com/ruby/matrix/actions?query=workflow%3Atest)
|
2
2
|
|
3
3
|
An implementation of `Matrix` and `Vector` classes.
|
4
4
|
|
@@ -20,7 +20,7 @@ And then execute:
|
|
20
20
|
|
21
21
|
$ bundle
|
22
22
|
|
23
|
-
To make sure the gem takes over the builtin library,
|
23
|
+
To make sure that the gem takes precedence over the builtin library, call `bundle exec ...` (or call `gem 'matrix'` explicitly).
|
24
24
|
|
25
25
|
## Usage
|
26
26
|
|
data/lib/matrix.rb
CHANGED
@@ -532,7 +532,8 @@ class Matrix
|
|
532
532
|
alias map! collect!
|
533
533
|
|
534
534
|
def freeze
|
535
|
-
@rows.freeze
|
535
|
+
@rows.each(&:freeze).freeze
|
536
|
+
|
536
537
|
super
|
537
538
|
end
|
538
539
|
|
@@ -1233,26 +1234,49 @@ class Matrix
|
|
1233
1234
|
# # => 67 96
|
1234
1235
|
# # 48 99
|
1235
1236
|
#
|
1236
|
-
def **(
|
1237
|
-
case
|
1237
|
+
def **(exp)
|
1238
|
+
case exp
|
1238
1239
|
when Integer
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1246
|
-
|
1247
|
-
z = z ? z * x : x if other[0] == 1
|
1248
|
-
return z if (other >>= 1).zero?
|
1249
|
-
x *= x
|
1240
|
+
case
|
1241
|
+
when exp == 0
|
1242
|
+
_make_sure_it_is_invertible = inverse
|
1243
|
+
self.class.identity(column_count)
|
1244
|
+
when exp < 0
|
1245
|
+
inverse.power_int(-exp)
|
1246
|
+
else
|
1247
|
+
power_int(exp)
|
1250
1248
|
end
|
1251
1249
|
when Numeric
|
1252
1250
|
v, d, v_inv = eigensystem
|
1253
|
-
v * self.class.diagonal(*d.each(:diagonal).map{|e| e **
|
1251
|
+
v * self.class.diagonal(*d.each(:diagonal).map{|e| e ** exp}) * v_inv
|
1252
|
+
else
|
1253
|
+
raise ErrOperationNotDefined, ["**", self.class, exp.class]
|
1254
|
+
end
|
1255
|
+
end
|
1256
|
+
|
1257
|
+
protected def power_int(exp)
|
1258
|
+
# assumes `exp` is an Integer > 0
|
1259
|
+
#
|
1260
|
+
# Previous algorithm:
|
1261
|
+
# build M**2, M**4 = (M**2)**2, M**8, ... and multiplying those you need
|
1262
|
+
# e.g. M**0b1011 = M**11 = M * M**2 * M**8
|
1263
|
+
# ^ ^
|
1264
|
+
# (highlighted the 2 out of 5 multiplications involving `M * x`)
|
1265
|
+
#
|
1266
|
+
# Current algorithm has same number of multiplications but with lower exponents:
|
1267
|
+
# M**11 = M * (M * M**4)**2
|
1268
|
+
# ^ ^ ^
|
1269
|
+
# (highlighted the 3 out of 5 multiplications involving `M * x`)
|
1270
|
+
#
|
1271
|
+
# This should be faster for all (non nil-potent) matrices.
|
1272
|
+
case
|
1273
|
+
when exp == 1
|
1274
|
+
self
|
1275
|
+
when exp.odd?
|
1276
|
+
self * power_int(exp - 1)
|
1254
1277
|
else
|
1255
|
-
|
1278
|
+
sqrt = power_int(exp / 2)
|
1279
|
+
sqrt * sqrt
|
1256
1280
|
end
|
1257
1281
|
end
|
1258
1282
|
|
@@ -2118,6 +2142,9 @@ class Vector
|
|
2118
2142
|
all?(&:zero?)
|
2119
2143
|
end
|
2120
2144
|
|
2145
|
+
#
|
2146
|
+
# Makes the matrix frozen and Ractor-shareable
|
2147
|
+
#
|
2121
2148
|
def freeze
|
2122
2149
|
@elements.freeze
|
2123
2150
|
super
|
data/lib/matrix/version.rb
CHANGED
data/matrix.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |spec|
|
|
16
16
|
spec.summary = %q{An implementation of Matrix and Vector classes.}
|
17
17
|
spec.description = %q{An implementation of Matrix and Vector classes.}
|
18
18
|
spec.homepage = "https://github.com/ruby/matrix"
|
19
|
-
spec.
|
19
|
+
spec.licenses = ["Ruby", "BSD-2-Clause"]
|
20
20
|
spec.required_ruby_version = ">= 2.5.0"
|
21
21
|
|
22
22
|
spec.files = [".gitignore", "Gemfile", "LICENSE.txt", "README.md", "Rakefile", "bin/console", "bin/setup", "lib/matrix.rb", "lib/matrix/eigenvalue_decomposition.rb", "lib/matrix/lup_decomposition.rb", "lib/matrix/version.rb", "matrix.gemspec"]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: matrix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marc-Andre Lafortune
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -59,9 +59,10 @@ files:
|
|
59
59
|
- matrix.gemspec
|
60
60
|
homepage: https://github.com/ruby/matrix
|
61
61
|
licenses:
|
62
|
+
- Ruby
|
62
63
|
- BSD-2-Clause
|
63
64
|
metadata: {}
|
64
|
-
post_install_message:
|
65
|
+
post_install_message:
|
65
66
|
rdoc_options: []
|
66
67
|
require_paths:
|
67
68
|
- lib
|
@@ -76,8 +77,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
77
|
- !ruby/object:Gem::Version
|
77
78
|
version: '0'
|
78
79
|
requirements: []
|
79
|
-
rubygems_version: 3.
|
80
|
-
signing_key:
|
80
|
+
rubygems_version: 3.2.3
|
81
|
+
signing_key:
|
81
82
|
specification_version: 4
|
82
83
|
summary: An implementation of Matrix and Vector classes.
|
83
84
|
test_files: []
|