Exemplo n.º 1
0
int
reader_read_double_data_file(const char *path,
                             struct matrix *x,
                             struct matrix *d)
{
    FILE *fd;

    matrix_initialize(x, 0, 0);
    matrix_initialize(d, 0, 0);

    if ((fd = fopen(path, "r")))
    {
        size_t length;
        int c = 1;
        char *line = NULL;

        while (1)
        {
            if (getline(&line, &length, fd) == -1)
                break;

            if (c == 1)
                reader_extract_numbers(x, line);
            else
                reader_extract_numbers(d, line);

            c *= -1;
        }

        return (c != 1) ? 0 : 1;
    }

    return 0;
}
Exemplo n.º 2
0
Arquivo: som.c Projeto: dsimba/clann
void
som_initialize(struct som *ann,
               som_grid_type grid_type,
               clann_size_type input_size,
               clann_size_type width)
{
    ann->input_size = input_size;
    ann->grid.width = width;
    ann->learning_rate = 0.1;
    ann->const_1 = 1000 / CLANN_LOG(ann->grid.width);
    ann->const_2 = 1000;
    ann->step = 1;
    ann->epoch = 1;
    ann->grid.type = grid_type;

    switch (ann->grid.type)
    {
        case SOM_GRID_LINE:
            ann->grid.dimension = SOM_GRID_1D;
            break;
        case SOM_GRID_SQUARE:
            ann->grid.dimension = SOM_GRID_2D;
            break;
        case SOM_GRID_CUBE:
            ann->grid.dimension = SOM_GRID_3D;
    }

    /*
     * Compute the number of neurons and initialize weights
     */
    ann->grid.n_neurons = CLANN_POW(ann->grid.width, ann->grid.dimension);

    matrix_initialize(&ann->grid.indexes,
                      ann->grid.n_neurons,
                      ann->grid.dimension);

    matrix_initialize(&ann->grid.weights,
                      ann->grid.n_neurons,
                      ann->input_size);

    matrix_fill_rand(&ann->grid.weights, -1, 1);

    /*
     * Generate the indexes
     */
    clann_size_type count = 0;
    clann_real_type *buffer;
    
    buffer = malloc(sizeof(clann_real_type) * ann->grid.dimension);

    som_grid_indexes(ann, 0, buffer, &count);

    free(buffer);
}
Exemplo n.º 3
0
void
kmeans_initialize(struct kmeans *ann,
                  clann_size_type n_centers,
                  clann_size_type center_size,
                  clann_real_type noticeable_change_rate)
{
    ann->n_centers = n_centers;
    ann->center_size = center_size;
    ann->noticeable_change_rate = noticeable_change_rate;
    ann->old_centers = NULL;

    matrix_initialize(&ann->centers, ann->n_centers, ann->center_size);
    matrix_fill_rand(&ann->centers, -1, 1);
}
Exemplo n.º 4
0
/*********************************
 *             Test
 ********************************/
int main(){
	printf("-----------------------------\n");
	Matrix* m1 = matrix_Alloc(3,2);
	Matrix* m2 = matrix_Alloc(2,4);
	matrix_initialize(m1, 2.0);
	matrix_initialize(m2, 3.0);

	matrix_print(m1);
	matrix_print(m2);
	//matrix_save(m2, "tests/m3.mat");
	//Matrix* res = matrix_kmult(m1,m2);
	/*Matrix* res1 = matrix_load( (char *)"tests/m1.mat");
	Matrix* res2 = matrix_load( (char *)"tests/m2.mat");
	matrix_print(res2);
	Matrix* res = matrix_transpose(res2);
	matrix_print(res);*/

	free(m1);
	free(m2);

	//main2();
	return 0;
}
Exemplo n.º 5
0
int
reader_read_data_file(const char *path,
                      struct matrix *v)
{
    FILE *fd;

    matrix_initialize(v, 0, 0);

    if ((fd = fopen(path, "r")))
    {
        char *line = NULL;
        size_t len;

        while (getline(&line, &len, fd) != -1)
            reader_extract_numbers(v, line);

        return 1;
    }

    return 0;
}
Exemplo n.º 6
0
Arquivo: keyboard.c Projeto: mxkl/gh60
/** Configures the board hardware and chip peripherals for the demo's functionality. */
void initialize_hardware()
{
#if (ARCH == ARCH_AVR8)
  /* Disable watchdog if enabled by bootloader/fuses */
  MCUSR &= ~(1 << WDRF);
  wdt_disable();

  /* Disable clock division */
  clock_prescale_set(clock_div_1);
#elif (ARCH == ARCH_XMEGA)
  /* Start the PLL to multiply the 2MHz RC oscillator to 32MHz and switch the CPU core to run from it */
  XMEGACLK_StartPLL(CLOCK_SRC_INT_RC2MHZ, 2000000, F_CPU);
  XMEGACLK_SetCPUClockSource(CLOCK_SRC_PLL);

  /* Start the 32MHz internal RC oscillator and start the DFLL to increase it to 48MHz using the USB SOF as a reference */
  XMEGACLK_StartInternalOscillator(CLOCK_SRC_INT_RC32MHZ);
  XMEGACLK_StartDFLL(CLOCK_SRC_INT_RC32MHZ, DFLL_REF_INT_USBSOF, F_USB);

  PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
#endif

  matrix_initialize();
  USB_Init();
}
Exemplo n.º 7
0
int main( int argc , char *argv[] )
{
   MRI_IMAGE *inim , *outim ; float *iv ;
   int iarg , ii,jj , nreg , ntime , *tau=NULL , rnum ;
   NI_element *nelmat=NULL ; char *matname=NULL ;
   MTYPE rhomax=0.7 , bmax=0.7 ; int rhonum=7 , bnum=14 ;
   char *cgl , *rst ;
   matrix X ; vector y ;
   float cput ;
   reml_collection *rrcol ;

   if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
     printf(
      "Usage: 1dREMLfit [option] file.1D\n"
      "Least squares fit with REML estimation of the ARMA(1,1) noise.\n"
      "\n"
      "Options (the first one is mandatory)\n"
      "------------------------------------\n"
      " -matrix mmm = Read the matrix 'mmm', which should have been\n"
      "                 output from 3dDeconvolve via the '-x1D' option.\n"
      " -MAXrho rm  = Set the max allowed rho parameter to 'rm' (default=0.7).\n"
      " -Nrho nr    = Use 'nr' values for the rho parameter (default=7).\n"
      " -MAXb bm    = Set max allow MA b parameter to 'bm' (default=0.7).\n"
      " -Nb nb      = Use 'nb' values for the b parameter (default=7).\n"
     ) ;
      PRINT_COMPILE_DATE ; exit(0) ;
   }

   iarg = 1 ;
   while( iarg < argc && argv[iarg][0] == '-' ){

      /** rho and del params **/

      if( strcmp(argv[iarg],"-MAXrho") == 0 ){
        rhomax = (MTYPE)strtod(argv[++iarg],NULL) ;
             if( rhomax < 0.3 ) rhomax = 0.3 ;
        else if( rhomax > 0.9 ) rhomax = 0.9 ;
        iarg++ ; continue ;
      }
      if( strcmp(argv[iarg],"-Nrho") == 0 ){
        rhonum = (int)strtod(argv[++iarg],NULL) ;
             if( rhonum <  2 ) rhonum =  2 ;
        else if( rhonum > 20 ) rhonum = 20 ;
        iarg++ ; continue ;
      }
      if( strcmp(argv[iarg],"-MAXb") == 0 ){
        bmax = (MTYPE)strtod(argv[++iarg],NULL) ;
             if( bmax < 0.3 ) bmax = 0.3 ;
        else if( bmax > 0.9 ) bmax = 0.9 ;
        iarg++ ; continue ;
      }
      if( strcmp(argv[iarg],"-Nb") == 0 ){
        bnum = (int)strtod(argv[++iarg],NULL) ;
             if( bnum <  2 ) bnum =  2 ;
        else if( bnum > 20 ) bnum = 20 ;
        iarg++ ; continue ;
      }

      /** -matrix **/

      if( strcmp(argv[iarg],"-matrix") == 0 ){
        if( nelmat != NULL ) ERROR_exit("More than 1 -matrix option!");
        nelmat = NI_read_element_fromfile( argv[++iarg] ) ; /* read NIML file */
        matname = argv[iarg];
        if( nelmat == NULL ){                     /* try to read as a 1D file */
          MRI_IMAGE *nim ; float *nar ;
          nim = mri_read_1D(argv[iarg]) ;
          if( nim != NULL ){              /* construct a minimal NIML element */
            nelmat = NI_new_data_element( "matrix" , nim->nx ) ;
            nar    = MRI_FLOAT_PTR(nim) ;
            for( jj=0 ; jj < nim->ny ; jj++ )
              NI_add_column( nelmat , NI_FLOAT , nar + nim->nx*jj ) ;
            mri_free(nim) ;
          }
        }
        if( nelmat == NULL || nelmat->type != NI_ELEMENT_TYPE )
          ERROR_exit("Can't process -matrix file!");
        iarg++ ; continue ;
      }

     ERROR_exit("Unknown option '%s'",argv[iarg]) ;
   }

   if( iarg >= argc ) ERROR_exit("No 1D file on command line?!") ;

   inim = mri_read_1D( argv[iarg] ) ;
   if( inim == NULL ) ERROR_exit("Can't read 1D file %s",argv[iarg]) ;

   nreg  = nelmat->vec_num ;
   ntime = nelmat->vec_len ;
   if( ntime != inim->nx )
     ERROR_exit("matrix vectors are %d long but input 1D file is %d long",
                ntime,inim->nx) ;

   cgl = NI_get_attribute( nelmat , "GoodList" ) ;
   if( cgl != NULL ){
     int Ngoodlist,*goodlist , Nruns,*runs ;
     NI_int_array *giar ;
     giar = NI_decode_int_list( cgl , ";," ) ;
     if( giar == NULL || giar->num < ntime )
       ERROR_exit("-matrix 'GoodList' badly formatted?") ;
     Ngoodlist = giar->num ; goodlist = giar->ar ;
     rst = NI_get_attribute( nelmat , "RunStart" ) ;
     if( rst != NULL ){
       NI_int_array *riar = NI_decode_int_list( rst , ";,") ;
       if( riar == NULL ) ERROR_exit("-matrix 'RunStart' badly formatted?") ;
       Nruns = riar->num ; runs = riar->ar ;
     } else {
       Nruns = 1 ; runs = calloc(sizeof(int),1) ;
     }
     rnum = 0 ; tau = (int *)malloc(sizeof(int)*ntime) ;
     for( ii=0 ; ii < ntime ; ii++ ){
       jj = goodlist[ii] ;
       for( ; rnum+1 < Nruns && jj >= runs[rnum+1] ; rnum++ ) ; /*nada*/
       tau[ii] = jj + 10000*rnum ;
     }
   }

   matrix_initialize( &X ) ;
   matrix_create( ntime , nreg , &X ) ;
   if( nelmat->vec_typ[0] == NI_FLOAT ){
     float *cd ;
     for( jj=0 ; jj < nreg ; jj++ ){
       cd = (float *)nelmat->vec[jj] ;
       for( ii=0 ; ii < ntime ; ii++ ) X.elts[ii][jj] = (MTYPE)cd[ii] ;
     }
   } else if( nelmat->vec_typ[0] == NI_DOUBLE ){
     double *cd ;
     for( jj=0 ; jj < nreg ; jj++ ){
       cd = (double *)nelmat->vec[jj] ;
       for( ii=0 ; ii < ntime ; ii++ ) X.elts[ii][jj] = (MTYPE)cd[ii] ;
     }
   } else {
     ERROR_exit("-matrix file stored will illegal data type!?") ;
   }

   cput = COX_cpu_time() ;
   rrcol = REML_setup( &X , tau , rhonum,rhomax,bnum,bmax ) ;
   if( rrcol == NULL ) ERROR_exit("REML setup fails?" ) ;
   cput = COX_cpu_time() - cput ;
   INFO_message("REML setup: rows=%d cols=%d %d cases CPU=%.2f",
                ntime,nreg,rrcol->nset,cput) ;

   cput = COX_cpu_time() ;
   vector_initialize( &y ) ; vector_create_noinit( ntime , &y ) ;
   for( jj=0 ; jj < inim->ny ; jj++ ){
     iv = MRI_FLOAT_PTR(inim) + ntime*jj ;
     for( ii=0 ; ii < ntime ; ii++ ) y.elts[ii] = (MTYPE)iv[ii] ;
     (void)REML_find_best_case( &y , rrcol ) ;
     INFO_message(
       "Vector #%d: best_rho=%.2f best_b=%.2f best_lam=%.2f best_ssq=%g  olsq_ssq=%g",
       jj, REML_best_rho, REML_best_bb, REML_best_lam, REML_best_ssq, REML_olsq_ssq ) ;
   }
   cput = COX_cpu_time() - cput ;
   INFO_message("REML fitting: CPU=%.2f",cput) ;
   exit(0) ;
}
Exemplo n.º 8
0
/*
 * Inversion d'une matrice
 * A Vérifier !
 */
