Txeo v0.1
A Developer-Friendly TensorFlow C++ Wrapper
Loading...
Searching...
No Matches
txeo::TensorFunc< T > Class Template Reference

A utility class for common math functions on tensors. More...

#include <TensorFunc.h>

Collaboration diagram for txeo::TensorFunc< T >:
Collaboration graph

Public Member Functions

 TensorFunc (const TensorFunc &)=delete
 
 TensorFunc (TensorFunc &&)=delete
 
TensorFuncoperator= (const TensorFunc &)=default
 
TensorFuncoperator= (TensorFunc &&)=delete
 
 ~TensorFunc ()=default
 

Static Public Member Functions

static txeo::Tensor< Tpower_elem (const txeo::Tensor< T > &tensor, const T &exponent)
 Returns the element-wise potentiation of a tensor.
 
static txeo::Tensor< T > & power_elem_by (txeo::Tensor< T > &tensor, const T &exponent)
 Performs element-wise potentiation of the tensor (in-place)
 
static txeo::Tensor< Tsquare (const txeo::Tensor< T > &tensor)
 Computes the element-wise square of a tensor.
 
static txeo::Tensor< T > & square_by (txeo::Tensor< T > &tensor)
 Computes the element-wise square of a tensor in-place.
 
static txeo::Tensor< Tsqrt (const txeo::Tensor< T > &tensor)
 Computes the element-wise square root of a tensor.
 
static txeo::Tensor< T > & sqrt_by (txeo::Tensor< T > &tensor)
 Computes the element-wise square root of a tensor in-place.
 
static txeo::Tensor< Tabs (const txeo::Tensor< T > &tensor)
 Computes the element-wise absolute value of a tensor.
 
static txeo::Tensor< T > & abs_by (txeo::Tensor< T > &tensor)
 Computes the element-wise absolute value of a tensor in-place.
 
static txeo::Tensor< Tpermute (const txeo::Tensor< T > &tensor, const std::vector< size_t > &axes)
 Permutes the axes of a tensor.
 
static txeo::Tensor< T > & permute_by (txeo::Tensor< T > &tensor, const std::vector< size_t > &axes)
 Permutes the axes of a tensor in-place.
 
static txeo::Tensor< T > & normalize_by (txeo::Tensor< T > &tensor, size_t axis, txeo::NormalizationType type)
 Normalizes the input tensor along a specified axis in-place.
 
static txeo::Tensor< Tnormalize (const txeo::Tensor< T > &tensor, size_t axis, txeo::NormalizationType type)
 Creates a normalized copy of the input tensor along a specified axis.
 
static std::vector< std::function< T(const T &)> > make_normalize_functions (const txeo::Tensor< T > &tensor, size_t axis, txeo::NormalizationType type)
 Construct the normalization functions for each dimension of the specified axis.
 
static Tensor< T > & normalize_by (txeo::Tensor< T > &tensor, txeo::NormalizationType type)
 Normalizes the entire tensor in-place (global normalization)
 
static txeo::Tensor< Tnormalize (const txeo::Tensor< T > &tensor, txeo::NormalizationType type)
 Creates a normalized copy of the entire tensor (global normalization)
 
static std::function< T(const T &)> make_normalize_function (const txeo::Tensor< T > &tensor, txeo::NormalizationType type)
 Construct the normalization function for the elements of the specified tensor.
 
static txeo::Matrix< Ttranspose (const txeo::Matrix< T > &matrix)
 Transposes a matrix.
 
static txeo::Matrix< T > & transpose_by (txeo::Matrix< T > &matrix)
 Transposes a matrix in-place.
 
static txeo::Matrix< Tcompute_gram_matrix (const txeo::Matrix< T > &matrix)
 Computes the Gram matrix ( \(A^TA\)) of the input matrix.
 

Detailed Description

template<typename T>
class txeo::TensorFunc< T >

A utility class for common math functions on tensors.

This class provides static methods for common tensor functions, such as square.

Template Parameters
TThe data type of the tensor elements (e.g., int, double).

Definition at line 27 of file TensorFunc.h.

Constructor & Destructor Documentation

◆ TensorFunc() [1/2]

template<typename T >
txeo::TensorFunc< T >::TensorFunc ( const TensorFunc< T > &  )
delete

◆ TensorFunc() [2/2]

template<typename T >
txeo::TensorFunc< T >::TensorFunc ( TensorFunc< T > &&  )
delete

◆ ~TensorFunc()

