← Back to team overview

dolfin team mailing list archive

Re: [Merge] lp:~jobh/dolfin/python into lp:dolfin

 

>
> > What I wanted was to prevent this:
> >   >>> v = dolfin.Vector(1)
> >   >>> print type(numpy.float64(1) * v)
> >
> >   <type 'numpy.ndarray'>
> >
> > ... but it didn't achieve that due to float64.__mul__ being called before
> > Vector.__rmul__. So this still needs looking at, to ensure the Vector
> isn't
> > implicitly converted to an ndarray (so that Vector.__rmul__ is called as
> it
> > should).
>
> Yes I recall having the same problem. These numpy types are really greedy
> wrt
> what you can multiply them with...


First: I messed up one instance of the isinstance checks, patch attached (I
can publish it as a branch if you prefer, but it's really trivial).

Second: I tried to find out how to avoid the conversion to ndarray mentioned
above, with a numpy type on the left, but the only way I could find is ugly:
redefining Vector.size to not take an (optional) dimension argument, since
if the call to other.size(0) fails then numpy won't attempt to convert.
Which would make the python and c++ interface diverge further. Maybe it is a
lost cause.

-j.
diff --git a/dolfin/swig/la_post.i b/dolfin/swig/la_post.i
index 6d7836c..03e0722 100644
--- a/dolfin/swig/la_post.i
+++ b/dolfin/swig/la_post.i
@@ -273,7 +273,7 @@ PyObject* _get_eigenpair(dolfin::PETScVector& r, dolfin::PETScVector& c, const i
         from numpy import ndarray, integer, isscalar
         from types import SliceType
         if isinstance(indices, (int, integer)):
-            if numpy.isscalar(values):
+            if isscalar(values):
                 return _set_vector_items_value(self, indices, values)
             else:
                 raise TypeError, "provide a scalar to set single item"

Follow ups

References