From 5c619ee2851fb7c74f15fad07d8a48c32cce5b7d Mon Sep 17 00:00:00 2001 From: Kurt Kremitzki Date: Wed, 28 Dec 2016 01:15:08 -0600 Subject: [PATCH] Handle right and left hand scalar-vector multiplication --- src/Base/VectorPyImp.cpp | 52 +++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/Base/VectorPyImp.cpp b/src/Base/VectorPyImp.cpp index ddb1f0fb5..0b9bcc34e 100644 --- a/src/Base/VectorPyImp.cpp +++ b/src/Base/VectorPyImp.cpp @@ -120,29 +120,43 @@ PyObject* VectorPy::number_subtract_handler(PyObject *self, PyObject *other) PyObject* VectorPy::number_multiply_handler(PyObject *self, PyObject *other) { - if (!PyObject_TypeCheck(self, &(VectorPy::Type))) { - PyErr_SetString(PyExc_TypeError, "First arg must be Vector"); - return 0; - } - - if (PyObject_TypeCheck(other, &(VectorPy::Type))) { + if (PyObject_TypeCheck(self, &(VectorPy::Type))) { Base::Vector3d a = static_cast(self) ->value(); - Base::Vector3d b = static_cast(other)->value(); - Py::Float mult(a * b); - return Py::new_reference_to(mult); + if (PyObject_TypeCheck(other, &(VectorPy::Type))) { + Base::Vector3d b = static_cast(other)->value(); + Py::Float mult(a * b); + return Py::new_reference_to(mult); + } + else if (PyFloat_Check(other)) { + double b = PyFloat_AsDouble(other); + return new VectorPy(a * b); + } + else if (PyInt_Check(other)) { + long b = PyInt_AsLong(other); + return new VectorPy(a * (double)b); + } + else { + PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); + return 0; + } } - else if (PyFloat_Check(other)) { - Base::Vector3d a = static_cast(self) ->value(); - double b = PyFloat_AsDouble(other); - return new VectorPy(a * b); - } - else if (PyInt_Check(other)) { - Base::Vector3d a = static_cast(self) ->value(); - long b = PyInt_AsLong(other); - return new VectorPy(a * (double)b); + else if (PyObject_TypeCheck(other, &(VectorPy::Type))) { + Base::Vector3d a = static_cast(other) ->value(); + if (PyFloat_Check(self)) { + double b = PyFloat_AsDouble(self); + return new VectorPy(a * b); + } + else if (PyInt_Check(self)) { + long b = PyInt_AsLong(self); + return new VectorPy(a * (double)b); + } + else { + PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); + return 0; + } } else { - PyErr_SetString(PyExc_TypeError, "A Vector can only be multiplied by Vector or number"); + PyErr_SetString(PyExc_TypeError, "First or second arg must be Vector"); return 0; } }