Skip to main content

Passing a user defined python object to C++ code(4)


How to access to self.face_idx_list. extracting a numpy.int64 type failed.

The difference between this article and the last one is numpy.array contains numpy.int64 instead of numpy.float64. We can access numpy.float64 value as the following:

vec[i] = boost::python::extract< float >(    float32_3_obj.attr("__getitem__")(i));

I.e., we can convert numpy.float64 value to float value by boost::python::extract< float >(f). However, when we try to convert numpy.int64 to int as the following code, it fails.

vec[i] = boost::python::extract< int >(
    int32_3_obj.attr("__getitem__")(i));

We can not convert numpy.int64 by boost::python::extract< int >(i). We will get the following error:
TypeError: No registered converter was able to produce a C++ rvalue of type int from this Python object of type numpy.int64.
That's strange. float is OK, but int isn't.  I looked up the web, there are some articles regarding with this issue. http://boost.2283326.n4.nabble.com/extracting-a-numpy-int32-type-td2701573.html I understand as that: numpy registered a conversion function from numpy.float64 to float, but, there is no conversion function registered numpy.int64 in 64bit machine. There was a suggestion to use python function int() to convert the numpy.int64 values. This works in the python world, but not in the C++ world.

I suggest the following code:
int vec[] = {0, 0, 0};
for(int i = 0; i < 3; ++i){
    object int64_obj = int32_3_obj.attr("__getitem__")(i);
    vec[i] = boost::python::extract< int >(
        int64_obj.attr("__int__")());
}
This code first gets a numpy.int64 object as a python object, then uses numpy.int64's attribute method __int__ that converts numpy.int64 to int.

This gives you information about the conversion numpy.int64 to int.  But some of readers might wonder how to find this information. How to find this information seems more useful, actually it is easy, ask python.

The former article mentioned that all the python members are attributes, and the attributes can be seen themselves a sequence of the attributes using dir() function. Also we can use type() function to see the type. In this case, from the python interpreter, we can use type() to get the type information.

>>> import numpy>>> a = numpy.array([1,2,3])>>> type(a[0])
Now we see the numpy.array element is 'numpy.int64' instead of 'int'. This type object is again python object, we can use dir() to get the attribute information. Then you can find an attribute called __int__. Let's use that attribute and see the type of it:
>>> type(a[0].__int__())
Now we know this is the conversion function we are looking for Therefore, we can use this conversion attribute method to get the int object. When we have int object, then we can use extract< int > to get a C++ int.
This is the basic of how to access to a user defined python object from C++ code.

I hope this article is useful to you.

Comments

Popular posts from this blog

Why A^{T}A is invertible? (2) Linear Algebra

Why A^{T}A has the inverse Let me explain why A^{T}A has the inverse, if the columns of A are independent. First, if a matrix is n by n, and all the columns are independent, then this is a square full rank matrix. Therefore, there is the inverse. So, the problem is when A is a m by n, rectangle matrix.  Strang's explanation is based on null space. Null space and column space are the fundamental of the linear algebra. This explanation is simple and clear. However, when I was a University student, I did not recall the explanation of the null space in my linear algebra class. Maybe I was careless. I regret that... Explanation based on null space This explanation is based on Strang's book. Column space and null space are the main characters. Let's start with this explanation. Assume  x  where x is in the null space of A .  The matrices ( A^{T} A ) and A share the null space as the following: This means, if x is in the null space of A , x is also in the null spa

Gauss's quote for positive, negative, and imaginary number

Recently I watched the following great videos about imaginary numbers by Welch Labs. https://youtu.be/T647CGsuOVU?list=PLiaHhY2iBX9g6KIvZ_703G3KJXapKkNaF I like this article about naming of math by Kalid Azad. https://betterexplained.com/articles/learning-tip-idea-name/ Both articles mentioned about Gauss, who suggested to use other names of positive, negative, and imaginary numbers. Gauss wrote these names are wrong and that is one of the reason people didn't get why negative times negative is positive, or, pure positive imaginary times pure positive imaginary is negative real number. I made a few videos about explaining why -1 * -1 = +1, too. Explanation: why -1 * -1 = +1 by pattern https://youtu.be/uD7JRdAzKP8 Explanation: why -1 * -1 = +1 by climbing a mountain https://youtu.be/uD7JRdAzKP8 But actually Gauss's insight is much powerful. The original is in the Gauß, Werke, Bd. 2, S. 178 . Hätte man +1, -1, √-1) nicht positiv, negative, imaginäre (oder gar um

Why parallelogram area is |ad-bc|?

Here is my question. The area of parallelogram is the difference of these two rectangles (red rectangle - blue rectangle). This is not intuitive for me. If you also think it is not so intuitive, you might interested in my slides. I try to explain this for hight school students. Slides:  A bit intuitive (for me) explanation of area of parallelogram  (to my site, external link) .