|
6 | 6 | # The full license is in the file LICENSE, distributed with this software. #
|
7 | 7 | ############################################################################
|
8 | 8 |
|
9 |
| -include("tensors.jl") |
| 9 | +using Test |
| 10 | + |
| 11 | +module TensorFunctions |
| 12 | +using CxxWrap |
| 13 | +using Xtensor |
| 14 | +@wrapmodule Xtensor._l_tensors |
| 15 | +end |
| 16 | + |
| 17 | +# Test example functions from the TensorFunctions module |
| 18 | + |
| 19 | +# example1 |
| 20 | +arr = [[1.0 2.0] |
| 21 | + [3.0 4.0]] |
| 22 | +@test TensorFunctions.test_access(arr) == 4.0 |
| 23 | +@test TensorFunctions.example1([4., 5., 6.]) == 4.0 |
| 24 | + |
| 25 | +# example2 |
| 26 | +x = [[0. 1.] |
| 27 | + [2. 3.]] |
| 28 | +res = [[2. 3.] |
| 29 | + [4. 5.]] |
| 30 | +y = TensorFunctions.example2(x) |
| 31 | +@test y ≈ res atol=1e-12 |
| 32 | + |
| 33 | +# vectorize_example1 |
| 34 | +x1 = [[0 1] |
| 35 | + [2 3]] |
| 36 | +x2 = [0, 1] |
| 37 | +res = [[0 2] |
| 38 | + [2 4]] |
| 39 | +y = TensorFunctions.vectorize_example1(x1, x2) |
| 40 | +@test y == res |
| 41 | + |
| 42 | +# readme_example1 |
| 43 | +v = [[0.0 3.0 6.0 9.0 12.0] |
| 44 | + [1.0 4.0 7.0 10.0 13.0] |
| 45 | + [2.0 5.0 8.0 11.0 14.0]] |
| 46 | + |
| 47 | +y = TensorFunctions.readme_example1(v) |
| 48 | +@test y ≈ 1.2853996391883833 atol=1e-12 |
| 49 | + |
| 50 | +# readme_example2 |
| 51 | +x = [[ 0.0 1.0 2.0 3.0 4.0] |
| 52 | + [ 5.0 6.0 7.0 8.0 9.0] |
| 53 | + [10.0 11.0 12.0 13.0 14.0]] |
| 54 | +y = [1.0, 2.0, 3.0, 4.0, 5.0] |
| 55 | +z = TensorFunctions.readme_example2(x, y) |
| 56 | +expected = [[-0.540302 1.257618 1.89929 0.794764 -1.040465] |
| 57 | + [-1.499227 0.136731 1.646979 1.643002 0.128456] |
| 58 | + [-1.084323 -0.583843 0.45342 1.073811 0.706945]] |
| 59 | +@test z ≈ expected atol=1e-5 |
| 60 | + |
| 61 | +# compare_shapes |
| 62 | +x = ones(4, 4) |
| 63 | +y = ones(5, 5) |
| 64 | +z = zeros(4, 4) |
| 65 | +@assert !TensorFunctions.compare_shapes(x, y) |
| 66 | +@assert TensorFunctions.compare_shapes(x, z) |
0 commit comments