Matrix API

The matrix_api.py module provides the main NumPy-compatible interface. Key functions include:

  • add(a, b) - Element-wise addition

  • multiply(a, b) - Element-wise multiplication

  • matmul(a, b) - Matrix multiplication

  • sum(a, axis=None, keepdims=False) - Sum reduction

  • mean(a, axis=None, keepdims=False) - Mean calculation

  • cumulative_sum(a, axis=None) - Cumulative sum

  • transpose(a, axes=None) - Transpose operation

  • roll(a, shift, axis=None) - Roll/shift elements

Matrix operations API for OpenFHE-Numpy.

This module provides NumPy-compatible matrix operations that can be performed on encrypted data using homomorphic encryption. Functions follow NumPy naming conventions and similar signatures where possible.

All functions use the tensor_function_api decorator to handle different tensor types and dispatch to the appropriate backend implementation.

openfhe_numpy.operations.matrix_api.add(a, b)[source]

Add two tensors or a tensor and a scalar.

Parameters:
  • a (ArrayLike) – First operand.

  • b (ArrayLike) – Second operand.

Returns:

out – Element-wise sum of a and b.

Return type:

ArrayLike

See also

numpy.add

Corresponding NumPy function.

Examples

>>> add([1, 2], [3, 4])
array([4, 6])
openfhe_numpy.operations.matrix_api.subtract(a, b)[source]

Subtract two tensors or a tensor and a scalar.

Parameters:
  • a (ArrayLike) – First operand.

  • b (ArrayLike) – Second operand.

Returns:

out – Element-wise difference of a and b.

Return type:

ArrayLike

See also

numpy.subtract

Corresponding NumPy function.

Examples

>>> subtract([5, 7], [2, 4])
array([3, 3])
openfhe_numpy.operations.matrix_api.multiply(a, b)[source]

Element-wise multiply two tensors or a tensor and a scalar.

Parameters:
  • a (ArrayLike) – First operand.

  • b (ArrayLike or scalar) – Second operand.

Returns:

out – Element-wise product of a and b.

Return type:

ArrayLike

See also

numpy.multiply

Corresponding NumPy function.

Examples

>>> multiply([1, 2], [3, 4])
array([3, 8])
openfhe_numpy.operations.matrix_api.power(a, exponent)[source]

For each element of the tensor, it raises that element to the given power.

Note

This only supports integer exponents due to homomorphic-encryption constraints.

Parameters:
  • a (ArrayLike) – Base tensor.

  • exponent (int) – Power to raise each array element to.

Returns:

out – Element-wise a raised to exponent.

Return type:

ArrayLike

See also

numpy.power

Corresponding element-wise power function.

numpy.linalg.matrix_power

Repeated matrix multiplication for square matrices.

Examples

>>> power([1, 2, 3], 2)
array([1, 4, 9])
openfhe_numpy.operations.matrix_api.dot(a, b)[source]

Compute the dot (inner) product of two tensors. 1-D vectors: inner product 2-D matrices: matrix multiplication

Parameters:
  • a (ArrayLike) – First operand.

  • b (ArrayLike) – Second operand.

Returns:

out – Dot product of a and b.

Return type:

ArrayLike

See also

numpy.dot

Corresponding NumPy function.

Examples

# 1-D vectors: inner product >>> dot([1, 2], [3, 4]) 11

# 2-D matrices: matrix multiplication >>> import numpy as np >>> A = np.array([[1, 2], [3, 4]]) >>> B = np.array([[5, 6], [7, 8]]) >>> dot(A, B) array([[19, 22],

[43, 50]])

openfhe_numpy.operations.matrix_api.matmul(a, b)[source]

Matrix multiply two tensors.

Parameters:
  • a (ArrayLike) – First operand.

  • b (ArrayLike) – Second operand.

Returns:

out – Matrix product of a and b.

Return type:

ArrayLike

See also

numpy.matmul

Corresponding NumPy function.

Examples

>>> import numpy as np
>>> matmul(np.array([[1, 2], [3, 4]]), np.array([[5, 6], [7, 8]]))
array([[19, 22],
       [43, 50]])
openfhe_numpy.operations.matrix_api.transpose(a)[source]

Transpose a tensor.

Parameters:

a (ArrayLike) – Input tensor.

Returns:

out – Transposed tensor.

Return type:

ArrayLike

See also

numpy.transpose

Corresponding NumPy function.

Examples

>>> import numpy as np
>>> transpose(np.array([[1, 2], [3, 4]]))
array([[1, 3],
       [2, 4]])
openfhe_numpy.operations.matrix_api.cumsum(a, axis=0, keepdims=False)[source]

Compute the cumulative sum of tensor elements along an axis.

Parameters:
  • a (ArrayLike) – Input tensor.

  • axis (int, optional) – Axis along which to compute the sum. Default is 0.

  • keepdims (bool, optional) – If True, retains reduced dimensions. Default is False.

Returns:

out – Cumulative sum of a.

Return type:

ArrayLike

See also

numpy.cumsum

Corresponding NumPy function.

Examples

>>> import numpy as np
>>> cumsum(np.array([[1, 2], [3, 4]]), axis=1)
array([[1, 3],
       [3, 7]])
openfhe_numpy.operations.matrix_api.cumreduce(a, axis=0, keepdims=False)[source]

Compute the cumulative reduction (e.g., product) of tensor elements.

Parameters:
  • a (ArrayLike) – Input tensor.

  • axis (int, optional) – Axis along which to compute the reduction. Default is 0.

  • keepdims (bool, optional) – If True, retains reduced dimensions. Default is False.

Returns:

out – Cumulative reduction of a.

Return type:

ArrayLike

See also

numpy.cumprod

Similar operation for product.

Examples

>>> import numpy as np
>>> cumreduce(np.array([[1, 2, 3], [4, 5, 6]]), axis=0)
array([[1, 2, 3],
       [-3, -3, -3]])
openfhe_numpy.operations.matrix_api.sum(a, axis=0, keepdims=False)[source]

Sum of tensor elements over a given axis.

Parameters:
  • a (ArrayLike) – Input tensor.

  • axis (int, optional) – Axis along which to compute the sum. Default is 0. 0: sum over rows 1: sum over cols

  • keepdims (bool, optional) – If True, retains reduced dimensions. Default is False.

Returns:

out – Sum of a elements.

Return type:

ArrayLike

See also

numpy.sum

Corresponding NumPy function.

Examples

>>> import numpy as np
>>> a = np.array([[1, 2],
                  [3, 4]])
>>> sum(a)
10
>>> sum(a, axis=0)
array([4, 6])
>>> sum(a, axis=1)
array([3, 7])
openfhe_numpy.operations.matrix_api.mean(a, axis=0, keepdims=False)[source]

Compute the arithmetic mean along the specified axis.

Returns the average of the array elements. The average is taken over the flattened array by default, otherwise over the specified axis.

Parameters:
  • a (ArrayLike) – Input tensor.

  • axis (int, optional) – Axis along which to compute the mean. Default is 0.

  • keepdims (bool, optional) – If True, retains reduced dimensions. Default is False.

Returns:

out – Mean of a elements.

Return type:

ArrayLike

See also

numpy.mean

Corresponding NumPy function.

Examples

>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> mean(a)
2.5
>>> mean(a, axis=0)
array([2., 3.])
>>> mean(a, axis=1)
array([1.5, 3.5])