template<typename T >
txeo::TensorFunc< T >::~TensorFunc ( )
default

Member Function Documentation

◆ abs()

template<typename T >
static txeo::Tensor< T > txeo::TensorFunc< T >::abs ( const txeo::Tensor< T > &  tensor)
static

Computes the element-wise absolute value of a tensor.

Parameters
tensorThe input tensor to be modified.

Example Usage:

txeo::Tensor<int> tensor({3}, {-1, 2, -3});
TensorOp<int>::abs_by(tensor);
// tensor = [1, 2, 3]
A container for managing training, evaluation, and test data splits.
Definition DataTable.h:24

◆ abs_by()

template<typename T >
static txeo::Tensor< T > & txeo::TensorFunc< T >::abs_by ( txeo::Tensor< T > &  tensor)
static

Computes the element-wise absolute value of a tensor in-place.

Parameters
tensorThe input tensor to be modified.

Example Usage:

txeo::Tensor<int> tensor({3}, {-1, 2, -3});
TensorOp<int>::abs_by(tensor);
// tensor = [1, 2, 3]

◆ compute_gram_matrix()

template<typename T >
static txeo::Matrix< T > txeo::TensorFunc< T >::compute_gram_matrix ( const txeo::Matrix< T > &  matrix)
static

Computes the Gram matrix ( \(A^TA\)) of the input matrix.

Parameters
matrixInput matrix where rows represent samples and columns represent features
Returns
New matrix containing the Gram matrix (square matrix of size n x n, where n is the number of columns)
Exceptions
TensorFuncError

The Gram matrix is calculated as the matrix product of the transpose of the input matrix with the original matrix. This produces a symmetric positive semi-definite matrix where each element (i,j) represents the inner product of column i and column j from the original matrix.

Example Usage:

// Create a 2x3 matrix (2 samples, 3 features)
txeo::Matrix<double> A(2, 3, {1.0, 2.0, 3.0,
4.0, 5.0, 6.0});
// Compute Gram matrix (will be 3x3)
// Resulting Gram matrix:
// [17.0 22.0 27.0]
// [22.0 29.0 36.0]
// [27.0 36.0 45.0]
// Verify diagonal elements (squared norms of columns)
assert(G(0,0) == 1.0*1.0 + 4.0*4.0); // 17.0
assert(G(1,1) == 2.0*2.0 + 5.0*5.0); // 29.0
assert(G(2,2) == 3.0*3.0 + 6.0*6.0); // 45.0
A class representing a matrix, derived from Tensor.
Definition Matrix.h:46
Note
Common applications include covariance matrices in statistics and kernel methods in machine learning.

◆ make_normalize_function()

template<typename T >
static std::function< T(const T &)> txeo::TensorFunc< T >::make_normalize_function ( const txeo::Tensor< T > &  tensor,
txeo::NormalizationType  type 
)
static

Construct the normalization function for the elements of the specified tensor.

Parameters
tensorTensor to be inspected
typeNOrmalization type
Returns
std::function<T(const T &)>
Exceptions
std::TensorFuncError

◆ make_normalize_functions()

template<typename T >
static std::vector< std::function< T(const T &)> > txeo::TensorFunc< T >::make_normalize_functions ( const txeo::Tensor< T > &  tensor,
size_t  axis,
txeo::NormalizationType  type 
)
static

Construct the normalization functions for each dimension of the specified axis.

Parameters
tensorTensor to be inspected
axisAxis from which the normalization functions will be computed
typeNOrmalization type
Returns
std::vector<std::function<T(const T &)>>
Exceptions
std::TensorFuncError

◆ normalize() [1/2]

template<typename T >
static txeo::Tensor< T > txeo::TensorFunc< T >::normalize ( const txeo::Tensor< T > &  tensor,
size_t  axis,
txeo::NormalizationType  type 
)
static

Creates a normalized copy of the input tensor along a specified axis.

Parameters
tensorThe input tensor to normalize
axisThe dimension along which to apply normalization
typeThe normalization method (MIN_MAX or Z_SCORE)
Returns
New tensor containing normalized values
Exceptions
std::TensorFuncError

Example Usage:

// Example: Min-max normalization of a vector
txeo::Tensor<float> vec({2.0f, 4.0f, 6.0f}); // min=2, max=6
// normalized contains [0.0, 0.5, 1.0]
static txeo::Tensor< T > normalize(const txeo::Tensor< T > &tensor, size_t axis, txeo::NormalizationType type)
Creates a normalized copy of the input tensor along a specified axis.

