Esempio n. 1
0
File: math.c Progetto: priom/CCodes
int main(int argc, char* argv[]) {
    // open file
    FILE *f = fopen(argv[1],"r");

    // Throw an error if file not found
    if (f == NULL) {
        printf("Sorry, file not found.\n");
    }

    // Take the first line which is the count of number of lines
    int n;
    fscanf(f, "%d", &n);

    if (n > 4097) {
        printf("Sorry, file too large, cannot be more than 4097 lines");
    }

    if (n < 2) {
        printf("Sorry, file too short, cannot be less than 2 lines");
    }

    // Store the values from file in array
    int values[n];
    int i;
    for (i = 0; i < n; i++) {
        fscanf(f, "%d", &values[i]);
    }

    // Sort the values in ascending order
    qsort(values, n, sizeof(int), comparator);

    // Calculate the mean
    printf ("Mean = %0.2f\n", meanFunc(values, n));

    // Calculate the median
    printf ("Median = %0.2f\n", medianFunc(values, n));

    // Calculate the mode
    printf("Mode = %0.2f\n", modeFunc(values, n));

    // Find & print min value
    printf ("Min = %0.2f\n", minFunc(values));

    // Find & print max value
    printf ("Max = %0.2f\n", maxFunc(values, n));

    return 0;
}
Esempio n. 2
0
/* @returns: 	 0 -> success
 * 				-1 -> inconsistent lower/upper specification
 *				-2 -> can't open pSampleInputFile
 *				-3 -> can't allocate enough memory
 *				-4 -> can't open pOutputParameters
 *				-5 -> can't open pInputParameters
 *				-6 -> can't open pScaledSampleOutputFile
 *				-7 -> cannot use read and store of parameters simultaneously
 *
 */
