Ñò
 pXJc           @   sÒ   d  Z  d d k Td d k Z e i i d d ƒ d d k i i Z d d k i i	 Z	 e	 i
 Z d d k i i Z d „  Z d „  Z d „  Z d d k Z d d k Z d	 „  Z e d
 j o e d d d d ƒ n d S(   t   
iÿÿÿÿ(   t   *Ni    s   ..c      	   C   s   t  |  ƒ }  t  | ƒ } t |  ƒ } t | ƒ } t i |  i ƒ  } d } d t ƒ  } t i | d d g d t d d d d	 ƒ\ } } | | f S(
   Nt   PyArray_FLOATs‚  
            #line 37 "vq.py"
            // Use tensor notation.
            blitz::Array<%(type)s,2> dist_sq(Ncode_book[0],Nobs[0]);
                blitz::firstIndex i;
            blitz::secondIndex j;
            blitz::thirdIndex k;
            dist_sq = sum(pow2(obs(j,k) - code_book(i,k)),k);
            // Surely there is a better way to do this...
            PyArrayObject* py_code = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_LONG);
                blitz::Array<int,1> code((int*)(py_code->data),
                                     blitz::shape(Nobs[0]), blitz::neverDeleteData);
                code = minIndex(dist_sq(j,i),j);

                PyArrayObject* py_min_dist = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_FLOAT);
                blitz::Array<float,1> min_dist((float*)(py_min_dist->data),
                                               blitz::shape(Nobs[0]), blitz::neverDeleteData);
                min_dist = sqrt(min(dist_sq(j,i),j));
                py::tuple results(2);
                results[0] = py_code;
                results[1] = py_min_dist;
                return_val = results;
            t   obst	   code_bookt   type_converterst   compilert   gcct   verbosei   (	   t   asarrayt   shapet   c_spect   num_to_c_typest   typecodet   localst   inline_toolst   inlinet   blitz_type_converters(   R   R   t   obs_sht   code_book_sht   typet   ar_typet   codet
   distortion(    (    sG   C:\graphics\Tools\Python26\Lib\site-packages\scipy\weave\examples\vq.pyt   vq   s    c      	   C   s   t  |  ƒ }  t  | ƒ } t |  ƒ } t | ƒ } t i |  i ƒ  } d } d t ƒ  } t i | d d g d t d d d d	 ƒ\ } } | | f S(
   sd    doesn't use blitz (except in conversion)
        ALSO DOES NOT HANDLE STRIDED ARRAYS CORRECTLY
    R   s½  
            #line 83 "vq.py"
            // THIS DOES NOT HANDLE STRIDED ARRAYS CORRECTLY
            // Surely there is a better way to do this...
            PyArrayObject* py_code = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_LONG);
                PyArrayObject* py_min_dist = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_FLOAT);

            int* raw_code = (int*)(py_code->data);
            float* raw_min_dist = (float*)(py_min_dist->data);
            %(type)s* raw_obs = obs.data();
            %(type)s* raw_code_book = code_book.data();
            %(type)s* this_obs = NULL;
            %(type)s* this_code = NULL;
            int Nfeatures = Nobs[1];
            float diff,dist;
            for(int i=0; i < Nobs[0]; i++)
            {
                this_obs = &raw_obs[i*Nfeatures];
                raw_min_dist[i] = (%(type)s)10000000.; // big number
                for(int j=0; j < Ncode_book[0]; j++)
                {
                    this_code = &raw_code_book[j*Nfeatures];
                    dist = 0;
                    for(int k=0; k < Nfeatures; k++)
                    {
                        diff = this_obs[k] - this_code[k];
                        dist +=  diff*diff;
                    }
                    dist = dist;
                    if (dist < raw_min_dist[i])
                    {
                        raw_code[i] = j;
                        raw_min_dist[i] = dist;
                    }
                }
                raw_min_dist[i] = sqrt(raw_min_dist[i]);
                }
                py::tuple results(2);
                results[0] = py_code;
                results[1] = py_min_dist;
                return_val = results;
            R   R   R   R   R   R   i   (	   R	   R
   R   R   R   R   R   R   R   (   R   R   R   R   R   R   R   R   (    (    sG   C:\graphics\Tools\Python26\Lib\site-packages\scipy\weave\examples\vq.pyt   vq2B   s    *c         C   s„   t  |  ƒ }  t  | ƒ } t |  ƒ } t | ƒ } t i |  i ƒ  } d t ƒ  } d d k } t i | d d g ƒ \ } } | | f S(   sw    Uses standard array conversion completely bi-passing blitz.
        THIS DOES NOT HANDLE STRIDED ARRAYS CORRECTLY
    s  
            #line 139 "vq.py"
            // Surely there is a better way to do this...
            PyArrayObject* py_code = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_LONG);
                PyArrayObject* py_min_dist = (PyArrayObject*) PyArray_FromDims(1,&Nobs[0],PyArray_FLOAT);

            int* code_data = (int*)(py_code->data);
            float* min_dist_data = (float*)(py_min_dist->data);
            %(type)s* this_obs = NULL;
            %(type)s* this_code = NULL;
            int Nfeatures = Nobs[1];
            float diff,dist;

            for(int i=0; i < Nobs[0]; i++)
            {
                this_obs = &obs_data[i*Nfeatures];
                min_dist_data[i] = (float)10000000.; // big number
                for(int j=0; j < Ncode_book[0]; j++)
                {
                    this_code = &code_book_data[j*Nfeatures];
                    dist = 0;
                    for(int k=0; k < Nfeatures; k++)
                    {
                        diff = this_obs[k] - this_code[k];
                        dist +=  diff*diff;
                    }
                    if (dist < min_dist_data[i])
                    {
                        code_data[i] = j;
                        min_dist_data[i] = dist;
                    }
                }
                min_dist_data[i] = sqrt(min_dist_data[i]);
                }
                py::tuple results(2);
                results[0] = py_code;
                results[1] = py_min_dist;
                return_val = results;
            iÿÿÿÿNR   R   (	   R	   R
   R   R   R   R   t	   ext_toolsR   R   (   R   R   R   R   R   R   R   R   (    (    sG   C:\graphics\Tools\Python26\Lib\site-packages\scipy\weave\examples\vq.pyt   vq3ƒ   s    'c         C   sð  t  i d d | | f ƒ } t  i d d | | f ƒ } d d  k } | i i d | | | |  f GHt i ƒ  } x2 t |  ƒ D]$ } | i i i | | ƒ \ }	 }
 qz Wt i ƒ  } | | } d G| | |  GH|	 d  G|
 d  GHt i ƒ  } x2 t |  ƒ D]$ } | i i i | | ƒ \ }	 }
 qó Wt i ƒ  } d G| | |  GH|	 d  G|
 d  GHd | | | GHt | | ƒ } t i ƒ  } x) t |  ƒ D] } t | | ƒ \ }	 }
 q‚Wt i ƒ  } d	 G| | |  GH|	 d  G|
 d  GHd | | | GHt | | ƒ } t i ƒ  } x) t |  ƒ D] } t | | ƒ \ }	 }
 qWt i ƒ  } d
 G| | |  GH|	 d  G|
 d  GHd | | | GHt	 | | ƒ } t i ƒ  } x) t |  ƒ D] } t	 | | ƒ \ }	 }
 qŽWt i ƒ  } d G| | |  GH|	 d  G|
 d  GHd | | | GHd  S(   Ng        g      ð?iÿÿÿÿsB   vq with %d observation, %d features and %d codes for %d iterationss    speed in python:i   s    speed in standard c:s    speed up: %3.2fs    speed inline/blitz:s    speed inline/blitz2:s    speed using C arrays:(
   t   RandomArrayt   normalt   scipy.cluster.vqt   clusterR   t   timet   ranget   py_vqR   R   (   t   mt   Nobst   Ncodest	   NfeaturesR   t   codest   scipyt   t1t   iR   t   distt   t2t   pyt   b(    (    sG   C:\graphics\Tools\Python26\Lib\site-packages\scipy\weave\examples\vq.pyt   compare¿   sb    
 "
 "   t   __main__id   iè  i   i
   (   t   __doc__t   numpyt   syst   patht   insertt   scipy.weave.inline_toolst   weaveR   t   scipy.weave.converterst
   converterst   blitzR   t   scipy.weave.c_specR   R   R   R   R    R   R/   t   __name__(    (    (    sG   C:\graphics\Tools\Python26\Lib\site-packages\scipy\weave\examples\vq.pyt   <module>   s   
		)	A	:	5