dem_main.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // expects each point on its own line with real positions.
  2. // expects first line to be "dim npoints reals"--from thiemard
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include "dem_discr.h"
  6. void usage()
  7. {
  8. fprintf(stderr, "Usage: dem_discr [dim npoints] [file]\n\nIf file not present, read from stdin. If dim, npoints not present, \nassume header '%%dim %%npoints reals' (e.g. '2 100 reals') in file.\n");
  9. }
  10. int main(int argc, char **argv)
  11. {
  12. int dim, npoints,i,j;
  13. FILE *pointfile;
  14. double **pointset, upper,lower;
  15. FILE *random;
  16. unsigned int seed;
  17. //random = fopen("/dev/random", "rb");
  18. //fread(&seed, 4, 1, random);
  19. //srand(seed);
  20. // sometimes /dev/random is really slow (at least on my computer)...
  21. // we do :
  22. srand(time(NULL)+getpid());
  23. switch (argc) {
  24. case 0:
  25. case 1: // nothing, or program name only: all from stdin
  26. i=scanf("%d %d reals\n", &dim, &npoints);
  27. if (i != 2) {
  28. fprintf(stderr, "stdin mode and header line not present\n");
  29. usage();
  30. exit(EXIT_FAILURE);
  31. }
  32. pointfile=stdin;
  33. break;
  34. case 2: // one arg, interpret as file name
  35. pointfile = fopen(argv[1], "r");
  36. if (!pointfile) {
  37. fprintf(stderr, "File open failed: %s\n", argv[3]);
  38. exit(EXIT_FAILURE);
  39. }
  40. i=fscanf(pointfile, "%d %d reals\n", &dim, &npoints);
  41. if (i != 2) {
  42. fprintf(stderr, "stdin mode and header line not present\n");
  43. exit(EXIT_FAILURE);
  44. }
  45. break;
  46. case 3: // interpret as dim npoints
  47. dim=atoi(argv[1]);
  48. npoints=atoi(argv[2]);
  49. pointfile=stdin;
  50. break;
  51. case 4: // interpret as dim npoints file; file not allowed to have header
  52. dim=atoi(argv[1]);
  53. npoints=atoi(argv[2]);
  54. pointfile = fopen(argv[3], "r");
  55. if (!pointfile) {
  56. fprintf(stderr, "File open failed: %s\n", argv[3]);
  57. exit(EXIT_FAILURE);
  58. }
  59. break;
  60. default:
  61. usage();
  62. exit(EXIT_FAILURE);
  63. }
  64. fprintf(stderr, "Reading dim %d npoints %d\n", dim, npoints);
  65. pointset = malloc(npoints*sizeof(double*));
  66. for (i=0; i<npoints; i++) {
  67. pointset[i] = malloc(dim*sizeof(double));
  68. for (j=0; j<dim; j++) {
  69. // newline counts as whitespace
  70. if (!fscanf(pointfile, "%lg ", &(pointset[i][j]))) {
  71. fprintf(stderr, "File does not contain enough data points!\n");
  72. exit(EXIT_FAILURE);
  73. }
  74. }
  75. }
  76. fprintf(stderr, "Calling discrepancy calculation\n");
  77. upper = oydiscr(pointset, dim, npoints, &lower);
  78. printf("%g\n", upper);
  79. return EXIT_SUCCESS;
  80. }