← Back to team overview

dolfin team mailing list archive

Re: Python and std::vector (again)

 


Johan Hake wrote:
> On Sunday 07 March 2010 12:51:20 Garth N. Wells wrote:
>> Johan Hake wrote:
>>> On Sunday 07 March 2010 06:07:58 Garth N. Wells wrote:
>>>> Anders Logg wrote:
>>>>> On Sun, Mar 07, 2010 at 12:11:53PM +0000, Garth N. Wells wrote:
>>>>>> I've added the function
>>>>>>
>>>>>>   GenericVector::sum(const std::vector<uint>& rows)
>>>>>>
>>>>>> which will sum the given row entries. But, frustratingly, it doesn't
>>>>>> work through Python. I can't pass a list or numpy array to it. I get
>>>>>> the error
>>>>>>
>>>>>> TypeError: in method 'GenericVector_sum', argument 2 of type
>>>>>> 'std::vector< dolfin::uint > const &'
>>>>>>
>>>>>> How can I pass a list of integers to this function from Python? It
>>>>>> seems that we haven't resolved passing std::vector from Python to C++
>>>>>> satisfactorily.
>>>>>>
>>>>>> Garth
>>>>> Why std::vector? I thought we discussed replacing it with
>>>>> dolfin::Array in the interface so that it would map easily to Python
>>>>> which would let us remove the std::vector typemaps.
>>>> dolfin::Array doesn't work either.
>>> This is because these typemaps needed to be instantiated per argument
>>> name. I have now added "greedy" (argument independent) typemaps for all
>>>
>>>    const Array<{uint,int,double}> & foo
>>>
>>> arguments, using the typemap macro. I guess _all_ const Array argument of
>>> the above primitive types can safely be passed using NumPy arrays. It
>>> should now just work for all argument names of the above types.
>>>
>>> We still cannot do this for all Array arguments, as some might be turned
>>> into an argout typemap, which needs to be treated independently. But
>>> again I do not have the whole overview of these cases (if any).
>>>
>>> It should now be possible to change all const
>>> std::vector<{uint,int,double}>& foo to Array, and the above mentioned
>>> typemap should kick in.
>> Can you give an example of how it should work? I've tried
>>
>>   tmp0 = numpy.array( [0, 1, 2], dtype=numpy.uint32)
>>   tmp1 = numpy.array( [0, 1, 2])
>>   tmp2 = [0, 1, 2]
>>   sum  = u.vector().sum(tmp0)
>>
>> (with tmp0, tmp1 and tmp2), but in all cases I get:
>>
>> TypeError: in method 'GenericVector_sum', argument 2 of type
>> 'dolfin::Array< dolfin::uint > const &'
> 
> Have you tried dtype="I"?
>

Yes, but I screwed up some merging, so the changes didn't get through.
I'll try again.

Garth

> It work for me.
> 
> Johan



References