Class: Matrix

Inherits:
Object
  • Object
show all
Defined in:
lib/statsample/matrix.rb

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object

Select elements and submatrixes Implement row, column and minor in one method

  • [i,j]

    Element i,j

  • [i,:*]

    Row i

  • [:*,j]

    Column j

  • [i1..i2,j]

    Row i1 to i2, column j

Raises:

  • (ArgumentError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/statsample/matrix.rb', line 49

def [](*args)
  raise ArgumentError if args.size!=2
  x=args[0]
  y=args[1]
  if x.is_a? Integer and y.is_a? Integer
    @rows[args[0]][args[1]]
  else
    # set ranges according to arguments
    
    rx=case x
      when Numeric
        x..x
      when :*
        0..(row_size-1)
      when Range
        x
    end
    ry=case y
      when Numeric
        y..y
      when :*
        0..(column_size-1)
      when Range
        y
    end
    Matrix.rows(rx.collect {|i| ry.collect {|j| @rows[i][j]}})
  end
end

#column_sumObject

Calculate marginal of columns



32
33
34
35
36
# File 'lib/statsample/matrix.rb', line 32

def column_sum
(0...column_size).collect {|i|
  column(i).to_a.inject(0) {|a,v| a+v}
}
end

#old_parObject



39
# File 'lib/statsample/matrix.rb', line 39

alias :old_par :[]

#row_sumObject

Calculate marginal of rows



26
27
28
29
30
# File 'lib/statsample/matrix.rb', line 26

def row_sum
(0...row_size).collect {|i|
  row(i).to_a.inject(0) {|a,v| a+v}
}
end

#to_gslObject



17
18
19
20
21
22
23
# File 'lib/statsample/matrix.rb', line 17

def to_gsl
  out=[]
  self.row_size.times{|i|
    out[i]=self.row(i).to_a
  }
  GSL::Matrix[*out]
end

#total_sumObject

Calculate sum of cells



78
79
80
# File 'lib/statsample/matrix.rb', line 78

def total_sum
  row_sum.inject(0){|a,v| a+v}
end