Mantid
Loading...
Searching...
No Matches
MatrixToNumpy.h
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2012 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#pragma once
8
10#include "MantidKernel/System.h"
14
15#include <type_traits>
16
17#include <boost/mpl/and.hpp>
18#include <boost/mpl/if.hpp>
19
20namespace Mantid {
21namespace PythonInterface {
22namespace Policies {
23
24namespace // anonymous
25{
26//-----------------------------------------------------------------------
27// MPL helper structs
28//-----------------------------------------------------------------------
31template <typename T> struct is_matrix : boost::false_type {};
32
35template <typename T> struct is_matrix<Kernel::Matrix<T>> : boost::true_type {};
36
37//-----------------------------------------------------------------------
38// MatrixRefToNumpyImpl - Policy for reference returns
39//-----------------------------------------------------------------------
44template <typename MatrixType, typename ConversionPolicy> struct MatrixRefToNumpyImpl {
45 inline PyObject *operator()(const MatrixType &cmatrix) const {
47 }
48
49 inline PyTypeObject const *get_pytype() const { return ndarrayType(); }
50};
51
52template <typename T> struct MatrixRefToNumpy_Requires_Reference_To_Matrix_Return_Type {};
53} // namespace
54
65template <typename ConversionPolicy> struct MatrixRefToNumpy {
66 // The boost::python framework calls return_value_policy::apply<T>::type
67 template <class T> struct apply {
68 // Typedef that removes and const or reference qualifiers from the return
69 // type
70 using non_const_type = typename std::remove_const<typename std::remove_reference<T>::type>::type;
71 // MPL compile-time check that T is a reference to a Kernel::Matrix
72 using type = typename boost::mpl::if_c<boost::mpl::and_<std::is_reference<T>, is_matrix<non_const_type>>::value,
73 MatrixRefToNumpyImpl<non_const_type, ConversionPolicy>,
74 MatrixRefToNumpy_Requires_Reference_To_Matrix_Return_Type<T>>::type;
75 };
76};
77
78//-----------------------------------------------------------------------
79// MatrixToNumpy return_value_policy
80//-----------------------------------------------------------------------
81namespace {
86template <typename MatrixType> struct MatrixToNumpyImpl {
87 inline PyObject *operator()(const MatrixType &cvector) const {
89 }
90
91 inline PyTypeObject const *get_pytype() const { return ndarrayType(); }
92};
93
94template <typename T> struct MatrixToNumpy_Requires_Matrix_Return_By_Value {};
95} // namespace
96
104 // The boost::python framework calls return_value_policy::apply<T>::type
105 template <class T> struct apply {
106 // Typedef that removes any const from the type
107 using non_const_type = typename std::remove_const<T>::type;
108 // MPL compile-time check that T is a std::vector
109 using type = typename boost::mpl::if_c<is_matrix<non_const_type>::value,
110 MatrixRefToNumpyImpl<non_const_type, Converters::Clone>,
111 MatrixToNumpy_Requires_Matrix_Return_By_Value<T>>::type;
112 };
113};
114} // namespace Policies
115} // namespace PythonInterface
116} // namespace Mantid
double value
The value of the point.
Definition: FitMW.cpp:51
MANTID_PYTHONINTERFACE_CORE_DLL PyTypeObject * ndarrayType()
Return the type object for a numpy.NDArray.
Definition: NDArray.cpp:38
Helper class which provides the Collimation Length for SANS instruments.
Converter that takes a Mantid Matrix and converts it into a numpy array.
typename std::remove_const< typename std::remove_reference< T >::type >::type non_const_type
Definition: MatrixToNumpy.h:70
typename boost::mpl::if_c< boost::mpl::and_< std::is_reference< T >, is_matrix< non_const_type > >::value, MatrixRefToNumpyImpl< non_const_type, ConversionPolicy >, MatrixRefToNumpy_Requires_Reference_To_Matrix_Return_Type< T > >::type type
Definition: MatrixToNumpy.h:74
Implements a return value policy that returns a numpy array from a Matrix.
Definition: MatrixToNumpy.h:65
typename std::remove_const< T >::type non_const_type
typename boost::mpl::if_c< is_matrix< non_const_type >::value, MatrixRefToNumpyImpl< non_const_type, Converters::Clone >, MatrixToNumpy_Requires_Matrix_Return_By_Value< T > >::type type
Implements a return value policy that returns a numpy array from a function returning a std::vector b...