◆ normalize() [2/2]

template<typename T >
static txeo::Tensor< T > txeo::TensorFunc< T >::normalize ( const txeo::Tensor< T > &  tensor,
txeo::NormalizationType  type 
)
static

Creates a normalized copy of the entire tensor (global normalization)

Parameters
tensorThe input tensor to normalize
typeThe normalization method (MIN_MAX or Z_SCORE)
Returns
New tensor containing normalized values
Exceptions
std::TensorFuncError

Example Usage:

// Example: Global Z-score normalization
txeo::Tensor<float> cube({{{1.0f, 2.0f}, {3.0f, 4.0f}}, {{5.0f, 6.0f}, {7.0f, 8.0f}}});
// result contains values with μ=4.5 and σ=2.449

◆ normalize_by() [1/2]

template<typename T >
static txeo::Tensor< T > & txeo::TensorFunc< T >::normalize_by ( txeo::Tensor< T > &  tensor,
size_t  axis,
txeo::NormalizationType  type 
)
static

Normalizes the input tensor along a specified axis in-place.

Parameters
[in,out]tensorThe tensor to be normalized (modified in-place)
axisThe dimension along which to apply normalization
typeThe normalization method from NormalizationType enum:
  • MIN_MAX: Scales values to [0, 1] range
  • Z_SCORE: Standardizes to mean=0, std=1
Returns
Reference to the modified input tensor
Exceptions
std::TensorFuncError

Example Usage:

// Example: Z-score normalization along columns (axis=1)
txeo::Tensor<double> matrix({{1.0, 2.0}, {3.0, 4.0}}); // 2x2 matrix
// Column 0 becomes [-1.0, 1.0], Column 1 becomes [-1.0, 1.0]
static txeo::Tensor< T > & normalize_by(txeo::Tensor< T > &tensor, size_t axis, txeo::NormalizationType type)
Normalizes the input tensor along a specified axis in-place.

◆ normalize_by() [2/2]

template<typename T >
static Tensor< T > & txeo::TensorFunc< T >::normalize_by ( txeo::Tensor< T > &  tensor,
txeo::NormalizationType  type 
)
static

Normalizes the entire tensor in-place (global normalization)

Parameters
[in,out]tensorThe tensor to be normalized (modified in-place)
typeThe normalization method (MIN_MAX or Z_SCORE)
Returns
Reference to the modified input tensor
Exceptions
std::TensorFuncError

Example Usage:

// Example: Global min-max normalization
txeo::Tensor<double> data({{10.0, 20.0}, {30.0, 40.0}}); // min=10, max=40
// data now contains [[0.0, 0.333], [0.666, 1.0]]

◆ operator=() [1/2]

template<typename T >
TensorFunc & txeo::TensorFunc< T >::operator= ( const TensorFunc< T > &  )
default

◆ operator=() [2/2]

template<typename T >
TensorFunc & txeo::TensorFunc< T >::operator= ( TensorFunc< T > &&  )
delete

◆ permute()

template<typename T >
static txeo::Tensor< T > txeo::TensorFunc< T >::permute ( const txeo::Tensor< T > &  tensor,
const std::vector< size_t > &  axes 
)
static

Permutes the axes of a tensor.

Parameters
tensorThe input tensor.
axesThe new order of the tensor axes. Must be a valid permutation of the tensor's dimensions.
Returns
A new tensor with the axes permuted.
Exceptions
std::invalid_argumentIf the axes are invalid (e.g., size mismatch or out of range).

Example Usage:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
});
// The new postion of axis 1 is zero, of axis 2 is one and of axis zero is 2
auto result = TensorFunc<int>::permute(tensor, {1, 2, 0});
// result shape: (3, 4, 2)
static txeo::Tensor< T > permute(const txeo::Tensor< T > &tensor, const std::vector< size_t > &axes)
Permutes the axes of a tensor.

◆ permute_by()

template<typename T >
static txeo::Tensor< T > & txeo::TensorFunc< T >::permute_by ( txeo::Tensor< T > &  tensor,
const std::vector< size_t > &  axes 
)
static

Permutes the axes of a tensor in-place.

Parameters
tensorThe input tensor to be modified.
axesThe new order of axes. Must be a valid permutation of the tensor's dimensions.
Returns
A reference to the modified tensor.
Exceptions
std::TensorFuncError

