solvers.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <math.h>
  11. #include <pathplan/solvers.h>
  12. static int solve1(double *coeff, double *roots);
  13. static int solve2(double *coeff, double *roots);
  14. #ifndef M_PI
  15. #define M_PI 3.14159265358979323846
  16. #endif
  17. #define EPS 1E-7
  18. #define AEQ0(x) (((x) < EPS) && ((x) > -EPS))
  19. int solve3(double *coeff, double *roots)
  20. {
  21. double a, b, c, d;
  22. int rootn, i;
  23. double p, q, disc, b_over_3a, c_over_a, d_over_a;
  24. double r, theta, temp, alpha, beta;
  25. a = coeff[3], b = coeff[2], c = coeff[1], d = coeff[0];
  26. if (AEQ0(a))
  27. return solve2(coeff, roots);
  28. b_over_3a = b / (3 * a);
  29. c_over_a = c / a;
  30. d_over_a = d / a;
  31. p = b_over_3a * b_over_3a;
  32. q = 2 * b_over_3a * p - b_over_3a * c_over_a + d_over_a;
  33. p = c_over_a / 3 - p;
  34. disc = q * q + 4 * p * p * p;
  35. if (disc < 0) {
  36. r = .5 * sqrt(-disc + q * q);
  37. theta = atan2(sqrt(-disc), -q);
  38. temp = 2 * cbrt(r);
  39. roots[0] = temp * cos(theta / 3);
  40. roots[1] = temp * cos((theta + M_PI + M_PI) / 3);
  41. roots[2] = temp * cos((theta - M_PI - M_PI) / 3);
  42. rootn = 3;
  43. } else {
  44. alpha = .5 * (sqrt(disc) - q);
  45. beta = -q - alpha;
  46. roots[0] = cbrt(alpha) + cbrt(beta);
  47. if (disc > 0)
  48. rootn = 1;
  49. else
  50. roots[1] = roots[2] = -.5 * roots[0], rootn = 3;
  51. }
  52. for (i = 0; i < rootn; i++)
  53. roots[i] -= b_over_3a;
  54. return rootn;
  55. }
  56. static int solve2(double *coeff, double *roots)
  57. {
  58. double a, b, c;
  59. double disc, b_over_2a, c_over_a;
  60. a = coeff[2], b = coeff[1], c = coeff[0];
  61. if (AEQ0(a))
  62. return solve1(coeff, roots);
  63. b_over_2a = b / (2 * a);
  64. c_over_a = c / a;
  65. disc = b_over_2a * b_over_2a - c_over_a;
  66. if (disc < 0) {
  67. return 0;
  68. } else if (disc > 0) {
  69. roots[0] = -b_over_2a + sqrt(disc);
  70. roots[1] = -2 * b_over_2a - roots[0];
  71. return 2;
  72. }
  73. roots[0] = -b_over_2a;
  74. return 1;
  75. }
  76. static int solve1(double *coeff, double *roots)
  77. {
  78. double a, b;
  79. a = coeff[1], b = coeff[0];
  80. if (AEQ0(a)) {
  81. if (AEQ0(b))
  82. return 4;
  83. else
  84. return 0;
  85. }
  86. roots[0] = -b / a;
  87. return 1;
  88. }