def addAbsorber(self, qp, data=None, **kwargs): # todo? absorber type (mpArray) -> type(Stateoq) ? if data != None and qp == 'q': if self.__dummy[0]: self.absorber[0] = mpArray(self.dim) self.__dummy[0] = False self.absorber[0] += data if True in (numpy.abs(self.absorber[0]) > 1.0): self.absorber[0] = self.absorber[0]/self.absorber[0].real.max() x = State(self.scaleinfo, self.absorber[0]) x.savetxt("absorber_q.dat") elif qp =='p': if self.__dummy[1]: self.absorber[1] = mpArray(self.dim) self.__dummy[1] = False self.absorber[1] += data if True in (numpy.abs(self.absorber[1]) > 1.0): self.absorber[1] = self.absorber[1]/self.absorber[1].real.max() x = State(self.scaleinfo, self.absorber[1]) x.savetxt("absorber_p.dat",qp="p") elif qp == 'cs': index = len(self.absorber) self.absorber.append(mpArray(self.dim)) self.absorber[index] = data x = State(self.scaleinfo, mpArray.ones(self.dim) - self.absorber[index]) x.savetxt("absorber_cs_%d.dat" % (index - 2))
def op0(self, x, isShift=False): if isShift: sub_x = numpy.fft.fftshift(x) func = self.map.ifunc0(sub_x) self.operator[0] = mpArray([ mpmath.exp(mpmath.mpc("0", -twopi*func[i]*self.tau/self.h*mpmath.mpf("0.5"))) for i in range(self.dim) ]) else: func = self.map.ifunc0(x) self.operator[0] = mpArray([ mpmath.exp(mpmath.mpc("0", -twopi*func[i]*self.tau/self.h*mpmath.mpf("0.5"))) for i in range(self.dim) ])
def op1(self, x, isShift=True): if isShift: sub_x = numpy.fft.fftshift(x) func = self.map.ifunc1(sub_x) self.operator[1] = mpArray([ mpmath.exp(mpmath.mpc("0", -twopi*func[i]*self.tau/self.h)) for i in range(self.dim) ]) else: func = self.map.ifunc1(x) self.operator[1] = mpArray([ mpmath.exp(mpmath.mpc("0", -twopi*func[i]*self.tau/self.h)) for i in range(self.dim) ])
def hole_operate(self, invec=None, queue=None, verbose=False): self._operate(invec, None, False) # = self.operate sum1 = mpArray(self.dim) for i in range(2,len(self.absorber)): sum0 = mpmath.fsum(self.absorber[i].conj()*mpArray(self.stateOut)) sum1 += self.absorber[i]*sum0*mpmath.mpf("0.5")*self.gamma[i-2] self.stateOut = self.stateOut - State(self.scaleinfo, sum1) if queue !=None: queue.put(self.stateOut)
def _expotanh(self, x, x1, x2, **kwargs): if 'alpha' in kwargs: alpha=kwargs['alpha'] else: alpha=mpmath.mpf("1e10") z1 = x - x1 z2 = x - x2 y1 = mpArray([ mpmath.mpf("0.5")*(mpmath.mpf("1") - mpmath.tanh(x*alpha)) for x in z1 ]) y2 = mpArray([ mpmath.mpf("0.5")*(mpmath.mpf("1") + mpmath.tanh(x*alpha)) for x in z2 ]) return -(z1*z1*y1 + z2*z2*y2)
def operate(self, invec=None, queue=None, verbose=False): if invec != None: if isinstance(invec, mpArray): self.setIn(invec) elif isinstance(invec, list) and self.dim==len(invec): self.setIn(mpArray(invec)) qvec = self.operator[0]*self.stateIn pvec = mpArray(mpfft.fft(qvec))#, inverse=False, verbose=verbose)) pvec = self.operator[1]*pvec self.stateOut = mpArray(mpfft.ifft(pvec))#, inverse=True, verbose=verbose)) if queue !=None: queue.put(self.stateOut)
def operate(self, invec=None, queue=None, verbose=False): if invec != None: self.setIn(invec) ## tood: 一度だけしか呼びたくない if self._isShift(): self.absorber[1] = self.absorber[1].fftshift() qvec = self.absorber[0]*self.operator[0]*self.stateIn pvec = mpArray(mpfft.fft(qvec))#, inverse=False, verbose=verbose)) pvec = self.absorber[1]*self.operator[1]*pvec qvec = self.absorber[0]*mpArray(mpfft.ifft(pvec))#, inverse=True, verbose=verbose)) self.stateOut = State(self.scaleinfo, qvec) if queue !=None: queue.put(self.stateOut)
def setMatrix(self, fft=True, core=2, verbose=False,**kwargs): from utility import parallel self.setOperator(fft) matrix = mpMatrix(self.dim) orth_basis = [mpArray(self.dim) for i in range(self.dim)] for i in range(self.dim): orth_basis[i][i] = mpmath.mpc("1","0") start = time.time() if core > 1: #data = [orth_basis[i] for i in range(self.dim)] multi = parallel.MultiProcessing(self.operate, orth_basis, core=core) multi.run() res = multi.getResults() for i in range(self.dim): matrix[i,:] = res[i] elif core == 1: for i in range(self.dim): self.setIn(orth_basis[i]) self.operate() matrix[i,:] = self.stateOut[:] #mpmath.matrix(self.stateOut.data).T t = time.time() -start self.matrix = matrix.T if verbose : print("Making matrix of size" , self.dim, "in", t, "sec.", "using core", core)
def coherent(self, q_c, p_c, x=None): if not isinstance(q_c, mpmath.mpf) and not isinstance(p_c, mpmath.mpf): raise ValueError("q_c, p_c must be mpmath.mpf") if x == None: x = self.scaleinfo.x[0] re = -(x - q_c)*(x - q_c)*mpmath.pi/(self.scaleinfo.h) im = (x - q_c)*p_c*twopi/self.scaleinfo.h res = mpArray([mpmath.exp(mpmath.mpc(re[i], im[i])) for i in range(len(x))]) return res.normalize()
def cs(self, q_c, p_c): self.__raiseTypeError(q_c) self.__raiseTypeError(p_c) qrange = self.scaleinfo.domain[0] d = qrange[1] - qrange[0] lqmin, lqmax = qrange[0] - 2*d, qrange[1] + 2*d long_q = mpArray.linspace(lqmin, lqmax, 5*self.dim, endpoint=False) coh_state = self.coherent(q_c, p_c, long_q) vec = mpArray(self.dim) m = len(coh_state)/self.dim coh_state = coh_state.reshape(m,self.dim) for i in range(m): vec += coh_state[i][::1] return State(self.scaleinfo, vec.normalize())
def setOperator(self, fft=True): self.operator = [mpArray(self.dim) for i in range(2)] self.op0(self.scaleinfo.x[0], False) self.op1(self.scaleinfo.x[1], self._isShift())
def tanh(self, x, x1, x2, **kwargs): if not 'beta' in kwargs: raise ValueError("excepted kwargs: 'beta'") beta = kwargs['beta'] z1 = (x - x1)*beta z2 = (x - x2)*beta return mpArray( [ mpmath.mpf("0.5")*(mpmath.tanh(z1[i]) - mpmath.tanh(z2[i])) for i in range(len(x)) ])
def tompArray(self): return mpArray(self)
def exp(self, x, x1,x2,**kwargs): if not 'beta' in kwargs: raise ValueError("excepted kwargs: 'beta'") beta = kwargs['beta'] expo = self._expotanh(x, x1, x2, **kwargs) return mpArray([mpmath.exp(expo[i]/(twopi)*beta) for i in range(len(x))])
def set(self,x): if len(x) != self.scaleinfo.dim: raise TypeError("len(x) != dim") return State(self.scaleinfo, data = mpArray(x))
def expcs(self, q_c,p_c, gamma): cs = self.cs(q_c,p_c) expcs=mpArray([mpmath.exp(-mpmath.mpf("0.5")*gamma*x) for x in cs]) return State(self.scaleinfo,expcs)
def __init__(self,dim, domain): HilbertSpace.__init__(self, dim) self.scaleinfo = ScaleInfo(dim, domain) self.stateIn = mpArray(dim) self.stateOut = mpArray(dim)
def getEnergy(self,**kwargs): evals, evecs = self.getEigen(**kwargs) energy = mpArray([1.j*self.qmap.h*mpmath.log(val)/self.qmap.tau for val in evals]) return energy
def __setDomain(self, r): self.__setQdomain(r[0][0], r[0][1]) self.__setPdomain(r[1][0], r[1][1]) x = [mpArray(mpmath.linspace(self.domain[i][0], self.domain[i][1], self.dim, endpoint=False)) for i in range(2) ] self.__x = (x[0], x[1])