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

Scripts for generating positive definiton inputs #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions apps/choleskey/generateInputMatrix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/env python
# coding: utf-8

# This is an Jupyter Script for generating the Input Positive Definition(PD) Matrix for Cholesky decomposition.
# The generated .txt file stores the entire PD matrix with the matrix elems row by row.
# This input.txt file can be used directly In benchmark part in each cpp file.


import numpy as np

#enter matrix size
matrixSize = 4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding this is not a good idea. Consider using argparse module and use that to take input from the user on the matrix size to generate.


#1. Generates a random matrix A of size matrixSize x matrixSize.
A = np.random.rand(matrixSize, matrixSize)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend not generating matrices using rand here as the probability of generating a wrong matrix may get increasingly higher for larger matrices. Instead I would recommend looking into methods to generate positive definitive matrices with high probability such as translating this matlab code to python: https://math.stackexchange.com/questions/357980/how-to-generate-random-symmetric-positive-definite-matrices-using-matlab

Copy link
Contributor Author

@hcq9102 hcq9102 Dec 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree, I have another helper function in tiled_cholesky implementation which can generate the pd matrix using blas lib.
I suggest to leave this PR alone at this moment(I usually use this when i did performance test before).
And we can close this pr when get the better method.

print(A)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print(A) for large matrixSize would just flood the console


#2. Computes the product of matrix A and its transpose, i.e., A * A^T, storing the result in matrix B.
B = np.dot(A, A.transpose())
print("B")
print(B)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment for large matrixSize for B


#3. Adds the transpose of matrix B to itself, generating a symmetric matrix C.
C = B + B.T # Ensure it's symmetric

print("C")
print(C)

try:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the script will either generate or not generate a positive definitive matrix when run so that should be in a loop until we do get a correct solution.

Also using Cholesky transformation itself to check if the generated matrix is positive definitive doesn't seem to be the correct solution especially for larger matrices. I would recommend using a cheaper (eigen value based or Gershgorin circle theorem to verify this). You can refer to Gilbert Strang's book/videos or online resources for this.

D = np.linalg.cholesky(C)
print("Matrix C is positive definite.")

# Write the Cholesky decomposition result to a text file
with open(f"cholesky_result_{matrixSize}.txt", "w") as file:
for row in C:
for value in row:
file.write(f"{value}\n")
except np.linalg.LinAlgError:
print("Matrix C is not positive definite.")