Dynamic Complex Library 1.0.0
Reference-counted arbitrary precision complex number library (MIT OR Unlicense)
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
dynamic_complex.h File Reference

Reference-counted arbitrary precision complex number library. More...

#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <complex.h>
#include <assert.h>
#include "dynamic_int.h"
#include "dynamic_fraction.h"

Go to the source code of this file.

Data Structures

struct  dc_complex_int_internal
 Internal structure for a Gaussian integer. More...
 
struct  dc_complex_frac_internal
 Internal structure for a rational complex number. More...
 
struct  dc_complex_double_internal
 Internal structure for a floating-point complex number. More...
 

Macros

#define DC_MALLOC   malloc
 
#define DC_FREE   free
 
#define DC_ASSERT   assert
 
#define DC_ATOMIC_REFCOUNT   0
 
#define DC_ATOMIC_SIZE_T   size_t
 
#define DC_ATOMIC_FETCH_ADD(ptr, val)   (*(ptr) += (val), *(ptr) - (val))
 
#define DC_ATOMIC_FETCH_SUB(ptr, val)   (*(ptr) -= (val), *(ptr) + (val))
 
#define DC_ATOMIC_LOAD(ptr)   (*(ptr))
 
#define DC_ATOMIC_STORE(ptr, val)   (*(ptr) = (val))
 
#define DC_DEC   extern
 
#define DC_DEF   /* nothing - default linkage */
 

Typedefs

typedef struct dc_complex_int_internaldc_complex_int
 Opaque pointer to a Gaussian integer (complex number with integer components)
 
typedef struct dc_complex_frac_internaldc_complex_frac
 Opaque pointer to a rational complex number.
 
typedef struct dc_complex_double_internaldc_complex_double
 Opaque pointer to a floating-point complex number.
 

Functions

dc_complex_int dc_int_from_ints (int64_t real, int64_t imag)
 Create a Gaussian integer from int64_t real and imaginary parts.
 
dc_complex_int dc_int_from_di (di_int real, di_int imag)
 Create a Gaussian integer from dynamic integer components.
 
dc_complex_int dc_int_zero (void)
 Get the Gaussian integer zero (0 + 0i)
 
dc_complex_int dc_int_one (void)
 Get the Gaussian integer one (1 + 0i)
 
dc_complex_int dc_int_i (void)
 Get the Gaussian integer i (0 + 1i)
 
dc_complex_int dc_int_neg_one (void)
 Get the Gaussian integer -1 (-1 + 0i)
 
dc_complex_int dc_int_neg_i (void)
 Get the Gaussian integer -i (0 - 1i)
 
dc_complex_int dc_int_retain (dc_complex_int c)
 Increment reference count and return the same object.
 
void dc_int_release (dc_complex_int *c)
 Decrement reference count and possibly free memory.
 
dc_complex_int dc_int_copy (dc_complex_int c)
 Create a new copy with reference count 1.
 
dc_complex_int dc_int_add (dc_complex_int a, dc_complex_int b)
 Add two Gaussian integers.
 
dc_complex_int dc_int_sub (dc_complex_int a, dc_complex_int b)
 Subtract two Gaussian integers.
 
dc_complex_int dc_int_mul (dc_complex_int a, dc_complex_int b)
 Multiply two Gaussian integers.
 
dc_complex_frac dc_int_div (dc_complex_int a, dc_complex_int b)
 Divide two Gaussian integers (returns exact rational result)
 
dc_complex_int dc_int_negate (dc_complex_int c)
 Negate a Gaussian integer.
 
dc_complex_int dc_int_conj (dc_complex_int c)
 Complex conjugate of a Gaussian integer.
 
di_int dc_int_real (dc_complex_int c)
 Get the real part of a Gaussian integer.
 
di_int dc_int_imag (dc_complex_int c)
 Get the imaginary part of a Gaussian integer.
 
bool dc_int_eq (dc_complex_int a, dc_complex_int b)
 Test if two Gaussian integers are equal.
 
bool dc_int_is_zero (dc_complex_int c)
 Test if a Gaussian integer is zero.
 
bool dc_int_is_real (dc_complex_int c)
 Test if a Gaussian integer is real (imaginary part is zero)
 
