extendmatrix 0.3.1 → 0.4
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 +7 -0
- data/.gitignore +6 -0
- data/.rspec +1 -0
- data/History.txt +6 -0
- data/{README.txt → README.md} +12 -13
- data/Rakefile +8 -7
- data/extendmatrix.gemspec +32 -0
- data/lib/extendmatrix.rb +152 -175
- data/lib/version.rb +3 -0
- data/spec/extendmatrix_spec.rb +27 -5
- metadata +79 -106
- data.tar.gz.sig +0 -0
- data/Manifest.txt +0 -9
- data/ORIGINAL_README.txt +0 -22
- data/spec/spec.opts +0 -1
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 41d2eb4e69c1269aea537992ff8651672afbfd06
|
4
|
+
data.tar.gz: 13dc88d722a5503ae54aaf1085cd01474f18f125
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46bcba1edb7edc880547b04a0a96d187e3e81a12f59313c70099e07481e67a7b1bfc5e0442e85f6dba5924d172f82638d1833f5c70975508281149976a52d8e9
|
7
|
+
data.tar.gz: d9b7acf76163487e6d1ac537c314d4479ddba9d4d88367b0b0a19bcd45300289519bb942e578695af97eae4ca0f0cd64883999c69f61b6167290b2de6f14a35d
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/History.txt
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
=== 0.4
|
2
|
+
* Code cleanup
|
3
|
+
* Added separate version.rb file for storing VERSION info
|
4
|
+
* Added gem essentials like gemspec, etc.
|
5
|
+
* Removed support for ruby < 1.9.3
|
6
|
+
|
1
7
|
=== 0.3.1 / 2010-08-16
|
2
8
|
* Bug fix: method mssq returns only the last row squares
|
3
9
|
* Added method diagonal
|
data/{README.txt → README.md}
RENAMED
@@ -1,17 +1,17 @@
|
|
1
|
-
|
1
|
+
# extendmatrix
|
2
2
|
|
3
|
-
* http://github.com/
|
3
|
+
* http://github.com/SciRuby/extendmatrix
|
4
4
|
|
5
|
-
|
5
|
+
## DESCRIPTION:
|
6
6
|
|
7
7
|
The project consists of some enhancements to the Ruby "Matrix" module and includes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg) decompositions, bidiagonalization, eigenvalue and eigenvector calculations.
|
8
8
|
Include some aditional code to obtains marginal for rows and columns.
|
9
9
|
|
10
|
-
Original code
|
10
|
+
Original code done by Cosmin Bonchis as a Google Summer of Code 2007 project for Ruby Central Inc.
|
11
11
|
|
12
|
-
Gem, github repository and current version
|
12
|
+
Gem, github repository and current version mantained by Claudio Bustos and the Ruby Science Foundation.
|
13
13
|
|
14
|
-
|
14
|
+
## SYNOPSIS:
|
15
15
|
|
16
16
|
require 'extendmatrix'
|
17
17
|
v = Vector[1, 2, 3, 4]
|
@@ -36,20 +36,20 @@ Gem, github repository and current version manteined by Claudio Bustos.
|
|
36
36
|
m.eigenvaluesJacobi => Vector[0.523942339006665, 0.0632833995384682, 2.41277426145487]
|
37
37
|
m.cJacobiV
|
38
38
|
# => Matrix[[0.818814082563014, 0.249617871497675, 0.516947208547894], [-0.550168858227442, 0.598307531004925, 0.58253096551128], [-0.163883268313767, -0.761392813580323, 0.62723461144538]]
|
39
|
-
== REQUIREMENTS:
|
40
39
|
|
41
|
-
|
40
|
+
## REQUIREMENTS:
|
42
41
|
|
43
|
-
|
42
|
+
* Ruby > 1.9.3
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
== LICENSE:
|
44
|
+
## INSTALL:
|
48
45
|
|
46
|
+
* sudo gem install extendmatrix
|
49
47
|
|
48
|
+
## LICENSE:
|
50
49
|
|
51
50
|
Copyright [2007] Cosmin Bonchis
|
52
51
|
Copyright [2010] Claudio Bustos
|
52
|
+
Copyright [2015] Ruby Science Foundation
|
53
53
|
|
54
54
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
|
55
55
|
this file except in compliance with the License. You may obtain a copy of the
|
@@ -62,5 +62,4 @@ under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
62
62
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
63
63
|
specific language governing permissions and limitations under the License.
|
64
64
|
|
65
|
-
|
66
65
|
See LICENSE.txt for more details
|
data/Rakefile
CHANGED
@@ -2,12 +2,13 @@
|
|
2
2
|
$:.unshift(File.dirname(__FILE__)+"/lib")
|
3
3
|
require 'rubygems'
|
4
4
|
require 'hoe'
|
5
|
-
require '
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
self.version = Matrix::EXTENSION_VERSION
|
10
|
-
self.developer('Cosmin Bonchis', 'cbonchis_info.uvt.ro')
|
11
|
-
end
|
5
|
+
require 'version'
|
6
|
+
|
7
|
+
require 'rspec'
|
8
|
+
require 'rspec/core/rake_task'
|
12
9
|
|
10
|
+
RSpec::Core::RakeTask.new do |t|
|
11
|
+
t.rspec_opts = ["-c", "-f progress"]
|
12
|
+
t.pattern = 'spec/**/*_spec.rb'
|
13
|
+
end
|
13
14
|
# vim: syntax=ruby
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
$:.unshift File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
require 'version.rb'
|
5
|
+
|
6
|
+
DESCRIPTION = <<MSG
|
7
|
+
The project consists of some enhancements to the Ruby "Matrix" module and
|
8
|
+
includes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg)
|
9
|
+
decompositions, bidiagonalization, eigenvalue and eigenvector calculations.
|
10
|
+
|
11
|
+
Includes some aditional code to obtains marginal for rows and columns.
|
12
|
+
MSG
|
13
|
+
|
14
|
+
Gem::Specification.new do |spec|
|
15
|
+
spec.name = 'extendmatrix'
|
16
|
+
spec.version = Matrix::EXTENSION_VERSION
|
17
|
+
spec.authors = ['Cosmin Bonchis', 'Claudio Bustos', 'Sameer Deshmukh']
|
18
|
+
spec.email = ['sameer.deshmukh93@gmail.com']
|
19
|
+
spec.summary = %q{Enhancements to ruby "Matrix" and "Vector" modules}
|
20
|
+
spec.description = DESCRIPTION
|
21
|
+
spec.homepage = "http://github.com/SciRuby/extendmatrix"
|
22
|
+
spec.license = 'Apache v2'
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0")
|
25
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
26
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler'
|
30
|
+
spec.add_development_dependency 'rake'
|
31
|
+
spec.add_development_dependency 'rspec'
|
32
|
+
end
|
data/lib/extendmatrix.rb
CHANGED
@@ -2,18 +2,6 @@ require 'rational'
|
|
2
2
|
require 'matrix'
|
3
3
|
class Vector
|
4
4
|
include Enumerable
|
5
|
-
# fix for Vector#coerce on Ruby 1.8.x
|
6
|
-
if RUBY_VERSION<="1.9.0"
|
7
|
-
alias_method :old_coerce, :coerce
|
8
|
-
def coerce(other)
|
9
|
-
case other
|
10
|
-
when Numeric
|
11
|
-
return Matrix::Scalar.new(other), self
|
12
|
-
else
|
13
|
-
raise TypeError, "#{self.class} can't be coerced into #{other.class}"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
5
|
|
18
6
|
module Norm
|
19
7
|
def self.sqnorm(obj, p)
|
@@ -21,6 +9,7 @@ class Vector
|
|
21
9
|
end
|
22
10
|
end
|
23
11
|
|
12
|
+
alias :to_ary :to_a
|
24
13
|
alias :length :size
|
25
14
|
alias :index :[]
|
26
15
|
#
|
@@ -221,10 +210,9 @@ class Vector
|
|
221
210
|
end
|
222
211
|
|
223
212
|
class Matrix
|
224
|
-
|
225
|
-
EXTENSION_VERSION="0.3.1"
|
226
213
|
include Enumerable
|
227
214
|
|
215
|
+
alias :to_ary :to_a
|
228
216
|
attr_reader :rows, :wrap
|
229
217
|
@wrap = nil
|
230
218
|
#
|
@@ -250,7 +238,6 @@ class Matrix
|
|
250
238
|
# m[1, 2] => 5
|
251
239
|
# m[3,1..2] => Vector[10, 11]
|
252
240
|
# m[0..1, 0..2] => Matrix[[0, 1, 2], [3, 4, 5]]
|
253
|
-
#
|
254
241
|
def [](i, j)
|
255
242
|
case i
|
256
243
|
when Range
|
@@ -270,9 +257,6 @@ class Matrix
|
|
270
257
|
end
|
271
258
|
end
|
272
259
|
|
273
|
-
|
274
|
-
|
275
|
-
|
276
260
|
#
|
277
261
|
# Set the values of a matrix
|
278
262
|
# m = Matrix.build(3, 3){|i, j| i * 3 + j}
|
@@ -283,7 +267,6 @@ class Matrix
|
|
283
267
|
# m[2,1..2] = Vector[8, 8] => Matrix[[0, 1, 2], [3, 8, 8], [6, 7, 8]]
|
284
268
|
# m[0..1, 0..1] = Matrix[[0, 0, 0],[0, 0, 0]]
|
285
269
|
# => Matrix[[0, 0, 2], [0, 0, 8], [6, 7, 8]]
|
286
|
-
#
|
287
270
|
def []=(i, j, v)
|
288
271
|
case i
|
289
272
|
when Range
|
@@ -318,7 +301,6 @@ class Matrix
|
|
318
301
|
|
319
302
|
#
|
320
303
|
# Return a duplicate matrix, with all elements copied
|
321
|
-
#
|
322
304
|
def dup
|
323
305
|
(self.class).rows(self.rows.dup)
|
324
306
|
end
|
@@ -335,8 +317,6 @@ class Matrix
|
|
335
317
|
# Creates a matrix <tt>n</tt> x <tt>m</tt>
|
336
318
|
# If you provide a block, it will be used to set the values.
|
337
319
|
# If not, <tt>val</tt> will be used
|
338
|
-
#
|
339
|
-
|
340
320
|
def build(n,m,val=0)
|
341
321
|
f = (block_given?)? lambda {|i,j| yield(i, j)} : lambda {|i,j| val}
|
342
322
|
Matrix.rows((0...n).collect {|i| (0...m).collect {|j| f.call(i,j)}})
|
@@ -449,7 +429,6 @@ class Matrix
|
|
449
429
|
def cols_len
|
450
430
|
(0...column_size).collect {|j| max_len_column(j)}
|
451
431
|
end
|
452
|
-
|
453
432
|
alias :to_s_old :to_s
|
454
433
|
|
455
434
|
#
|
@@ -495,18 +474,22 @@ class Matrix
|
|
495
474
|
self[row,column].send(op,other[row,column])
|
496
475
|
end
|
497
476
|
end
|
477
|
+
|
498
478
|
# Element wise multiplication
|
499
479
|
def e_mult(other)
|
500
480
|
elementwise_operation(:*,other)
|
501
481
|
end
|
482
|
+
|
502
483
|
# Element wise multiplication
|
503
484
|
def e_quo(other)
|
504
485
|
elementwise_operation(:quo,other)
|
505
486
|
end
|
487
|
+
|
506
488
|
# Matrix sum of squares
|
507
489
|
def mssq
|
508
490
|
@rows.inject(0){|ac,row| ac+(row.inject(0) {|acr,i| acr+(i**2)})}
|
509
491
|
end
|
492
|
+
|
510
493
|
def eigenpairs
|
511
494
|
eigval, eigvec= eigenvaluesJacobi, cJacobiV
|
512
495
|
eigenpairs=eigval.size.times.map {|i|
|
@@ -514,6 +497,7 @@ class Matrix
|
|
514
497
|
}
|
515
498
|
eigenpairs=eigenpairs.sort{|a,b| a[0]<=>b[0]}.reverse
|
516
499
|
end
|
500
|
+
|
517
501
|
# Returns eigenvalues and eigenvectors of a matrix on a Hash
|
518
502
|
# like CALL EIGEN on SPSS.
|
519
503
|
# * _:eigenvectors_: contains the eigenvectors as columns of a new Matrix, ordered in descendent order
|
@@ -547,8 +531,7 @@ class Matrix
|
|
547
531
|
# :section: Advanced methods
|
548
532
|
#
|
549
533
|
|
550
|
-
#
|
551
|
-
# a hided module of Matrix
|
534
|
+
# a hidden module of Matrix
|
552
535
|
module MMatrix
|
553
536
|
def self.default_block(block)
|
554
537
|
block ? lambda { |i| block.call(i) } : lambda {|i| i }
|
@@ -559,7 +542,6 @@ class Matrix
|
|
559
542
|
# 1) the index of row/column and
|
560
543
|
# 2) the values Vector for changing the row/column and
|
561
544
|
# 3) the range of changes
|
562
|
-
#
|
563
545
|
def self.id_vect_range(args, l)
|
564
546
|
i = args[0] # the column(/the row) to be change
|
565
547
|
vect = args[1] # the values vector
|
@@ -577,7 +559,6 @@ class Matrix
|
|
577
559
|
#
|
578
560
|
# Returns an array with the elements collected from the row "i".
|
579
561
|
# When a block is given, the elements of that vector are iterated.
|
580
|
-
#
|
581
562
|
def row_collect(i, &block)
|
582
563
|
f = MMatrix.default_block(block)
|
583
564
|
@rows[i].collect {|e| f.call(e)}
|
@@ -586,7 +567,6 @@ class Matrix
|
|
586
567
|
#
|
587
568
|
# Returns row vector number "i" like Matrix.row as a Vector.
|
588
569
|
# When the block is given, the elements of row "i" are modified
|
589
|
-
#
|
590
570
|
def row!(i)
|
591
571
|
if block_given?
|
592
572
|
@rows[i].collect! {|e| yield e }
|
@@ -599,176 +579,171 @@ class Matrix
|
|
599
579
|
#
|
600
580
|
# Returns an array with the elements collected from the column "j".
|
601
581
|
# When a block is given, the elements of that vector are iterated.
|
602
|
-
#
|
603
582
|
def column_collect(j, &block)
|
604
583
|
f = MMatrix.default_block(block)
|
605
584
|
(0...row_size).collect {|r| f.call(self[r, j])}
|
606
585
|
end
|
607
586
|
|
608
|
-
#
|
609
|
-
# Returns column vector number "j" as a Vector.
|
610
|
-
# When the block is given, the elements of column "j" are mmodified
|
611
|
-
#
|
612
|
-
def column!(j)
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
587
|
+
#
|
588
|
+
# Returns column vector number "j" as a Vector.
|
589
|
+
# When the block is given, the elements of column "j" are mmodified
|
590
|
+
#
|
591
|
+
def column!(j)
|
592
|
+
if block_given?
|
593
|
+
(0...row_size).collect { |i| @rows[i][j] = yield @rows[i][j] }
|
594
|
+
else
|
595
|
+
column(j)
|
596
|
+
end
|
617
597
|
end
|
618
|
-
|
619
|
-
alias :column_collect! :column!
|
620
|
-
|
621
|
-
#
|
622
|
-
# Set a certain column with the values of a Vector
|
623
|
-
# m = Matrix.build(3, 3){|i, j| i * 3 + j + 1}
|
624
|
-
# m.column= 1, Vector[1, 1, 1], 1..2
|
625
|
-
# m => 1 2 3
|
626
|
-
# 4 1 6
|
627
|
-
# 7 1 9
|
628
|
-
#
|
629
|
-
def column=(args)
|
630
|
-
m = row_size
|
631
|
-
c, v, r = MMatrix.id_vect_range(args, m)
|
632
|
-
(m..r.begin - 1).each{|i| self[i, c] = 0}
|
633
|
-
[v.size, r.entries.size].min.times{|i| self[i + r.begin, c] = v[i]}
|
634
|
-
((v.size + r.begin)..r.entries.last).each {|i| self[i, c] = 0}
|
635
|
-
end
|
636
|
-
|
637
|
-
#
|
638
|
-
# Set a certain row with the values of a Vector
|
639
|
-
# m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
|
640
|
-
# m.row= 0, Vector[0, 0], 1..2
|
641
|
-
# m => 1 0 0
|
642
|
-
# 4 5 6
|
643
|
-
# 7 8 9
|
644
|
-
#
|
645
|
-
def row=(args)
|
646
|
-
i, val, range = MMatrix.id_vect_range(args, column_size)
|
647
|
-
row!(i)[range] = val
|
648
|
-
end
|
649
|
-
|
650
|
-
def norm(p = 2)
|
651
|
-
Vector::Norm.sqnorm(self, p) ** (1.quo(p))
|
652
|
-
end
|
598
|
+
alias :column_collect! :column!
|
653
599
|
|
654
|
-
|
655
|
-
|
656
|
-
|
657
|
-
|
658
|
-
|
659
|
-
#
|
660
|
-
#
|
661
|
-
#
|
662
|
-
def
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
667
|
-
|
668
|
-
#
|
669
|
-
|
670
|
-
def row2matrix(r)
|
671
|
-
if r.is_a? Range
|
672
|
-
a=r.map {|v| v<row_size ? row(v).to_a : nil }.find_all {|v| !v.nil?}
|
673
|
-
else
|
674
|
-
a = row(r).to_a
|
675
|
-
end
|
676
|
-
if r.is_a?(Range) and r.entries.size > 1
|
677
|
-
return Matrix[*a]
|
678
|
-
else
|
679
|
-
return Matrix[a]
|
600
|
+
#
|
601
|
+
# Set a certain column with the values of a Vector
|
602
|
+
# m = Matrix.build(3, 3){|i, j| i * 3 + j + 1}
|
603
|
+
# m.column= 1, Vector[1, 1, 1], 1..2
|
604
|
+
# m => 1 2 3
|
605
|
+
# 4 1 6
|
606
|
+
# 7 1 9
|
607
|
+
#
|
608
|
+
def column=(args)
|
609
|
+
m = row_size
|
610
|
+
c, v, r = MMatrix.id_vect_range(args, m)
|
611
|
+
(m..r.begin - 1).each{|i| self[i, c] = 0}
|
612
|
+
[v.size, r.entries.size].min.times{|i| self[i + r.begin, c] = v[i]}
|
613
|
+
((v.size + r.begin)..r.entries.last).each {|i| self[i, c] = 0}
|
680
614
|
end
|
681
|
-
end
|
682
615
|
|
683
|
-
#
|
684
|
-
#
|
685
|
-
#
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
616
|
+
#
|
617
|
+
# Set a certain row with the values of a Vector
|
618
|
+
# m = Matrix.new(3, 3){|i, j| i * 3 + j + 1}
|
619
|
+
# m.row= 0, Vector[0, 0], 1..2
|
620
|
+
# m => 1 0 0
|
621
|
+
# 4 5 6
|
622
|
+
# 7 8 9
|
623
|
+
#
|
624
|
+
def row=(args)
|
625
|
+
i, val, range = MMatrix.id_vect_range(args, column_size)
|
626
|
+
row!(i)[range] = val
|
691
627
|
end
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
return Matrix[*a.collect{|x| [x]}]
|
628
|
+
|
629
|
+
def norm(p = 2)
|
630
|
+
Vector::Norm.sqnorm(self, p) ** (1.quo(p))
|
696
631
|
end
|
697
|
-
end
|
698
632
|
|
699
|
-
|
700
|
-
|
701
|
-
|
702
|
-
|
703
|
-
}
|
704
|
-
end
|
705
|
-
# Returns the marginal of columns
|
706
|
-
def column_sum
|
707
|
-
(0...column_size).collect {|i|
|
708
|
-
column(i).sum
|
709
|
-
}
|
710
|
-
end
|
711
|
-
# Calculate sum of cells
|
712
|
-
def total_sum
|
713
|
-
row_sum.inject(&:+)
|
714
|
-
end
|
633
|
+
def norm_frobenius
|
634
|
+
norm
|
635
|
+
end
|
636
|
+
alias :normF :norm_frobenius
|
715
637
|
|
716
|
-
module LU
|
717
638
|
#
|
718
|
-
#
|
639
|
+
# Tests if the matrix is empty or not
|
719
640
|
#
|
720
|
-
def
|
721
|
-
|
722
|
-
tk = t[k, 0]
|
723
|
-
(0..k).each{|i| t[i, 0] = 0}
|
724
|
-
return t if tk == 0
|
725
|
-
(k+1...mat.row_size).each{|i| t[i, 0] = t[i, 0].to_f / tk}
|
726
|
-
t
|
641
|
+
def empty?
|
642
|
+
@rows.empty? if @rows
|
727
643
|
end
|
728
644
|
|
729
645
|
#
|
730
|
-
#
|
646
|
+
# Returns row(s) of matrix as a Matrix
|
731
647
|
#
|
732
|
-
|
733
|
-
|
734
|
-
|
735
|
-
|
736
|
-
|
648
|
+
|
649
|
+
def row2matrix(r)
|
650
|
+
if r.is_a? Range
|
651
|
+
a=r.map {|v| v<row_size ? row(v).to_a : nil }.find_all {|v| !v.nil?}
|
652
|
+
else
|
653
|
+
a = row(r).to_a
|
654
|
+
end
|
655
|
+
if r.is_a?(Range) and r.entries.size > 1
|
656
|
+
return Matrix[*a]
|
657
|
+
else
|
658
|
+
return Matrix[a]
|
659
|
+
end
|
737
660
|
end
|
738
661
|
|
739
662
|
#
|
740
|
-
#
|
741
|
-
# where L is lower triangular and U is upper triangular
|
663
|
+
# Returns the column/s of matrix as a Matrix
|
742
664
|
#
|
743
|
-
def
|
744
|
-
|
745
|
-
|
746
|
-
|
747
|
-
|
748
|
-
|
749
|
-
|
750
|
-
|
751
|
-
|
665
|
+
def column2matrix(c)
|
666
|
+
if c.is_a?(Range)
|
667
|
+
a=c.map {|v| column(v).to_a}.find_all {|v| v.size>0}
|
668
|
+
else
|
669
|
+
a = column(c).to_a
|
670
|
+
end
|
671
|
+
if c.is_a?(Range)
|
672
|
+
return Matrix.columns(a)
|
673
|
+
else
|
674
|
+
return Matrix[*a.collect{|x| [x]}]
|
675
|
+
end
|
676
|
+
end
|
677
|
+
|
678
|
+
# Returns the marginal of rows
|
679
|
+
def row_sum
|
680
|
+
(0...row_size).collect {|i|
|
681
|
+
row(i).sum
|
752
682
|
}
|
753
|
-
return l, u
|
754
683
|
end
|
755
|
-
|
684
|
+
# Returns the marginal of columns
|
685
|
+
def column_sum
|
686
|
+
(0...column_size).collect {|i|
|
687
|
+
column(i).sum
|
688
|
+
}
|
689
|
+
end
|
690
|
+
# Calculate sum of cells
|
691
|
+
def total_sum
|
692
|
+
row_sum.inject(&:+)
|
693
|
+
end
|
756
694
|
|
757
|
-
|
758
|
-
#
|
759
|
-
#
|
760
|
-
#
|
761
|
-
def
|
762
|
-
|
763
|
-
|
695
|
+
module LU
|
696
|
+
#
|
697
|
+
# Return the Gauss vector, MC, Golub, 3.2.1 Gauss Transformation, p94
|
698
|
+
#
|
699
|
+
def self.gauss_vector(mat, k)
|
700
|
+
t = mat.column2matrix(k)
|
701
|
+
tk = t[k, 0]
|
702
|
+
(0..k).each{|i| t[i, 0] = 0}
|
703
|
+
return t if tk == 0
|
704
|
+
(k+1...mat.row_size).each{|i| t[i, 0] = t[i, 0].to_f / tk}
|
705
|
+
t
|
706
|
+
end
|
764
707
|
|
765
|
-
#
|
766
|
-
# Return the
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
|
708
|
+
#
|
709
|
+
# Return the Gauss transformation matrix: M_k = I - tau * e_k^T
|
710
|
+
def self.gauss(mat, k)
|
711
|
+
i = Matrix.I(mat.column_size)
|
712
|
+
tau = gauss_vector(mat, k)
|
713
|
+
e = i.row2matrix(k)
|
714
|
+
i - tau * e
|
715
|
+
end
|
716
|
+
|
717
|
+
#
|
718
|
+
# LU factorization: A = LU
|
719
|
+
# where L is lower triangular and U is upper triangular
|
720
|
+
def self.factorization(mat)
|
721
|
+
u = mat.clone
|
722
|
+
n = u.column_size
|
723
|
+
i = Matrix.I(n)
|
724
|
+
l = i.clone
|
725
|
+
(n-1).times {|k|
|
726
|
+
mk = gauss(u, k)
|
727
|
+
u = mk * u # M_{n-1} * ... * M_1 * A = U
|
728
|
+
l += i - mk # L = M_1^{-1} * ... * M_{n-1}^{-1} = I + sum_{k=1}^{n-1} tau * e
|
729
|
+
}
|
730
|
+
return l, u
|
731
|
+
end
|
732
|
+
end
|
733
|
+
|
734
|
+
#
|
735
|
+
# Return the upper triangular matrix of LU factorization
|
736
|
+
# M_{n-1} * ... * M_1 * A = U
|
737
|
+
def U
|
738
|
+
LU.factorization(self)[1]
|
739
|
+
end
|
740
|
+
|
741
|
+
#
|
742
|
+
# Return the lower triangular matrix of LU factorization
|
743
|
+
# L = M_1^{-1} * ... * M_{n-1}^{-1} = I + sum_{k=1}^{n-1} tau * e
|
744
|
+
def L
|
745
|
+
LU.factorization(self)[0]
|
746
|
+
end
|
772
747
|
|
773
748
|
module Householder
|
774
749
|
#
|
@@ -1083,8 +1058,12 @@ end
|
|
1083
1058
|
#
|
1084
1059
|
def self.J(p, q, c, s, n)
|
1085
1060
|
j = Matrix.I(n)
|
1086
|
-
|
1087
|
-
j[
|
1061
|
+
|
1062
|
+
j[p,p] = c
|
1063
|
+
j[p,q] = s
|
1064
|
+
j[q,p] = -s
|
1065
|
+
j[q,q] = c
|
1066
|
+
|
1088
1067
|
j
|
1089
1068
|
end
|
1090
1069
|
end
|
@@ -1133,5 +1112,3 @@ end
|
|
1133
1112
|
cJacobi(tol)[1]
|
1134
1113
|
end
|
1135
1114
|
end
|
1136
|
-
|
1137
|
-
|
data/lib/version.rb
ADDED
data/spec/extendmatrix_spec.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
$:.unshift(File.dirname(__FILE__)+"/../lib")
|
2
|
-
require '
|
3
|
-
require '
|
2
|
+
require 'rspec'
|
3
|
+
require 'rspec/core/rake_task'
|
4
4
|
|
5
5
|
require 'extendmatrix'
|
6
6
|
|
@@ -90,6 +90,11 @@ describe "Vector class extension:" do
|
|
90
90
|
it "sum method returns the sum of elements" do
|
91
91
|
@v.sum.should==10
|
92
92
|
end
|
93
|
+
|
94
|
+
it "allows for multiple assignment" do
|
95
|
+
a, b, c, d = @v
|
96
|
+
[a, b, c, d].should == [1, 2, 3, 4]
|
97
|
+
end
|
93
98
|
end
|
94
99
|
|
95
100
|
describe "Matrix class extension:" do
|
@@ -287,10 +292,22 @@ describe "Matrix class extension:" do
|
|
287
292
|
eigenvalues=[1.92,1.88,0.0,0.0]
|
288
293
|
eigen=m.eigen
|
289
294
|
eigen[:eigenvalues].each_with_index do |v,i|
|
290
|
-
v.should
|
295
|
+
v.should be_within(0.01).of(eigenvalues[i])
|
291
296
|
end
|
292
297
|
eigenvectors=Matrix[[0.5, 0.5, 0.0, 0.707106781186547], [0.5, 0.5, 0.0, -0.707106781186547], [0.5, -0.5, 0.707106781186547, 0.0], [0.5, -0.5, -0.707106781186547, 0.0]]
|
293
|
-
Matrix.equal_in_delta?(eigen[:eigenvectors], eigenvectors).
|
298
|
+
expect(Matrix.equal_in_delta?(eigen[:eigenvectors], eigenvectors)).to be(true)
|
299
|
+
end
|
300
|
+
it "eigenpairs" do
|
301
|
+
m=Matrix[[0.95,0.95,0.01,0.01],[0.95,0.95,0.01,0.01],[0.01, 0.01,0.95,0.95], [0.01, 0.01, 0.95, 0.95]]
|
302
|
+
eigenpairs=[[1.92, Vector[0.5, 0.5, 0.5, 0.5]], [1.88, Vector[0.5, 0.5, -0.5, -0.5]], [0.0, Vector[0.0, 0.0, 0.707, -0.707]], [0.0, Vector[0.707, -0.707, 0.0, 0.0]]]
|
303
|
+
observed=m.eigenpairs
|
304
|
+
eigenpairs.each_with_index do |v,i|
|
305
|
+
observed[i][0].should be_within(0.001).of(v[0])
|
306
|
+
observed[i][1].each_with_index {|vv,ii|
|
307
|
+
vv.should be_within(0.001).of(v[1][ii])
|
308
|
+
}
|
309
|
+
|
310
|
+
end
|
294
311
|
end
|
295
312
|
it "sqrt" do
|
296
313
|
m=Matrix[[1,4,9],[16,25,36]]
|
@@ -418,6 +435,11 @@ describe "Matrix class extension:" do
|
|
418
435
|
[4, 5, 4, 1]]
|
419
436
|
e = Matrix[[-0.26828, 0, 0, 0], [0, -5.97550, 0, 0], [0, 0, 1.01373, 0], [0, 0, 0, 9.23004]]
|
420
437
|
Matrix.diag_in_delta?(e, a.cJacobiA, 1.0e-5).should == true
|
421
|
-
end
|
438
|
+
end
|
439
|
+
|
440
|
+
it "allows for multiple assignment" do
|
441
|
+
a, b = @m
|
442
|
+
[a, b].should == [[1, 2, 222], [2, 33, 4]]
|
443
|
+
end
|
422
444
|
end
|
423
445
|
|
metadata
CHANGED
@@ -1,130 +1,103 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: extendmatrix
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
segments:
|
6
|
-
- 0
|
7
|
-
- 3
|
8
|
-
- 1
|
9
|
-
version: 0.3.1
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.4'
|
10
5
|
platform: ruby
|
11
|
-
authors:
|
6
|
+
authors:
|
12
7
|
- Cosmin Bonchis
|
8
|
+
- Claudio Bustos
|
9
|
+
- Sameer Deshmukh
|
13
10
|
autorequire:
|
14
11
|
bindir: bin
|
15
|
-
cert_chain:
|
16
|
-
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
yjyXyTc/oJiw1cXskUL8UtMWZmrwNLHXuZWWIMzkjiz3UNdhJr/t5ROk8S2WPznl
|
27
|
-
0bMy/PMIlAbqWolRn1zl2VFJ3TaXScbqImY8Wf4g62b/1ZSUlGrtnLNsCYXrWiso
|
28
|
-
UPUCAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFGu9
|
29
|
-
rrJ1H64qRmNNu3Jj/Qjvh0u5MA0GCSqGSIb3DQEBBQUAA4IBAQCV0Unka5isrhZk
|
30
|
-
GjqSDqY/6hF+G2pbFcbWUpjmC8NWtAxeC+7NGV3ljd0e1SLfoyBj4gnFtFmY8qX4
|
31
|
-
K02tgSZM0eDV8TpgFpWXzK6LzHvoanuahHLZEtk/+Z885lFene+nHadkem1n9iAB
|
32
|
-
cs96JO9/JfFyuXM27wFAwmfHCmJfPF09R4VvGHRAvb8MGzSVgk2i06OJTqkBTwvv
|
33
|
-
JHJdoyw3+8bw9RJ+jLaNoQ+xu+1pQdS2bb3m7xjZpufml/m8zFCtjYM/7qgkKR8z
|
34
|
-
/ZZt8lCiKfFArppRrZayE2FVsps4X6WwBdrKTMZ0CKSXTRctbEj1BAZ67eoTvBBt
|
35
|
-
rpP0jjs0
|
36
|
-
-----END CERTIFICATE-----
|
37
|
-
|
38
|
-
date: 2010-08-16 00:00:00 -04:00
|
39
|
-
default_executable:
|
40
|
-
dependencies:
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rubyforge
|
12
|
+
cert_chain: []
|
13
|
+
date: 2015-05-23 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: bundler
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ">="
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
43
23
|
prerelease: false
|
44
|
-
|
45
|
-
requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
46
26
|
- - ">="
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: '0'
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
53
36
|
type: :development
|
54
|
-
version_requirements: *id001
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: hoe
|
57
37
|
prerelease: false
|
58
|
-
|
59
|
-
requirements:
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
60
47
|
- - ">="
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
- 2
|
64
|
-
- 6
|
65
|
-
- 1
|
66
|
-
version: 2.6.1
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
67
50
|
type: :development
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
description: "The project consists of some enhancements to the Ruby \"Matrix\" module
|
58
|
+
and \nincludes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg) \ndecompositions,
|
59
|
+
bidiagonalization, eigenvalue and eigenvector calculations.\n\nIncludes some aditional
|
60
|
+
code to obtains marginal for rows and columns.\n"
|
61
|
+
email:
|
62
|
+
- sameer.deshmukh93@gmail.com
|
78
63
|
executables: []
|
79
|
-
|
80
64
|
extensions: []
|
81
|
-
|
82
|
-
|
65
|
+
extra_rdoc_files: []
|
66
|
+
files:
|
67
|
+
- ".gitignore"
|
68
|
+
- ".rspec"
|
83
69
|
- History.txt
|
84
70
|
- LICENSE.txt
|
85
|
-
-
|
86
|
-
- ORIGINAL_README.txt
|
87
|
-
- README.txt
|
88
|
-
files:
|
89
|
-
- History.txt
|
90
|
-
- LICENSE.txt
|
91
|
-
- Manifest.txt
|
92
|
-
- ORIGINAL_README.txt
|
93
|
-
- README.txt
|
71
|
+
- README.md
|
94
72
|
- Rakefile
|
73
|
+
- extendmatrix.gemspec
|
95
74
|
- lib/extendmatrix.rb
|
75
|
+
- lib/version.rb
|
96
76
|
- spec/extendmatrix_spec.rb
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
77
|
+
homepage: http://github.com/SciRuby/extendmatrix
|
78
|
+
licenses:
|
79
|
+
- Apache v2
|
80
|
+
metadata: {}
|
102
81
|
post_install_message:
|
103
|
-
rdoc_options:
|
104
|
-
|
105
|
-
- README.txt
|
106
|
-
require_paths:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
107
84
|
- lib
|
108
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
109
|
-
requirements:
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
110
87
|
- - ">="
|
111
|
-
- !ruby/object:Gem::Version
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
-
requirements:
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
117
92
|
- - ">="
|
118
|
-
- !ruby/object:Gem::Version
|
119
|
-
|
120
|
-
- 0
|
121
|
-
version: "0"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
122
95
|
requirements: []
|
123
|
-
|
124
|
-
|
125
|
-
rubygems_version: 1.3.6
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.6
|
126
98
|
signing_key:
|
127
|
-
specification_version:
|
128
|
-
summary:
|
129
|
-
test_files:
|
130
|
-
|
99
|
+
specification_version: 4
|
100
|
+
summary: Enhancements to ruby "Matrix" and "Vector" modules
|
101
|
+
test_files:
|
102
|
+
- spec/extendmatrix_spec.rb
|
103
|
+
has_rdoc:
|
data.tar.gz.sig
DELETED
Binary file
|
data/Manifest.txt
DELETED
data/ORIGINAL_README.txt
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
Extensions to the Ruby Matrix module
|
2
|
-
====================================
|
3
|
-
|
4
|
-
This README is a small description of the work done by Cosmin Bonchis as a
|
5
|
-
Google Summer of Code 2007 project for Ruby Central Inc.
|
6
|
-
|
7
|
-
The project consists of some enhancements to the Ruby "Matrix" module and includes: LU and QR (Householder, Givens, Gram Schmidt, Hessenberg) decompositions, bidiagonalization, eigenvalue and eigenvector calculations.
|
8
|
-
|
9
|
-
This archive contains in extendmatrix.rb file the source code of the project, an implementation of mapcar used in extending matrix, and all the tests files in the "tests" directory.
|
10
|
-
|
11
|
-
The code can also be found on the RubyForge repository at http://matrix.rubyforge.org/svn/trunk/ or the project's SVN repository can be checked out through anonymous access with the following command(s).
|
12
|
-
|
13
|
-
svn checkout svn://rubyforge.org/var/svn/matrix
|
14
|
-
svn checkout http://matrix.rubyforge.org/svn/trunk/
|
15
|
-
|
16
|
-
|
17
|
-
Relevant URLs:
|
18
|
-
==============
|
19
|
-
|
20
|
-
Project sources:
|
21
|
-
http://matrix.rubyforge.org/svn/trunk/
|
22
|
-
|
data/spec/spec.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
metadata.gz.sig
DELETED
Binary file
|