LSSTApplications  10.0-2-g4f67435,11.0.rc2+1,11.0.rc2+12,11.0.rc2+3,11.0.rc2+4,11.0.rc2+5,11.0.rc2+6,11.0.rc2+7,11.0.rc2+8
LSSTDataManagementBasePackage
eigen.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 /*
3  * LSST Data Management System
4  * Copyright 2008, 2009, 2010 LSST Corporation.
5  *
6  * This product includes software developed by the
7  * LSST Project (http://www.lsst.org/).
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the LSST License Statement and
20  * the GNU General Public License along with this program. If not,
21  * see <http://www.lsstcorp.org/LegalNotices/>.
22  */
23 
24 #ifndef NDARRAY_SWIG_eigen_h_INCLUDED
25 #define NDARRAY_SWIG_eigen_h_INCLUDED
26 
32 #include "ndarray/eigen.h"
33 #include "ndarray/swig.h"
34 #include "ndarray/swig/eigen.h"
35 
36 namespace ndarray {
37 
42 template <typename T, int N, int C, typename XprKind_, int Rows_, int Cols_>
43 struct PyConverter< EigenView<T,N,C,XprKind_,Rows_,Cols_> > {
44 
45  static bool fromPythonStage1(PyPtr & p) {
46  // add or remove dimensions with size one so we have the right number of dimensions
47  if (PyArray_Check(p.get())) {
48  if ((Rows_ == 1 || Cols_ == 1) && N == 2) {
49  npy_intp shape[2] = { -1, -1 };
50  if (Rows_ == 1) {
51  shape[0] = 1;
52  } else {
53  shape[1] = 1;
54  }
55  PyArray_Dims dims = { shape, 2 };
56  PyPtr r(PyArray_Newshape(reinterpret_cast<PyArrayObject*>(p.get()), &dims, NPY_ANYORDER));
57  if (!r) return false;
58  p.swap(r);
59  } else if (N == 1) {
60  PyPtr r(PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(p.get())));
61  if (!r) return false;
62  p.swap(r);
63  }
64  } // else let the Array converter raise the exception
65  if (!PyConverter< Array<T,N,C> >::fromPythonStage1(p)) return false;
66  // check whether the size is correct if it's static
67  if (N == 2) {
68  if (Rows_ != Eigen::Dynamic && PyArray_DIM(p.get(), 0) != Rows_) {
69  PyErr_SetString(PyExc_ValueError, "incorrect number of rows for matrix");
70  return false;
71  }
72  if (Cols_ != Eigen::Dynamic && PyArray_DIM(p.get(), 1) != Cols_) {
73  PyErr_SetString(PyExc_ValueError, "incorrect number of columns for matrix");
74  return false;
75  }
76  } else {
77  int requiredSize = Rows_ * Cols_;
78  if (requiredSize != Eigen::Dynamic && PyArray_SIZE(p.get()) != requiredSize) {
79  PyErr_SetString(PyExc_ValueError, "incorrect number of elements for vector");
80  return false;
81  }
82  }
83  return true;
84  }
85 
86  static bool fromPythonStage2(
87  PyPtr const & input,
89  ) {
90  Array<T,N,C> array;
91  if (!PyConverter< Array<T,N,C> >::fromPythonStage2(input, array)) return false;
92  output.reset(array);
93  return true;
94  }
95 
96  static PyObject * toPython(EigenView<T,N,C,XprKind_,Rows_,Cols_> const & m, PyObject * owner=NULL) {
97  PyPtr r(PyConverter< Array<T,N,C> >::toPython(m.shallow(), owner));
98  if (!r) return NULL;
99  PyPtr p(PyArray_Squeeze(reinterpret_cast<PyArrayObject*>(r.get())));
100  Py_XINCREF(p.get());
101  return p.get();
102  }
103 
104 };
105 
106 namespace detail {
107 
112 template <typename Matrix>
116 public:
117 
118  static PyObject * toPython(Matrix const & input, PyObject * owner = NULL) {
119  return PyConverter<OutputView>::toPython(ndarray::copy(input), owner);
120  }
121 
122  static PyTypeObject const * getPyType() {
123  return &PyArray_Type;
124  }
125 
126  static bool fromPythonStage1(PyPtr & p) {
128  }
129 
130  static bool fromPythonStage2(PyPtr const & p, Matrix & output) {
131  InputView v;
132  if (!PyConverter<InputView>::fromPythonStage2(p, v)) return false;
133  output = v;
134  return true;
135  }
136 
137 };
138 
139 } // namespace detail
140 
145 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
146 struct PyConverter< Eigen::Matrix<Scalar,Rows,Cols,Options,MaxRows,MaxCols> >
147  : public detail::EigenPyConverter< Eigen::Matrix<Scalar,Rows,Cols,Options,MaxRows,MaxCols> >
148 {};
149 
154 template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
155 struct PyConverter< Eigen::Array<Scalar,Rows,Cols,Options,MaxRows,MaxCols> >
156  : public detail::EigenPyConverter< Eigen::Array<Scalar,Rows,Cols,Options,MaxRows,MaxCols> >
157 {};
158 
159 } // namespace ndarray
160 
161 #endif // !NDARRAY_SWIG_eigen_h_INCLUDED
static bool fromPythonStage2(PyPtr const &p, T &output)
Complete a Python to C++ conversion begun with fromPythonStage1().
Eigen matrix objects that present a view into an ndarray::Array.
boost::intrusive_ptr< PyObject > PyPtr
A reference-counting smart pointer for PyObject.
Definition: PyConverter.h:48
Eigen3 view into an ndarray::Array.
Definition: eigen.h:232
static PyObject * toPython(T const &input)
Convert a C++ object to a new Python object.
static bool fromPythonStage2(PyPtr const &p, Matrix &output)
Definition: eigen.h:130
SelectEigenView< Matrix >::Type OutputView
Definition: eigen.h:114
static PyObject * toPython(Matrix const &input, PyObject *owner=NULL)
Definition: eigen.h:118
SelectEigenView< T >::Type copy(Eigen::EigenBase< T > const &other)
Copy an arbitrary Eigen expression into a new EigenView.
Definition: eigen.h:390
void reset(Array< T, N, C > const &array)
Definition: eigen.h:363
Python C-API conversions for Eigen matrices.
SelectEigenView< Matrix const, false >::Type InputView
Definition: eigen.h:115
A class providing Python conversion functions for T.
Definition: PyConverter.h:50
static bool fromPythonStage1(PyPtr &p)
Definition: eigen.h:126
static PyTypeObject const * getPyType()
Definition: eigen.h:122
static bool fromPythonStage1(PyPtr &p)
Check if a Python object is convertible to T and optionally begin the conversion by replacing the inp...
static bool fromPythonStage2(PyPtr const &input, EigenView< T, N, C, XprKind_, Rows_, Cols_ > &output)
Definition: eigen.h:86
A multidimensional strided array.
Definition: Array.h:47
tuple m
Definition: lsstimport.py:48
static PyObject * toPython(EigenView< T, N, C, XprKind_, Rows_, Cols_ > const &m, PyObject *owner=NULL)
Definition: eigen.h:96
Public header file for SWIG-based Python support.
Array< T, N, C > const & shallow() const
Definition: eigen.h:360