bool dc_int_is_imag (dc_complex_int c)
 Test if a Gaussian integer is purely imaginary (real part is zero)
 
char * dc_int_to_string (dc_complex_int c)
 Convert Gaussian integer to mathematical string representation.
 
dc_complex_frac dc_frac_from_ints (int64_t real_num, int64_t real_den, int64_t imag_num, int64_t imag_den)
 Create a rational complex number from integer components.
 
dc_complex_frac dc_frac_from_df (df_frac real, df_frac imag)
 Create a rational complex number from fraction components.
 
dc_complex_frac dc_frac_zero (void)
 Get the rational complex zero (0/1 + 0/1 i)
 
dc_complex_frac dc_frac_one (void)
 Get the rational complex one (1/1 + 0/1 i)
 
dc_complex_frac dc_frac_i (void)
 Get the rational complex i (0/1 + 1/1 i)
 
dc_complex_frac dc_frac_neg_one (void)
 Get the rational complex -1 (-1/1 + 0/1 i)
 
dc_complex_frac dc_frac_neg_i (void)
 Get the rational complex -i (0/1 + -1/1 i)
 
dc_complex_frac dc_frac_retain (dc_complex_frac c)
 Increment reference count and return the same object.
 
void dc_frac_release (dc_complex_frac *c)
 Decrement reference count and possibly free memory.
 
dc_complex_frac dc_frac_copy (dc_complex_frac c)
 Create a new copy with reference count 1.
 
dc_complex_frac dc_frac_add (dc_complex_frac a, dc_complex_frac b)
 Add two rational complex numbers.
 
dc_complex_frac dc_frac_sub (dc_complex_frac a, dc_complex_frac b)
 Subtract two rational complex numbers.
 
dc_complex_frac dc_frac_mul (dc_complex_frac a, dc_complex_frac b)
 Multiply two rational complex numbers.
 
dc_complex_frac dc_frac_div (dc_complex_frac a, dc_complex_frac b)
 Divide two rational complex numbers.
 
dc_complex_frac dc_frac_negate (dc_complex_frac c)
 Negate a rational complex number.
 
dc_complex_frac dc_frac_conj (dc_complex_frac c)
 Complex conjugate of a rational complex number.
 
dc_complex_frac dc_frac_reciprocal (dc_complex_frac c)
 Reciprocal of a rational complex number.
 
df_frac dc_frac_real (dc_complex_frac c)
 Get the real part of a rational complex number.
 
df_frac dc_frac_imag (dc_complex_frac c)
 Get the imaginary part of a rational complex number.
 
bool dc_frac_eq (dc_complex_frac a, dc_complex_frac b)
 Test if two rational complex numbers are equal.
 
bool dc_frac_is_zero (dc_complex_frac c)
 Test if a rational complex number is zero.
 
bool dc_frac_is_real (dc_complex_frac c)
 Test if a rational complex number is real (imaginary part is zero)
 
bool dc_frac_is_imag (dc_complex_frac c)
 Test if a rational complex number is purely imaginary (real part is zero)
 
bool dc_frac_is_gaussian_int (dc_complex_frac c)
 Test if a rational complex number is actually a Gaussian integer.
 
char * dc_frac_to_string (dc_complex_frac c)
 Convert rational complex number to mathematical string representation.
 
dc_complex_double dc_double_from_doubles (double real, double imag)
 Create a floating-point complex number from real and imaginary parts.
 
dc_complex_double dc_double_from_polar (double magnitude, double angle)
 Create a floating-point complex number from polar coordinates.
 
dc_complex_double dc_double_zero (void)
 Get the floating-point complex zero (0.0 + 0.0i)
 
dc_complex_double dc_double_one (void)
 Get the floating-point complex one (1.0 + 0.0i)
 
dc_complex_double dc_double_i (void)
 Get the floating-point complex i (0.0 + 1.0i)
 
dc_complex_double dc_double_neg_one (void)
 Get the floating-point complex -1 (-1.0 + 0.0i)
 
dc_complex_double dc_double_neg_i (void)
 Get the floating-point complex -i (0.0 + -1.0i)
 
dc_complex_double dc_double_retain (dc_complex_double c)
 Increment reference count and return the same object.
 
void dc_double_release (dc_complex_double *c)
 Decrement reference count and possibly free memory.
 
