int main() { // produce llvm.memcpy's char carray[LEN] = {1, 3, 5, 3, 2, 5, 3, 5, 3, 5, -2, 100}; short int sarray[LEN] = {-1, -3, -5, -3, -2, -5, -3, -5, -3, -5, 2, 100}; int array[LEN] = {-1, -3, -5, -3, -2, -5, -3, -5, -3, -5, 2, 100}; long long larray[LEN] = {1, 3, 5, 3, 2, 5, 3, 5, 3, 5, -2, 100}; int i, sum = arr_sum(carray, sarray, array, larray); empty_byte(carray); empty_short(sarray); empty_word(array); empty_long(larray); sum += arr_sum(carray, sarray, array, larray); printf("Result: %d\n", sum); if (sum == 400) { printf("RESULT: PASS\n"); } else { printf("RESULT: FAIL\n"); } return sum; }
float arr_average(int arr[], int length) { int sum = arr_sum(arr, length); float average = (float)sum / length; return average; }
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; }
double arr_average(double array[], int arrLength) { double sum = arr_sum(array, arrLength); double average = sum / arrLength; return average; }
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; }
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; }
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; }
int main(int argc, char *argv[]) { /* Important definitions */ // Lengths size_t N = 0; // Length of time series size_t M = 0; // Length of sampling vector (number of frequencies) // Filenames char inname[100]; char outname[100]; // Sampling double low, high, rate; // Frequency of window function double winfreq = 0; // Options int quiet = 0; int unit = 1; int prep = 1; int autosamp = 0; int fast = 0; int useweight = 0; int windowmode = 0; int Nclean = 0; int filter = 0; /* Process command line arguments and return line count of the input file */ N = cmdarg(argc, argv, inname, outname, &quiet, &unit, &prep, &low, &high,\ &rate, &autosamp, &fast, &useweight, &windowmode, &winfreq,\ &Nclean, &filter, NULL, NULL); // Pretty print if ( quiet == 0 || fast == 1 ){ if ( windowmode == 0 && useweight != 0 ) printf("\nCalculating the weighted power spectrum of \"%s\" ...\n",\ inname); else if ( windowmode == 0 ) printf("\nCalculating the power spectrum of \"%s\" ...\n", inname); else printf("\nCalculating the window function of \"%s\" ...\n", inname); } /* Read data (and weights) from the input file */ if ( quiet == 0 ) printf(" - Reading input\n"); double* time = malloc(N * sizeof(double)); double* flux = malloc(N * sizeof(double)); double* weight = malloc(N * sizeof(double)); readcols(inname, time, flux, weight, N, useweight, unit, quiet); // Do if fast-mode and window-mode is not activated if ( fast == 0 && windowmode == 0 ) { // Calculate Nyquist frequency double* dt = malloc(N-1 * sizeof(double)); double nyquist; arr_diff(time, dt, N); nyquist = 1.0 / (2.0 * arr_median(dt, N-1)) * 1e6; // microHz ! free(dt); // Calculate suggested sampling (4 times oversampling) double minsamp; minsamp = 1.0e6 / (4 * (time[N-1] - time[0])); // microHz ! // Display info? if ( quiet == 0 ){ printf(" -- INFO: Length of time series = %li\n", N); printf(" -- INFO: Nyquist frequency = %.2lf microHz\n", nyquist); printf(" -- INFO: Suggested minimum sampling = %.3lf microHz\n",\ minsamp); } // Apply automatic sampling? if ( autosamp != 0 ) { low = 5.0; high = nyquist; rate = minsamp; } } /* Prepare for power spectrum */ // Calculate proper frequency range for window-function-mode double limit = 0; if ( windowmode != 0 ) { limit = low; low = winfreq - limit; high = winfreq + limit; } // Get length of sampling vector M = arr_util_getstep(low, high, rate); // Fill sampling vector with cyclic frequencies double* freq = malloc(M * sizeof(double)); arr_init_linspace(freq, low, rate, M); // Initialise arrays for data storage double* power = malloc(M * sizeof(double)); double* alpha = malloc(M * sizeof(double)); double* beta = malloc(M * sizeof(double)); /* Calculate power spectrum OR window function */ if ( windowmode == 0 ) { // Subtract the mean to avoid "zero-frequency" problems if ( prep != 0 ) { if ( quiet == 0 ){ printf(" - Subtracting the mean from time series\n"); } arr_sca_add(flux, -arr_mean(flux, N), N); } else { if ( quiet == 0 ) printf(" - Time series used *without* mean subtraction!\n"); } // Display info if ( quiet == 0 ){ printf(" - Calculating fourier transform\n"); if ( autosamp != 0 ) { printf(" -- NB: Using automatic sampling!\n"); printf(" -- INFO: Auto-sampling (in microHz): %.2lf to %.2lf"\ " in steps of %.4lf\n", low, high, rate); } else { printf(" -- INFO: Sampling (in microHz): %.2lf to %.2lf in"\ " steps of %.4lf\n", low, high, rate); } printf(" -- INFO: Number of sampling frequencies = %li\n", M); } // Calculate power spectrum with or without weights fourier(time, flux, weight, freq, N, M, power, alpha, beta, useweight); } else { if ( quiet == 0 ){ printf(" - Calculating window function\n"); printf(" -- INFO: Window frequency = %.2lf microHz\n", winfreq); printf(" -- INFO: Sampling in the range +/- %.2lf microHz in" \ " steps of %.4lf microHz\n", limit, rate); printf(" -- INFO: Number of sampling frequencies = %li\n", M); } // Calculate spectral window with or without weights windowfunction(time, freq, weight, N, M, winfreq, power, useweight); if ( quiet == 0 ) printf(" - Sum of spectral window = %.4lf\n", arr_sum(power, M)); // Move frequencies to the origin arr_sca_add(freq, -winfreq, M); } /* Write data to file */ if ( quiet == 0 ) printf(" - Saving to file \"%s\"\n", outname); writecols(outname, freq, power, M); /* Free data */ free(time); free(flux); free(weight); free(freq); free(power); free(alpha); free(beta); /* Done! */ if ( quiet == 0 || fast ==1 ) printf("Done!\n\n"); return 0; }
/** Train the unsupervised SOM network. * * The network is initialized with random (non-graduate) values. It is then * trained with the image pixels (RGB). Once the network is done training the * centroids of the resulting clusters is returned. * * @note The length of the clusters depends on the parameter 'n'. The greatest * n, the more colors in the clusters, the less posterized the image. * * @param[out] res The resulting R, G and B clusters centroids. * @param[in] imgPixels The original image pixels. * @param[in] nbPixels The number of pixels of th image (height * width). * @param[in] nbNeurons The posterization leveldefined by its number of * neurons. * @param[in] noEpoch Number of training passes (a too small number of epochs * won't be enough for the network to correctly know the image but a too high * value will force the network to modify the wieghts so much that the image * can actually looks "ugly"). * @param[in] thresh The threshold value. The SOM delta parameter and the * training rate are dicreasing from one epoch to the next. The treshold value * set a stop condifition in case the value has fallen under this minimum * value. * * @return SOM_OK if everything goes right or SOM_NO_MEMORY if a memory * allocation (malloc) fail. */ int som_train(float **res, float **imgPixels, unsigned int nbPixels, int nbNeurons, int noEpoch, float thresh){ int mapWidth = // Map width. This can be calculated from its (int)sqrt(nbNeurons); // number of neurons as the map is square. int mapHeight = // Map height. This can be calculated from its (int)sqrt(nbNeurons); // number of neurons as the map is square. float delta = INT_MAX; // Start with an almost impossible value int it = 0; // Count the network iterations unsigned int pick; // The choosen input pixel float rad; // Neighbooring radius (keep dicreasing) float eta; // Learning rate (keep dicreasing) float pickRGB[3] = {0}; // Contains the choosen input RGB vector size_t choosen; // Best Matching Unit (BMU) int choosen_x; // BMU abscissa int choosen_y; // BMU ordinate float *neigh = // Neighbooring mask malloc(sizeof(float) * nbNeurons); if(neigh == NULL){ return SOM_NO_MEMORY; } memset(neigh, 0, nbNeurons); float *WR = // RED part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(WR == NULL){ return SOM_NO_MEMORY; } memset(WR, 0, nbNeurons); float *WG = // GREEN part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(WG == NULL){ return SOM_NO_MEMORY; } memset(WG, 0, nbNeurons); float *WB = // BLUE part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(WB == NULL){ return SOM_NO_MEMORY; } memset(WB, 0, nbNeurons); float *deltaR = // New RED part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(deltaR == NULL){ return SOM_NO_MEMORY; } memset(deltaR, 0, nbNeurons); float *deltaG = // New GREEN part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(deltaG == NULL){ return SOM_NO_MEMORY; } memset(deltaG, 0, nbNeurons); float *deltaB = // New BLUE part of the network weight vectors malloc(sizeof(float) * nbNeurons); if(deltaB == NULL){ return SOM_NO_MEMORY; } memset(deltaB, 0, nbNeurons); float *absDeltaR = // Absolute value of deltaR malloc(sizeof(float) * nbNeurons); if(absDeltaR == NULL){ return SOM_NO_MEMORY; } memset(absDeltaR, 0, nbNeurons); float *absDeltaG = // Absolute value of deltaG malloc(sizeof(float) * nbNeurons); if(absDeltaG == NULL){ return SOM_NO_MEMORY; } memset(absDeltaG, 0, nbNeurons); float *absDeltaB = // Absolute value of deltaB malloc(sizeof(float) * nbNeurons); if(absDeltaB == NULL){ return SOM_NO_MEMORY; } memset(absDeltaB, 0, nbNeurons); float *dists = // Vectors euclidian distances from input form malloc(sizeof(float) * nbNeurons); if(dists == NULL){ return SOM_NO_MEMORY; } memset(dists, 0, nbNeurons); /* Randomly initialize weight vectors */ srand(time(NULL)); random_sample(WR, nbNeurons); random_sample(WG, nbNeurons); random_sample(WB, nbNeurons); while(it < noEpoch && delta >= thresh){ /* Randomly choose an input form */ pick = random_uint(nbPixels); pickRGB[0] = imgPixels[pick][0]; pickRGB[1] = imgPixels[pick][1]; pickRGB[2] = imgPixels[pick][2]; /* Compute every vectors euclidian distance from the input form */ euclidian(dists, nbNeurons, WR, WG, WB, pickRGB); /* Determine the BMU */ choosen = arr_min_idx(dists, nbNeurons); choosen_x = (int)choosen % mapWidth; choosen_y = choosen / mapHeight; /* Compute the new neighbooring radius */ rad = som_radius(it, noEpoch, mapWidth, mapHeight); /* Find the BMU neighboors */ som_neighbourhood(neigh, choosen_x, choosen_y, rad, mapWidth, mapHeight); /* Compute the new learning rate */ eta = som_learning_rate(it, noEpoch); /* Compute new value of the network weight vectors */ compute_delta(deltaR, eta, neigh, nbNeurons, pickRGB[0], WR); compute_delta(deltaG, eta, neigh, nbNeurons, pickRGB[1], WG); compute_delta(deltaB, eta, neigh, nbNeurons, pickRGB[2], WB); /* Update the network weight vectors values */ arr_add(WR, deltaR, nbNeurons); arr_add(WG, deltaG, nbNeurons); arr_add(WB, deltaB, nbNeurons); arr_abs(absDeltaR, deltaR, nbNeurons); arr_abs(absDeltaG, deltaG, nbNeurons); arr_abs(absDeltaB, deltaB, nbNeurons); delta = arr_sum(absDeltaR, nbNeurons) + arr_sum(absDeltaG, nbNeurons) + arr_sum(absDeltaB, nbNeurons); it++; } res[0] = WR; res[1] = WG; res[2] = WB; free(dists); free(deltaR); free(deltaG); free(deltaB); free(absDeltaR); free(absDeltaG); free(absDeltaB); free(neigh); return SOM_OK; }
double arr_average(double *arr, int len) { return arr_sum(arr, len)/(double)len; }
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; }