powerspectrum.py 22.6 KB
Newer Older
Marco Selig's avatar
Marco Selig committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
## NIFTY (Numerical Information Field Theory) has been developed at the
## Max-Planck-Institute for Astrophysics.
##
## Copyright (C) 2013 Max-Planck-Society
##
## Author: Marco Selig
## Project homepage: <http://www.mpa-garching.mpg.de/ift/nifty/>
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
## See the GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.

## TODO: cythonize

from __future__ import division
import numpy as np


28
def draw_vector_nd(axes,dgrid,ps,symtype=0,fourier=False,zerocentered=False,kpack=None):
Marco Selig's avatar
Marco Selig committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    """
        Draws a n-dimensional field on a regular grid from a given power
        spectrum. The grid parameters need to be specified, together with a
        couple of global options explained below. The dimensionality of the
        field is determined automatically.

        Parameters
        ----------
        axes : ndarray
            An array with the length of each axis.

        dgrid : ndarray
            An array with the pixel length of each axis.

        ps : ndarray
            The power spectrum as a function of Fourier modes.

        symtype : int {0,1,2} : *optional*
            Whether the output should be real valued (0), complex-hermitian (1)
            or complex without symmetry (2). (default=0)

        fourier : bool : *optional*
            Whether the output should be in Fourier space or not
            (default=False).

        zerocentered : bool : *optional*
            Whether the output array should be zerocentered, i.e. starting with
            negative Fourier modes going over the zero mode to positive modes,
            or not zerocentered, where zero, positive and negative modes are
            simpy ordered consecutively.

        Returns
        -------
        field : ndarray
            The drawn random field.

    """
67 68 69 70
    if(kpack is None):
        kdict = np.fft.fftshift(nkdict(axes,dgrid,fourier))
        klength = nklength(kdict)
    else:
71
        kdict = kpack[1][np.fft.ifftshift(kpack[0],axes=shiftaxes(zerocentered,st_to_zero_mode=False))]
72
        klength = kpack[1]
Marco Selig's avatar
Marco Selig committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

    #output is in position space
    if(not fourier):

        #output is real-valued
        if(symtype==0):
            vector = drawherm(klength,kdict,ps)
            if(np.any(zerocentered==True)):
                return np.real(np.fft.fftshift(np.fft.ifftn(vector),axes=shiftaxes(zerocentered)))
            else:
                return np.real(np.fft.ifftn(vector))

        #output is complex with hermitian symmetry
        elif(symtype==1):
            vector = drawwild(klength,kdict,ps,real_corr=2)
            if(np.any(zerocentered==True)):
                return np.fft.fftshift(np.fft.ifftn(np.real(vector)),axes=shiftaxes(zerocentered))
            else:
                return np.fft.ifftn(np.real(vector))

        #output is complex without symmetry
        else:
            vector = drawwild(klength,kdict,ps)
            if(np.any(zerocentered==True)):
                return np.fft.fftshift(np.fft.ifftn(vector),axes=shiftaxes(zerocentered))
            else:
                return np.fft.ifftn(vector)

    #output is in fourier space
    else:

        #output is real-valued
        if(symtype==0):
            vector = drawwild(klength,kdict,ps,real_corr=2)
            if np.any(zerocentered == True):
                return np.real(np.fft.fftshift(vector,axes=shiftaxes(zerocentered)))
            else:
                return np.real(vector)

        #output is complex with hermitian symmetry
        elif(symtype==1):
            vector = drawherm(klength,kdict,ps)
            if(np.any(zerocentered==True)):
                return np.fft.fftshift(vector,axes=shiftaxes(zerocentered))
            else:
                return vector

        #output is complex without symmetry
        else:
            vector = drawwild(klength,kdict,ps)
            if(np.any(zerocentered==True)):
                return np.fft.fftshift(vector,axes=shiftaxes(zerocentered))
            else:
                return vector


