Skip to content

Instantly share code, notes, and snippets.

@amir734jj
Created October 21, 2016 03:28
Show Gist options
  • Save amir734jj/33cca81545547f240dd83a0fa0c6c1d6 to your computer and use it in GitHub Desktop.
Save amir734jj/33cca81545547f240dd83a0fa0c6c1d6 to your computer and use it in GitHub Desktop.
flatten and unflatten matrix in Sage via numpy
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