Generating a random sparse hermitian matrix in Python
up vote
1
down vote
favorite
I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?
Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.
Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
$$ d = frac{mathrm{nnz}}{n^2} $$
where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).
EDIT
Calculating the density should be easy:
density = np.count_nonzero(A)/n**2
should do the trick.
matrices numerical-linear-algebra random python sparse-matrices
add a comment |
up vote
1
down vote
favorite
I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?
Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.
Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
$$ d = frac{mathrm{nnz}}{n^2} $$
where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).
EDIT
Calculating the density should be easy:
density = np.count_nonzero(A)/n**2
should do the trick.
matrices numerical-linear-algebra random python sparse-matrices
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?
Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.
Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
$$ d = frac{mathrm{nnz}}{n^2} $$
where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).
EDIT
Calculating the density should be easy:
density = np.count_nonzero(A)/n**2
should do the trick.
matrices numerical-linear-algebra random python sparse-matrices
I'd like to find a way to generate random sparse hermitian matrices in Python, but don't really know how to do so efficiently. How would I go about doing this?
Obviously, there are slow, ugly ways to do this, but since I'm going to be doing this a lot, I'd like if there was a faster way to do it.
Is there an easy way to calculate the density of the matrix? It's the parameter that I'd be using to compare two factorisation algorithms. It's defined as
$$ d = frac{mathrm{nnz}}{n^2} $$
where $mathrm{nnz}$ is defined as the number of nonzero entries in the matrix and $n$ is its number of rows or columns (hence $n^2$ is the number of elements in the matrix).
EDIT
Calculating the density should be easy:
density = np.count_nonzero(A)/n**2
should do the trick.
matrices numerical-linear-algebra random python sparse-matrices
matrices numerical-linear-algebra random python sparse-matrices
edited 2 hours ago
asked 2 hours ago
Peiffap
326
326
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
This generates a random mask with of a given size n
and density dens
, you just need to apply it to a dense matrix of your choice
import numpy as np
np.random.seed(0)
n = 300
dens = 0.2
mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])
print(np.count_nonzero(mask) / n**2)
The result of this is
>>> 0.20224444444444445
To force the result to be Hermitian just use
$$
H = frac{1}{2}(A + A^*)
$$
Alright, I see how this works but how do I apply the mask to a matrix?
– Peiffap
40 mins ago
@Peiffap Define the matrixM
and then runA = M[mask]
– caverac
39 mins ago
M[mask], no? Thanks!
– Peiffap
38 mins ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
This generates a random mask with of a given size n
and density dens
, you just need to apply it to a dense matrix of your choice
import numpy as np
np.random.seed(0)
n = 300
dens = 0.2
mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])
print(np.count_nonzero(mask) / n**2)
The result of this is
>>> 0.20224444444444445
To force the result to be Hermitian just use
$$
H = frac{1}{2}(A + A^*)
$$
Alright, I see how this works but how do I apply the mask to a matrix?
– Peiffap
40 mins ago
@Peiffap Define the matrixM
and then runA = M[mask]
– caverac
39 mins ago
M[mask], no? Thanks!
– Peiffap
38 mins ago
add a comment |
up vote
1
down vote
This generates a random mask with of a given size n
and density dens
, you just need to apply it to a dense matrix of your choice
import numpy as np
np.random.seed(0)
n = 300
dens = 0.2
mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])
print(np.count_nonzero(mask) / n**2)
The result of this is
>>> 0.20224444444444445
To force the result to be Hermitian just use
$$
H = frac{1}{2}(A + A^*)
$$
Alright, I see how this works but how do I apply the mask to a matrix?
– Peiffap
40 mins ago
@Peiffap Define the matrixM
and then runA = M[mask]
– caverac
39 mins ago
M[mask], no? Thanks!
– Peiffap
38 mins ago
add a comment |
up vote
1
down vote
up vote
1
down vote
This generates a random mask with of a given size n
and density dens
, you just need to apply it to a dense matrix of your choice
import numpy as np
np.random.seed(0)
n = 300
dens = 0.2
mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])
print(np.count_nonzero(mask) / n**2)
The result of this is
>>> 0.20224444444444445
To force the result to be Hermitian just use
$$
H = frac{1}{2}(A + A^*)
$$
This generates a random mask with of a given size n
and density dens
, you just need to apply it to a dense matrix of your choice
import numpy as np
np.random.seed(0)
n = 300
dens = 0.2
mask = np.array([np.random.choice([True, False], n, p = [dens, 1 - dens]) for i in range(n)])
print(np.count_nonzero(mask) / n**2)
The result of this is
>>> 0.20224444444444445
To force the result to be Hermitian just use
$$
H = frac{1}{2}(A + A^*)
$$
answered 50 mins ago
caverac
11.7k21027
11.7k21027
Alright, I see how this works but how do I apply the mask to a matrix?
– Peiffap
40 mins ago
@Peiffap Define the matrixM
and then runA = M[mask]
– caverac
39 mins ago
M[mask], no? Thanks!
– Peiffap
38 mins ago
add a comment |
Alright, I see how this works but how do I apply the mask to a matrix?
– Peiffap
40 mins ago
@Peiffap Define the matrixM
and then runA = M[mask]
– caverac
39 mins ago
M[mask], no? Thanks!
– Peiffap
38 mins ago
Alright, I see how this works but how do I apply the mask to a matrix?
– Peiffap
40 mins ago
Alright, I see how this works but how do I apply the mask to a matrix?
– Peiffap
40 mins ago
@Peiffap Define the matrix
M
and then run A = M[mask]
– caverac
39 mins ago
@Peiffap Define the matrix
M
and then run A = M[mask]
– caverac
39 mins ago
M[mask], no? Thanks!
– Peiffap
38 mins ago
M[mask], no? Thanks!
– Peiffap
38 mins ago
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3019821%2fgenerating-a-random-sparse-hermitian-matrix-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown