Linear algebra

Matrix and vector operations

Interpolate

static Tensor *Tensor::interpolate(float factor1, Tensor *A, float factor2, Tensor *B)

Element-wise weighted sum (interpolation) operation of two tensors.

Parameters
  • factor1 – The weight for first member.

  • A – A tensor.

  • factor2 – The weight for second member.

  • B – Another tensor.

Returns

A new tensor C = factor1*A + factor2*B.

Example:

Tensor* t1 = Tensor::full({2, 3}, 1.0f);
// [
// [1.00 1.00 1.00]
// [1.00 1.00 1.00]
// ]

Tensor* t2 = Tensor::full({2, 3}, 10.0f);
// [
// [10.00 10.00 10.00]
// [10.00 10.00 10.00]
// ]

 Tensor* t3 = Tensor::interpolate(2.5f, t1, 0.5f, t2);  // a*t1 + b*t2
// [
// [7.50 7.50 7.50]
// [7.50 7.50 7.50]
// ]

Trace

float Tensor::trace(int k = 0)

Sum all the elements in a matrix diagonal.

Parameters

k – Offset. Used to select the diagonal to be summed.

Returns

The sum of all the elements in the selected diagonal.

Example:

Tensor* t1 = new Tensor({1, 3, 5, 4, 1, 3, 5, 4, 1}, {3, 3});
// [
// [1.00 3.00 5.00]
// [4.00 1.00 3.00]
// [5.00 4.00 1.00]
// ]

float tr0 = t1->trace(0);
// 3

float tr1 = t1->trace(1);
// 6