Matrix* matrix_reverse(Matrix* m){
       // Méthode de Gauss

       // Allocation de la matrice resultat
     Matrix* res = matrix_Alloc(m->r, m->c);
     if(res == NULL) {
		printf("Error creating matrix !");
		return NULL;
      }
      
      //Initialise le resultat : res = Id
     matrix_initialize(res, 0);     
     for(int i = 0; i< res->c ; i++){
       res->matrix[i][i] = 1;
     }
              
     double coeff = 1;
     double coeff2 = 1;
     double aux1 = 0;
     double aux2 = 0;
     
     
     for(int i = 0 ; i< (m->r)-1 ; i++){
       
       // cas ou le pivot est nul, on permute 2 lignes
       if(m->matrix[i][i]==0){ 
           int h = 1;
           
           // Recherche d'un pivot non nulle, il existe car on suppose la matrice inversible
           while( i+h < m->r && m->matrix[i+h][i]==0){
              h++; 
           }
           if(i+h >= m->r) {
		printf("Matrice NON inversible - dansinversion matrice !");
		return NULL;
           }
           
           //Permutation des lignes
           for(int k = i ; k< m->c ; k++){
              aux1 = m->matrix[i][k];
              m->matrix[i][k] = m->matrix[i+h][k];
              m->matrix[i+h][k] = aux1;
              // Reapplique sur la matrice résultat
              aux2 = res->matrix[i][k];
              res->matrix[i][k] = res->matrix[i+h][k];
              res->matrix[i+h][k] = aux2;
           }
       }
       
       // Sans fraction
       for(int j = i+1 ; j< m->r; j++){// Pour chaque ligne
         coeff = m->matrix[j][i];
         //coeff2 = res->matrix[j][i];
         for(int k = 0 ; k < m->c; k++){ // On l'applique sur toute la ligne, On est obligé de commencer à 0 pour le resultat ...        
            m->matrix[j][k] = m->matrix[i][k] * m->matrix[j][k] - coeff * m->matrix[i][k];
            // Reapplique sur la matrice résultat            
            res->matrix[j][k] = m->matrix[i][k] * res->matrix[j][k] - coeff * res->matrix[i][k];
         }
       }   
     }
   
   
   
   //Maintenant on a une matrice triangulaire supérieur, il n'y a plus qu'à remonter
     for(int i = (m->r)-1 ; i > 1 ; i--){// On part de la fin       
       for(int j = i-1 ; j > 0; j--){// Pour chaque ligne en remontant
         m->matrix[j][i] = m->matrix[i][i] * m->matrix[j][i] - coeff * m->matrix[i][i];
         coeff = m->matrix[j][i];
         //coeff2 = res->matrix[j][i];
         for(int k = m->c-1 ; k >= 0; k--){ // On l'applique sur toute la ligne          
            res->matrix[j][k] = m->matrix[i][k] * res->matrix[j][k] - coeff * res->matrix[i][k];
         }
       }   
     }
   
   return res; 
}
Exemplo n.º 9
0
Arquivo: narx.c Projeto: dsimba/clann
int
narx_open(struct narx *ann,
          const char *file)
{
    FILE *fd;

    if ((fd = fopen(file, "r")))
    {
        char *line = NULL;
        size_t len;

        getline(&line, &len, fd);

        if (!strncmp(NARX_FILE_HEADER, line, (4 > len ? len: 4)))
        {
            struct matrix a, o, w, f;
            unsigned int i;
            int c = 1;

            matrix_initialize(&a, 0, 0);
            matrix_initialize(&o, 0, 0);
            matrix_initialize(&w, 0, 0);
            matrix_initialize(&f, 0, 0);

            getline(&line, &len, fd);
            reader_extract_numbers(&a, line);

            unsigned int arch[a.cols];
            for (i = 0; i < a.cols; i++)
                arch[i] = (unsigned int) a.values[i];

            getline(&line, &len, fd);
            reader_extract_numbers(&o, line);

            narx_initialize(ann, arch, a.cols, (unsigned int) o.values[1]);
            ann->ann.avarage_error = o.values[0];

            while (1)
            {
                if (getline(&line, &len, fd) == -1) break;

                if (c == 1)
                    reader_extract_numbers(&w, line);
                else
                    reader_extract_numbers(&f, line);

                c *= -1;
            }

            if (c == 1)
            {
                mlp_fill(&ann->ann, &w, &f);

                return 1;
            }

            printf("E. [NARX] Inconsistency in opened file.\n");
        }

        printf("E. [NARX] Opened file is not a NARX file.\n");
    }
    else
        printf("E. [NARX] Can not open file to read.\n");

    return 0;
}