Example Usage:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24
});
// The new postion of axis 1 is zero, of axis 2 is one and of axis zero is 2
TensorFunc<int>::permute_by(tensor, {1, 2, 0});
// tensor shape after permutation: (3, 4, 2)
static txeo::Tensor< T > & permute_by(txeo::Tensor< T > &tensor, const std::vector< size_t > &axes)
Permutes the axes of a tensor in-place.

◆ power_elem()

template<typename T >
static txeo::Tensor< T > txeo::TensorFunc< T >::power_elem ( const txeo::Tensor< T > &  tensor,
const T exponent 
)
static

Returns the element-wise potentiation of a tensor.

Parameters
tensorTensor to be powered
exponentExponent of the potentiation
Returns
txeo::Tensor<T> Result

Example Usage:

txeo::Tensor<float> a({3}, {2.0f, 3.0f, 4.0f});
auto b = TensorOp<float>::power_elem(a, 2.0f); // Result: [4.0f, 9.0f, 16.0f]

◆ power_elem_by()

template<typename T >
static txeo::Tensor< T > & txeo::TensorFunc< T >::power_elem_by ( txeo::Tensor< T > &  tensor,
const T exponent 
)
static

Performs element-wise potentiation of the tensor (in-place)

Parameters
tensorTensor to be modified
exponentExponent of the potentiation

Example Usage:

txeo::Tensor<double> a({2}, {3.0, 4.0});
TensorOp<double>::power_elem_by(a, 3.0); // a becomes [27.0, 64.0]

◆ sqrt()

template<typename T >
static txeo::Tensor< T > txeo::TensorFunc< T >::sqrt ( const txeo::Tensor< T > &  tensor)
static

Computes the element-wise square root of a tensor.

Parameters
tensorThe input tensor.
Returns
A new tensor containing the square root values.

Example Usage:

txeo::Tensor<double> tensor({3}, {1.0, 4.0, 9.0});
auto result = TensorOp<double>::sqrt(tensor);
// result = [1.0, 2.0, 3.0]

◆ sqrt_by()

template<typename T >
static txeo::Tensor< T > & txeo::TensorFunc< T >::sqrt_by ( txeo::Tensor< T > &  tensor)
static

Computes the element-wise square root of a tensor in-place.

Parameters
tensorThe input tensor to be modified.

Example Usage:

txeo::Tensor<double> tensor({3}, {1.0, 4.0, 9.0});
TensorOp<double>::sqrt_by(tensor);
// tensor = [1.0, 2.0, 3.0]

◆ square()

template<typename T >
static txeo::Tensor< T > txeo::TensorFunc< T >::square ( const txeo::Tensor< T > &  tensor)
static

Computes the element-wise square of a tensor.

Parameters
tensorThe input tensor.
Returns
A new tensor containing the squared values.

Example Usage:

txeo::Tensor<int> tensor({3}, {1, 2, 3});
auto result = TensorOp<int>::square(tensor);
// result = [1, 4, 9]

◆ square_by()

template<typename T >
static txeo::Tensor< T > & txeo::TensorFunc< T >::square_by ( txeo::Tensor< T > &  tensor)
static

Computes the element-wise square of a tensor in-place.

Parameters
tensorThe input tensor to be modified.

Example Usage:

txeo::Tensor<int> tensor({3}, {1, 2, 3});
TensorOp<int>::square_by(tensor);
// tensor = [1, 4, 9]

◆ transpose()

template<typename T >
static txeo::Matrix< T > txeo::TensorFunc< T >::transpose ( const txeo::Matrix< T > &  matrix)
static

Transposes a matrix.

Parameters
matrixThe input matrix.
Returns
A new matrix that is the transpose of the input matrix.

Example Usage:

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6});
// result shape: (3, 2)
static txeo::Matrix< T > transpose(const txeo::Matrix< T > &matrix)
Transposes a matrix.

◆ transpose_by()

template<typename T >
static txeo::Matrix< T > & txeo::TensorFunc< T >::transpose_by ( txeo::Matrix< T > &  matrix)
static

Transposes a matrix in-place.

Parameters
matrixThe input matrix to be modified.
Returns
A reference to the modified matrix.

Example Usage:

txeo::Matrix<int> matrix(2, 3, {1, 2, 3, 4, 5, 6});
// matrix shape after transpose: (3, 2)
static txeo::Matrix< T > & transpose_by(txeo::Matrix< T > &matrix)
Transposes a matrix in-place.

The documentation for this class was generated from the following files: