File: py_ntensor.cc

package info (click to toggle)
cadabra2 2.4.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 78,732 kB
  • sloc: ansic: 133,450; cpp: 92,064; python: 1,530; javascript: 203; sh: 184; xml: 182; objc: 53; makefile: 51
file content (27 lines) | stat: -rw-r--r-- 1,012 bytes parent folder | download | duplicates (2)
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
#include <pybind11/pybind11.h>
#include "NTensor.hh"

namespace py = pybind11;

namespace cadabra {
	void init_ntensor(py::module& m) {
   	py::class_<NTensor>(m, "NTensor", py::buffer_protocol())
		.def_buffer([](NTensor &nt) -> py::buffer_info {
						size_t stride=sizeof(double);
						std::vector<size_t> strides(nt.shape.size(), 0);
						for(size_t i=0; i<nt.shape.size(); i++) {
							strides[nt.shape.size()-1-i] = stride;
							stride *= nt.shape[i];
							}
						return py::buffer_info(
							nt.values.data(),                         /* Pointer to buffer */
							sizeof(double),                          /* Size of one scalar */
							py::format_descriptor<double>::format(), /* Python struct-style format descriptor */
							nt.shape.size(),                          /* Number of dimensions */
							nt.shape,                                 /* Buffer dimensions */
							strides                                  /* Strides (in bytes) for each index */
													  );
						});
	};

}