Created
October 21, 2016 03:28
-
-
Save amir734jj/33cca81545547f240dd83a0fa0c6c1d6 to your computer and use it in GitHub Desktop.
flatten and unflatten matrix in Sage via numpy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from sage.stats.distributions.discrete_gaussian_lattice import DiscreteGaussianDistributionLatticeSampler | |
n = 256 | |
D = DiscreteGaussianDistributionLatticeSampler(ZZ^n, 3.0) | |
arr = [list(D()) for i in range(n)] | |
flat = flatten(list(arr)) | |
print arr | |
print flat | |
def unflatten(flat): | |
return [flat[i:i+sqrt(len(flat))] for i in np.arange(0, len(flat), sqrt(len(flat)))] | |
unflat = unflatten(flat) | |
print np.array_equal(arr, np.asarray(unflat)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment