def prox_sdss_symmetry(X, step): """SDSS/HSC symmetry operator This function uses the *minimum* of the two symmetric pixels in the update. """ Xs = np.fliplr(np.flipud(X)) X[:] = np.min([X, Xs], axis=0) return X
def fft_convolve2d(x, y): """ 2D convolution, using FFT""" fr = fft.fft2(x) fr2 = fft.fft2(np.flipud(np.fliplr(y))) m, n = fr.shape cc = np.real(fft.ifft2(fr * fr2)) cc = np.roll(cc, -int(m / 2) + 1, axis=0) cc = np.roll(cc, -int(n / 2) + 1, axis=1) return cc
def f_symmetry(dof, Mx, My, xsym, ysym, Nlayer=1): out = [] for i in range(Nlayer): df = np.reshape(dof[i * Mx * My:(i + 1) * Mx * My], (Mx, My)) if xsym == 1: df = np.hstack((df, np.fliplr(df))) if ysym == 1: df = np.vstack((df, np.flipud(df))) out.append(df.flatten()) return np.concatenate(np.array(out))
def prox_soft_symmetry(X, step, strength=1): """Soft version of symmetry Using a `sigma` that varies from 0 to 1, with 0 meaning no symmetry enforced at all and 1 being completely symmetric, the user can customize the level of symmetry required for a component """ Xs = np.fliplr(np.flipud(X)) X[:] = 0.5 * strength * (X + Xs) + (1 - strength) * X return X
def projectSimplex(mat): """ project each row vector to the simplex """ nPoints, nVars = mat.shape mu = np.fliplr(np.sort(mat, axis=1)) sum_hist = np.cumsum(mu, axis=1) flag = (mu - 1./np.tile(np.arange(1,nVars+1),(nPoints,1))*(sum_hist-1) > 0) f_flag = lambda flagPoint: len(flagPoint) - 1 - \ flagPoint[::-1].argmax() lastTrue = map(f_flag, flag) sm_row = sum_hist[np.arange(nPoints), lastTrue] theta = (sm_row - 1)*1./(np.array(lastTrue)+1.) w = np.maximum(mat - np.tile(theta, (nVars,1)).T, 0.) return w
def projectSimplex(mat): """ project each row vector to the simplex """ nPoints, nVars = mat.shape mu = np.fliplr(np.sort(mat, axis=1)) sum_hist = np.cumsum(mu, axis=1) flag = (mu - 1. / np.tile(np.arange(1, nVars + 1), (nPoints, 1)) * (sum_hist - 1) > 0) f_flag = lambda flagPoint: len(flagPoint) - 1 - \ flagPoint[::-1].argmax() lastTrue = map(f_flag, flag) sm_row = sum_hist[np.arange(nPoints), lastTrue] theta = (sm_row - 1) * 1. / (np.array(lastTrue) + 1.) w = np.maximum(mat - np.tile(theta, (nVars, 1)).T, 0.) return w
def fun(x): return to_scalar(np.fliplr(x)) d_fun = lambda x : to_scalar(grad(fun)(x))
def fun(x): return to_scalar(np.fliplr(x))
lambda g, ans, gvs, vs, x, shift, axis=None: anp.roll(g, shift, axis=axis)) anp.array_split.defjvp(lambda g, ans, gvs, vs, ary, idxs, axis=0: anp. array_split(g, idxs, axis=axis)) anp.split.defjvp( lambda g, ans, gvs, vs, ary, idxs, axis=0: anp.split(g, idxs, axis=axis)) anp.vsplit.defjvp(lambda g, ans, gvs, vs, ary, idxs: anp.vsplit(g, idxs)) anp.hsplit.defjvp(lambda g, ans, gvs, vs, ary, idxs: anp.hsplit(g, idxs)) anp.dsplit.defjvp(lambda g, ans, gvs, vs, ary, idxs: anp.dsplit(g, idxs)) anp.ravel.defjvp( lambda g, ans, gvs, vs, x, order=None: anp.ravel(g, order=order)) anp.expand_dims.defjvp( lambda g, ans, gvs, vs, x, axis: anp.expand_dims(g, axis)) anp.squeeze.defjvp(lambda g, ans, gvs, vs, x, axis=None: anp.squeeze(g, axis)) anp.diag.defjvp(lambda g, ans, gvs, vs, x, k=0: anp.diag(g, k)) anp.flipud.defjvp(lambda g, ans, gvs, vs, x, : anp.flipud(g)) anp.fliplr.defjvp(lambda g, ans, gvs, vs, x, : anp.fliplr(g)) anp.rot90.defjvp(lambda g, ans, gvs, vs, x, k=1: anp.rot90(g, k)) anp.trace.defjvp(lambda g, ans, gvs, vs, x, offset=0: anp.trace(g, offset)) anp.full.defjvp(lambda g, ans, gvs, vs, shape, fill_value, dtype=None: anp. full(shape, g, dtype), argnum=1) anp.triu.defjvp(lambda g, ans, gvs, vs, x, k=0: anp.triu(g, k=k)) anp.tril.defjvp(lambda g, ans, gvs, vs, x, k=0: anp.tril(g, k=k)) anp.clip.defjvp(lambda g, ans, gvs, vs, x, a_min, a_max: g * anp.logical_and( ans != a_min, ans != a_max)) anp.swapaxes.defjvp( lambda g, ans, gvs, vs, x, axis1, axis2: anp.swapaxes(g, axis1, axis2)) anp.rollaxis.defjvp( lambda g, ans, gvs, vs, a, axis, start=0: anp.rollaxis(g, axis, start)) anp.real_if_close.defjvp(lambda g, ans, gvs, vs, x: npg.match_complex(vs, g)) anp.real.defjvp(lambda g, ans, gvs, vs, x: anp.real(g))
def fun(x): return np.fliplr(x) mat = npr.randn(10, 11)
def fun(x): return to_scalar(np.fliplr(x)) d_fun = lambda x : to_scalar(grad(fun)(x))
def fun(x): return np.fliplr(x)