Line data Source code
1 : // SPDX-License-Identifier: BSD-3-Clause
2 : // Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
3 : /// @file
4 : /// @author Simon Heybrock
5 : #include "scipp/core/eigen.h"
6 : #include "scipp/core/tag_util.h"
7 : #include "scipp/variable/creation.h"
8 :
9 : #include "dim.h"
10 : #include "dtype.h"
11 : #include "pybind11.h"
12 : #include "unit.h"
13 :
14 : using namespace scipp;
15 :
16 : namespace py = pybind11;
17 :
18 : template <class T> struct MakeZeros {
19 139 : static variable::Variable apply(const std::vector<std::string> &dims,
20 : const std::vector<scipp::index> &shape,
21 : const units::Unit &unit,
22 : const bool with_variances) {
23 : return with_variances
24 3 : ? makeVariable<T>(make_dims(dims, shape), unit, Values{},
25 3 : Variances{})
26 142 : : makeVariable<T>(make_dims(dims, shape), unit, Values{});
27 : }
28 : };
29 :
30 3 : void init_creation(py::module &m) {
31 3 : m.def(
32 : "empty",
33 373 : [](const std::vector<std::string> &dims,
34 : const std::vector<scipp::index> &shape, const ProtoUnit &unit,
35 : const py::object &dtype, const bool with_variances,
36 : const bool aligned) {
37 373 : const auto dtype_ = scipp_dtype(dtype);
38 373 : py::gil_scoped_release release;
39 373 : const auto unit_ = unit_or_default(unit, dtype_);
40 746 : return variable::empty(make_dims(dims, shape), unit_, dtype_,
41 1119 : with_variances, aligned);
42 373 : },
43 6 : py::arg("dims"), py::arg("shape"), py::arg("unit") = DefaultUnit{},
44 6 : py::arg("dtype") = py::none(), py::arg("with_variances") = false,
45 6 : py::arg("aligned") = true);
46 3 : m.def(
47 : "zeros",
48 139 : [](const std::vector<std::string> &dims,
49 : const std::vector<scipp::index> &shape, const ProtoUnit &unit,
50 : const py::object &dtype, const bool with_variances) {
51 139 : const auto dtype_ = scipp_dtype(dtype);
52 139 : py::gil_scoped_release release;
53 139 : const auto unit_ = unit_or_default(unit, dtype_);
54 : return core::CallDType<
55 : double, float, int64_t, int32_t, bool, scipp::core::time_point,
56 : std::string, Eigen::Vector3d,
57 : Eigen::Matrix3d>::apply<MakeZeros>(dtype_, dims, shape, unit_,
58 278 : with_variances);
59 139 : },
60 6 : py::arg("dims"), py::arg("shape"), py::arg("unit") = DefaultUnit{},
61 6 : py::arg("dtype") = py::none(), py::arg("with_variances") = std::nullopt);
62 3 : m.def(
63 : "ones",
64 1826 : [](const std::vector<std::string> &dims,
65 : const std::vector<scipp::index> &shape, const ProtoUnit &unit,
66 : const py::object &dtype, const bool with_variances) {
67 1826 : const auto dtype_ = scipp_dtype(dtype);
68 1826 : py::gil_scoped_release release;
69 1826 : const auto unit_ = unit_or_default(unit, dtype_);
70 3653 : return variable::ones(make_dims(dims, shape), unit_, dtype_,
71 5477 : with_variances);
72 1826 : },
73 6 : py::arg("dims"), py::arg("shape"), py::arg("unit") = DefaultUnit{},
74 6 : py::arg("dtype") = py::none(), py::arg("with_variances") = std::nullopt);
75 3 : }
|