
////////////////////////////////////////////////////////////////////////////

/////////////////////////////////////////

/*
sub Derivative1 {
    my ($x,$y)=@_;
    my @y2;
    my $n=$#{$x};
    $y2[0]=($y->[1]-$y->[0])/($x->[1]-$x->[0]);
    $y2[$n]=($y->[$n]-$y->[$n-1])/($x->[$n]-$x->[$n-1]);
    my $i;
    for($i=1; $i<$n; $i++) {
	$y2[$i]=($y->[$i+1]-$y->[$i-1])/($x->[$i+1]-$x->[$i-1]);
    }
    return @y2;
}







 //printf("AMEMORY0 %p\n",(void *)tablex->encabezados);
	    	    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	    /*	     if ( tablex->numeroslintot==0){
	        tablex->numeroslintot=1;
		  printf("AM M\n");
	        if ((tablex->encabezados = (char **) malloc((unsigned int) tablex->numeroslintot*sizeof(char*))) == NULL){  // strings con los encabezados de la tabla
                       fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                       exit(OOM);
                   }
             }else   if (tablex->znumeroslin+1> tablex->numeroslintot){
		         printf("AM R\n");
       	       tablex->numeroslintot= 2 * (tablex->znumeroslin+1);
	       if ((tablex->encabezados = (char **) realloc(tablex->encabezados, tablex->numeroslintot * sizeof(char*)))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
		     }else{

  printf("AM 0\n");
		     }
	    */ 
	    //printf("AMEMORY %d %d %p\n",tablex->numeroslintot,tablex->znumeroslin,(void *) tablex->encabezados);
	    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////




 //printf("-----REALLOC ----------- encabe %d %d\n",  tablex->znumeroslin, numeroslintot);
    //tmpe = malloc((numeroslintot +1) * sizeof(char*));
	      //printf(" NLOK %d \n",tablex->znumeroslin);
	      //for(int i = 0; i < tablex->znumeroslin; i++) {
	      //	  printf("%d: %s - %d \n",i,tablex->encabezados[i],tablex->znumeroslin);
	      //	  tmpe[i]=tablex->encabezados[i];
              //    }
	      //tablex->encabezados=tmpe;
	      //usar memcpy


     /*    
   if (sizer==0){
      sizer=3;
     if ((string = (char*) malloc(sizer*sizeof(char)))==NULL){ 
        fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
        exit(OOM);
        }
      *string = '\0';
    }else if (sizem+1>sizer){
	sizer=2*sizer+1;//sizer * sizeof(char); sizer++;
	//sizer++;
	//size_t mem;
	//mem=malloc_usable_size (string);
	//"REALLOC %lu >%s< %lu \n"
	//printf("REALLOC %d >%s< %d \n",sizer * sizeof(char),string,mem);
	//printf("REALLOC %d >%s< \n",sizer * sizeof(char),string);

	
             
        if ((string = (char *) realloc(string, sizer * sizeof(char)))==NULL){
            fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
            exit(OOM);
            }
   


      
	//tmp = malloc(sizer * sizeof(char));
      //        if (!string) {
      //        free(string);
      //        string = NULL;
      //          exit(-12); 
      //    }
	//strcpy(tmp,string);
	//free(string);  
        //string=tmp;
	
      }
      */




	         //double *tmpd;
             //int *tmpi;
	     //tmpd = malloc( sizeof(double)*tablex->datostam);
	     //tmpi = malloc( sizeof(int)*tablex->datostam);
	     //   for(int i = 0; i < tablex->numdatos; i++) {
	     //	  printf("PRE %d, %f: %d \n",i,tablex->datos[i],tablex->lineadatos[i]);
             //		  tmpd[i]=tablex->datos[i];
             //		  tmpi[i]=tablex->lineadatos[i];
             //     }
	     // tablex->datos=tmpd;
	     // tablex->lineadatos=tmpi;

	  //tablex->datos = malloc(tablex->numeroslinok * sizeof(double)*tablex->datostam); //lineas
	  //tablex->lineadatos = malloc(tablex->numeroslinok * sizeof(int)*tablex->datostam); //lineas
	  //}


	//if (lineas==1 && tablex->znumeroslin==0){
        //printf("DATOS PREREALLOC %d - %d\n",tablex->datostam,tablex->numdatos);





	////////////////////////////////////////////////////////////
	
	/*	if (tablex->datostam==0){
	  tablex->datostam=5;
          if ((tablex->datos=(double *) malloc( sizeof(double) *tablex->datostam))==NULL){        // lista con los datos de TODA LA TABLA
               fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
               exit(OOM);
                }

              
          if ((tablex->lineadatos= (int *) malloc( sizeof(int) *tablex->datostam))==NULL){       // indica la linea de cada dato
               fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
               exit(OOM);
                }
	
	}else	if (tablex->datostam<tablex->numdatos+1){
	     //printf("DATOS REALLOC %d - %d\n",tablex->datostam,tablex->numdatos);
 	     tablex->datostam=2*tablex->numdatos+1;

	    
	     if ((tablex->datos = (double *) realloc(tablex->datos, sizeof(double)*tablex->datostam))==NULL){
               fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
               exit(OOM);
               }
	     if ((tablex->lineadatos = (int *) realloc(tablex->lineadatos, sizeof(int)*tablex->datostam))==NULL){
	       fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
               exit(OOM);
               } 
    
	  }	
	*/

	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////


	
	/*	double JJJA= (y1[i+1]-y3[i])/(x[i+1]-x[i]);
	double JJJB= - (y3[i]-y3[i-1])/(x[i]-x[i-1]);

	double j1= (JJJA+JJJB)/   (x[i+1]-x[i-1]);

	
//double j1=( (y1[i+1]-y3[i])/(x[i+1]-x[i])- (y3[i]-y3[i-1])/(x[i]-x[i-1]))/	(x[i+1]-x[i-1]);

        double j2=-sig*u[i-1];

        double jj= 6.0*j1+j2;
	//printf("HHH %g %g %g %g %g  %g\n",j1,j2,jj,JJJA,JJJB,x[i-1]);
        u[i]=jj  /p;
	*/

	

	//	printf("CBB %g\n",u[i-1]);  
        // printf("MALLLLLLLL3  %g %g\n",y3[0],x[0]);
	// printf("CBB %d  %g %g %g \n",i,x[i-1],y3[i-1],u[i-1]);  
	// printf("CBB %d  %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %d\n",i,u[i],y1[i+1],y3[i],p,(x[i+1]-x[i]),(x[i]-x[i-1]),(x[i+1]-x[i-1]),sig*u[i-1],((y1[i+1]-y3[i])/(x[i+1]-x[i])- (y3[i]-y3[i-1])/(x[i]-x[i-1]))/(x[i+1]-x[i-1]), sig, u[i-1], y3[i]-y3[i-1], y1[i+1]-y3[i],x[i-1],y3[i-1],x[0],y3[0],i-1);  
     




 /*  if(tablex->numviewsavailable==0){
      tablex->numviewsavailable=1;
      if ((tablex->pointers= (struct view **) malloc(tablex->numviewsavailable*sizeof(struct view)))==NULL){ // strings con los encabezados de la tabla
        fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
        exit(OOM);
        }

    }else  if (  tablex->numviewsavailable+1<tablex->numviews){
	tablex->numviewsavailable=2*tablex->numviewsavailable+1;	
        if (( tablex->pointers = (struct view **) realloc(tablex->pointers, tablex->numviewsavailable * sizeof(struct view *)))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }    
      }
    */




