-
Notifications
You must be signed in to change notification settings - Fork 3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
#1. Generates a random matrix A of size matrixSize x matrixSize. | ||
A = np.random.rand(matrixSize, matrixSize) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
print(A) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print(A) for large |
||
|
||
#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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment for large |
||
|
||
#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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.") | ||
|
There was a problem hiding this comment.
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.