class RMath3D::RMtx2
Document-class: RMath3D::RMtx2
provies 2x2 matrix arithmetic.
Notice
-
elements are stored in column-major order.
Public Class Methods
Creates a new 2x2 matrix.
# File lib/rmath3d/rmath3d_plain.rb, line 23 def initialize( *a ) # [NOTE] elemetns are stored in column-major order. @e = [] case a.length when 0 @e = [ 0.0, 0.0, 0.0, 0.0 ] when 1 case a[0] when Float, Integer @e = [ a[0], a[0], a[0], a[0] ] when RMtx2 # Copy Constructor @e = [ a[0].e00, a[0].e10, a[0].e01, a[0].e11 ] else raise TypeError, "RMtx2#initialize : Unknown type #{a[0].class}." return nil end when 4 # Element-wise setter for row in 0...2 do for col in 0...2 do index = 2*row + col case a[index] when Float, Integer setElement( row, col, a[index] ) else raise TypeError, "RMtx2#initialize : Unknown type #{a[0].class}." return nil end end end else raise RuntimeError, "RMtx2#initialize : wrong # of arguments (#{a.length})" return nil end return self end
Public Instance Methods
mtx1 * mtx2 : Binary multiply operator.
# File lib/rmath3d/rmath3d_plain.rb, line 412 def *( arg ) case arg when Float, Integer return RMtx2.new( arg*self.e00, arg*self.e01, arg*self.e10, arg*self.e11 ) when RMtx2 result = RMtx2.new for row in 0...2 do for col in 0...2 do sum = 0.0 for i in 0...2 do sum += getElement( row, i ) * arg.getElement( i, col ) end result.setElement( row, col, sum ) end end return result else raise TypeError, "RMtx2#*(arg) : Unknown type #{arg.class} given." return nil end end
mtx1 + mtx2 : Binary plus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 370 def +( arg ) if ( arg.class != RMtx2 ) raise TypeError, "RMtx2#+(arg) : Unknown type #{arg.class} given as RMtx2." return nil end result = RMtx2.new for row in 0...2 do for col in 0...2 do result.setElement( row, col, getElement(row,col) + arg.getElement(row,col) ) end end return result end
+mtx : Unary plus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 352 def +@ return self end
mtx1 - mtx2 : Binary minus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 391 def -( arg ) if ( arg.class != RMtx2 ) raise TypeError, "RMtx2#-(arg) : Unknown type #{arg.class} given as RMtx2." return nil end result = RMtx2.new for row in 0...2 do for col in 0...2 do result.setElement( row, col, getElement(row,col) - arg.getElement(row,col) ) end end return result end
-mtx : Unary minus operator.
# File lib/rmath3d/rmath3d_plain.rb, line 361 def -@ return RMtx2.new( self * -1.0 ) end
mtx1 == mtx2 : evaluates equality.
# File lib/rmath3d/rmath3d_plain.rb, line 442 def ==( other ) if other.class == RMtx2 for row in 0...2 do for col in 0...2 do if ( (getElement(row,col) - other.getElement(row,col)).abs > TOLERANCE ) return false end end end return true else return false end end
Returns the element at (row
,col
).
# File lib/rmath3d/rmath3d_plain.rb, line 134 def [](row,col) # [NOTE] elemetns are stored in column-major order. return @e[col*2+row] end
Stores value
at (row
,col
).
# File lib/rmath3d/rmath3d_plain.rb, line 123 def []=(row,col,value) # [NOTE] elemetns are stored in column-major order. @e[col*2+row] = value end
mtx1 += mtx2 : appends the elements of mtx2
into corresponding mtx1
elements.
# File lib/rmath3d/rmath3d_plain.rb, line 462 def add!( other ) if ( other.class != RMtx2 ) raise TypeError, "RMtx2#add! : Unknown type #{other.class} given as RMtx2." return nil end result = RMtx2.new for row in 0...2 do for col in 0...2 do self.setElement( row, col, getElement(row,col) + other.getElement(row,col) ) end end return self end
Resolves type mismatch.
# File lib/rmath3d/rmath3d_plain.rb, line 89 def coerce case arg when Float, Integer return [ self, arg ] else raise TypeError, "RMtx2#coerce : #{arg.self} can't be coerced into #{self.class}." return nil end end
Returns the element at row 0 and column 0.
# File lib/rmath3d/rmath3d_plain.rb, line 141 def e00() getElement(0,0) end
Replaces the element at row 0 and column 0 by value
.
# File lib/rmath3d/rmath3d_plain.rb, line 150 def e00=(value) setElement(0,0,value) end
Returns the element at row 0 and column 1.
# File lib/rmath3d/rmath3d_plain.rb, line 143 def e01() getElement(0,1) end
Replaces the element at row 0 and column 1 by value
.
# File lib/rmath3d/rmath3d_plain.rb, line 152 def e01=(value) setElement(0,1,value) end
Returns the element at row 1 and column 0.
# File lib/rmath3d/rmath3d_plain.rb, line 145 def e10() getElement(1,0) end
Replaces the element at row 1 and column 0 by value
.
# File lib/rmath3d/rmath3d_plain.rb, line 154 def e10=(value) setElement(1,0,value) end
Returns the element at row 1 and column 1.
# File lib/rmath3d/rmath3d_plain.rb, line 147 def e11() getElement(1,1) end
Replaces the element at row 1 and column 1 by value
.
# File lib/rmath3d/rmath3d_plain.rb, line 156 def e11=(value) setElement(1,1,value) end
Returns c
-th column vector.
# File lib/rmath3d/rmath3d_plain.rb, line 173 def getColumn( column ) return RVec2.new( self[0,column], self[1,column] ) end
Calculates determinant.
# File lib/rmath3d/rmath3d_plain.rb, line 233 def getDeterminant e00 * e11 - e01 * e10 end
Returns the inverse.
# File lib/rmath3d/rmath3d_plain.rb, line 261 def getInverse det = getDeterminant() if ( det.abs < TOLERANCE ) raise RuntimeError, "RMtx2#getInverse : det.abs < TOLERANCE" return nil end result = RMtx2.new result.e00 = self.e11 result.e01 = -self.e01 result.e10 = -self.e10 result.e11 = self.e00 d = 1.0 / det result.mul!( d ) return result end
Returns r
-th row vector.
# File lib/rmath3d/rmath3d_plain.rb, line 164 def getRow( row ) return RVec2.new( self[row,0], self[row,1] ) end
Returns transposed matrix.
# File lib/rmath3d/rmath3d_plain.rb, line 242 def getTransposed return RMtx2.new( @e[0], @e[1], @e[2], @e[3] ) end
Makes itself as the inverse of the original matrix.
# File lib/rmath3d/rmath3d_plain.rb, line 289 def invert! det = getDeterminant() if ( det.abs < TOLERANCE ) raise RuntimeError, "RMtx2#invert! : det.abs < TOLERANCE" return nil end elements = Array.new( 4 ) elements[2*0+0] = self.e11 elements[2*0+1] = -self.e01 elements[2*1+0] = -self.e10 elements[2*1+1] = self.e00 d = 1.0 / det setElement( 0, 0, d * elements[2*0+0] ) setElement( 0, 1, d * elements[2*0+1] ) setElement( 1, 0, d * elements[2*1+0] ) setElement( 1, 1, d * elements[2*1+1] ) return self end
mtx1 *= mtx2
# File lib/rmath3d/rmath3d_plain.rb, line 504 def mul!( other ) case other when Float, Integer self.e00 = other*self.e00 self.e01 = other*self.e01 self.e10 = other*self.e10 self.e11 = other*self.e11 return self when RMtx2 result = RMtx2.new for row in 0...2 do for col in 0...2 do sum = 0.0 for i in 0...2 do sum += getElement( row, i ) * other.getElement( i, col ) end result.setElement( row, col, sum ) end end self.e00 = result.e00 self.e01 = result.e01 self.e10 = result.e10 self.e11 = result.e11 return self end end
Makes a matrix that rotates around the z-axis.
# File lib/rmath3d/rmath3d_plain.rb, line 321 def rotation( radian ) s = Math.sin( radian ) c = Math.cos( radian ) setIdentity() self.e00 = c self.e01 = -s self.e10 = s self.e11 = c return self end
Makes itself as a scaling matrix.
# File lib/rmath3d/rmath3d_plain.rb, line 339 def scaling( sx, sy ) setIdentity() setElement( 0, 0, sx ) setElement( 1, 1, sy ) return self end
Returns sets c
-th column by vector v
.
# File lib/rmath3d/rmath3d_plain.rb, line 192 def setColumn( v, column ) self[0,column] = v.x self[1,column] = v.y end
Stores given 4 new values.
# File lib/rmath3d/rmath3d_plain.rb, line 104 def setElements( *a ) if a.length != 4 raise RuntimeError, "RMtx2#setElements : wrong # of arguments (#{a.length})" return nil end for row in 0...2 do for col in 0...2 do index = 2*row + col setElement( row, col, a[index] ) end end end
Sets as identity matrix.
# File lib/rmath3d/rmath3d_plain.rb, line 214 def setIdentity for row in 0...2 do for col in 0...2 do index = 2*row + col if ( row == col ) setElement( row, col, 1.0 ) else setElement( row, col, 0.0 ) end end end return self end
Returns sets r
-th row by vector v
.
# File lib/rmath3d/rmath3d_plain.rb, line 182 def setRow( v, row ) self[row,0] = v.x self[row,1] = v.y end
Clears all elements by 0.0
# File lib/rmath3d/rmath3d_plain.rb, line 202 def setZero 4.times do |i| @e[i] = 0.0 end return self end
mtx1 -= mtx2 : subtracts the elements of mtx2
from corresponding mtx1
elements.
# File lib/rmath3d/rmath3d_plain.rb, line 483 def sub!( other ) if ( other.class != RMtx2 ) raise TypeError, "RMtx2#sub! : Unknown type #{other.class} given as RMtx2." return nil end result = RMtx2.new for row in 0...2 do for col in 0...2 do self.setElement( row, col, getElement(row,col) - other.getElement(row,col) ) end end return self end
Returns its elements as a new Array.
# File lib/rmath3d/rmath3d_plain.rb, line 80 def to_a return @e end
Returns human-readable string.
# File lib/rmath3d/rmath3d_plain.rb, line 70 def to_s "( #{@e[0]}, #{@e[2]} )\n" + "( #{@e[1]}, #{@e[3]} )\n" end
Transposeas its elements.
# File lib/rmath3d/rmath3d_plain.rb, line 252 def transpose! @e[1], @e[2] = @e[2], @e[1] end