Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

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