This library provides an implementation for creating, manipulating, and performing arithmetic operations on tensors in C. This library uses my own 'c utlities library' as a dependency, except for which everything is self-dependent. Below is an overview of the key features and functionality.
- Create tensors with specified dimensions and initialize them with same value in heap.
- Example:
Tensor t = tensor_create(allocr, 1.f, 2,3,4);
- Supports indexing of tensor elements directly via macros as:
tensor_inx(t, 1,1,1) = 34;
tensor_inx(t, 0,0,0) = 12;
- Print tensors in a human-readable format for debugging or visualization.
- Example:
tensor_print(allocr, t);
- Change the order of dimensions in a tensor using permutation.
- Example:
tensor_permute(t, 1, 2);
Perform element-wise operations on tensors:
- Addition: Add two tensors element-wise.
Tensor t3 = tensor_add(allocr, t1, t2);
- Multiplication: Multiply two tensors element-wise.
Tensor t5 = tensor_prod(allocr, t3, t4);
- Generate random tensors within specified ranges:
Tensor t1 = tensor_random(allocr, 0.f, 100.f, DIM);
- Create and initialize stack-based tensors with multi-dimensional data.
- Example:
Tensor t = MAKE_STACK_TENSOR(({{{1,2}, {2,3}, {4,5}},
{{-3,2}, {-8,7}, {0,-9}},
{{1,8}, {4,-2}, {7,-3}}}),
3,4,2);
- Convert tensors into contiguous memory layout for efficient storage and access.
- Example:
Tensor dt = tensor_contiguous(allocr, t);
- Retrieve and print storage shape and stride information for tensors.
- Example:
printf("\nTensor storage shape: %zu", t.storage.count);
print_tensor_inx(t.shape);
print_tensor_inx(t.stride);
Demonstrates basic tensor creation and manipulation:
- Create a tensor with dimensions
(2,3,4)
. - Modify specific elements using indexing.
- Permute dimensions for reshaping.
Illustrates stack-based tensor creation:
- Create tensors with data directly from multi-dimensional arrays.
- Perform permutation on higher-dimensional tensors.
Showcases element-wise arithmetic operations:
- Generate random tensors within specified ranges.
- Perform addition and multiplication between two tensors.
To use this library:
-
Ensure the utilities library is downloaded from [[https://github.com/bipul018/c-utils]]
-
Clone this repo or download the files
tensor.h
andtensor.c
-
Include the required headers:
#include "tensor.h"
-
Use the provided functions to create and manipulate tensors.
-
Compile tensor.c along with your files.