ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
tensor_function.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "core/TensorFunction.h"
10 #include "pybind/core/core.h"
11 #include "pybind/docstring.h"
12 #include "pybind/pybind_utils.h"
13 
14 namespace cloudViewer {
15 namespace core {
16 
17 void pybind_core_tensor_function(py::module& m) {
18  m.def(
19  "concatenate",
20  [](const std::vector<Tensor>& tensors,
21  const utility::optional<int64_t>& axis) {
22  if (axis.has_value()) {
23  return core::Concatenate(tensors, axis);
24  }
25  return core::Concatenate(tensors);
26  },
27  R"(Concatenates the list of tensors in their order, along the given
28 axis into a new tensor. All the tensors must have same data-type, device, and
29 number of dimensions. All dimensions must be the same, except the dimension
30 along the axis the tensors are to be concatenated.
31 Using Concatenate for a single tensor, the tensor is split along its first
32 dimension (length), and concatenated along the axis.
33 
34 This is the same as NumPy's semantics:
35 - https://numpy.org/doc/stable/reference/generated/numpy.concatenate.html
36 
37 Returns:
38  A new tensor with the values of list of tensors concatenated in order,
39  along the given axis.
40 
41 Example:
42  >>> a = o3d.core.Tensor([[0, 1], [2, 3]])
43  >>> b = o3d.core.Tensor([[4, 5]])
44  >>> c = o3d.core.Tensor([[6, 7])
45  >>> o3d.core.concatenate((a, b, c), 0)
46  [[0 1],
47  [2 3],
48  [4 5],
49  [6 7],
50  [8 9]]
51  Tensor[shape={5, 2}, stride={2, 1}, Int64, CPU:0, 0x55b454b09390])",
52  "tensors"_a, "axis"_a = 0);
53 
54  m.def(
55  "append",
56  [](const Tensor& self, const Tensor& values,
57  const utility::optional<int64_t>& axis) {
58  if (axis.has_value()) {
59  return core::Append(self, values, axis);
60  }
61  return core::Append(self, values);
62  },
63  R"(Appends the `values` tensor to the `self` tensor, along the
64 given axis and returns a new tensor. Both the tensors must have same data-type
65 device, and number of dimensions. All dimensions must be the same, except the
66 dimension along the axis the tensors are to be appended.
67 
68 This is the same as NumPy's semantics:
69 - https://numpy.org/doc/stable/reference/generated/numpy.append.html
70 
71 Returns:
72  A copy of the `self` tensor with `values` appended to axis. Note that
73  append does not occur in-place: a new array is allocated and filled.
74  If axis is null, out is a flattened tensor.
75 
76 Example:
77  >>> o3d.core.append([[0, 1], [2, 3]], [[4, 5]], axis = 0)
78  [[0 1],
79  [2 3],
80  [4 5]]
81  Tensor[shape={3, 2}, stride={2, 1}, Int64, CPU:0, 0x55555abc6b00]
82 
83  >>> o3d.core.append([[0, 1], [2, 3]], [[4, 5]])
84  [0 1 2 3 4 5]
85  Tensor[shape={6}, stride={1}, Int64, CPU:0, 0x55555abc6b70])",
86  "self"_a, "values"_a, "axis"_a = py::none());
87 
88  m.def("maximum", &core::Maximum,
89  R"(Computes the element-wise maximum of input and other. The tensors
90 must have same data type and device.
91 If input.GetShape() != other.GetShape(), then they will be broadcasted to a
92 common shape (which becomes the shape of the output).)",
93  "input"_a, "other"_a);
94  m.def("minimum", &core::Minimum,
95  R"(Computes the element-wise minimum of input and other. The tensors
96 must have same data type and device.
97 If input.GetShape() != other.GetShape(), then they will be broadcasted to a
98 common shape (which becomes the shape of the output).)",
99  "input"_a, "other"_a);
100 }
101 
102 } // namespace core
103 } // namespace cloudViewer
constexpr bool has_value() const noexcept
Definition: Optional.h:440
void pybind_core_tensor_function(py::module &m)
Tensor Concatenate(const std::vector< Tensor > &tensors, const utility::optional< int64_t > &axis)
Concatenates the list of tensors in their order, along the given axis into a new tensor....
Tensor Append(const Tensor &self, const Tensor &other, const utility::optional< int64_t > &axis)
Appends the two tensors, along the given axis into a new tensor. Both the tensors must have same data...
Tensor Minimum(const Tensor &input, const Tensor &other)
Computes the element-wise minimum of input and other. The tensors must have same data type and device...
Tensor Maximum(const Tensor &input, const Tensor &other)
Computes the element-wise maximum of input and other. The tensors must have same data type and device...
Generic file read and write utility for python interface.