Class: Eigen::Matrix4

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

Overview

A 4x4 matrix holding floating-point numbers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._load(elements) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
93
# File 'lib/eigen/matrix4.rb', line 85

def self._load(elements) # :nodoc:
    m = new
    if elements.size == 8 * 16
        m.from_a(elements.unpack("E*"))
    else
        m.from_a(Marshal.load(elements)['data'])
    end
    m
end

.from_a(*args) ⇒ Matrix4

Create a new matrix from the content of an array

Parameters:

  • the (Array<Numeric>)

    values. It must be of size at most 16. If smaller than 16, the rest is filled with zeroes

Returns:



14
15
16
17
18
# File 'lib/eigen/matrix4.rb', line 14

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

Instance Method Details

#*(v) ⇒ Matrix4

Multiplies by a scalar

Parameters:

  • v (Numeric)

    the scalar

Returns:

#+(m) ⇒ Matrix4

Sums two matrices

Parameters:

  • m (Matrix4)

    the matrix to add

Returns:

#-(m) ⇒ Matrix4

Subtracts two matrices

Parameters:

  • m (Matrix4)

    the matrix to subtract to self

Returns:

#-@(v) ⇒ Matrix4

Returns this matrix’ negation

Returns:

#/(v) ⇒ Matrix4

Divides this matrix by a scalar

Parameters:

  • v (Numeric)

    the scalar

Returns:

#==(m) ⇒ Object



66
67
68
69
# File 'lib/eigen/matrix4.rb', line 66

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



# File 'lib/eigen/matrix4.rb', line 4

#[]=(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



81
82
83
# File 'lib/eigen/matrix4.rb', line 81

def _dump(level)
    to_a.pack("E*")
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)

#from_a(array, column_major = true) ⇒ Object

sets matrix from a 1d array



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/eigen/matrix4.rb', line 51

def from_a(array, column_major=true)
    if array.size > 16
        raise ArgumentError, "array should be of size maximum 16"
    end

    16.times do |i|
        v = array[i] || 0
        if column_major
            self[i%4,i/4] = v
        else
            self[i/4,i%4] = v
        end
    end
end

#normNumeric

Returns the matrix’ norm

Returns:

  • (Numeric)

#rowsInteger

Returns the number of rows.

Returns:

  • (Integer)

    the number of rows

#sizeNumeric

Returns the number of elements.

Returns:

  • (Numeric)

    the number of elements

#TMatrix4

Returns the transposed matrix

Returns:

#to_a(column_major = true) ⇒ Array<Numeric>

Returns the values flattened in a ruby array

Examples:

column-major ordering

matrix.to_s => Matrix4(1 2 3 4
                       5 6 7 8
                       9 10 11 12
                       13 14 15 16)
matrix.to_a(true) => [1 5 9 13 2 6 10 14 ...]

Parameters:

  • column_major (Boolean) (defaults to: true)

    if true, the values of a column will be adjacent in the resulting array, if not the values of a row will

Returns:

  • (Array<Numeric>)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/eigen/matrix4.rb', line 32

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

#to_s(line_format = "%g %g %g %g") ⇒ Object

:nodoc:



71
72
73
74
75
76
77
78
79
# File 'lib/eigen/matrix4.rb', line 71

def to_s(line_format = "%g %g %g %g") # :nodoc:
    lines = to_a.each_slice(3).to_s
    <<-EOSTRING
Matrix4(#{line_format % lines[0]}"
#{line_format % lines[1]}"
#{line_format % lines[2]}"
#{line_format % lines[3]})"
    EOSTRING
end