dc_complex_double dc_double_copy (dc_complex_double c)
 Create a new copy with reference count 1.
 
dc_complex_double dc_double_add (dc_complex_double a, dc_complex_double b)
 Add two floating-point complex numbers.
 
dc_complex_double dc_double_sub (dc_complex_double a, dc_complex_double b)
 Subtract two floating-point complex numbers.
 
dc_complex_double dc_double_mul (dc_complex_double a, dc_complex_double b)
 Multiply two floating-point complex numbers.
 
dc_complex_double dc_double_div (dc_complex_double a, dc_complex_double b)
 Divide two floating-point complex numbers.
 
dc_complex_double dc_double_negate (dc_complex_double c)
 Negate a floating-point complex number.
 
dc_complex_double dc_double_conj (dc_complex_double c)
 Complex conjugate of a floating-point complex number.
 
dc_complex_double dc_double_exp (dc_complex_double c)
 Complex exponential function.
 
dc_complex_double dc_double_log (dc_complex_double c)
 Complex natural logarithm.
 
dc_complex_double dc_double_pow (dc_complex_double a, dc_complex_double b)
 Complex power function.
 
dc_complex_double dc_double_sqrt (dc_complex_double c)
 Complex square root.
 
dc_complex_double dc_double_sin (dc_complex_double c)
 Complex sine function.
 
dc_complex_double dc_double_cos (dc_complex_double c)
 Complex cosine function.
 
dc_complex_double dc_double_tan (dc_complex_double c)
 Complex tangent function.
 
dc_complex_double dc_double_sinh (dc_complex_double c)
 Complex hyperbolic sine function.
 
dc_complex_double dc_double_cosh (dc_complex_double c)
 Complex hyperbolic cosine function.
 
dc_complex_double dc_double_tanh (dc_complex_double c)
 Complex hyperbolic tangent function.
 
double dc_double_real (dc_complex_double c)
 Get the real part of a floating-point complex number.
 
double dc_double_imag (dc_complex_double c)
 Get the imaginary part of a floating-point complex number.
 
double dc_double_abs (dc_complex_double c)
 Get the absolute value (magnitude) of a complex number.
 
double dc_double_arg (dc_complex_double c)
 Get the argument (phase angle) of a complex number.
 
bool dc_double_eq (dc_complex_double a, dc_complex_double b)
 Test if two floating-point complex numbers are equal.
 
bool dc_double_is_zero (dc_complex_double c)
 Test if a floating-point complex number is zero.
 
bool dc_double_is_real (dc_complex_double c)
 Test if a floating-point complex number is real (imaginary part is zero)
 
bool dc_double_is_imag (dc_complex_double c)
 Test if a floating-point complex number is purely imaginary (real part is zero)
 
bool dc_double_is_nan (dc_complex_double c)
 Test if a floating-point complex number contains NaN.
 
bool dc_double_is_inf (dc_complex_double c)
 Test if a floating-point complex number contains infinity.
 
char * dc_double_to_string (dc_complex_double c)
 Convert floating-point complex number to mathematical string representation.
 
dc_complex_frac dc_int_to_frac (dc_complex_int c)
 Convert Gaussian integer to rational complex (lossless)
 
dc_complex_double dc_int_to_double (dc_complex_int c)
 Convert Gaussian integer to floating-point complex (lossless for small integers)
 
dc_complex_double dc_frac_to_double (dc_complex_frac c)
 Convert rational complex to floating-point complex.
 
dc_complex_int dc_frac_to_int (dc_complex_frac c)
 Convert rational complex to Gaussian integer (with rounding)
 
dc_complex_int dc_double_to_int (dc_complex_double c)
 Convert floating-point complex to Gaussian integer (with rounding)
 
dc_complex_frac dc_double_to_frac (dc_complex_double c, int64_t max_denominator)
 Convert floating-point complex to rational complex (with approximation)
 

Detailed Description

Reference-counted arbitrary precision complex number library.

Version
0.1.0
Date
September 2025

Single header library for arbitrary precision complex numbers with reference counting. Supports three distinct complex number types:

Configuration

Customize the library by defining these macros before including:

