~ubuntu-branches/debian/experimental/eso-midas/experimental

« back to all changes in this revision

Viewing changes to libsrc/math/mutil.c

  • Committer: Package Import Robot
  • Author(s): Ole Streicher
  • Date: 2015-03-17 15:17:38 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20150317151738-04qxxeqm36oful9i
Tags: 15.02pl1.1-1~exp1
New upstream version

Show diffs side-by-side

added added

removed removed

Lines of Context:
255
255
#undef SWAP
256
256
 
257
257
#ifdef __STDC__
 
258
void heap_copy(int n, float arr[], float out[])
 
259
#else
 
260
void heap_copy (n, arr, out)
 
261
  float arr[];
 
262
  float out[];
 
263
  int   n;
 
264
#endif
 
265
/*              copy arrays         */
 
266
{
 
267
  int    j; 
 
268
  float  diff; 
 
269
  printf("Copying array of size %d\n",n); 
 
270
 
 
271
  for (j=0; j<n; j++)  out[j] = arr[j];
 
272
 
 
273
}
 
274
 
 
275
 
 
276
#ifdef __STDC__
 
277
int heap_compare(int n, float arr1[], float arr2[])
 
278
#else
 
279
int heap_compare (n, arr1, arr2)
 
280
  float arr1[];
 
281
  float arr2[]; 
 
282
  int   n;
 
283
#endif
 
284
/*              sort distribution (heapsort)           */
 
285
{
 
286
  int j;
 
287
  int check; 
 
288
 
 
289
  printf("Comparing arrays of size %d\n",n); 
 
290
 
 
291
  check = 0;
 
292
 
 
293
 
 
294
  for (j = 0; j < 4; j++) 
 
295
       printf("HEAPSORT: Array elements [%d] = %f %f\n",j,arr1[j],arr2[j]); 
 
296
  for (j = n-4; j < n; j++) 
 
297
       printf("HEAPSORT: Array elements [%d] = %f %f\n",j,arr1[j],arr2[j]); 
 
298
 
 
299
 
 
300
  for (j = 0; j < n; j++)
 
301
    {
 
302
      // printf("Index : %d\n",j);      
 
303
      if (arr1[j] != arr2[j]) { 
 
304
          check=1;
 
305
          printf("HEAPSORT: Array difference at index %d (%f, %f)\n",j,arr1[j],arr2[j]); 
 
306
      }
 
307
    }
 
308
  printf ("Comparison flag = %d\n",check); 
 
309
 
 
310
  return(check); 
 
311
}
 
312
 
 
313
#ifdef __STDC__
 
314
void heap_sort (int n, float arr[])
 
315
#else
 
316
void heap_sort (n, arr)
 
317
  float arr[];
 
318
  int   n;
 
319
#endif
 
320
/*              sort distribution (heapsort)           */
 
321
{
 
322
  int i, j, index;
 
323
  float a, *b;
 
324
 
 
325
  for (j = 1; j < n; j++)
 
326
    {
 
327
      a = arr[j];
 
328
      i = j - 1;
 
329
      while (i >= 0 && arr[i] > a)
 
330
        {
 
331
          arr[i + 1] = arr[i];
 
332
          i--;
 
333
        }
 
334
      arr[i + 1] = a;
 
335
    }
 
336
 
 
337
  return;
 
338
}
 
339
 
 
340
#ifdef __STDC__
258
341
float heap_median (int n, float arr[])
259
342
#else
260
343
float heap_median (n, arr)
264
347
/*              find median of distribution (heapsort)           */
265
348
{
266
349
  int i, j, index;
267
 
  float a, b[101];
268
 
 
269
 
  for (j = 0; j < n; j++)
270
 
    b[j] = arr[j];
271
 
 
272
 
  for (j = 1; j < n; j++)
273
 
    {
274
 
      a = b[j];
275
 
      i = j - 1;
276
 
      while (i >= 0 && b[i] > a)
277
 
        {
278
 
          b[i + 1] = b[i];
279
 
          i--;
280
 
        }
281
 
      b[i + 1] = a;
282
 
    }
 
350
  float a, *b, hmed;
 
351
  int   n2, n2p; 
 
352
 
 
353
  b = (float*) malloc ((size_t)((n)*sizeof (float)));
 
354
 
 
355
  heap_copy(n, arr, b); 
 
356
  // printf("heap_sort: Comparing copies\n"); 
 
357
  // heap_compare (n, arr, b); 
 
358
  heap_sort(n,b); 
 
359
 
283
360
  index = (n - 1) / 2;
284
 
  return(b[index]);
 
361
  hmed = b[index]; 
 
362
 
 
363
  // n2p=(n2=n/2)+1;
 
364
  // median = (n % 2 ? b[n2p] : 0.5*(b[n2]+b[n2p])); // b[(n-1)/2];
 
365
 
 
366
  free(b); 
 
367
  // printf("heap_sort: Median value %f\n",hmed); 
 
368
  return(hmed);
285
369
}
286
370
 
287
371