def cg(A, b, x=None, tol=1e-5, force_niter=None): """ Conjugate Gradient (CG) solver Implemented as example MATLAB code from <https://en.wikipedia.org/wiki/Conjugate_gradient_method> """ # If no guess is given, set an empty guess if x is None: x = array_create.zeros_like(b) r = b - dot(A, x) p = r.copy() r_squared = r * r rsold = np.sum(r_squared) tol_squared = tol * tol i = 0 while np.max(r_squared) > tol_squared or force_niter is not None: Ap = dot(A, p) alpha = rsold / dot(p, Ap) x = x + alpha * p r = r - alpha * Ap r_squared = r * r rsnew = np.sum(r_squared) p = r + (rsnew / rsold) * p rsold = rsnew if force_niter is not None and i >= force_niter: break i += 1 return x
def cg(A, b, x=None, tol=1e-5): # If no guess is given, set an empty guess if x is None: x = np.zeros(shape=b.shape, dtype=b.dtype) # Initialize arrays alpha = np.zeros(shape=(1, 1), dtype=b.dtype) rsold = np.zeros(shape=(1, 1), dtype=b.dtype) rsnew = np.zeros(shape=(1, 1), dtype=b.dtype) r = b - np.dot(A, x) p = r.copy() r_squared = r * r rsold = np.sum(r_squared) tol_squared = tol * tol while np.max(r_squared) > tol_squared: Ap = np.dot(A, p) alpha = rsold / np.dot(p, Ap) x = x + alpha * p r = r - alpha * Ap r_squared = r * r rsnew = np.sum(r_squared) p = r + (rsnew / rsold) * p rsold = rsnew return x
def normalMapFromHeightMap(heightmap): """ Create a normal map from a height map (sometimes called a bumpmap) :param heightmap: height map to convert Comes from: http://www.juhanalankinen.com/calculating-normalmaps-with-python-and-numpy/ """ heightmap = numpyArray(heightmap) heightmap = normalize(heightmap) matI = np.identity(heightmap.shape[0] + 1) matNeg = -1 * matI[0:-1, 1:] matNeg[0, -1] = -1 matI = matI[1:, 0:-1] matI[-1, 0] = 1 matI += matNeg matGradX = (matI.dot(heightmap.T)).T matGradY = matI.dot(heightmap) matNormal = np.zeros([heightmap.shape[0], heightmap.shape[1], 3]) matNormal[:, :, 0] = -matGradX matNormal[:, :, 1] = matGradY matNormal[:, :, 2] = 1.0 fNormMax = np.max(np.abs(matNormal)) matNormal = ((matNormal / fNormMax) + 1.0) / 2.0 return matNormal
def toneSplit(image, atPoints=None, absolute=False): """ Break an image into n+1 images at selected value points. The default points are to get images for tritone adjustment :param image: image to split :param atPoints: what grayscale values to split the image :param absolute: whether atPoints are absolute values or percentages of the available space :return: given n atPoints, will return n+1 images """ ret = [] if atPoints is None: atPoints = [0.15, 0.85] if not absolute: b = min(1.0, np.max(image)) m = 1 / (max(0.0, np.min(image)) - b) else: b = 0 m = 1 for pointRange in _pointsToRanges(atPoints): ret.append( np.logical_and(image >= pointRange[0] * m + b, image < pointRange[1] * m + b), atPoints) return ret
count = 0 #xsave = [0]*steps #save_As=[] #save_distribution = [] WRITE("Starting simulation") avalanche_sizes = [] start = time.time() for i in range(steps): print('Step:', i) WRITE("Step: " + str(i)) if np.mod(i, float(steps) / 100) == 0: # dist = list(np.bincount(list(degree.flat))) x_axis = list(np.arange(0, int(np.max(degree) + 1))) save_frame(i, A, [x_axis, dist]) #Particle addition add_site = np.random.randint(0, N - 1) x[add_site] = x[add_site] + 1 WRITE("Starting avalanche processing") #Avalanche processing """ degree = np.array(np.sum(A,0)) xc = degree + (degree==0)