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

Geometric Multiplicity: eignvectors (2)

If eigenvectors of a matrix A are independent, it is a happy property. Because the matrix A can be diagonalized with a matrix S that column vectors are eigenvectors of A . For example, Why this is a happy property of A? Because I can find A's power easily. A^{10} is not a big deal. Because Λ is a diagonal matrix and power of a diagonal matrix is quite simple. A^{10} = SΛ^{10} S^{-1} Then, why if I want to compute power of A ? That is the same reason to find eigenvectors. Eigenvectors are a basis of a matrix. A matrix can be represented by a single scalar. I repeat this again. This is the happy point, a matrix becomes a scalar. What can be simpler than a scalar value. But, this is only possible when the matrix S's columns are independent. Because S^{-1} must be exist. Now I come back to my first question. Is the λ's multiplicity related with the number of eigenvectors? This time I found this has the name. Geometric multiplicity (GM): the number of in...

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...

Tezuka Osamu's Black Jack, "Shrinking"

I like several novel authors. My first favorite author is probably Teduka, Osamu. I still love him. The list grows by adding Hoshi, Shinichi, Agatha Christie, Hermann Hesse, and so forth. My first favorite article of Tezuka was Atom as most of the (boy's) Tezuka fans did. But my favorite is Black Jack. I try to summarize one story, it is still quite vivid in my memory. I first read this story when I was 13 - 15 years old. I re-read it at least several times since Black Jack is composed of many short episodes. The title should be "ちぢむ (SHRINKING)" or it might be "縮む(Shrinking)". (It is not so convenient to translate this to English, since English does not have a system to say the exact same word in several ways. So I just simulate it with capital letters.) Black Jack is a genius surgeon, but he does not have the license. In short, his medical activity is illegal. His skill is top level in the world, but, the fee is also out-of-law expensive. In the story ...