def test_legvander(self) : # check for 1d x x = np.arange(3) v = leg.legvander(x, 3) assert_(v.shape == (3, 4)) for i in range(4) : coef = [0]*i + [1] assert_almost_equal(v[..., i], leg.legval(x, coef)) # check for 2d x x = np.array([[1, 2], [3, 4], [5, 6]]) v = leg.legvander(x, 3) assert_(v.shape == (3, 2, 4)) for i in range(4) : coef = [0]*i + [1] assert_almost_equal(v[..., i], leg.legval(x, coef))
def test_legendre_val(): """Test Legendre polynomial (derivative) equivalence """ rng = np.random.RandomState(0) # check table equiv xs = np.linspace(-1., 1., 1000) n_terms = 100 # True, numpy vals_np = legendre.legvander(xs, n_terms - 1) # Table approximation for fun, nc in zip([_get_legen_lut_fast, _get_legen_lut_accurate], [100, 50]): lut, n_fact = _get_legen_table('eeg', n_coeff=nc, force_calc=True) vals_i = fun(xs, lut) # Need a "1:" here because we omit the first coefficient in our table! assert_allclose(vals_np[:, 1:vals_i.shape[1] + 1], vals_i, rtol=1e-2, atol=5e-3) # Now let's look at our sums ctheta = rng.rand(20, 30) * 2.0 - 1.0 beta = rng.rand(20, 30) * 0.8 lut_fun = partial(fun, lut=lut) c1 = _comp_sum_eeg(beta.flatten(), ctheta.flatten(), lut_fun, n_fact) c1.shape = beta.shape # compare to numpy n = np.arange(1, n_terms, dtype=float)[:, np.newaxis, np.newaxis] coeffs = np.zeros((n_terms,) + beta.shape) coeffs[1:] = (np.cumprod([beta] * (n_terms - 1), axis=0) * (2.0 * n + 1.0) * (2.0 * n + 1.0) / n) # can't use tensor=False here b/c it isn't in old numpy c2 = np.empty((20, 30)) for ci1 in range(20): for ci2 in range(30): c2[ci1, ci2] = legendre.legval(ctheta[ci1, ci2], coeffs[:, ci1, ci2]) assert_allclose(c1, c2, 1e-2, 1e-3) # close enough... # compare fast and slow for MEG ctheta = rng.rand(20 * 30) * 2.0 - 1.0 beta = rng.rand(20 * 30) * 0.8 lut, n_fact = _get_legen_table('meg', n_coeff=10, force_calc=True) fun = partial(_get_legen_lut_fast, lut=lut) coeffs = _comp_sums_meg(beta, ctheta, fun, n_fact, False) lut, n_fact = _get_legen_table('meg', n_coeff=20, force_calc=True) fun = partial(_get_legen_lut_accurate, lut=lut) coeffs = _comp_sums_meg(beta, ctheta, fun, n_fact, False)
def test_100(self): x, w = leg.leggauss(100) # test orthogonality. Note that the results need to be normalized, # otherwise the huge values that can arise from fast growing # functions like Laguerre can be very confusing. v = leg.legvander(x, 99) vv = np.dot(v.T * w, v) vd = 1/np.sqrt(vv.diagonal()) vv = vd[:,None] * vv * vd assert_almost_equal(vv, np.eye(100)) # check that the integral of 1 is correct tgt = 2.0 assert_almost_equal(w.sum(), tgt)
def approx(X0, Y0, X1, approx_type: ApproxType, dim): """ this method should perform approximation on [-1; 1] interval :param X0: X-values (1 x N0) :param Y0: Y-values (1 x N0) :param X1: approximation points (1 x N1) :param approx_type: 0 - algebraic polynomes (1, x, x^2, ...) 1 - legendre polynomes 2 - harmonic :param dim: dimension :return Y1: approximated Y-values (1 x N1) :return a: vector (1 x dim) of approximation coefficients :return P: (for approx_type 0 and 1) coefficients of approximation polynome P (1 x dim) """ if approx_type is ApproxType.algebraic: Q = np.vander(X0, dim, increasing=True) mat = Q.T @ Q b = Q.T @ Y0 P = np.linalg.solve(mat, b) y = polyval(X1, P) return y, [], P if approx_type is ApproxType.legendre: Q = legvander(X0, dim - 1) mat = Q.T @ Q b = Q.T @ Y0 P = np.linalg.solve(mat, b) y = legval(X1, P) D = [[0 for i in range(dim)] for j in range(dim)] D[0][0] = 1 D[0][1] = 0 D[1][0] = 0 D[1][1] = 1 i = 2 while i < (dim): D[i][0] = -(i - 1) / (i) * D[i - 2][0] j = 0 while j <= i: D[i][j] = (2 * i - 1) / i * D[i - 1][j - 1] - (i - 1) / i * D[i - 2][j] j += 1 i += 1 D = np.array(D) G = P @ D return y, P, G raise Exception(f'approximation of type {approx_type} not supported yet')
def test_legendre_val(): """Test Legendre polynomial (derivative) equivalence """ # check table equiv xs = np.linspace(-1., 1., 1000) n_terms = 100 # True, numpy vals_np = legendre.legvander(xs, n_terms - 1) # Table approximation for fun, nc in zip([_get_legen_lut_fast, _get_legen_lut_accurate], [100, 50]): lut, n_fact = _get_legen_table('eeg', n_coeff=nc, force_calc=True) vals_i = fun(xs, lut) # Need a "1:" here because we omit the first coefficient in our table! assert_allclose(vals_np[:, 1:vals_i.shape[1] + 1], vals_i, rtol=1e-2, atol=5e-3) # Now let's look at our sums ctheta = np.random.rand(20, 30) * 2.0 - 1.0 beta = np.random.rand(20, 30) * 0.8 lut_fun = partial(fun, lut=lut) c1 = _comp_sum_eeg(beta.flatten(), ctheta.flatten(), lut_fun, n_fact) c1.shape = beta.shape # compare to numpy n = np.arange(1, n_terms, dtype=float)[:, np.newaxis, np.newaxis] coeffs = np.zeros((n_terms,) + beta.shape) coeffs[1:] = (np.cumprod([beta] * (n_terms - 1), axis=0) * (2.0 * n + 1.0) * (2.0 * n + 1.0) / n) # can't use tensor=False here b/c it isn't in old numpy c2 = np.empty((20, 30)) for ci1 in range(20): for ci2 in range(30): c2[ci1, ci2] = legendre.legval(ctheta[ci1, ci2], coeffs[:, ci1, ci2]) assert_allclose(c1, c2, 1e-2, 1e-3) # close enough... # compare fast and slow for MEG ctheta = np.random.rand(20 * 30) * 2.0 - 1.0 beta = np.random.rand(20 * 30) * 0.8 lut, n_fact = _get_legen_table('meg', n_coeff=10, force_calc=True) fun = partial(_get_legen_lut_fast, lut=lut) coeffs = _comp_sums_meg(beta, ctheta, fun, n_fact, False) lut, n_fact = _get_legen_table('meg', n_coeff=20, force_calc=True) fun = partial(_get_legen_lut_accurate, lut=lut) coeffs = _comp_sums_meg(beta, ctheta, fun, n_fact, False)
def leginterpol(x_coords, y_coords): deg = len(x_coords) A = np.ndarray((deg, deg)) A_ = np.ndarray((deg, deg)) B = y_coords # print(A) for i in range(deg): A[i] = leg.legval(x_coords, np.ones((i + 1, 1))) A = A.T A_ = leg.legvander(x_coords, deg - 1) # print('A=', A) # print('A_=', A_) # A*coefs = B coefs = np.linalg.solve(A_, y_coords) return coefs
def fitlegendre(wavepts, fluxpts, sigpts, order): from numpy.linalg import svd vander = L.legvander(wavepts, order) design = np.zeros(vander.shape) for i in range(len(wavepts)): design[i] = vander[i] / sigpts[i] U, s, v = svd(design, full_matrices=False, compute_uv=True) V = v.transpose() solvec = np.zeros(order + 1) for i in range(order + 1): solvec += np.dot(U[:, i], fluxpts / sigpts) / s[i] * V[:, i] ### Build covariance matrix covmtx = np.zeros([order + 1, order + 1]) for j in range(order + 1): for k in range(order + 1): covmtx[j, k] = sum(V[:, j] * V[:, k] / s) return solvec, covmtx
def poles(self): orders = 4 xi = self.xi r = self.r mu = self.mu Nr, Nmu = self.xi.shape v = legvander(mu, orders) poles = numpy.empty((len(r), orders+1)) sym = (mu >= 0).all() for i in range(orders + 1): norm = simps(v[:, i] ** 2, mu) if sym and i % 2 == 1: poles[:, i] = 0 else: poles[:, i] = simps(xi * v[:, i][None, :], mu) / norm return poles
def pose_postprocess(self, pose_3d): # focus_time = None time_length = len(pose_3d) t_axis = np.linspace(0, time_length, time_length + 1) # nsamples = self.end - self.start # The independent variable sampled at regular intervals xs = np.linspace(-1, 1, nsamples) # The Legendre Vandermonde matrix V = leg.legvander(xs, self.approx_deg) # Generate some data to fit ys = xs * np.pi # Do the fit for all 17 poses resampledpose = {} for ii in range(self.num_joints): respsoexyz = [] for ij in range(3): jnt_coord = [pose_3d[i][ij, ii] for i in range(time_length)] # coeffs = np.linalg.lstsq(V, jnt_coord[self.start:self.end], rcond=None)[0] # Evaluate the fit for plotting purposes g = leg.legval(xs, coeffs) focus_val, _time = signal.resample(g, len(g) * self.resamp, t_axis[self.start:self.end]) focus_val = list(focus_val) if focus_time == None: focus_time = list(_time) for ti in range(self.start): focus_time.insert(0, ti) # adding the inactive times for ti in range(self.start): focus_val.insert(0, focus_val[0]) respsoexyz.append(focus_val) # resampledpose.update({'joint_' + str(ii): np.array(respsoexyz)}) return resampledpose, focus_time
def detrend_poly(self, npol=20, istart=None, iend=None): flux = self.flux[istart:iend] covs = self.covariates[istart:iend, [2, 4, 5, 6]].copy() covs -= covs.mean(0) covs /= covs.std(0) x = (self.time - self.time[0]) / diff(self.time[[0, -1]]) * 2 - 1 pol = legvander(x, npol) pol[:, 1:] /= pol[:, 1:].ptp(0) covs = concatenate([covs, pol], 1) c, _, _, _ = lstsq(covs, flux, rcond=None) cbl = c.copy() cbl[:4] = 0. css = c.copy() css[4:] = 0. fbl = dot(covs, cbl) fsys = dot(covs, css) fall = dot(covs, c) return self.time[istart:iend], flux - fall + 1, fsys, fbl
def culledLegForwardTransform(orders, locations, functionVals, threshold=None): # inspired by : A Simple Regularization of the Polynomial Interpolation For the Runge Phenomemenon if len(locations.shape)==1: vandermonde=leg.legvander(locations, orders[0]) elif locations.shape[1]==2: vandermonde=leg.legvander2d(locations[:,0], locations[:,1], orders) elif locations.shape[1]==3: vandermonde=leg.legvander3d(locations[:,0],locations[:,1],locations[:,2], orders) elif locations.shape[1]==4: vandermonde=legvander4d(locations,orders) elif locations.shape[1]==5: vandermonde=legvander5d(locations,orders) else: raise NotImplementedError # there's a bad startup joke about this being good enough for the paper. # preconditioner = np.diag((0.94) ** (2* (np.arange(vandermonde.shape[0])))) # vandermonde=np.dot(preconditioner, vandermonde) U,S,Vh=np.linalg.svd(vandermonde) numTake=0 filtS=S if threshold is None: Eps= np.finfo(functionVals.dtype).eps Neps = np.prod(cmn.numpyze(orders)) * Eps * S[0] #truncation due to ill-conditioning Nt = max(np.argmax(Vh, axis=0)) #"Automatic" determination of threshold due to Runge's phenomenon threshold=min(Neps, Nt) while numTake<=0: filter=S>threshold numTake=filter.sum() if numTake>0: filtU=U[:,:numTake]; filtS=S[:numTake]; filtVh=Vh[:numTake, :] else: if threshold>1e-13: threshold=threshold/2 warnings.warn('cutting threshold for eigenvalues to '+str(threshold)) else: warnings('seems all eigenvalues are zero (<1e-13), setting to zero and breaking') filtS=np.zeros_like(S) truncVander=np.dot(filtU,np.dot(np.diag(filtS),filtVh)) ret, _, _, _=npl.lstsq(truncVander, functionVals, rcond=None) return np.reshape(ret, np.array(orders).flatten()+1)
def __init__(self,n,k,a,m): self.k2 = 2*k # Uniform grid points x = np.linspace(-1,1,n) # Legendre Polynomials on grid self.V = legvander(x,m-1) # Do QR factorization of Vandermonde for least squares self.Q,self.R = qr(self.V,mode='economic') I = np.eye(m) D = np.zeros((m,m)) D[:-self.k2,:] = legder(I,self.k2) # Legendre modal approximation of differential operator self.A = I-a*D # Store LU factors for repeated solves self.PLU = lu_factor(self.A[:-self.k2,self.k2:])
def __init__(self, n, k, a, m): self.k2 = 2 * k # Uniform grid points x = np.linspace(-1, 1, n) # Legendre Polynomials on grid self.V = legvander(x, m - 1) # Do QR factorization of Vandermonde for least squares self.Q, self.R = qr(self.V, mode='economic') I = np.eye(m) D = np.zeros((m, m)) D[:-self.k2, :] = legder(I, self.k2) # Legendre modal approximation of differential operator self.A = I - a * D # Store LU factors for repeated solves self.PLU = lu_factor(self.A[:-self.k2, self.k2:])
def __init__(self, name: str, dfile: Path, zero_epoch: float, period: float, nsamples: int = 10, trdur: float = 0.125, bldur: float = 0.3, nlegendre: int = 2, ctx=None, queue=None): tb = Table.read(dfile) self.bjdrefi = tb.meta['BJDREFI'] zero_epoch = zero_epoch - self.bjdrefi df = tb.to_pandas().dropna(subset=['TIME', 'SAP_FLUX', 'PDCSAP_FLUX']) self.lc = lc = KeplerLC(df.TIME.values, df.PDCSAP_FLUX.values, zeros(df.shape[0]), zero_epoch, period, trdur, bldur) self.nlegendre = nlegendre super().__init__(name, zero_epoch, period, ['TESS'], times=lc.time_per_transit, fluxes=lc.normalized_flux_per_transit, pbids=lc.nt * [0], nsamples=nsamples, exptimes=[0.00139], cl_ctx=ctx, cl_queue=queue) self.mtimes = [t - t.mean() for t in self.times] self.windows = window = concatenate(self.mtimes).ptp() self.mtimes = [t / window for t in self.mtimes] self.legs = [legvander(t, self.nlegendre) for t in self.mtimes] self.ofluxa = self.ofluxa.astype('d') self.lnlikelihood = self.lnlikelihood_nb
def getVandermondeLegendre(self): from numpy.polynomial.legendre import legvander return legvander(self.Nodes, self.Order)
import numpy as np import scipy as sp import matplotlib.pyplot as plt import numpy.polynomial.legendre as leg n = 16 def f(x, y): return np.sin(2 * np.pi * x) * np.sin(2 * np.pi * y) def curve(a, b, c, x): return a + b * x + c * x**2 Qx, Qw = leg.leggauss(n) nx, ny = np.meshgrid(Qx, Qx) Le = leg.legvander(Qx, n - 1) #La = sp.interpolate.lagrange(n) Q, R = np.linalg.qr(Le)
def vandermonde(self, x): return leg.legvander(x, 1)
def _get_legen(x, n_coeff=100): """Get Legendre polynomials expanded about x""" return legendre.legvander(x, n_coeff - 1)
def legvander(x, deg) : from numpy.polynomial.legendre import legvander return legvander(x, deg)
def ortholegvander(x, deg): """See numpy.polynomial.legendre.legvander(). Uses an orthonormal basis.""" result = leg.legvander(x, deg) factors = np.array([np.sqrt((2 * n + 1) / 2) for n in range(0, 1 + deg)]) return result * factors
def __init__(self, target: str, photometry: list, tid: int, cids: list, filters: tuple, aperture_lims: tuple = (0, inf), use_opencl: bool = False, n_legendre: int = 0, use_toi_info=True, with_transit=True, with_contamination=False, radius_ratio: str = 'achromatic'): assert radius_ratio in ('chromatic', 'achromatic') self.use_opencl = use_opencl self.planet = None self.photometry_frozen = False self.with_transit = with_transit self.with_contamination = with_contamination self.chromatic_transit = radius_ratio == 'chromatic' self.radius_ratio = radius_ratio self.n_legendre = n_legendre self.toi = None if self.with_transit and 'toi' in target.lower() and use_toi_info: self.toi = get_toi(float(target.lower().strip('toi'))) # Set photometry # -------------- self.phs = photometry self.nph = len(photometry) # Set the aperture ranges # ----------------------- self.min_apt = amin = min(max(aperture_lims[0], 0), photometry[0].flux.aperture.size) self.max_apt = amax = max( min(aperture_lims[1], photometry[0].flux.aperture.size), 0) self.napt = amax - amin # Target and comparison star IDs # ------------------------------ self.tid = atleast_1d(tid) if self.tid.size == 1: self.tid = tile(self.tid, self.nph) self.cids = atleast_2d(cids) if self.cids.shape[0] == 1: self.cids = tile(self.cids, (self.nph, 1)) assert self.tid.size == self.nph assert self.cids.shape[0] == self.nph self.covnames = 'intercept sky airmass xshift yshift entropy'.split() times = [array(ph.bjd) for ph in photometry] fluxes = [ array(ph.flux[:, tid, 1]) for tid, ph in zip(self.tid, photometry) ] fluxes = [f / nanmedian(f) for f in fluxes] self.apertures = ones(len(times)).astype('int') self.t0 = floor(times[0].min()) times = [t - self.t0 for t in times] self._tmin = times[0].min() self._tmax = times[0].max() covariates = [] for ph in photometry: covs = concatenate( [ones([ph._fmask.sum(), 1]), array(ph.aux)[:, [1, 3, 4, 5]]], 1) covariates.append(covs) self.airmasses = [array(ph.aux[:, 2]) for ph in photometry] wns = [ones(ph.nframes) for ph in photometry] if use_opencl: import pyopencl as cl ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) tm = QuadraticModelCL(klims=(0.005, 0.25), nk=512, nz=512, cl_ctx=ctx, cl_queue=queue) else: tm = QuadraticModel(interpolate=True, klims=(0.005, 0.25), nk=512, nz=512) super().__init__(target, filters, times, fluxes, wns, arange(len(photometry)), covariates, arange(len(photometry)), tm=tm) self.legendre = [ legvander((t - t.min()) / (0.5 * t.ptp()) - 1, self.n_legendre)[:, 1:] for t in self.times ] # Create the target and reference star flux arrays # ------------------------------------------------ self.ofluxes = [ array(ph.flux[:, self.tid[i], amin:amax + 1] / ph.flux[:, self.tid[i], amin:amax + 1].median('mjd')) for i, ph in enumerate(photometry) ] self.refs = [] for ip, ph in enumerate(photometry): self.refs.append([ pad(array(ph.flux[:, cid, amin:amax + 1]), ((0, 0), (1, 0)), mode='constant') for cid in self.cids[ip] ]) self.set_orbit_priors()
x = Symbol("x") u = (1.0 - x**2)**2 * cos( np.pi * 4 * x) * (x - 0.25)**3 + b * (1 + x) / 2. + a * (1 - x) / 2. kx = np.sqrt(5) f = -u.diff(x, 2) + kx**2 * u fl = lambdify(x, f, "numpy") ul = lambdify(x, u, "numpy") n = 32 domain = sys.argv[-1] if len(sys.argv) == 2 else "C1" # Chebyshev-Gauss nodes and weights points, w = leg.leggauss(n + 1) # Chebyshev Vandermonde matrix V = leg.legvander(points, n) scl = 1.0 # First derivative matrix zero padded to (n+1)x(n+1) D1 = np.zeros((n + 1, n + 1)) D1[:-1, :] = leg.legder(np.eye(n + 1), 1, scl=scl) Vx = np.dot(V, D1) # Matrix of trial functions P = np.zeros((n + 1, n + 1)) P[:, 0] = (V[:, 0] - V[:, 1]) / 2 P[:, 1:-1] = V[:, :-2] - V[:, 2:] P[:, -1] = (V[:, 0] + V[:, 1]) / 2
def generate_Vandermonde_matrixu(x_coords): """ Returns the pseudo-Vandermonde matrix of degree len(x_coords) and sample points x_coords""" return legndr.legvander(x_coords, len(x_coords) - 1)
def named_target_matrix(name, size): """ Parameter: name: name of the target matrix Return: target_matrix: (n, n) numpy array for real matrices or (n, n, 2) for complex matrices. """ if name == 'dft': return LA.dft(size, scale='sqrtn')[:, :, None].view('float64') elif name == 'idft': return np.ascontiguousarray(LA.dft(size, scale='sqrtn').conj().T)[:, :, None].view('float64') elif name == 'dft2': size_sr = int(math.sqrt(size)) matrix = np.fft.fft2(np.eye(size_sr**2).reshape(-1, size_sr, size_sr), norm='ortho').reshape(-1, size_sr**2) # matrix1d = LA.dft(size_sr, scale='sqrtn') # assert np.allclose(np.kron(m1d, m1d), matrix) # return matrix[:, :, None].view('float64') from butterfly.utils import bitreversal_permutation br_perm = bitreversal_permutation(size_sr) br_perm2 = np.arange(size_sr**2).reshape(size_sr, size_sr)[br_perm][:, br_perm].reshape(-1) matrix = np.ascontiguousarray(matrix[:, br_perm2]) return matrix[:, :, None].view('float64') elif name == 'dct': # Need to transpose as dct acts on rows of matrix np.eye, not columns # return dct(np.eye(size), norm='ortho').T return dct(np.eye(size)).T / math.sqrt(size) elif name == 'dst': return dst(np.eye(size)).T / math.sqrt(size) elif name == 'hadamard': return LA.hadamard(size) / math.sqrt(size) elif name == 'hadamard2': size_sr = int(math.sqrt(size)) matrix1d = LA.hadamard(size_sr) / math.sqrt(size_sr) return np.kron(matrix1d, matrix1d) elif name == 'b2': size_sr = int(math.sqrt(size)) from butterfly import Block2x2DiagProduct b = Block2x2DiagProduct(size_sr) matrix1d = b(torch.eye(size_sr)).t().detach().numpy() return np.kron(matrix1d, matrix1d) elif name == 'convolution': np.random.seed(0) x = np.random.randn(size) return LA.circulant(x) / math.sqrt(size) elif name == 'hartley': return hartley_matrix(size) / math.sqrt(size) elif name == 'haar': return haar_matrix(size, normalized=True) / math.sqrt(size) elif name == 'legendre': grid = np.linspace(-1, 1, size + 2)[1:-1] return legendre.legvander(grid, size - 1).T / math.sqrt(size) elif name == 'hilbert': H = hilbert_matrix(size) return H / np.linalg.norm(H, 2) elif name == 'randn': np.random.seed(0) return np.random.randn(size, size) / math.sqrt(size) elif name == 'permutation': np.random.seed(0) perm = np.random.permutation(size) P = np.eye(size)[perm] return P elif name.startswith('rank-unnorm'): r = int(name[11:]) np.random.seed(0) G = np.random.randn(size, r) H = np.random.randn(size, r) M = G @ H.T # M /= math.sqrt(size*r) return M elif name.startswith('rank'): r = int(name[4:]) np.random.seed(0) G = np.random.randn(size, r) H = np.random.randn(size, r) M = G @ H.T M /= math.sqrt(size*r) return M elif name.startswith('sparse'): s = int(name[6:]) # 2rn parameters np.random.seed(0) mask = sparse.random(size, size, density=s/size, data_rvs=np.ones) M = np.random.randn(size, size) * (mask.toarray()) M /= math.sqrt(s) return M elif name.startswith('toeplitz'): r = int(name[8:]) G = np.random.randn(size, r) / math.sqrt(size*r) H = np.random.randn(size, r) / math.sqrt(size*r) M = toeplitz_like(G, H) return M elif name == 'fastfood': n = size S = np.random.randn(n) G = np.random.randn(n) B = np.random.randn(n) # P = np.arange(n) P = np.random.permutation(n) H = hadamard(n) # SHGPHB # print(H) # print((H*B)[P,:]) # print((H @ (G[:,np.newaxis] * (H * B)[P,:]))) F = S[:,np.newaxis] * (H @ (G[:,np.newaxis] * (H * B)[P,:])) / n return F # x = np.random.randn(batch_size,n) # HB = hadamard_transform(B) # PHBx = HBx[:, P] # HGPHBx = hadamard_transform(G*PHBx) # return S*HGPHBx elif name == 'butterfly': # n (log n+1) params in the hierarchy b = Butterfly(in_size=size, out_size=size, bias=False, tied_weight=False, param='odo', nblocks=0) M = b(torch.eye(size)) return M.cpu().detach().numpy() else: assert False, 'Target matrix name not recognized or implemented'
# Some exact solution x = Symbol("x") u = (1.0-x**2)**2*cos(np.pi*4*x)*(x-0.25)**3 + b*(1 + x)/2. + a*(1 - x)/2. kx = np.sqrt(5) f = -u.diff(x, 2) + kx**2*u fl = lambdify(x, f, "numpy") ul = lambdify(x, u, "numpy") n = 32 domain = sys.argv[-1] if len(sys.argv) == 2 else "C1" # Chebyshev-Gauss nodes and weights points, w = leg.leggauss(n+1) # Chebyshev Vandermonde matrix V = leg.legvander(points, n) scl=1.0 # First derivative matrix zero padded to (n+1)x(n+1) D1 = np.zeros((n+1,n+1)) D1[:-1,:] = leg.legder(np.eye(n+1), 1, scl=scl) Vx = np.dot(V, D1) # Matrix of trial functions P = np.zeros((n+1,n+1)) P[:,0] = (V[:,0] - V[:,1])/2 P[:,1:-1] = V[:,:-2] - V[:,2:] P[:,-1] = (V[:,0] + V[:,1])/2
def setX(self, x): self.x = numpy.copy(x) self.x = xshift(self.x) self.vander = legendre.legvander(self.x, self.order)
print "[INFO] Yp_test.shape (E-DMD) " + repr(Yp_test.shape); print "[INFO] Yf_test.shape (E-DMD) " + repr(Yf_test.shape); Yp_final_train = Yp_train; # straight up quadratics training data 0,...,n-1 Yf_final_train = Yf_train; # straight up quadratics training data 1 ,... n 1 step forward propagation of Yp_train Yp_final_test = Yp_test; #straight up quadratics test data - Yf_final_test = Yf_test; # straight up quadratics test data - 1-step forward propagation of Yp_test # # # - - - - - - Legendre polynomial dictionary generation - - - - - # # # if use_legendre: #print Yp.shape deg_polynomial = poly_deg Yp_leg = legvander(Yp,deg_polynomial) n_points,n_channels,n_deg_polyp1= Yp_leg.shape Yp_leg = np.reshape(Yp_leg,(n_deg_polyp1,n_points,n_channels)); #print Yp_leg.shape Yf_leg = legvander(Yf,deg_polynomial) n_points,n_channels,n_deg_polyp1= Yf_leg.shape Yf_leg = np.reshape(Yf_leg,(n_deg_polyp1,n_points,n_channels)); #print Yf_leg.shape Yp_leg_stack = np.concatenate((Yp_leg[:,:,0],Yp_leg[:,:,1])) ; Yf_leg_stack = np.concatenate((Yf_leg[:,:,0],Yf_leg[:,:,1])) ; for i in range(2,n_channels): if i%1000==0: #print i #print Yp_leg_stack.shape print Yf_leg_stack.shape
def named_target_matrix(name, size): """ Parameter: name: name of the target matrix Return: target_matrix: (n, n) numpy array for real matrices or (n, n, 2) for complex matrices. """ if name == 'dft': return LA.dft(size, scale='sqrtn')[:, :, None].view('float64') elif name == 'idft': return np.ascontiguousarray(LA.dft( size, scale='sqrtn').conj().T)[:, :, None].view('float64') elif name == 'dft2': size_sr = int(math.sqrt(size)) matrix = np.fft.fft2(np.eye(size_sr**2).reshape(-1, size_sr, size_sr), norm='ortho').reshape(-1, size_sr**2) # matrix1d = LA.dft(size_sr, scale='sqrtn') # assert np.allclose(np.kron(m1d, m1d), matrix) # return matrix[:, :, None].view('float64') from butterfly.utils import bitreversal_permutation br_perm = bitreversal_permutation(size_sr) br_perm2 = np.arange(size_sr**2).reshape( size_sr, size_sr)[br_perm][:, br_perm].reshape(-1) matrix = np.ascontiguousarray(matrix[:, br_perm2]) return matrix[:, :, None].view('float64') elif name == 'dct': # Need to transpose as dct acts on rows of matrix np.eye, not columns # return dct(np.eye(size), norm='ortho').T return dct(np.eye(size)).T / math.sqrt(size) elif name == 'dst': return dst(np.eye(size)).T / math.sqrt(size) elif name == 'hadamard': return LA.hadamard(size) / math.sqrt(size) elif name == 'hadamard2': size_sr = int(math.sqrt(size)) matrix1d = LA.hadamard(size_sr) / math.sqrt(size_sr) return np.kron(matrix1d, matrix1d) elif name == 'b2': size_sr = int(math.sqrt(size)) import torch from butterfly import Block2x2DiagProduct b = Block2x2DiagProduct(size_sr) matrix1d = b(torch.eye(size_sr)).t().detach().numpy() return np.kron(matrix1d, matrix1d) elif name == 'convolution': np.random.seed(0) x = np.random.randn(size) return LA.circulant(x) / math.sqrt(size) elif name == 'hartley': return hartley_matrix(size) / math.sqrt(size) elif name == 'haar': return haar_matrix(size, normalized=True) / math.sqrt(size) elif name == 'legendre': grid = np.linspace(-1, 1, size + 2)[1:-1] return legendre.legvander(grid, size - 1).T / math.sqrt(size) elif name == 'hilbert': H = hilbert_matrix(size) return H / np.linalg.norm(H, 2) elif name == 'randn': np.random.seed(0) return np.random.randn(size, size) / math.sqrt(size) else: assert False, 'Target matrix name not recognized or implemented'
def _get_exp_coeffs(self, y: np.ndarray, x: np.ndarray, initial_guess: Optional[np.ndarray] = None ) -> Tuple[np.ndarray, np.ndarray]: r"""Calculate the exponential coefficients As a first step to fitting, calculate the exponential "rate" factors :math:`\lambda_k`. Parameters ---------- y Function values corresponding to `x`. x Independent variable values Returns ------- exp_coeff List of exponential coefficients ode_coeff Optimal coefficients of the ODE involved in calculating the exponential coefficients. Other parameters ---------------- initial_guess An initial guess for determining the parameters of the ODE (if you don't know what this is about, don't bother). The array is 1D and has `n_exp` + 1 entries. If `None`, use ``numpy.ones(n_exp + 1)``. """ if initial_guess is None: initial_guess = np.ones(self.n_exp + 1) s = _ODESolver(self.n_exp, self.poly_order) # Map x to [-1, 1] x_min = np.min(x) x_range = np.ptp(x) x_mapped = 2 * (x - x_min) / x_range - 1 # Weights (trapezoidal) d_x = np.diff(x) w = np.zeros(len(x)) w[0] = d_x[0] w[1:-1] = d_x[1:] + d_x[:-1] w[-1] = d_x[-1] w /= x_range leg = legvander(x_mapped, self.n_exp - 1) def residual(coeffs): s.coefficients = coeffs # The following is the residual condition # \hat{y}_k = (k + 1 / 2) \sum_{i=1}^n w_i z_i P(t_i) rc = leg.T @ (w * y) rc *= np.arange(0.5, self.n_exp) # Solve the ODE in Legendre space # \sum_{k=0}^p a_k D^k \hat{y} = e_1 rhs = np.zeros(self.poly_order) rhs[0] = 1 sol_hat = s.solve(rc, rhs) # transform to real space sol = legval(x_mapped, sol_hat) return y - sol def jacobian(coeffs): s.coefficients = coeffs jac = np.zeros((len(x), self.n_exp + 1)) tan = s.tangent() for i in range(self.n_exp + 1): jac[:, i] = -legval(x_mapped, tan[:, i]) return jac ode_coeff = leastsq(residual, initial_guess, Dfun=jacobian)[0] exp_coeff = np.roots(ode_coeff[::-1]) * 2 / x_range return exp_coeff, ode_coeff
def legvander(x, deg): from numpy.polynomial.legendre import legvander return legvander(x, deg)
def frompoles(self, poles): v = legvander(self.mu, poles.shape[1] - 1) self.xi[...] = numpy.einsum('jl,il->ij', v, poles) if hasattr(self, 'poles'): del self.poles