Background: I have a pre-existing C code, that I have written, which works. The only problem with it is it is slow. I have decided to rewrite the two control functions from C to python. I have succeeded in getting most everything set up, but I am having trouble with, what seems to me, very simple things.
In my C code I declare all of my arrays, I do most everything in arrays, up front, and I have preserved that in my Python. I am using c_types and numpy.
Here are some of my array declarations:
Expand|Select|Wrap|Line Numbers
- pop_size = gens = ... = c_int
- rank = (c_int * 500)()
- ...
- p1_chromo = p2_chromo = ... = (c_float * 500)()
- population = []
Expand|Select|Wrap|Line Numbers
- for i in range(pop_size)
- population.append([])
- for j in range(n)
- population[i].append(0.0)
Then I go on down and I do various things to the population and all seems to work well. Then i need to pass PART of my 2D population array to a C function.
it looks like this:
Expand|Select|Wrap|Line Numbers
- encode(n,population[x1], population[x2], p1_chromo, p2_chromo)
Expand|Select|Wrap|Line Numbers
- void encode(int n, float pop1[], float pop2[], float p1_chromo[], float p2_chromo[])
I understand what the error is telling me. I am trying to put a list into an array of floats. I over came this problem in previous function calls, when I was passing 1D arrays, by using the byref(...) tag, flag or whatever it is called.
I cannot understand a good way to create indexible arrays in python and then pass them to ANSI C funtions to be worked on. Thats my question... A mountain of text for that little line. But I wanted to show what I was doing and where I was coming from. I have read most of numpy's documentation, and pretty much all of the forum posts I could find... Any suggestions would be greatly appreciated... THANKS!!!