def fromfile(k): ''' See if the two-scale coeffs have already been computed and stored on disk. Return the data are successfully read from disk, otherwise None. ''' fname = '/tmp/two-scale-coeffs.' + str(k) try: f = open(fname, 'rb') data = array('d', []) data.fromfile(f, 4 * k * k) h0 = mathutil.zeromatrix(k, k) h1 = mathutil.zeromatrix(k, k) g0 = mathutil.zeromatrix(k, k) g1 = mathutil.zeromatrix(k, k) pt = 0 for i in range(k): for j in range(k): h0[i][j] = data[pt] h1[i][j] = data[pt + 1] g0[i][j] = data[pt + 2] g1[i][j] = data[pt + 3] pt = pt + 4 #print ' Read two-scale coefficients from file',fname return (h0, h1, g0, g1) except IOError, EOFError: return None
def fromfile(k): ''' See if the two-scale coeffs have already been computed and stored on disk. Return the data are successfully read from disk, otherwise None. ''' fname = '/tmp/two-scale-coeffs.' + str(k) try: f = open(fname,'rb') data = array('d',[]) data.fromfile(f,4*k*k) h0 = mathutil.zeromatrix(k,k) h1 = mathutil.zeromatrix(k,k) g0 = mathutil.zeromatrix(k,k) g1 = mathutil.zeromatrix(k,k) pt = 0 for i in range(k): for j in range(k): h0[i][j] = data[pt] h1[i][j] = data[pt+1] g0[i][j] = data[pt+2] g1[i][j] = data[pt+3] pt = pt + 4 #print ' Read two-scale coefficients from file',fname return (h0,h1,g0,g1) except IOError, EOFError: return None
def twoscalecoeffs(k, usecached=1): ''' Return the two-scale coefficients for Alperts basis of given order coarser <= finer phi_i(x) = sqrt(2)*sum(j) h0_ij*phi_j(2x) + h1_ij*phi_j(2x-1) ditto for psi_i(x) with g instead of h s_n,i,l = sum(j) h0_ij*s_n+1,j,2l + h1_ij*s_n+1,j,2l+1 ditto for d with g instead of h finer <= coarser phi_i(2x) = (1/sqrt(2))*sum(j) h0_ji*phi_j(x) + g0_ji*psi(x) phi_i(2x-1) = (1/sqrt(2))*sum(j) h1_ji*phi_j(x) + g1_ji*psi(x) s_n+1,i,2l = sum(j) h0_ji*sn,j,l + g0_ji*d_n,j,l s_n+1,i,2l+1 = sum(j) h1_ji*sn,j,l + g1_ji*d_n,j,l ''' global phi_norms_long if usecached: cached = fromfile(k) if cached: return cached nbitssave = longfloat.nbits if k < 11: longfloat.nbits = 156 elif k < 19: longfloat.nbits = 208 elif k < 24: longfloat.nbits = 260 elif k < 29: longfloat.nbits = 320 elif k < 33: longfloat.nbits = 360 elif k < 37: longfloat.nbits = 400 elif k < 42: longfloat.nbits = 440 elif k < 46: longfloat.nbits = 480 elif k < 49: longfloat.nbits = 520 else: longfloat.nbits = 800 longfloat.nbits = 800 print ' Generating two-scale coeffcients for k=%d using %d-bit floating point numbers' \ % (k, longfloat.nbits) # Force recomputation of the cached sqrt(2*j+1) values phi_norms_long = [] # Compute the coeffcicients to high precision using longfloats (h0l, h1l) = h0h1(k) (g0l, g1l) = g0g1(k) # Copy them into arrays of floats h0 = mathutil.zeromatrix(k, k) g0 = mathutil.zeromatrix(k, k) h1 = mathutil.zeromatrix(k, k) g1 = mathutil.zeromatrix(k, k) gerr = 0.0 herr = 0.0 for i in range(k): for j in range(k): hphase = (-1)**(i + j) gphase = (-1)**(i + j + k) h0[i][j] = float(h0l[i][j]) h1[i][j] = h0[i][j] * hphase g0[i][j] = float(g0l[i][j]) g1[i][j] = g0[i][j] * gphase if abs(h0[i][j]) < 4e-16: h0[i][j] = 0.0 if abs(h1[i][j]) < 4e-16: h1[i][j] = 0.0 if abs(g0[i][j]) < 4e-16: g0[i][j] = 0.0 if abs(g1[i][j]) < 4e-16: g1[i][j] = 0.0 testerr = float(abs(g0l[i][j] - gphase * g1l[i][j])) if g0[i][j] != 0.0: testerr = testerr / abs(g0[i][j]) gerr = max(gerr, testerr) testerr = float(abs(h0l[i][j] - hphase * h1l[i][j])) if h0[i][j] != 0.0: testerr = testerr / abs(h0[i][j]) herr = max(herr, testerr) print ' maximum symmetry error in h ', float(herr) print ' maximum symmetry error in g ', float(gerr) longfloat.nbits = nbitssave if max(float(herr), float(gerr)) > 4e-16: raise "twoscalecoeffs have inadequate precision" tofile(h0, h1, g0, g1) return (h0, h1, g0, g1)
def twoscalecoeffs(k,usecached=1): ''' Return the two-scale coefficients for Alperts basis of given order coarser <= finer phi_i(x) = sqrt(2)*sum(j) h0_ij*phi_j(2x) + h1_ij*phi_j(2x-1) ditto for psi_i(x) with g instead of h s_n,i,l = sum(j) h0_ij*s_n+1,j,2l + h1_ij*s_n+1,j,2l+1 ditto for d with g instead of h finer <= coarser phi_i(2x) = (1/sqrt(2))*sum(j) h0_ji*phi_j(x) + g0_ji*psi(x) phi_i(2x-1) = (1/sqrt(2))*sum(j) h1_ji*phi_j(x) + g1_ji*psi(x) s_n+1,i,2l = sum(j) h0_ji*sn,j,l + g0_ji*d_n,j,l s_n+1,i,2l+1 = sum(j) h1_ji*sn,j,l + g1_ji*d_n,j,l ''' global phi_norms_long if usecached: cached = fromfile(k) if cached: return cached nbitssave = longfloat.nbits if k < 11: longfloat.nbits = 156 elif k < 19: longfloat.nbits = 208 elif k < 24: longfloat.nbits = 260 elif k < 29: longfloat.nbits = 320 elif k < 33: longfloat.nbits = 360 elif k < 37: longfloat.nbits = 400 elif k < 42: longfloat.nbits = 440 elif k < 46: longfloat.nbits = 480 elif k < 49: longfloat.nbits = 520 else: longfloat.nbits = 800 longfloat.nbits = 800 print ' Generating two-scale coeffcients for k=%d using %d-bit floating point numbers' \ % (k, longfloat.nbits) # Force recomputation of the cached sqrt(2*j+1) values phi_norms_long = [] # Compute the coeffcicients to high precision using longfloats (h0l,h1l) = h0h1(k) (g0l,g1l) = g0g1(k) # Copy them into arrays of floats h0 = mathutil.zeromatrix(k,k) g0 = mathutil.zeromatrix(k,k) h1 = mathutil.zeromatrix(k,k) g1 = mathutil.zeromatrix(k,k) gerr = 0.0 herr = 0.0 for i in range(k): for j in range(k): hphase = (-1)**(i+j) gphase = (-1)**(i+j+k) h0[i][j] = float(h0l[i][j]) h1[i][j] = h0[i][j]*hphase g0[i][j] = float(g0l[i][j]) g1[i][j] = g0[i][j]*gphase if abs(h0[i][j]) < 4e-16: h0[i][j] = 0.0 if abs(h1[i][j]) < 4e-16: h1[i][j] = 0.0 if abs(g0[i][j]) < 4e-16: g0[i][j] = 0.0 if abs(g1[i][j]) < 4e-16: g1[i][j] = 0.0 testerr = float(abs(g0l[i][j]-gphase*g1l[i][j])) if g0[i][j] != 0.0: testerr = testerr/abs(g0[i][j]) gerr = max(gerr,testerr) testerr = float(abs(h0l[i][j]-hphase*h1l[i][j])) if h0[i][j] != 0.0: testerr = testerr/abs(h0[i][j]) herr = max(herr,testerr) print ' maximum symmetry error in h ', float(herr) print ' maximum symmetry error in g ', float(gerr) longfloat.nbits = nbitssave if max(float(herr),float(gerr)) > 4e-16: raise "twoscalecoeffs have inadequate precision" tofile(h0,h1,g0,g1) return (h0,h1,g0,g1)