Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,15 @@ Planned additional functionality, but still to implement
* coth (hyperbolic cotangent)
* acoth (inverse hyperbolic cotangent)
* pow (power)


---
Functions added
- tanh (hyperbolic tangent)
- atanh (inverse hyperbolic tangent)
- coth (hyperbolic cotangent)
- acoth (inverse hyperbolic cotangent)
- gudermannian () Hyperbolic
- inversegudermannian () Hyperbolic
- haversine () Trigonometric
- inversehaversine () Trigonometric

36 changes: 36 additions & 0 deletions classes/src/functions/acoth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
*
* Function code for the complex acoth() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
*
* Returns the inverse hyperbolic cotangent of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float The inverse hyperbolic cotangent of the complex argument.
* @throws \Exception f argument isn't a valid real or complex number.
*/
function acoth( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
if ( ( $complex->getReal() == -1 ) || ( $complex->getReal() == 1 ) ) {
return INF;
} else {
$calc = clone $complex;
$complex = $complex->add( 1 );
$calc = $calc->subtract( 1 );
$complex = $complex->divideBy( $calc );
$complex = ln( $complex );
$complex = $complex->divideBy( 2 );
return $complex;
}
}
}
42 changes: 42 additions & 0 deletions classes/src/functions/atanh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
*
* Function code for the complex atanh() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
*
* Returns the inverse hyperbolic tangent of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float The inverse hyperbolic tangent of the complex argument.
* @throws \Exception f argument isn't a valid real or complex number.
*/
function atanh( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
if ( ( $complex->getReal() == -1 ) || ( $complex->getReal() == 1 ) ){
return INF;
} else {
$calc = clone $complex;

$complex->add( 1 );
$complex = ln( $complex );

$calc = $calc->multiply( -1 );
$calc = $calc->add( 1 );
$calc = ln( $calc );

$complex = $complex->subtract( $calc );
$complex = $complex->divideBy( 2 );

return $complex;
}
}
}
35 changes: 35 additions & 0 deletions classes/src/functions/coth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
*
* Function code for the complex coth() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
*
* Returns the hyperbolic cotangent of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float|null The hyperbolic cotangent of the complex argument.
* @throws \Exception f argument isn't a valid real or complex number.
*/
function coth( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
$calc = clone $complex;
$complex = cosh( $complex );
$calc = sinh( $calc );

if ( $calc->getReal() != 0 ) {
$complex = $complex->divideBy( $calc );
return $complex;
} else {
return null;
}
}
}
30 changes: 30 additions & 0 deletions classes/src/functions/gudermannian.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
*
* Function code for the complex gudermannian() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
*
* Returns the gudermannian of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float The gudermannian of the complex argument.
* @throws \Exception f argument isn't a valid real or complex number.
*/
function gudermannian( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
$complex = exp( $complex );
$complex = atan( $complex );
$complex = $complex->multiply( 2 );
$complex = $complex->add( -( M_PI/2 ) );
return $complex;
}
}
29 changes: 29 additions & 0 deletions classes/src/functions/haversine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
*
* Function code for the complex haversine() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
*
* Returns the haversine function of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float The haversine of the complex argument.
* @throws \Exception f argument isn't a valid real or complex number.
*/
function haversine( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
$complex = $complex->divideBy( 2 );
$complex = sin( $complex );
$complex = $complex->multiply( $complex );
return $complex;
}
}
30 changes: 30 additions & 0 deletions classes/src/functions/inversegudermannian.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
*
* Function code for the complex inversegudermannian() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
*
* Returns the inverse gudermannian of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float The inverse gudermannian of the complex argument.
* @throws \Exception f argument isn't a valid real or complex number.
*/
function inversegudermannian( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
$complex = $complex->divideBy( 2 );
$complex = $complex->add( M_PI/4 );
$complex = tan( $complex );
$complex = ln( $complex );
return $complex;
}
}
31 changes: 31 additions & 0 deletions classes/src/functions/inversehaversine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
*
* Function code for the complex inversehaversine() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
*
* Returns the inverse haversine of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float The inverse haversine of the complex argument.
* @throws \Exception f argument isn't a valid real or complex number.
*/
function inversehaversine( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
if ( ( $complex->getReal() >= 0.0 ) && ( $complex->getReal() <= 1.0 ) ) {
$complex = sqrt( $complex );
$complex = asin( $complex );
$complex = $complex->multiply( 2 );
return $complex;
}
}
}
28 changes: 28 additions & 0 deletions classes/src/functions/tanh.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
*
* Function code for the complex tanh() function
*
* @author Michael A. Johnson
* @License http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
*/
namespace Complex;