129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
#def calc_ps(field,axes,dgrid,zerocentered=False,fourier=False):
#
#    """
#        Calculates the power spectrum of a given field assuming that the field
#        is statistically homogenous and isotropic.
#
#        Parameters
#        ----------
#        field : ndarray
#            The input field from which the power spectrum should be determined.
#
#        axes : ndarray
#            An array with the length of each axis.
#
#        dgrid : ndarray
#            An array with the pixel length of each axis.
#
#        zerocentered : bool : *optional*
#            Whether the output array should be zerocentered, i.e. starting with
#            negative Fourier modes going over the zero mode to positive modes,
#            or not zerocentered, where zero, positive and negative modes are
#            simpy ordered consecutively.
#
#        fourier : bool : *optional*
#            Whether the output should be in Fourier space or not
#            (default=False).
#
#    """
#
#    ## field absolutes
#    if(not fourier):
#        foufield = np.fft.fftshift(np.fft.fftn(field))
#    elif(np.any(zerocentered==False)):
#        foufield = np.fft.fftshift(field, axes=shiftaxes(zerocentered,st_to_zero_mode=True))
#    else:
#        foufield = field
#    fieldabs = np.abs(foufield)**2
#
#    kdict = nkdict(axes,dgrid,fourier)
#    klength = nklength(kdict)
#
#    ## power spectrum
#    ps = np.zeros(klength.size)
#    rho = np.zeros(klength.size)
#    for ii in np.ndindex(kdict.shape):
#        position = np.searchsorted(klength,kdict[ii])
#        rho[position] += 1
#        ps[position] += fieldabs[ii]
#    ps = np.divide(ps,rho)
#    return ps

def calc_ps_fast(field,axes,dgrid,zerocentered=False,fourier=False,pindex=None,kindex=None,rho=None):
Marco Selig's avatar
Marco Selig committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

    """
        Calculates the power spectrum of a given field faster assuming that the
        field is statistically homogenous and isotropic.

        Parameters
        ----------
        field : ndarray
            The input field from which the power spectrum should be determined.

        axes : ndarray
            An array with the length of each axis.

        dgrid : ndarray
            An array with the pixel length of each axis.

        zerocentered : bool : *optional*
            Whether the output array should be zerocentered, i.e. starting with
            negative Fourier modes going over the zero mode to positive modes,
            or not zerocentered, where zero, positive and negative modes are
            simpy ordered consecutively.

        fourier : bool : *optional*
            Whether the output should be in Fourier space or not
            (default=False).

207 208 209 210 211 212 213 214 215 216
        pindex : ndarray
            Index of the Fourier grid points in a numpy.ndarray ordered
            following the zerocentered flag (default=None).

        kindex : ndarray
            Array of all k-vector lengths (default=None).

        rho : ndarray
            Degeneracy of the Fourier grid, indicating how many k-vectors in
            Fourier space have the same length (default=None).
Marco Selig's avatar
Marco Selig committed
217 218 219 220 221 222 223 224 225 226 227

    """

    ## field absolutes
    if(not fourier):
        foufield = np.fft.fftshift(np.fft.fftn(field))
    elif(np.any(zerocentered==False)):
        foufield = np.fft.fftshift(field, axes=shiftaxes(zerocentered,st_to_zero_mode=True))
    else:
        foufield = field
    fieldabs = np.abs(foufield)**2
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271

    if(rho is None):
        if(pindex is None):
            ## kdict
            kdict = nkdict(axes,dgrid,fourier)
            ## klength
            if(kindex is None):
                klength = nklength(kdict)
            else:
                klength = kindex
            ## power spectrum
            ps = np.zeros(klength.size)
            rho = np.zeros(klength.size)
            for ii in np.ndindex(kdict.shape):
                position = np.searchsorted(klength,kdict[ii])
                ps[position] += fieldabs[ii]
                rho[position] += 1
        else:
            ## power spectrum
            ps = np.zeros(len(set(pindex.flatten())))
            rho = np.zeros(ps.size)
            for ii in np.ndindex(pindex.shape):
                ps[pindex[ii]] += fieldabs[ii]
                rho[pindex[ii]] += 1
    elif(pindex is None):
        ## kdict
        kdict = nkdict(axes,dgrid,fourier)
        ## klength
        if(kindex is None):
            klength = nklength(kdict)
        else:
            klength = kindex
        ## power spectrum
        ps = np.zeros(klength.size)
        for ii in np.ndindex(kdict.shape):
            position = np.searchsorted(klength,kdict[ii])
            ps[position] += fieldabs[ii]
    else:
        ## power spectrum
        ps = np.zeros(rho.size)
        for ii in np.ndindex(pindex.shape):
            ps[pindex[ii]] += fieldabs[ii]

    ps = np.divide(ps,rho)
