You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

88 lines
1.8 KiB

  1. //
  2. // LolRemez - Remez algorithm implementation
  3. //
  4. // Copyright © 2005-2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This program is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #if HAVE_CONFIG_H
  13. # include "config.h"
  14. #endif
  15. #include <lol/engine.h>
  16. #include <lol/math/real.h>
  17. #include "solver.h"
  18. #include "expression.h"
  19. using lol::real;
  20. using lol::String;
  21. void FAIL()
  22. {
  23. printf("Usage:\n");
  24. printf(" lolremez [-d degree] [-i xmin xmax] x-expression [x-error]\n");
  25. printf("Example:\n");
  26. printf(" lolremez -d 4 -i -1 1 \"atan(exp(1+x))\"\n");
  27. printf(" lolremez -d 4 -i -1 1 \"atan(exp(1+x))\" \"exp(1+x)\"\n");
  28. exit(EXIT_FAILURE);
  29. }
  30. /* See the tutorial at http://lolengine.net/wiki/doc/maths/remez */
  31. int main(int argc, char **argv)
  32. {
  33. char const *xmin = "-1", *xmax = "1";
  34. char const *f = nullptr, *g = nullptr;
  35. int degree = 4;
  36. for (int i = 1; i < argc; ++i)
  37. {
  38. if (argv[i] == String("-d"))
  39. {
  40. if (i + 1 >= argc)
  41. FAIL();
  42. degree = atoi(argv[++i]);
  43. }
  44. else if (argv[i] == String("-i"))
  45. {
  46. if (i + 2 >= argc)
  47. FAIL();
  48. xmin = argv[++i];
  49. xmax = argv[++i];
  50. i += 2;
  51. }
  52. else if (g)
  53. {
  54. FAIL();
  55. }
  56. else if (f)
  57. {
  58. g = argv[i];
  59. }
  60. else
  61. {
  62. f = argv[i];
  63. }
  64. }
  65. if (!f || real(xmin) >= real(xmax))
  66. {
  67. FAIL();
  68. }
  69. RemezSolver solver(degree + 1, 20);
  70. solver.Run(xmin, xmax, f, g);
  71. return 0;
  72. }