PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/Math/TrigOp.php

https://bitbucket.org/adarshj/convenient_website
PHP | 206 lines | 90 code | 16 blank | 100 comment | 29 complexity | 90f73318a0aabb7aaa412ba188cf1c4d MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Jesus M. Castagnetto <jmcastagnetto@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: TrigOp.php,v 1.1 2002/11/24 07:16:24 jmcastagnetto Exp $
  20. //
  21. require_once 'PEAR.php';
  22. /**
  23. * Static class implementing supplementary trigonometric functions
  24. *
  25. * Example of use:
  26. *
  27. * $cot = Math_TrigOp::cot(0.3445);
  28. * $x = Math_TrigOp::acsch(-0.231);
  29. *
  30. * Originally this class was part of NumPHP (Numeric PHP package)
  31. *
  32. * @author Jesus M. Castagnetto <jmcastagnetto@php.net>
  33. * @version 1.0
  34. * @access public
  35. * @package Math_TrigOp
  36. */
  37. class Math_TrigOp {/*{{{*/
  38. // supplementary trigonometric functions
  39. /**
  40. * Calculates the secant of the parameter
  41. *
  42. * @param float $x
  43. * @returns mixed A floating point on success, PEAR_Error object otherwise
  44. * @access public
  45. */
  46. function sec($x) {/*{{{*/
  47. $x = floatval($x);
  48. $cos = cos($x);
  49. if ($cos == 0.0) {
  50. return PEAR::raiseError('Undefined operation, cosine of parameter is zero');
  51. } else {
  52. return 1/$cos;
  53. }
  54. }/*}}}*/
  55. /**
  56. * Calculates the cosecant of the parameter
  57. *
  58. * @param float $x
  59. * @returns mixed A floating point on success, PEAR_Error object otherwise
  60. * @access public
  61. */
  62. function csc($x) {/*{{{*/
  63. $x = floatval($x);
  64. $sin = sin($x);
  65. if ($sin == 0.0) {
  66. return PEAR::raiseError('Undefined operation, sine of parameter is zero');
  67. } else {
  68. return 1/$sin;
  69. }
  70. }/*}}}*/
  71. /**
  72. * Calculates the cotangent of the parameter
  73. *
  74. * @param float $x
  75. * @returns mixed A floating point on success, PEAR_Error object otherwise
  76. * @access public
  77. */
  78. function cot($x) {/*{{{*/
  79. $x = floatval($x);
  80. $tan = tan($x);
  81. if ($tan == 0.0) {
  82. return PEAR::raiseError('Undefined operation, tangent of parameter is zero');
  83. } else {
  84. return 1/$tan;
  85. }
  86. }/*}}}*/
  87. // Hyperbolic functions
  88. /**
  89. * Calculates the hyperbolic secant of the parameter
  90. *
  91. * @param float $x
  92. * @returns mixed A floating point on success, PEAR_Error object otherwise
  93. * @access public
  94. */
  95. function sech ($x) {/*{{{*/
  96. $x = floatval($x);
  97. $cosh = cosh($x);
  98. if ($cosh == 0.0) {
  99. return PEAR::raiseError('Undefined operation, hyperbolic cosine of parameter is zero');
  100. } else {
  101. return 1/$cosh;
  102. }
  103. }/*}}}*/
  104. /**
  105. * Calculates the hyperbolic cosecant of the parameter
  106. *
  107. * @param float $x
  108. * @returns mixed A floating point on success, PEAR_Error object otherwise
  109. * @access public
  110. */
  111. function csch ($x) {/*{{{*/
  112. $x = floatval($x);
  113. $sinh = sinh($x);
  114. if ($sinh == 0.0) {
  115. return PEAR::raiseError('Undefined operation, hyperbolic sine of parameter is zero');
  116. } else {
  117. return 1/$sinh;
  118. }
  119. }/*}}}*/
  120. /**
  121. * Calculates the hyperbolic cotangent of the parameter
  122. *
  123. * @param float $x
  124. * @returns mixed A floating point on success, PEAR_Error object otherwise
  125. * @access public
  126. */
  127. function coth ($x) {/*{{{*/
  128. $x = floatval($x);
  129. $tanh = tanh($x);
  130. if ($tanh == 0.0) {
  131. return PEAR::raiseError('Undefined operation, hyperbolic tangent of parameter is zero');
  132. } else {
  133. return 1/$tanh;
  134. }
  135. }/*}}}*/
  136. // Inverse hyperbolic functions
  137. /**
  138. * Calculates the inverse hyperbolic secant of the parameter
  139. *
  140. * @param float $x
  141. * @returns mixed A floating point on success, PEAR_Error object otherwise
  142. * @access public
  143. */
  144. function asech ($x) {/*{{{*/
  145. $x = floatval($x);
  146. if ($x == 0.0) {
  147. return PEAR::raiseError('Undefined operation, parameter is zero');
  148. } else {
  149. return log((1 + sqrt(1 - $x*$x)) / $x);
  150. }
  151. }/*}}}*/
  152. /**
  153. * Calculates the inverse hyperbolic cosecant of the parameter
  154. *
  155. * @param float $x
  156. * @returns mixed A floating point on success, PEAR_Error object otherwise
  157. * @access public
  158. */
  159. function acsch ($x) {/*{{{*/
  160. $x = floatval($x);
  161. if ($x == 0.0) {
  162. return PEAR::raiseError('Undefined operation, parameter is zero');
  163. } elseif ($x < 0) {
  164. return PEAR::raiseError('Undefined operation, parameter is negative');
  165. } else {
  166. return log((1 + sqrt(1 + $x*$x)) / $x);
  167. }
  168. }/*}}}*/
  169. /**
  170. * Calculates the inverse hyperbolic cotangent of the parameter
  171. *
  172. * @param float $x
  173. * @returns mixed A floating point on success, PEAR_Error object otherwise
  174. * @access public
  175. */
  176. function acoth ($x) {/*{{{*/
  177. $x = floatval($x);
  178. if ($x == 1.0) {
  179. return PEAR::raiseError('Undefined operation, parameter is 1.0');
  180. } else {
  181. $rat = ($x + 1)/($x - 1);
  182. if ($rat < 0) {
  183. return PEAR::raiseError('Undefined operation, (x+1)/(x-1) is negative');
  184. } else {
  185. return 0.5*log($rat);
  186. }
  187. }
  188. }/*}}}*/
  189. }/*}}}*/
  190. ?>