开始设计
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
#ifndef INCLUDED_C_COMPLEX_H
|
||||
#define INCLUDED_C_COMPLEX_H
|
||||
|
||||
#ifndef INCLUDED_C_BASE_H
|
||||
#include <c_Base.h>
|
||||
#endif /*INCLUDED_C_BASE_H*/
|
||||
|
||||
#ifndef INCLUDED_MATH_H
|
||||
#define INCLUDED_MATH_H
|
||||
#include <math.h>
|
||||
#endif /*INCLUDED_MATH_H*/
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
|
||||
typedef struct {
|
||||
double real; // Real component field
|
||||
double imag; // Imaginary component field
|
||||
} c_Complex_t;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------------------------ */
|
||||
/* */
|
||||
|
||||
/**
|
||||
* Factory Primitive: Construct a new complex number on the stack frame.
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
c_Complex_t c_Complex_Create(double real, double imag) {
|
||||
c_Complex_t c;
|
||||
c.real = real;
|
||||
c.imag = imag;
|
||||
return c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complex Addition: returns (a + b) by value.
|
||||
* Time Complexity: O(1) | Space Complexity: O(1) on stack
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
c_Complex_t c_Complex_Add(c_Complex_t a, c_Complex_t b) {
|
||||
c_Complex_t res;
|
||||
res.real = a.real + b.real;
|
||||
res.imag = a.imag + b.imag;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complex Subtraction: returns (a - b) by value.
|
||||
* Time Complexity: O(1) | Space Complexity: O(1) on stack
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
c_Complex_t c_Complex_Sub(c_Complex_t a, c_Complex_t b) {
|
||||
c_Complex_t res;
|
||||
res.real = a.real - b.real;
|
||||
res.imag = a.imag - b.imag;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complex Multiplication: returns (a * b) by value.
|
||||
* Time Complexity: O(1) | Space Complexity: O(1) on stack
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
c_Complex_t c_Complex_Mul(c_Complex_t a, c_Complex_t b) {
|
||||
c_Complex_t res;
|
||||
res.real = a.real * b.real - a.imag * b.imag;
|
||||
res.imag = a.real * b.imag + a.imag * b.real;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complex Division: returns (a / b) by value.
|
||||
* Features a safety check to intercept division-by-zero boundary exceptions.
|
||||
*
|
||||
* Time Complexity: O(1) | Space Complexity: O(1) on stack
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
c_err_t c_Complex_Div(c_Complex_t a, c_Complex_t b, c_Complex_t* out_res) {
|
||||
if (out_res == NULL) return C_ERR_PARAM;
|
||||
|
||||
double denom = b.real * b.real + b.imag * b.imag;
|
||||
if (denom == 0.0) {
|
||||
out_res->real = 0.0;
|
||||
out_res->imag = 0.0;
|
||||
return C_ERR_INVALID; // Catch division by zero anomalies
|
||||
}
|
||||
|
||||
out_res->real = (a.real * b.real + a.imag * b.imag) / denom;
|
||||
out_res->imag = (a.imag * b.real - a.real * b.imag) / denom;
|
||||
return C_ERR_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complex Conjugate: returns the conjugate of a (a.real - i * a.imag).
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
c_Complex_t c_Complex_Conjugate(c_Complex_t a) {
|
||||
c_Complex_t res;
|
||||
res.real = a.real;
|
||||
res.imag = -a.imag;
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Complex Absolute Value (Magnitude): returns sqrt(a.real^2 + a.imag^2).
|
||||
*/
|
||||
C_STATIC_FORCE_INLINE
|
||||
double c_Complex_Abs(c_Complex_t a) {
|
||||
return sqrt(a.real * a.real + a.imag * a.imag);
|
||||
}
|
||||
|
||||
|
||||
#endif /*INCLUDED_C_COMPLEX_H*/
|
||||
Reference in New Issue
Block a user