/*struct table{
   char **encabezados;               //espacio numeroslintot
   double *datos;                   //espacio datostam
   int *lineadatos;                 //cada dato tiene su columna tambien datostam
   unsigned int numdatos;
   unsigned int numeroslinok; //available,  numeroslin es lo cargado en cada linea
//parece estar mal no es solo lo available sino lo que esta en el encabezado
   const char *file;   //redundante
   struct view  **pointers;
   //struct view  *pointers[1000];
   unsigned int numviews;  //occupy      //todo lo de malloc a unsigned, elimino error
   unsigned int numviewsavailable; 
};
*/

	//		if  (lineas==0 && tablex->znumeroslin>tablex->numeroslinok){
       	//          tablex->numeroslinok= tablex->znumeroslin;
	//          }
		/*if (lineas==0 && tablex->numeroslinok>numeroslintot){

	      printf("---------------- encabe %d %d\n",  tablex->numeroslinok, numeroslintot);

       	      numeroslintot= 2 * tablex->numeroslinok;
	      //tablex->encabezados = realloc(tablex->encabezados, tablex->numeroslinok * sizeof(char*)+1);
	      char **tmpe;
	      tmpe = malloc(numeroslintot * sizeof(char*)+1);
	      for(int i = 0; i < tablex->numeroslinok+1; i++) {
		  //printf("%d: %s \n",i,tablex->encabezados[i]);
	      	  tmpe[i]=tablex->encabezados[i];
                  }
	      tablex->encabezados=tmpe;
	      //usar memcpy
	      
	      }*/



	  
	  /*	  if (lineas==0){
	    tablex->lineasavailable=1;
            if ((tablex-> columnasporlinea= (unsigned int *) malloc(sizeof(unsigned int)* tablex->lineasavailable))==NULL){
               fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM); //cambiar error
               exit(OOM);
               }
	      if ((tablex-> terminalinea= (unsigned int *) malloc( sizeof(unsigned int)* tablex->lineasavailable))==NULL){
               fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM); //cambiar error
               exit(OOM);
               }
	  }else if (tablex->totallineas+1>tablex->lineasavailable){

 	     tablex->lineasavailable=2*tablex->totallineas+1;

	    
	     if ((tablex-> columnasporlinea= (unsigned int *) realloc(tablex-> columnasporlinea, sizeof(unsigned int)* tablex->lineasavailable))==NULL){
               fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM); //cambiar error
               exit(OOM);
               }
	     	    
	     if ((tablex-> terminalinea= (unsigned int *) realloc(tablex-> terminalinea, sizeof(unsigned int)* tablex->lineasavailable))==NULL){
               fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM); //cambiar error
               exit(OOM);
               }
	  }

	  */

 	



  // if (xlist->occupy>0){
  //	size_t mem=malloc_usable_size (xlist->names);
  //      printf("CANTS %d %p %d\n",xlist->occupy,xlist->names,mem);
  //}


 //  printf("CANT %d \n",xlist->occupy);
  /*
   if (xlist->occupy==0){    //poner este if antes del for anterior?
     if ((xlist->names = (const char **) malloc(xlist->available * sizeof(char *)))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }


    
    if ((xlist->dpointers=(struct table**) malloc(xlist->available * sizeof(struct table *)))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }

	//size_t mem=malloc_usable_size (xlist->names);
        //printf("CANT %d %p %d\n",xlist->occupy,xlist->names,mem);
  }else if (xlist->occupy+1>xlist->available){
        // ojo con el +1, si no el  xlist->names[xlist->occupy]=token; destruye la asignacion de memoria 
        //size_t mem=malloc_usable_size (xlist->names);	 
        //  printf("CANTR  %d %p %d %d %d\n",xlist->occupy, xlist->names,mem,xlist->available,xlist->available*2+1);
	xlist->available=2*xlist->available+1;
       

	//"REALLOC %lu >%s< %lu \n"
	//printf("REALLOC LISTATABLAS  %d \n",xlist->available * sizeof(char * ));
	//printf("REALLOC LISTATABLAS  %d >%s< %d \n",available * sizeof(char * ),names,mem);
	//char **tmp;
	//struct table **tmpp;
	
        if ((xlist->names  = (const char **) realloc(xlist->names, xlist->available * sizeof(char *)))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
        if ((xlist->dpointers = (struct table **) realloc(xlist->dpointers, xlist->available * sizeof(struct table *)))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
	//tmp =  malloc(xlist->available * sizeof(char *));

	*/
        /*if (!xlist->names) {
   	        printf("ERROR REALLOC list\n");
                free(xlist->names);
                xlist->names = NULL;
		exit(-2); //manejo de errores VERRR pq no seguir con memoria anterior
                }
         if (!xlist->dpointers) {
   	        printf("ERROR REALLOC list po\n");
                free(xlist->dpointers);
                xlist->dpointers = NULL;
		exit(-3); //manejo de errores VERRR pq no seguir con memoria anterior
                }
	*/
	//strcpy(tmp,token);
	//free(string);
	// aca debiera ir un ciclo for en el caso malloc, para copiar contenidos del viejo
        //xlist->names=tmp;
        //xlist->dpointers=tmpp;
  //}


  /*
  if (( vista->dy2=(double *) malloc( sizeof(double) * (n+2) ))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }  
  */
  //int comienza=0;



       /* if ((xlist->dpointers[xlist->occupy]=(struct table *) malloc(sizeof(struct table)))==NULL){
	       fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
               exit(OOM);
               } 
       */




   
	  /*

  */
  
  
  //    if (!defined $yp1) {

   //double qn=0;
   //double un=0;
   //    if (!defined $ypn) {
   //	$qn=0;
   //	$un=0;
   //    }  else {
   //	$qn=0.5;
   //	$un=(3.0/($x->[$n]-$x->[$n-1]))*
   //	    ($ypn-($y->[$n]-$y->[$n-1])/($x->[$n]-$x->[$n-1]));
   //  }
   //dy2[n]=(un-qn*u[n-1])/(qn*y3[n-1]+1.0);
	
