Mathematical complex number manipulation library written in and for C.
This C library introduces a complex number data type to C that comes
along a wide range of functions that help to manipulate this data type
and perform various mathematical operations with it.
-
Elementary operations with complex numbers (addition, substraction,
multiplication, division)
- Exponentiation of complex numbers
- Square root function
- Degrees to radians conversion and viceversa
- Conjugate of a complex number
- Real and imaginary part of a complex number
-
Convert a complex number type (
complex_t
) to a displayable string.
- Modulus of a complex number (absolute value)
- Polar coordinates of a complex number
- Trigonometric functions of a complex number
- Hyperbolic trigonometric functions of a complex number
To import the library, simply add
#include
"complex.h"
to the file. All
functions will be imported and you'll now be able to use the
library.
The complex number data type is defined as
complex_t
, which is a struct with two
long double
data types: the real
part and the imaginary part. Although it is possible to change the
individual values of the
complex_t
data type,
it is recommended to use the built-in functions.
In this documentation,
complex_t
means the complex data
type, long double
is a real
number, and char*
is a C
null-terminated string. These are all the functions with the
corresponding description and arguments:
-
char*
ctoa(complex_t
cnum)
Convert complex number to a string, in format a+bi
or
a-bi.
-
long double
degtorad(long double
num)
Convert degrees to radians.
-
long double
radtodeg(long double
num)
Convert radians to degrees.
-
long double
cabs(complex_t
cnum)
Absolute value (modulus) of a complex number.
-
complex_t
cadd(complex_t
cnum1, complex_t cnum2)
Add 2 complex numbers.
-
complex_t
cdiff(complex_t
cnum1, complex_t cnum1)
Substract 2 complex numbers.
-
complex_t
cdivide(complex_t
cnum1, complex_t cnum2)
Divide 2 complex numbers.
-
complex_t
cmultiply(complex_t
cnum1, complex_t cnum2)
Multiply 2 complex numbers.
-
complex_t
conj(complex_t
cnum)
Conjugate of a complex number (a-bi).
-
complex_t
cpower(complex_t base, complex_t power)
Raise a complex number to a complex power.
-
complex_t
csqrt(complex_t) num
Square root of a complex number
complex_t
cexp(complex_t
num)
Exponential function for complex numbers (ez)
complex_t
clogbase(complex_t
base, complex_t num)
Complex logarithm of any base
complex_t
clog10(complex_t
num)
Complex natural logarithm
complex_t
csin(complex_t
num)
Sine of a complex number.
complex_t
ccos(complex_t
num)
Cosine of a complex number.
complex_t
ctan(complex_t
num)
Tangent of a complex number.
complex_t
ccsc(complex_t
num)
Cosecant of a complex number.
complex_t
csec(complex_t
num)
Secant of a complex number.
complex_t
ccot(complex_t
num)
Cotangent of a complex number.
complex_t
csinh(complex_t
num)
Hyperbolic sine of a complex number.
complex_t
ccosh(complex_t
num)
Hyperbolic cosine of a complex number.
complex_t
ctanh(complex_t
num)
Hyperbolic tangent of a complex number.
complex_t
csch(complex_t
num)
Hyperbolic cosecant of a complex number.
complex_t
csech(complex_t
num)
Hyperbolic secant of a complex number.
complex_t
ccoth(complex_t
num)
Hyperbolic cotangent of a complex number.
-
long double
imag(complex_t)
Get the imaginary part of a complex number, returns 0 if no
imaginary part exists.
-
long double
real(complex_t)
Get the real part of a complex number, returns 0 if no real
part exists.
-
complex_t
cvalue(long double, long double)
Assign new values to a complex number, and return that
value.
There is equally the shorthand alias
j(long double, long double)
The
ctoa()
function
takes a complex number in the
complex_t
format and
transforms it into a null-pointed string, in the form of a+bi or a-bi.
This function automatically allocates the required memory for the
output but
does not free it afterwards, which should
be handled by the user to avoid memory leaks.
There are two
possible output formats of
ctoa()
:
-
Full number representation: This is the default
mode. It always prints numbers in its full form a+bi even if one
of the two parts is zero.
-
Short number representation: Must be activated by
changing
SHORT_CTOA
option to one. This format will print the result in the shortest
possible way, removing zero-valued parts of the complex number.
This function will print a part of the complex number as zero if it
is smaller than 10-15, automatically handling rounding
errors inherent to 64-bit floating-point numbers.