| 1 | ;; Trigonometry |
|---|
| 2 | ;; Liam Healy, Thu May 4 2006 - 22:58 |
|---|
| 3 | ;; Time-stamp: <2008-02-17 18:30:19EST trigonometry.lisp> |
|---|
| 4 | ;; $Id$ |
|---|
| 5 | |
|---|
| 6 | (in-package :gsl) |
|---|
| 7 | |
|---|
| 8 | ;;;;**************************************************************************** |
|---|
| 9 | ;;;; Circular Trigonometric Functions |
|---|
| 10 | ;;;;**************************************************************************** |
|---|
| 11 | |
|---|
| 12 | (defgeneric gsl-sin (x) |
|---|
| 13 | (:documentation "The sine function sin(x).")) |
|---|
| 14 | |
|---|
| 15 | (defgeneric gsl-cos (x) |
|---|
| 16 | (:documentation "The cosine function cos(x).")) |
|---|
| 17 | |
|---|
| 18 | (defmfun gsl-sin ((x float)) |
|---|
| 19 | "gsl_sf_sin_e" ((x :double) (ret sf-result)) |
|---|
| 20 | :type :method |
|---|
| 21 | :export t) |
|---|
| 22 | |
|---|
| 23 | (defmfun gsl-sin ((x complex)) |
|---|
| 24 | "gsl_sf_complex_sin_e" |
|---|
| 25 | (((realpart x) :double) ((imagpart x) :double) |
|---|
| 26 | (re-ret sf-result) (im-ret sf-result)) |
|---|
| 27 | :type :method |
|---|
| 28 | :return ((complex (val re-ret) (val im-ret)) |
|---|
| 29 | (complex (err re-ret) (err im-ret)))) |
|---|
| 30 | |
|---|
| 31 | (defmfun gsl-cos ((x float)) |
|---|
| 32 | "gsl_sf_cos_e" ((x :double) (ret sf-result)) |
|---|
| 33 | :type :method |
|---|
| 34 | :export t) |
|---|
| 35 | |
|---|
| 36 | (defmfun gsl-cos ((x complex)) |
|---|
| 37 | "gsl_sf_complex_cos_e" |
|---|
| 38 | (((realpart x) :double) ((imagpart x) :double) |
|---|
| 39 | (re-ret sf-result) (im-ret sf-result)) |
|---|
| 40 | :type :method |
|---|
| 41 | :return ((complex (val re-ret) (val im-ret)) |
|---|
| 42 | (complex (err re-ret) (err im-ret)))) |
|---|
| 43 | |
|---|
| 44 | (defmfun hypotenuse (x y) |
|---|
| 45 | "gsl_sf_hypot_e" ((x :double) (y :double) (ret sf-result)) |
|---|
| 46 | :documentation ; FDL |
|---|
| 47 | "The hypotenuse function sqrt{x^2 + y^2}.") |
|---|
| 48 | |
|---|
| 49 | (defmfun sinc (x) |
|---|
| 50 | "gsl_sf_sinc_e" ((x :double) (ret sf-result)) |
|---|
| 51 | :documentation ; FDL |
|---|
| 52 | "sinc(x) = sin(pi x) / (pi x)}") |
|---|
| 53 | |
|---|
| 54 | (defmfun log-sin (x) |
|---|
| 55 | "gsl_sf_complex_logsin_e" |
|---|
| 56 | (((realpart x) :double) ((imagpart x) :double) |
|---|
| 57 | (re-ret sf-result) (im-ret sf-result)) |
|---|
| 58 | :documentation ; FDL |
|---|
| 59 | "This function computes the logarithm of the complex sine, |
|---|
| 60 | \log(\sin(z_r + i z_i)) storing the real and imaginary parts in |
|---|
| 61 | szr, szi." |
|---|
| 62 | :return ((complex (val re-ret) (val im-ret)) |
|---|
| 63 | (complex (err re-ret) (err im-ret)))) |
|---|
| 64 | |
|---|
| 65 | ;;;;**************************************************************************** |
|---|
| 66 | ;;;; Hyperbolic Trigonometric Functions |
|---|
| 67 | ;;;;**************************************************************************** |
|---|
| 68 | |
|---|
| 69 | (defmfun log-sinh (x) |
|---|
| 70 | "gsl_sf_lnsinh_e" ((x :double) (ret sf-result)) |
|---|
| 71 | :documentation ; FDL |
|---|
| 72 | "Logarithm of sinh function, special functions |
|---|
| 73 | These routines compute log(\sinh(x)) for x > 0.") |
|---|
| 74 | |
|---|
| 75 | (defmfun log-cosh (x) |
|---|
| 76 | "gsl_sf_lncosh_e" ((x :double) (ret sf-result)) |
|---|
| 77 | :documentation ; FDL |
|---|
| 78 | "Logarithm of cosh function, special functions |
|---|
| 79 | These routines compute log(cosh(x)) for any x.") |
|---|
| 80 | |
|---|
| 81 | ;;;;**************************************************************************** |
|---|
| 82 | ;;;; Conversion Functions |
|---|
| 83 | ;;;;**************************************************************************** |
|---|
| 84 | |
|---|
| 85 | (defmfun polar-to-rectangular (r theta) |
|---|
| 86 | "gsl_sf_polar_to_rect" |
|---|
| 87 | ((r :double) (theta :double) (x sf-result) (y sf-result)) |
|---|
| 88 | :return ((val x) (val y) (err x) (err y)) |
|---|
| 89 | :documentation ; FDL |
|---|
| 90 | "Convert the polar coordinates (r, theta) to |
|---|
| 91 | rectilinear coordinates (x, y), x = r\cos(\theta), y = r\sin(\theta).") |
|---|
| 92 | |
|---|
| 93 | (defmfun rectangular-to-polar (x y) |
|---|
| 94 | "gsl_sf_rect_to_polar" |
|---|
| 95 | ((x :double) (y :double) (r sf-result) (theta sf-result)) |
|---|
| 96 | :return ((val r) (val theta) (err r) (err theta)) |
|---|
| 97 | :documentation ; FDL |
|---|
| 98 | "Convert the rectilinear coordinates (x, y) to |
|---|
| 99 | polar coordinates (r, theta), such that x = |
|---|
| 100 | r cos(theta)}, y = r sin(theta). The argument theta |
|---|
| 101 | lies in the range [-\pi, \pi].") |
|---|
| 102 | |
|---|
| 103 | ;;;;**************************************************************************** |
|---|
| 104 | ;;;; Restriction Functions |
|---|
| 105 | ;;;;**************************************************************************** |
|---|
| 106 | |
|---|
| 107 | (defmfun restrict-symmetric (theta) |
|---|
| 108 | "gsl_sf_angle_restrict_symm" ((theta :double)) |
|---|
| 109 | :c-return :double |
|---|
| 110 | :documentation ; FDL |
|---|
| 111 | "Force the angle theta to lie in the range (-\pi,\pi].") |
|---|
| 112 | |
|---|
| 113 | (defmfun restrict-positive (theta) |
|---|
| 114 | "gsl_sf_angle_restrict_pos" ((theta :double)) |
|---|
| 115 | :c-return :double |
|---|
| 116 | :documentation ; FDL |
|---|
| 117 | "Force the angle theta to lie in the range [0,2\pi).") |
|---|
| 118 | |
|---|
| 119 | ;;;;**************************************************************************** |
|---|
| 120 | ;;;; Trigonometric Functions With Error Estimates |
|---|
| 121 | ;;;;**************************************************************************** |
|---|
| 122 | |
|---|
| 123 | (defmfun sin-err (x dx) |
|---|
| 124 | "gsl_sf_sin_err_e" ((x :double) (dx :double) (ret sf-result)) |
|---|
| 125 | :documentation ; FDL |
|---|
| 126 | "Compute the sine of an angle x with |
|---|
| 127 | an associated absolute error dx, sin(x \pm dx).") |
|---|
| 128 | |
|---|
| 129 | (defmfun cos-err (x dx) |
|---|
| 130 | "gsl_sf_cos_err_e" ((x :double) (dx :double) (ret sf-result)) |
|---|
| 131 | :documentation ; FDL |
|---|
| 132 | "The cosine of an angle x with an associated |
|---|
| 133 | absolute error dx, cos(x \pm dx).") |
|---|
| 134 | |
|---|
| 135 | ;;;;**************************************************************************** |
|---|
| 136 | ;;;; Examples and unit test |
|---|
| 137 | ;;;;**************************************************************************** |
|---|
| 138 | |
|---|
| 139 | #| |
|---|
| 140 | (make-tests trigonometry |
|---|
| 141 | (gsl-sin 1.0d0) |
|---|
| 142 | (gsl-sin #C(1.0d0 1.0d0)) |
|---|
| 143 | (gsl-cos 1.0d0) |
|---|
| 144 | (gsl-cos #C(1.0d0 1.0d0)) |
|---|
| 145 | (hypotenuse 1.0d0 2.0d0) |
|---|
| 146 | (sinc 0.5d0) |
|---|
| 147 | (log-sin #C(1.0d0 1.0d0)) |
|---|
| 148 | (log-sinh 0.5d0) |
|---|
| 149 | (log-cosh 0.5d0) |
|---|
| 150 | (polar-to-rectangular 2.0d0 1.0d0) |
|---|
| 151 | (rectangular-to-polar 2.0d0 1.0d0) |
|---|
| 152 | (restrict-symmetric 5.0d0) |
|---|
| 153 | (restrict-positive -1.0d0) |
|---|
| 154 | (sin-err 0.5d0 0.01d0) |
|---|
| 155 | (cos-err 0.5d0 0.01d0)) |
|---|
| 156 | |# |
|---|
| 157 | |
|---|
| 158 | (LISP-UNIT:DEFINE-TEST TRIGONOMETRY |
|---|
| 159 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 160 | (LIST 0.8414709848078965d0 3.736881847550928d-16) |
|---|
| 161 | (MULTIPLE-VALUE-LIST (GSL-SIN 1.0d0))) |
|---|
| 162 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 163 | (LIST #C(1.2984575814159773d0 0.6349639147847361d0) |
|---|
| 164 | #C(5.766310013548447d-16 2.8198062320005596d-16)) |
|---|
| 165 | (MULTIPLE-VALUE-LIST (GSL-SIN #C(1.0d0 1.0d0)))) |
|---|
| 166 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 167 | (LIST 0.5403023058681398d0 2.3994242409314904d-16) |
|---|
| 168 | (MULTIPLE-VALUE-LIST (GSL-COS 1.0d0))) |
|---|
| 169 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 170 | (LIST #C(0.8337300251311491d0 -0.9888977057628651d0) |
|---|
| 171 | #C(3.7025050808876487d-16 4.3915880077477046d-16)) |
|---|
| 172 | (MULTIPLE-VALUE-LIST (GSL-COS #C(1.0d0 1.0d0)))) |
|---|
| 173 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 174 | (LIST 2.23606797749979d0 9.930136612989092d-16) |
|---|
| 175 | (MULTIPLE-VALUE-LIST (HYPOTENUSE 1.0d0 2.0d0))) |
|---|
| 176 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 177 | (LIST 0.6366197723675814d0 3.0072729231305663d-16) |
|---|
| 178 | (MULTIPLE-VALUE-LIST (SINC 0.5d0))) |
|---|
| 179 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 180 | (LIST #C(0.3683837314249251d0 0.4548202333099499d0) |
|---|
| 181 | #C(1.6359524021011266d-16 8.179762010505633d-17)) |
|---|
| 182 | (MULTIPLE-VALUE-LIST (LOG-SIN #C(1.0d0 1.0d0)))) |
|---|
| 183 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 184 | (LIST -0.6518223259470272d0 2.8946726169244526d-16) |
|---|
| 185 | (MULTIPLE-VALUE-LIST (LOG-SINH 0.5d0))) |
|---|
| 186 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 187 | (LIST 0.12011450695827751d0 4.401938023864669d-17) |
|---|
| 188 | (MULTIPLE-VALUE-LIST (LOG-COSH 0.5d0))) |
|---|
| 189 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 190 | (LIST 1.0806046117362795d0 1.682941969615793d0 |
|---|
| 191 | 8.535730329413909d-16 9.873187936033346d-16) |
|---|
| 192 | (MULTIPLE-VALUE-LIST |
|---|
| 193 | (POLAR-TO-RECTANGULAR 2.0d0 1.0d0))) |
|---|
| 194 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 195 | (LIST 2.23606797749979d0 0.4636476090008061d0 |
|---|
| 196 | 9.930136612989092d-16 2.0590090033003876d-16) |
|---|
| 197 | (MULTIPLE-VALUE-LIST |
|---|
| 198 | (RECTANGULAR-TO-POLAR 2.0d0 1.0d0))) |
|---|
| 199 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 200 | (LIST -1.2831853071795862d0) |
|---|
| 201 | (MULTIPLE-VALUE-LIST (RESTRICT-SYMMETRIC 5.0d0))) |
|---|
| 202 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 203 | (LIST 5.283185307179586d0) |
|---|
| 204 | (MULTIPLE-VALUE-LIST (RESTRICT-POSITIVE -1.0d0))) |
|---|
| 205 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 206 | (LIST 0.479425538604203d0 0.008775825618904047d0) |
|---|
| 207 | (MULTIPLE-VALUE-LIST (SIN-ERR 0.5d0 0.01d0))) |
|---|
| 208 | (LISP-UNIT::ASSERT-NUMERICAL-EQUAL |
|---|
| 209 | (LIST 0.8775825618903728d0 0.004794255386042614d0) |
|---|
| 210 | (MULTIPLE-VALUE-LIST (COS-ERR 0.5d0 0.01d0)))) |
|---|