-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
66 changed files
with
15,680 additions
and
11,438 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
namespace Rindow\Math\Matrix; | ||
|
||
use Interop\Polite\Math\Matrix\NDArray; | ||
|
||
function C( | ||
float $r=null, | ||
float $i=null, | ||
) : Complex | ||
{ | ||
$r = $r ?? 0.0; | ||
$i = $i ?? 0.0; | ||
return new Complex($r,$i); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
namespace Rindow\Math\Matrix; | ||
|
||
// typedef struct { float real, imag; } openblas_complex_float; | ||
|
||
class Complex | ||
{ | ||
public function __construct( | ||
public readonly float $real, | ||
public readonly float $imag, | ||
) {} | ||
|
||
public function __toString() : string | ||
{ | ||
$and = ($this->imag>=0)?'+':''; | ||
return "{$this->real}{$and}{$this->imag}i"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
namespace Rindow\Math\Matrix; | ||
|
||
require_once __DIR__.'/C.php'; | ||
//use function Rindow\Math\Matrix\C; | ||
use Interop\Polite\Math\Matrix\NDArray; | ||
|
||
trait ComplexUtils | ||
{ | ||
protected function cbuild(float $r=null,float $i=null) : object | ||
{ | ||
return C($r,i:$i); | ||
} | ||
|
||
protected function crebuild(object $value) : object | ||
{ | ||
return C($value->real,i:$value->imag); | ||
} | ||
|
||
protected function cisobject(mixed $value) : bool | ||
{ | ||
if(!is_object($value)) { | ||
return false; | ||
} | ||
return (property_exists($value,'real') && | ||
property_exists($value,'imag') ); | ||
} | ||
|
||
protected function cobjecttype(mixed $value) : string | ||
{ | ||
if(is_object($value)) { | ||
return get_class($value); | ||
} | ||
return gettype($value); | ||
} | ||
|
||
protected function cistype(int $dtype=null) : bool | ||
{ | ||
return $dtype==NDArray::complex64||$dtype==NDArray::complex128; | ||
} | ||
|
||
protected function ciszero(object $value) : bool | ||
{ | ||
return $value->real==0 && $value->imag==0; | ||
} | ||
|
||
protected function cisone(object $value) : bool | ||
{ | ||
return $value->real==1 && $value->imag==0; | ||
} | ||
|
||
protected function cabs(object $value) : float | ||
{ | ||
return sqrt($value->real*$value->real + $value->imag*$value->imag); | ||
} | ||
|
||
protected function cconj(object $value) : object | ||
{ | ||
return C($value->real,i:-$value->imag); | ||
} | ||
|
||
protected function cadd(object $x,object $y) : object | ||
{ | ||
return C( | ||
($x->real+$y->real), | ||
i:($x->imag+$y->imag) | ||
); | ||
} | ||
|
||
protected function csub(object $x,object $y) : object | ||
{ | ||
return C( | ||
($x->real-$y->real), | ||
i:($x->imag-$y->imag) | ||
); | ||
} | ||
|
||
protected function cmul(object $x,object $y) : object | ||
{ | ||
return C( | ||
(($x->real*$y->real)-($x->imag*$y->imag)), | ||
i:(($x->real*$y->imag)+($x->imag*$y->real)) | ||
); | ||
} | ||
|
||
protected function cdiv(object $x,object $y) : object | ||
{ | ||
$denominator = $y->real * $y->real + $y->imag * $y->imag; | ||
if($denominator==0) { | ||
return C(NAN,i:NAN); | ||
} | ||
$real = (($x->real*$y->real)+($x->imag*$y->imag))/$denominator; | ||
$imag = (($x->imag*$y->real)-($x->real*$y->imag))/$denominator; | ||
return C($real,i:$imag); | ||
} | ||
|
||
protected function csqrt(object $x) : object | ||
{ | ||
$r = sqrt($x->real*$x->real + $x->imag*$x->imag); | ||
$theta = atan2($x->imag, $x->real) / 2.0; | ||
return C( | ||
(sqrt($r)*cos($theta)), | ||
i:(sqrt($r)*sin($theta)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
namespace Rindow\Math\Matrix\Drivers; | ||
|
||
use LogicException; | ||
|
||
abstract class AbstractDriver implements Driver | ||
{ | ||
protected string $LOWEST_VERSION = '1000.1000.1000'; | ||
protected string $OVER_VERSION = '0.0.0'; | ||
|
||
protected function assertExtensionVersion($name,$lowestVersion,$overVersion) | ||
{ | ||
$currentVersion = phpversion($name); | ||
if(version_compare($currentVersion,$lowestVersion)<0|| | ||
version_compare($currentVersion,$overVersion)>=0 ) { | ||
throw new LogicException($name.' '.$currentVersion.' is an unsupported version. '. | ||
'Supported versions are greater than or equal to '.$lowestVersion. | ||
' and less than '.$overVersion.'.'); | ||
} | ||
} | ||
|
||
protected function assertVersion() | ||
{ | ||
$this->assertExtensionVersion($this->extName, | ||
$this->LOWEST_VERSION, | ||
$this->OVER_VERSION); | ||
} | ||
|
||
public function name() : string | ||
{ | ||
return $this->extName(); | ||
} | ||
|
||
public function isAvailable() : bool | ||
{ | ||
return extension_loaded($this->extName); | ||
} | ||
|
||
public function extName() : string | ||
{ | ||
return $this->extName; | ||
} | ||
|
||
public function version() : string | ||
{ | ||
return phpversion($this->extName); | ||
} | ||
|
||
} |
Oops, something went wrong.