File: test_dot.cpp

package info (click to toggle)
xtensor-blas 0.21.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 13,864 kB
  • sloc: cpp: 97,843; makefile: 202; perl: 178; python: 153
file content (221 lines) | stat: -rw-r--r-- 7,110 bytes parent folder | download
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/***************************************************************************
 * Copyright (c) Wolf Vollprecht, Johan Mabille and Sylvain Corlay          *
 * Copyright (c) QuantStack                                                 *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#include "xtensor/xarray.hpp"
#include "xtensor/xbuilder.hpp"
#include "xtensor/xstrided_view.hpp"
#include "xtensor/xview.hpp"

#include "gtest/gtest.h"
#include "xtensor-blas/xlinalg.hpp"

namespace xt
{
    TEST(xdot, matrix_times_vector)
    {
        xarray<float> a = xt::ones<float>({1, 4});
        xarray<float> b = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 1, 1}};

        xarray<float> e1 = {{13, 16, 19}};

        auto r1 = linalg::dot(a, b);
        EXPECT_EQ(e1, r1);

        xarray<float> c = xt::ones<float>({3, 1});

        auto r2 = linalg::dot(b, c);
        xarray<float> e2 = {{6, 15, 24, 3}};
        e2.reshape({4, 1});
        EXPECT_EQ(e2, r2);

        EXPECT_THROW(linalg::dot(b, a), std::runtime_error);
        EXPECT_THROW(linalg::dot(c, b), std::runtime_error);
    }

    TEST(xdot, matrix_transpose_times_column)
    {
        xarray<double, layout_type::row_major> a = xt::ones<double>({2, 4});
        xarray<double, layout_type::row_major> b = xt::ones<double>({2, 1});
        auto r1 = linalg::dot(xt::transpose(a), b);
        EXPECT_TRUE(all(equal(r1, 2.0)));
    }

    TEST(xdot, matrix_transpose_times_column_cm)
    {
        xarray<double, layout_type::column_major> a = xt::ones<double>({2, 4});
        xarray<double, layout_type::column_major> b = xt::ones<double>({2, 1});

        auto r1 = linalg::dot(xt::transpose(a), b);
        EXPECT_TRUE(all(equal(r1, 2.0)));
    }

    TEST(xdot, square_matrix_times_vector)
    {
        xarray<float> a = {{1, 1, 1}};
        xarray<float> b = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

        auto r1 = linalg::dot(a, b);

        xarray<float> e1 = {{12, 15, 18}};
        EXPECT_EQ(r1, e1);

        auto r2 = linalg::dot(b, xt::transpose(a));
        xarray<float> e2 = xarray<float>::from_shape({3, 1});
        e2(0, 0) = 6.f;
        e2(1, 0) = 15.f;
        e2(2, 0) = 24.f;
        EXPECT_EQ(r2, e2);

        EXPECT_THROW(linalg::dot(b, a), std::runtime_error);
    }

    TEST(xdot, vector_times_vector)
    {
        xarray<float> a = xt::ones<float>({1, 3});
        xarray<float> b = xt::ones<float>({3, 1});

        auto r1 = linalg::dot(a, b);

        xarray<float> e1 = xarray<float>::from_shape({1, 1});
        e1(0, 0) = 3;

        EXPECT_EQ(e1, r1);

        auto r2 = linalg::dot(b, a);
        xarray<float> e2 = xt::ones<float>({3, 3});
        EXPECT_EQ(e2, r2);

        auto r3 = linalg::dot(b, e1);
        EXPECT_EQ(b * 3.f, r3);
    }

    TEST(xdot, matrix_times_1d)
    {
        xarray<float> a = xt::ones<float>({5, 3});
        xarray<float> b = eval(xt::ones<float>({5}));
        xarray<float> c = eval(xt::ones<float>({3}));
        auto r1 = linalg::dot(xt::transpose(a), b);

        EXPECT_TRUE(all(equal(r1, 5.0)));

        auto r2 = linalg::dot(c, xt::transpose(a));
        EXPECT_TRUE(all(equal(r2, 3.0)));

        auto r3 = linalg::dot(a, c);
        EXPECT_TRUE(all(equal(r3, 3.0)));

        auto r4 = linalg::dot(c, eval(xt::ones<float>({3, 5})));
        EXPECT_TRUE(all(equal(r4, 3.0)));
    }

    TEST(xdot, A_times_A_T)
    {
        xarray<float> a = xt::ones<float>({5, 3});

        auto r1 = linalg::dot(a, xt::transpose(a));
        EXPECT_TRUE(all(equal(r1, 3.0)));

        auto r2 = linalg::dot(xt::transpose(a), a);
        EXPECT_TRUE(all(equal(r2, 5.0)));
    }

    TEST(xdot, matrix_times_vector_cm)
    {
        xarray<float, layout_type::column_major> a = xt::ones<float>({1, 4});
        xarray<float, layout_type::column_major> b = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 1, 1}};

        xarray<float, layout_type::column_major> e1 = {{13, 16, 19}};

        auto r1 = linalg::dot(a, b);
        EXPECT_EQ(e1, r1);

        xarray<float, layout_type::column_major> c = xt::ones<float>({3, 1});

        auto r2 = linalg::dot(b, c);
        xarray<float, layout_type::column_major> e2 = {{6, 15, 24, 3}};
        e2.reshape({4, 1});
        EXPECT_EQ(e2, r2);

        EXPECT_THROW(linalg::dot(b, a), std::runtime_error);
        EXPECT_THROW(linalg::dot(c, b), std::runtime_error);
    }

    TEST(xdot, square_matrix_times_vector_cm)
    {
        xarray<float, layout_type::column_major> a = {{1, 1, 1}};
        xarray<float, layout_type::column_major> b = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

        auto r1 = linalg::dot(a, b);

        xarray<float, layout_type::column_major> e1 = {{12, 15, 18}};
        EXPECT_EQ(r1, e1);

        auto r2 = linalg::dot(b, xt::transpose(a));
        xarray<float, layout_type::column_major> e2 = xarray<float, layout_type::column_major>::from_shape({3, 1}
        );
        e2(0, 0) = 6.f;
        e2(1, 0) = 15.f;
        e2(2, 0) = 24.f;
        EXPECT_EQ(r2, e2);

        EXPECT_THROW(linalg::dot(b, a), std::runtime_error);
    }

    TEST(xdot, vector_times_vector_cm)
    {
        xarray<float, layout_type::column_major> a = xt::ones<float>({1, 3});
        xarray<float, layout_type::column_major> b = xt::ones<float>({3, 1});

        auto r1 = linalg::dot(a, b);

        xarray<float, layout_type::column_major> e1 = xarray<float, layout_type::column_major>::from_shape({1, 1}
        );
        e1(0, 0) = 3;

        EXPECT_EQ(e1, r1);

        auto r2 = linalg::dot(b, a);
        xarray<float, layout_type::column_major> e2 = xt::ones<float>({3, 3});
        EXPECT_EQ(e2, r2);

        auto r3 = linalg::dot(b, e1);
        EXPECT_EQ(b * 3.f, r3);
    }

    TEST(xdot, on_view)
    {
        xt::xarray<int> a = xt::reshape_view(xt::arange<int>(10 * 10 * 10), {10, 10, 10});
        xt::xarray<int> b = xt::reshape_view(xt::arange<int>(10 * 10 * 10), {10, 10, 10});

        auto res = xt::linalg::dot(view(a, 0, 0), view(b, 0, 0));
        auto res1 = xt::linalg::dot(view(a, 0, range(0, 3)), transpose(view(b, 0, range(0, 3))));

        EXPECT_EQ(res(0), 285.);
        EXPECT_EQ(res1(0, 0), 285.);
        EXPECT_EQ(res1(1, 2), 3635.);

        EXPECT_EQ(res1.dimension(), 2u);
        EXPECT_EQ(res1.shape()[0], 3u);
        EXPECT_EQ(res1.shape()[1], 3u);

        auto res2 = xt::linalg::dot(strided_view(a, {0, 0}), strided_view(b, {0, 0}));
        auto res3 = xt::linalg::dot(
            strided_view(a, {0, range(0, 3)}),
            transpose(strided_view(b, {0, range(0, 3)}))
        );
        EXPECT_EQ(res2(0), 285.);
        EXPECT_EQ(res3(0, 0), 285.);
        EXPECT_EQ(res3(1, 2), 3635.);

        EXPECT_EQ(res3.dimension(), 2u);
        EXPECT_EQ(res3.shape()[0], 3u);
        EXPECT_EQ(res3.shape()[1], 3u);
    }

}  // namespace xt