示例#1
0
文件: problem5.c 项目: bsdemon/C
int main(int argc, char *argv[])
{
	double arr[] = {1,2,3,-1,5.5,5.5};
	double arrTwo[] ={10,20,30,40,50};
	double *resultArr;
	double number = 5.5;
	int i, arrSize = sizeof(arr)/sizeof(double);

	printf("arr_min = %.3lf \n", arr_min(arr,arrSize));
	printf("arr_max = %.3lf \n", arr_max(arr,arrSize));
	printf("arr_average = %.3lf \n", arr_average(arr,arrSize));
	printf("arr_sum = %.3lf \n", arr_sum(arr,arrSize));
	printf("arr_contains %lf, %d times. \n", number, arr_contains(arr,arrSize,number));
	puts("Now we merge arays");
	resultArr = arr_merge(arr, arrSize, arrTwo,5);
	for (i = 0; i < arrSize +5; ++i)
	{
		printf("arr[%d] = %.3lf \n", i, resultArr[i]);
	}
	puts("After arr_clear:");
	arr_clear(arr,arrSize);
	for (i = 0; i < arrSize; ++i)
	{
		printf("arr[%d] = %.3lf \n", i, arr[i]);
	}

	return 0;

}
示例#2
0
void test_bingham_discretize(int argc, char *argv[])
{
  if (argc < 5) {
    printf("usage: %s <z1> <z2> <z3> <ncells>\n", argv[0]);
    exit(1);
  }

  double z1 = atof(argv[1]);
  double z2 = atof(argv[2]);
  double z3 = atof(argv[3]);
  int ncells = atoi(argv[4]);

  double Z[3] = {z1, z2, z3};
  double V[3][4] = {{1,0,0,0}, {0,1,0,0}, {0,0,1,0}};
  double *Vp[3] = {&V[0][0], &V[1][0], &V[2][0]};

  bingham_t B;
  bingham_new(&B, 4, Vp, Z);

  bingham_pmf_t pmf;
  bingham_discretize(&pmf, &B, ncells);

  // check if pmf sums to 1
  //double tot_mass = sum(pmf.mass, pmf.n);
  //printf("tot_mass = %f\n", tot_mass);

  int i;

  //printf("break 1\n");
  int *colors; safe_malloc(colors, pmf.n, int);
  //printf("break 2\n");

  double max_mass = arr_max(pmf.mass, pmf.n);
  for (i = 0; i < pmf.n; i++)
    colors[i] = (int)(255*(pmf.mass[i] / max_mass));

  //printf("Calling tetramesh_meshgraph()...");
  double t = get_time_ms();
  meshgraph_t *graph = tetramesh_meshgraph(pmf.tessellation->tetramesh);
  //printf("%f ms\n", get_time_ms() - t);

  //printf("Calling tetramesh_graph()...");
  t = get_time_ms();
  tetramesh_graph(pmf.tessellation->tetramesh);
  //printf("%f ms\n", get_time_ms() - t);

  //printf("Calling tetramesh_save_PLY_colors()...\n");

  //tetramesh_save_PLY_colors(pmf.tessellation->tetramesh, graph, "mesh.ply", colors);
  tetramesh_save_PLY(pmf.tessellation->tetramesh, graph, "mesh.ply");

  // print out the points
  //printf("pmf.points = [ ");
  //for (i = 0; i < pmf.n; i++)
  //  printf("%f %f %f %f ; ", pmf.points[i][0], pmf.points[i][1], pmf.points[i][2], pmf.points[i][3]);
  //printf("];\n");
}
示例#3
0
文件: gmm.c 项目: awf/autodiff
double logsumexp(int n, double* x)
{
  int i;
  double mx, semx;

  mx = arr_max(n, x);
  semx = 0.;
  for (i = 0; i < n; i++)
  {
    semx += exp(x[i] - mx);
  }
  return log(semx) + mx;
}
示例#4
0
文件: main.c 项目: atommed/OP_repo
int main(void) {
  int a1[] = {3, 4, 5, 6, 7, -1, -6, 19, 8, 9, 15, -4, 3, 3, 10, 11, 12};
  int a2[] = {-1, 1,  40, -2, 3, 4, -7, 4, -9, 5, 6,
              2,  -1, -1, -1, 1, 1, 1,  1, -1, 1, 1};
  int m3[] = {
      -4, 3,  2,  1,  // r1
      5,  6,  -8, -8, // r2
      33, 3,  3,  1,  // r3
      -4, -4, -4, -4, // r4
  };

  puts("Task 1");
  for (unsigned int i = 0; i < LEN(a1); i++)
    printf("%d ", a1[i]);
  puts("");
  print_n_min(a1, LEN(a1), 5);
  puts("\n");

  puts("Task 2");
  place_negatives_first(a2, LEN(a2));
  for (unsigned int i = 0; i < LEN(a2); i++)
    printf("%d ", a2[i]);
  puts("\n");

  puts("Matrix:");
  for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= 4; j++)
      printf("%d\t", m3[mat_to_arr(i, j, 4)]);
    puts("");
  }
  puts("");

  puts("Task 3");
  printf("Min: %d, max: %d, avg: %lf, sum: %lld, mdsum: %lld, sdsum: %lld\n\n",
         arr_min(m3, LEN(m3)), arr_max(m3, LEN(m3)), arr_avg(m3, LEN(m3)),
         arr_sum(m3, LEN(m3)), arr_diagonal_sum(m3, 4, LEN(m3)),
         arr_subdiagonal_sum(m3, 4, LEN(m3)));

  puts("Task 4");
  swap_ext_rows(m3, 4, 4);
  puts("Matrix:");
  for (int i = 1; i <= 4; i++) {
    for (int j = 1; j <= 4; j++)
      printf("%d\t", m3[mat_to_arr(i, j, 4)]);
    puts("");
  }
  puts("");

  return 0;
}
示例#5
0
int main(void)
{
	int ar[SIZE];
	int t = 0;

	printf("Please input 5 integer:\n");
	for (t = 0; t < 5; t++)
	{
		scanf_s("%d", &ar[t], 4);
		fflush(stdin);
	}

	printf("The max number in the array is %d.\n", arr_max(ar));
	return 0;
}
示例#6
0
int main()
{   
    double arrayA[size];
    double arrayB[size];
    double searchedElement;
    int isFound;
    printf("Size of arrays is %d.\n", size);
    printf("Enter elements of arrayA\n");
    for(int i=0; i<size; i++)
    {
        scanf("%lf", &arrayA[i]);
    }  
    printf("Min element=%.2lf\n", arr_min(arrayA, size));
    printf("Max element=%.2lf\n", arr_max(arrayA, size));
    printf("Average sum of elements is %lf\n", arr_average(arrayA, size));
    printf("Sum of elements is %lf\n", arr_sum(arrayA, size));
    printf("Enter element you are looking for: ");
    scanf("%lf", &searchedElement);
    isFound=arr_contains(arrayA, size, searchedElement);
    printf(isFound? "The element is in the array\n":"The element is not in the array\n");
    printf("Enter elements of arrayB:\n");
    for(int i=0; i<size; i++)
    {
        scanf("%lf", &arrayB[i]);
    }
    double *mergedArray=arr_merge(arrayA, arrayB, size, size);
    printf("Merged array is:");
    for(int i=0; i< 2*size ; i++)
    {
        printf("%.2lf ", mergedArray[i]);
    }
    free(mergedArray);
    printf("\n");
    printf("Cleared array is: ");
    arr_clear(arrayA, size);
    for(int i=0; i<size; i++)
    {
        printf("%.2lf ", arrayA[i]);
    }
    
    return 0;
}
示例#7
0
文件: gmm_b-all.c 项目: awf/autodiff
/*
  Differentiation of logsumexp in reverse (adjoint) mode:
   gradient     of useful results: *x logsumexp
   with respect to varying inputs: *x
   Plus diff mem management of: x:in
*/
double logsumexp_b(int n, double *x, double *xb, double logsumexpb) {
  int i;
  double mx, semx;
  double mxb, semxb;
  double logsumexp;
  double tempb;
  mx = arr_max(n, x);
  semx = 0.;
  for (i = 0; i < n; ++i)
    semx = semx + exp(x[i] - mx);
  semxb = logsumexpb / semx;
  mxb = logsumexpb;
  for (i = n - 1; i > -1; --i) {
    tempb = exp(x[i] - mx)*semxb;
    xb[i] = xb[i] + tempb;
    mxb = mxb - tempb;
  }
  arr_max_b(n, x, xb, mxb);
  return log(semx) + mx;
}
int main()
{
	int numbers[] = { 12, 2, 23, 41, 5, 6, 7, 8, 1 , 4};
	int length = sizeof(numbers) / sizeof(int);

	int mina = arr_min(numbers, length);
	printf("Min is: %d\n", mina);

	int max = arr_max(numbers, length);
	printf("Max is: %d\n", max);
	
	double average = arr_average(numbers, length);
	printf("Average is: %0.4lf\n", average);
	
	long sum = arr_sum(numbers, length);
	printf("Sum is: %u\n", sum);
	
	int element = 431;
	int contains = arr_contains(numbers, length, element);
	if(contains == 1)
	{
		printf("The array contains: %d\n", element);
	} else
	{
		printf("The array does not contain: %d\n", element);
	}

	int *ptr = arr_merge(numbers, length, numbers, length);
	printf("%d == %d %d == %d \n", ptr[0], ptr[10], ptr[1], ptr[11]);
	free(ptr);

	arr_clear(numbers, length);
	printf("%d %d\n", numbers[0], numbers[1]);

	return 0;
}
示例#9
0
int ws_inv_cmod5(double sigma0, double phi0, double theta0,
                 double *wnd1, double *wnd2,
                 double min_ws, double max_ws, int npts,
                 double hh)
{
  // ws_cmod5 will return a 2-element array where the first element is the sigma0
  // that you'd get at min_ws, and the second element is the sigma0 that you get
  // at max_ws;
  // (phi0 is phi_diff, the diff between wind_dir and r_look, and theta0 is incidence angle)
  // Note that the 0.0 (last parameter) is r_look according to ws_cmod5, but the ws_cmod5 function
  // doesn't make use of it ...or more accurately, it's already built into the phi0 value.
  g_assert(max_ws > min_ws);
  g_assert(npts > 0);
  double *wd;
  int i;

  // Make an npts-element array of windspeeds that range from min_ws to max_ws linearly
  wd = maken(min_ws, max_ws, npts);
  g_assert(wd != NULL);

  // Calculate an npts-element array of normalized radar cross section (NRCS) using
  // the CMOD5 algorithm
  double *sg0 = ws_cmod5(wd, npts, phi0, theta0, 0.0); // Returned sg0[] has npts elements in it
  g_assert(sg0 != NULL);

  // See lines 118-123 in ws_inv_cmod5.pro
  // NOTE: The CMOD5 and CMOD4 algorithms need an adjustment when the polarization
  // is HH (since the original algorithms were developed for VV polarization only)
  // hh == -3 when polarization is VV, hh == 0.6 when polarization is HH
  // FIXME: Add cases for hh eq to -1 and -2
  double rr = (hh > 0.0) ? ws_pol_ratio(theta0, hh) : 1.0;
  sigma0 = (hh != -3) ? sigma0 / rr : sigma0; // HH adjustment or not

  // If sigma0 is lower than what you'd get at minimum windspeed, then set the windspeed to
  // the minimum and return. (Line 129)
  double min_nrcs = arr_min(sg0, npts); // Min should be sg0[0], but make sure...
  if (sigma0 < min_nrcs) {
    *wnd1 = wd[0];
    *wnd2 = WND1_IS_MINIMUM_WIND;
    FREE(wd);
    FREE(sg0);
    return 0;
  }

  // Create sign of differences array
  int ct=0;
  double *s = (double *)MALLOC(sizeof(double) * npts);
  for (i=0; i<npts; i++) {
    s[i] = SIGN(sg0[i] - sigma0);
    ct += s[i] == 0 ? 1 : 0;
    s[i] = (ct > 0 && s[i] == 0) ? 1.0 : s[i];
  }

  // Count the sign changes
  ct = 0;
  int ww = -1; // Where first sign change occurs
  int ww2 = -1; // Where second sign change occurs
  for (i=1; i<npts; i++) {
    ct += (s[i] != s[i-1]) ? 1 : 0;
    ww = (ct > 0 && ww < 0) ? i : ww;
    ww2 = (ct > 0 && ww > 0 && ww2 < 0) ? i : ww2;
  }

  // Calculate wind for the 3 cases (no sign changes, one sign change, two sign changes, and other)
  switch(ct) {
    case 0:
      {
        // No sign changes means sigma0 > max(sg0[])
        int nn = npts;
        double max_sg0 = arr_max(sg0, npts);
        int idx_first_max = -1;
        int *w = (int *)CALLOC(nn, sizeof(int));
        int wx = 0;
        for (i=0; i<npts; i++) {
          if (sg0[i] >= max_sg0) {
            idx_first_max = (idx_first_max < 0) ? i : idx_first_max;
            w[wx++] = i; // Collect indices of max's
          }
        }
        if (idx_first_max == nn - 1) {
          // Last element was the largest element, so send back max wind
          *wnd1 = wd[nn-1];
          *wnd2 = WND_FROM_MAX_SIGMA0;
        }
        else {
          int ix1 = (w[0] - 1 >= 0) ? w[0] - 1 : 0;
          int ix2 = w[0];
          int ix3 = w[0] + 1;
          double fit1;
          double *f1 = poly_fit(wd, sg0, ix1, ix2, ix3, 2, &fit1);
          *wnd1 = (f1[1]+sqrt(f1[1]*f1[1]-4.0*f1[2]*(f1[0]-sg0[ix2])))/2/f1[2];
          *wnd2 = WND_FROM_MAX_SIGMA0;
        }
        FREE(w);
      }
      break;
    case 1:
      {
        // Single solution
        int ix1 = ww;
        int ix2 = (ww+1) > npts - 1 ? npts - 1 : ww+1;
        int ix3 = (ww+2) > npts - 1 ? npts - 1 : ww+2;
        double fit1;
        double *f1 = poly_fit(wd, sg0, ix1, ix2, ix3, 2, &fit1);
        *wnd1 = (f1[1]+sqrt(f1[1]*f1[1]-4.0*f1[2]*(f1[0]-sigma0)))/2/f1[2];
        *wnd2 = WND1_IS_ONLY_SOLUTION;
      }
      break;
    case 2:
      {
        // Two solutions (usually lowest answer of the two is best answer)
        int ix1 = ww;
        int ix2 = (ww+1) > npts - 1 ? npts - 1 : ww+1;
        int ix3 = (ww+2) > npts - 1 ? npts - 1 : ww+2;
        int ix4 = ww2;
        int ix5 = (ww2+1) > npts - 1 ? npts - 1 : ww2+1;
        int ix6 = (ww2+2) > npts - 1 ? npts - 1 : ww2+2;
        double fit1, fit2;
        double *f1 = poly_fit(wd, sg0, ix1, ix2, ix3, 2, &fit1);
        double *f2 = poly_fit(wd, sg0, ix4, ix5, ix6, 2, &fit2);
        *wnd1 = (f1[1]+sqrt(f1[1]*f1[1]-4.0*f1[2]*(f1[0]-sigma0)))/2/f1[2];
        *wnd2 = (f2[1]+sqrt(f2[1]*f2[1]-4.0*f2[2]*(f2[0]-sigma0)))/2/f2[2];
      }
      break;
    default:
      *wnd1 = WND_FROM_MAX_SIGMA0;
      *wnd2 = WND_FROM_MAX_SIGMA0;
      break;
  }

  FREE(s);
  FREE(wd);
  FREE(sg0);

  return 0;
}
示例#10
0
文件: hll.c 项目: dtbinh/riss_bingham
/*
 * Sample n Gaussians (with means X and covariances S) from HLL at sample points Q.
 */
