def __ilshift__(self, A): """ A can be two things : * G <<= any_gf_init will init the BlockGf with the initializer * G <<= g2 where g2 is a BlockGf will copy g2 into self """ if isinstance(A, self.__class__) : if self is not A : self.copy_from(A) # otherwise it is useless AND does not work !! elif isinstance(A, lazy_expressions.LazyExpr) : # A is a lazy_expression made of BlockGf, scalars, descriptors A2= descriptors.convert_scalar_to_const(A) def e_t (x) : if not isinstance(x, descriptors.Base) : return x tmp = self.copy() x(tmp) return tmp #e_t2 = self.__lazy_expr_eval_context__() self.copy_from ( lazy_expressions.eval_lazy_expr(e_t, A2) ) elif isinstance(A, lazy_expressions.LazyExprTerminal) : #e.g. g<<= SemiCircular (...) self <<= lazy_expressions.LazyExpr(A) elif descriptors.is_scalar(A) : #in the case it is a scalar .... self <<= lazy_expressions.LazyExpr(A) elif isinstance(A, gf_init.Base) : # backwards compatibility, deprecated A(self) else : raise RuntimeError, " BlockGf: <<= operator : RHS not understood" return self
def __lshift__(self, A): """ A can be two things: * G << any_init will init the GFBloc with the initializer * G << g2 where g2 is a GFBloc will copy g2 into self """ if isinstance(A, Gf): if self is not A: self.copy_from( A) # otherwise it is useless AND does not work !! elif isinstance( A, lazy_expressions.LazyExpr ): # A is a lazy_expression made of GF, scalars, descriptors A2 = descriptors.convert_scalar_to_const(A) def e_t(x): if not isinstance(x, descriptors.Base): return x tmp = self.copy() x(tmp) return tmp self.copy_from(lazy_expressions.eval_expr_with_context(e_t, A2)) elif isinstance(A, lazy_expressions.LazyExprTerminal ): #e.g. g<< SemiCircular (...) self << lazy_expressions.LazyExpr(A) elif descriptors.is_scalar(A): #in the case it is a scalar .... self << lazy_expressions.LazyExpr(A) else: raise NotImplemented return self
def __idiv__(self, arg): """ If arg is a scalar, simple scalar multiplication """ if descriptors.is_lazy(arg): return lazy_expressions.make_lazy(self) / arg if descriptors.is_scalar(arg): self.data /= arg self.tail /= arg else: raise RuntimeError, " argument type not recognized in imul for %s"%arg return self
def __imul__(self, arg): if type(self) == type(arg): d, d2 = self.data, arg.data assert d.shape == d2.shape, " Green function block multiplication with arrays of different size !" for om in range (d.shape[0]): d[om,:,:] = numpy.dot(d[om,:,:], d2[om,:,:]) self.tail = self.tail * arg.tail elif descriptors.is_scalar(arg): self.data *= arg self.tail *= arg else: raise RuntimeError, " argument type not recognized in *= for %s"%arg return self
def __isub__(self, arg): d, t = self.data, self.tail if type(self) == type(arg): d[:,:,:] -= arg.data t -= arg.tail elif isinstance(arg, numpy.ndarray): # an array considered as a constant function for om in range (d.shape[-1]): d[:,:, om ] -= arg t[0][:,:] -= arg elif descriptors.is_scalar(arg): # just a scalar arg = arg*numpy.identity(self.N1) for om in range (d.shape[-1]): d[:,:, om ] -= arg t[0][:,:] -= arg else: raise RuntimeError, " argument type not recognized in -= for %s"%arg return self
def __idiv__(self,arg): d,t = self._data.array, self._tail if hasattr(arg,"_data") : d2 = arg._data.array assert d.shape == d2.shape ," Green function block multiplication with arrays of different size !" for om in range (d.shape[-1]) : d[:,:,om ] = numpy.dot(d[:,:,om], numpy.linalg.inv(d2[:,:,om])) t /= arg._tail elif descriptors.is_scalar(arg): # a scalar d[:,:,:] /= arg for n in range(t.OrderMax): t[n].array[:,:] /= arg #t = self._tail; t /= arg else: raise NotImplementedError return self
def __isub__(self,arg): d,t = self._data.array, self._tail if hasattr(arg,"_data") : # a Green's function d[:,:,:] -= arg._data.array t -= arg._tail elif isinstance(arg,numpy.ndarray): # an array considered as a constant function for om in range (d.shape[-1]) : d[:,:,om ] -= arg t[0].array[:,:] -= arg elif isinstance(arg,list): # a list arg = numpy.array(arg) for om in range (d.shape[-1]) : d[:,:,om ] -= arg t[0].array[:,:] -= arg elif descriptors.is_scalar(arg): # just a scalar arg = arg*numpy.identity(self.N1) for om in range (d.shape[-1]) : d[:,:,om ] -= arg t[0].array[:,:] -= arg else: raise NotImplementedError return self
def __imul__(self,arg): """ If arg is a scalar, simple scalar multiplication If arg is a BlockGf(any object with _data and _tail as in BlockGf), they it is a matrix multiplication, slice by slice """ d,t = self._data.array, self._tail if hasattr(arg,"_data") : d2 = arg._data.array assert d.shape == d2.shape ," Green function block multiplication with arrays of different size !" for om in range (d.shape[-1]) : d[:,:,om ] = numpy.dot(d[:,:,om], d2[:,:,om]) t *= arg._tail elif descriptors.is_scalar(arg): # a scalar d[:,:,:] *= arg # to be simplified when the *= scalar for tail will be added ! for n in range(t.OrderMin,t.OrderMax+1): t[n].array[:,:] *= arg else: print type(arg) raise NotImplementedError return self
def __sub_isub_impl (self, lhs, arg, is_sub): d, t, rhs = lhs.data, lhs.tail,None if type(lhs) == type(arg): d[:,:,:] -= arg.data t -= arg.tail elif isinstance(arg, numpy.ndarray): # an array considered as a constant function MatrixStack(lhs.data).sub(arg) rhs = arg elif descriptors.is_scalar(arg): # just a scalar arg = arg*numpy.identity(lhs.N1,dtype = lhs.data.dtype ) MatrixStack(lhs.data).sub(arg) assert lhs.tail.shape[0] == lhs.tail.shape[1], "tail - scalar only valid in diagonal case" rhs = numpy.identity(lhs.tail.shape[0]) *arg else: raise RuntimeError, " argument type not recognized in -= for %s"%arg if rhs !=None : new_tail = TailGf(shape=lhs.tail.shape) new_tail[0][:,:] = rhs if is_sub : lhs._singularity = lhs.tail - new_tail else : lhs.tail = lhs.tail - new_tail return lhs
def _lshift_(self, A): """ A can be two things: * G << any_init will init the GFBloc with the initializer * G << g2 where g2 is a GFBloc will copy g2 into self """ import descriptors if isinstance(A, self.__class__): if self is not A: self.copy_from(A) # otherwise it is useless AND does not work !! elif isinstance(A, lazy_expressions.LazyExpr): # A is a lazy_expression made of GF, scalars, descriptors A2 = descriptors.convert_scalar_to_const(A) def e_t (x): if not isinstance(x, descriptors.Base): return x tmp = self.copy() x(tmp) return tmp self.copy_from (lazy_expressions.eval_expr_with_context(e_t, A2) ) elif isinstance(A, lazy_expressions.LazyExprTerminal): #e.g. g<< SemiCircular (...) self << lazy_expressions.LazyExpr(A) elif descriptors.is_scalar(A): #in the case it is a scalar .... self << lazy_expressions.LazyExpr(A) else: raise RuntimeError, " << operator: RHS not understood" return self
def __rmul__(self, arg): if descriptors.is_lazy(arg): return arg * lazy_expressions.make_lazy(self) elif descriptors.is_scalar(arg): return self.__mul__(arg)
def __div__(self, arg): assert descriptors.is_scalar(arg), "Error in /" res = self.copy() res /= arg return res