This commit is contained in:
2026-09-07 19:33:37 +08:00
parent c945aa211f
commit c7b6c90ec9
20 changed files with 1029 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
#include "c_Complex.h"
+208
View File
@@ -0,0 +1,208 @@
#ifndef INCLUDED_C_COMPLEX_H
#define INCLUDED_C_COMPLEX_H
#ifndef INCLUDED_C_TYPES_H
#include <c_Types.h>
#endif /*INCLUDED_C_TYPES_H*/
#ifndef INCLUDED_MATH_H
#define INCLUDED_MATH_H
#include <math.h>
#endif /*INCLUDED_MATH_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
typedef struct {
double real;
double imag;
} c_Complex_t;
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Make(double real, double imag) {
c_Complex_t c;
c.real = real;
c.imag = imag;
return c;
}
/**
* @brief 从极坐标创建复数 (Polar to Rectangular)
* @param r 幅值 (Magnitude)
* @param theta 幅角 (Phase angle in radians)
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_FromPolar(double r, double theta) {
c_Complex_t c;
c.real = r * cos(theta);
c.imag = r * sin(theta);
return c;
}
/**
* @brief Adds two complex numbers: res = a + b
*/
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;
}
/**
* @brief Subtracts two complex numbers: res = a - b
*/
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;
}
/**
* @brief Multiplies two complex numbers: res = a * b
*/
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;
}
/**
* @brief 复数除法: res = a / b
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Div(c_Complex_t a, c_Complex_t b) {
double denom = b.real * b.real + b.imag * b.imag;
if (denom == 0.0) {
return c_Complex_Make(0.0, 0.0); /* 健壮性防线:防止除以 0 导致崩溃 */
}
return c_Complex_Make(
(a.real * b.real + a.imag * b.imag) / denom,
(a.imag * b.real - a.real * b.imag) / denom
);
}
/**
* @brief Computes the complex conjugate: res = real - i*imag
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Conjugate(c_Complex_t c) {
c_Complex_t res;
res.real = c.real;
res.imag = -c.imag;
return res;
}
/**
* @brief 计算复数的相反数 (Negate): res = -real - i*imag
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Negate(c_Complex_t c) {
return c_Complex_Make(-c.real, -c.imag);
}
/**
* @brief 计算复数的模长的平方 (Magnitude Squared / Norm)
* @note 避免了开方操作,适合用于比对大小以提升性能
*/
C_STATIC_FORCE_INLINE double c_Complex_Norm(c_Complex_t c) {
return c.real * c.real + c.imag * c.imag;
}
/**
* @brief 计算复数的模长 (Magnitude / Absolute Value)
*/
C_STATIC_FORCE_INLINE double c_Complex_Abs(c_Complex_t c) {
return sqrt(c.real * c.real + c.imag * c.imag);
}
/**
* @brief 计算复数的幅角 (Phase Angle / Argument)
* @return 返回介于 -PI 到 PI 之间的弧度值
*/
C_STATIC_FORCE_INLINE double c_Complex_Arg(c_Complex_t c) {
return atan2(c.imag, c.real);
}
/* ================================================================================================================== */
/* 标量混合运算接口 (Scalar & Complex Operations) */
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_AddScalar(c_Complex_t c, double s) {
return c_Complex_Make(c.real + s, c.imag);
}
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_SubScalar(c_Complex_t c, double s) {
return c_Complex_Make(c.real - s, c.imag);
}
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_MulScalar(c_Complex_t c, double s) {
return c_Complex_Make(c.real * s, c.imag * s);
}
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_DivScalar(c_Complex_t c, double s) {
if (s == 0.0) return c_Complex_Make(0.0, 0.0);
return c_Complex_Make(c.real / s, c.imag / s);
}
/* ================================================================================================================== */
/* 超越函数与幂运算接口 (Transcendental & Power Functions) */
/**
* @brief 复数的指数函数: e^c
* @note e^(x+iy) = e^x * (cos(y) + i*sin(y))
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Exp(c_Complex_t c) {
double exp_x = exp(c.real);
return c_Complex_Make(exp_x * cos(c.imag), exp_x * sin(c.imag));
}
/**
* @brief 复数的自然对数函数: ln(c)
* @note ln(c) = ln(|c|) + i*arg(c)
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Log(c_Complex_t c) {
return c_Complex_Make(log(c_Complex_Abs(c)), c_Complex_Arg(c));
}
/**
* @brief 复数的幂运算: base^exp
* @note a^b = exp(b * log(a))
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Pow(c_Complex_t base, c_Complex_t exponent) {
if (base.real == 0.0 && base.imag == 0.0) return c_Complex_Make(0.0, 0.0);
return c_Complex_Exp(c_Complex_Mul(exponent, c_Complex_Log(base)));
}
/**
* @brief 复数的实数幂运算: base^p (p 为实数标量)
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_PowScalar(c_Complex_t base, double p) {
if (base.real == 0.0 && base.imag == 0.0) return c_Complex_Make(0.0, 0.0);
double r = pow(c_Complex_Abs(base), p);
double theta = c_Complex_Arg(base) * p;
return c_Complex_FromPolar(r, theta);
}
/**
* @brief 复数开平方根: sqrt(c)
*/
C_STATIC_FORCE_INLINE c_Complex_t c_Complex_Sqrt(c_Complex_t c) {
double r = c_Complex_Abs(c);
double real_part = sqrt((r + c.real) / 2.0);
double imag_part = sqrt((r - c.real) / 2.0);
if (c.imag < 0.0) imag_part = -imag_part;
return c_Complex_Make(real_part, imag_part);
}
/* ================================================================================================================== */
/* 比较运算 (Comparison Interfaces) */
/**
* @brief 判断两个复数是否在允许的误差内相等
*/
C_STATIC_FORCE_INLINE bool c_Complex_Equals(c_Complex_t a, c_Complex_t b, double epsilon) {
return (fabs(a.real - b.real) <= epsilon) && (fabs(a.imag - b.imag) <= epsilon);
}
#endif /*INCLUDED_C_COMPLEX_H*/
+35
View File
@@ -0,0 +1,35 @@
#include "c_Test.h"
#include "c_Complex.h"
TEST_CASE(test_complex_extended_apis) {
c_Complex_t a = c_Complex_Make(3.0, 4.0);
c_Complex_t b = c_Complex_Make(1.0, -2.0);
/* 1. 测试属性提取 */
ASSERT_DOUBLE_EQ_MSG(25.0, c_Complex_Norm(a), "Norm 运算错误");
ASSERT_DOUBLE_EQ_MSG(5.0, c_Complex_Abs(a), "Abs 运算错误");
/* 2. 测试除法 */
c_Complex_t res_div = c_Complex_Div(a, b);
/* (3+4i)/(1-2i) = (3+4i)(1+2i)/5 = (-5+10i)/5 = -1 + 2i */
ASSERT_DOUBLE_EQ_MSG(-1.0, res_div.real, "复数除法实部错误");
ASSERT_DOUBLE_EQ_MSG(2.0, res_div.imag, "复数除法虚部错误");
/* 3. 测试极坐标转换与欧拉公式基础验证 */
/* e^(i*PI) = -1 + 0i */
c_Complex_t polar = c_Complex_FromPolar(1.0, 3.141592653589793);
ASSERT_TRUE(c_Complex_Equals(polar, c_Complex_Make(-1.0, 0.0), 1e-6));
/* 4. 测试标量混合运算 */
c_Complex_t res_scalar = c_Complex_MulScalar(b, 3.0);
ASSERT_DOUBLE_EQ_MSG(3.0, res_scalar.real, "标量乘法实部错误");
ASSERT_DOUBLE_EQ_MSG(-6.0, res_scalar.imag, "标量乘法虚部错误");
}
int main(void) {
TEST_START(TestSuite);
RUN_TEST(test_complex_extended_apis);
TEST_REPORT();
return (g_test_registry.failed_count > 0 ? 1 : 0);
}
+49
View File
@@ -0,0 +1,49 @@
#include <c_HardwareConcurrency.h>
/* ================================================================================================================== */
/* Cross-Platform OS Handle Interceptions */
#if defined(_WIN32) || defined(_WIN64)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#elif defined(__linux__) || defined(__ANDROID__) || defined(__hpux) || defined(_AIX)
#include <unistd.h>
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
#include <unistd.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
c_size_t c_HardwareConcurrency(void) {
#if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO sys_info;
GetSystemInfo(&sys_info);
return (c_size_t)sys_info.dwNumberOfProcessors > 0 ? (c_size_t)sys_info.dwNumberOfProcessors : 1;
#elif defined(__linux__) || defined(__ANDROID__) || defined(__hpux) || defined(_AIX)
long cores = sysconf(_SC_NPROCESSORS_ONLN);
return (cores > 0) ? (c_size_t)cores : 1;
#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
int mib[2];
mib[0] = CTL_HW;
mib[1] = HW_NCPU; /* Fallback baseline key token selector */
#ifdef HW_AVAILCPU
mib[1] = HW_AVAILCPU; /* Preferred choice: online/available logical cores count query */
#endif
int num_cores = 0;
size_t len = sizeof(num_cores);
if (sysctl(mib, 2, &num_cores, &len, NULL, 0) == 0 && num_cores > 0) {
return (c_size_t)num_cores;
}
return 1;
#else
/* Safe fallback anchor primitive for completely unknown or specialized bare-metal setups */
return 1;
#endif
}
+15
View File
@@ -0,0 +1,15 @@
#ifndef INCLUDED_C_HARDWARECONCURRENCY_H
#define INCLUDED_C_HARDWARECONCURRENCY_H
#ifndef INCLUDED_C_TYPES_H
#include <c_Types.h>
#endif /*INCLUDED_C_TYPES_H*/
/* ------------------------------------------------------------------------------------------------------------------ */
/* */
c_size_t c_HardwareConcurrency(void);
#endif /*INCLUDED_C_HARDWARECONCURRENCY_H*/
+23
View File
@@ -0,0 +1,23 @@
#include "c_Test.h"
#include "c_HardwareConcurrency.h"
TEST_CASE(test_hardware_concurrency_detection) {
c_size_t cores = c_HardwareConcurrency();
/* Log numerical results using your system's architectural type-spec macro format */
printf(" " COLOR_CYAN "[INFO] Detected System Hardware Concurrency: %" C_PRId " logical core(s)" COLOR_RESET "\n", (uint64_t)cores);
/*
* Invariants verification:
* Even on old or highly constrained embedded hardware setups,
* the system must feature at least 1 logical core running the test loop thread.
*/
ASSERT_TRUE(cores >= 1);
}
int main(void) {
TEST_START(HardwareConcurrency_Detection_Suite);
RUN_TEST(test_hardware_concurrency_detection);
TEST_REPORT();
RETURN_TEST_STATUS;
}