8 #ifndef _PyImathFixedMatrix_h_
9 #define _PyImathFixedMatrix_h_
11 #define HBOOST_BIND_GLOBAL_PLACEHOLDERS
12 #include <hboost/python.hpp>
35 : _ptr(ptr), _rows(rows), _cols(cols),
42 : _ptr(new
T[rows*cols]), _rows(rows), _cols(cols),
43 _rowStride(1), _colStride(1), _refcount(new
int(1))
49 : _ptr(other._ptr), _rows(other._rows), _cols(other._cols),
50 _rowStride(other._rowStride), _colStride(other._colStride),
51 _refcount(other._refcount)
53 if (_refcount) *_refcount += 1;
59 if (&other ==
this)
return *
this;
64 _rowStride = other._rowStride;
65 _colStride = other._colStride;
66 _refcount = other._refcount;
68 if (_refcount) *_refcount += 1;
77 if (*_refcount == 0) {
97 if (index < 0) index += _rows;
98 if (index >= _rows || index < 0) {
99 PyErr_SetString(PyExc_IndexError,
"Index out of range");
100 hboost::python::throw_error_already_set();
108 if (PySlice_Check(index)) {
109 #if PY_MAJOR_VERSION > 2
110 PyObject *slice = index;
112 PySliceObject *slice =
reinterpret_cast<PySliceObject *
>(index);
114 if (PySlice_GetIndicesEx(slice,_rows,&start,&end,&step,&slicelength) == -1) {
115 hboost::python::throw_error_already_set();
117 }
else if (PyInt_Check(index)) {
119 start = i; end = i+1; step = 1; slicelength = 1;
121 PyErr_SetString(PyExc_TypeError,
"Object is not a slice");
122 hboost::python::throw_error_already_set();
134 Py_ssize_t
start,
end, step, slicelength;
137 for (
int i=0; i<slicelength; ++i) {
138 for (
int j=0;
j<_cols; ++
j) {
148 Py_ssize_t
start,
end, step, slicelength;
150 for (
int i=0; i<slicelength; ++i) {
151 for (
int j = 0;
j < _cols; ++
j) {
160 Py_ssize_t
start,
end, step, slicelength;
162 if (data.
len() != _cols) {
163 PyErr_SetString(PyExc_IndexError,
"Dimensions of source do not match destination");
164 hboost::python::throw_error_already_set();
166 for (
int i=0; i<slicelength; ++i) {
167 for (
int j = 0;
j < _cols; ++
j) {
176 Py_ssize_t
start,
end, step, slicelength;
180 if (data.
rows() != slicelength || data.
cols() !=
cols()) {
181 PyErr_SetString(PyExc_IndexError,
"Dimensions of source do not match destination");
182 hboost::python::throw_error_already_set();
184 for (
int i=0; i<slicelength; ++i) {
191 int rows()
const {
return _rows; }
192 int cols()
const {
return _cols; }
196 T &
element(
int i,
int j) {
return _ptr[i*_rowStride*_cols*_colStride+j*_colStride]; }
197 const T &
element(
int i,
int j)
const {
return _ptr[i*_rowStride*_cols*_colStride+j*_colStride]; }
202 static hboost::python::class_<FixedMatrix<T> >
register_(
const char *
name,
const char *doc)
204 hboost::python::class_<FixedMatrix<T> >
c(name,doc, hboost::python::init<int,int>(
"return an uninitialized array of the specified rows and cols"));
222 PyErr_SetString(PyExc_IndexError,
"Dimensions of source do not match destination");
223 hboost::python::throw_error_already_set();
230 template <
template <
class,
class>
class Op,
class T1,
class Ret>
233 int rows = a1.
rows();
234 int cols = a1.
cols();
236 for (
int i=0;i<rows;++i)
for (
int j=0;
j<cols; ++
j) {
243 template <
template <
class,
class,
class>
class Op,
class T1,
class T2,
class Ret>
247 int cols = a1.
cols();
249 for (
int i=0;i<rows;++i)
for (
int j=0;
j<cols; ++
j) {
255 template <
template <
class,
class,
class>
class Op,
class T1,
class T2,
class Ret>
258 int rows = a1.
rows();
259 int cols = a1.
cols();
261 for (
int i=0;i<rows;++i)
for (
int j=0;
j<cols; ++
j) {
267 template <
template <
class,
class,
class>
class Op,
class T1,
class T2,
class Ret>
270 int rows = a1.
rows();
271 int cols = a1.
cols();
273 for (
int i=0;i<rows;++i)
for (
int j=0;
j<cols; ++
j) {
280 template <
template <
class,
class>
class Op,
class T1,
class T2>
284 int cols = a1.
cols();
285 for (
int i=0;i<rows;++i)
for (
int j=0;
j<cols; ++
j) {
292 template <
template <
class,
class>
class Op,
class T1,
class T2>
295 int rows = a1.
rows();
296 int cols = a1.
cols();
297 for (
int i=0;i<rows;++i)
for (
int j=0;
j<cols; ++
j) {
298 Op<T1,T2>::apply(a1.
element(i,
j),a2);
304 template <
class T>
static FixedMatrix<T> operator + (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_add,T,T,T>(a0,a1); }
305 template <
class T>
static FixedMatrix<T> operator + (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_add,T,T,T>(a0,
v1); }
306 template <
class T>
static FixedMatrix<T> operator + (
const T &
v1,
const FixedMatrix<T> &a0) {
return a0+
v1; }
309 template <
class T>
static FixedMatrix<T> operator - (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_sub,T,T,T>(a0,a1); }
310 template <
class T>
static FixedMatrix<T> operator - (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_sub,T,T,T>(a0,
v1); }
311 template <
class T>
static FixedMatrix<T> operator - (
const T &
v1,
const FixedMatrix<T> &a0) {
return apply_matrix_scalar_binary_op<op_rsub,T,T,T>(a0,
v1); }
314 template <
class T>
static FixedMatrix<T> operator * (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_mul,T,T,T>(a0,a1); }
315 template <
class T>
static FixedMatrix<T> operator * (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_mul,T,T,T>(a0,
v1); }
316 template <
class T>
static FixedMatrix<T> operator * (
const T &
v1,
const FixedMatrix<T> &a0) {
return a0*
v1; }
319 template <
class T>
static FixedMatrix<T> operator / (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_div,T,T,T>(a0,a1); }
320 template <
class T>
static FixedMatrix<T> operator / (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_div,T,T,T>(a0,
v1); }
326 template <
class T>
static FixedMatrix<T> operator % (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_mod,T,T,T>(a0,a1); }
327 template <
class T>
static FixedMatrix<T> operator % (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_mod,T,T,T>(a0,
v1); }
333 template <
class T>
static FixedMatrix<T> pow_matrix_matrix (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_pow,T,T,T>(a0,a1); }
334 template <
class T>
static FixedMatrix<T> pow_matrix_scalar (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_pow,T,T,T>(a0,
v1); }
338 template <
class T>
static FixedMatrix<T> operator - (
const FixedMatrix<T> &a0) {
return apply_matrix_unary_op<op_neg,T,T>(a0); }
343 template <
class T>
static FixedMatrix<T>
abs (
const FixedMatrix<T> &a0) {
return apply_matrix_unary_op<op_abs,T,T>(a0); }
346 template <
class T>
static FixedMatrix<T> operator ~ (
const FixedMatrix<T> &a0) {
return apply_matrix_unary_op<op_inverse,T,T>(a0); }
349 template <
class T>
static FixedMatrix<T> operator << (const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_lshift,T,T,T>(a0,a1); }
350 template <
class T>
static FixedMatrix<T> operator << (const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_lshift,T,T,T>(a0,
v1); }
354 template <
class T>
static FixedMatrix<T> operator >> (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_rshift,T,T,T>(a0,a1); }
355 template <
class T>
static FixedMatrix<T> operator >> (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_rshift,T,T,T>(a0,
v1); }
359 template <
class T>
static FixedMatrix<T> operator & (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_bitand,T,T,T>(a0,a1); }
360 template <
class T>
static FixedMatrix<T> operator & (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_bitand,T,T,T>(a0,
v1); }
361 template <
class T>
static FixedMatrix<T> operator & (
const T &
v1,
const FixedMatrix<T> &a0) {
return a0&
v1; }
364 template <
class T>
static FixedMatrix<T> operator ^ (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_xor,T,T,T>(a0,a1); }
365 template <
class T>
static FixedMatrix<T> operator ^ (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_xor,T,T,T>(a0,
v1); }
366 template <
class T>
static FixedMatrix<T> operator ^ (
const T &
v1,
const FixedMatrix<T> &a0) {
return a0^
v1; }
369 template <
class T>
static FixedMatrix<T> operator | (
const FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_binary_op<op_bitor,T,T,T>(a0,a1); }
370 template <
class T>
static FixedMatrix<T> operator | (
const FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_binary_op<op_bitor,T,T,T>(a0,
v1); }
371 template <
class T>
static FixedMatrix<T> operator | (
const T &
v1,
const FixedMatrix<T> &a0) {
return a0|
v1; }
375 template <
class T>
static FixedMatrix<T> & operator += (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_iadd,T,T>(a0,a1); }
376 template <
class T>
static FixedMatrix<T> & operator += (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_iadd,T,T>(a0,
v1); }
379 template <
class T>
static FixedMatrix<T> & operator -= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_isub,T,T>(a0,a1); }
380 template <
class T>
static FixedMatrix<T> & operator -= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_isub,T,T>(a0,
v1); }
383 template <
class T>
static FixedMatrix<T> & operator *= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_imul,T,T>(a0,a1); }
384 template <
class T>
static FixedMatrix<T> & operator *= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_imul,T,T>(a0,
v1); }
387 template <
class T>
static FixedMatrix<T> & operator /= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_idiv,T,T>(a0,a1); }
388 template <
class T>
static FixedMatrix<T> & operator /= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_idiv,T,T>(a0,
v1); }
397 template <
class T>
static FixedMatrix<T> & operator %= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_imod,T,T>(a0,a1); }
398 template <
class T>
static FixedMatrix<T> & operator %= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_imod,T,T>(a0,
v1); }
401 template <
class T>
static FixedMatrix<T> & ipow_matrix_matrix (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_ipow,T,T>(a0,a1); }
402 template <
class T>
static FixedMatrix<T> & ipow_matrix_scalar (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_ipow,T,T>(a0,
v1); }
405 template <
class T>
static FixedMatrix<T> & operator <<= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_ilshift,T,T>(a0,a1); }
406 template <
class T>
static FixedMatrix<T> & operator <<= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_ilshift,T,T>(a0,
v1); }
409 template <
class T>
static FixedMatrix<T> & operator >>= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_irshift,T,T>(a0,a1); }
410 template <
class T>
static FixedMatrix<T> & operator >>= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_irshift,T,T>(a0,
v1); }
413 template <
class T>
static FixedMatrix<T> & operator &= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_ibitand,T,T>(a0,a1); }
414 template <
class T>
static FixedMatrix<T> & operator &= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_ibitand,T,T>(a0,
v1); }
417 template <
class T>
static FixedMatrix<T> & operator ^= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_ixor,T,T>(a0,a1); }
418 template <
class T>
static FixedMatrix<T> & operator ^= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_ixor,T,T>(a0,
v1); }
421 template <
class T>
static FixedMatrix<T> & operator |= (FixedMatrix<T> &a0,
const FixedMatrix<T> &a1) {
return apply_matrix_matrix_ibinary_op<op_ibitor,T,T>(a0,a1); }
422 template <
class T>
static FixedMatrix<T> & operator |= (FixedMatrix<T> &a0,
const T &
v1) {
return apply_matrix_scalar_ibinary_op<op_ibitor,T,T>(a0,
v1); }
425 static void add_arithmetic_math_functions(hboost::python::class_<FixedMatrix<T> > &
c) {
426 using namespace hboost::python;
428 .def(
"__add__",&apply_matrix_matrix_binary_op<op_add,T,T,T>)
429 .def(
"__add__",&apply_matrix_scalar_binary_op<op_add,T,T,T>)
430 .def(
"__radd__",&apply_matrix_scalar_binary_rop<op_add,T,T,T>)
431 .def(
"__sub__",&apply_matrix_matrix_binary_op<op_sub,T,T,T>)
432 .def(
"__sub__",&apply_matrix_scalar_binary_op<op_sub,T,T,T>)
433 .def(
"__rsub__",&apply_matrix_scalar_binary_op<op_rsub,T,T,T>)
434 .def(
"__mul__",&apply_matrix_matrix_binary_op<op_mul,T,T,T>)
435 .def(
"__mul__",&apply_matrix_scalar_binary_op<op_mul,T,T,T>)
436 .def(
"__rmul__",&apply_matrix_scalar_binary_rop<op_mul,T,T,T>)
437 .def(
"__div__",&apply_matrix_matrix_binary_op<op_div,T,T,T>)
438 .def(
"__div__",&apply_matrix_scalar_binary_op<op_div,T,T,T>)
439 .def(
"__truediv__",&apply_matrix_matrix_binary_op<op_div,T,T,T>)
440 .def(
"__truediv__",&apply_matrix_scalar_binary_op<op_div,T,T,T>)
441 .def(
"__neg__",&apply_matrix_unary_op<op_neg,T,T>)
442 .def(
"__iadd__",&apply_matrix_matrix_ibinary_op<op_iadd,T,T>,return_internal_reference<>())
443 .def(
"__iadd__",&apply_matrix_scalar_ibinary_op<op_iadd,T,T>,return_internal_reference<>())
444 .def(
"__isub__",&apply_matrix_matrix_ibinary_op<op_isub,T,T>,return_internal_reference<>())
445 .def(
"__isub__",&apply_matrix_scalar_ibinary_op<op_isub,T,T>,return_internal_reference<>())
446 .def(
"__imul__",&apply_matrix_matrix_ibinary_op<op_imul,T,T>,return_internal_reference<>())
447 .def(
"__imul__",&apply_matrix_scalar_ibinary_op<op_imul,T,T>,return_internal_reference<>())
448 .def(
"__idiv__",&apply_matrix_matrix_ibinary_op<op_idiv,T,T>,return_internal_reference<>())
449 .def(
"__idiv__",&apply_matrix_scalar_ibinary_op<op_idiv,T,T>,return_internal_reference<>())
450 .def(
"__itruediv__",&apply_matrix_matrix_ibinary_op<op_idiv,T,T>,return_internal_reference<>())
451 .def(
"__itruediv__",&apply_matrix_scalar_ibinary_op<op_idiv,T,T>,return_internal_reference<>())
456 static void add_pow_math_functions(hboost::python::class_<FixedMatrix<T> > &
c) {
457 using namespace hboost::python;
459 .def(
"__pow__",&pow_matrix_scalar<T>)
460 .def(
"__pow__",&pow_matrix_matrix<T>)
461 .def(
"__ipow__",&ipow_matrix_scalar<T>,return_internal_reference<>())
462 .def(
"__ipow__",&ipow_matrix_matrix<T>,return_internal_reference<>())
467 static void add_mod_math_functions(hboost::python::class_<FixedMatrix<T> > &
c) {
468 using namespace hboost::python;
471 .def(
self % other<T>())
473 .def(
self %= other<T>())
478 static void add_shift_math_functions(hboost::python::class_<FixedMatrix<T> > &
c) {
479 using namespace hboost::python;
482 .def(
self << other<T>())
484 .def(
self <<= other<T>())
486 .def(
self >> other<T>())
488 .def(
self >>= other<T>())
493 static void add_bitwise_math_functions(hboost::python::class_<FixedMatrix<T> > &
c) {
494 using namespace hboost::python;
497 .def(
self & other<T>())
499 .def(
self &= other<T>())
501 .def(
self | other<T>())
503 .def(
self |= other<T>())
505 .def(
self ^ other<T>())
507 .def(
self ^= other<T>())
typedef int(APIENTRYP RE_PFNGLXSWAPINTERVALSGIPROC)(int)
FixedMatrix< Ret > apply_matrix_matrix_binary_op(const FixedMatrix< T1 > &a1, const FixedMatrix< T2 > &a2)
FixedMatrix getslice(PyObject *index) const
T & element(int i, int j)
const T & element(int i, int j) const
FixedMatrix< Ret > apply_matrix_scalar_binary_rop(const FixedMatrix< T1 > &a1, const T2 &a2)
FixedMatrix< T1 > & apply_matrix_matrix_ibinary_op(FixedMatrix< T1 > &a1, const FixedMatrix< T2 > &a2)
void setitem_scalar(PyObject *index, const T &data)
Py_ssize_t convert_index(int index) const
GLuint const GLchar * name
static hboost::python::class_< FixedMatrix< T > > register_(const char *name, const char *doc)
FixedMatrix(T *ptr, int rows, int cols, int rowStride=1, int colStride=1)
const FixedMatrix & operator=(const FixedMatrix &other)
void setitem_matrix(PyObject *index, const FixedMatrix &data)
FixedMatrix< Ret > apply_matrix_scalar_binary_op(const FixedMatrix< T1 > &a1, const T2 &a2)
void extract_slice_indices(PyObject *index, Py_ssize_t &start, Py_ssize_t &end, Py_ssize_t &step, Py_ssize_t &slicelength) const
FixedMatrix< Ret > apply_matrix_unary_op(const FixedMatrix< T1 > &a1)
FixedMatrix(const FixedMatrix &other)
void setitem_vector(PyObject *index, const FixedArray< T > &data)
FixedMatrix(int rows, int cols)
IMATH_INTERNAL_NAMESPACE_HEADER_ENTER IMATH_HOSTDEVICE constexpr T abs(T a) IMATH_NOEXCEPT
FixedArray< T > operator[](int i)
int match_dimension(const FixedMatrix< T2 > &a1) const
FixedMatrix< T1 > & apply_matrix_scalar_ibinary_op(FixedMatrix< T1 > &a1, const T2 &a2)
const FixedArray< T > * getitem(int index) const