File: utest-linalg.cpp

package info (click to toggle)
python-pyclustering 0.10.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 11,128 kB
  • sloc: cpp: 38,888; python: 24,311; sh: 384; makefile: 105
file content (99 lines) | stat: -rwxr-xr-x 2,284 bytes parent folder | download | duplicates (2)
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
99
/*!

@authors Andrei Novikov (pyclustering@yandex.ru)
@date 2014-2020
@copyright BSD-3-Clause

*/


#include <gtest/gtest.h>

#include <algorithm>

#include <pyclustering/utils/linalg.hpp>


using namespace pyclustering::utils::linalg;


TEST(utest_lingalg, subtract) {
    std::vector<double> a = { 2, 3, 4, 5 };
    std::vector<double> b = { 1, 2, 3, 4 };

    auto result = subtract(a, b);

    std::vector<double> expected = { 1, 1, 1, 1 };
    ASSERT_EQ(expected, result);
}

TEST(utest_lingalg, subtract_with_number) {
    std::vector<double> a = { 2, 4, 6, 8 };

    auto result = subtract(a, 2);

    std::vector<double> expected = { 0, 2, 4, 6 };
    ASSERT_EQ(expected, result);
}

TEST(utest_lingalg, multiply) {
    std::vector<double> a = { 2, 3, 4, 5 };
    std::vector<double> b = { 1, 2, 3, 4 };

    auto result = multiply(a, b);

    std::vector<double> expected = { 2, 6, 12, 20 };
    ASSERT_EQ(expected, result);
}

TEST(utest_lingalg, multiply_with_number) {
    std::vector<double> a = { 2, 3, 4, 5 };

    auto result = multiply(a, 2);

    std::vector<double> expected = { 4, 6, 8, 10 };
    ASSERT_EQ(expected, result);
}

TEST(utest_lingalg, multiply_matrix) {
    matrix a = { { 1, 1 }, { 2, 2 }, { 3, 3 } };
    std::vector<double> b = { 2, 2 };

    auto result = multiply(a, b);

    matrix expected = { { 2, 2 }, { 4, 4 }, { 6, 6 } };
    ASSERT_EQ(expected, result);
}

TEST(utest_lingalg, divide) {
    std::vector<double> a = { 2, 6, 9, 16 };
    std::vector<double> b = { 2, 2, 3, 4 };

    auto result = divide(a, b);

    std::vector<double> expected = { 1, 3, 3, 4 };
    ASSERT_EQ(expected, result);
}

TEST(utest_lingalg, divide_with_number) {
    std::vector<double> a = { 10, 20, 30, 40 };

    auto result = divide(a, 5);

    std::vector<double> expected = { 2, 4, 6, 8 };
    ASSERT_EQ(expected, result);
}

TEST(utest_linalg, sum_vector) {
    std::vector<double> a = { 1, 2, 3 };
    ASSERT_EQ(6, sum(a));
}

TEST(utest_linalg, sum_matrix) {
    matrix a = { { 1, 1 }, { 2, 2 }, { 3, 3 } };
    std::vector<double> expected = { 6, 6 };
    ASSERT_EQ(expected, sum(a, 0));

    expected = { 2, 4, 6 };
    ASSERT_EQ(expected, sum(a, 1));
}