Complex




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.



Features





Usage


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:

Conversion functions

Basic complex operations

Logarithms & exponentials

  • 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
  • Trigonometric functions

  • 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.
  • Hyperbolic functions

  • 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.
  • Other functions




    Complex number to string


    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.