Mantid
Loading...
Searching...
No Matches
MatrixToNDArray.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
7//-----------------------------------------------------------------------------
8// Includes
9//-----------------------------------------------------------------------------
12
13#define PY_ARRAY_UNIQUE_SYMBOL CORE_ARRAY_API
14#define NO_IMPORT_ARRAY
15#include <numpy/arrayobject.h>
16
17namespace Mantid {
18namespace PythonInterface {
19namespace Converters {
20namespace Impl {
30template <typename ContainerType> PyObject *wrapWithNDArray(const ContainerType &cdata, const NumpyWrapMode mode) {
31 std::pair<size_t, size_t> matrixDims = cdata.size();
32 npy_intp dims[2] = {matrixDims.first, matrixDims.second};
34 PyObject *ndarray = PyArray_SimpleNewFromData(2, dims, datatype, (void *)&(cdata[0][0]));
35 if (mode == ReadOnly) {
36 PyArrayObject *np = (PyArrayObject *)ndarray;
37 np->flags &= ~NPY_WRITEABLE;
38 }
39 return ndarray;
40}
41//-----------------------------------------------------------------------
42// Explicit instantiations
43//-----------------------------------------------------------------------
44#define INSTANTIATE_MATRIX_WRAP(ElementType) \
45 template DLLExport PyObject *wrapWithNDArray<Kernel::Matrix<ElementType>>(const Kernel::Matrix<ElementType> &, \
46 const NumpyWrapMode);
47
51} // namespace Impl
52} // namespace Converters
53} // namespace PythonInterface
54} // namespace Mantid
#define INSTANTIATE_MATRIX_WRAP(ElementType)
tagPyArrayObject PyArrayObject
PyObject * wrapWithNDArray(const ElementType *, const int ndims, Py_intptr_t *dims, const NumpyWrapMode mode, const OwnershipMode oMode=OwnershipMode::Cpp)
Defines the wrapWithNDArray specialization for C array types.
NumpyWrapMode
Enum defining wrapping type for conversion to numpy.
Helper class which provides the Collimation Length for SANS instruments.
Defines a mapping between C++ type given by the template parameter and numpy type enum NPY_TYPES.