-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Diag #6
Labels
feature
Feature to implement
Comments
/**
* Extract the diagonal from a matrix
* @param matrix
* Matrix of shape (m, n) (not necessarily a square matrix)
* @param asRowVector
* Whether to return as a rowVector or as a col Vector
* @return
* The diagonal elements as a vector
*/
static pTensor getDiagonal(const pTensor& matrix, bool asRowVector);
/**
* Make a matrix where the diagonals are elements of the vector
* @param vector
* Vector of (1, numElems)
* @return
* Return a matrix where the diagonals are populated by the vector's elements
*/
static pTensor makeDiagonal(pTensor vector); |
pTensor pTensor::getDiagonal(const pTensor &matrix, bool asRowVector) {
throw NotImplementedException();
assert (matrix.isMatrix());
if (asRowVector) {
// We mask out all but the row of interest then accumulate into a row vector
messageVector _container(matrix.m_cols, 0);
cipherVector vectorContainer = (*m_cc)->Encrypt(
m_public_key,
(*m_cc)->MakeCKKSPackedPlaintext(_container));
int diagIndex = 0;
for (auto &v: matrix.m_ciphertexts) {
messageVector mask(matrix.m_cols, 0);
mask[diagIndex] = 1;
auto ptMask = (*m_cc)->MakeCKKSPackedPlaintext(mask);
auto maskedVal = (*m_cc)->EvalMult(v, ptMask);
vectorContainer = (*m_cc)->EvalAdd(vectorContainer, maskedVal);
}
cipherTensor matrixContainer = {vectorContainer};
pTensor newTensor(1, matrix.m_cols, matrixContainer);
newTensor.m_isEncrypted = true;
return newTensor;
}
return pTensor();
}
pTensor pTensor::makeDiagonal(pTensor vector) {
throw NotImplementedException();
assert (vector.isVector());
pTensor identity = pTensor::identity(vector.m_rows);
auto eIdentity = identity.encrypt();
return eIdentity * vector; // Hadamard
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Emulate Numpy
diag
Include testing. Progress so far listed below
The text was updated successfully, but these errors were encountered: