#NEWLINE# def vq2( obs , code_book ) : #NEWLINE# #INDENT# " doesn't use blitz (except in conversion)\n        ALSO DOES NOT HANDLE STRIDED ARRAYS CORRECTLY\n    " #NEWLINE# obs = asarray ( obs ) #NEWLINE# code_book = asarray ( code_book ) #NEWLINE# obs_sh = shape ( obs ) #NEWLINE# code_book_sh = shape ( code_book ) #NEWLINE# assert ( ( len ( obs_sh ) == 2 ) and ( len ( code_book_sh ) == 2 ) ) #NEWLINE# assert ( obs_sh [ 1 ] == code_book_sh [ 1 ] ) #NEWLINE# assert ( obs . typecode ( ) == code_book . typecode ( ) ) #NEWLINE# type = c_spec . num_to_c_types [ obs . typecode ( ) ] #NEWLINE# ar_type = 'PyArray_FLOAT' #NEWLINE# code = ( '\n            #line 83 "vq.py"\n            // THIS DOES NOT HANDLE STRIDED ARRAYS CORRECTLY\n            // Surely there is a better way to do this...\n            PyArrayObject* py_code = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_LONG);\n                PyArrayObject* py_min_dist = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_FLOAT);\n\n            int* raw_code = (int*)(py_code->data);\n            float* raw_min_dist = (float*)(py_min_dist->data);\n            %(type)s* raw_obs = obs.data();\n            %(type)s* raw_code_book = code_book.data();\n            %(type)s* this_obs = NULL;\n            %(type)s* this_code = NULL;\n            int Nfeatures = Nobs[1];\n            float diff,dist;\n            for(int i=0; i < Nobs[0]; i++)\n            {\n                this_obs = &raw_obs[i*Nfeatures];\n                raw_min_dist[i] = (%(type)s)10000000.; // big number\n                for(int j=0; j < Ncode_book[0]; j++)\n                {\n                    this_code = &raw_code_book[j*Nfeatures];\n                    dist = 0;\n                    for(int k=0; k < Nfeatures; k++)\n                    {\n                        diff = this_obs[k] - this_code[k];\n                        dist +=  diff*diff;\n                    }\n                    dist = dist;\n                    if (dist < raw_min_dist[i])\n                    {\n                        raw_code[i] = j;\n                        raw_min_dist[i] = dist;\n                    }\n                }\n                raw_min_dist[i] = sqrt(raw_min_dist[i]);\n                }\n                py::tuple results(2);\n                results[0] = py_code;\n                results[1] = py_min_dist;\n                return_val = results;\n            ' % locals ( ) ) #NEWLINE# ( code , distortion ) = inline_tools . inline ( code , [ 'obs' , 'code_book' ] , type_converters = blitz_type_converters , compiler = 'gcc' , verbose = 1 ) #NEWLINE# return ( code , distortion )