simple_main.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "simple_discr.h"
  6. void usage()
  7. {
  8. fprintf(stderr, "Usage: simple_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;
  15. switch (argc) {
  16. case 0:
  17. case 1: // nothing, or program name only: all from stdin
  18. i=scanf("%d %d reals\n", &dim, &npoints);
  19. if (i != 2) {
  20. fprintf(stderr, "stdin mode and header line not present\n");
  21. usage();
  22. exit(EXIT_FAILURE);
  23. }
  24. pointfile=stdin;
  25. break;
  26. case 2: // one arg, interpret as file name
  27. pointfile = fopen(argv[1], "r");
  28. i=fscanf(pointfile, "%d %d reals\n", &dim, &npoints);
  29. if (i != 2) {
  30. fprintf(stderr, "stdin mode and header line not present\n");
  31. exit(EXIT_FAILURE);
  32. }
  33. break;
  34. case 3: // interpret as dim npoints
  35. dim=atoi(argv[1]);
  36. npoints=atoi(argv[2]);
  37. pointfile=stdin;
  38. break;
  39. case 4: // interpret as dim npoints file; file not allowed to have header
  40. dim=atoi(argv[1]);
  41. npoints=atoi(argv[2]);
  42. pointfile = fopen(argv[3], "r");
  43. break;
  44. default:
  45. usage();
  46. exit(EXIT_FAILURE);
  47. }
  48. fprintf(stderr, "Reading dim %d npoints %d\n", dim, npoints);
  49. pointset = malloc(npoints*sizeof(double*));
  50. for (i=0; i<npoints; i++) {
  51. pointset[i] = malloc(dim*sizeof(double));
  52. for (j=0; j<dim; j++) {
  53. fscanf(pointfile, "%lg ", &(pointset[i][j]));
  54. // newline counts as whitespace
  55. }
  56. }
  57. fprintf(stderr, "Calling xdiscr calculation\n");
  58. printf("%g\n", exact_discr(pointset, npoints, dim));
  59. return EXIT_SUCCESS;
  60. }