Marco Selig's avatar
Marco Selig committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    return ps


def get_power_index(axes,dgrid,zerocentered,irred=False,fourier=True):

    """
        Returns the index of the Fourier grid points in a numpy
        array, ordered following the zerocentered flag.

        Parameters
        ----------
        axes : ndarray
            An array with the length of each axis.

        dgrid : ndarray
            An array with the pixel length of each axis.

        zerocentered : bool
            Whether the output array should be zerocentered, i.e. starting with
            negative Fourier modes going over the zero mode to positive modes,
            or not zerocentered, where zero, positive and negative modes are
            simpy ordered consecutively.

        irred : bool : *optional*
            If True, the function returns an array of all k-vector lengths and
            their degeneracy factors. If False, just the power index array is
            returned.

        fourier : bool : *optional*
            Whether the output should be in Fourier space or not
            (default=False).

        Returns
        -------
            index or {klength, rho} : scalar or list
                Returns either an array of all k-vector lengths and
                their degeneracy factors or just the power index array
                depending on the flag irred.

    """

    ## kdict, klength
    if(np.any(zerocentered==False)):
        kdict = np.fft.fftshift(nkdict(axes,dgrid,fourier),axes=shiftaxes(zerocentered,st_to_zero_mode=True))
    else:
        kdict = nkdict(axes,dgrid,fourier)
    klength = nklength(kdict)
    ## output
    if(irred):
        rho = np.zeros(klength.shape,dtype=np.int)
        for ii in np.ndindex(kdict.shape):
            rho[np.searchsorted(klength,kdict[ii])] += 1
        return klength,rho
    else:
        ind = np.empty(axes,dtype=np.int)
        for ii in np.ndindex(kdict.shape):
            ind[ii] = np.searchsorted(klength,kdict[ii])
        return ind


332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
def get_power_indices(axes,dgrid,zerocentered,fourier=True):
    """
        Returns the index of the Fourier grid points in a numpy
        array, ordered following the zerocentered flag.

        Parameters
        ----------
        axes : ndarray
            An array with the length of each axis.

        dgrid : ndarray
            An array with the pixel length of each axis.

        zerocentered : bool
            Whether the output array should be zerocentered, i.e. starting with
            negative Fourier modes going over the zero mode to positive modes,
            or not zerocentered, where zero, positive and negative modes are
            simpy ordered consecutively.

        irred : bool : *optional*
            If True, the function returns an array of all k-vector lengths and
            their degeneracy factors. If False, just the power index array is
            returned.

        fourier : bool : *optional*
            Whether the output should be in Fourier space or not
            (default=False).

        Returns
        -------
        index, klength, rho : ndarrays
            Returns the power index array, an array of all k-vector lengths and
            their degeneracy factors.

    """

    ## kdict, klength
    if(np.any(zerocentered==False)):
        kdict = np.fft.fftshift(nkdict(axes,dgrid,fourier),axes=shiftaxes(zerocentered,st_to_zero_mode=True))
    else:
        kdict = nkdict(axes,dgrid,fourier)
    klength = nklength(kdict)
    ## output
    ind = np.empty(axes,dtype=np.int)
    rho = np.zeros(klength.shape,dtype=np.int)
    for ii in np.ndindex(kdict.shape):
        ind[ii] = np.searchsorted(klength,kdict[ii])
        rho[ind[ii]] += 1
    return ind,klength,rho


