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.



1839
1840
1841
# File 'lib/matrix.rb', line 1839

def initialize(value)
  @value = value
end

Instance Method Details

#*(other) ⇒ Object



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

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



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

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



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

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



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

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



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

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