Skip to content

Commit

Permalink
type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiis committed Apr 25, 2024
1 parent 983fb7b commit 18d2ab7
Show file tree
Hide file tree
Showing 39 changed files with 3,665 additions and 1,138 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ jobs:
# ( cd addpack && composer require rindow/rindow-math-matrix-matlibffi )
# fi

- name: PHP Static Analysys
uses: php-actions/phpstan@v3
with:
php_version: ${{ matrix.php }}
path: src/

- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
vendor/
/.vscode/
*.log
composer.lock
composer.phar
tests/.phpunit.*
phpstan.neon
1 change: 1 addition & 0 deletions bin/rindow-math-matrix.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
}
}
use Rindow\Math\Matrix\MatrixOperator;

$mo = new MatrixOperator();
echo $mo->service()->info();
126 changes: 126 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
parameters:
ignoreErrors:
-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$blasCLFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$bufferCLFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$bufferFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$clblastFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$mathCLFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$mathFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$openblasFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\MatlibPhp has an unused parameter \\$openclFactory\\.$#"
count: 1
path: src/Drivers/MatlibPHP/MatlibPhp.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\PhpBlas has an unused parameter \\$blas\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpBlas.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\PhpBlas has an unused parameter \\$forceBlas\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpBlas.php

-
message: "#^Class SplFixedArray specifies template type TKey of interface ArrayAccess as int but it's already specified as mixed\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpBuffer.php

-
message: "#^Comparison operation \"\\>\\=\" between int\\<0, max\\> and 0 is always true\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpLapack.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\PhpLapack has an unused parameter \\$forceLapack\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpLapack.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\PhpLapack has an unused parameter \\$lapack\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpLapack.php

-
message: "#^Comparison operation \"\\>\" between 2 and 1 is always true\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpMath.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\PhpMath has an unused parameter \\$forceMath\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpMath.php

-
message: "#^Constructor of class Rindow\\\\Math\\\\Matrix\\\\Drivers\\\\MatlibPHP\\\\PhpMath has an unused parameter \\$math\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpMath.php

-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpMath.php

-
message: "#^If condition is always false\\.$#"
count: 1
path: src/Drivers/MatlibPHP/PhpMath.php

-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\Buffer\\:\\:copy\\(\\)\\.$#"
count: 2
path: src/LinearAlgebraCL.php

-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\Buffer\\:\\:fill\\(\\)\\.$#"
count: 1
path: src/LinearAlgebraCL.php

-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray\\:\\:getEvents\\(\\)\\.$#"
count: 1
path: src/LinearAlgebraCL.php

-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray\\:\\:toNDArray\\(\\)\\.$#"
count: 1
path: src/LinearAlgebraCL.php

-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray\\:\\:valueSize\\(\\)\\.$#"
count: 1
path: src/LinearAlgebraCL.php

-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray\\:\\:serialize\\(\\)\\.$#"
count: 1
path: src/MatrixOperator.php
18 changes: 18 additions & 0 deletions phpstan-interop.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
parameters:
ignoreErrors:
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dtype\\(\\)#"
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:dump\\(\\)#"
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\.*[Device\\|Linear]*Buffer\\:\\:value_size\\(\\)#"
-
message: "#function count expects array|Countable, Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray given#"
-
message: "#^Argument of an invalid type Interop\\\\Polite\\\\Math\\\\Matrix\\\\NDArray supplied for foreach, only iterables are supported#"
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:bytes\\(\\)#"
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:copy\\(\\)#"
-
message: "#^Call to an undefined method Interop\\\\Polite\\\\Math\\\\Matrix\\\\DeviceBuffer\\:\\:read\\(\\)#"
26 changes: 26 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
includes:
- phpstan-interop.neon
- phpstan-baseline.neon

parameters:
level: 6
paths:
- src
excludePaths:
- src/Drivers/MatlibCL/OpenCLMathTunner.php

# ignoreErrors:
# level 7
# -
# message: "#^Call to an undefined method object::#"
# -
# message: "#^Access to an undefined property object::\\$real#"
# path: src/ComplexUtils.php
# -
# message: "#^Access to an undefined property object::\\$imag#"
# path: src/ComplexUtils.php
# level 8
# -
# message: "#^Cannot access property \\$real#"
# -
# message: "#^Cannot access property \\$imag#"
6 changes: 2 additions & 4 deletions src/C.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
function C(
float $r=null,
float $i=null,
) : Complex
{
) : Complex {
$r = $r ?? 0.0;
$i = $i ?? 0.0;
return new Complex($r,$i);
return new Complex($r, $i);
}

3 changes: 2 additions & 1 deletion src/Complex.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Complex
public function __construct(
public readonly float $real,
public readonly float $imag,
) {}
) {
}

public function __toString() : string
{
Expand Down
34 changes: 17 additions & 17 deletions src/ComplexUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@

trait ComplexUtils
{
protected function cbuild(float $r=null,float $i=null) : object
protected function cbuild(float $r=null, float $i=null) : object
{
return C($r,i:$i);
return C($r, i:$i);
}

protected function crebuild(object $value) : object
{
return C($value->real,i:$value->imag);
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') );
return (property_exists($value, 'real') &&
property_exists($value, 'imag'));
}

protected function cobjecttype(mixed $value) : string
Expand Down Expand Up @@ -56,51 +56,51 @@ protected function cabs(object $value) : float

protected function cconj(object $value) : object
{
return C($value->real,i:-$value->imag);
return C($value->real, i:-$value->imag);
}

protected function cadd(object $x,object $y) : object
protected function cadd(object $x, object $y) : object
{
return C(
($x->real+$y->real),
($x->real+$y->real),
i:($x->imag+$y->imag)
);
}

protected function csub(object $x,object $y) : object
protected function csub(object $x, object $y) : object
{
return C(
($x->real-$y->real),
($x->real-$y->real),
i:($x->imag-$y->imag)
);
}

protected function cmul(object $x,object $y) : object
protected function cmul(object $x, object $y) : object
{
return C(
(($x->real*$y->real)-($x->imag*$y->imag)),
(($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
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);
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);
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)),
(sqrt($r)*cos($theta)),
i:(sqrt($r)*sin($theta))
);
}
}
}
41 changes: 29 additions & 12 deletions src/Drivers/AbstractDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,42 @@

abstract class AbstractDriver implements Driver
{
// abstract properties
protected string $LOWEST_VERSION = '1000.1000.1000';
protected string $OVER_VERSION = '0.0.0';
protected string $extName = 'unknown';

protected function assertExtensionVersion($name,$lowestVersion,$overVersion)
protected function strVersion(string $name=null) : string
{
$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.'.');
if($name==null) {
$version = phpversion();
} else {
$version = phpversion($name);
}
if($version===false) {
$version = '0.0.0';
}
return $version;
}

protected function assertExtensionVersion(string $name, string $lowestVersion, string $overVersion) : void
{
$currentVersion = $this->strVersion($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()
protected function assertVersion() : void
{
$this->assertExtensionVersion($this->extName,
$this->assertExtensionVersion(
$this->extName,
$this->LOWEST_VERSION,
$this->OVER_VERSION);
$this->OVER_VERSION
);
}

public function name() : string
Expand All @@ -43,7 +60,7 @@ public function extName() : string

public function version() : string
{
return phpversion($this->extName);
$version = $this->strVersion($this->extName);
return $version;
}

}
Loading

0 comments on commit 18d2ab7

Please sign in to comment.