abs.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Absolute value, aka vector magnitude
  2. #include "stdafx.h"
  3. #include "defs.h"
  4. void
  5. eval_abs(void)
  6. {
  7. push(cadr(p1));
  8. eval();
  9. absval();
  10. }
  11. void
  12. absval(void)
  13. {
  14. int h;
  15. save();
  16. p1 = pop();
  17. if (istensor(p1)) {
  18. absval_tensor();
  19. restore();
  20. return;
  21. }
  22. if (isnum(p1)) {
  23. push(p1);
  24. if (isnegativenumber(p1))
  25. negate();
  26. restore();
  27. return;
  28. }
  29. if (iscomplexnumber(p1)) {
  30. push(p1);
  31. push(p1);
  32. conjugate();
  33. multiply();
  34. push_rational(1, 2);
  35. power();
  36. restore();
  37. return;
  38. }
  39. // abs(1/a) evaluates to 1/abs(a)
  40. if (car(p1) == symbol(POWER) && isnegativeterm(caddr(p1))) {
  41. push(p1);
  42. reciprocate();
  43. absval();
  44. reciprocate();
  45. restore();
  46. return;
  47. }
  48. // abs(a*b) evaluates to abs(a)*abs(b)
  49. if (car(p1) == symbol(MULTIPLY)) {
  50. h = tos;
  51. p1 = cdr(p1);
  52. while (iscons(p1)) {
  53. push(car(p1));
  54. absval();
  55. p1 = cdr(p1);
  56. }
  57. multiply_all(tos - h);
  58. restore();
  59. return;
  60. }
  61. if (isnegativeterm(p1) || (car(p1) == symbol(ADD) && isnegativeterm(cadr(p1)))) {
  62. push(p1);
  63. negate();
  64. p1 = pop();
  65. }
  66. push_symbol(ABS);
  67. push(p1);
  68. list(2);
  69. restore();
  70. }
  71. void
  72. absval_tensor(void)
  73. {
  74. if (p1->u.tensor->ndim != 1)
  75. stop("abs(tensor) with tensor rank > 1");
  76. push(p1);
  77. push(p1);
  78. conjugate();
  79. inner();
  80. push_rational(1, 2);
  81. power();
  82. simplify();
  83. eval();
  84. }