GenericVector * numpy.float = numpy.array, not GenericVector

Issue #874 resolved
Nico Schlömer created an issue

When multiplying a GenericVector with a float, the result is a GenericVector. When multiplying a GenericVector with a numpy.float, the result is a numpy.array. Yikes! This leads to all sorts of unexpected behavior since of course numpy.array doesn't play well with FEniCS's methods.

It was hard to find this cause of the errors I got since print() doesn't tell you about the difference between a float and a numpy.float.

MWE:

from dolfin import *
import numpy


mesh = UnitSquareMesh(10, 10)
V = FunctionSpace(mesh, 'CG', 1)

u = Function(V)
uvec = u.vector()

a = 1.23
b = numpy.sin(1.23)

print(type(b))

print(type(a * uvec))
print(type(b * uvec))

Output:

<class 'numpy.float64'>
<class 'dolfin.cpp.la.GenericVector'>
<class 'numpy.ndarray'>

Comments (5)

  1. Log in to comment