def runTests(sampleNum, a, b): runs = [] for ii in xrange(sampleNum): out = [] ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) out.append(timing.dotest(lambda: a + b)) out.append(timing.dotest(lambda: ra + rb)) out.append(timing.dotest(lambda: a - b)) out.append(timing.dotest(lambda: ra - rb)) out.append(timing.dotest(lambda: a * b)) out.append(timing.dotest(lambda: ra * rb)) if (b != 0): out.append(timing.dotest(lambda: a / b)) out.append(timing.dotest(lambda: ra / rb)) out.append(timing.dotest(lambda: a % b)) out.append(timing.dotest(lambda: ra % rb)) else: out.extend([0, 0, 0, 0]) runs.append(out) base = runs.pop(0) for run in runs: for ii in xrange(len(base)): base[ii] += run[ii] for ii in xrange(len(base)): base[ii] /= (1.0 * sampleNum) return base
def stressTest(): count = 0 product = 0 sparseAns = 0 rand1 = random.randint(0, 10000000000) rand2 = random.randint(0, 10000000000) if (rand2 > rand2): temp = rand2 rand2 = rand1 rand1 = temp productNorm = rand1 * rand2 sumNorm = rand1 + rand2 diffNorm = rand1 - rand2 quotNorm = rand1 / rand2 modNorm = rand1 % rand2 a = rrrsparse.RRRSparse(rand1) b = rrrsparse.RRRSparse(rand2) if (int(a + b) != sumNorm): print "Failed on " + str(rand1) + " + " + str(rand2) return False #if(int(a*b)!=productNorm): # print "Failed on "+ str(rand1) + " * " + str(rand2) # return False #if(int(a-b)!=diffNorm): # print "Failed on "+ str(rand1) + " - " + str(rand2) # return False #if(int(a/b)!=quotNorm): # print "Failed on "+ str(rand1) + " / " + str(rand2) # return False #if(int(a%b)!=modNorm): # print "Failed on "+ str(rand1) + " % " + str(rand2) # return False return True
def doMath(a, b, c): # get the correct answers if (c == 0): c = 1 powModNorm = a**b % c ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) rc = rrrsparse.RRRSparse(c) result = rrrsparse.RRRSparse() result = result.powMod(ra, rb, rc) if (result != powModNorm): print "Pow Mod Failed" print "Sparse gave " + str(int(result)) print "Normal math gave " + str(powModNorm) return False return True
def runTests(sampleNum, a, b): out = [] ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) out.append(timing.dotest(lambda: a + b)) out.append(timing.dotest(lambda: ra + rb)) out.append(timing.dotest(lambda: a - b)) out.append(timing.dotest(lambda: ra - rb)) out.append(timing.dotest(lambda: a * b)) out.append(timing.dotest(lambda: ra * rb)) if (b != 0): out.append(timing.dotest(lambda: a / b)) out.append(timing.dotest(lambda: ra / rb)) out.append(timing.dotest(lambda: a % b)) out.append(timing.dotest(lambda: ra % rb)) else: out.extend([0, 0, 0, 0]) return out
def getTimes(sampleNum, a, b): test = [] for ii in xrange(max(sampleNum, 1)): out = [] t1 = datetime.datetime.now() z = a + b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = a - b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = a * b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) if (b != 0): t1 = datetime.datetime.now() z = a / b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) else: out.append(0) if (b != 0): t1 = datetime.datetime.now() z = a % b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) else: out.append(0) t1 = datetime.datetime.now() z = a == b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = a < b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = a <= b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = a != b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = a > b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = a >= b t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) t1 = datetime.datetime.now() z = -a t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra + rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra - rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra * rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) if b != 0: ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra / rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) else: out.append(0) if b != 0: ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra % rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) else: out.append(0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra == rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra < rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra <= rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra != rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra > rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = ra >= rb t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) t1 = datetime.datetime.now() z = -ra t2 = datetime.datetime.now() c = t1 - t2 out.append(c.seconds + c.microseconds / 1000000.0) test.append(out) result = [] for ii in xrange(len(test[-1])): result.append(0) for jj in xrange(len(test)): result[-1] += test[jj][ii] result[-1] /= (len(test) * 1.0) return result
import rrrsparse x = rrrsparse.RRRSparse(2) y = rrrsparse.RRRSparse(1) print int(y - x) z = y - x print z.digits print y - x print int(z)
def doMath(a, b): # get the correct answers normSum = a + b normDiff = a - b normMult = a * b normNe = a != b normEq = a == b normLt = a < b normLte = a <= b normGt = a > b normGte = a >= b if (b != 0): normDiv = a // b if (b != 0): normMod = a % b # first test sparse int """asp = sparseInt.SparseInt(a) bsp = sparseInt.SparseInt(b) if(asp + bsp != normSum): print "Sum Failed" return False if(asp - bsp != normDiff): print "Difference Failed" return False if(asp * bsp != normMult): print "Normal Multiplication Failed" return False if(b>0): if(asp / bsp != normDiv): print "Division Failed" return False if(asp % bsp != normMod): print "Modulus Failed" return False if(sparseInt(heapMult.HeapMult.sparseHeapMult(asp.digits,bsp.digits))!= normMult): print "Heap Mult Failed" return False""" # Now Test the sparse rrr ra = rrrsparse.RRRSparse(a) rb = rrrsparse.RRRSparse(b) if (ra + rb != normSum): print "RRR Sum Failed" return False #ra = rrrsparse.RRRSparse(a) #rb = rrrsparse.RRRSparse(b) if (ra - rb != normDiff): print "RRR Sub Failed" #print "had " +str(int(ra -rb)) + " when it needed "+str(normDiff) return False #ra = rrrsparse.RRRSparse(a) #rb = rrrsparse.RRRSparse(b) if (ra * rb != normMult): print "RRR Mult Failed" return False #ra = rrrsparse.RRRSparse(a) #rb = rrrsparse.RRRSparse(b) if b != 0: #ra = rrrsparse.RRRSparse(a) #rb = rrrsparse.RRRSparse(b) # print (ra / rb).digits product = ra / rb if (product != normDiv): print "RRR Div Failed" print "Normal Math gave " + str(normDiv) print "Sparse Math gave " + str(product) return False #ra = rrrsparse.RRRSparse(a) #rb = rrrsparse.RRRSparse(b) if (b != 0 and b > 0 and a > 0): temp = ra % rb if (temp != normMod): print "RRR mod failed" print "Sparse gave " + str(int(temp)) print "Real Math gave " + str(normMod) return False #return True""" # boolean section #ra = rrrsparse.RRRSparse(a) #rb = rrrsparse.RRRSparse(b) if (ra != rb) != normNe: print "RRR Not Equal failed" return False if (ra == rb) != normEq: print "RRR Equal failed" return False if (ra < rb) != normLt: print "RRR Less Than failed" return False if (ra <= rb) != normLte: print "RRR Less Than or Equal failed" return False if (ra > rb) != normGt: print "RRR Greaer failed" return False if (ra >= rb) != normGte: print "RRR Greater Than or Equal failed" return False return True
outStr += "1" sn = d[p][0] < 1 p += 1 else: if sn: outStr += "1" else: outStr += "0" if (outStr[-1] == "0"): return outStr[:len(outStr) - 1] return outStr #print "decimal\tsparse\nbinary reversed" num = 0 while (True): spnum = rrrsparse.RRRSparse(num) if (bin(num).split('b')[1][::-1] != simple(spnum)): outStr = str(num) + "\t" spnum = rrrsparse.RRRSparse(num) outStr += spnum.oldStr() + "\t" outStr += bin(num).split('b')[1][::-1] print outStr + "\tfliped binary" outStr = str(num) + "\t" spnum = rrrsparse.RRRSparse(num) outStr += spnum.oldStr() + "\t" outStr += simple(spnum) print outStr + "\tcalculated" break num += 1