-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.php
More file actions
79 lines (67 loc) · 1.79 KB
/
Copy pathComplex.php
File metadata and controls
79 lines (67 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
declare(strict_types=1);
namespace PhpMlKit\NDArray;
/**
* Complex number value object for NDArray operations.
*
* Represents a complex number with real and imaginary parts.
* Used for creating complex arrays and receiving complex scalar results.
*/
final class Complex
{
public function __construct(
public readonly float $real,
public readonly float $imag = 0.0
) {}
public function __toString(): string
{
if ($this->imag >= 0.0) {
return "{$this->real}+{$this->imag}j";
}
return "{$this->real}{$this->imag}j";
}
/**
* Create a Complex from a PHP array [real, imag] or scalar value.
*/
public static function from(mixed $value): self
{
if ($value instanceof self) {
return $value;
}
if (\is_array($value)) {
return new self((float) ($value[0] ?? 0.0), (float) ($value[1] ?? 0.0));
}
return new self((float) $value);
}
/**
* Convert to a flat array [real, imag] for FFI transmission.
*
* @return array{float, float}
*/
public function toArray(): array
{
return [$this->real, $this->imag];
}
/**
* Get the magnitude (absolute value) of the complex number.
*/
public function magnitude(): float
{
return \hypot($this->real, $this->imag);
}
/**
* Get the phase angle in radians.
*/
public function angle(): float
{
return atan2($this->imag, $this->real);
}
/**
* Check if this complex number equals another within a tolerance.
*/
public function equals(self $other, float $tol = 1e-10): bool
{
return \abs($this->real - $other->real) <= $tol
&& \abs($this->imag - $other->imag) <= $tol;
}
}