Open3D (C++ API)  0.17.0
Tensor.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <algorithm>
11 #include <cstddef>
12 #include <memory>
13 #include <string>
14 #include <type_traits>
15 
16 #include "open3d/core/Blob.h"
17 #include "open3d/core/DLPack.h"
18 #include "open3d/core/Device.h"
19 #include "open3d/core/Dtype.h"
20 #include "open3d/core/Scalar.h"
21 #include "open3d/core/ShapeUtil.h"
22 #include "open3d/core/SizeVector.h"
24 #include "open3d/core/TensorInit.h"
25 #include "open3d/core/TensorKey.h"
26 
27 namespace open3d {
28 namespace core {
29 
32 class Tensor : public IsDevice {
33 public:
34  Tensor() {}
35 
37  Tensor(const SizeVector& shape,
38  Dtype dtype,
39  const Device& device = Device("CPU:0"))
40  : shape_(shape),
41  strides_(shape_util::DefaultStrides(shape)),
42  dtype_(dtype),
43  blob_(std::make_shared<Blob>(shape.NumElements() * dtype.ByteSize(),
44  device)) {
45  data_ptr_ = blob_->GetDataPtr();
46  }
47 
49  template <typename T>
50  Tensor(const std::vector<T>& init_vals,
51  const SizeVector& shape,
52  Dtype dtype,
53  const Device& device = Device("CPU:0"))
54  : Tensor(shape, dtype, device) {
55  // Check number of elements
56  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
58  "Tensor initialization values' size {} does not match the "
59  "shape {}",
60  init_vals.size(), shape_.NumElements());
61  }
62 
63  // Check data types
64  AssertTemplateDtype<T>();
65  if (!std::is_pod<T>()) {
66  utility::LogError("Object must be a POD.");
67  }
68 
69  // Copy data to blob
71  init_vals.data(),
72  init_vals.size() * dtype.ByteSize());
73  }
74 
76  template <typename T>
77  Tensor(const T* init_vals,
78  const SizeVector& shape,
79  Dtype dtype,
80  const Device& device = Device("CPU:0"))
81  : Tensor(shape, dtype, device) {
82  // Check data types
83  AssertTemplateDtype<T>();
84 
85  // Copy data to blob
87  init_vals,
88  shape_.NumElements() * dtype.ByteSize());
89  }
90 
94  Tensor(const SizeVector& shape,
95  const SizeVector& strides,
96  void* data_ptr,
97  Dtype dtype,
98  const std::shared_ptr<Blob>& blob)
99  : shape_(shape),
100  strides_(strides),
101  data_ptr_(data_ptr),
102  dtype_(dtype),
103  blob_(blob) {}
104 
115  template <typename T>
116  Tensor(std::vector<T>&& vec, const SizeVector& shape = {})
117  : shape_(shape), dtype_(Dtype::FromType<T>()) {
118  if (shape_.empty()) {
119  shape_ = {static_cast<int64_t>(vec.size())};
120  }
121 
122  // Check number of elements.
123  if (static_cast<int64_t>(vec.size()) != shape_.NumElements()) {
125  "Tensor initialization values' size {} does not match the "
126  "shape {}",
127  vec.size(), shape_.NumElements());
128  }
130  auto sp_vec = std::make_shared<std::vector<T>>();
131  sp_vec->swap(vec);
132  data_ptr_ = static_cast<void*>(sp_vec->data());
133 
134  // Create blob that owns the shared pointer to vec. The deleter function
135  // object just stores a shared pointer, ensuring that memory is freed
136  // only when the Tensor is destructed.
137  blob_ = std::make_shared<Blob>(Device("CPU:0"), data_ptr_,
138  [sp_vec](void*) { (void)sp_vec; });
139  }
140 
161  Tensor(void* data_ptr,
162  Dtype dtype,
163  const SizeVector& shape,
164  const SizeVector& strides = {},
165  const Device& device = Device("CPU:0"))
166  : shape_(shape), strides_(strides), data_ptr_(data_ptr), dtype_(dtype) {
167  if (strides_.empty()) {
169  }
170  // Blob with no-op deleter.
171  blob_ = std::make_shared<Blob>(device, (void*)data_ptr_, [](void*) {});
172  }
173 
176  Tensor(const Tensor& other) = default;
177 
180  Tensor(Tensor&& other) = default;
181 
184  Tensor& operator=(const Tensor& other) &;
185 
188  Tensor& operator=(Tensor&& other) &;
189 
192  Tensor& operator=(const Tensor& other) &&;
193 
196  Tensor& operator=(Tensor&& other) &&;
197 
203  template <typename T>
204  Tensor& operator=(const T v) && {
205  this->Fill(v);
206  return *this;
207  }
208 
213  Tensor ReinterpretCast(const core::Dtype& dtype) const;
214 
218  template <typename Object>
219  Tensor& AssignObject(const Object& v) && {
220  if (shape_.size() != 0) {
222  "Assignment with scalar only works for scalar Tensor of "
223  "shape ()");
224  }
225  AssertTemplateDtype<Object>();
227  sizeof(Object));
228  return *this;
229  }
230 
233  template <typename S>
234  void Fill(S v);
235 
236  template <typename Object>
237  void FillObject(const Object& v);
238 
240  static Tensor Empty(const SizeVector& shape,
241  Dtype dtype,
242  const Device& device = Device("CPU:0"));
243 
246  static Tensor EmptyLike(const Tensor& other) {
247  return Tensor::Empty(other.shape_, other.dtype_, other.GetDevice());
248  }
249 
251  template <typename T>
252  static Tensor Full(const SizeVector& shape,
253  T fill_value,
254  Dtype dtype,
255  const Device& device = Device("CPU:0")) {
256  Tensor t = Empty(shape, dtype, device);
257  t.Fill(fill_value);
258  return t;
259  }
260 
262  static Tensor Zeros(const SizeVector& shape,
263  Dtype dtype,
264  const Device& device = Device("CPU:0"));
265 
267  static Tensor Ones(const SizeVector& shape,
268  Dtype dtype,
269  const Device& device = Device("CPU:0"));
270 
273  template <typename T>
274  static Tensor Init(const T val, const Device& device = Device("CPU:0")) {
275  Dtype type = Dtype::FromType<T>();
276  std::vector<T> ele_list{val};
277  SizeVector shape;
278  return Tensor(ele_list, shape, type, device);
279  }
280 
283  template <typename T>
284  static Tensor Init(const std::initializer_list<T>& in_list,
285  const Device& device = Device("CPU:0")) {
286  return InitWithInitializerList<T, 1>(in_list, device);
287  }
288 
291  template <typename T>
292  static Tensor Init(
293  const std::initializer_list<std::initializer_list<T>>& in_list,
294  const Device& device = Device("CPU:0")) {
295  return InitWithInitializerList<T, 2>(in_list, device);
296  }
297 
300  template <typename T>
301  static Tensor Init(
302  const std::initializer_list<
303  std::initializer_list<std::initializer_list<T>>>& in_list,
304  const Device& device = Device("CPU:0")) {
305  return InitWithInitializerList<T, 3>(in_list, device);
306  }
307 
309  static Tensor Eye(int64_t n, Dtype dtype, const Device& device);
310 
312  static Tensor Diag(const Tensor& input);
313 
315  static Tensor Arange(const Scalar start,
316  const Scalar stop,
317  const Scalar step = 1,
318  const Dtype dtype = core::Int64,
319  const Device& device = core::Device("CPU:0"));
320 
322  Tensor Reverse() const;
323 
343  Tensor GetItem(const TensorKey& tk) const;
344 
363  Tensor GetItem(const std::vector<TensorKey>& tks) const;
364 
366  Tensor SetItem(const Tensor& value);
367 
383  Tensor SetItem(const TensorKey& tk, const Tensor& value);
384 
399  Tensor SetItem(const std::vector<TensorKey>& tks, const Tensor& value);
400 
431  Tensor Append(
432  const Tensor& other,
433  const utility::optional<int64_t>& axis = utility::nullopt) const;
434 
436  Tensor Broadcast(const SizeVector& dst_shape) const;
437 
442  Tensor Expand(const SizeVector& dst_shape) const;
443 
456  Tensor Reshape(const SizeVector& dst_shape) const;
457 
478  Tensor Flatten(int64_t start_dim = 0, int64_t end_dim = -1) const;
479 
498  Tensor View(const SizeVector& dst_shape) const;
499 
501  Tensor Clone() const { return To(GetDevice(), /*copy=*/true); }
502 
504  void CopyFrom(const Tensor& other);
505 
510  Tensor To(Dtype dtype, bool copy = false) const;
511 
516  Tensor To(const Device& device, bool copy = false) const;
517 
524  Tensor To(const Device& device, Dtype dtype, bool copy = false) const;
525 
526  std::string ToString(bool with_suffix = true,
527  const std::string& indent = "") const;
528 
530  Tensor operator[](int64_t i) const;
531 
534  Tensor IndexExtract(int64_t dim, int64_t idx) const;
535 
542  Tensor Slice(int64_t dim,
543  int64_t start,
544  int64_t stop,
545  int64_t step = 1) const;
546 
557  Tensor AsRvalue() { return *this; }
558 
560  const Tensor AsRvalue() const { return *this; }
561 
566  Tensor IndexGet(const std::vector<Tensor>& index_tensors) const;
567 
575  void IndexSet(const std::vector<Tensor>& index_tensors,
576  const Tensor& src_tensor);
577 
582  Tensor Permute(const SizeVector& dims) const;
583 
586  Tensor AsStrided(const SizeVector& new_shape,
587  const SizeVector& new_strides) const;
588 
593  Tensor Transpose(int64_t dim0, int64_t dim1) const;
594 
598  Tensor T() const;
599 
602  double Det() const;
603 
606  template <typename T>
607  T Item() const {
608  if (shape_.NumElements() != 1) {
610  "Tensor::Item() only works for Tensor with exactly one "
611  "element.");
612  }
613  AssertTemplateDtype<T>();
614  T value;
615  MemoryManager::MemcpyToHost(&value, data_ptr_, GetDevice(), sizeof(T));
616  return value;
617  }
618 
620  Tensor Add(const Tensor& value) const;
621  Tensor Add(Scalar value) const;
622  Tensor operator+(const Tensor& value) const { return Add(value); }
623  Tensor operator+(Scalar value) const { return Add(value); }
624 
627  Tensor Add_(const Tensor& value);
628  Tensor Add_(Scalar value);
629  Tensor operator+=(const Tensor& value) { return Add_(value); }
630  Tensor operator+=(Scalar value) { return Add_(value); }
631 
633  Tensor Sub(const Tensor& value) const;
634  Tensor Sub(Scalar value) const;
635  Tensor operator-(const Tensor& value) const { return Sub(value); }
636  Tensor operator-(Scalar value) const { return Sub(value); }
637 
640  Tensor Sub_(const Tensor& value);
641  Tensor Sub_(Scalar value);
642  Tensor operator-=(const Tensor& value) { return Sub_(value); }
643  Tensor operator-=(Scalar value) { return Sub_(value); }
644 
646  Tensor Mul(const Tensor& value) const;
647  Tensor Mul(Scalar value) const;
648  Tensor operator*(const Tensor& value) const { return Mul(value); }
649  Tensor operator*(Scalar value) const { return Mul(value); }
650 
653  Tensor Mul_(const Tensor& value);
654  Tensor Mul_(Scalar value);
655  Tensor operator*=(const Tensor& value) { return Mul_(value); }
656  Tensor operator*=(Scalar value) { return Mul_(value); }
657 
659  Tensor Div(const Tensor& value) const;
660  Tensor Div(Scalar value) const;
661  Tensor operator/(const Tensor& value) const { return Div(value); }
662  Tensor operator/(Scalar value) const { return Div(value); }
663 
666  Tensor Div_(const Tensor& value);
667  Tensor Div_(Scalar value);
668  Tensor operator/=(const Tensor& value) { return Div_(value); }
669  Tensor operator/=(Scalar value) { return Div_(value); }
670 
674  Tensor Sum(const SizeVector& dims, bool keepdim = false) const;
675 
679  Tensor Mean(const SizeVector& dims, bool keepdim = false) const;
680 
684  Tensor Prod(const SizeVector& dims, bool keepdim = false) const;
685 
689  Tensor Min(const SizeVector& dims, bool keepdim = false) const;
690 
694  Tensor Max(const SizeVector& dims, bool keepdim = false) const;
695 
704  Tensor ArgMin(const SizeVector& dims) const;
705 
714  Tensor ArgMax(const SizeVector& dims) const;
715 
717  Tensor Sqrt() const;
718 
720  Tensor Sqrt_();
721 
723  Tensor Sin() const;
724 
726  Tensor Sin_();
727 
729  Tensor Cos() const;
730 
732  Tensor Cos_();
733 
735  Tensor Neg() const;
736 
738  Tensor Neg_();
739 
741  Tensor operator-() const { return Neg(); }
742 
744  Tensor Exp() const;
745 
747  Tensor Exp_();
748 
750  Tensor Abs() const;
751 
753  Tensor Abs_();
754 
757  Tensor IsNan() const;
758 
761  Tensor IsInf() const;
762 
766  Tensor IsFinite() const;
767 
772  Tensor Clip(Scalar min_val, Scalar max_val) const;
773 
778  Tensor Clip_(Scalar min_val, Scalar max_val);
779 
781  Tensor Floor() const;
782 
784  Tensor Ceil() const;
785 
787  Tensor Round() const;
788 
790  Tensor Trunc() const;
791 
796  Tensor LogicalNot() const;
797 
805 
810  Tensor LogicalAnd(const Tensor& value) const;
811  Tensor operator&&(const Tensor& value) const { return LogicalAnd(value); }
812  Tensor LogicalAnd(Scalar value) const;
813 
820  Tensor LogicalAnd_(const Tensor& value);
821  Tensor LogicalAnd_(Scalar value);
822 
827  Tensor LogicalOr(const Tensor& value) const;
828  Tensor operator||(const Tensor& value) const { return LogicalOr(value); }
829  Tensor LogicalOr(Scalar value) const;
830 
837  Tensor LogicalOr_(const Tensor& value);
838  Tensor LogicalOr_(Scalar value);
839 
845  Tensor LogicalXor(const Tensor& value) const;
846  Tensor LogicalXor(Scalar value) const;
847 
854  Tensor LogicalXor_(const Tensor& value);
855  Tensor LogicalXor_(Scalar value);
856 
858  Tensor Gt(const Tensor& value) const;
859  Tensor operator>(const Tensor& value) const { return Gt(value); }
860  Tensor Gt(Scalar value) const;
861 
864  Tensor Gt_(const Tensor& value);
865  Tensor Gt_(Scalar value);
866 
868  Tensor Lt(const Tensor& value) const;
869  Tensor operator<(const Tensor& value) const { return Lt(value); }
870  Tensor Lt(Scalar value) const;
871 
874  Tensor Lt_(const Tensor& value);
875  Tensor Lt_(Scalar value);
876 
879  Tensor Ge(const Tensor& value) const;
880  Tensor operator>=(const Tensor& value) const { return Ge(value); }
881  Tensor Ge(Scalar value) const;
882 
885  Tensor Ge_(const Tensor& value);
886  Tensor Ge_(Scalar value);
887 
890  Tensor Le(const Tensor& value) const;
891  Tensor operator<=(const Tensor& value) const { return Le(value); }
892  Tensor Le(Scalar value) const;
893 
896  Tensor Le_(const Tensor& value);
897  Tensor Le_(Scalar value);
898 
900  Tensor Eq(const Tensor& value) const;
901  Tensor operator==(const Tensor& value) const { return Eq(value); }
902  Tensor Eq(Scalar value) const;
903 
906  Tensor Eq_(const Tensor& value);
907  Tensor Eq_(Scalar value);
908 
910  Tensor Ne(const Tensor& value) const;
911  Tensor operator!=(const Tensor& value) const { return Ne(value); }
912  Tensor Ne(Scalar value) const;
913 
916  Tensor Ne_(const Tensor& value);
917  Tensor Ne_(Scalar value);
918 
922  std::vector<Tensor> NonZeroNumpy() const;
923 
928  Tensor NonZero() const;
929 
939  bool IsNonZero() const;
940 
944  bool keepdim = false) const;
945 
949  bool keepdim = false) const;
950 
962  bool AllEqual(const Tensor& other) const;
963 
981  bool AllClose(const Tensor& other,
982  double rtol = 1e-5,
983  double atol = 1e-8) const;
984 
1003  Tensor IsClose(const Tensor& other,
1004  double rtol = 1e-5,
1005  double atol = 1e-8) const;
1006 
1010  bool IsSame(const Tensor& other) const;
1011 
1013  template <typename T>
1014  std::vector<T> ToFlatVector() const {
1015  AssertTemplateDtype<T>();
1016  std::vector<T> values(NumElements());
1018  GetDevice(),
1019  GetDtype().ByteSize() * NumElements());
1020  return values;
1021  }
1022 
1025  inline bool IsContiguous() const {
1027  }
1028 
1032  Tensor Contiguous() const;
1033 
1036  Tensor Matmul(const Tensor& rhs) const;
1037 
1040  Tensor Solve(const Tensor& rhs) const;
1041 
1044  Tensor LeastSquares(const Tensor& rhs) const;
1045 
1053  std::tuple<Tensor, Tensor, Tensor> LU(const bool permute_l = false) const;
1054 
1067  std::tuple<Tensor, Tensor> LUIpiv() const;
1068 
1078  Tensor Triu(const int diagonal = 0) const;
1079 
1089  Tensor Tril(const int diagonal = 0) const;
1090 
1102  std::tuple<Tensor, Tensor> Triul(const int diagonal = 0) const;
1103 
1106  Tensor Inverse() const;
1107 
1110  std::tuple<Tensor, Tensor, Tensor> SVD() const;
1111 
1114  inline int64_t GetLength() const { return GetShape().GetLength(); }
1115 
1116  inline SizeVector GetShape() const { return shape_; }
1117 
1118  inline const SizeVector& GetShapeRef() const { return shape_; }
1119 
1120  inline int64_t GetShape(int64_t dim) const {
1121  return shape_[shape_util::WrapDim(dim, NumDims())];
1122  }
1123 
1124  inline SizeVector GetStrides() const { return strides_; }
1125 
1126  inline const SizeVector& GetStridesRef() const { return strides_; }
1127 
1128  inline int64_t GetStride(int64_t dim) const {
1129  return strides_[shape_util::WrapDim(dim, NumDims())];
1130  }
1131 
1132  template <typename T>
1133  inline T* GetDataPtr() {
1134  return const_cast<T*>(const_cast<const Tensor*>(this)->GetDataPtr<T>());
1135  }
1136 
1137  template <typename T>
1138  inline const T* GetDataPtr() const {
1139  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1141  "Requested values have type {} but Tensor has type {}. "
1142  "Please use non templated GetDataPtr() with manual "
1143  "casting.",
1144  Dtype::FromType<T>().ToString(), dtype_.ToString());
1145  }
1146  return static_cast<T*>(data_ptr_);
1147  }
1148 
1149  inline void* GetDataPtr() { return data_ptr_; }
1150 
1151  inline const void* GetDataPtr() const { return data_ptr_; }
1152 
1153  inline Dtype GetDtype() const { return dtype_; }
1154 
1155  Device GetDevice() const override;
1156 
1157  inline std::shared_ptr<Blob> GetBlob() const { return blob_; }
1158 
1159  inline int64_t NumElements() const { return shape_.NumElements(); }
1160 
1161  inline int64_t NumDims() const { return shape_.size(); }
1162 
1163  template <typename T>
1164  void AssertTemplateDtype() const {
1165  if (!dtype_.IsObject() && Dtype::FromType<T>() != dtype_) {
1167  "Requested values have type {} but Tensor has type {}",
1168  Dtype::FromType<T>().ToString(), dtype_.ToString());
1169  }
1170  if (dtype_.ByteSize() != sizeof(T)) {
1171  utility::LogError("Internal error: element size mismatch {} != {}",
1172  dtype_.ByteSize(), sizeof(T));
1173  }
1174  }
1175 
1177  DLManagedTensor* ToDLPack() const;
1178 
1180  static Tensor FromDLPack(const DLManagedTensor* dlmt);
1181 
1183  void Save(const std::string& file_name) const;
1184 
1186  static Tensor Load(const std::string& file_name);
1187 
1189  struct Iterator {
1190  using iterator_category = std::forward_iterator_tag;
1191  using difference_type = std::ptrdiff_t;
1194  using reference = value_type; // Typically Tensor&, but a tensor slice
1195  // creates a new Tensor object with
1196  // shared memory.
1197 
1198  // Iterator must be constructible, copy-constructible, copy-assignable,
1199  // destructible and swappable.
1200  Iterator(pointer tensor, int64_t index);
1201  Iterator(const Iterator&);
1202  ~Iterator();
1203  reference operator*() const;
1204  pointer operator->() const;
1205  Iterator& operator++();
1206  Iterator operator++(int);
1207  bool operator==(const Iterator& other) const;
1208  bool operator!=(const Iterator& other) const;
1209 
1210  private:
1211  struct Impl;
1212  std::unique_ptr<Impl> impl_;
1213  };
1214 
1216  struct ConstIterator {
1217  using iterator_category = std::forward_iterator_tag;
1218  using difference_type = std::ptrdiff_t;
1219  using value_type = const Tensor;
1221  using reference = value_type; // Typically Tensor&, but a tensor slice
1222  // creates a new Tensor object with
1223  // shared memory.
1224 
1225  // ConstIterator must be constructible, copy-constructible,
1226  // copy-assignable, destructible and swappable.
1227  ConstIterator(pointer tensor, int64_t index);
1228  ConstIterator(const ConstIterator&);
1229  ~ConstIterator();
1230  reference operator*() const;
1231  pointer operator->() const;
1234  bool operator==(const ConstIterator& other) const;
1235  bool operator!=(const ConstIterator& other) const;
1236 
1237  private:
1238  struct Impl;
1239  std::unique_ptr<Impl> impl_;
1240  };
1241 
1245  Iterator begin();
1246 
1250  Iterator end();
1251 
1255  ConstIterator cbegin() const;
1256 
1260  ConstIterator cend() const;
1261 
1266  ConstIterator begin() const { return cbegin(); }
1267 
1272  ConstIterator end() const { return cend(); }
1273 
1274 protected:
1275  std::string ScalarPtrToString(const void* ptr) const;
1276 
1277 private:
1279  template <typename T, size_t D>
1280  static Tensor InitWithInitializerList(
1281  const tensor_init::NestedInitializerList<T, D>& nested_list,
1282  const Device& device = Device("CPU:0")) {
1283  SizeVector shape = tensor_init::InferShape(nested_list);
1284  std::vector<T> values =
1285  tensor_init::ToFlatVector<T, D>(shape, nested_list);
1286  return Tensor(values, shape, Dtype::FromType<T>(), device);
1287  }
1288 
1289 protected:
1292 
1301 
1315  void* data_ptr_ = nullptr;
1316 
1319 
1321  std::shared_ptr<Blob> blob_ = nullptr;
1322 }; // namespace core
1323 
1324 template <>
1325 inline Tensor::Tensor(const std::vector<bool>& init_vals,
1326  const SizeVector& shape,
1327  Dtype dtype,
1328  const Device& device)
1329  : Tensor(shape, dtype, device) {
1330  // Check number of elements
1331  if (static_cast<int64_t>(init_vals.size()) != shape_.NumElements()) {
1333  "Tensor initialization values' size {} does not match the "
1334  "shape {}",
1335  init_vals.size(), shape_.NumElements());
1336  }
1337 
1338  // Check data types
1339  AssertTemplateDtype<bool>();
1340 
1341  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1342  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1343  std::vector<uint8_t> init_vals_uchar(init_vals.size());
1344  std::transform(init_vals.begin(), init_vals.end(), init_vals_uchar.begin(),
1345  [](bool v) -> uint8_t { return static_cast<uint8_t>(v); });
1346 
1348  init_vals_uchar.data(),
1349  init_vals_uchar.size() * dtype.ByteSize());
1350 }
1351 
1352 template <>
1353 inline std::vector<bool> Tensor::ToFlatVector() const {
1354  AssertTemplateDtype<bool>();
1355  std::vector<bool> values(NumElements());
1356  std::vector<uint8_t> values_uchar(NumElements());
1357  MemoryManager::MemcpyToHost(values_uchar.data(), Contiguous().GetDataPtr(),
1358  GetDevice(),
1359  GetDtype().ByteSize() * NumElements());
1360 
1361  // std::vector<bool> possibly implements 1-bit-sized boolean storage.
1362  // Open3D uses 1-byte-sized boolean storage for easy indexing.
1363  std::transform(values_uchar.begin(), values_uchar.end(), values.begin(),
1364  [](uint8_t v) -> bool { return static_cast<bool>(v); });
1365  return values;
1366 }
1367 
1368 template <>
1369 inline bool Tensor::Item() const {
1370  if (shape_.NumElements() != 1) {
1372  "Tensor::Item only works for Tensor with one element.");
1373  }
1374  AssertTemplateDtype<bool>();
1375  uint8_t value;
1377  sizeof(uint8_t));
1378  return static_cast<bool>(value);
1379 }
1380 
1381 template <typename S>
1382 inline void Tensor::Fill(S v) {
1384  scalar_t casted_v = static_cast<scalar_t>(v);
1385  Tensor tmp(std::vector<scalar_t>({casted_v}), SizeVector({}),
1386  GetDtype(), GetDevice());
1387  AsRvalue() = tmp;
1388  });
1389 }
1390 
1391 template <typename Object>
1392 inline void Tensor::FillObject(const Object& v) {
1393  Tensor tmp(std::vector<Object>({v}), SizeVector({}), GetDtype(),
1394  GetDevice());
1395  AsRvalue() = tmp;
1396 }
1397 
1398 template <typename T>
1399 inline Tensor operator+(T scalar_lhs, const Tensor& rhs) {
1400  return rhs + scalar_lhs;
1401 }
1402 
1403 template <typename T>
1404 inline Tensor operator-(T scalar_lhs, const Tensor& rhs) {
1405  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) - rhs;
1406 }
1407 
1408 template <typename T>
1409 inline Tensor operator*(T scalar_lhs, const Tensor& rhs) {
1410  return rhs * scalar_lhs;
1411 }
1412 
1413 template <typename T>
1414 inline Tensor operator/(T scalar_lhs, const Tensor& rhs) {
1415  return Tensor::Full({}, scalar_lhs, rhs.GetDtype(), rhs.GetDevice()) / rhs;
1416 }
1417 
1418 } // namespace core
1419 } // namespace open3d
The common header of DLPack.
#define DISPATCH_DTYPE_TO_TEMPLATE_WITH_BOOL(DTYPE,...)
Definition: Dispatch.h:67
#define LogError(...)
Definition: Logging.h:48
bool copy
Definition: VtkUtils.cpp:73
Definition: Blob.h:38
Definition: Device.h:18
Definition: Dtype.h:20
std::string ToString() const
Definition: Dtype.h:64
bool IsObject() const
Definition: Dtype.h:62
int64_t ByteSize() const
Definition: Dtype.h:58
Definition: Device.h:88
static void MemcpyToHost(void *host_ptr, const void *src_ptr, const Device &src_device, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default dst_device.
Definition: MemoryManager.cpp:85
static void MemcpyFromHost(void *dst_ptr, const Device &dst_device, const void *host_ptr, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default src_device.
Definition: MemoryManager.cpp:77
Definition: Scalar.h:23
Definition: SizeVector.h:69
int64_t NumElements() const
Definition: SizeVector.cpp:108
int64_t GetLength() const
Definition: SizeVector.cpp:124
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:122
size_t size() const
Definition: SmallVector.h:119
Definition: Tensor.h:32
Tensor operator*(Scalar value) const
Definition: Tensor.h:649
Tensor Clone() const
Copy Tensor to the same device.
Definition: Tensor.h:501
T * GetDataPtr()
Definition: Tensor.h:1133
SizeVector strides_
Definition: Tensor.h:1300
Tensor NonZero() const
Definition: Tensor.cpp:1685
Tensor Neg() const
Element-wise negation of a tensor, returning a new tensor.
Definition: Tensor.cpp:1259
const SizeVector & GetStridesRef() const
Definition: Tensor.h:1126
const T * GetDataPtr() const
Definition: Tensor.h:1138
Tensor Ge_(const Tensor &value)
Definition: Tensor.cpp:1566
int64_t GetShape(int64_t dim) const
Definition: Tensor.h:1120
Tensor LogicalNot() const
Definition: Tensor.cpp:1372
Tensor LogicalXor_(const Tensor &value)
Definition: Tensor.cpp:1469
Tensor operator*=(Scalar value)
Definition: Tensor.h:656
Tensor Exp() const
Element-wise exponential of a tensor, returning a new tensor.
Definition: Tensor.cpp:1270
static Tensor Init(const std::initializer_list< T > &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:284
std::vector< T > ToFlatVector() const
Retrieve all values as an std::vector, for debugging and testing.
Definition: Tensor.h:1014
Tensor Flatten(int64_t start_dim=0, int64_t end_dim=-1) const
Definition: Tensor.cpp:652
std::vector< Tensor > NonZeroNumpy() const
Definition: Tensor.cpp:1676
Tensor LeastSquares(const Tensor &rhs) const
Definition: Tensor.cpp:1847
Tensor operator&&(const Tensor &value) const
Definition: Tensor.h:811
Tensor Ge(const Tensor &value) const
Definition: Tensor.cpp:1548
bool AllClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1804
ConstIterator cend() const
Definition: Tensor.cpp:312
double Det() const
Compute the determinant of a 2D square tensor.
Definition: Tensor.cpp:1022
Tensor All(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1700
Tensor & operator=(const T v) &&
Definition: Tensor.h:204
std::string ScalarPtrToString(const void *ptr) const
Definition: Tensor.cpp:792
Tensor SetItem(const Tensor &value)
Set all items. Equivalent to tensor[:] = value in Python.
Definition: Tensor.cpp:531
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:367
Tensor Cos() const
Element-wise cosine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1248
Tensor operator||(const Tensor &value) const
Definition: Tensor.h:828
Tensor IndexExtract(int64_t dim, int64_t idx) const
Definition: Tensor.cpp:808
Tensor Sin() const
Element-wise sine of a tensor, returning a new tensor.
Definition: Tensor.cpp:1237
Tensor(Tensor &&other)=default
Tensor Floor() const
Element-wise floor value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1341
Tensor Inverse() const
Definition: Tensor.cpp:1891
void Fill(S v)
Fill the whole Tensor with a scalar value, the scalar will be casted to the Tensor's Dtype.
Definition: Tensor.h:1382
Tensor & AssignObject(const Object &v) &&
Definition: Tensor.h:219
Tensor Sum(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1170
Tensor Gt_(const Tensor &value)
Definition: Tensor.cpp:1502
Tensor LogicalAnd(const Tensor &value) const
Definition: Tensor.cpp:1383
static Tensor Ones(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with ones.
Definition: Tensor.cpp:379
Tensor Lt_(const Tensor &value)
Definition: Tensor.cpp:1534
Tensor IsClose(const Tensor &other, double rtol=1e-5, double atol=1e-8) const
Definition: Tensor.cpp:1809
Tensor(const Tensor &other)=default
Tensor Le(const Tensor &value) const
Definition: Tensor.cpp:1580
static Tensor Arange(const Scalar start, const Scalar stop, const Scalar step=1, const Dtype dtype=core::Int64, const Device &device=core::Device("CPU:0"))
Create a 1D tensor with evenly spaced values in the given interval.
Definition: Tensor.cpp:403
SizeVector GetShape() const
Definition: Tensor.h:1116
Tensor LogicalOr_(const Tensor &value)
Definition: Tensor.cpp:1436
int64_t GetLength() const
Definition: Tensor.h:1114
Tensor & operator=(const Tensor &other) &
Definition: Tensor.cpp:322
Tensor LogicalOr(const Tensor &value) const
Definition: Tensor.cpp:1417
Iterator end()
Definition: Tensor.cpp:243
Tensor operator+=(Scalar value)
Definition: Tensor.h:630
Tensor Sub(const Tensor &value) const
Substracts a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1063
Tensor AsStrided(const SizeVector &new_shape, const SizeVector &new_strides) const
Create a Tensor view of specified shape and strides. The underlying buffer and data_ptr offsets remai...
Definition: Tensor.cpp:991
Tensor(void *data_ptr, Dtype dtype, const SizeVector &shape, const SizeVector &strides={}, const Device &device=Device("CPU:0"))
Tensor wrapper constructor from raw host buffer.
Definition: Tensor.h:161
const void * GetDataPtr() const
Definition: Tensor.h:1151
std::tuple< Tensor, Tensor, Tensor > SVD() const
Definition: Tensor.cpp:1899
Tensor Add_(const Tensor &value)
Definition: Tensor.cpp:1047
Tensor ArgMin(const SizeVector &dims) const
Definition: Tensor.cpp:1212
Tensor Tril(const int diagonal=0) const
Returns the lower triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1879
void Save(const std::string &file_name) const
Save tensor to numpy's npy format.
Definition: Tensor.cpp:1786
Tensor Reverse() const
Reverse a Tensor's elements by viewing the tensor as a 1D array.
Definition: Tensor.cpp:432
void * data_ptr_
Definition: Tensor.h:1315
std::string ToString(bool with_suffix=true, const std::string &indent="") const
Definition: Tensor.cpp:747
Tensor Slice(int64_t dim, int64_t start, int64_t stop, int64_t step=1) const
Definition: Tensor.cpp:824
Tensor operator*=(const Tensor &value)
Definition: Tensor.h:655
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1198
Iterator begin()
Definition: Tensor.cpp:236
Tensor IsFinite() const
Definition: Tensor.cpp:1312
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1009
Tensor AsRvalue()
Definition: Tensor.h:557
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1177
Tensor Div(const Tensor &value) const
Divides a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1135
Tensor Lt(const Tensor &value) const
Element-wise less-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1516
Tensor ReinterpretCast(const core::Dtype &dtype) const
Definition: Tensor.cpp:355
Tensor Trunc() const
Element-wise trunc value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1359
Tensor Sub_(const Tensor &value)
Definition: Tensor.cpp:1083
Tensor Sqrt_()
Element-wise square root of a tensor, in-place.
Definition: Tensor.cpp:1232
Tensor Neg_()
Element-wise negation of a tensor, in-place.
Definition: Tensor.cpp:1265
Tensor operator/(const Tensor &value) const
Definition: Tensor.h:661
Tensor Contiguous() const
Definition: Tensor.cpp:739
Tensor ArgMax(const SizeVector &dims) const
Definition: Tensor.cpp:1219
Dtype dtype_
Data type.
Definition: Tensor.h:1318
static Tensor Diag(const Tensor &input)
Create a square matrix with specified diagonal elements in input.
Definition: Tensor.cpp:391
int64_t NumDims() const
Definition: Tensor.h:1161
ConstIterator cbegin() const
Definition: Tensor.cpp:305
Tensor LogicalAnd_(const Tensor &value)
Definition: Tensor.cpp:1402
Tensor operator-() const
Unary minus of a tensor, returning a new tensor.
Definition: Tensor.h:741
Tensor Transpose(int64_t dim0, int64_t dim1) const
Transpose a Tensor by swapping dimension dim0 and dim1.
Definition: Tensor.cpp:998
Tensor Clip_(Scalar min_val, Scalar max_val)
Definition: Tensor.cpp:1328
Tensor Gt(const Tensor &value) const
Element-wise greater-than of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1484
DLManagedTensor * ToDLPack() const
Convert the Tensor to DLManagedTensor.
Definition: Tensor.cpp:1738
Tensor(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor.
Definition: Tensor.h:37
std::shared_ptr< Blob > GetBlob() const
Definition: Tensor.h:1157
void CopyFrom(const Tensor &other)
Copy Tensor values to current tensor from the source tensor.
Definition: Tensor.cpp:737
Tensor operator/=(const Tensor &value)
Definition: Tensor.h:668
Tensor operator/(Scalar value) const
Definition: Tensor.h:662
static Tensor EmptyLike(const Tensor &other)
Definition: Tensor.h:246
static Tensor Full(const SizeVector &shape, T fill_value, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with specified value.
Definition: Tensor.h:252
Tensor View(const SizeVector &dst_shape) const
Definition: Tensor.cpp:688
void FillObject(const Object &v)
Definition: Tensor.h:1392
bool IsContiguous() const
Definition: Tensor.h:1025
Tensor operator<(const Tensor &value) const
Definition: Tensor.h:869
Tensor Abs() const
Element-wise absolute value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1281
Tensor Any(const utility::optional< SizeVector > &dims=utility::nullopt, bool keepdim=false) const
Definition: Tensor.cpp:1719
std::shared_ptr< Blob > blob_
Underlying memory buffer for Tensor.
Definition: Tensor.h:1321
Tensor operator+=(const Tensor &value)
Definition: Tensor.h:629
Tensor Eq_(const Tensor &value)
Definition: Tensor.cpp:1630
Tensor operator-=(Scalar value)
Definition: Tensor.h:643
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:373
Tensor Le_(const Tensor &value)
Definition: Tensor.cpp:1598
bool AllEqual(const Tensor &other) const
Definition: Tensor.cpp:1794
void * GetDataPtr()
Definition: Tensor.h:1149
Tensor operator+(Scalar value) const
Definition: Tensor.h:623
Tensor Reshape(const SizeVector &dst_shape) const
Definition: Tensor.cpp:638
void AssertTemplateDtype() const
Definition: Tensor.h:1164
static Tensor Init(const std::initializer_list< std::initializer_list< T >> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:292
Tensor Add(const Tensor &value) const
Adds a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1027
ConstIterator begin() const
Definition: Tensor.h:1266
Tensor Prod(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1191
Tensor Cos_()
Element-wise cosine of a tensor, in-place.
Definition: Tensor.cpp:1254
Tensor Triu(const int diagonal=0) const
Returns the upper triangular matrix of the 2D tensor, above the given diagonal index....
Definition: Tensor.cpp:1873
Tensor Div_(const Tensor &value)
Definition: Tensor.cpp:1155
Tensor Sqrt() const
Element-wise square root of a tensor, returns a new tensor.
Definition: Tensor.cpp:1226
SizeVector shape_
SizeVector of the Tensor. shape_[i] is the length of dimension i.
Definition: Tensor.h:1291
Tensor(const T *init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor from raw host buffer. The memory will be copied.
Definition: Tensor.h:77
Tensor()
Definition: Tensor.h:34
Tensor Clip(Scalar min_val, Scalar max_val) const
Definition: Tensor.cpp:1322
Device GetDevice() const override
Definition: Tensor.cpp:1365
static Tensor Load(const std::string &file_name)
Load tensor from numpy's npy format.
Definition: Tensor.cpp:1790
bool IsSame(const Tensor &other) const
Definition: Tensor.cpp:1821
std::tuple< Tensor, Tensor, Tensor > LU(const bool permute_l=false) const
Computes LU factorisation of the 2D square tensor, using A = P * L * U; where P is the permutation ma...
Definition: Tensor.cpp:1857
Tensor operator==(const Tensor &value) const
Definition: Tensor.h:901
int64_t GetStride(int64_t dim) const
Definition: Tensor.h:1128
static Tensor Init(const T val, const Device &device=Device("CPU:0"))
Definition: Tensor.h:274
bool IsNonZero() const
Definition: Tensor.cpp:1687
Tensor operator-(Scalar value) const
Definition: Tensor.h:636
Tensor Ne(const Tensor &value) const
Element-wise not-equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1644
Tensor Solve(const Tensor &rhs) const
Definition: Tensor.cpp:1837
Tensor Matmul(const Tensor &rhs) const
Definition: Tensor.cpp:1828
Tensor operator>=(const Tensor &value) const
Definition: Tensor.h:880
T Item() const
Definition: Tensor.h:607
std::tuple< Tensor, Tensor > Triul(const int diagonal=0) const
Returns the tuple of upper and lower triangular matrix of the 2D tensor, above and below the given di...
Definition: Tensor.cpp:1885
Tensor Mul_(const Tensor &value)
Definition: Tensor.cpp:1119
Tensor Exp_()
Element-wise base-e exponential of a tensor, in-place.
Definition: Tensor.cpp:1276
Tensor operator/=(Scalar value)
Definition: Tensor.h:669
Tensor Sin_()
Element-wise sine of a tensor, in-place.
Definition: Tensor.cpp:1243
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:385
static Tensor Init(const std::initializer_list< std::initializer_list< std::initializer_list< T >>> &in_list, const Device &device=Device("CPU:0"))
Definition: Tensor.h:301
Tensor(const SizeVector &shape, const SizeVector &strides, void *data_ptr, Dtype dtype, const std::shared_ptr< Blob > &blob)
Definition: Tensor.h:94
Tensor operator+(const Tensor &value) const
Definition: Tensor.h:622
Tensor(const std::vector< T > &init_vals, const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Constructor for creating a contiguous Tensor with initial values.
Definition: Tensor.h:50
int64_t NumElements() const
Definition: Tensor.h:1159
Tensor GetItem(const TensorKey &tk) const
Definition: Tensor.cpp:440
Tensor operator*(const Tensor &value) const
Definition: Tensor.h:648
Tensor operator[](int64_t i) const
Extract the i-th Tensor along the first axis, returning a new view.
Definition: Tensor.cpp:806
static Tensor FromDLPack(const DLManagedTensor *dlmt)
Convert DLManagedTensor to Tensor.
Definition: Tensor.cpp:1742
Tensor Abs_()
Element-wise absolute value of a tensor, in-place.
Definition: Tensor.cpp:1287
Tensor operator!=(const Tensor &value) const
Definition: Tensor.h:911
const SizeVector & GetShapeRef() const
Definition: Tensor.h:1118
Tensor operator-(const Tensor &value) const
Definition: Tensor.h:635
Tensor IsInf() const
Definition: Tensor.cpp:1302
Tensor Broadcast(const SizeVector &dst_shape) const
Broadcast Tensor to a new broadcastable shape.
Definition: Tensor.cpp:595
Tensor(std::vector< T > &&vec, const SizeVector &shape={})
Take ownership of data in std::vector<T>
Definition: Tensor.h:116
Dtype GetDtype() const
Definition: Tensor.h:1153
Tensor Mul(const Tensor &value) const
Multiplies a tensor and returns the resulting tensor.
Definition: Tensor.cpp:1099
Tensor operator<=(const Tensor &value) const
Definition: Tensor.h:891
ConstIterator end() const
Definition: Tensor.h:1272
Tensor operator-=(const Tensor &value)
Definition: Tensor.h:642
Tensor Permute(const SizeVector &dims) const
Permute (dimension shuffle) the Tensor, returns a view.
Definition: Tensor.cpp:958
void IndexSet(const std::vector< Tensor > &index_tensors, const Tensor &src_tensor)
Advanced indexing getter.
Definition: Tensor.cpp:903
Tensor Expand(const SizeVector &dst_shape) const
Definition: Tensor.cpp:605
Tensor Ne_(const Tensor &value)
Definition: Tensor.cpp:1662
SizeVector GetStrides() const
Definition: Tensor.h:1124
Tensor IsNan() const
Definition: Tensor.cpp:1292
Tensor LogicalXor(const Tensor &value) const
Definition: Tensor.cpp:1450
std::tuple< Tensor, Tensor > LUIpiv() const
Computes LU factorisation of the 2D square tensor, using A = P * L * U; where P is the permutation ma...
Definition: Tensor.cpp:1865
Tensor operator>(const Tensor &value) const
Definition: Tensor.h:859
const Tensor AsRvalue() const
Convert to constant rvalue.
Definition: Tensor.h:560
Tensor Append(const Tensor &other, const utility::optional< int64_t > &axis=utility::nullopt) const
Appends the other tensor, along the given axis and returns a copy of the tensor. The other tensors mu...
Definition: Tensor.cpp:589
Tensor Eq(const Tensor &value) const
Element-wise equals-to of tensors, returning a new boolean tensor.
Definition: Tensor.cpp:1612
Tensor LogicalNot_()
Definition: Tensor.cpp:1378
Tensor To(Dtype dtype, bool copy=false) const
Definition: Tensor.cpp:706
Tensor Ceil() const
Element-wise ceil value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1347
Tensor IndexGet(const std::vector< Tensor > &index_tensors) const
Advanced indexing getter. This will always allocate a new Tensor.
Definition: Tensor.cpp:872
Tensor Round() const
Element-wise round value of a tensor, returning a new tensor.
Definition: Tensor.cpp:1353
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1205
TensorKey is used to represent single index, slice or advanced indexing on a Tensor.
Definition: TensorKey.h:26
char type
Definition: FilePCD.cpp:41
int64_t WrapDim(int64_t dim, int64_t max_dim, bool inclusive)
Wrap around negative dim.
Definition: ShapeUtil.cpp:131
SizeVector DefaultStrides(const SizeVector &shape)
Compute default strides for a shape when a tensor is contiguous.
Definition: ShapeUtil.cpp:214
SizeVector InferShape(const L &list)
Definition: TensorInit.h:82
typename NestedInitializerImpl< T, D >::type NestedInitializerList
Definition: TensorInit.h:36
Tensor operator+(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1399
const Dtype Int64
Definition: Dtype.cpp:47
Tensor operator/(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1414
const Dtype Undefined
Definition: Dtype.cpp:41
Tensor operator-(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1404
Tensor operator*(T scalar_lhs, const Tensor &rhs)
Definition: Tensor.h:1409
const char const char value recording_handle imu_sample void
Definition: K4aPlugin.cpp:250
constexpr nullopt_t nullopt
Definition: Optional.h:152
Definition: PinholeCameraIntrinsic.cpp:16
Definition: Device.h:107
C Tensor object, manage memory of DLTensor. This data structure is intended to facilitate the borrowi...
Definition: DLPack.h:174
Const iterator for Tensor.
Definition: Tensor.h:1216
ConstIterator & operator++()
Definition: Tensor.cpp:283
ConstIterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:258
~ConstIterator()
Definition: Tensor.cpp:272
pointer operator->() const
Definition: Tensor.cpp:278
const Tensor value_type
Definition: Tensor.h:1219
reference operator*() const
Definition: Tensor.cpp:274
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1217
std::ptrdiff_t difference_type
Definition: Tensor.h:1218
bool operator!=(const ConstIterator &other) const
Definition: Tensor.cpp:300
bool operator==(const ConstIterator &other) const
Definition: Tensor.cpp:294
Iterator for Tensor.
Definition: Tensor.h:1189
Iterator(pointer tensor, int64_t index)
Definition: Tensor.cpp:191
std::ptrdiff_t difference_type
Definition: Tensor.h:1191
Iterator & operator++()
Definition: Tensor.cpp:216
bool operator==(const Iterator &other) const
Definition: Tensor.cpp:227
pointer operator->() const
Definition: Tensor.cpp:211
std::forward_iterator_tag iterator_category
Definition: Tensor.h:1190
~Iterator()
Definition: Tensor.cpp:205
bool operator!=(const Iterator &other) const
Definition: Tensor.cpp:232
reference operator*() const
Definition: Tensor.cpp:207
Tensor value_type
Definition: Tensor.h:1192