Skip to content

This is a simple dynamic array tool implemented in C with a single header file

License

Notifications You must be signed in to change notification settings

Zncl2222/c_array_tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

licence size language language_percent cmake build

Quality Gate Status Reliability Rating Security Rating Maintainability Rating

c_array_tools

This is a simple dynamic array tool, similar to C++'s std::vector, implemented in C. It is coded using macros to simulate template-like functions found in languages like C++. This allows users to work with arrays in C more easily, without the need to repeatedly declare the data type.

Installation

It's a single-header library. For most basic dynamic array functionality, you only need to include src/c_array.h in your project. If you want to use extended features such as statistical calculations and the mt19937 random number generator, you should also link either src/c_array.c or src/c_array_mt.c when compiling your code. (All the functions are declared in src/c_array.h, so you can call any of them by including the header file src/c_array.h in your source code.)

For example, if you have a program like main.c:

main.c

# include "c_array.h"

int main() {
    c_array_int array;
    mt19937_state state;
    mt19937_init(&state, 1314);

    // This funciton is implemented in c_array_mt.c
    c_array_rand_range(&array, 10, mt19937_get_int32_range(&state, -5, 20));

     // This funciton is implemented in c_array.c
    int sum = c_array_sum(&array);

    c_array_free(&array);
}

compile with gcc (remember to add -lm to link math.h in linux os)

gcc main.c c_array_mt.c c_array.c -lm -o main.out

Because of the typeof and _Generic features in the code, the project has been tested exclusively with the GCC compiler on both Windows (MinGW) and Ubuntu operating systems. (A minimum version of GCC 4.9 is required, as _Generic support was introduced starting from version 4.9.)

Compiler Windows Ubuntu Mac
GNU Compiler Collection (gcc) - âś… Not yet tested
x86_64 mingw series (gcc) âś… - Not yet tested
Visual Studio Build Tools ❌ ❌ Not yet tested

Extension in c_array.c

Extension in c_array_mt.c

Usuage

Here is an example in example.c that demonstrates how to use this library. You can simply use the shell script example.sh (for Linux) or the batch file example.bat to compile and run the program. The example program prints out results to help users understand how it works.

Example

# include <stdio.h>
# include "./src/c_array.h"

int main() {
    printf("\nc_array Example\n\n");

    // Initialize the array. If capacity > 0, then it will initialize with 0 val.
    c_array_int array;
    c_array_init(&array, 0);

    // add element to the back (the idx of back is the current size of array)
    c_array_push_back(&array, 10);
    c_array_push_back(&array, 2222);
    c_array_push_back(&array, -1024);

    // print the array in this style -> arr = [1, 2, 3]
    printf("After push back\n");
    c_array_printf(array, "%d");
    printf("Array size = %zu\n", array.size);
    printf("Array capacity = %zu\n\n", array.capacity);

    // assign value directly or via function.
    // assign by function can help to check if the memory is allocated.
    c_array_assign(&array, 0, 999);
    printf("Use c_array_assign: \n");
    c_array_print(array, "%d");
    printf("Assign directly: \n");
    array.data[0] = 111;
    c_array_printf(array, "%d");

    // pop the last element in array.
    // That element will be reseted to 0, and - 1 to the array size.
    c_array_pop_back(&array);
    printf("\nAfter pop_back\n");
    c_array_printf(array, "%d");
    printf("Array size = %zu\n", array.size);
    printf("Array capacity = %zu\n\n", array.capacity);

    // insert the element at certain idx of the array
    // c_array_insert(array, index, value)
    c_array_insert(&array, 1, 90);
    printf("\nAfter insert\n");
    c_array_printf(array, "%d");
    printf("Array size = %zu\n", array.size);
    printf("Array capacity = %zu\n\n", array.capacity);

    // remove the element at certain idx of the array
    // c_array_remove(array, index)
    c_array_remove(&array, 1);
    printf("\nAfter remove\n");
    c_array_printf(array, "%d");
    printf("Array size = %zu\n", array.size);
    printf("Array capacity = %zu\n\n", array.capacity);

    // adjust the array capacity
    // should be careful the value was not initialize
    c_array_resize(&array, 20);
    printf("\nAfter resize\n");
    c_array_printf(array, "%d");
    printf("Array size = %zu\n", array.size);
    printf("Array capacity = %zu\n\n", array.capacity);

    // adjust the array size
    c_array_set_size(&array, 20);
    printf("\nAfter set size\n");
    c_array_printf(array, "%d");
    printf("Array size = %zu\n", array.size);
    printf("Array capacity = %zu\n\n", array.capacity);

    // Remember to free the memory after you don't need it.
    c_array_free(&array);

    return 0;
}

