Project

General

Profile

Feature #13208 ยป 0001-Add-Vector.zero-n-and-vector.zero.patch

qitar888 (Chia-sheng Chen), 02/11/2017 07:54 PM

View differences:

lib/matrix.rb
1685 1685
# * Vector.[](*array)
1686 1686
# * Vector.elements(array, copy = true)
1687 1687
# * Vector.basis(size: n, index: k)
1688
# * Vector.zero(n)
1688 1689
#
1689 1690
# To access elements:
1690 1691
# * #[](i)
......
1697 1698
# * #angle_with(v)
1698 1699
# * Vector.independent?(*vs)
1699 1700
# * #independent?(*vs)
1701
# * #zero?
1700 1702
#
1701 1703
# Vector arithmetic:
1702 1704
# * #*(x) "is matrix or number"
......
1769 1771
  end
1770 1772

  
1771 1773
  #
1774
  # Return a zero vector.
1775
  #
1776
  #    Vector.zero(3) => Vector[0, 0, 0]
1777
  #
1778
  def Vector.zero(size)
1779
    raise ArgumentError, "invalid size (#{size} for 1..)" if size < 1
1780
    array = Array.new(size, 0)
1781
    new convert_to_array(array, false)
1782
  end
1783

  
1784
  #
1772 1785
  # Vector.new is private; use Vector[] or Vector.elements to create.
1773 1786
  #
1774 1787
  def initialize(array)
......
1882 1895
    self.class.independent?(self, *vs)
1883 1896
  end
1884 1897

  
1898
  #
1899
  # Returns +true+ iff all elements are zero.
1900
  #
1901
  def zero?
1902
    all?(&:zero?)
1903
  end
1904

  
1885 1905
  #--
1886 1906
  # COMPARING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
1887 1907
  #++
test/matrix/test_vector.rb
11 11
    @w1 = Vector[2,3,4]
12 12
  end
13 13

  
14
  def test_zero
15
    assert_equal Vector[0, 0, 0, 0], Vector.zero(4)
16
    assert_raise(ArgumentError) { Vector.zero(-1) }
17
    assert Vector[0, 0, 0, 0].zero?
18
  end
19
  
14 20
  def test_basis
15 21
    assert_equal(Vector[1, 0, 0], Vector.basis(size: 3, index: 0))
16 22
    assert_raise(ArgumentError) { Vector.basis(size: -1, index: 2) }
17
-