Skip to content

Commit

Permalink
Handle right and left hand scalar-vector multiplication
Browse files Browse the repository at this point in the history
  • Loading branch information
kkremitzki authored and wwmayer committed Dec 28, 2016
1 parent c3401f8 commit 5c619ee
Showing 1 changed file with 33 additions and 19 deletions.
52 changes: 33 additions & 19 deletions src/Base/VectorPyImp.cpp
Expand Up @@ -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<VectorPy*>(self) ->value();
Base::Vector3d b = static_cast<VectorPy*>(other)->value();
Py::Float mult(a * b);
return Py::new_reference_to(mult);
}
else if (PyFloat_Check(other)) {
Base::Vector3d a = static_cast<VectorPy*>(self) ->value();
double b = PyFloat_AsDouble(other);
return new VectorPy(a * b);
if (PyObject_TypeCheck(other, &(VectorPy::Type))) {
Base::Vector3d b = static_cast<VectorPy*>(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 (PyInt_Check(other)) {
Base::Vector3d a = static_cast<VectorPy*>(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<VectorPy*>(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;
}
}
Expand Down

0 comments on commit 5c619ee

Please sign in to comment.