| 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\Functions;
|
|
|
9 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
10 |
|
|
|
11 |
class ComplexOperations
|
|
|
12 |
{
|
|
|
13 |
use ArrayEnabled;
|
|
|
14 |
|
|
|
15 |
/**
|
|
|
16 |
* IMDIV.
|
|
|
17 |
*
|
|
|
18 |
* Returns the quotient of two complex numbers in x + yi or x + yj text format.
|
|
|
19 |
*
|
|
|
20 |
* Excel Function:
|
|
|
21 |
* IMDIV(complexDividend,complexDivisor)
|
|
|
22 |
*
|
|
|
23 |
* @param array|string $complexDividend the complex numerator or dividend
|
|
|
24 |
* Or can be an array of values
|
|
|
25 |
* @param array|string $complexDivisor the complex denominator or divisor
|
|
|
26 |
* Or can be an array of values
|
|
|
27 |
*
|
|
|
28 |
* @return array|string If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
29 |
* with the same dimensions
|
|
|
30 |
*/
|
|
|
31 |
public static function IMDIV(array|string $complexDividend, array|string $complexDivisor): array|string
|
|
|
32 |
{
|
|
|
33 |
if (is_array($complexDividend) || is_array($complexDivisor)) {
|
|
|
34 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $complexDividend, $complexDivisor);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
try {
|
|
|
38 |
return (string) (new ComplexObject($complexDividend))->divideby(new ComplexObject($complexDivisor));
|
|
|
39 |
} catch (ComplexException) {
|
|
|
40 |
return ExcelError::NAN();
|
|
|
41 |
}
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
/**
|
|
|
45 |
* IMSUB.
|
|
|
46 |
*
|
|
|
47 |
* Returns the difference of two complex numbers in x + yi or x + yj text format.
|
|
|
48 |
*
|
|
|
49 |
* Excel Function:
|
|
|
50 |
* IMSUB(complexNumber1,complexNumber2)
|
|
|
51 |
*
|
|
|
52 |
* @param array|string $complexNumber1 the complex number from which to subtract complexNumber2
|
|
|
53 |
* Or can be an array of values
|
|
|
54 |
* @param array|string $complexNumber2 the complex number to subtract from complexNumber1
|
|
|
55 |
* Or can be an array of values
|
|
|
56 |
*
|
|
|
57 |
* @return array|string If an array of numbers is passed as an argument, then the returned result will also be an array
|
|
|
58 |
* with the same dimensions
|
|
|
59 |
*/
|
|
|
60 |
public static function IMSUB(array|string $complexNumber1, array|string $complexNumber2): array|string
|
|
|
61 |
{
|
|
|
62 |
if (is_array($complexNumber1) || is_array($complexNumber2)) {
|
|
|
63 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $complexNumber1, $complexNumber2);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
try {
|
|
|
67 |
return (string) (new ComplexObject($complexNumber1))->subtract(new ComplexObject($complexNumber2));
|
|
|
68 |
} catch (ComplexException) {
|
|
|
69 |
return ExcelError::NAN();
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* IMSUM.
|
|
|
75 |
*
|
|
|
76 |
* Returns the sum of two or more complex numbers in x + yi or x + yj text format.
|
|
|
77 |
*
|
|
|
78 |
* Excel Function:
|
|
|
79 |
* IMSUM(complexNumber[,complexNumber[,...]])
|
|
|
80 |
*
|
|
|
81 |
* @param string ...$complexNumbers Series of complex numbers to add
|
|
|
82 |
*/
|
|
|
83 |
public static function IMSUM(...$complexNumbers): string
|
|
|
84 |
{
|
|
|
85 |
// Return value
|
|
|
86 |
$returnValue = new ComplexObject(0.0);
|
|
|
87 |
$aArgs = Functions::flattenArray($complexNumbers);
|
|
|
88 |
|
|
|
89 |
try {
|
|
|
90 |
// Loop through the arguments
|
|
|
91 |
foreach ($aArgs as $complex) {
|
|
|
92 |
$returnValue = $returnValue->add(new ComplexObject($complex));
|
|
|
93 |
}
|
|
|
94 |
} catch (ComplexException) {
|
|
|
95 |
return ExcelError::NAN();
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
return (string) $returnValue;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* IMPRODUCT.
|
|
|
103 |
*
|
|
|
104 |
* Returns the product of two or more complex numbers in x + yi or x + yj text format.
|
|
|
105 |
*
|
|
|
106 |
* Excel Function:
|
|
|
107 |
* IMPRODUCT(complexNumber[,complexNumber[,...]])
|
|
|
108 |
*
|
|
|
109 |
* @param string ...$complexNumbers Series of complex numbers to multiply
|
|
|
110 |
*/
|
|
|
111 |
public static function IMPRODUCT(...$complexNumbers): string
|
|
|
112 |
{
|
|
|
113 |
// Return value
|
|
|
114 |
$returnValue = new ComplexObject(1.0);
|
|
|
115 |
$aArgs = Functions::flattenArray($complexNumbers);
|
|
|
116 |
|
|
|
117 |
try {
|
|
|
118 |
// Loop through the arguments
|
|
|
119 |
foreach ($aArgs as $complex) {
|
|
|
120 |
$returnValue = $returnValue->multiply(new ComplexObject($complex));
|
|
|
121 |
}
|
|
|
122 |
} catch (ComplexException) {
|
|
|
123 |
return ExcelError::NAN();
|
|
|
124 |
}
|
|
|
125 |
|
|
|
126 |
return (string) $returnValue;
|
|
|
127 |
}
|
|
|
128 |
}
|