classRange(Set):""" Represents a range of integers. Examples ======== >>> from sympy import Range >>> list(Range(5)) # 0 to 5 [0, 1, 2, 3, 4] >>> list(Range(10, 15)) # 10 to 15 [10, 11, 12, 13, 14] >>> list(Range(10, 20, 2)) # 10 to 20 in steps of 2 [10, 12, 14, 16, 18] >>> list(Range(20, 10, -2)) # 20 to 10 backward in steps of 2 [12, 14, 16, 18, 20] """is_iterable=Truedef__new__(cls,*args):# expand rangeslc=slice(*args)start,stop,step=slc.startor0,slc.stop,slc.stepor1try:start,stop,step=[S(as_int(w))forwin(start,stop,step)]exceptValueError:raiseValueError("Inputs to Range must be Integer Valued\n"+"Use TransformationSets of Ranges for other cases")n=ceiling((stop-start)/step)ifn<=0:returnS.EmptySet# normalize args: regardless of how they are entered they will show# canonically as Range(inf, sup, step) with step > 0start,stop=sorted((start,start+(n-1)*step))step=abs(step)returnBasic.__new__(cls,start,stop+step,step)start=property(lambdaself:self.args[0])stop=property(lambdaself:self.args[1])step=property(lambdaself:self.args[2])def_intersect(self,other):ifother.is_Interval:osup=other.supoinf=other.inf# if other is [0, 10) we can only go up to 9ifosup.is_integerandother.right_open:osup-=1ifoinf.is_integerandother.left_open:oinf+=1# Take the most restrictive of the bounds set by the two sets# round inwardsinf=ceiling(Max(self.inf,oinf))sup=floor(Min(self.sup,osup))# if we are off the sequence, get back onoff=(inf-self.inf)%self.stepifoff:inf+=self.step-offreturnRange(inf,sup+1,self.step)ifother==S.Naturals:returnself._intersect(Interval(1,oo))ifother==S.Integers:returnselfreturnNonedef_contains(self,other):return(other>=self.infandother<=self.supandask(Q.integer((self.start-other)/self.step)))def__iter__(self):i=self.startwhile(i<self.stop):yieldii=i+self.stepdef__len__(self):return((self.stop-self.start)//self.step)def_ith_element(self,i):returnself.start+i*self.step@propertydef_last_element(self):returnself._ith_element(len(self)-1)@propertydef_inf(self):returnself.start@propertydef_sup(self):returnself.stop-self.step