int SVM::scaleSamples(char *pSampleInputFile, char* pScaledSampleOutputFile, double dLowerBound, double dUpperBound, char* pInputParameters, char* pOutputParameters)
{
	char *line = NULL;
	int max_line_len = 1024;
	double lower=-1.0,upper=1.0,y_lower,y_upper;
	int y_scaling = 0;
	double *feature_max;
	double *feature_min;
	double y_max = -DBL_MAX;
	double y_min = DBL_MAX;
	int max_index;
	long int num_nonzeros = 0;
	int i,index;
	FILE *fp, *fp_restore = NULL;

	if(!(upper > lower) )
	{
		fprintf(stderr,"inconsistent lower/upper specification\n");
		return -1;
	}

	if(pInputParameters != NULL && pOutputParameters != NULL)
	{
		fprintf(stderr,"cannot use read and store of parameters simultaneously\n");
		return -7;
	}

	fp=fopen(pSampleInputFile,"r");

	if(fp==NULL)
	{
		fprintf(stderr,"can't open file %s\n", pSampleInputFile);
		return -2;
	}

	line = (char *) malloc(max_line_len*sizeof(char));

	/* assumption: min index of attributes is 1 */
	/* pass 1: find out max index of attributes */
	max_index = 0;

	if(pInputParameters != NULL)
	{
		int idx, c;

		fp_restore = fopen(pInputParameters,"r");
		if(fp_restore==NULL)
		{
			fprintf(stderr,"can't open file %s\n", pInputParameters);
			return -5;
		}

		c = fgetc(fp_restore);
		if(c == 'y')
		{
			line = this->read_line(fp_restore, 1024);
			line = this->read_line(fp_restore, 1024);
			line = this->read_line(fp_restore, 1024);
		}
		line = this->read_line(fp_restore, 1024);
		line = this->read_line(fp_restore, 1024);

		while(fscanf(fp_restore,"%d %*f %*f\n",&idx) == 1)
			max_index = max(idx,max_index);
		rewind(fp_restore);
	}

	while((line = this->read_line(fp, 1024))!=NULL)
	{
		char *p=line;

		SKIP_TARGET

		while(sscanf(p,"%d:%*f",&index)==1)
		{
			max_index = max(max_index, index);
			SKIP_ELEMENT
			num_nonzeros++;
		}
	}
	rewind(fp);

	feature_max = (double *)malloc((max_index+1)* sizeof(double));
	feature_min = (double *)malloc((max_index+1)* sizeof(double));

	if(feature_max == NULL || feature_min == NULL)
	{
		fprintf(stderr,"can't allocate enough memory\n");
		return -3;
	}

	for(i=0;i<=max_index;i++)
	{
		feature_max[i]=-DBL_MAX;
		feature_min[i]=DBL_MAX;
	}

	/* pass 2: find out min/max value */
	while((line = this->read_line(fp, 1024))!=NULL)
	{
		char *p=line;
		int next_index=1;
		double target;
		double value;

		sscanf(p,"%lf",&target);
		y_max = max(y_max,target);
		y_min = min(y_min,target);

		SKIP_TARGET

		while(sscanf(p,"%d:%lf",&index,&value)==2)
		{
			for(i=next_index;i<index;i++)
			{
				feature_max[i]=maxFunc(feature_max[i],0);
				feature_min[i]=minFunc(feature_min[i],0);
			}

			feature_max[index]=maxFunc(feature_max[index],value);
			feature_min[index]=minFunc(feature_min[index],value);

			SKIP_ELEMENT
			next_index=index+1;
		}

		for(i=next_index;i<=max_index;i++)
		{
			feature_max[i]=maxFunc(feature_max[i],0);
			feature_min[i]=minFunc(feature_min[i],0);
		}
	}

	rewind(fp);

	/* pass 2.5: save/restore feature_min/feature_max */

	if(pInputParameters != NULL)
	{
		/* fp_restore rewinded in finding max_index */
		int idx, c;
		double fmin, fmax;

		if((c = fgetc(fp_restore)) == 'y')
		{
			fscanf(fp_restore, "%lf %lf\n", &y_lower, &y_upper);
			fscanf(fp_restore, "%lf %lf\n", &y_min, &y_max);
			y_scaling = 1;
		}
		else
			ungetc(c, fp_restore);

		if (fgetc(fp_restore) == 'x') {
			fscanf(fp_restore, "%lf %lf\n", &lower, &upper);
			while(fscanf(fp_restore,"%d %lf %lf\n",&idx,&fmin,&fmax)==3)
			{
				if(idx<=max_index)
				{
					feature_min[idx] = fmin;
					feature_max[idx] = fmax;
				}
			}
		}
		fclose(fp_restore);
	}

	if(pOutputParameters != NULL)
	{
		FILE *fp_save = fopen(pOutputParameters,"w");
		if(fp_save==NULL)
		{
			fprintf(stderr,"can't open file %s\n", pOutputParameters);
			exit(1);
		}
		if(y_scaling)
		{
			fprintf(fp_save, "y\n");
			fprintf(fp_save, "%.16g %.16g\n", y_lower, y_upper);
			fprintf(fp_save, "%.16g %.16g\n", y_min, y_max);
		}
		fprintf(fp_save, "x\n");
		fprintf(fp_save, "%.16g %.16g\n", lower, upper);
		for(i=1;i<=max_index;i++)
		{
			if(feature_min[i]!=feature_max[i])
				fprintf(fp_save,"%d %.16g %.16g\n",i,feature_min[i],feature_max[i]);
		}
		fclose(fp_save);
	}

	/* pass 3: scale */
	FILE *fp_scaledOutput = fopen(pScaledSampleOutputFile,"w");
	if(fp_scaledOutput==NULL)
	{
		fprintf(stderr,"can't open file %s\n", pScaledSampleOutputFile);
		return -6;
	}

	while((line = this->read_line(fp,1024))!=NULL)
	{
		char *p=line;
		int next_index=1;
		double target;
		double value;

		sscanf(p,"%lf",&target);
		fprintf(fp_scaledOutput, "%s", this->output_target(target, y_scaling, y_lower, y_upper, y_min, y_max));

		SKIP_TARGET

		while(sscanf(p,"%d:%lf",&index,&value)==2)
		{
			for(i=next_index;i<index;i++)
				fprintf(fp_scaledOutput, "%s", this->output(i,0, feature_min, feature_max, lower, upper));

			fprintf(fp_scaledOutput, "%s", this->output(index, value, feature_min, feature_max, lower, upper));

			SKIP_ELEMENT
			next_index=index+1;
		}

		for(i=next_index;i<=max_index;i++)
			fprintf(fp_scaledOutput, "%s", this->output(i, 0, feature_min, feature_max, lower, upper));

		fprintf(fp_scaledOutput, "\n");
	}

	/*
	if (new_num_nonzeros > num_nonzeros)
		fprintf(stderr,
			"Warning: original #nonzeros %ld\n"
			"         new      #nonzeros %ld\n"
			"Use -l 0 if many original feature values are zeros\n",
			num_nonzeros, new_num_nonzeros);
			*/

	free(line);
	free(feature_max);
	free(feature_min);
	fclose(fp);
	fclose(fp_scaledOutput);
	return 0;
}
Esempio n. 3
0
int main(int argc, char* argv[]) {
  options opts;
  { int ret = opts.parse( argc, argv );
    if ( ret >= 0 )
      return ret;
  }

  if ( opts.map.count("input-file") == 0 ) {
    opts.printUsage(std::cerr << "error:  missing input file\n\n") << std::endl;
    return EXIT_FAILURE;
  }

  xylose::pthreadCache.set_max_threads(
    opts.map["num-threads"].as<unsigned int>()
  );

  const std::string input_file = opts.map["input-file"].as<std::string>();
  xylose::fit::appspack::Input P( input_file );

  /* get the parameter information */
  xylose::fit::appspack::PackedParameters pp = P.packParameters();

  /* get the coupled parameter constraints */
  xylose::fit::appspack::PackedConstraints pc = P.packConstraints();

  // *** Create and fill the parameter list ***
  APPSPACK::Parameter::List params;
  params.sublist("Linear").setParameter("Lower", pp.lower);
  params.sublist("Solver").setParameter("Initial X", pp.initial);
  params.sublist("Linear").setParameter("Upper", pp.upper);
  params.sublist("Linear").setParameter("Scaling", pp.scaling);
  params.sublist("Linear").setParameter("Inequality Lower", pc.lower);
  params.sublist("Linear").setParameter("Inequality Matrix", pc.m);
  params.sublist("Linear").setParameter("Inequality Upper", pc.upper);
  //params.sublist("Solver").setParameter("Initial F", initialf);

  // Be sure to do casts on any ambiguous constants, such as in this case
  if ( P.getDebug() >= 0 )
    params.sublist("Solver").setParameter("Debug", P.getDebug());
  if ( P.getCacheOutput().size() )
    params.sublist("Solver").setParameter("Cache Output File", P.getCacheOutput() );
  if ( P.getCacheInput().size() )
    params.sublist("Solver").setParameter("Cache Input File", P.getCacheInput() );
  if ( P.getInitialStep() > 0.0 )
    params.sublist("Solver").setParameter("Initial Step", P.getInitialStep() );
  if ( P.getStepTolerance() > 0.0 )
    params.sublist("Solver").setParameter("Step Tolerance", P.getStepTolerance() );
  if ( P.getCacheComparisonTolerance() > 0.0 )
    params.sublist("Solver").setParameter("Cache Comparison Tolerance", P.getCacheComparisonTolerance() );
  if ( P.getBoundsTolerance() > 0.0 )
    params.sublist("Solver").setParameter("Bounds Tolerance", P.getBoundsTolerance() );


  // *** Instantiate the bounds ***
  APPSPACK::Constraints::Linear linear(params.sublist("Linear"));

  typedef APPSPACK::Solver<> Solver;
  Solver::State state;

#ifdef HAVE_PSTREAMS
  if ( opts.map.count("pipe-comm") ) {
    // *** Create the MinFunc
    PMinFunc minFunc( P.getProbFile(), input_file );

    // *** Instantiate the custom executor ***
    xylose::fit::appspack::ThreadedExecutor<PMinFunc> executor(minFunc);
    
    // *** Create and run the solver ***
    state = Solver(params.sublist("Solver"), executor, linear).solve();
  } else
#endif
  {
    // *** Create the MinFunc
    MinFunc minFunc( P.getProbFile(), input_file );

    // *** Instantiate the custom executor ***
    xylose::fit::appspack::ThreadedExecutor<MinFunc> executor(minFunc);
    
    // *** Create and run the solver ***
    state = Solver(params.sublist("Solver"), executor, linear).solve();
  }
  
  switch( state ) {
    case Solver::Continue:
      std::cerr << "Finished APPSPack with a continue state?" << std::endl;
      break;
    case Solver::StepConverged:
      std::cerr << "Finished APPSPack with step length converged"
                << std::endl;
      break;
    case Solver::FunctionConverged:
      std::cerr << "Finished APPSPack with function tolerance converged"
                << std::endl;
      break;
    case Solver::EvaluationsExhausted:
      std::cerr << "Finished APPSPack with number of function "
                   "evaluations exhausted" << std::endl;
      break;
    default:
      std::cerr << "Unknown finish APPSPack finish state" << std::endl;
  }
  
  return EXIT_SUCCESS;
}