Beispiel #1
0
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
Beispiel #2
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)
Beispiel #5
0
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)
Beispiel #8
0
    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
Beispiel #9
0
    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
Beispiel #10
0
    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
Beispiel #11
0
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)
Beispiel #12
0
    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
Beispiel #13
0
 def from_function(cls, x_domain, fun: Function):
     return cls.to_function(fun.get_domain(), fun.get_function(), x_domain)
Beispiel #14
0
 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)
Beispiel #15
0
def sup(f: Function, domain=None):
    if domain is None:
        domain = f.get_domain()

    return f(domain).max()