Shape Manipulation
reshape - Change tensor shape
Change the shape of a tensor without changing its data.
transpose - Permute dimensions
Reverse or permute the dimensions of a tensor.
squeeze - Remove single dimensions
Remove dimensions of size 1.
expand_dims - Add dimension
Insert a new dimension of size 1.
Complete Example
#include <tensr/tensr.h>
int main() {
/* Create 1D array */
Tensor* vec = tensr_arange(0, 12, 1, TENSR_FLOAT32, TENSR_CPU);
printf("Original shape: ");
tensr_print(vec);
/* Reshape to 3x4 matrix */
Tensor* mat = tensr_reshape(vec, (size_t[]){3, 4}, 2);
printf("Reshaped to 3x4:\n");
tensr_print(mat);
/* Transpose to 4x3 */
Tensor* mat_t = tensr_transpose(mat, NULL, 0);
printf("Transposed to 4x3:\n");
tensr_print(mat_t);
/* Reshape back to 1D */
Tensor* flat = tensr_reshape(mat_t, (size_t[]){12}, 1);
printf("Flattened:\n");
tensr_print(flat);
/* Cleanup */
tensr_free(vec);
tensr_free(mat);
tensr_free(mat_t);
tensr_free(flat);
return 0;
}
Use Cases
Batch Processing
/* Reshape data for batch processing */
Tensor* data = tensr_rand((size_t[]){100}, 1, TENSR_CPU);
Tensor* batched = tensr_reshape(data, (size_t[]){10, 10}, 2);
/* Now have 10 batches of 10 samples each */
Matrix Operations
/* Prepare vectors for matrix multiplication */
Tensor* vec = tensr_rand((size_t[]){5}, 1, TENSR_CPU);
Tensor* col = tensr_expand_dims(vec, 1); /* Shape: (5, 1) */
Tensor* row = tensr_expand_dims(vec, 0); /* Shape: (1, 5) */
/* Outer product */
Tensor* outer = tensr_matmul(col, row); /* Shape: (5, 5) */