chengkun
2025-09-09 774d962b76d63366ed26c395e0a33cdbec309242
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
 
namespace PhpOffice\PhpSpreadsheet\Calculation\Engineering;
 
use Complex\Complex as ComplexObject;
use Complex\Exception as ComplexException;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception;
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
 
class Complex
{
    use ArrayEnabled;
 
    /**
     * COMPLEX.
     *
     * Converts real and imaginary coefficients into a complex number of the form x +/- yi or x +/- yj.
     *
     * Excel Function:
     *        COMPLEX(realNumber,imaginary[,suffix])
     *
     * @param mixed $realNumber the real float coefficient of the complex number
     *                      Or can be an array of values
     * @param mixed $imaginary the imaginary float coefficient of the complex number
     *                      Or can be an array of values
     * @param mixed $suffix The character suffix for the imaginary component of the complex number.
     *                          If omitted, the suffix is assumed to be "i".
     *                      Or can be an array of values
     *
     * @return array|string If an array of numbers is passed as an argument, then the returned result will also be an array
     *            with the same dimensions
     */
    public static function COMPLEX(mixed $realNumber = 0.0, mixed $imaginary = 0.0, mixed $suffix = 'i'): array|string
    {
        if (is_array($realNumber) || is_array($imaginary) || is_array($suffix)) {
            return self::evaluateArrayArguments([self::class, __FUNCTION__], $realNumber, $imaginary, $suffix);
        }
 
        $realNumber = $realNumber ?? 0.0;
        $imaginary = $imaginary ?? 0.0;
        $suffix = $suffix ?? 'i';
 
        try {
            $realNumber = EngineeringValidations::validateFloat($realNumber);
            $imaginary = EngineeringValidations::validateFloat($imaginary);
        } catch (Exception $e) {
            return $e->getMessage();
        }
 
        if (($suffix === 'i') || ($suffix === 'j') || ($suffix === '')) {
            $complex = new ComplexObject($realNumber, $imaginary, $suffix);
 
            return (string) $complex;
        }
 
        return ExcelError::VALUE();
    }
 
    /**
     * IMAGINARY.
     *
     * Returns the imaginary coefficient of a complex number in x + yi or x + yj text format.
     *
     * Excel Function:
     *        IMAGINARY(complexNumber)
     *
     * @param array|string $complexNumber the complex number for which you want the imaginary
     *                                         coefficient
     *                      Or can be an array of values
     *
     * @return array|float|string (string if an error)
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
     *            with the same dimensions
     */
    public static function IMAGINARY($complexNumber): array|string|float
    {
        if (is_array($complexNumber)) {
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $complexNumber);
        }
 
        try {
            $complex = new ComplexObject($complexNumber);
        } catch (ComplexException) {
            return ExcelError::NAN();
        }
 
        return $complex->getImaginary();
    }
 
    /**
     * IMREAL.
     *
     * Returns the real coefficient of a complex number in x + yi or x + yj text format.
     *
     * Excel Function:
     *        IMREAL(complexNumber)
     *
     * @param array|string $complexNumber the complex number for which you want the real coefficient
     *                      Or can be an array of values
     *
     * @return array|float|string (string if an error)
     *         If an array of numbers is passed as an argument, then the returned result will also be an array
     *            with the same dimensions
     */
    public static function IMREAL($complexNumber): array|string|float
    {
        if (is_array($complexNumber)) {
            return self::evaluateSingleArgumentArray([self::class, __FUNCTION__], $complexNumber);
        }
 
        try {
            $complex = new ComplexObject($complexNumber);
        } catch (ComplexException) {
            return ExcelError::NAN();
        }
 
        return $complex->getReal();
    }
}