def test_shift_without_domain_shift(): f = Function(np.linspace(0, 10, 100), lambda x: 5 * x) shift = 2 fshifted = Function(f.get_domain(), lambda x: 5 * (x - shift)) f = f.shift(shift) for x in fshifted.get_domain(): assert abs(fshifted(x) - f(x)) == 0
def plot(self, name, f: Function, **kwargs): self.axs.plot(f.get_domain(), f.eval(), label=name, **self._plot_args, **kwargs) self.axs.set_xlabel("depth [$\AA$]") self.axs.set_ylabel(self._ylabel) self.axs.legend() return self
def test_subtraction(): f1 = Function(np.linspace(0, 10, 100), lambda x: 5 * x + 1) f2 = Function(np.linspace(0, 20, 100), lambda x: 4 * x) f3 = Function(np.linspace(0, 10, 100), lambda x: x + 1) f = f1 - f2 assert np.all(f.get_domain() == f1.get_domain()) assert_equal(f, f3)
def test_absolute_value(): f1 = Function(np.linspace(0, 10, 100), lambda x: np.exp(1j * x)) f2 = Function(np.linspace(0, 10, 100), lambda x: 1) f3 = Function(f1.get_domain(), lambda x: -x) f4 = Function(f1.get_domain(), lambda x: x) assert_equal(f1.abs(), f2) assert_equal(f3.abs(), f4)
def test_shift_with_domain_shift(): f = Function(np.linspace(0, 10, 100), lambda x: 5 * x) shift = 10 fshifted = Function(np.linspace(10, 20, 100), lambda x: 5 * (x - shift)) f = f.shift(shift, domain=True) for x in fshifted.get_domain(): assert abs(fshifted(x) - f(x)) == 0
def function_to_sin(fctn: Function): psi = fctn.get_domain() theta = fctn.eval() sinsqrd = np.fromiter(map(lambda x: np.sin(np.deg2rad(x)) ** 2, psi), float) dy = fctn.dy if dy is not None: dy = Function.to_function(sinsqrd, dy.eval()) return Function.to_function(sinsqrd, theta, dy=dy)
def test_addition(): f1 = Function(np.linspace(0, 10, 100), lambda x: np.sin(x)**2) f2 = Function(np.linspace(0, 20, 100), lambda x: np.cos(x)**2) f3 = Function(np.linspace(0, 10, 100), lambda x: 1) f = f1 + f2 assert np.all(f.get_domain() == f1.get_domain()) assert_equal(f, f3)
def plot(self, name, f: Function, **kwargs): if f is None: return transform = self.transform() dy = f.dy if self.scale: f = f.transform(transform) if dy is not None: dy = dy.transform(transform) ylabel = "(100 q$)^2$ $R(q)$ [$10^{-4} \AA^{-2}$]" else: ylabel = "R(q) [1]" feval = f.eval() color = [None, None] if "color" in kwargs: if not isinstance(kwargs["color"], list) or len(kwargs["color"]) <= 1: color = [kwargs["color"], kwargs["color"]] kwargs.pop("color") else: color = kwargs.pop("color") if self.real: if name is not None: kwargs["label"] = "Re R(q) {}".format(name) self._do_plot(f.get_domain(), feval.real, color[0], dy, **kwargs) if self.imag: if name is not None: kwargs["label"] = "Im R(q) {}".format(name) self._do_plot(f.get_domain(), feval.imag, color[1], dy, imag=True, **kwargs) self.axs.set_xlabel("q [$\AA$]") self.axs.set_ylabel(ylabel) self.axs.legend() return self
def plot(self, name, f: Function, f0: Function, f1: Function = None, **kwargs): x = f.get_domain() y = f.eval() kwargs['label'] = name if 'color' in kwargs: self._plot_args['color'] = kwargs['color'] self._axs.fill_between(x, y, f0(x), **self._plot_args) if f1 is not None: self._axs.fill_between(x, y, f1(x), **self._plot_args) super(FillBetweenPlotter, self).plot(name, f, **kwargs) return self
def __init__(self, f: Function, x_error: Function = None, y_error: Function = None): self._f = f # error in x and y domain = f.get_domain() if x_error is None: x_error = NullFunction(domain) if y_error is None: y_error = NullFunction(domain) self._ex = x_error self._ey = y_error
def Lp(f: Function, p=2, domain=None): r""" Computes the Lp-norm of a function. The Lp-Norm of a function is defined as ::math.. (\int |f(x)|^{p} dx)^(1/p) for any :math:`1 \leq p \le \infty` and any function f which p-th power is lebesgue integrable :param f: :param p: :param domain: :return: """ assert 1 <= p if domain is None: domain = f.get_domain() return Integral.integrate((f.abs()) ** p) ** (1 / p)
def plot(self, name, f: Function, **kwargs): x = f.get_domain() y = f.eval() plot_function = self._axs.plot kwargs['label'] = name if f.dx is not None or f.dy is not None: if f.dx is not None and self.plot_dx is True: kwargs['xerr'] = f.dx.eval() if f.dy is not None and self.plot_dy is True: kwargs['yerr'] = f.dy.eval() plot_function = self._axs.errorbar plot_function(x, y, **self._plot_args, **kwargs) return self
def from_function(cls, x_domain, fun: Function): return cls.to_function(fun.get_domain(), fun.get_function(), x_domain)
def from_function(cls, fun: Function, **kwargs): domain = fun.get_domain() conv = numpy.convolve(fun(domain), cls.get_kernel(fun, **kwargs), mode='same') return Function.to_function(domain, conv)
def sup(f: Function, domain=None): if domain is None: domain = f.get_domain() return f(domain).max()