//    }
//    else {
//	y2[0]=-0.5;
//	u[0]=(3/(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0])-$yp1);
	//

	//}



  /*  if ((u = (double *) malloc( sizeof(double) * (n+2) ))==NULL){
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
  */



  /*
  if ((vista=malloc(sizeof(struct view )+1))==NULL){                                               //no realloc
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
  */
  //vista=&dvista;
  //////////////////////////////////////////////////////////////
  //*************************************************** aqui remalloc  tablex->pointers[tablex->numviews]



   /*
  if ((tablacalcy1 = (double *)  malloc( sizeof(double) * (tablex->numdatos+1)))==NULL){ //excesivo!!!!
                fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
  double *tablacalcy2;
    if ((tablacalcy2 = (double *) malloc( sizeof(double) * (tablex->numdatos+1)))==NULL){ //excesivo!!!!
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
  double *tablacalcy3;
      if ((tablacalcy3 = (double *) malloc( sizeof(double) * (tablex->numdatos+1)))==NULL){ //excesivo!!!!
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }
   

  int *tablacalcmeta;
	if ((tablacalcmeta = (int *) malloc( sizeof(int) * (tablex->numdatos+1)))==NULL){ //excesivo!!!!
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }   
   */



   /*  if ((tablacalcx = (double *) malloc( sizeof(double) * (tablex->numdatos +1)))==NULL){ //excesivo!!!!
                 fprintf(stderr,"OOM at \"%s\": %d exit code %d\n", __FILE__, __LINE__,OOM);
                 exit(OOM);
                 }   
   */

   
  //  char * hh;
  //hh=(char *) malloc(500 * sizeof(char *));
  //strcat(hh,x);
  //strcat(hh,y);
  //strcat(hh,file);



   //      if (size <= index) {
      //      size += sizeof(char);
      //       tmp = realloc(line, size);
      //      if (!tmp) {
      //          free(line);
      //          line = NULL;
      //           break;
      //      }
      //      line = tmp;
      //  }



      
    
      // line[index++] = ch;


    //  for(int i = 0; i < tablex->numeroslinok+1; i++) {
    //		      printf("%d: %s - %d \n",i,tablex->encabezados[i],tablex->numeroslinok+1);	      	 
    //              }
    


 /*
  double rr1= a*y3[klo] + b*y1[klo+1];
  double rr2= (a*a*a-a)*dy2[klo];
 
  double rr3=(b*b*b-b)*dy2[klo+1];
  double rr4=(h*h)/6.0;
*/

  
  /*    for(unsigned int i = 0; i < vista->size+1; i++) {
       printf("DD %d  %g %g \n",i,x[i],dy2[i]);      
     }
  */

  
  // printf("RRR %g %g %g %g %g %g %g %g %g %g %g %d \n",rr1,rr2,rr3,rr4,a,b,h,y3[klo],y1[klo+1],dy2[klo],dy2[klo+1],klo);


  // *result=rr1+(rr2+rr3)/rr4;



 /*
  get_ini_tablelist();
  
  
  //int errorrs=
  int error2=create_search_table((const char *)file,  &num_table);  //ponerla comno const en todos lados
  if (error2<0){
       return error2;
       }
  
  

  int error=create_search_view(num_table,(const char *)scx,(const char *)scy,&num_view);
    
   if (error<0){
    return error;
  }
  */
  
  //double *x=vista->cx; //poner esto adentro de evaluate

  //double *y1=vista->cy1;
  //double *y2=vista->cy2;
  //double *y3=vista->cy3;

  //int *meta=vista->meta;
  //int N=vista->size;
  



  //  struct dynlist tablelist;
  //tablelist.occupy=0;
  //tablelist.available=1;

  //struct dynlist *tablelist;

  //tablelist=




	//         for(int i = 0; i <  generictable->numeroslinok+1; i++) {
	//	      printf("%d: %s - %d \n",i,generictable->encabezados[i], generictable->numeroslinok+1);	      	 
        //          }
	
	//exit(0);
	//printf("IMPRIME TABLA\n");


*/
    //unsigned int i;  
    // for(i=0;i<max;i++){
    //  if (fn[i]){
    //	printf("%d:  %s\n",i,  fn[i]);
    //  }//else{
      //	printf("No %d \n",i );
      //}
    //}

  



/////////////////////////////////////////////


// FUNCION PARA SER LLAMADA DESDE FORTRAN, probablemente innecesaria, SACADA
//void tabulate_(int file, int x, int y, double vx, int v, double *result) /* equivalent: char string[]  */
  //  {
//     
//    //printf("I %d,%d,%d,%g,%d\n",file,x,y,vx,v);
//    int error2=interpolate_num(file,x,y,vx,v, result);
//    if (error2 <0){
//               printf("ERROR INTERPOLATE test.txt\n");
//	       exit(error2);
//               }
//   printf("---RESULTADO %d, %g\n",2,*result);
    // printf("%s\n", string);
   
// }


