25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

90 lines
1.9 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("\n");
  26. printf("Example:\n");
  27. printf(" lolremez -d 4 -i -1 1 \"atan(exp(1+x))\"\n");
  28. printf(" lolremez -d 4 -i -1 1 \"atan(exp(1+x))\" \"exp(1+x)\"\n");
  29. printf("\n");
  30. exit(EXIT_FAILURE);
  31. }
  32. /* See the tutorial at http://lolengine.net/wiki/doc/maths/remez */
  33. int main(int argc, char **argv)
  34. {
  35. char const *xmin = "-1", *xmax = "1";
  36. char const *f = nullptr, *g = nullptr;
  37. int degree = 4;
  38. for (int i = 1; i < argc; ++i)
  39. {
  40. if (argv[i] == String("-d"))
  41. {
  42. if (i + 1 >= argc)
  43. FAIL();
  44. degree = atoi(argv[++i]);
  45. }
  46. else if (argv[i] == String("-i"))
  47. {
  48. if (i + 2 >= argc)
  49. FAIL();
  50. xmin = argv[++i];
  51. xmax = argv[++i];
  52. i += 2;
  53. }
  54. else if (g)
  55. {
  56. FAIL();
  57. }
  58. else if (f)
  59. {
  60. g = argv[i];
  61. }
  62. else
  63. {
  64. f = argv[i];
  65. }
  66. }
  67. if (!f || real(xmin) >= real(xmax))
  68. {
  69. FAIL();
  70. }
  71. remez_solver solver(degree, 20);
  72. solver.run(xmin, xmax, f, g);
  73. return 0;
  74. }