c_array(T) & c_array_init(arr, c)

  • params:
    T: the data type of the array -> (int, long long, float, double etc..)
    arr: c_array structure -> (c_array)
    c: the capacity for init -> (size_t)

Create the array with given datatype by c_array(datatype) arr;. You should initialize it before use. arr.size is the logical size of the array and arr.capacity is the whole space for the container. However it is better to use c_array_int, c_array_float, c_array_doulbe which was defined by typdef. You can also define your own type like typedef c_array_long c_array(long);.

Initialize the array with the given size & capacity by c_array_init(&arr, size).

int main() {
    c_array(int) arr;  // You can use this method to declare array, but it can't be used in other function which need to declare the dtype.
    c_array_int array; // This is the dtype of c_array(int). You can use this in any function which need to declare dtype like void foo(c_array_int array, int num);
    c_array_init(&arr, 10); // arr[0] ~ arr[9] will be initialized with 0
    c_array_free(&arr); // free memory
    return 0;
}

c_array_empty_init(arr, c)

  • params:
    T: the data type of the array -> (int, long long, float, double etc..)
    arr: c_array structure -> (c_array)
    c: the capacity for init -> (size_t)

Allocate memory of the array with the given size & capacity (No initialize value).

int main() {
    c_array(int) arr;  // You can use this method to declare array, but it can't be used in other function which need to declare the dtype.
    c_array_int array; // This is the dtype of c_array(int). You can use this in any function which need to declare dtype like void foo(c_array_int array, int num);
    c_array_init(&arr, 10); // arr[0] ~ arr[9] will be initialized with 0
    c_array_free(&arr); // free memory
    return 0;
}

c_array_copy(arr_old, arr_new)

  • params:
    arr_old: c_array structure you want to copy -> (c_array)
    arr_new: new c_array structure -> (c_array)

Copy an array to the target arry.

int main() {
    c_array_int arr;
    c_array_init(&arr, 0); // arr[0] ~ arr[9] will be initialized with 0
    c_array_push_back(&arr, 7);
    c_array_push_back(&arr, 8);
    c_array_push_back(&arr, 9);

    c_array_int arr_new;  // declare arr_new without initialize.
    c_array_copy(&arr, &arr_new); // copy the memory from arr to arr_new
    c_array_print(arr_new, "%d"); // arr_new = [7, 8, 9], size and capacity are also equals to arr
    c_array_free(&arr); // free memory

    return 0;
}

c_array_capacity(arr) & c_array_size(arr)

  • params:
    arr: c_array structure -> (c_array)

Get array capacity and size.

int main() {
    c_array_int arr;
    c_array_init(&arr, 10);
    // Get size and capacity by macro
    int size = c_array_size(&arr); // equals to 10
    int capacity = c_array_capacity(&arr); // equals to 10

    // Get these attributes by struct
    size = arr.size;
    capacity = arr.capacity;
    c_array_free(&arr);
    return 0;
}

c_array_assign(arr, idx, val)

  • params:
    arr: c_array structure -> (c_array)
    idx: index of the array -> (int)
    val: value -> (int, long long, float, double etc..)

Assign the value at given location. This funciton will help to check if the memory is allocated before assigning the value. If memory is not allocated, then the program will abort with error.

int main() {
    c_array_int arr;
    c_array_init(&arr, 10);
    c_array_assign(&arr, 0, 150); // arr.data[0] now is 150
    c_array_assign(&arr, 11, 150); // program abort due to the size of array is only 10.

    // Assign value in struct directly
    arr.data[0] = -150; // warning: this method didn't check if the memory is allocated.
    c_array_free(&arr); // free memory
    return 0;
}

c_array_grow

This macro is for array to grow the capacity while the memory is not enough. User don't have to call it manually. If you want to adjust the size or the capacity, please refer to the c_array_resize and c_array_set_size. Features in c_array_tools will grow the capacity while the memory space is not enough. By default, each growing will double the current capacity.


c_array_resize(arr, c) & c_array_set_size(arr, size)

  • params:
    arr: c_array structure -> (c_array)
    c: capacity -> (size_t)
    size: size -> (size_t)

