Exemple #1
0
static void do_stuff(void)
{
	//int m = 7;
	int m = 3;
	int n = 3;
	double a[n*m], d[n], u[m*m], v[n*n], dmat[n*m];

	// build and print a matrix of random numbers
	FORI(n*m) a[i] = randombounds(10, 99);
	PMATRIX(a, m, n);

	// compute the SVD
	int r = svd(d, a, u, m, v, n);

	// show the results
	FORI(n*m) dmat[i] = 0;
	FORI(n) dmat[n*i+i] = d[i];
	PMATRIX(dmat, m, n);
	PMATRIX(u, m, m);
	PMATRIX(v, n, n);

	// check the reconstruction
	double ud[m*n], udv[m*n];
	rmmult(ud, u, dmat, m, m, n);
	trnm(v,n);
	rmmult(udv, ud, v, m, n, n);
	PMATRIX(udv, m, n);
}
Exemple #2
0
int main() {
  string NEG = "negative";
  string TWENTY[20] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
		       "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
  string TENS[8] = {"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
  string BIG[3] = {"hundred","thousand", "million"};

  map<string,long> M;
  FORI(20)
    M[TWENTY[i]] = i;
  map<string,long> tens;
  FORI(8)
    M[TENS[i]] = 10*(i+2);  

  while(true) {
    bool neg = false;
  
    string s;
    if(!getline(cin, s))
      return 0;
    stringstream ss; ss << s;
    
    long ret = 0;
    long thousand = 0;
    while(ss >> s) {
      if(s == NEG) {
	neg = true;
	continue;
      }
      if(s == BIG[2]) { // Million:
	ret = thousand * 1000000;
	thousand = 0;
	continue;
      }
      if(s == BIG[1]) { // thousand:
	ret += thousand * 1000;
	thousand = 0;
	continue;
      }
      if(s == BIG[0]) { // hundred:
	thousand *= 100;
	continue;
      }
      thousand += M[s];
    }    

    ret += thousand;
    if(neg)
      ret = -ret;
    cout << ret << endl;
  }
}
Exemple #3
0
// TODO: add more scalarization options here: CIELABs, XYZ, PCA ...
static float *scalar(float *x, int w, int h, int pd, char *level_type)
{
	if (pd == 1) return x;
	float *y = xmalloc(w * h * pd * sizeof*y);
	if (false) { ;
	} else if (pd == 3 && 0 == strcmp(level_type, "h")) {
		FORI(w*h) get_hsv(y+i, NULL, NULL, x + i*pd);
	} else if (pd == 3 && 0 == strcmp(level_type, "s")) {
		FORI(w*h) get_hsv(NULL, y+i, NULL, x + i*pd);
	} else if (pd == 3 && 0 == strcmp(level_type, "v")) {
		FORI(w*h) get_hsv(NULL, NULL, y+i, x + i*pd);
	} else if (pd == 3 && 0 == strcmp(level_type, "i")) {
		FORI(w*h) y[i] = get_rgb_intensity(x + i*pd);
	} else if (pd == 3 && 0 == strcmp(level_type, "r")) {
		FORI(w*h) y[i] = x[i*pd];
	} else if (pd == 3 && 0 == strcmp(level_type, "g")) {
		FORI(w*h) y[i] = x[i*pd + 1];
	} else if (pd == 3 && 0 == strcmp(level_type, "b")) {
		FORI(w*h) y[i] = x[i*pd + 2];
	} else if (isdigit(level_type[0])) {
		int c = bound(0, atoi(level_type), pd-1);
		FORI(w*h) y[i] = x[i*pd + c];
	} else {
		FORI(w*h) {
			float m = 0;
			FORL(pd) m += x[i*pd + l];
			y[i] = m/pd;
		}
	}
Exemple #4
0
// Wrapper around FFTW3 that computes the real-valued inverse Fourier transform
// of a complex-valued frequantial image.
// The input data must be hermitic.
static void ifft_2dfloat(float *ifx,  fftwf_complex *fx, int w, int h)
{
	fftwf_complex *a = fftwf_xmalloc(w*h*sizeof*a);
	fftwf_complex *b = fftwf_xmalloc(w*h*sizeof*b);

	//fprintf(stderr, "planning...\n");
	evoke_wisdom();
	fftwf_plan p = fftwf_plan_dft_2d(h, w, a, b,
						FFTW_BACKWARD, FFTW_ESTIMATE);
	bequeath_wisdom();
	//fprintf(stderr, "...planned!\n");

	FORI(w*h) a[i] = fx[i];
	fftwf_execute(p);
	float scale = 1.0/(w*h);
	FORI(w*h) {
		fftwf_complex z = b[i] * scale;
		ifx[i] = crealf(z);
		if (FIWARN() > 0)
		{
			if (cimagf(z) > 0.001)
				fail("z is not real {cimagf(z)=%g} (set FIWARN=0 to run anyway)", cimagf(z));
			//assert(cimagf(z) < 0.001);
		}
	}
Exemple #5
0
// external interface to GSL code
int minimize_objective_function(double *result, double *first, double *step,
		objective_function *f, int n, void *data)
{
	gsl_vector *ss = gsl_vector_alloc(n);
	gsl_vector *x = gsl_vector_alloc(n);

	FORI(n) gsl_vector_set(ss, i, step[i]);
	FORI(n) gsl_vector_set(x, i, first[i]);

	const gsl_multimin_fminimizer_type *T =
		gsl_multimin_fminimizer_nmsimplex2;
	gsl_multimin_fminimizer *s = gsl_multimin_fminimizer_alloc(T, n);

	struct enveloped_function e[1] = {{
		.f = f,
		.n = n,
		.data = data }};
Exemple #6
0
static void print_matrix(char *name, double *a, int m, int n)
{
	printf("%s =", name);
	FORJ(m) {
		FORI(n)
			printf("\t%.3f", a[n*j + i]);
		printf("\n");
	}
}
Exemple #7
0
int main(void){
	int m, n;
	for(RI(n), RI(m); (m || n); RI(n), RI(m)){
		int a, ans = 0, b, maxppa = MINPPA, ppa;
		FOR(i, n) cnt[i] = 0, p[i] = i;
		FOR(i, n) FORI(j, i + 1, n) adj[i][j] = MINPPA;
		FOR(i, m){
			RI(a), RI(b), RI(ppa);
			--a; --b;
			if(a > b) swap(a, b);
			if(ppa > adj[a][b]){
				adj[a][b] = ppa;
				maxppa = max(maxppa, ppa);				
			}
		}
		FOR(i, n) FORI(j, i + 1, n) if(adj[i][j] == maxppa) unionfind(i, j);
		FOR(i, n) ans = max(ans, ++cnt[findroot(i)]);
		printf("%d\n", ans);
	}
Exemple #8
0
// when entering a square from edge indexed "from", where do we exit?
int march_one_step(float values[4], float threshold, int from)
{
	assert(from >= 0 && from < 4);
	int k = marching_squares_case(a, b, c, d, t);
	if (k == 0 || k == 15) error("empty case!");
	float w[4];
	FORI(4)
		w[i] = v[(i-from)%4];
	int r = march_one_step_from_below(w, threshold);
	return (r+from)%4;
}
Exemple #9
0
static double envelop_objective_function(const gsl_vector *v, void *pp)
{
	struct enveloped_function *p = pp;
	assert(p->n == (int)v->size);
	int n = p->n;
	double point[n];
	FORI(n)
		point[i] = gsl_vector_get(v, i);
	double r = (p->f)(point, n, p->data);
	return r;
}
Exemple #10
0
int main(int argc, const char *argv[])
{
#ifdef DB
	fp = fopen("input", "r");
#endif
	int i, n;
	while (scanf("%d", &n), n) {
		FORIZ(i, i < n) scanf("%d", &list[i]);
		qsort(list, n, sizeof(int), cmpr);
		printf("%d", list[0]);
		FORI(i, 1, i < n) printf(" %d", list[i]);
		putchar('\n');
	}
	return 0;
}
Exemple #11
0
static float *crop(float *x, int w, int h, int pd, int c[4], int *ow, int *oh)
{
	int x0 = bound(0, c[0], w-1);
	int y0 = bound(0, c[1], h-1);
	int xf = bound(x0+1, c[2]==-1?w:c[2], w);
	int yf = bound(y0+1, c[3]==-1?h:c[3], h);
	int cw = xf - x0;
	int ch = yf - y0;
	float *y = xmalloc(cw*ch*pd*sizeof*y);
	FORJ(ch) FORI(cw) FORL(pd)
		y[(i + j*cw)*pd + l] = x[(x0 + i + (y0 + j)*w)*pd + l];
	*ow = cw;
	*oh = ch;
	return y;
}
Exemple #12
0
// wrapper around FFTW3 that computes the complex-valued Fourier transform
// of a real-valued image
static void fft_2dfloat(fftwf_complex *fx, float *x, int w, int h)
{
	fftwf_complex *a = fftwf_malloc(w*h*sizeof*a);

	//fprintf(stderr, "planning...\n");
	evoke_wisdom();
	fftwf_plan p = fftwf_plan_dft_2d(h, w, a, fx,
						FFTW_FORWARD, FFTW_ESTIMATE);
	bequeath_wisdom();
	//fprintf(stderr, "...planned!\n");

	FORI(w*h) a[i] = x[i]; // complex assignment!
	fftwf_execute(p);

	fftwf_destroy_plan(p);
	fftwf_free(a);
	fftwf_cleanup();
}
Exemple #13
0
// Wrapper around FFTW3 that computes the real-valued inverse Fourier transform
// of a complex-valued frequantial image.
// The input data must be hermitic.
static void ifft_2dfloat(float *ifx,  fftwf_complex *fx, int w, int h)
{
	fftwf_complex *a = fftwf_malloc(w*h*sizeof*a);
	fftwf_complex *b = fftwf_malloc(w*h*sizeof*b);

	//fprintf(stderr, "planning...\n");
	evoke_wisdom();
	fftwf_plan p = fftwf_plan_dft_2d(h, w, a, b,
						FFTW_BACKWARD, FFTW_ESTIMATE);
	bequeath_wisdom();
	//fprintf(stderr, "...planned!\n");

	FORI(w*h) a[i] = fx[i];
	fftwf_execute(p);
	float scale = 1.0/(w*h);
	FORI(w*h) {
		fftwf_complex z = b[i] * scale;
		ifx[i] = crealf(z);
		//assert(cimagf(z) < 0.001);
	}
Exemple #14
0
int main() {
  set<PI> rows[10001];
  int pos[10001];

  int width, height;
  while(cin >> height >> width) {
    FORI(width)
      rows[i].clear();
    FORI(height) {
      GI(N);
      FORJ(N) {
	cin >> pos[j];
	--pos[j];
      }
      FORJ(N) {
	GI(x);
	rows[pos[j]].insert(PI(i, x));
      }
    }
    cout << width << " " << height << endl;
    FORI(width) {
      cout << rows[i].size();
      FORIT(set<PI>, rows[i]) {
	cout << " " << it->first+1;
      }
      cout << endl;
      bool first = true;
      FORIT(set<PI>, rows[i]) {
	if(!first)
	  cout << " ";
	first = false;
	cout << it->second;
      }
      cout << endl;	
    } // FORI
  } // while(cin)
Exemple #15
0
int main() {
  int n, k; 
  long long costToCity[11];
  vector<long long> prices[11][11];

  for(int cas = 1; true; ++cas) {
    cin >> n >> k; // n cities s=1..n=t, k days.
    if(n == 0 && k == 0)
      return 0;
    
    FORI(n)
      costToCity[i] = INF;
    costToCity[n-1] = 0; // Last day we must be in Atlanta.
    
    // Read fares:
    FORI(n) {
      FORJ(n) {
	prices[i][j].clear();
	if(i == j)
	  continue;
	int len;
	cin >> len;
	//cerr << i << "->" << j << ":";
	FORK(len) {
	  long long price;
	  cin >> price;
	  //cerr << " " << price;
	  prices[i][j].push_back(price);
	}
	//cerr << endl;
      }
    }

    // Walk backwards in time:
    for(int day = k-1; day >= 0; --day) {
      long long newCostToCity[11];
      FORI(n) { // Go to Atlanta from this destination at day:
	long long best = INF;
	FORJ(n) {
	  if(i == j || prices[i][j].empty())
	    continue;
	  long long price = prices[i][j][day % prices[i][j].size()];
	  if(price == 0)
	    continue;
	  if(price + costToCity[j] < best) {
	    best = price + costToCity[j];
	  }
	}
	newCostToCity[i] = best;
      }
      FORI(n) {
	costToCity[i] = newCostToCity[i];
	//cerr << " " << costToCity[i];
      }
      // cerr << endl;
    }
    
    cout << "Scenario #" << cas << endl;
    if(costToCity[0] == INF) {
      cout << "No flight possible." << endl;
    }
    else {
      cout << "The best flight costs " << costToCity[0] << "." << endl;
    }
    cout << endl;
  } // for cas
} // int main