#  JLMatrix - Matrix support for JRuby
#  Copyright (C) 2008 Approximatrix, LLC
#  http://www.approximatrix.com
#  
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#  NOTE: This software is licensed under the GPL and does not allow
#        linking to closed-source software.
#

require 'test/unit'
require 'jlmatrix'
  
class TC_Vector < Test::Unit::TestCase
  
  # def setup
  # end

  # def teardown
  # end
  
  def test_dot
    a = Vector.elements((1..50).to_a)
    b = Vector.elements((51..100).to_a)
    assert_equal 106675.0, a.dot(b)
    
    b = Vector.elements [1,3,5,7]
    assert_raises ArgumentError do
        a.dot(b)
    end
    
  end
  
  def test_multiply_elements
      a = Vector.elements [-1.0,-2.0,-3.0]
      b = Vector.elements [-2.0,2.0,-2.0]
      
      a = a.multiply_elements(b)
      assert_equal 2.0, a[0]
      assert_equal -4.0, a[1]
      assert_equal 6.0, a[2]
  end
  
  def test_divide
      a = Vector.elements [-1.0,-2.0,-3.0]
      
      b = a/2
      assert_equal -0.5, b[0]
      assert_equal -1.0, b[1]
      assert_equal -1.5, b[2]
      
      b = a/2.0
      assert_equal -0.5, b[0]
      assert_equal -1.0, b[1]
      assert_equal -1.5, b[2]
  end
  
  def test_mean
      a = Vector.elements [-3.0,3.0]
      assert_equal 0.0, a.mean
      
      a = Vector.elements((0..100).to_a)
      assert_equal 50.0, a.mean
  end
  
  def test_median
  	  a = Vector.elements [-1.0,-4.0,-6.0,-3.0,-0.5]
  	  assert_equal -3.0, a.median
  end
  
  def test_standard_deviation
      a = Vector.elements((0..100).to_a)
      assert_in_delta 29.3, a.standard_deviation, 1.0E-2
  end
  
  def test_sum
      a = Vector.elements [1.0, 6.0, 2.0, 8.0]
      assert_equal 17.0, a.sum
      
      a = Vector.elements [-3.0,3.0]
      assert_equal 0.0, a.sum
  end
  
  def test_power
      a = Vector.elements [1.0,2.0,3.0]
      b = a**2
      
      assert_instance_of Vector, b
      assert_equal 1.0, b[0]
      assert_equal 4.0, b[1]
      assert_equal 9.0, b[2]
  end
  
  def test_sum_squared
      a = Vector.elements [-3.0,3.0]
      assert_equal 18.0, a.sum_squared
  end
  
  def test_sort
      a = Vector.elements [1.0, 6.0, 2.0, 8.0]
      b = a.sort
      assert_equal 4, b.size 
      b.size.times do |i|
          assert(a.to_a.include? b[i])
          assert(b.to_a.include? a[i])
          if i > 0
              assert(b[i] > b[i-1])
          end
          if i < b.size-1
              assert(b[i] < b[i+1])
          end
      end
  end
  
  def test_ones
      vec = Vector.ones(6)
      vec.each do |x|
          assert_equal 1.0, x        
      end
  end
  
  def test_zeroes
      vec = Vector.zeroes(5)
      vec.each do |x|
          assert_equal 0.0, x        
      end    
  end
  
end
