def testDiv(self): """ test division """ a = Table(['a','b','c','d'],[2,3,4,5],range(2*3*4*5)) b = Table(['c','b','e'],[4,3,6],range(12*6)) c = Table(['a','b','c','d','e'],[2,3,4,5,6],range(2*3*4*5*6)) acpt = copy(a.cpt)[...,na.NewAxis] bcpt = copy(b.cpt)[...,na.NewAxis,na.NewAxis] bcpt.transpose([3,1,0,4,2]) ab = a/b cc = c/c bb = b/b cres = na.ones(2*3*4*5*6) cres[0] = 0 bres = na.ones(12*6) bres[0] = 0 ares = acpt/bcpt ares[getnan(ares)] = 0.0 assert (ab == Table(['a','b','c','d','e'],[2,3,4,5,6],ares) and \ cc == Table(['a','b','c','d','e'],[2,3,4,5,6],cres) and \ bb == Table(['c','b','e'],[4,3,6],bres) ), \ " Division does not work"
def evaluate(c, y, x): "Evaluates c in the context of _UFuncs, x, and y." x,y = (x - SIZE/2) * RATIO, (SIZE/2 - y) * RATIO # Misalign some math errors x += 1e-10 y += 1e-10 result = eval(c, ufunc._UFuncs, dict(x=x,y=y)) try: len(result) except TypeError: # Idiot c evaluated to a float. This is because the random tree # generator picked all constants, without any x or y. Let's be # nice and turn it into a bunch of that result. result *= ones(type=Float64, shape=(SIZE, SIZE)) # Normalize the data to interval[0,255]. Do this by finding the # minimum and maximum values. copy = ravel(result.copy()) copy.sort() for minv in copy: if not is_special(minv): break else: raise ValueError("Ack! No numbers!") for maxv in copy[::-1]: if not is_special(maxv): break else: break else: raise ValueError("Ack! No numbers!") print "Min, max:", (minv,maxv) diff = maxv-minv+1 result[ieee.getnan(result)] = minv result[ieee.getneginf(result)] = minv result[ieee.getposinf(result)] = maxv result -= minv result /= diff result *= 255 result[ieee.getnan(result)] = 0 result[ieee.getinf(result)] = 0 return result.astype(UInt32)
def testIDiv(self): """ test inplace division """ b = Table(['c','b','e'],[4,3,6],range(12*6)) c = Table(['a','b','c','d','e'],[2,3,4,5,6],range(2*3*4*5*6)) bcpt = b.cpt[...,na.NewAxis,na.NewAxis] bcpt.transpose([3,1,0,4,2]) res = c.cpt/bcpt res[getnan(res)] = 0.0 c /= b assert (na.all(c.cpt == res)), \ " InPlace Division does not work"
def __div__(a, b): """ multiplication PRE: a=Pr(A); A = {'a','b','c'} b=Pr(B); B = {'c','a','d','e'} usage: c = a/b c is a NEW Table instance POST: c=Pr(A U B) = Pr(a,b,c,d,e) Notes : - c keeps the order of the variables in a - any new variables in b (d and e) are added at the end of c in the order they appear in b - a and b are not touched during this operation - return a NEW Table instance """ ######################################### #---TODO: add division with a number ######################################### # prepare dimensions in a and b for multiplication new, cptb = a.union(b) # multiply #new.cpt /= cptb # this does not work correctly for some reason... #na.divide(new.cpt,cptb,new.cpt) # does not work either new.cpt = new.cpt / cptb #this one works fine #is this a numarray BUG???? ## WARNING, division by zero, avoided using na.Error.setMode(invalid='ignore') # replace INFs by 0s new.cpt[getnan(new.cpt)] = 0 #---TODO: replace this very SLOW function with a ufunc return new
def __idiv__(a,b): """ in place division PRE: - B must be a subset of A!!! eg. a=Pr(A); A = {'a','b','c'} b=Pr(B); B = {'c','a'} usage: a/=b POST: a=Pr(A)/Pr(B) = Pr(a,b,c) Notes : - a keeps the order of its existing variables - b is not touched during this operation - operation is done in-place for a, a is not the same after the operation """ # prepare dimensions in b for multiplication cptb = a.prepareOther(b) # multiply in place, a's values are changed #a.cpt /= cptb # this does not work correctly for some reason... #na.divide(a.cpt,cptb,a.cpt) # does not work either a.cpt = a.cpt / cptb #this one works fine #is this a numarray BUG???? ## WARNING, division by zero, avoided using na.Error.setMode(invalid='ignore') # replace INFs by 0s a.cpt[getnan(a.cpt)] = 0 #---TODO: replace this very SLOW function with a ufunc return a