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.



1837
1838
1839
# File 'lib/matrix.rb', line 1837

def initialize(value)
  @value = value
end

Instance Method Details

#*(other) ⇒ Object



1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
# File 'lib/matrix.rb', line 1864

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



1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
# File 'lib/matrix.rb', line 1888

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



1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
# File 'lib/matrix.rb', line 1842

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



1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
# File 'lib/matrix.rb', line 1853

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



1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
# File 'lib/matrix.rb', line 1875

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