Commit 90555f750cc761dd40372e8b8d09851df4666cca
1 parent
fcd5778e
Implement __getitem__ and __setitem__ operators.
Implement matrix access operators that use Python tuples. For handling of Python tuples, a special SWIG file names GTypemaps.i was created. This file has to be included to expose the typemaps to the interface.
Showing
3 changed files
with
30 additions
and
49 deletions
pyext/GMatrix.i
| @@ -29,6 +29,9 @@ | @@ -29,6 +29,9 @@ | ||
| 29 | #include "GTools.hpp" | 29 | #include "GTools.hpp" |
| 30 | %} | 30 | %} |
| 31 | 31 | ||
| 32 | +/* __ Includes ___________________________________________________________ */ | ||
| 33 | +%include "GTypemaps.i" | ||
| 34 | + | ||
| 32 | 35 | ||
| 33 | /***********************************************************************//** | 36 | /***********************************************************************//** |
| 34 | * @class GMatrix | 37 | * @class GMatrix |
| @@ -86,26 +89,16 @@ public: | @@ -86,26 +89,16 @@ public: | ||
| 86 | 89 | ||
| 87 | /***********************************************************************//** | 90 | /***********************************************************************//** |
| 88 | * @brief GMatrix class extension | 91 | * @brief GMatrix class extension |
| 89 | - * | ||
| 90 | - * @todo Use typemap to allow for a [row,col] matrix access | ||
| 91 | ***************************************************************************/ | 92 | ***************************************************************************/ |
| 92 | %extend GMatrix { | 93 | %extend GMatrix { |
| 93 | char *__str__() { | 94 | char *__str__() { |
| 94 | return tochar(self->print()); | 95 | return tochar(self->print()); |
| 95 | } | 96 | } |
| 96 | - double __getslice__(int row, int col) { | ||
| 97 | - if (row >=0 && row < (int)self->rows() && col >= 0 && col < (int)self->cols()) | ||
| 98 | - return (*self)(row,col); | ||
| 99 | - else | ||
| 100 | - throw GException::out_of_range("__getitem__(int,int)", row, col, | ||
| 101 | - (int)self->rows(), (int)self->cols()); | 97 | + double __getitem__(int GTuple[2]) { |
| 98 | + return (*self)(GTuple[0], GTuple[1]); | ||
| 102 | } | 99 | } |
| 103 | - void __setslice__(int row, int col, const double val) { | ||
| 104 | - if (row >=0 && row < (int)self->rows() && col >= 0 && col < (int)self->cols()) | ||
| 105 | - (*self)(row,col) = val; | ||
| 106 | - else | ||
| 107 | - throw GException::out_of_range("__setitem__(int,int)", row, col, | ||
| 108 | - (int)self->rows(), (int)self->cols()); | 100 | + void __setitem__(int GTuple[2], double value) { |
| 101 | + (*self)(GTuple[0], GTuple[1]) = value; | ||
| 109 | } | 102 | } |
| 110 | GMatrix __mul__(const double &a) { | 103 | GMatrix __mul__(const double &a) { |
| 111 | return (*self) * a; | 104 | return (*self) * a; |
pyext/GSparseMatrix.i
| @@ -29,12 +29,17 @@ | @@ -29,12 +29,17 @@ | ||
| 29 | #include "GTools.hpp" | 29 | #include "GTools.hpp" |
| 30 | %} | 30 | %} |
| 31 | 31 | ||
| 32 | +/* __ Includes ___________________________________________________________ */ | ||
| 33 | +%include "GTypemaps.i" | ||
| 32 | 34 | ||
| 33 | -/*************************************************************************** | ||
| 34 | - * GSparseMatrix class definition * | ||
| 35 | - * ----------------------------------------------------------------------- * | ||
| 36 | - * GMatrix implements a sparse matrix storage class. It derives from the * | ||
| 37 | - * abstract base class GMatrixBase. * | 35 | + |
| 36 | +/***********************************************************************//** | ||
| 37 | + * @class GSparseMatrix | ||
| 38 | + * | ||
| 39 | + * @brief Sparse matrix class definition | ||
| 40 | + * | ||
| 41 | + * GSparseMatrix implements a sparse matrix storage class. It derives from | ||
| 42 | + * the abstract base class GMatrixBase. | ||
| 38 | ***************************************************************************/ | 43 | ***************************************************************************/ |
| 39 | class GSparseMatrix : public GMatrixBase { | 44 | class GSparseMatrix : public GMatrixBase { |
| 40 | public: | 45 | public: |
| @@ -95,28 +100,18 @@ public: | @@ -95,28 +100,18 @@ public: | ||
| 95 | }; | 100 | }; |
| 96 | 101 | ||
| 97 | 102 | ||
| 98 | -/*************************************************************************** | ||
| 99 | - * GSparseMatrix class SWIG extension * | ||
| 100 | - * | ||
| 101 | - * @todo Use typemap to allow for a [row,col] matrix access | 103 | +/***********************************************************************//** |
| 104 | + * @brief GSparseMatrix class extension | ||
| 102 | ***************************************************************************/ | 105 | ***************************************************************************/ |
| 103 | %extend GSparseMatrix { | 106 | %extend GSparseMatrix { |
| 104 | char *__str__() { | 107 | char *__str__() { |
| 105 | return tochar(self->print()); | 108 | return tochar(self->print()); |
| 106 | } | 109 | } |
| 107 | - double __getslice__(int row, int col) { | ||
| 108 | - if (row >=0 && row < (int)self->rows() && col >= 0 && col < (int)self->cols()) | ||
| 109 | - return (*self)(row,col); | ||
| 110 | - else | ||
| 111 | - throw GException::out_of_range("__getitem__(int,int)", row, col, | ||
| 112 | - (int)self->rows(), (int)self->cols()); | 110 | + double __getitem__(int GTuple[2]) { |
| 111 | + return (*self)(GTuple[0], GTuple[1]); | ||
| 113 | } | 112 | } |
| 114 | - void __setslice__(int row, int col, const double val) { | ||
| 115 | - if (row >=0 && row < (int)self->rows() && col >= 0 && col < (int)self->cols()) | ||
| 116 | - (*self)(row,col) = val; | ||
| 117 | - else | ||
| 118 | - throw GException::out_of_range("__setitem__(int,int)", row, col, | ||
| 119 | - (int)self->rows(), (int)self->cols()); | 113 | + void __setitem__(int GTuple[2], double value) { |
| 114 | + (*self)(GTuple[0], GTuple[1]) = value; | ||
| 120 | } | 115 | } |
| 121 | GSparseMatrix __mul__(const double &a) { | 116 | GSparseMatrix __mul__(const double &a) { |
| 122 | return (*self) * a; | 117 | return (*self) * a; |
pyext/GSymMatrix.i
| @@ -29,6 +29,9 @@ | @@ -29,6 +29,9 @@ | ||
| 29 | #include "GTools.hpp" | 29 | #include "GTools.hpp" |
| 30 | %} | 30 | %} |
| 31 | 31 | ||
| 32 | +/* __ Includes ___________________________________________________________ */ | ||
| 33 | +%include "GTypemaps.i" | ||
| 34 | + | ||
| 32 | 35 | ||
| 33 | /***********************************************************************//** | 36 | /***********************************************************************//** |
| 34 | * @class GSymMatrix | 37 | * @class GSymMatrix |
| @@ -85,26 +88,16 @@ public: | @@ -85,26 +88,16 @@ public: | ||
| 85 | 88 | ||
| 86 | /***********************************************************************//** | 89 | /***********************************************************************//** |
| 87 | * @brief GSymMatrix class extension | 90 | * @brief GSymMatrix class extension |
| 88 | - * | ||
| 89 | - * @todo Use typemap to allow for a [row,col] matrix access | ||
| 90 | ***************************************************************************/ | 91 | ***************************************************************************/ |
| 91 | %extend GSymMatrix { | 92 | %extend GSymMatrix { |
| 92 | char *__str__() { | 93 | char *__str__() { |
| 93 | return tochar(self->print()); | 94 | return tochar(self->print()); |
| 94 | } | 95 | } |
| 95 | - double __getslice__(int row, int col) { | ||
| 96 | - if (row >=0 && row < (int)self->rows() && col >= 0 && col < (int)self->cols()) | ||
| 97 | - return (*self)(row,col); | ||
| 98 | - else | ||
| 99 | - throw GException::out_of_range("__getitem__(int,int)", row, col, | ||
| 100 | - (int)self->rows(), (int)self->cols()); | 96 | + double __getitem__(int GTuple[2]) { |
| 97 | + return (*self)(GTuple[0], GTuple[1]); | ||
| 101 | } | 98 | } |
| 102 | - void __setslice__(int row, int col, const double val) { | ||
| 103 | - if (row >=0 && row < (int)self->rows() && col >= 0 && col < (int)self->cols()) | ||
| 104 | - (*self)(row,col) = val; | ||
| 105 | - else | ||
| 106 | - throw GException::out_of_range("__setitem__(int,int)", row, col, | ||
| 107 | - (int)self->rows(), (int)self->cols()); | 99 | + void __setitem__(int GTuple[2], double value) { |
| 100 | + (*self)(GTuple[0], GTuple[1]) = value; | ||
| 108 | } | 101 | } |
| 109 | GSymMatrix __mul__(const double &a) { | 102 | GSymMatrix __mul__(const double &a) { |
| 110 | return (*self) * a; | 103 | return (*self) * a; |