Resize the capacity to given value.

int main() {
    c_array_int arr;
    c_array_init(&arr, 0); // capacity and size are both 0 now
    c_array_resize(&arr, 10); // capacity become 10 and size remain 0.
    c_array_set_size(&arr, 8); // capacity is 10 and size is 8 (element will initialize with 0)
    c_array_free(&arr); // free memory
    return 0;
}

c_array_push_back(arr, val)

  • params:
    arr: c_array structure -> (c_array)
    val: value -> (int, long long, float, double etc..)

Push the element at the end of the array (depend on arr.size). If the capacity is not enough, this funciton will call c_array_grow to enlarge the capacity automatically. Notice if you need to push_back frequently, please use c_array_resize to set enough capacity to avoid the realloc of memory.

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1); // capacity and size is 1 now
    c_array_push_back(&arr, 2); // capacity and size is 2 now
    c_array_push_back(&arr, 3); // capacity is 4 and size is 3 now
    c_array_free(&arr); // free memory
    return 0;
}

c_array_pop_back(arr)

  • params:
    arr: c_array structure -> (c_array)

Remove the last element of the array (depend on arr.size).

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3); // arr = [1, 2, 3]
    c_array_pop_back(&arr);     // arr = [1, 2]
    c_array_free(&arr); // free memory
    return 0;
}

c_array_move_left & c_array_move_right

These are the feature for c_array_insert and c_array_remove.


c_array_insert(arr, idx, val)

  • params:
    arr: c_array structure -> (c_array)
    idx: index of the array -> (int)
    val: value -> (int, long long, float, double etc..)

Insert the element at given index.

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);  // arr = [1, 2, 3]
    c_array_insert(&arr, 1, 99); // arr = [1, 99, 2, 3]
    c_array_free(&arr); // free memory
    return 0;
}

c_array_remove(arr, idx)

  • params:
    arr: c_array structure -> (c_array)
    idx: index of the array -> (int)

Remove the elemnt at given index.

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3); // arr = [1, 2, 3]
    c_array_remove(&arr, 1);    // arr = [1, 3]
    c_array_free(&arr); // free memory
    return 0;
}

c_array_print(arr) & c_array_printf(arr, format)

  • params:
    arr: c_array structure -> (c_array)
    format: format specifier -> (char*)

Print the array with clean style.

int main() {
    c_array_int arr_print_test;
    c_array(&arr_print_test, 0);
    c_array_push_back(&arr_print_test, 1);
    c_array_push_back(&arr_print_test, 2);
    c_array_push_back(&arr_print_test, 3);
    c_array_printf(arr_print_test, "%d");   // Custom format to print the array
    // Results shows: arr_print_test = [1, 2, 3]

    c_array_print(arr_print_test); // Default format like %d %lld %f %lf. You can add more format in c_array_autoformat macro
    // Results shows: arr_print_test = [1, 2, 3]

    c_array_free(&arr); // free memory
    return 0;
}

c_array_empty(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: 0 or 1 -> (int)

Check if array is empty, if yes return 1 else 0

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    int e = c_array_empty(&arr); // e = 1
    c_array_free(&arr); // free memory
    return 0;
}

c_array_swap(arr, idx1, idx2)

  • params:
    arr: c_array structure -> (c_array)
    idx1: index 1 -> (int)
    idx2: index 2 -> (int)

Swap the data in array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3); // arr = [1, 2, 3]
    c_array_swap(&arr, 0, 1);   // arr = [2, 1, 3]
    c_array_free(&arr); // free memory
    return 0;
}

c_array_reverse(arr)

  • params:
    arr: c_array structure -> (c_array)

Reverse the array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3); // arr = [1, 2, 3]
    c_array_reverse(&arr);      // arr = [3, 2, 1]
    c_array_free(&arr); // free memory
    return 0;
}

c_array_concat(arr1, arr2)

  • params:
    arr1: c_array structure (result will concat at this array) -> (c_array)
    arr2: c_array structure -> (c_array)

Concat two arrays.

int main() {
    c_array_int arr;
    c_array_int arr2;
    c_array(&arr, 0);
    c_array(&arr2, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr2, 9);
    c_array_push_back(&arr2, -45);

    c_array_concat(&arr1, &arr2); // arr1 = [1, 2, 9, -45], arr2 = [9, -45]

    c_array_free(&arr); // free memory
    c_array_free(&arr2); // free memory
    return 0;
}

