C Library - Useful tools
Table of Contents
Installation
Place the main file main.c
at the root of the project.
Compile with:
gcc -Iinclude src/*.c main.c -o <executable_name>
./<executable_name>
Or use the provided Makefile:
make
./a.out
To clean compiled files:
make clean
Modules and Functions
DataFrame (cPandas
)
Allows manipulation of heterogeneous tabular data (int, double, string).
Creation and Destruction
DataFrame* df_create(int n_cols);
void df_free(DataFrame* df);
CSV Reading and Writing
DataFrame* df_read_csv(const char* filename);
int df_to_csv(DataFrame* df, const char* filename);
Information and Display
void df_info(DataFrame* df);
void df_head(DataFrame* df, int n);
void df_shape(DataFrame* df, int* out_rows, int* out_cols);
Column Access
Column* df_get_column(DataFrame* df, const char* col_name);
int df_get_column_index(DataFrame* df, const char* col_name);
Example
DataFrame* df = df_read_csv("data.csv");
df_info(df);
df_head(df, 3);
df_free(df);
Matrices (cMatrix
)
Simple and advanced matrix operations.
Creation and Destruction
Matrix_t* mat_create(int rows, int cols);
void mat_free(Matrix_t* m);
Matrix_t* mat_identity(int n);
Matrix_t* mat_copy(Matrix_t* m);
Display and Information
void mat_print(Matrix_t* m);
double mat_trace(Matrix_t* m);
Basic Operations
Matrix_t* mat_add(Matrix_t* a, Matrix_t* b);
Matrix_t* mat_sub(Matrix_t* a, Matrix_t* b);
Matrix_t* mat_scalar_mul(Matrix_t* a, double k);
Matrix_t* mat_transpose(Matrix_t* a);
Matrix_t* mat_mul(Matrix_t* a, Matrix_t* b);
Advanced Operations
double mat_determinant(Matrix_t* m);
Matrix_t* mat_inverse(Matrix_t* m);
Example
Matrix_t* A = mat_create(2, 2);
A->data[0][0] = 4; A->data[0][1] = 7;
A->data[1][0] = 2; A->data[1][1] = 6;
Matrix_t* Inv = mat_inverse(A);
printf("A:\n");
mat_print(A);
printf("A^-1:\n");
mat_print(Inv);
mat_free(A);
mat_free(Inv);
File Utilities (file_utils
)
Read Entire File
char* file_read_all(const char* filename);
Returns the file content as a string (must be freed with free
).
Write to File
int file_write_all(const char* filename, const char* content);
Writes content
to the file. Returns 0 on success.
Read File Line by Line
char** file_read_lines(const char* filename, int* out_count);
Returns an array of strings (one per line). out_count
stores the number of lines.
Math Functions (math_utils
)
Factorial
int math_factorial(int n);
GCD and LCM
int math_gcd(int a, int b);
int math_lcm(int a, int b);
Mean, Median, Variance
double math_mean(double* arr, int size);
double math_median(double* arr, int size);
double math_variance(double* arr, int size);
String Functions (string_utils
)
Trim Spaces
char* str_strip(char* s);
Case Conversion
char* str_lower(char* s);
char* str_upper(char* s);
Substring Replacement
char* str_replace(char* s, char* old, char* New);
Split and Join
char** str_split(char* s, char* sep, int* out_count);
char* str_join(char* sep, char** parts, int count);