Posted By

Scooter on 03/12/08


Tagged

math trigonometry


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

mmccrack
oso96_2000
silentbob84


Trigonometry Library


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com

Library of trigonometry functions, both circular and hyperbolic.

  1. <?php
  2. // PHP Mathematics Library - Trigonometry
  3. //
  4. // Copyright (c) 2008, reusablecode.blogspot.com; some rights reserved.
  5. //
  6. // This work is licensed under the Creative Commons Attribution License. To view
  7. // a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  8. // send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  9. // 94305, USA.
  10.  
  11. // CIRCULAR FUNCTIONS
  12.  
  13. // There are six basic trigonometric functions, three of which (sine, cosine, and tangent) are already defined in PHP.
  14. // function sin() - Returns the sine of the specified angle.
  15. // function cos() - Returns the cosine of the specified angle.
  16. // function tan() - Returns the tangent of the specified angle.
  17.  
  18. // The other three basic trigonometric functions are the multiplicative inverses of the previous three functions.
  19.  
  20. // Returns the cosecant of the specified angle.
  21. function csc($x)
  22. {
  23. return (1 / sin($x));
  24. }
  25.  
  26. // Returns the secant of the specified angle.
  27. function sec($x)
  28. {
  29. return (1 / cos($x));
  30. }
  31.  
  32. // Returns the cotangent of the specified angle.
  33. function cot($x)
  34. {
  35. return (1 / tan($x));
  36. }
  37.  
  38. // The following trigonometric functions are rarely used anymore, but are included in this function library for completeness.
  39.  
  40. // Returns the versed sine (also called versine) of the specified angle.
  41. function versin($x)
  42. {
  43. return (1 - cos($x));
  44. }
  45.  
  46. // Returns the coversed sine (also called coversine) of the specified angle.
  47. function coversin($x)
  48. {
  49. return (1 - sin($x));
  50. }
  51.  
  52. // Returns the haversed sine (also called haversine) of the specified angle.
  53. function haversin($x)
  54. {
  55. return (versin($x) / 2);
  56. }
  57.  
  58. // Returns the hacoversed sine (also called hacoversine, cohaversine, or havercosine) of the specified angle.
  59. function hacoversin($x)
  60. {
  61. return (coversin($x) / 2);
  62. }
  63.  
  64. // Returns the exsecant of the specified angle.
  65. function exsec($x)
  66. {
  67. return (sec($x) - 1);
  68. }
  69.  
  70. // Returns the excosecant of the specified angle.
  71. function excsc($x)
  72. {
  73. return (csc($x) - 1);
  74. }
  75.  
  76. // Each of the six basic trigonometric functions also has an inverse function, three of which are already defined in PHP.
  77. // function asin() - Returns the angle whose sine is the specified whole number.
  78. // function acos() - Returns the angle whose cosine is the specified number.
  79. // function atan() - Returns the angle whose tangent is the specified number.
  80.  
  81. // Returns the angle whose cosecant is the specified number.
  82. function acsc($x)
  83. {
  84. return (atan($x / sqrt($x * $x - 1)) + (sin($x) - 1) * (2 * atan(1)));
  85. }
  86.  
  87. // Returns the angle whose secant is the specified number.
  88. function asec($x)
  89. {
  90. return (atan($x / sqr($x * $x - 1)) + sin(($x) - 1) * (2 * atan(1)));
  91. }
  92.  
  93. // Returns the angle whose cotangent is the specified number.
  94. function acot($x)
  95. {
  96. return (atan($x) + 2 * atan(1));
  97. }
  98.  
  99. // HYPERBOLIC FUNCTIONS
  100.  
  101. // The hyperbolic functions are like the basic trigonometric functions, but for hyperbola instead of circles.
  102. // function sinh() - Returns the hyperbolic sine of the specified angle.
  103. // function cosh() - Returns the hyperbolic cosine of the specified angle.
  104. // function tanh() - Returns the hyperbolic tangent of the specified angle.
  105.  
  106. // Returns the hyperbolic cosecant of the specified angle.
  107. function csch($x)
  108. {
  109. return (2 / (exp($x) - exp(-$x)));
  110. }
  111.  
  112. // Returns the hyperbolic secant of the specified angle.
  113. function sech($x)
  114. {
  115. return (2 / (exp($x) + exp(-$x)));
  116. }
  117.  
  118. // Returns the hyperbolic cotangent of the specified angle.
  119. function coth($x)
  120. {
  121. return (exp($x) + exp(-$x)) / (exp($x) - exp(-$x));
  122. }
  123.  
  124. // Just like in trigonometry, the six basic hyperbolic functions have inverse functions.
  125. // function asinh() - Returns the angle whose hyperbolic sine is the specified number.
  126. // function acosh() - Returns the angle whose hyperbolic cosine is the specified number.
  127. // function atanh() - Returns the angle whose hyperbolic tangent is the specified number.
  128.  
  129. // Returns the angle whose hyperbolic cosecant is the specified number.
  130. function acsch($x)
  131. {
  132. return (log((sin($x) * sqrt($x * $x + 1) + 1) / $x));
  133. }
  134.  
  135. // Returns the angle whose hyperbolic secant is the specified number.
  136. function asech($x)
  137. {
  138. return (log((sqrt(-$x * $x + 1) + 1) / $x));
  139. }
  140.  
  141. // Returns the angle whose hyperbolic cotangent is the specified number.
  142. function acoth($x)
  143. {
  144. return (log(($x + 1) / ($x - 1)) / 2);
  145. }
  146.  
  147.  
  148. // GUDERMANNIAN FUNCTIONS
  149.  
  150. // The Gudermannian function relates the circular and hyperbolic trigonometric functions without resorting to complex numbers.
  151. function gd($x)
  152. {
  153. return (2 * atan(tanh($x / 2)));
  154. }
  155.  
  156. // Like other trigonometric and hyperbolic functions, Gudermannian has an inverse.
  157. function agd($x)
  158. {
  159. return atanh(sin($x));
  160. }
  161. ?>

Report this snippet  

You need to login to post a comment.