File: oper_test.rb

package info (click to toggle)
ruby-gsl 2.1.0.3%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,604 kB
  • sloc: ansic: 62,050; ruby: 15,845; sh: 19; makefile: 10
file content (98 lines) | stat: -rw-r--r-- 1,975 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require 'test_helper'

class OperTest < GSL::TestCase

  def test_multiplication_matrix
    matrix = GSL::Matrix.ones(1)

    mul_int   = 2 * matrix
    mul_float = 0.2 * matrix

    assert_equal 2,   mul_int[0, 0]
    assert_equal 0.2, mul_float[0, 0]
  end

  def test_multiplication_matrix_int
    matrix = GSL::Matrix::Int.ones(1)

    mul_int   = 2 * matrix
    mul_float = 0.2 * matrix

    assert_equal 2, mul_int[0, 0]
    assert_equal 0, mul_float[0, 0]
  end

  def test_multiplication_matrix_complex
    matrix = GSL::Matrix::Complex.eye(1)

    result = 0.2 * matrix

    assert_equal 0.2, result[0][0]
  end

  def test_multiplication_vector
    vector = GSL::Vector[1, 2]

    mul_int   = 2 * vector
    mul_float = 0.2 * vector

    assert_equal 2,   mul_int[0]
    assert_equal 0.2, mul_float[0]
  end

  def test_multiplication_vector_int
    vector = GSL::Vector::Int[1, 2]

    mul_int   = 2 * vector
    mul_float = 0.2 * vector

    assert_equal 2, mul_int[0]
    assert_equal 0, mul_float[0]
  end

  def test_multiplication_vector_complex
    re = GSL::Vector[1..4]
    im = GSL::Vector[5..8]

    vector = GSL::Vector::Complex[re, im]

    mul_int   = 2 * vector
    mul_float = 0.2 * vector

    assert_equal 10,  mul_int[0][1]
    assert_equal 1.0, mul_float[0][1]
  end

  def test_division_poly
    poly = GSL::Poly.alloc([2])

    a = GSL::Poly[1]; a[0] = 2

    result   = 2 / poly
    expected = GSL::Rational.new(a, poly)

    assert_equal expected.num, result.num
    assert_equal expected.den, result.den
  end

  def test_division_vector_col
    vector = GSL::Vector[1, 2].col

    result1 = 2 / vector
    result2 = 2 / result1

    assert_in_epsilon 0.4, result1[0]
    assert_equal result2, vector
  end

  def test_division_vector_int_col
    vector = GSL::Vector::Int[1, 2].col

    result1 = 2 / vector
    result2 = 2 / result1

    assert_in_epsilon 0.4, result1[0]
    assert_equal result2.to_a.map(&:to_i), vector.to_a
  end

end