383
def bin_power_indices(pindex,kindex,rho,log=False,nbin=None,binbounds=None):
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    """
        Returns the (re)binned power indices associated with the Fourier grid.

        Parameters
        ----------
        pindex : ndarray
            Index of the Fourier grid points in a numpy.ndarray ordered
            following the zerocentered flag (default=None).
        kindex : ndarray
            Array of all k-vector lengths (default=None).
        rho : ndarray
            Degeneracy of the Fourier grid, indicating how many k-vectors in
            Fourier space have the same length (default=None).
        log : bool
            Flag specifying if the binning is performed on logarithmic scale
399
            (default: False).
400 401 402 403 404 405 406 407 408 409 410 411 412 413
        nbin : integer
            Number of used bins (default: None).
        binbounds : {list, array}
            Array-like inner boundaries of the used bins (default: None).

        Returns
        -------
        pindex, kindex, rho : ndarrays
            The (re)binned power indices.

    """
    ## boundaries
    if(binbounds is not None):
        binbounds = np.sort(binbounds)
414
    ## equal binning
415
    else:
416 417
        if(log is None):
            log = False
418 419 420 421
        if(log):
            k = np.r_[0,np.log(kindex[1:])]
        else:
            k = kindex
422 423 424 425 426 427
        dk = np.max(k[2:]-k[1:-1]) ## minimal dk
        if(nbin is None):
            nbin = int((k[-1]-0.5*(k[2]+k[1]))/dk-0.5) ## maximal nbin
        else:
            nbin = min(int(nbin),int((k[-1]-0.5*(k[2]+k[1]))/dk+2.5))
            dk = (k[-1]-0.5*(k[2]+k[1]))/(nbin-2.5)
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        binbounds = np.r_[0.5*(3*k[1]-k[2]),0.5*(k[1]+k[2])+dk*np.arange(nbin-2)]
        if(log):
            binbounds = np.exp(binbounds)
    ## reordering
    reorder = np.searchsorted(binbounds,kindex)
    rho_ = np.zeros(len(binbounds)+1,dtype=rho.dtype)
    kindex_ = np.empty(len(binbounds)+1,dtype=kindex.dtype)
    for ii in range(len(reorder)):
        if(rho_[reorder[ii]]==0):
            kindex_[reorder[ii]] = kindex[ii]
            rho_[reorder[ii]] += rho[ii]
        else:
            kindex_[reorder[ii]] = (kindex_[reorder[ii]]*rho_[reorder[ii]]+kindex[ii]*rho[ii])/(rho_[reorder[ii]]+rho[ii])
            rho_[reorder[ii]] += rho[ii]

    return reorder[pindex],kindex_,rho_


Marco Selig's avatar
Marco Selig committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
def nhermitianize(field,zerocentered):

    """
        Hermitianizes an arbitrary n-dimensional field. Becomes relatively slow
        for large n.

        Parameters
        ----------
        field : ndarray
            The input field that should be hermitianized.

        zerocentered : bool
            Whether the output array should be zerocentered, i.e. starting with
            negative Fourier modes going over the zero mode to positive modes,
            or not zerocentered, where zero, positive and negative modes are
            simpy ordered consecutively.

        Returns
        -------
        hermfield : ndarray
            The hermitianized field.

    """
    ## shift zerocentered axes
    if(np.any(zerocentered==True)):
        field = np.fft.fftshift(field, axes=shiftaxes(zerocentered))