c_array_free(arr)

  • params:
    arr: c_array structure -> (c_array)

Free the memory allocated from heap.

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_free(&arr);
    return 0;
}

c_array_qsort(arr) & c_array_msort(arr)

  • params:
    arr: c_array structure -> (c_array)

Sort the array by the qsort function in <stdlib.h>, or sort the array by the 'merge sort'.

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 99);
    c_array_push_back(&arr, -2);
    c_array_push_back(&arr, 2); // arr = [99, -2, 2]
    c_array_qsort(&arr); // arr = [-2, 2, 99]

    c_array_push_back(&arr, -91);
    c_array_push_back(&arr, 1); // arr = [-2, 2, 99, -91, 1]
    c_array_msort(&arr); // arr = [-91, -2, 1, 2, 99]
    c_array_free(&arr); // free memory
    return 0;
}

c_array_sum(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: sum -> (int, long long, float, double etc..)

Get the sum of an array.

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);
    int sum = c_array_sum(&arr); // sum = 6
    c_array_free(&arr); // free memory
    return 0;
}

c_array_max(arr) & c_array_min(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: min or max -> (int, long long, float, double etc..)

Get min or max value of an array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);
    int max = c_array_max(&arr); // max = 3
    int min = c_array_min(&arr); // min = 1
    c_array_free(&arr); // free memory
    return 0;
}

c_array_maxmin(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: pointer contain min and max (arr[0] = min, arr[1] = max) -> (int, long long, float*, double* etc..**)

Get min or max value of an array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);
    int* max_min = c_array_maxmin(&arr); // max_min[0] = 1, max_min[1] = 3
    c_array_free(&arr); // free memory
    free(max_min);
    return 0;
}

c_array_argmax(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: pointer contain the index of max value in array -> (int, long long, float*, double* etc..**)

Get index of max value of an array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);
    c_array_push_back(&arr, 3);
    int* max_indices = c_array_argmax(&arr); // max_indices[0] = 2, max_indices[1] = 3
    c_array_free(&arr); // free memory
    free(max_min); // You should free the max_indices manually.
    return 0;
}

c_array_argmin(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: pointer contain the index of min value in array -> (int, long long, float*, double* etc..**)

Get the index of min value of an array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);
    c_array_push_back(&arr, 3);
    int* min_indices = c_array_argmin(&arr); // min_indices[0] = 2, min_indices[1] = 3
    c_array_free(&arr); // free memory
    free(min_min); // You should free the min_indices manually.
    return 0;
}

c_array_mean(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: mean value -> (mean_t)

Get mean value of an array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);
    mean_t mean = c_arry_mean(&arr); // mean = 2
    c_array_free(&arr); // free memory
    return 0;
}

c_array_std(arr) & c_array_var(arr)

  • params:
    arr: c_array structure -> (c_array)
  • return: standard deviation or variance -> (std_t, var_t)

Get the standard deviation or the variance of an array

int main() {
    c_array_int arr;
    c_array(&arr, 0);
    c_array_push_back(&arr, 1);
    c_array_push_back(&arr, 2);
    c_array_push_back(&arr, 3);
    std_t std = c_array_std(&arr); // std = 0.8164
    var_t var = c_array_var(&arr); // var =  0.6667
    c_array_free(&arr); // free memory
    return 0;
}

void mt19937_init(mt19937_state* state, unsigned int seed)

  • params:
    state: state of random seed generator -> (mt19937_state*)
    seed: random seed number -> (unsigned int)
  • return: void -> (void)

Create the random seed generator with the given random seed.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    return 0;
}

unsigned long mt19937_generate(mt19937_state* state)

  • params:
    state: state of random seed generator -> (mt19937_state*)
  • return: random value -> (unsigned long)

Get the long or integer type random value between 0 ~ 4294967296.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    long num = mt19937_generate(&state);
    return 0;
}

float mt19937_get_float(mt19937_state* state)

  • params:
    state: state of random seed generator -> (mt19937_state*)
  • return: random value -> (float)

Get the float type random value between 0 ~ 1.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    float num = mt19937_get_float(&state);
    return 0;
}

double mt19937_get_double(mt19937_state* state)

  • params:
    state: state of random seed generator -> (mt19937_state*)
  • return: random value -> (double)

Get the double type random value between 0 ~ 1.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    double num = mt19937_get_double(&state);
    return 0;
}

