int main(int argc, const char *argv[]) { int n; //numero puts("Informe um numero:"); scanf("%d",&n); //pede ao usuario o ultimo 'n' da sequencia s = 1 + 1/1! + 1/2! +...+ 1/n! printf("O resultado é %1.2f\n",(float) calc_num(n));//Informa ao usuario o resultado do calculo mostrado acima. return 0; }
void num_min_paths(const std::vector<node> &grafo, int source, int destination, std::ofstream &out) { // Used to calculate the length of path from source to destination. breadth_first_search(grafo, source); int min_length = grafo[destination].dist; out << min_length << " " << calc_num(grafo, source, destination, min_length, min_length); }
int calc_num(const std::vector<node> &graph, int source, int destination, int length, int &numcammini) { if (source == destination && length == 0) { return 1; } if (source != destination && length == 0) { return 0; } int return_value = 0; for (int v : graph[source].neighbors) { return_value += calc_num(graph, v, destination, length - 1, numcammini); } return return_value; }
void regression(point *points, size_t point_count, transform_t t, double *slope, double *intercept) { assert(slope); assert(intercept); double mx = 0; double my = 0; if (!calc_means(points, point_count, t, &mx, &my)) { *slope = EMPTY_VALUE; *intercept = EMPTY_VALUE; return; } double num = calc_num(points, point_count, t, mx, my); double den = calc_den(points, point_count, t, mx); *slope = num / den; *intercept = my - *slope * mx; LOG(1, "-- slope: %g, intercept: %g\n", *slope, *intercept); }
/* DEFINE SAMPLING UNITS MANUALLY */ static void man_unit(int t, int b, int l, int r, char *n1, char *n2, char *n3, double *mx, int fmask) { int i, j, dx, dy, w_w, w_l, u_w, u_l, method, l0, t0, randflag = 0, unit_num, num = 0, scales, h_d = 1, v_d = 1, itmp, thick, sites, *row_buf, fr, k, count = 0, maxsize = 0, nx = 0, ny = 0, numx = 0, numy = 0, al = 0, ar = 0, at = 0, ab = 0, au_w = 0, au_l = 0; double *ux, *uy; FILE *fp; double dtmp, ratio, size, intv = 0.0, start[2], cnt = 0, radius = 0.0; char *sites_mapset; struct Cell_head wind; /* VARIABLES: COORDINATES IN THIS ROUTINE ARE IN CELLS t = top row of sampling frame b = bottom row of sampling frame l = left col of sampling frame r = right col of sampling frame n1 = n2 = n3 = start[0]= row of UL corner of starting pt for strata start[1]= col of UL corner of starting pt for strata mx[0] = cols of region/width of screen mx[1] = rows of region/height of screen */ start[0] = 0.0; start[1] = 0.0; l = (int)((double)(l * mx[0]) + 0.5); r = (int)((double)(r * mx[0]) + 0.5); t = (int)((double)(t * mx[1]) + 0.5); b = (int)((double)(b * mx[1]) + 0.5); w_w = r - l; w_l = b - t; /* draw the sampling frame */ R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5), (int)(r / mx[0] + 0.5), (int)(b / mx[1] + 0.5), 1); R_close_driver(); /* open the units file for output */ fp = fopen0("r.le.para/units", "w"); G_sleep_on_error(0); /* get the number of scales */ do { fprintf(stderr, "\n How many different SCALES do you want (1-15)? "); numtrap(1, &dtmp); if (dtmp > 15 || dtmp < 1) { fprintf(stderr, "\n Too many (>15) or too few scales; try again"); } } while (dtmp < 1 || dtmp > 15); fprintf(fp, "%10d # of scales\n", (scales = (int)dtmp)); /* for each scale */ for (i = 0; i < scales; i++) { for (;;) { G_system("clear"); radius = 0.0; fprintf(stderr, "\n\n TYPE IN PARAMETERS FOR SCALE %d:\n", i + 1); /* get the distribution method */ fprintf(stderr, "\n Choose method of sampling unit DISTRIBUTION \n"); fprintf(stderr, " Random nonoverlapping 1\n"); fprintf(stderr, " Systematic contiguous 2\n"); fprintf(stderr, " Systematic noncontiguous 3\n"); fprintf(stderr, " Stratified random 4\n"); fprintf(stderr, " Centered over sites 5\n"); fprintf(stderr, " Exit to setup option menu 6\n\n"); do { fprintf(stderr, " Which Number? "); numtrap(1, &dtmp); if ((method = fabs(dtmp)) > 6 || method < 1) { fprintf(stderr, "\n Choice must between 1-5; try again"); } } while (method > 6 || method < 1); if (method == 6) return; /* for stratified random distribution, determine the number of strata */ if (method == 4) { getstrata: fprintf(stderr, "\n Number of strata along the x-axis? (1-60) "); numtrap(1, &dtmp); h_d = fabs(dtmp); fprintf(stderr, "\n Number of strata along the y-axis? (1-60) "); numtrap(1, &dtmp); v_d = fabs(dtmp); if (h_d < 1 || v_d < 1 || h_d > 60 || v_d > 60) { fprintf(stderr, "\n Number must be between 1-60; try again."); goto getstrata; } } /* for methods with strata */ if (method == 2 || method == 3 || method == 4) { strata: fprintf(stderr, "\n Sampling frame row & col for upper left corner of"); fprintf(stderr, " the strata?\n Rows are numbered down and columns"); fprintf(stderr, " are numbered to the right\n Enter 1 1 to start in"); fprintf(stderr, " upper left corner of sampling frame: "); numtrap(2, start); start[0] = start[0] - 1.0; start[1] = start[1] - 1.0; if (start[0] > w_l || start[0] < 0 || start[1] > w_w || start[1] < 0) { fprintf(stderr, "\n The starting row and col you entered are outside"); fprintf(stderr, " the sampling frame\n Try again\n"); goto strata; } } if (method == 4) { /* call draw_grid with the left, top, width, length, the number of horizontal and vertical strata, and the starting row and col for the strata */ draw_grid((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5), (int)(w_w / mx[0] + 0.5), (int)(w_l / mx[1] + 0.5), h_d, v_d, (int)(start[0] / mx[1] + 0.5), (int)(start[1] / mx[0] + 0.5), mx[0], mx[1]); if (!G_yes(" Are these strata OK? ", 1)) { if (G_yes("\n\n Refresh the screen? ", 1)) { paint_map(n1, n2, n3); R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5), (int)(r / mx[0] + 0.5), (int)(b / mx[1] + 0.5), 1); R_close_driver(); } goto getstrata; } } /* if sampling using circles */ fprintf(stderr, "\n Do you want to sample using rectangles"); if (!G_yes ("\n (including squares) (y) or circles (n)? ", 1)) { getradius: fprintf(stderr, "\n What radius do you want for the circles? Radius"); fprintf(stderr, "\n is in pixels; add 0.5 pixels, for the center"); fprintf(stderr, "\n pixel, to the number of pixels outside the"); fprintf(stderr, "\n center pixel. Type a real number with one"); fprintf(stderr, "\n decimal place ending in .5 (e.g., 4.5): "); numtrap(1, &radius); if (radius > 100.0) { fprintf(stderr, "\n Are you sure that you want such a large"); if (!G_yes("\n radius (> 100 pixels)? ", 1)) goto getradius; } ratio = 1.0; u_w = (int)(2 * radius); u_l = (int)(2 * radius); if (fmask > 0) { count = 0; row_buf = Rast_allocate_buf(CELL_TYPE); fr = Rast_open_old(n1, G_mapset()); for (j = t; j < b; j++) { Rast_zero_buf(row_buf, CELL_TYPE); Rast_get_row(fr, row_buf, j, CELL_TYPE); for (k = l; k < r; k++) { if (*(row_buf + k)) count++; } } G_free(row_buf); Rast_close(fr); cnt = (double)(count); if (cnt) cnt = sqrt(cnt); else cnt = 0; } else { count = (w_l - (int)(start[0])) * (w_w - (int)(start[1])); } } /* if sampling using rectangles/squares */ else { /* get the width/length ratio */ getratio: fprintf(stderr, "\n Sampling unit SHAPE (aspect ratio, #cols/#rows) " "expressed as real number" "\n (e.g., 10 cols/5 rows = 2.0) for sampling units " "of scale %d? ", i + 1); numtrap(1, &ratio); if (ratio < 0) ratio = -ratio; else if (ratio > 25.0) if (!G_yes ("\n Are you sure you want such a large ratio? ", 1)) goto getratio; /* determine the recommended maximum size for sampling units */ getsize: dtmp = (ratio > 1) ? 1 / ratio : ratio; dtmp /= (h_d > v_d) ? h_d * h_d : v_d * v_d; tryagain: if (method == 1) { if (fmask > 0) { count = 0; row_buf = Rast_allocate_buf(CELL_TYPE); fr = Rast_open_old(n1, G_mapset()); for (j = t; j < b; j++) { Rast_zero_buf(row_buf, CELL_TYPE); Rast_get_row(fr, row_buf, j, CELL_TYPE); for (k = l; k < r; k++) { if (*(row_buf + k)) count++; } } G_free(row_buf); Rast_close(fr); cnt = (double)(count); if (cnt) cnt = sqrt(cnt); else cnt = 0; maxsize = ((cnt * dtmp / 2) * (cnt * dtmp / 2) > 1.0 / dtmp) ? (cnt * dtmp / 2) * (cnt * dtmp / 2) : 1.0 / dtmp; fprintf(stderr, "\n Recommended maximum SIZE is %d in %d cell total", maxsize, count); fprintf(stderr, " area\n"); } else { fprintf(stderr, "\n Recommended maximum SIZE is"); fprintf(stderr, " %d in %d pixel total area\n", (int)((w_l - (int)(start[0])) * (w_w - (int)(start [1])) * dtmp / 2), (w_l - (int)(start[0])) * (w_w - (int)(start[1]))); count = (w_l - (int)(start[0])) * (w_w - (int)(start[1])); maxsize = (int)((w_l - (int)(start[0])) * (w_w - (int)(start[1])) * dtmp / 2); } } else if (method == 2 || method == 3 || method == 5) { fprintf(stderr, "\n Recommended maximum SIZE is %d in %d pixel total", (int)((w_l - (int)(start[0])) * (w_w - (int)(start[1])) * dtmp / 2), (w_l - (int)(start[0])) * (w_w - (int)(start[1]))); fprintf(stderr, " area\n"); } else if (method == 4) { fprintf(stderr, "\n Recommended maximum SIZE is"); fprintf(stderr, " %d in %d pixel individual", (int)(w_w * w_l * dtmp / 2), ((w_w - (int)(start[1])) / h_d) * ((w_l - (int)(start [0])) / v_d)); fprintf(stderr, " stratum area\n"); } /* get the unit size, display the calculated size, and ask if it is OK */ fprintf(stderr, " What size (in pixels) for each sampling unit of scale %d? ", i + 1); numtrap(1, &size); thick = 1; if (size < 15 || ratio < 0.2 || ratio > 5) thick = 0; u_w = sqrt(size * ratio); u_l = sqrt(size / ratio); fprintf(stderr, "\n The nearest size is %d cells wide X %d cells high = %d", u_w, u_l, u_w * u_l); fprintf(stderr, " cells\n"); if (!u_w || !u_l) { fprintf(stderr, "\n 0 cells wide or high is not acceptable; try again"); goto tryagain; } if (!G_yes(" Is this SIZE OK? ", 1)) goto getsize; } /* for syst. noncontig. distribution, get the interval between units */ if (method == 3) { fprintf(stderr, "\n The interval, in pixels, between the units of scale"); fprintf(stderr, " %d? ", i + 1); numtrap(1, &intv); } /* if the unit dimension + the interval is too large, print a warning and try getting another size */ if (u_w + intv > w_w / h_d || u_l + intv > w_l / v_d) { fprintf(stderr, "\n Unit size too large for sampling frame; try again\n"); if (radius) goto getradius; else goto getsize; } /* for stratified random distribution, the number of units is the same as the number of strata */ if (method == 4) num = h_d * v_d; /* for the other distributions, calculate the maximum number of units, then get the number of units */ else if (method == 1 || method == 2 || method == 3) { if (method == 1) { if (! (unit_num = calc_num(w_w, w_l, ratio, u_w, u_l, method, intv, (int)(start[1]), (int)(start[0]), u_w * u_l, count))) { fprintf(stderr, "\n Something wrong with sampling unit size, try again\n"); if (radius) goto getradius; else goto getsize; } fprintf(stderr, "\n Maximum NUMBER of units in scale %d is %d\n", i + 1, unit_num); fprintf(stderr, " Usually 1/2 of this number can be successfully"); fprintf(stderr, " distributed\n More than 1/2 can sometimes be"); fprintf(stderr, " distributed\n"); } else if (method == 2 || method == 3) { numx = floor((double)(w_w - start[1]) / (u_w + intv)); numy = floor((double)(w_l - start[0]) / (u_l + intv)); if (((w_w - (int)(start[1])) % (numx * (u_w + (int)(intv)))) >= u_w) numx++; if (((w_l - (int)(start[0])) % (numy * (u_l + (int)(intv)))) >= u_l) numy++; unit_num = numx * numy; fprintf(stderr, "\n Maximum NUMBER of units in scale %d is %d as %d", i + 1, unit_num, numy); fprintf(stderr, " rows with %d units per row", numx); } do { fprintf(stderr, "\n What NUMBER of sampling units do you want to try"); fprintf(stderr, " to use? "); numtrap(1, &dtmp); if ((num = dtmp) > unit_num || num < 1) { fprintf(stderr, "\n %d is greater than the maximum number of", num); fprintf(stderr, " sampling units; try again\n"); } else if (method == 2 || method == 3) { fprintf(stderr, "\n How many sampling units do you want per row? "); numtrap(1, &dtmp); if ((nx = dtmp) > num) { fprintf(stderr, "\n Number in each row > number requested; try"); fprintf(stderr, " again\n"); } else { if (nx > numx) { fprintf(stderr, "\n Can't fit %d units in each row, try", nx); fprintf(stderr, " again\n"); } else { if (num % nx) ny = num / nx + 1; else ny = num / nx; if (ny > numy) { fprintf(stderr, "\n Can't fit the needed %d rows, try", ny); fprintf(stderr, " again\n"); } } } } } while (num > unit_num || num < 1 || nx > num || nx > numx || ny > numy); } /* dynamically allocate storage for arrays to store the upper left corner of sampling units */ if (method != 5) { ux = G_calloc(num + 1, sizeof(double)); uy = G_calloc(num + 1, sizeof(double)); } else { ux = G_calloc(250, sizeof(double)); uy = G_calloc(250, sizeof(double)); } /* calculate the upper left corner of sampling units and store them in arrays ux and uy */ if (!calc_unit_loc (radius, t, b, l, r, ratio, u_w, u_l, method, intv, num, h_d, v_d, ux, uy, &sites, (int)(start[1]), (int)(start[0]), fmask, nx, mx[0], mx[1])) goto last; signal(SIGINT, SIG_DFL); if (method == 5) num = sites; /* draw the sampling units on the screen */ if (method == 2 || method == 3 || method == 5) { R_open_driver(); R_standard_color(D_translate_color("red")); for (j = 0; j < num; j++) { if (radius) { draw_circle((int)((double)(ux[j]) / mx[0]), (int)((double)(uy[j]) / mx[1]), (int)((double)(ux[j] + u_w) / mx[0]), (int)((double)(uy[j] + u_l) / mx[1]), 3); } else { draw_box((int)((double)(ux[j]) / mx[0]), (int)((double)(uy[j]) / mx[1]), (int)((double)(ux[j] + u_w) / mx[0]), (int)((double)(uy[j] + u_l) / mx[1]), 1); } } R_close_driver(); } if (G_yes("\n Is this set of sampling units OK? ", 1)) break; last: signal(SIGINT, SIG_DFL); if (G_yes("\n Refresh the screen? ", 1)) { paint_map(n1, n2, n3); R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0]), (int)(t / mx[1]), (int)(r / mx[0]), (int)(b / mx[1]), 1); R_close_driver(); } } /* save the sampling unit parameters in r.le.para/units file */ fprintf(fp, "%10d # of units of scale %d.\n", num, (i + 1)); fprintf(fp, "%10d%10d u_w, u_l of units in scale %d\n", u_w, u_l, (i + 1)); fprintf(fp, "%10.1f radius of circles in scale %d\n", radius, (i + 1)); for (j = 0; j < num; j++) fprintf(fp, "%10d%10d left, top of unit[%d]\n", (int)ux[j], (int)uy[j], j + 1); if (i < scales - 1 && G_yes("\n\n Refresh the screen? ", 1)) { paint_map(n1, n2, n3); R_open_driver(); R_standard_color(D_translate_color("grey")); draw_box((int)(l / mx[0]), (int)(t / mx[1]), (int)(r / mx[0]), (int)(b / mx[1]), 1); R_close_driver(); } } /* free dynamically allocated memory */ G_free(ux); G_free(uy); fclose(fp); return; }
/* Performs LSE of consequent parameters for all branches in the network * * PARAMETERS: A ---> predictor variables matrix * y ---> expected results for this sample set (P in total) * * PRE: A != NULL * y != NULL * * POS: result != NULL && best fit consequent parameters vector returned * or * result == NULL */ static gsl_vector * anfis_fit_linear (const gsl_matrix *A, const gsl_vector *y, size_t P, size_t M) { gsl_matrix *S = NULL, /* Covariance matrix */ *Snew = NULL, *Saux = NULL; gsl_vector *X = NULL, /* Future best fit parameters */ *Xnew = NULL; unsigned int i = 0; double den = 0.0, factor = 0.0; assert (A != NULL); assert (y != NULL); /* Generating necessary workspace */ S = gsl_matrix_alloc (M,M); if (S == NULL) goto exit_point; Snew = gsl_matrix_calloc (M,M); if (Snew == NULL) goto exit_point; Saux = gsl_matrix_calloc (M,M); if (Saux == NULL) goto exit_point; Xnew = gsl_vector_alloc (M); if (Xnew == NULL) goto exit_point; X = gsl_vector_calloc (M); if (X == NULL) goto exit_point; /* S = γ*Id , where γ is a large number */ gsl_matrix_set_identity (S); gsl_matrix_scale (S, _gamma); /* Performing Least Square Estimation */ for (i=0 ; i < P ; i++) { /* Matrix A i-th row (row At_i+1 in Jang's paper) */ gsl_vector_const_view A_i = gsl_matrix_const_row (A, i); /* Snew = S(i) * A_i+1 * At_i+1 * S(i) */ calc_num (S, &(A_i.vector), Snew, Saux, Xnew, M); /* scale = At_i+1 * S(i) * A_i+1 */ den = calc_den (S, &(A_i.vector), Xnew); /* Snew = Snew / (1+scale) */ gsl_matrix_scale (Snew, 1.0 / (1.0+den)); /* S(i+1) = S(i) - Snew */ gsl_matrix_sub (S, Snew); /* factor = At_i+1 * X(i) */ gsl_blas_ddot (&(A_i.vector), X, &factor); /* factor = yt_i+1 - factor */ factor = gsl_vector_get (y, i) - factor; /* Xnew = S(i+1) * A_i+1 */ gsl_blas_dgemv (CblasNoTrans, 1.0, S, &(A_i.vector), 0.0, Xnew); /* Xnew = Xnew * factor */ gsl_vector_scale (Xnew, factor); /* X(i+1) = X(i) + Xnew */ gsl_vector_add (X, Xnew); } exit_point: if (S != NULL) { gsl_matrix_free (S); S = NULL;} if (Snew != NULL) { gsl_matrix_free (Snew); Snew = NULL;} if (Saux != NULL) { gsl_matrix_free (Saux); Saux = NULL;} if (Xnew != NULL) { gsl_vector_free (Xnew); Xnew = NULL;} return X; }