void hll_sample(double **X, double ***S, double **Q, hll_t *hll, int n)
{
  int i, j;
  if (hll->cache.n > 0) {
    for (i = 0; i < n; i++) {
      j = kdtree_NN(hll->cache.Q_kdtree, Q[i]);
      memcpy(X[i], hll->cache.X[j], hll->dx * sizeof(double));
      matrix_copy(S[i], hll->cache.S[j], hll->dx, hll->dx);
    }
    return;
  }

  double r = hll->r;
  int nx = hll->dx;
  int nq = hll->dq;

  double **WS = new_matrix2(nx, nx);

  for (i = 0; i < n; i++) {

    //printf("q = [%.2f %.2f %.2f %.2f]\n", Q[0][0], Q[0][1], Q[0][2], Q[0][3]);

    //for (j = 0; j < hll->n; j++)
    //  printf("hll->Q[%d] = [%.2f %.2f %.2f %.2f]\n", j, hll->Q[j][0], hll->Q[j][1], hll->Q[j][2], hll->Q[j][3]);

    // compute weights
    double dq, qdot;
    double w[hll->n];
    //printf("w = [");
    for (j = 0; j < hll->n; j++) {
      qdot = fabs(dot(Q[i], hll->Q[j], nq));
      dq = acos(MIN(qdot, 1.0));
      w[j] = exp(-(dq/r)*(dq/r));
      //printf("%.2f ", w[j]);
    }
    //printf("]\n");

    // threshold weights
    double wmax = arr_max(w, hll->n);
    double wthresh = wmax/50;  //dbug: make this a parameter?
    for (j = 0; j < hll->n; j++)
      if (w[j] < wthresh)
	w[j] = 0;
    double wtot = hll->w0 + sum(w, hll->n);

    // compute posterior mean
    mult(X[i], hll->x0, hll->w0, nx);  // X[i] = w0*x0
    for (j = 0; j < hll->n; j++) {
      if (w[j] > 0) {
	double wx[nx];
	mult(wx, hll->X[j], w[j], nx);
	add(X[i], X[i], wx, nx);       // X[i] += wx
      }
    }
    mult(X[i], X[i], 1/wtot, nx);  // X[i] /= wtot

    // compute posterior covariance matrix
    mult(S[i][0], hll->S0[0], hll->w0, nx*nx);  // S[i] = w0*S0

    for (j = 0; j < hll->n; j++) {
      if (w[j] > 0) {
	double wdx[nx];
	sub(wdx, hll->X[j], X[i], nx);
	mult(wdx, wdx, w[j], nx);
	outer_prod(WS, wdx, wdx, nx, nx);    // WS = wdx'*wdx
	matrix_add(S[i], S[i], WS, nx, nx);  // S[i] += WS
      }
    }

    mult(S[i][0], S[i][0], 1/wtot, nx*nx);  // S[i] /= wtot
  }

  free_matrix2(WS);
}
int main()
{
    int n;
    if (scanf("%d", &n) != 1)
    {
        printf("Invalid input!");
        return 1;
    }

    double numbers[n];
    int i;
    for (i = 0; i < n; i++)
    {
        scanf("%lf", &numbers[i]);
    }

    int length = lengthArr(numbers);
    int zeroFraction = count_of_zero_fractions(numbers, length);
    int notZeroFraction = length - zeroFraction;
    double fractioned[notZeroFraction];
    double zeroFractioned[zeroFraction];
    int j= 0, k = 0;
    for (i = 0; i < n; i++)
    {
        if (fmod(numbers[i], 1) == 0)
        {
            zeroFractioned[j] = numbers[i];
            j++;
        }
        else
        {
            fractioned[k] = numbers[i];
            k++;
        }
    }
    double fractionedMin = arr_min(fractioned, notZeroFraction);
    double zeroFractionedMin = arr_min(zeroFractioned, zeroFraction);

    double fractionedMax = arr_max(fractioned, notZeroFraction);
    double zeroFractionedMax = arr_max(zeroFractioned, zeroFraction);

    double fractionedSum = arr_sum(fractioned, notZeroFraction);
    double zeroFractionedSum = arr_sum(zeroFractioned, zeroFraction);

    double fractionedAvg = arr_average(fractioned, notZeroFraction);
    double zeroFractionedAvg = arr_average(zeroFractioned, zeroFraction);

    printf("\n");
    print_array(fractioned, notZeroFraction);
    printf(" -> min: %.3lf", fractionedMin);
    printf(", max: %.3lf", fractionedMax);
    printf(", sum: %.3lf", fractionedSum);
    printf(", avg: %.2lf", fractionedAvg);
    printf("\n");
    print_array(zeroFractioned, zeroFraction);
    printf(" -> min: %.lf", zeroFractionedMin);
    printf(", max: %.lf", zeroFractionedMax);
    printf(", sum: %.lf", zeroFractionedSum);
    printf(", avg: %.2lf", zeroFractionedAvg);

    printf("\n");

    return 0;
}