int mt19937_get_int32_range(mt19937_state* state, int m, int n)

  • params:
    state: state of random seed generator -> (mt19937_state*)
    m: min value -> (int)
    n: max value -> (int)
  • return: random value -> (int)

Get the random value between given min and max.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    int num = mt19937_get_int32_range(&state, -10, 20); // -10 <= num <= 20
    return 0;
}

float mt19937_get_float_range(mt19937_state* state, float m, float n)

  • params:
    state: state of random seed generator -> (mt19937_state*)
    m: min value -> (float)
    n: max value -> (float)
  • return: random value -> (float)

Get the random value between given min and max.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    float num = mt19937_get_float_range(&state, -5.65, 20.11); // -5.65 <= num < 20.11
    return 0;
}

double mt19937_get_double_range(mt19937_state* state, double m, double n)

  • params:
    state: state of random seed generator -> (mt19937_state*)
    m: min value -> (double)
    n: max value -> (double)
  • return: random value -> (double)

Get the random value between given min and max.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    double num = mt19937_get_double_range(&state, -5.65, 20.11); // -5.65 <= num < 20.11
    return 0;
}

double random_normal(mt19937_state* state)

  • params:
    state: state of random seed generator -> (mt19937_state*)
  • return: random value -> (double)

Draw the random value from standard normal distribution.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    double num = random_normal(&state);
    return 0;
}

c_array_randnormal(arr, c, rng_state)

  • params:
    arr: c_array structure -> (c_array)
    c: capacity and size for init -> (int)
    rng_state: state of random seed generator -> (mt19937_state*)

Initialize the c_array with the random value from standard normal distribution.

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    c_array_double array;
    c_array_randnormal(&array, 10, &state);
    c_array_free(&array); // free memory
    return 0;
}

c_array_rand_range(arr, c, rng_function)

  • params:
    arr: c_array structure -> (c_array)
    c: capacity and size for init -> (int)
    rng_function: rng_function -> (rng_function)

Initialize the c_array with the random value from given random function

int main() {
    mt19937_state state;
    mt19937_init(&state, 12345);
    c_array_double array;
    double num = random_normal(&state);
    c_array_rand_range(&arr, c, mt19937_get_int32_range(&state, -5, 20));
    c_array_free(&array); // free memory
    return 0;
}

c_array_search(arr, target)

  • params:
    arr: c_array structure -> (c_array)
    target: target you want to search -> (int, long long, float, double etc..)
  • return: Index of the target -> (int)

Initialize the c_array with the random value from given random function

int main() {
    c_array_int array;
    c_array_init(&array, 0);
    for (int i = 0; i < 5; i++) {
        array.data[i] = i * 2;
    }
    int idx = c_array_search(&array, 8); // idx = 4
    c_array_free(&array); // free memory
    return 0;
}

c_matrix_init(mat, rows, cols)

  • params:
    mat: c_matrix structure -> (c_matrix)
    rows: number of rows -> (int)
    cols: number of cols -> (int)

Initialize c_matrix with given rows and cols.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2); // matrix will init with value = 0
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_copy(mat_old, mat_new)

  • params:
    mat_old: c_matrix structure to be copied -> (c_matrix)
    mat_new: The new clone of c_matrix structure -> (c_matrix)

Copy memory of mat_old to mat_new.

int main() {
    c_matrix_int mat;
    c_matrix_int mat_new;
    c_matrix_init(&mat, 4, 5);
    c_matrix_copy(&mat, &mat_new); // mat_new row = 4, mat_new col = 5
    c_matrix_free(&mat); // free memory
    c_matrix_free(&mat_new);
    return 0;
}

c_matrix_empty_init(mat, rows, cols)

  • params:
    mat: c_matrix structure -> (c_matrix)
    rows: number of rows -> (int)
    cols: number of cols -> (int)

Allocate memory of the matrix with given rows and cols (No initialize value).