#    for index in np.ndenumerate(field):
#        negind = tuple(-np.array(index[0]))
#        field[negind] = np.conjugate(index[1])
#        if(field[negind]==field[index[0]]):
#            field[index[0]] = np.abs(index[1])*(np.sign(index[1].real)+(np.sign(index[1].real)==0)*np.sign(index[1].imag)).astype(np.int)
    subshape = np.array(field.shape,dtype=np.int) ## == axes
    maxindex = subshape//2
    subshape[np.argmax(subshape)] = subshape[np.argmax(subshape)]//2+1 ## ~half larges axis
    for ii in np.ndindex(tuple(subshape)):
        negii = tuple(-np.array(ii))
        field[negii] = np.conjugate(field[ii])
    for ii in np.ndindex((2,)*maxindex.size):
        index = tuple(ii*maxindex)
        field[index] = np.abs(field[index])*(np.sign(field[index].real)+(np.sign(field[index].real)==0)*-np.sign(field[index].imag)).astype(np.int) ## minus since overwritten before
    ## reshift zerocentered axes
    if(np.any(zerocentered==True)):
        field = np.fft.fftshift(field,axes=shiftaxes(zerocentered))
    return field

def nhermitianize_fast(field,zerocentered,special=False):

    """
        Hermitianizes an arbitrary n-dimensional field faster.
        Still becomes comparably slow for large n.

        Parameters
        ----------
        field : ndarray
            The input field that should be hermitianized.

        zerocentered : bool
            Whether the output array should be zerocentered, i.e. starting with
            negative Fourier modes going over the zero mode to positive modes,
            or not zerocentered, where zero, positive and negative modes are
            simpy ordered consecutively.

        special : bool, *optional*
            Must be True for random fields drawn from Gaussian or pm1
            distributions.

        Returns
        -------
        hermfield : ndarray
            The hermitianized field.

    """
    ## shift zerocentered axes
    if(np.any(zerocentered==True)):
        field = np.fft.fftshift(field, axes=shiftaxes(zerocentered))
    dummy = np.conjugate(field)
    ## mirror conjugate field
    for ii in range(field.ndim):
        dummy = np.swapaxes(dummy,0,ii)
        dummy = np.flipud(dummy)
        dummy = np.roll(dummy,1,axis=0)
        dummy = np.swapaxes(dummy,0,ii)
    if(special): ## special normalisation for certain random fields
        field = np.sqrt(0.5)*(field+dummy)
        maxindex = np.array(field.shape,dtype=np.int)//2
        for ii in np.ndindex((2,)*maxindex.size):
            index = tuple(ii*maxindex)
            field[index] *= np.sqrt(0.5)
    else: ## regular case
        field = 0.5*(field+dummy)
    ## reshift zerocentered axes
    if(np.any(zerocentered==True)):
        field = np.fft.fftshift(field,axes=shiftaxes(zerocentered))
    return field


def random_hermitian_pm1(datatype,zerocentered,shape):

    """
        Draws a set of hermitianized random, complex pm1 numbers.

    """

    field = np.random.randint(4,high=None,size=np.prod(shape,axis=0,dtype=np.int,out=None)).reshape(shape,order='C')
    dummy = np.copy(field)
    ## mirror field
    for ii in range(field.ndim):
        dummy = np.swapaxes(dummy,0,ii)
        dummy = np.flipud(dummy)
        dummy = np.roll(dummy,1,axis=0)
        dummy = np.swapaxes(dummy,0,ii)
    field = (field+dummy+2*(field>dummy)*((field+dummy)%2))%4 ## wicked magic
    x = np.array([1+0j,0+1j,-1+0j,0-1j],dtype=datatype)[field]
    ## (re)shift zerocentered axes
    if(np.any(zerocentered==True)):
        field = np.fft.fftshift(field,axes=shiftaxes(zerocentered))
    return x


#-----------------------------------------------------------------------------
# Auxiliary functions
#-----------------------------------------------------------------------------

