Introduction
CNEXP is a built‑in function provided by the SAS System for evaluating the complex exponential of a complex number. It extends the real exponential function exp() to support complex arithmetic, allowing SAS programmers to work with complex-valued data directly within the DATA step and PROC IML. The function returns the value of ez where z is a complex argument, defined as a pair of real numbers representing the real and imaginary parts of the complex input. CNEXP is part of the suite of complex number functions introduced in SAS 9.2, which also includes CNABS, CNARG, CNCONJ, CNLOG, CNPOW, and others. These functions provide a foundation for advanced numerical analysis and engineering applications that require complex arithmetic, such as signal processing, control systems, and computational electromagnetics.
History and Development
Origins in Complex Analysis
Complex numbers have long been used in mathematical analysis, engineering, and physics. The exponential function for complex arguments, ez, is a central element of complex analysis, connected to trigonometric functions through Euler’s formula. Traditional programming environments historically provided real exponential functions (exp()) but lacked native support for complex numbers. As computational demands grew, especially in telecommunications and power system studies, the need for robust complex arithmetic within statistical software became apparent.
Integration into SAS
The SAS System, released in the 1970s, evolved to include advanced mathematical functions over successive releases. Prior to SAS 9.2, users performed complex calculations by manually managing real and imaginary components. SAS 9.2, introduced in 2006, added a comprehensive set of complex functions, including CNEXP. This addition was motivated by the growing use of SAS in engineering disciplines and the demand for a single platform that could handle both statistical analysis and complex numerical calculations without resorting to external tools. CNEXP and its counterparts were implemented in the core SAS library, ensuring compatibility across all supported platforms and versions.
Syntax and Parameters
Basic Syntax
The canonical syntax for CNEXP is:
result = cnexp(complex_value);
Here, complex_value can be a complex variable or an inline complex literal expressed as complex(real, imag). The function returns a complex number of the same type, with the real part equal to eRe(z)·cos(Im(z)) and the imaginary part equal to eRe(z)·sin(Im(z)).
Parameter Types
- Complex Variable: A user‑defined variable that has been declared with the
complexdata type, typically using thecomplexkeyword or through acomplex()function call. - Complex Literal: A direct specification of a complex number, such as
complex(1.0, 2.0), which represents 1.0 + 2.0i. - Mixed Real and Imaginary Parts: In some contexts, the function accepts two separate numeric arguments representing the real and imaginary parts, e.g.,
cnexp(1.0, 2.0). However, the standard form requires a single complex argument.
Mathematical Foundations
Definition of Complex Exponential
For a complex number z = x + iy, the complex exponential is defined as:
ez = ex · (cos(y) + i·sin(y)).
This definition follows directly from the Taylor series expansion of ez and the properties of exponentials with imaginary exponents. CNEXP implements this definition exactly, using double‑precision arithmetic internally. The returned value retains the complex data type, preserving both the magnitude and phase of the result.
Euler’s Formula
Euler’s formula, eiy = cos(y) + i·sin(y), is the cornerstone of CNEXP’s implementation. By separating the real and imaginary components, the function computes the magnitude ex and the rotation represented by cos(y) and sin(y). This approach ensures numerical stability and accuracy across a wide range of input values, as it avoids the direct evaluation of the exponential function on a complex number which can lead to overflow or underflow issues.
Functionality and Behavior
Input Handling
The function accepts complex numbers of any magnitude, provided the internal representation fits within SAS’s double‑precision limits. For inputs where the real part is large and positive, the function may return infinity, represented as Inf in SAS. Conversely, a large negative real part may yield zero. Imaginary parts are limited by the range of the double‑precision sine and cosine functions; however, SAS’s implementation includes periodic reduction to maintain precision for very large angles.
Output Format
CNEXP returns a complex number of the same precision as the input. The real and imaginary parts are stored in separate internal fields. When the result is printed or displayed, SAS formats the complex number as complex(real, imag). For example, cnexp(complex(0, pi/2)) yields complex(0, 1), reflecting the identity eiπ/2 = i.
Edge Cases
- Zero Input: cnexp(complex(0,0)) returns complex(1,0), as e0 = 1.
- NaN or Infinite Components: If either component of the input is
NaNorInf, the result isNaNin the corresponding part. - Overflow/Underflow: Large positive real parts may overflow to
Inf; large negative real parts may underflow to zero. The function handles these gracefully by propagating the special values.
Use Cases and Applications
Statistical Analysis
In statistical genetics and psychometrics, complex numbers sometimes arise in spectral analysis or in modeling oscillatory processes. CNEXP allows researchers to compute complex-valued likelihoods or covariance matrices without leaving the SAS environment.
Signal Processing
Engineering disciplines frequently use complex exponentials to represent sinusoidal signals and to perform Fourier analysis. CNEXP can be employed to generate harmonic components, model damped oscillations, or apply phase shifts. The function integrates seamlessly with SAS/IML for matrix computations, enabling full‑scale spectral transforms.
Engineering Simulations
Control system analysis often requires the evaluation of transfer functions, which are rational functions of s = σ + iω. By converting s into a complex number, CNEXP can compute est terms used in time‑domain simulations of differential equations. Similarly, power system studies that model alternating current phasors can use CNEXP to transform between time and frequency domains.
Finance
Financial modeling of stochastic processes sometimes involves complex-valued characteristic functions. CNEXP can be used to evaluate such functions for risk assessment or option pricing models that rely on Fourier inversion techniques. While not common in routine SAS usage, the function offers the theoretical capability for advanced quantitative finance applications.
Comparison with Other Functions
exp vs cnexp
The real exponential function exp() operates on scalar real numbers and returns a real result. In contrast, cnexp() handles complex inputs and returns a complex result. When the imaginary part of the input is zero, cnexp() behaves identically to exp(). However, for non‑zero imaginary parts, the result incorporates trigonometric components, which cannot be obtained by simply applying exp() to the real part.
CNLOG
CNLOG computes the complex natural logarithm of a complex argument, providing the principal value. CNLOG is effectively the inverse of CNEXP: cnlog(cnexp(z)) = z, within the domain restrictions of the complex logarithm. While CNEXP focuses on exponentiation, CNLOG focuses on determining magnitude and phase, complementing CNEXP in complex algebraic operations.
Limitations and Constraints
Data Type Limits
Complex numbers in SAS are represented as double‑precision floating‑point numbers. Consequently, the maximum representable magnitude is approximately 1.79769×10308. Inputs exceeding this limit will produce overflow. The precision of the sine and cosine calculations is limited by the IEEE 754 standard, affecting very small or very large imaginary components.
Performance Considerations
Complex arithmetic is computationally more expensive than real arithmetic due to the additional sine and cosine evaluations. In large datasets or matrix operations, repeated calls to CNEXP may dominate execution time. Users should profile their code and consider pre‑computing constant terms or vectorizing operations where possible.
Examples
Simple Example
The following DATA step demonstrates the use of CNEXP to compute e1 + iπ/2:
data exp_example;
complex z;
z = complex(1, 3.141592653589793/2);
result = cnexp(z);
put z= result=;
run;
The output will show z=complex(1,1.5707963267948966) result=complex(0,1.718281828459045).
Advanced Example
This PROC IML example evaluates a Fourier transform of a complex exponential sequence:
proc iml;
t = 0:0.01:2;
s = complex(0, 2*pi);
y = cnexp(s*t);
/* Compute discrete Fourier transform */
N = ncol(y);
F = y * exp(-2i * pi * (0:N-1) / N);
print y[cols=1] F[cols=1];
quit;
The example demonstrates CNEXP’s integration with matrix operations, enabling advanced spectral analysis.
Batch Example
In a data set of complex numbers, CNEXP can be applied to each row efficiently:
data batch_exp;
set complex_data;
exp_val = cnexp(complex_val);
run;
Here, complex_data contains a variable complex_val of type complex. The resulting dataset includes the exponential values for all observations.
Integration with Other SAS Components
PROC FCMP
Functions defined in PROC FCMP can incorporate CNEXP for user‑defined complex operations. A custom function may wrap CNEXP to provide additional error handling or to expose the real and imaginary parts separately.
DATA Step
CNEXP is callable directly within the DATA step, enabling row‑by‑row computation of complex exponentials during data transformation or simulation tasks.
PROC IML
PROC IML’s matrix language supports complex data types natively. CNEXP can be used in matrix equations, enabling efficient solution of systems involving complex exponentials without resorting to external libraries.
Alternatives in Other Programming Languages
R
R handles complex numbers using built‑in support for complex arithmetic. The function exp() automatically applies to complex numbers, yielding the same result as CNEXP.
Python
Python’s cmath module provides exp() for complex arguments, mirroring SAS’s CNEXP. The numpy.exp() function also supports complex arrays.
MATLAB
MATLAB’s exp() accepts complex inputs directly. The function is vectorized and highly optimized for large matrices.
Julia
Julia’s standard library includes exp() for complex numbers, with type dispatch ensuring efficient computation.
Best Practices
Code Optimization
When working with large datasets, avoid repeated construction of complex literals. Instead, declare complex variables once and reuse them. Where possible, vectorize operations in PROC IML to reduce the number of function calls.
Error Handling
Always check for NaN or Inf outputs when the input data may contain extreme values. Use SAS’s ifn() and ifb() functions to guard against invalid results before further processing.
See Also
- CNABS – Complex Absolute Value
- CNARG – Complex Argument (Phase)
- CNCONJ – Complex Conjugate
- CNLOG – Complex Natural Logarithm
- CNPOW – Complex Power
No comments yet. Be the first to comment!