secure_learning.utils.util_matrix_vec module
Contains utils for matrices and vectors such as transposing and secure signing
- secure_learning.utils.util_matrix_vec.mat_to_vec(matrix, transpose=False)[source]
Transforms a vector in matrix format to vector format.
A = [[1], [2], [3]] mat_to_vec(A) == [1, 2, 3]
- Parameters:
matrix (
List
[List
[TypeVar
(AnyTV
)]]) – Vector in matrix formattranspose (
bool
) – Interpret vector as column vector
- Return type:
List
[TypeVar
(AnyTV
)]- Returns:
Vector
- secure_learning.utils.util_matrix_vec.mat_vec_mult(matrix, vector, transpose=False)[source]
Compute matrix-vector multiplication.
- Parameters:
matrix (
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]) – Matrix input with dimensions \(m * r\). Dimensions may be \(r * m\) when combined with tr=Truevector (
Union
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)],List
[float
]]) – Vector input of length \(r\), treated as a column vectortranspose (
bool
) – If True, first transpose mat
- Return type:
List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]- Returns:
Row vector with matrix-vector products
- secure_learning.utils.util_matrix_vec.matrix_sum(matrix, cols=False)[source]
Securely add all rows in X.
- Parameters:
matrix (
List
[List
[SecureFixedPoint
]]) – Matrix to be summedcols (
bool
) – If True, sum the columns of X instead.
- Return type:
List
[SecureFixedPoint
]- Returns:
Vector of sums
- secure_learning.utils.util_matrix_vec.matrix_transpose(matrix)[source]
Transpose a list of lists.
A = [[31, 64], [32, 68], [33, 72], [34, 76]] matrix_transpose(A) == [[31, 32, 33, 34], [64, 68, 72, 76]]
- Parameters:
matrix (
List
[List
[TypeVar
(AnyTV
)]]) – Matrix stored as list of lists- Return type:
List
[List
[TypeVar
(AnyTV
)]]- Returns:
Transpose of \(A\)
- secure_learning.utils.util_matrix_vec.mult_scalar_mul(scalars, matrix, transpose=False)[source]
Vectorized version of mpc.scalar_mul.
scalars = [2, -1] mat = [[1, 2], [3, 4], [5, 6]] mult_scalar_mul(scalars, mat) == [[2, -2], [6, -4], [10, -6]]
- Parameters:
scalars (
Union
[float
,List
[float
],SecureFixedPoint
,List
[SecureFixedPoint
]]) – Vector of scalarsmatrix (
List
[List
[SecureFixedPoint
]]) – Matrix of which the columns need to be scaled.transpose (
bool
) – If True, scale the rows of matrix instead.
- Return type:
List
[List
[SecureFixedPoint
]]- Returns:
Matrix with scaled columns
- secure_learning.utils.util_matrix_vec.permute_matrix(matrix)[source]
Permute matrix randomly.
- Parameters:
matrix (
Sequence
[Sequence
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]) – Matrix to be permuted- Raises:
TypeError – Input is not a matrix
- Return type:
List
[List
[TypeVar
(SecNumTypesTV
,SecureFiniteField
,SecureFixedPoint
,SecureInteger
)]]- Returns:
Permuted matrix
- secure_learning.utils.util_matrix_vec.scale_vector_or_matrix(factor, x)[source]
Corrects a vector or matrix by a given factor.
- Parameters:
factor (
float
) – Factor to scale matrix or vectorx (
List
[TypeVar
(AnyTV
)]) – Vector or matrix to be scaled
- Return type:
List
[TypeVar
(AnyTV
)]- Returns:
Scaled vector or matrix
- secure_learning.utils.util_matrix_vec.vector_to_matrix(vector, transpose=False)[source]
Convert vector to matrix.
vec_to_mat([1, 2, 3]) == [[1, 2, 3]] vec_to_mat([1, 2, 3], tr=True) == [[1], [2], [3]]
- Parameters:
vector (
List
[TypeVar
(AnyTV
)]) – Row vector to be convertedtranspose (
bool
) – Interpret vector as column vector
- Return type:
List
[List
[TypeVar
(AnyTV
)]]- Returns:
Matrix that encapsulates vector