extendmatrix 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
@@ -1,3 +1,7 @@
1
+ === 0.2.1 / 2010-05-05
2
+
3
+ * Works on Ruby 1.9.2. Fixed conflicts between old and new Matrix implementation
4
+
1
5
  === 0.2.0 / 2010-05-04
2
6
  * More rubyesque implementation of some methods
3
7
  * Matrix#new is not altered and previously extended #new is called #new_with_values
@@ -39,6 +39,7 @@ class Vector
39
39
  end
40
40
  end
41
41
 
42
+
42
43
  #
43
44
  # Sets a vector value/(range of values) with a new value/(values from a vector)
44
45
  # v = Vector[1, 2, 3]
@@ -216,7 +217,7 @@ end
216
217
 
217
218
  class Matrix
218
219
 
219
- EXTENSION_VERSION="0.2.0"
220
+ EXTENSION_VERSION="0.2.1"
220
221
  include Enumerable
221
222
 
222
223
  attr_reader :rows, :wrap
@@ -227,7 +228,11 @@ class Matrix
227
228
  def initialize_old(init_method, *argv) #:nodoc:
228
229
  self.send(init_method, *argv)
229
230
  end
230
-
231
+ # Return an empty matrix on n=0
232
+ # else, returns I(n)
233
+ def self.robust_I(n)
234
+ n>0 ? self.I(n) : self.[]
235
+ end
231
236
  alias :ids :[]
232
237
  #
233
238
  # Return a value or a vector/matrix of values depending
@@ -592,11 +597,15 @@ def empty?
592
597
  end
593
598
 
594
599
  #
595
- # Returns the row/s of matrix as a Matrix
600
+ # Returns row(s) of matrix as a Matrix
596
601
  #
597
602
 
598
603
  def row2matrix(r)
599
- a = row(r).to_a
604
+ if r.is_a? Range
605
+ a=r.map {|v| v<row_size ? row(v).to_a : nil }.find_all {|v| !v.nil?}
606
+ else
607
+ a = row(r).to_a
608
+ end
600
609
  if r.is_a?(Range) and r.entries.size > 1
601
610
  return Matrix[*a]
602
611
  else
@@ -608,9 +617,13 @@ end
608
617
  # Returns the column/s of matrix as a Matrix
609
618
  #
610
619
  def column2matrix(c)
611
- a = column(c).to_a
612
- if c.is_a?(Range) and c.entries.size > 1
613
- return Matrix[*a]
620
+ if c.is_a?(Range)
621
+ a=c.map {|v| column(v).to_a}.find_all {|v| v.size>0}
622
+ else
623
+ a = column(c).to_a
624
+ end
625
+ if c.is_a?(Range)
626
+ return Matrix.columns(a)
614
627
  else
615
628
  return Matrix[*a.collect{|x| [x]}]
616
629
  end
@@ -701,16 +714,15 @@ module Householder
701
714
  a = mat.clone
702
715
  m = a.row_size
703
716
  n = a.column_size
704
- n.times{|j|
717
+ n.times do |j|
705
718
  v, beta = a[j..m - 1, j].house
706
-
707
- h[j] = Matrix.diag(Matrix.I(j), Matrix.I(m-j)- beta * (v * v.t))
708
-
719
+ h[j] = Matrix.diag(Matrix.robust_I(j), Matrix.I(m-j)- beta * (v * v.t))
720
+
709
721
  a[j..m-1, j..n-1] = (Matrix.I(m-j) - beta * (v * v.t)) * a[j..m-1, j..n-1]
710
- a[(j+1)..m-1,j] = v[2..(m-j)] if j < m - 1 }
722
+ a[(j+1)..m-1,j] = v[2..(m-j)] if j < m - 1
723
+ end
711
724
  h
712
725
  end
713
-
714
726
  #
715
727
  # From the essential part of Householder vector
716
728
  # it returns the coresponding upper(U_j)/lower(V_j) matrix
@@ -718,7 +730,9 @@ module Householder
718
730
  def self.bidiagUV(essential, dim, beta)
719
731
  v = Vector.concat(Vector[1], essential)
720
732
  dimv = v.size
721
- Matrix.diag(Matrix.I(dim - dimv), Matrix.I(dimv) - beta * (v * v.t) )
733
+
734
+
735
+ Matrix.diag(Matrix.robust_I(dim - dimv), Matrix.I(dimv) - beta * (v * v.t) )
722
736
  end
723
737
 
724
738
  #
@@ -899,7 +913,7 @@ module Householder
899
913
  for j in (0...n-1)
900
914
  c, s = Givens.givens(r[j,j], r[j+1, j])
901
915
  cs = Matrix[[c, s], [-s, c]]
902
- q *= Matrix.diag(Matrix.I(j), cs, Matrix.I(n - j - 2))
916
+ q *= Matrix.diag(Matrix.robust_I(j), cs, Matrix.robust_I(n - j - 2))
903
917
  r[j..j+1, j..n-1] = cs.t * r[j..j+1, j..n-1]
904
918
  end
905
919
  return q, r
@@ -210,6 +210,7 @@ describe "Matrix class extension:" do
210
210
 
211
211
  it "row2matrix" do
212
212
  m = Matrix.build(4, 3){|i, j| i * 3 + j + 1}
213
+
213
214
  m.row2matrix(1..2).should == Matrix[[4, 5, 6],[7, 8, 9]]
214
215
  m.row2matrix(2).should == Matrix[[7, 8, 9]]
215
216
  m.row2matrix(0..4).should == m
@@ -218,6 +219,10 @@ describe "Matrix class extension:" do
218
219
  it "column2matrix" do
219
220
  m = Matrix.build(4, 3){|i, j| i * 3 + j + 1}
220
221
  m.column2matrix(1).should == Matrix[[2], [5], [8], [11]]
222
+ m.column2matrix(1..1).should == Matrix[[2], [5], [8], [11]]
223
+
224
+ m.column2matrix(1..2).should == Matrix[[2,3], [5,6], [8,9], [11,12]]
225
+
221
226
  end
222
227
 
223
228
  it "diag" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Cosmin Bonchis
@@ -35,7 +35,7 @@ cert_chain:
35
35
  rpP0jjs0
36
36
  -----END CERTIFICATE-----
37
37
 
38
- date: 2010-05-04 00:00:00 -04:00
38
+ date: 2010-05-06 00:00:00 -04:00
39
39
  default_executable:
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
metadata.gz.sig CHANGED
@@ -1,2 +1,5 @@
1
- �`@�\M8V��$�=h �g��Uc�e� ��b�Li8�p/��F�nfѦ�5���xm��bD�Uہ������o`�����}�w`C�x��Dvb�����\����y�]hd)�c��]���.��#�Om� !s��(y~���Y��! ����B��k��~�kS�C��
2
- !⥥7��AM�?�}Z|�=��a�Hϓk�j�f�L�J�0 ,y0M�I�gA�N"�2դy�TOBk�eIO��3�
1
+ lL�=�� �BA`���@<�HW
2
+ <O�.�M~X�>�M��
3
+ �x1Ψ�-Uq���ۋ��%TfVVE+g�UPN�5��.�.N�l��l��g�ɨ"�W4=f�{�HMR�����'�]2��`;������m9M�m�9�}l�XDn5�FQ�;�
4
+ q�6
5
+