#define DC_MALLOC malloc // custom allocator
#define DC_FREE free // custom deallocator
#define DC_ASSERT assert // custom assert macro
#define DC_ATOMIC_REFCOUNT 1 // enable atomic reference counting (requires C11)
#define DC_IMPLEMENTATION
Reference-counted arbitrary precision complex number library.

Basic Usage

// Integer complex
dc_complex_int a = dc_int_from_ints(3, 4); // 3 + 4i
dc_complex_int b = dc_int_from_ints(1, -2); // 1 - 2i
dc_complex_int sum = dc_int_add(a, b); // 4 + 2i
// Rational complex
dc_complex_frac c = dc_frac_from_ints(1, 2, 3, 4); // 1/2 + 3/4i
// Floating-point complex
// Type conversions
// Cleanup
dc_complex_double dc_frac_to_double(dc_complex_frac c)
Convert rational complex to floating-point complex.
dc_complex_frac dc_int_to_frac(dc_complex_int c)
Convert Gaussian integer to rational complex (lossless)
dc_complex_double dc_double_exp(dc_complex_double c)
Complex exponential function.
dc_complex_double dc_double_from_doubles(double real, double imag)
Create a floating-point complex number from real and imaginary parts.
void dc_double_release(dc_complex_double *c)
Decrement reference count and possibly free memory.
void dc_frac_release(dc_complex_frac *c)
Decrement reference count and possibly free memory.
dc_complex_frac dc_frac_from_ints(int64_t real_num, int64_t real_den, int64_t imag_num, int64_t imag_den)
Create a rational complex number from integer components.
dc_complex_int dc_int_from_ints(int64_t real, int64_t imag)
Create a Gaussian integer from int64_t real and imaginary parts.
dc_complex_int dc_int_add(dc_complex_int a, dc_complex_int b)
Add two Gaussian integers.
void dc_int_release(dc_complex_int *c)
Decrement reference count and possibly free memory.
Internal structure for a floating-point complex number.
Internal structure for a rational complex number.
Internal structure for a Gaussian integer.

Definition in file dynamic_complex.h.

Macro Definition Documentation

◆ DC_ASSERT

#define DC_ASSERT   assert

Definition at line 81 of file dynamic_complex.h.

◆ DC_ATOMIC_FETCH_ADD

#define DC_ATOMIC_FETCH_ADD (   ptr,
  val 
)    (*(ptr) += (val), *(ptr) - (val))

Definition at line 102 of file dynamic_complex.h.

◆ DC_ATOMIC_FETCH_SUB

#define DC_ATOMIC_FETCH_SUB (   ptr,
  val 
)    (*(ptr) -= (val), *(ptr) + (val))

Definition at line 103 of file dynamic_complex.h.

◆ DC_ATOMIC_LOAD

#define DC_ATOMIC_LOAD (   ptr)    (*(ptr))

Definition at line 104 of file dynamic_complex.h.

◆ DC_ATOMIC_REFCOUNT

#define DC_ATOMIC_REFCOUNT   0

Definition at line 86 of file dynamic_complex.h.

◆ DC_ATOMIC_SIZE_T

#define DC_ATOMIC_SIZE_T   size_t

Definition at line 101 of file dynamic_complex.h.

◆ DC_ATOMIC_STORE

#define DC_ATOMIC_STORE (   ptr,
  val 
)    (*(ptr) = (val))

Definition at line 105 of file dynamic_complex.h.

◆ DC_DEC

#define DC_DEC   extern

Definition at line 113 of file dynamic_complex.h.

◆ DC_DEF

#define DC_DEF   /* nothing - default linkage */

Definition at line 114 of file dynamic_complex.h.

◆ DC_FREE

#define DC_FREE   free

Definition at line 76 of file dynamic_complex.h.

◆ DC_MALLOC

#define DC_MALLOC   malloc

Definition at line 72 of file dynamic_complex.h.

Typedef Documentation

◆ dc_complex_double

Opaque pointer to a floating-point complex number.

Definition at line 141 of file dynamic_complex.h.

◆ dc_complex_frac

Opaque pointer to a rational complex number.

Definition at line 135 of file dynamic_complex.h.

◆ dc_complex_int

Opaque pointer to a Gaussian integer (complex number with integer components)

Definition at line 129 of file dynamic_complex.h.