/**
* Returns the hyperbolic tangent of a complex number.
*
* @param $complex Complex number or a numeric value.
* @return Complex|float The hyperbolic tangent of the complex argument.
*/
function tanh( $complex )
{
$complex = Complex::validateComplexArgument( $complex );

if ( $complex->getImaginary() == 0.0 ) {
$calc = clone $complex;
$complex = sinh( $complex );
$calc = cosh( $calc );
$complex = $complex->divideBy( $calc );
return $complex;
}
}
18 changes: 13 additions & 5 deletions examples/complexTest2.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

include('../classes/Bootstrap.php');

echo 'Function Examples', PHP_EOL;
echo "<h3>Function Examples</h3><br/>", PHP_EOL;

$functions = [
'abs',
Expand Down Expand Up @@ -33,7 +33,15 @@
'sin',
'sinh',
'sqrt',
'theta'
'theta',
'acoth',
'coth',
'gudermannian',
'haversine',
'inversegudermannian',
'inversehaversine',
'tanh',
'atanh'
];

for($real = -3.5; $real <= 3.5; $real += 0.5) {
Expand All @@ -42,11 +50,11 @@
$complexFunction = __NAMESPACE__ . '\\' . $function;
$complex = new Complex($real, $imaginary);
try {
echo $function, '(', $complex, ') = ', $complexFunction($complex), PHP_EOL;
echo $function, '(', $complex, ') = ', $complexFunction($complex), "<br/>",PHP_EOL;
} catch(\Exception $e) {
echo $function, '(', $complex, ') ERROR: ', $e->getMessage(), PHP_EOL;
echo $function, '(', $complex, ') ERROR: ', $e->getMessage(), "<br/>", PHP_EOL;
}
}
echo PHP_EOL;
echo "<br/>",PHP_EOL;
}
}
64 changes: 64 additions & 0 deletions examples/complexTest3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
namespace Complex;
include('../classes/Bootstrap.php');

$complexFunction1 = __NAMESPACE__ . '\\' . 'cos';
$complexFunction2 = __NAMESPACE__ . '\\' . 'haversine';
$complexFunction3 = __NAMESPACE__ . '\\' . 'inversehaversine';
$complexFunction4 = __NAMESPACE__ . '\\' . 'gudermannian';
$complexFunction5 = __NAMESPACE__ . '\\' . 'inversegudermannian';
$complexFunction6 = __NAMESPACE__ . '\\' . 'coth';
$complexFunction7 = __NAMESPACE__ . '\\' . 'acoth';
$complexFunction8 = __NAMESPACE__ . '\\' . 'tanh';
$complexFunction9 = __NAMESPACE__ . '\\' . 'atanh';


echo $complexFunction1( 9 ) . '<br/>' . PHP_EOL;

echo "<h3>Haversine</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction2( $i ) . '<br/>' . PHP_EOL;
}
unset($i);

echo "<h3>Inverse haversine</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction3( $i ) . '<br/>' . PHP_EOL;
}
unset($i);

echo "<h3>Gudermannian</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction4( $i ) . '<br/>' . PHP_EOL;
}
unset($i);

echo "<h3>Inverse Gudermannian</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction5( $i ) . '<br/>' . PHP_EOL;
}
unset($i);

echo "<h3>Coth</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction6( $i ) . '<br/>' . PHP_EOL;
}
unset($i);

echo "<h3>ArcCoth</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction7( $i ) . '<br/>' . PHP_EOL;
}
unset($i);

echo "<h3>Tanh</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction8( $i ) . '<br/>' . PHP_EOL;
}
unset($i);

echo "<h3>ArcTanh</h3><br/>" . PHP_EOL;
for ( $i = -10; $i <= 10; $i++ ) {
echo ( $i ) . ': ' . $complexFunction9( $i ) . '<br/>' . PHP_EOL;
}
unset($i);