Class: RMath3D::RMtx2
- Inherits:
-
Object
- Object
- RMath3D::RMtx2
- Defined in:
- lib/rmath3d/rmath3d_plain.rb
Overview
Document-class: RMath3D::RMtx2 provies 2x2 matrix arithmetic.
Notice
-
elements are stored in column-major order.
Instance Method Summary collapse
-
#*(arg) ⇒ Object
call-seq: *.
-
#+(arg) ⇒ Object
call-seq: +.
-
#+@ ⇒ Object
call-seq: +.
-
#-(arg) ⇒ Object
call-seq: -.
-
#-@ ⇒ Object
call-seq: -.
-
#==(other) ⇒ Object
call-seq: ==.
-
#[](row, col) ⇒ Object
(also: #getElement)
call-seq: [row,col] -> value.
-
#[]=(row, col, value) ⇒ Object
(also: #setElement)
call-seq: [row,col]= value.
-
#add!(other) ⇒ Object
call-seq: mtx1.add!( mtx2 ).
-
#coerce ⇒ Object
call-seq: coerse(other).
-
#e00 ⇒ Object
Returns the element at row 0 and column 0.
-
#e00=(value) ⇒ Object
Replaces the element at row 0 and column 0 by
value
. -
#e01 ⇒ Object
Returns the element at row 0 and column 1.
-
#e01=(value) ⇒ Object
Replaces the element at row 0 and column 1 by
value
. -
#e10 ⇒ Object
Returns the element at row 1 and column 0.
-
#e10=(value) ⇒ Object
Replaces the element at row 1 and column 0 by
value
. -
#e11 ⇒ Object
Returns the element at row 1 and column 1.
-
#e11=(value) ⇒ Object
Replaces the element at row 1 and column 1 by
value
. -
#getColumn(column) ⇒ Object
call-seq: mtx2.getColumn© -> RVec2.
-
#getDeterminant ⇒ Object
call-seq: getDeterminant -> determinant.
-
#getInverse ⇒ Object
call-seq: getInverse -> inverse.
-
#getRow(row) ⇒ Object
call-seq: mtx2.getRow® -> RVec2.
-
#getTransposed ⇒ Object
call-seq: getTransposed.
-
#initialize(*a) ⇒ RMtx2
constructor
call-seq: RMtx2.new -> ((1,0),(0,1)) RMtx2.new(e) -> ((e,e), (e,e)) RMtx2.new( other ) : Copy Constructor RMtx2.new( e0, e1, e2, e3 ) -> ((e0,e1), (e2,e3)).
-
#invert! ⇒ Object
call-seq: invert! -> self.
-
#mul!(other) ⇒ Object
call-seq: mtx1.mul!( mtx2 ).
-
#rotation(radian) ⇒ Object
call-seq: rotation(radian) -> self.
-
#scaling(sx, sy) ⇒ Object
call-seq: scaling(sx,sy) -> self.
-
#setColumn(v, column) ⇒ Object
call-seq: mtx2.setColumn(v,c).
-
#setElements(*a) ⇒ Object
call-seq: setElements( e0, e1, e2, e3 ).
-
#setIdentity ⇒ Object
call-seq: setIdentity.
-
#setRow(v, row) ⇒ Object
call-seq: mtx2.setRow(v,r).
-
#setZero ⇒ Object
call-seq: setZero.
-
#sub!(other) ⇒ Object
call-seq: mtx1.sub!( mtx2 ).
-
#to_a ⇒ Object
call-seq: to_a.
-
#to_s ⇒ Object
call-seq: to_s.
-
#transpose! ⇒ Object
call-seq: transpose!.
Constructor Details
#initialize(*a) ⇒ RMtx2
call-seq:
RMtx2.new -> ((1,0),(0,1))
RMtx2.new(e) -> ((e,e), (e,e))
RMtx2.new( other ) : Copy Constructor
RMtx2.new( e0, e1, e2, e3 ) -> ((e0,e1), (e2,e3))
Creates a new 2x2 matrix.
23 24 25 26 27 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 |
# 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 Fixnum, Float @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 Fixnum, Float 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 |
Instance Method Details
#*(arg) ⇒ Object
call-seq: *
mtx1 * mtx2 : Binary multiply operator.
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 412 def *( arg ) case arg when Fixnum, Float, Bignum 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 |
#+(arg) ⇒ Object
call-seq: +
mtx1 + mtx2 : Binary plus operator.
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# 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 |
#+@ ⇒ Object
call-seq: +
+mtx : Unary plus operator.
352 353 354 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 352 def +@ return self end |
#-(arg) ⇒ Object
call-seq: -
mtx1 - mtx2 : Binary minus operator.
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# 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 |
#-@ ⇒ Object
call-seq: -
-mtx : Unary minus operator.
361 362 363 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 361 def -@ return RMtx2.new( self * -1.0 ) end |
#==(other) ⇒ Object
call-seq: ==
mtx1 == mtx2 : evaluates equality.
442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# 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 |
#[](row, col) ⇒ Object Also known as: getElement
call-seq: [row,col] -> value
Returns the element at (row
,col
).
134 135 136 137 |
# 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 |
#[]=(row, col, value) ⇒ Object Also known as: setElement
call-seq: [row,col]= value
Stores value
at (row
,col
).
123 124 125 126 |
# 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 |
#add!(other) ⇒ Object
call-seq: mtx1.add!( mtx2 )
mtx1 += mtx2 : appends the elements of mtx2
into corresponding mtx1
elements.
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# 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 |
#coerce ⇒ Object
call-seq: coerse(other)
Resolves type mismatch.
89 90 91 92 93 94 95 96 97 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 89 def coerce case arg when Fixnum, Float, Bignum return [ self, arg ] else raise TypeError, "RMtx2#coerce : #{arg.self} can't be coerced into #{self.class}." return nil end end |
#e00 ⇒ Object
Returns the element at row 0 and column 0.
141 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 141 def e00() getElement(0,0) end |
#e00=(value) ⇒ Object
Replaces the element at row 0 and column 0 by value
.
150 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 150 def e00=(value) setElement(0,0,value) end |
#e01 ⇒ Object
Returns the element at row 0 and column 1.
143 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 143 def e01() getElement(0,1) end |
#e01=(value) ⇒ Object
Replaces the element at row 0 and column 1 by value
.
152 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 152 def e01=(value) setElement(0,1,value) end |
#e10 ⇒ Object
Returns the element at row 1 and column 0.
145 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 145 def e10() getElement(1,0) end |
#e10=(value) ⇒ Object
Replaces the element at row 1 and column 0 by value
.
154 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 154 def e10=(value) setElement(1,0,value) end |
#e11 ⇒ Object
Returns the element at row 1 and column 1.
147 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 147 def e11() getElement(1,1) end |
#e11=(value) ⇒ Object
Replaces the element at row 1 and column 1 by value
.
156 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 156 def e11=(value) setElement(1,1,value) end |
#getColumn(column) ⇒ Object
call-seq: mtx2.getColumn© -> RVec2
Returns c
-th column vector.
173 174 175 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 173 def getColumn( column ) return RVec2.new( self[0,column], self[1,column] ) end |
#getDeterminant ⇒ Object
call-seq: getDeterminant -> determinant
Calculates determinant.
233 234 235 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 233 def getDeterminant e00 * e11 - e01 * e10 end |
#getInverse ⇒ Object
call-seq: getInverse -> inverse
Returns the inverse.
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# 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 |
#getRow(row) ⇒ Object
call-seq: mtx2.getRow® -> RVec2
Returns r
-th row vector.
164 165 166 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 164 def getRow( row ) return RVec2.new( self[row,0], self[row,1] ) end |
#getTransposed ⇒ Object
call-seq: getTransposed
Returns transposed matrix.
242 243 244 245 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 242 def getTransposed return RMtx2.new( @e[0], @e[1], @e[2], @e[3] ) end |
#invert! ⇒ Object
call-seq: invert! -> self
Makes itself as the inverse of the original matrix.
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# 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 |
#mul!(other) ⇒ Object
call-seq: mtx1.mul!( mtx2 )
mtx1 *= mtx2
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 504 def mul!( other ) case other when Fixnum, Float, Bignum 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 |
#rotation(radian) ⇒ Object
call-seq: rotation(radian) -> self
Makes a matrix that rotates around the z-axis.
321 322 323 324 325 326 327 328 329 330 331 332 |
# 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 |
#scaling(sx, sy) ⇒ Object
call-seq: scaling(sx,sy) -> self
Makes itself as a scaling matrix.
339 340 341 342 343 344 345 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 339 def scaling( sx, sy ) setIdentity() setElement( 0, 0, sx ) setElement( 1, 1, sy ) return self end |
#setColumn(v, column) ⇒ Object
call-seq: mtx2.setColumn(v,c)
Returns sets c
-th column by vector v
.
192 193 194 195 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 192 def setColumn( v, column ) self[0,column] = v.x self[1,column] = v.y end |
#setElements(*a) ⇒ Object
call-seq: setElements( e0, e1, e2, e3 )
Stores given 4 new values.
104 105 106 107 108 109 110 111 112 113 114 115 116 |
# 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 |
#setIdentity ⇒ Object
call-seq: setIdentity
Sets as identity matrix.
214 215 216 217 218 219 220 221 222 223 224 225 226 |
# 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 |
#setRow(v, row) ⇒ Object
call-seq: mtx2.setRow(v,r)
Returns sets r
-th row by vector v
.
182 183 184 185 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 182 def setRow( v, row ) self[row,0] = v.x self[row,1] = v.y end |
#setZero ⇒ Object
call-seq: setZero
Clears all elements by 0.0
202 203 204 205 206 207 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 202 def setZero 4.times do |i| @e[i] = 0.0 end return self end |
#sub!(other) ⇒ Object
call-seq: mtx1.sub!( mtx2 )
mtx1 -= mtx2 : subtracts the elements of mtx2
from corresponding mtx1
elements.
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# 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 |
#to_a ⇒ Object
call-seq: to_a
Returns its elements as a new Array.
80 81 82 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 80 def to_a return @e end |
#to_s ⇒ Object
call-seq: to_s
Returns human-readable string.
70 71 72 73 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 70 def to_s "( #{@e[0]}, #{@e[2]} )\n" + "( #{@e[1]}, #{@e[3]} )\n" end |
#transpose! ⇒ Object
call-seq: transpose!
Transposeas its elements.
252 253 254 |
# File 'lib/rmath3d/rmath3d_plain.rb', line 252 def transpose! @e[1], @e[2] = @e[2], @e[1] end |