#  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_Matrix < Test::Unit::TestCase

  # def setup
  # end

  # def teardown
  # end
 
  def test_invert
    a = Matrix.identity 5
    
    b = a.inverse
    
    b.diagonal.each do |x|
      assert_in_delta 1.0, x, 1.0E-6
    end
    
  end
  
  def test_multiply
    a = Matrix.identity 5
    a = a*3.0
    
    a.diagonal.each do |x|
      assert_in_delta 3.0, x, 1.0E-6
    end
    
    a = Matrix.identity 3
    b = Vector.elements [2.0, 3.0, 4.0]

    a = a*b
    assert_instance_of Vector, a
    
    a.length.times do |i|
      assert_in_delta b[i], a[i], 1.0E-6
    end
    
    a = Matrix.identity 5
    a = a*5.0
    b = Matrix.identity 5
    b = b*2.0
    c = a*b
    
    c.diagonal.each do |x|
        assert_in_delta 10.0, x, 1.0E-6
    end
    
  end

  def test_power
      a = Matrix.rows [[0.0,1.0],[2.0,3.0]]
      b = a**2
      
      assert_instance_of Matrix, b
      assert_equal 0.0, b[0,0]
      assert_equal 1.0, b[0,1]
      assert_equal 4.0, b[1,0]
      assert_equal 9.0, b[1,1]
  end

  def test_reciprocal_condition
    
    a = Matrix.identity 5
    assert_in_delta 1.0, a.reciprocal_condition, 1E-10
        
  end

end