def shiftaxes(zerocentered,st_to_zero_mode=False):

    """
        Shifts the axes in a special way needed for some functions
    """

    axes = []
    for ii in range(len(zerocentered)):
        if(st_to_zero_mode==False)and(zerocentered[ii]):
            axes += [ii]
        if(st_to_zero_mode==True)and(not zerocentered[ii]):
            axes += [ii]
    return axes


def nkdict(axes,dgrid,fourier=True):

    """
        Calculates an n-dimensional array with its entries being the lengths of
        the k-vectors from the zero point of the Fourier grid.

    """

    if(fourier):
        dk = dgrid
    else:
        dk = np.array([1/axes[i]/dgrid[i] for i in range(len(axes))])

    kdict = np.empty(axes)
    for ii in np.ndindex(kdict.shape):
        kdict[ii] = np.sqrt(np.sum(((ii-axes//2)*dk)**2))
    return kdict


def nklength(kdict):
    return np.sort(list(set(kdict.flatten())))


#def drawherm(vector,klength,kdict,ps): ## vector = np.zeros(kdict.shape,dtype=np.complex)
#    for ii in np.ndindex(vector.shape):
#        if(vector[ii]==np.complex(0.,0.)):
#            vector[ii] = np.sqrt(0.5*ps[np.searchsorted(klength,kdict[ii])])*np.complex(np.random.normal(0.,1.),np.random.normal(0.,1.))
#            negii = tuple(-np.array(ii))
#            vector[negii] = np.conjugate(vector[ii])
#            if(vector[negii]==vector[ii]):
#                vector[ii] = np.float(np.sqrt(ps[klength==kdict[ii]]))*np.random.normal(0.,1.)
#    return vector

def drawherm(klength,kdict,ps):

    """
        Draws a hermitian random field from a Gaussian distribution.

    """

#    vector = np.zeros(kdict.shape,dtype='complex')
#    for ii in np.ndindex(vector.shape):
#        if(vector[ii]==np.complex(0.,0.)):
#            vector[ii] = np.sqrt(0.5*ps[np.searchsorted(klength,kdict[ii])])*np.complex(np.random.normal(0.,1.),np.random.normal(0.,1.))
#            negii = tuple(-np.array(ii))
#            vector[negii] = np.conjugate(vector[ii])
#            if(vector[negii]==vector[ii]):
#                vector[ii] = np.float(np.sqrt(ps[np.searchsorted(klength,kdict[ii])]))*np.random.normal(0.,1.)
#    return vector
    vec = np.random.normal(loc=0,scale=1,size=kdict.size).reshape(kdict.shape)
    vec = np.fft.fftn(vec)/np.sqrt(np.prod(kdict.shape))
    for ii in np.ndindex(kdict.shape):
        vec[ii] *= np.sqrt(ps[np.searchsorted(klength,kdict[ii])])
    return vec


#def drawwild(vector,klength,kdict,ps,real_corr=1): ## vector = np.zeros(kdict.shape,dtype=np.complex)
#    for ii in np.ndindex(vector.shape):
#        vector[ii] = np.sqrt(real_corr*0.5*ps[klength==kdict[ii]])*np.complex(np.random.normal(0.,1.),np.random.normal(0.,1.))
#    return vector

def drawwild(klength,kdict,ps,real_corr=1):

    """
        Draws a field of arbitrary symmetry from a Gaussian distribution.

    """

    vec = np.empty(kdict.size,dtype=np.complex)
    vec.real = np.random.normal(loc=0,scale=np.sqrt(real_corr*0.5),size=kdict.size)
    vec.imag = np.random.normal(loc=0,scale=np.sqrt(real_corr*0.5),size=kdict.size)
    vec = vec.reshape(kdict.shape)
    for ii in np.ndindex(kdict.shape):
        vec[ii] *= np.sqrt(ps[np.searchsorted(klength,kdict[ii])])
    return vec