| 1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
|
|
|
4 |
|
|
|
5 |
use Complex\Complex as ComplexObject;
|
|
|
6 |
use Complex\Exception as ComplexException;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
10 |
|
|
|
11 |
class Complex
|
|
|
12 |
{
|
|
|
13 |
use ArrayEnabled;
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* COMPLEX.
|
|
|
17 |
*
|
|
|
18 |
* Converts real and imaginary coefficients into a complex number of the form x +/- yi or x +/- yj.
|
|
|
19 |
*
|
|
|
20 |
* Excel Function:
|
|
|
21 |
* COMPLEX(realNumber,imaginary[,suffix])
|
|
|
22 |
*
|
|
|
23 |
* @param mixed $realNumber the real float coefficient of the complex number
|
|
|
24 |
* Or can be an array of values
|
|
|
25 |
* @param mixed $imaginary the imaginary float coefficient of the complex number
|
|
|
26 |
* Or can be an array of values
|
|
|
27 |
* @param mixed $suffix The character suffix for the imaginary component of the complex number.
|
|
|
28 |
* If omitted, the suffix is assumed to be "i".
|
|
|
29 |
* Or can be an array of values
|
|
|
30 |
*
|
|
|
31 |
* @return array|string If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
32 |
* with the same dimensions
|
|
|
33 |
*/
|
|
|
34 |
public static function COMPLEX(mixed $realNumber = 0.0, mixed $imaginary = 0.0, mixed $suffix = 'i'): array|string
|
|
|
35 |
{
|
|
|
36 |
if (is_array($realNumber) || is_array($imaginary) || is_array($suffix)) {
|
|
|
37 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $realNumber, $imaginary, $suffix);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
$realNumber = $realNumber ?? 0.0;
|
|
|
41 |
$imaginary = $imaginary ?? 0.0;
|
|
|
42 |
$suffix = $suffix ?? 'i';
|
|
|
43 |
|
|
|
44 |
try {
|
|
|
45 |
$realNumber = EngineeringValidations::validateFloat($realNumber);
|
|
|
46 |
$imaginary = EngineeringValidations::validateFloat($imaginary);
|
|
|
47 |
} catch (Exception $e) {
|
|
|
48 |
return $e->getMessage();
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
if (($suffix === 'i') || ($suffix === 'j') || ($suffix === '')) {
|
|
|
52 |
$complex = new ComplexObject($realNumber, $imaginary, $suffix);
|
|
|
53 |
|
|
|
54 |
return (string) $complex;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
return ExcelError::VALUE();
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* IMAGINARY.
|
|
|
62 |
*
|
|
|
63 |
* Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
|
|
|
64 |
*
|
|
|
65 |
* Excel Function:
|
|
|
66 |
* IMAGINARY(complexNumber)
|
|
|
67 |
*
|
|
|
68 |
* @param array|string $complexNumber the complex number for which you want the imaginary
|
|
|
69 |
* coefficient
|
|
|
70 |
* Or can be an array of values
|
|
|
71 |
*
|
|
|
72 |
* @return array|float|string (string if an error)
|
|
|
73 |
* If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
74 |
* with the same dimensions
|
|
|
75 |
*/
|
|
|
76 |
public static function IMAGINARY($complexNumber): array|string|float
|
|
|
77 |
{
|
|
|
78 |
if (is_array($complexNumber)) {
|
|
|
79 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $complexNumber);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
try {
|
|
|
83 |
$complex = new ComplexObject($complexNumber);
|
|
|
84 |
} catch (ComplexException) {
|
|
|
85 |
return ExcelError::NAN();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
return $complex->getImaginary();
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
/**
|
|
|
92 |
* IMREAL.
|
|
|
93 |
*
|
|
|
94 |
* Returns the real coefficient of a complex number in x + yi or x + yj text format.
|
|
|
95 |
*
|
|
|
96 |
* Excel Function:
|
|
|
97 |
* IMREAL(complexNumber)
|
|
|
98 |
*
|
|
|
99 |
* @param array|string $complexNumber the complex number for which you want the real coefficient
|
|
|
100 |
* Or can be an array of values
|
|
|
101 |
*
|
|
|
102 |
* @return array|float|string (string if an error)
|
|
|
103 |
* If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
104 |
* with the same dimensions
|
|
|
105 |
*/
|
|
|
106 |
public static function IMREAL($complexNumber): array|string|float
|
|
|
107 |
{
|
|
|
108 |
if (is_array($complexNumber)) {
|
|
|
109 |
return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $complexNumber);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
try {
|
|
|
113 |
$complex = new ComplexObject($complexNumber);
|
|
|
114 |
} catch (ComplexException) {
|
|
|
115 |
return ExcelError::NAN();
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
return $complex->getReal();
|
|
|
119 |
}
|
|
|
120 |
}
|