Class: Matrix::Scalar

Inherits:
Numeric
  • Object
show all
Includes:
ExceptionForMatrix, CoercionHelper
Defined in:
lib/matrix.rb

Overview

Private CLASS

Instance Method Summary collapse

Methods included from CoercionHelper

coerce_to, coerce_to_int

Constructor Details

#initialize(value) ⇒ Scalar


1429
1430
1431
# File 'lib/matrix.rb', line 1429

def initialize(value)
  @value = value
end

Instance Method Details

#*(other) ⇒ Object


1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
# File 'lib/matrix.rb', line 1456

def *(other)
  case other
  when Numeric
    Scalar.new(@value * other)
  when Vector, Matrix
    other.collect{|e| @value * e}
  else
    apply_through_coercion(other, __method__)
  end
end

#**(other) ⇒ Object


1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
# File 'lib/matrix.rb', line 1480

def ** (other)
  case other
  when Numeric
    Scalar.new(@value ** other)
  when Vector
    Scalar.Raise ErrOperationNotDefined, "**", @value.class, other.class
  when Matrix
    #other.powered_by(self)
    Scalar.Raise ErrOperationNotImplemented, "**", @value.class, other.class
  else
    apply_through_coercion(other, __method__)
  end
end

#+(other) ⇒ Object

ARITHMETIC


1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
# File 'lib/matrix.rb', line 1434

def +(other)
  case other
  when Numeric
    Scalar.new(@value + other)
  when Vector, Matrix
    Scalar.Raise ErrOperationNotDefined, "+", @value.class, other.class
  else
    apply_through_coercion(other, __method__)
  end
end

#-(other) ⇒ Object


1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
# File 'lib/matrix.rb', line 1445

def -(other)
  case other
  when Numeric
    Scalar.new(@value - other)
  when Vector, Matrix
    Scalar.Raise ErrOperationNotDefined, "-", @value.class, other.class
  else
    apply_through_coercion(other, __method__)
  end
end

#/(other) ⇒ Object


1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
# File 'lib/matrix.rb', line 1467

def / (other)
  case other
  when Numeric
    Scalar.new(@value / other)
  when Vector
    Scalar.Raise ErrOperationNotDefined, "/", @value.class, other.class
  when Matrix
    self * other.inverse
  else
    apply_through_coercion(other, __method__)
  end
end