def jacobi(A, B, tol=0.005, forcedIter=0): '''itteratively solving for matrix A with solution vector B tol = tolerance for dh/h init_val = array of initial values to use in the solver ''' h = np.zeros(np.shape(B), float) dmax = 1.0 n = 0 tmp0 = np.empty(np.shape(A), float) tmp1 = np.empty(np.shape(B), float) AD = np.diagonal(A) #np.timer_reset() #np.evalflush() t1 = time.time() while (forcedIter and forcedIter > n) or \ (forcedIter == 0 and dmax > tol): n += 1 np.multiply(A, h, tmp0) np.add.reduce(tmp0, 1, out=tmp1) tmp2 = AD np.subtract(B, tmp1, tmp1) np.divide(tmp1, tmp2, tmp1) hnew = h + tmp1 np.subtract(hnew, h, tmp2) np.divide(tmp2, h, tmp1) np.absolute(tmp1, tmp1) dmax = np.maximum.reduce(tmp1) h = hnew print dmax #np.evalflush() t1 = time.time() - t1 print 'Iter: ', n, ' size: ', np.shape(B), ' time: ', t1, print "(Dist) notes: %s" % sys.argv[4] #if A.dist(): # print "(Dist) notes: %s"%sys.argv[4] #else: # print "(Non-Dist) notes: %s"%sys.argv[4] return h
def jacobi(A, B, tol=0.005, forcedIter=0): """itteratively solving for matrix A with solution vector B tol = tolerance for dh/h init_val = array of initial values to use in the solver """ h = np.zeros(np.shape(B), float) dmax = 1.0 n = 0 tmp0 = np.empty(np.shape(A), float) tmp1 = np.empty(np.shape(B), float) AD = np.diagonal(A) # np.timer_reset() # np.evalflush() t1 = time.time() while (forcedIter and forcedIter > n) or (forcedIter == 0 and dmax > tol): n += 1 np.multiply(A, h, tmp0) np.add.reduce(tmp0, 1, out=tmp1) tmp2 = AD np.subtract(B, tmp1, tmp1) np.divide(tmp1, tmp2, tmp1) hnew = h + tmp1 np.subtract(hnew, h, tmp2) np.divide(tmp2, h, tmp1) np.absolute(tmp1, tmp1) dmax = np.maximum.reduce(tmp1) h = hnew print dmax # np.evalflush() t1 = time.time() - t1 print "Iter: ", n, " size: ", np.shape(B), " time: ", t1, print "(Dist) notes: %s" % sys.argv[4] # if A.dist(): # print "(Dist) notes: %s"%sys.argv[4] # else: # print "(Non-Dist) notes: %s"%sys.argv[4] return h
def MC_int(c, s): x = np.empty([s], dtype=np.double) y = np.empty([s], dtype=np.double) sum=0.0 #np.timer_reset() #np.evalflush() start=time.time() for i in range(c): #np.ufunc_random(x,x) #np.ufunc_random(y,y) x = np.random.random_sample([s]) y = np.random.random_sample([s]) np.square(x,x) np.square(y,y) np.add(x,y,x) z = np.less_equal(x, 1) sum += np.add.reduce(z)*4.0/s sum = sum / c #np.evalflush() stop=time.time() print 'Pi: ', sum, ' with ', s,' samples in sec: ', stop-start, print "(Dist) notes: %s"%sys.argv[3]