Class: Eigen::MatrixX

Inherits:
Object
  • Object
show all
Defined in:
lib/eigen/matrixx.rb,
ext/eigen/eigen.cpp

Overview

A variable-size matrix holding floating-point numbers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._load(coordinates) ⇒ Object

:nodoc:



97
98
99
100
101
102
# File 'lib/eigen/matrixx.rb', line 97

def self._load(coordinates) # :nodoc:
    o = Marshal.load(coordinates)
    m = new(o['rows'],o['cols'])
    m.from_a(o['data'],o['rows'],o['cols'])
    m
end

.from_a(*args) ⇒ Object



18
19
20
21
22
# File 'lib/eigen/matrixx.rb', line 18

def self.from_a(*args)
    m = new
    m.from_a(*args)
    m
end

.Zero(rows, cols) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/eigen/matrixx.rb', line 8

def self.Zero(rows, cols)
    m = new(rows, cols)
    rows.times do |r|
        cols.times do |c|
            m[r, c] = 0
        end
    end
    m
end

Instance Method Details

#*(v) ⇒ MatrixX

Multiplies by a scalar

Parameters:

  • v (Numeric)

    the scalar

Returns:

#+(m) ⇒ MatrixX

Sums two matrices

Parameters:

  • m (MatrixX)

    the matrix to add

Returns:

#-(m) ⇒ MatrixX

Subtracts two matrices

Parameters:

  • m (MatrixX)

    the matrix to subtract to self

Returns:

#-@(v) ⇒ MatrixX

Returns this matrix’ negation

Returns:

#/(v) ⇒ MatrixX

Divides this matrix by a scalar

Parameters:

  • v (Numeric)

    the scalar

Returns:

#==(m) ⇒ Object



76
77
78
79
# File 'lib/eigen/matrixx.rb', line 76

def ==(m)
    m.kind_of?(self.class) &&
        __equal__(m)
end

#[](row, col) ⇒ Numeric

Accesses an element

Parameters:

  • row (Integer)

    the element’s row

  • col (Integer)

    the element’s column

Returns:

  • (Numeric)

    the required element

#[]=(row, col, value) ⇒ Numeric

Sets an element

Parameters:

  • row (Integer)

    the element’s row

  • col (Integer)

    the element’s column

  • value (Numeric)

    the new value

Returns:

  • (Numeric)

    the value

#_dump(level) ⇒ Object

:nodoc:



93
94
95
# File 'lib/eigen/matrixx.rb', line 93

def _dump(level) # :nodoc:
    Marshal.dump({'rows' => rows, 'cols' => cols, 'data' => to_a})
end

#approx?(m, threshold = dummy_precision) ⇒ Boolean

Verifies that two matrices are within threshold of each other, elementwise

Parameters:

Returns:

  • (Boolean)

#colsNumeric

Returns the number of columns.

Returns:

  • (Numeric)

    the number of columns

#dotM(m) ⇒ Numeric

Matrix multiplication

Parameters:

Returns:

  • (Numeric)

#dotV(m) ⇒ Numeric

Matrix/vector multiplication

Parameters:

Returns:

  • (Numeric)

#dupObject



4
5
6
# File 'lib/eigen/matrixx.rb', line 4

def dup
    MatrixX.from_a(to_a, rows, cols)
end

#from_a(array, nrows = -1,, ncols = -1,, column_major = true) ⇒ Object

sets matrix from a 1d array



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/eigen/matrixx.rb', line 44

def from_a(array,nrows=-1,ncols=-1,column_major=true)
    if nrows == -1 && ncols == -1
        nrows = rows
        ncols = cols
    elsif nrows == -1
        nrows = array.size / ncols
    elsif ncols == -1
        ncols = array.size / nrows
    end
    resize(nrows,ncols)
    array.each_index do |i|
        v = array[i]
        if !v
            v = 0.0
        end
        if column_major
            self[i%nrows,i/nrows] = v
        else
            self[i/ncols,i%ncols] = v
        end
    end
end

#jacobiSvd(flags = 0) ⇒ JacobiSVD

Returns a SVD solver to find V from W in self.dotV(V) = W

Parameters:

  • flags (Integer) (defaults to: 0)

    solver flags, as OR-ed values of Eigen::ComputeFullU, Eigen::ComputeThinU and Eigen::ComputeThinV. See Eigen documentation

Returns:

#normNumeric

Returns the matrix’ norm

Returns:

  • (Numeric)

#pretty_print(pp) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/eigen/matrixx.rb', line 67

def pretty_print(pp)
    for i in 0..rows()-1
        for j in 0..cols()-1
            pp.text " #{self[i,j]}"
        end
        pp.text "\n"
    end
end

#resize(rows, cols) ⇒ Object

Resizes the matrix

Parameters:

  • rows (Integer)

    the new number of rows

  • cols (Integer)

    the new number of columns

#rowsInteger

Returns the number of rows.

Returns:

  • (Integer)

    the number of rows

#setColumn(column, vector) ⇒ Object

Sets a whole matrix column

Parameters:

  • row (Integer)

    the column index

  • vector (VectorX)

    the column values

#setRow(row, vector) ⇒ Object

Sets a whole matrix row

Parameters:

  • row (Integer)

    the row index

  • vector (VectorX)

    the row values

#sizeNumeric

Returns the number of elements.

Returns:

  • (Numeric)

    the number of elements

#TMatrixX

Returns the transposed matrix

Returns:

#to_a(column_major = true) ⇒ Object

Returns the array value in a vector



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eigen/matrixx.rb', line 25

def to_a(column_major=true)
    a = []
    if column_major
        for j in 0..cols()-1
            for i in 0..rows()-1
                a << self[i,j]
            end
        end
    else
        for i in 0..rows()-1
            for j in 0..cols()-1
                a << self[i,j]
            end
        end
    end
    a
end

#to_sObject

:nodoc:



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/eigen/matrixx.rb', line 81

def to_s # :nodoc:
    str = "MatrixX(\n"
    for i in 0..rows()-1
        for j in 0..cols()-1
            str += "#{self[i,j]} "
        end
        str[-1] = "\n"
    end
    str += ")"
    str
end