Class: Bayonetta::Linalg::Matrix
- Inherits:
-
Object
- Object
- Bayonetta::Linalg::Matrix
- Defined in:
- lib/bayonetta/linalg.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #[](i) ⇒ Object
-
#initialize ⇒ Matrix
constructor
A new instance of Matrix.
- #inverse ⇒ Object
- #transpose ⇒ Object
Constructor Details
#initialize ⇒ Matrix
Returns a new instance of Matrix.
7 8 9 10 11 12 |
# File 'lib/bayonetta/linalg.rb', line 7 def initialize @data = [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]] end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/bayonetta/linalg.rb', line 6 def data @data end |
Instance Method Details
#*(other) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/bayonetta/linalg.rb', line 81 def *(other) if other.kind_of?(Matrix) m = Matrix::new 4.times { |i| 4.times { |j| m.data[i][j] = 0.0 4.times { |k| m.data[i][j] += data[i][k] * other.data[k][j] } } } return m elsif other.kind_of?(Vector) || (other.kind_of?(Array) && other.length == 4) return Vector::new( @data[0][0] * other[0] + @data[0][1] * other[1] + @data[0][2] * other[2] + @data[0][3] * other[3], @data[1][0] * other[0] + @data[1][1] * other[1] + @data[1][2] * other[2] + @data[1][3] * other[3], @data[2][0] * other[0] + @data[2][1] * other[1] + @data[2][2] * other[2] + @data[2][3] * other[3], @data[3][0] * other[0] + @data[3][1] * other[1] + @data[3][2] * other[2] + @data[3][3] * other[3] ) elsif other.kind_of?(Numeric) m = Matrix::new 4.times { |i| 4.times { |j| m.data[i][j] = data[i][j] * other } } return m else raise "Invalid argument for matrix miltiply: #{other.inspect}!" end end |
#+(other) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bayonetta/linalg.rb', line 67 def +(other) if other.kind_of?(Matrix) m = Matrix::new 4.times { |i| 4.times { |j| m.data[i][j] = data[i][j] + other.data[i][j] } } return m else raise "Invalid argument for matrix add: #{other.inspect}!" end end |
#[](i) ⇒ Object
14 15 16 |
# File 'lib/bayonetta/linalg.rb', line 14 def [](i) @data[i] end |
#inverse ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bayonetta/linalg.rb', line 28 def inverse m = Matrix::new s0 = data[0][0] * data[1][1] - data[1][0] * data[0][1] s1 = data[0][0] * data[1][2] - data[1][0] * data[0][2] s2 = data[0][0] * data[1][3] - data[1][0] * data[0][3] s3 = data[0][1] * data[1][2] - data[1][1] * data[0][2] s4 = data[0][1] * data[1][3] - data[1][1] * data[0][3] s5 = data[0][2] * data[1][3] - data[1][2] * data[0][3] c5 = data[2][2] * data[3][3] - data[3][2] * data[2][3] c4 = data[2][1] * data[3][3] - data[3][1] * data[2][3] c3 = data[2][1] * data[3][2] - data[3][1] * data[2][2] c2 = data[2][0] * data[3][3] - data[3][0] * data[2][3] c1 = data[2][0] * data[3][2] - data[3][0] * data[2][2] c0 = data[2][0] * data[3][1] - data[3][0] * data[2][1] invdet = 1.0 / (s0 * c5 - s1 * c4 + s2 * c3 + s3 * c2 - s4 * c1 + s5 * c0) m.data[0][0] = ( data[1][1] * c5 - data[1][2] * c4 + data[1][3] * c3) * invdet m.data[0][1] = (-data[0][1] * c5 + data[0][2] * c4 - data[0][3] * c3) * invdet m.data[0][2] = ( data[3][1] * s5 - data[3][2] * s4 + data[3][3] * s3) * invdet m.data[0][3] = (-data[2][1] * s5 + data[2][2] * s4 - data[2][3] * s3) * invdet m.data[1][0] = (-data[1][0] * c5 + data[1][2] * c2 - data[1][3] * c1) * invdet m.data[1][1] = ( data[0][0] * c5 - data[0][2] * c2 + data[0][3] * c1) * invdet m.data[1][2] = (-data[3][0] * s5 + data[3][2] * s2 - data[3][3] * s1) * invdet m.data[1][3] = ( data[2][0] * s5 - data[2][2] * s2 + data[2][3] * s1) * invdet m.data[2][0] = ( data[1][0] * c4 - data[1][1] * c2 + data[1][3] * c0) * invdet m.data[2][1] = (-data[0][0] * c4 + data[0][1] * c2 - data[0][3] * c0) * invdet m.data[2][2] = ( data[3][0] * s4 - data[3][1] * s2 + data[3][3] * s0) * invdet m.data[2][3] = (-data[2][0] * s4 + data[2][1] * s2 - data[2][3] * s0) * invdet m.data[3][0] = (-data[1][0] * c3 + data[1][1] * c1 - data[1][2] * c0) * invdet m.data[3][1] = ( data[0][0] * c3 - data[0][1] * c1 + data[0][2] * c0) * invdet m.data[3][2] = (-data[3][0] * s3 + data[3][1] * s1 - data[3][2] * s0) * invdet m.data[3][3] = ( data[2][0] * s3 - data[2][1] * s1 + data[2][2] * s0) * invdet m end |