int main() {
    c_matrix_int mat;
    c_matrix_empty_init(&mat, 2, 2);
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_randnormal(mat, r, c, rng_state)

  • params:
    mat: c_matrix structure -> (c_matrix)
    r: number of rows -> (int)
    c: number of cols -> (int)
    rng_state: state of random seed generator -> (mt19937_state*)

Initialize c_matrix with random value draw from noraml distribution.

int main() {
    c_matrix_double mat;
    mt19937_state rng;
    unsigned int seed = 12345;
    mt19937_init(&rng, seed);
    c_matrix_randnormal(&mat, 10, 10, &rng);
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_rand_range(mat, r, c, rng_function)

  • params:
    mat: c_matrix structure -> (c_matrix)
    r: number of rows -> (int)
    c: number of cols -> (int)
    rng_state: state of random seed generator -> (mt19937_state*)

Initialize the c_matrix with the random value from given random function.

int main() {
    c_matrix_int mat;
    mt19937_state rng;
    unsigned int seed = 12345;
    mt19937_init(&rng, seed);
    c_matrix_rand_range(&mat, r, c, mt19937_get_int32_range(&state, -5, 20));
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_assign(mat, rows, cols, val)

  • params:
    mat: c_matrix structure -> (c_matrix)
    rows: number of rows -> (int)
    cols: number of cols -> (int)
    val: value -> (int, long long, float, double etc..)

Assign given value at given row and col.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    c_matrix_assign(&mat, 2, 2, 10);
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_print(mat) & c_matrix_printf(mat, format)

  • params:
    mat: c_matrix structure -> (c_matrix)
    format: format specifier -> (char*)

Print matirx with clean style.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = i + j;
        }
    }

    c_matrix_print(mat);
    c_matrix_printf(mat, "%d");

    c_array_free(&arr); // free memory
    return 0;
}

c_matrix_free(mat)

  • params:
    mat: c_matrix structure -> (c_matrix)

Free memory

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = i + j;
        }
    }
    c_matrix_free(&mat); // free memory
    return 0;
}

c_array_matrix_form(arr, n_row)

  • params:
    arr: c_matrix structure -> (c_array)
    n_row: number of rows -> (int)
  • return: c_matrix -> (c_matrix)

Reshape array to matrix with given row numbers.

int main() {
    c_array_int arr;
    c_matrix_init(&mat, 20);
    for (int i = 0; i < arr.size; i++) {
        arr.data[i] = i;
    }
    c_matrix_int mat = c_array_matrix_form(&arr, 4); // mat.rows = 4, mat.cols = 5
    c_array_free(&arr);
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_flatten(mat)

  • params:
    mat: c_matrix structure -> (c_matrix)
  • return: c_array -> (c_array)

Flatten c_matrix and return c_array

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = i + j;
        }
    }
    c_array_int arr = c_matrix_flatten(&mat);
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_reshape(mat, row, col)

  • params:
    mat: c_matrix structure -> (c_matrix)
    row: row number -> (int)
    col: col number -> (int)
  • return: c_matrix -> (c_matrix)

Reshape c_matrix.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = i + j;
        }
    }
    c_array_int arr = c_matrix_reshape(&mat, 4, 1);
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_sum(mat)

  • params:
    mat: c_matrix structure -> (c_matrix)
  • return: sum -> (int, long long, float, double etc..)

Calculate the sum of the matrix.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = 5;
        }
    }
    int sum_i = c_matrix_sum(&mat); // sum i = 20
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_mean(mat)

  • params:
    mat: c_matrix structure -> (c_matrix)
  • return: mean -> (mean_t)

Calculate the mean value of the matrix.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = 5;
        }
    }
    mean_t mean = c_matrix_mean(&mat); // mean = 5
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_max(mat) and c_matrix_min(mat)

  • params:
    mat: c_matrix structure -> (c_matrix)
  • return: max or min -> (int, long long, float, double etc..)

Get the max or min of the matrix.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 2, 2);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = i + j;
        }
    }
    int max = c_matrix_max(&mat); // mean = 2
    int min = c_matrix_min(&mat); // min = 0
    c_matrix_free(&mat); // free memory
    return 0;
}

c_matrix_var(mat) and c_matrix_std(mat)

  • params:
    mat: c_matrix structure -> (c_matrix)
  • return: std or var -> (std_t, var_t)

Calculate the variance or the standard deviation of the matrix.

int main() {
    c_matrix_int mat;
    c_matrix_init(&mat, 3, 3);
    for (int i = 0; i < mat.rows; i++) {
        for (int j = 0; j < mat.cols; j++) {
            mat.data[i][j] = i + j;
        }
    }
    var_t var = c_matrix_var(&mat); // var = 1.3333333
    std_t std = c_matrix_std(&mat); // std = 1.154701
    c_matrix_free(&mat); // free memory
    return 0;
}

About

This is a simple dynamic array tool implemented in C with a single header file

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages