Skip to content
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

Open
IanQS opened this issue Feb 1, 2021 · 2 comments
Open

Diag #6

IanQS opened this issue Feb 1, 2021 · 2 comments
Labels
feature Feature to implement

Comments

@IanQS
Copy link
Owner

IanQS commented Feb 1, 2021

Emulate Numpy diag

Extract a diagonal or construct a diagonal array.

Include testing. Progress so far listed below

@IanQS IanQS added the feature Feature to implement label Feb 1, 2021
@IanQS
Copy link
Owner Author

IanQS commented Feb 1, 2021

pTensor.h

  /**
   * 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);

@IanQS
Copy link
Owner Author

IanQS commented Feb 1, 2021

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
Labels
feature Feature to implement
Projects
None yet
Development

No branches or pull requests

1 participant