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

check_int, check_range, coerce_to, coerce_to_int, coerce_to_matrix

Constructor Details

#initialize(value) ⇒ Scalar

Returns a new instance of Scalar.



1808
1809
1810
# File 'lib/matrix.rb', line 1808

def initialize(value)
  @value = value
end

Instance Method Details

#*(other) ⇒ Object



1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
# File 'lib/matrix.rb', line 1835

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



1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
# File 'lib/matrix.rb', line 1859

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

#+(other) ⇒ Object

ARITHMETIC



1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
# File 'lib/matrix.rb', line 1813

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

#-(other) ⇒ Object



1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
# File 'lib/matrix.rb', line 1824

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

#/(other) ⇒ Object



1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
# File 'lib/matrix.rb', line 1846

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