Ejemplo n.º 1
0
    def test_domain_in_list_bspline(self):
        """Test the evaluation of FDataBasis"""

        for bspline in (BSpline(domain_range=[(0, 1)], nbasis=5, order=3),
                        BSpline(domain_range=((0, 1),), nbasis=5, order=3),
                        BSpline(domain_range=np.array((0, 1)), nbasis=5,
                                order=3),
                        BSpline(domain_range=np.array([(0, 1)]), nbasis=5,
                                order=3)
                        ):

            coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                            [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

            f = FDataBasis(bspline, coefficients)

            t = np.linspace(0, 1, 4)

            res = np.array([[0.001, 0.564, 0.435, 0.33],
                            [0.018, 0.468, 0.371, 0.12]])

            np.testing.assert_array_almost_equal(f(t).round(3), res)
            np.testing.assert_array_almost_equal(f.evaluate(t).round(3), res)

        # Check error
        with np.testing.assert_raises(ValueError):
            BSpline(domain_range=[(0, 1), (0, 1)])
Ejemplo n.º 2
0
 def test_basis_bspline_product(self):
     bspline = BSpline(n_basis=6, order=4)
     bspline2 = BSpline(domain_range=(0, 1), n_basis=6,
                        order=4, knots=[0, 0.3, 1 / 3, 1])
     prod = BSpline(domain_range=(0, 1), n_basis=10, order=7,
                    knots=[0, 0.3, 1 / 3, 2 / 3, 1])
     self.assertEqual(bspline.basis_of_product(bspline2), prod)
Ejemplo n.º 3
0
 def test_bspline_penalty(self):
     basis = BSpline(nbasis=5)
     np.testing.assert_array_almost_equal(
         basis.penalty(2).round(2),
         np.array([[96., -132., 24., 12., 0.],
                   [-132., 192., -48., -24., 12.],
                   [24., -48., 48., -48., 24.],
                   [12., -24., -48., 192., -132.],
                   [0., 12., 24., -132., 96.]]))
Ejemplo n.º 4
0
 def test_bspline_penalty_numerical(self):
     basis = BSpline(n_basis=5)
     np.testing.assert_array_almost_equal(
         basis.penalty(coefficients=[0, 0, 1]).round(2),
         np.array([[96., -132., 24., 12.,
                    0.], [-132., 192., -48., -24., 12.],
                   [24., -48., 48., -48., 24.],
                   [12., -24., -48., 192., -132.],
                   [0., 12., 24., -132., 96.]]))
Ejemplo n.º 5
0
 def test_bspline_penalty_special_case(self):
     basis = BSpline(n_basis=5)
     np.testing.assert_array_almost_equal(
         basis.penalty(basis.order - 1),
         np.array([[1152., -2016., 1152., -288., 0.],
                   [-2016., 3600., -2304., 1008., -288.],
                   [1152., -2304., 2304., -2304., 1152.],
                   [-288., 1008., -2304., 3600., -2016.],
                   [0., -288., 1152., -2016., 1152.]]))
Ejemplo n.º 6
0
 def test_basis_constant_product(self):
     constant = Constant()
     monomial = Monomial()
     fourier = Fourier()
     bspline = BSpline(n_basis=5, order=3)
     self.assertEqual(constant.basis_of_product(monomial), monomial)
     self.assertEqual(constant.basis_of_product(fourier), fourier)
     self.assertEqual(constant.basis_of_product(bspline), bspline)
     self.assertEqual(monomial.basis_of_product(constant), monomial)
     self.assertEqual(fourier.basis_of_product(constant), fourier)
     self.assertEqual(bspline.basis_of_product(constant), bspline)
Ejemplo n.º 7
0
    def test_fdatabasis_times_fdatabasis_fdatabasis(self):
        monomial = FDataBasis(Monomial(nbasis=3), [1, 2, 3])
        bspline = FDataBasis(BSpline(nbasis=6, order=4), [1, 2, 4, 1, 0, 1])
        times_fdar = monomial.times(bspline)

        prod_basis = BSpline(nbasis=9, order=6, knots=[0, 0.25, 0.5, 0.75, 1])
        prod_coefs = np.array([[0.9788352,  1.6289955,  2.7004969,  6.2678739,
                      8.7636441,  4.0069960,  0.7126961,  2.8826708,
                      6.0052311]])

        self.assertEqual(prod_basis, times_fdar.basis)
        np.testing.assert_array_almost_equal(prod_coefs, times_fdar.coefficients)
Ejemplo n.º 8
0
 def test_basis_basis_inprod(self):
     monomial = Monomial(n_basis=4)
     bspline = BSpline(n_basis=5, order=4)
     np.testing.assert_array_almost_equal(
         monomial.inner_product(bspline).round(3),
         np.array(
             [[0.12499983, 0.25000035, 0.24999965, 0.25000035, 0.12499983],
              [0.01249991, 0.07500017, 0.12499983, 0.17500017, 0.11249991],
              [0.00208338, 0.02916658, 0.07083342, 0.12916658, 0.10208338],
              [0.00044654, 0.01339264, 0.04375022, 0.09910693,
               0.09330368]]).round(3))
     np.testing.assert_array_almost_equal(monomial.inner_product(bspline),
                                          bspline.inner_product(monomial).T)
Ejemplo n.º 9
0
def bspline(fd, n_basis, order):
    from skfda.representation.basis import BSpline

    basis = BSpline(n_basis=n_basis, order=order)
    basis_fd = fd.to_basis(basis)

    return basis_fd
Ejemplo n.º 10
0
    def test_evaluation_composed_keepdims_bspline(self):
        """Test behaviour of keepdims with composed evaluation"""
        bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)
        f_keepdims = FDataBasis(bspline, coefficients, keepdims=True)

        t = [[0, 0.5, 0.6], [0.2, 0.7, 0.1]]

        res = np.array([[0.001, 0.57, 0.506], [0.524, 0.399, 0.359]])

        res_keepdims = res.reshape((2, 3, 1))

        # Case default behaviour keepdims=False
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False, keepdims=True).round(3),
            res_keepdims)

        # Case default behaviour keepdims=True
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False).round(3), res_keepdims)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False, keepdims=False).round(3),
            res)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False, keepdims=True).round(3),
            res_keepdims)
Ejemplo n.º 11
0
    def test_evaluation_keepdims_bspline(self):
        """Test behaviour of keepdims """
        bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)
        f_keepdims = FDataBasis(bspline, coefficients, keepdims=True)

        np.testing.assert_equal(f.keepdims, False)
        np.testing.assert_equal(f_keepdims.keepdims, True)

        t = np.linspace(0, 1, 4)

        res = np.array([[0.001, 0.564, 0.435, 0.33],
                        [0.018, 0.468, 0.371, 0.12]])

        res_keepdims = res.reshape((2, 4, 1))

        # Case default behaviour keepdims=False
        np.testing.assert_array_almost_equal(f(t).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, keepdims=True).round(3), res_keepdims)

        # Case default behaviour keepdims=True
        np.testing.assert_array_almost_equal(
            f_keepdims(t).round(3), res_keepdims)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, keepdims=True).round(3), res_keepdims)
Ejemplo n.º 12
0
 def test_basis_gram_matrix(self):
     np.testing.assert_array_almost_equal(
         Monomial(n_basis=3).gram_matrix(),
         [[1, 1 / 2, 1 / 3], [1 / 2, 1 / 3, 1 / 4], [1 / 3, 1 / 4, 1 / 5]])
     np.testing.assert_almost_equal(
         Fourier(n_basis=3).gram_matrix(), np.identity(3))
     np.testing.assert_almost_equal(
         BSpline(n_basis=6).gram_matrix().round(4),
         np.array([[
             4.760e-02, 2.920e-02, 6.200e-03, 4.000e-04, 0.000e+00,
             0.000e+00
         ],
                   [
                       2.920e-02, 7.380e-02, 5.210e-02, 1.150e-02,
                       1.000e-04, 0.000e+00
                   ],
                   [
                       6.200e-03, 5.210e-02, 1.090e-01, 7.100e-02,
                       1.150e-02, 4.000e-04
                   ],
                   [
                       4.000e-04, 1.150e-02, 7.100e-02, 1.090e-01,
                       5.210e-02, 6.200e-03
                   ],
                   [
                       0.000e+00, 1.000e-04, 1.150e-02, 5.210e-02,
                       7.380e-02, 2.920e-02
                   ],
                   [
                       0.000e+00, 0.000e+00, 4.000e-04, 6.200e-03,
                       2.920e-02, 4.760e-02
                   ]]))
Ejemplo n.º 13
0
    def test_evaluation_composed_bspline(self):
        """Test the evaluation of FDataBasis the a matrix of times instead of
        a list of times """
        bspline = BSpline(domain_range=(0, 1), nbasis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)
        t = np.linspace(0, 1, 4)

        res_test = f(t)

        # Test same result than evaluation standart
        np.testing.assert_array_almost_equal(f([1]),
                                             f([[1], [1]],
                                               aligned_evaluation=False))
        np.testing.assert_array_almost_equal(f(t), f(np.vstack((t, t)),
                                                     aligned_evaluation=False))

        # Different evaluation times
        t_multiple = [[0, 0.5], [0.2, 0.7]]
        np.testing.assert_array_almost_equal(f(t_multiple[0])[0],
                                             f(t_multiple,
                                               aligned_evaluation=False)[0])
        np.testing.assert_array_almost_equal(f(t_multiple[1])[1],
                                             f(t_multiple,
                                               aligned_evaluation=False)[1])
Ejemplo n.º 14
0
    def test_comutativity_inprod(self):
        monomial = Monomial(n_basis=4)
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_array_almost_equal(
            bsplinefd.inner_product(monomial).round(3),
            np.transpose(monomial.inner_product(bsplinefd).round(3)))
Ejemplo n.º 15
0
    def test_comutativity_inprod(self):
        monomial = Monomial(n_basis=4)
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_allclose(
            inner_product_matrix(bsplinefd, monomial),
            np.transpose(inner_product_matrix(monomial, bsplinefd)))
Ejemplo n.º 16
0
 def test_from_data_qr(self):
     t = np.linspace(0, 1, 5)
     x = np.sin(2 * np.pi * t) + np.cos(2 * np.pi * t)
     basis = BSpline((0, 1), n_basis=5)
     np.testing.assert_array_almost_equal(
         FDataBasis.from_data(x, t, basis,
                              method='qr').coefficients.round(2),
         np.array([[1., 2.78, -3., -0.78, 1.]]))
Ejemplo n.º 17
0
    def test_basis_fdatabasis_inprod(self):
        monomial = Monomial(n_basis=4)
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_array_almost_equal(
            monomial.inner_product(bsplinefd).round(3),
            np.array([[2., 7., 12.], [1.29626206, 3.79626206, 6.29626206],
                      [0.96292873, 2.62959539, 4.29626206],
                      [0.7682873, 2.0182873, 3.2682873]]).round(3))
Ejemplo n.º 18
0
 def test_from_data_qr(self):
     t = np.linspace(0, 1, 5)
     x = np.sin(2 * np.pi * t) + np.cos(2 * np.pi * t)
     basis = BSpline((0, 1), nbasis=5)
     np.testing.assert_array_almost_equal(
         FDataBasis.from_data(x, t, basis, smoothness_parameter=10,
                              penalty_degree=2, method='qr'
                              ).coefficients.round(2),
         np.array([[0.60, 0.47, 0.20, -0.07, -0.20]])
     )
Ejemplo n.º 19
0
    def test_basis_fdatabasis_inprod(self):
        monomial = Monomial(n_basis=4)
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_allclose(
            inner_product_matrix(monomial, bsplinefd),
            np.array([[2., 7., 12.],
                      [1.29626206, 3.79626206, 6.29626206],
                      [0.96292873, 2.62959539, 4.29626206],
                      [0.7682873, 2.0182873, 3.2682873]]), rtol=1e-4)
Ejemplo n.º 20
0
    def test_basis_fourier_product(self):
        # Test when periods are the same
        fourier = Fourier(n_basis=5)
        fourier2 = Fourier(n_basis=3)
        prod = Fourier(n_basis=7)
        self.assertEqual(fourier.basis_of_product(fourier2), prod)

        # Test when periods are different
        fourier2 = Fourier(n_basis=3, period=2)
        prod = BSpline(n_basis=9, order=8)
        self.assertEqual(fourier.basis_of_product(fourier2), prod)
Ejemplo n.º 21
0
    def test_bspline_penalty(self):
        basis = BSpline(n_basis=5)

        res = np.array([[96., -132., 24., 12., 0.],
                        [-132., 192., -48., -24., 12.],
                        [24., -48., 48., -48., 24.],
                        [12., -24., -48., 192., -132.],
                        [0., 12., 24., -132., 96.]])

        self._test_penalty(basis, linear_diff_op=2, result=res)

        basis = BSpline(n_basis=9, domain_range=(1, 5))
        self._test_penalty(basis, linear_diff_op=[1, 2, 3])
        self._test_penalty(basis, linear_diff_op=[2, 3, 0.1, 1])
        self._test_penalty(basis, linear_diff_op=0)
        self._test_penalty(basis, linear_diff_op=1)
        self._test_penalty(basis, linear_diff_op=3)
        self._test_penalty(basis, linear_diff_op=4)

        basis = BSpline(n_basis=16, order=8)
        self._test_penalty(basis, linear_diff_op=0, atol=1e-7)
Ejemplo n.º 22
0
 def __init__(self,):
 # model_Alpha is the NN to estimate B-splines basis coefficients
 # Nx, Ny, Nt are the dimensions along X, Y and T
 # Nfbx, Nfby, Nfbt are the number of B-splines basis along X, Y and T
     super(Encoder, self).__init__()
     self.model_Alpha = BsCNN(DimAE,shapeData,shapeBsBasis)
     self.Nx      = shapeData[2]
     self.Ny      = shapeData[1]
     self.Nt      = shapeData[0]
     self.Nfbx    = shapeBsBasis[2]
     self.Nfby    = shapeBsBasis[1]
     self.Nfbt    = shapeBsBasis[0]
     # 3D B-splines basis functions
     self.bspl_basis_alongX = BSpline(domain_range=[-1,self.Nx+1], n_basis=self.Nfbx, order=3)
     self.bspl_basis_alongY = BSpline(domain_range=[-1,self.Ny+1], n_basis=self.Nfby, order=3)
     # time domain of definition: [t-Ntdt,t]
     self.bspl_basis_alongT = BSpline(domain_range=[-1*self.Nt,1], n_basis=self.Nfbt, order=3)
     # define 3*3 identity kernel
     self.ID_kernel=torch.tensor([[0., 0., 0.],
                               [0., 1., 0.],
                               [0., 0., 0.]])
Ejemplo n.º 23
0
    def test_fdatabasis_derivative_bspline(self):
        bspline = FDataBasis(BSpline(n_basis=8), [1, 5, 8, 9, 7, 8, 4, 5])
        bspline2 = FDataBasis(
            BSpline(n_basis=5),
            [[4, 9, 7, 4, 3], [1, 7, 9, 8, 5], [4, 6, 6, 6, 8]])

        bs0 = bspline.derivative(order=0)
        bs1 = bspline.derivative()
        bs2 = bspline.derivative(order=2)
        np.testing.assert_equal(bs1.basis, BSpline(n_basis=7, order=3))
        np.testing.assert_almost_equal(
            bs1.coefficients, np.atleast_2d([60, 22.5, 5, -10, 5, -30, 15]))
        np.testing.assert_equal(bs0, bspline)
        np.testing.assert_equal(bs2.basis, BSpline(n_basis=6, order=2))
        np.testing.assert_almost_equal(
            bs2.coefficients, np.atleast_2d([-375, -87.5, -75, 75, -175, 450]))

        bs0 = bspline2.derivative(order=0)
        bs1 = bspline2.derivative()
        bs2 = bspline2.derivative(order=2)

        np.testing.assert_equal(bs1.basis, BSpline(n_basis=4, order=3))
        np.testing.assert_almost_equal(
            bs1.coefficients,
            [[30, -6, -9, -6], [36, 6, -3, -18], [12, 0, 0, 12]])
        np.testing.assert_equal(bs0, bspline2)
        np.testing.assert_equal(bs2.basis, BSpline(n_basis=3, order=2))
        np.testing.assert_almost_equal(
            bs2.coefficients, [[-144, -6, 12], [-120, -18, -60], [-48, 0, 48]])
Ejemplo n.º 24
0
    def test_basis_gram_matrix_bspline(self):

        basis = BSpline(n_basis=6)
        gram_matrix = basis.gram_matrix()
        gram_matrix_numerical = basis._gram_matrix_numerical()
        gram_matrix_res = np.array(
            [[0.04761905, 0.02916667, 0.00615079, 0.00039683, 0., 0.],
             [0.02916667, 0.07380952, 0.05208333, 0.01145833, 0.00014881, 0.],
             [
                 0.00615079, 0.05208333, 0.10892857, 0.07098214, 0.01145833,
                 0.00039683
             ],
             [
                 0.00039683, 0.01145833, 0.07098214, 0.10892857, 0.05208333,
                 0.00615079
             ],
             [0., 0.00014881, 0.01145833, 0.05208333, 0.07380952, 0.02916667],
             [0., 0., 0.00039683, 0.00615079, 0.02916667, 0.04761905]])

        np.testing.assert_allclose(gram_matrix, gram_matrix_res, rtol=1e-4)
        np.testing.assert_allclose(gram_matrix_numerical,
                                   gram_matrix_res,
                                   rtol=1e-4)
Ejemplo n.º 25
0
 def test_qr(self):
     t = np.linspace(0, 1, 5)
     x = np.sin(2 * np.pi * t) + np.cos(2 * np.pi * t)
     basis = BSpline((0, 1), n_basis=5)
     fd = FDataGrid(data_matrix=x, sample_points=t)
     smoother = smoothing.BasisSmoother(basis=basis,
                                        smoothing_parameter=10,
                                        penalty=2,
                                        method='qr',
                                        return_basis=True)
     fd_basis = smoother.fit_transform(fd)
     np.testing.assert_array_almost_equal(
         fd_basis.coefficients.round(2),
         np.array([[0.60, 0.47, 0.20, -0.07, -0.20]]))
Ejemplo n.º 26
0
    def data_to_basis(self, X, fit_fPCA=True):
        """Project the data to basis functions.

        Parameters
        ----------
        X: array, shape (n,n_points,d)
            Array of paths. It is a 3-dimensional array, containing the coordinates in R^d of n piecewise linear paths,
            each composed of n_points.

        fit_fPCA: boolean, default=True
            If n_basis='fPCA' and fit_fPCA=True, the basis functions are fitted to be the functional principal
            components of X.

        Returns
        -------
        fd_basis: object
            Instance of skfda.representation.basis.FDataBasis, the basis representation of X, where the type of basis is
            determined by self.n_basis.
        """
        grid_points = np.linspace(0, 1, X.shape[1])
        fd = FDataGrid(X, grid_points)
        basis_vec = []
        for i in range(X.shape[2]):
            if self.basis_type == 'bspline':
                basis_vec.append(BSpline(n_basis=self.nbasis))
            elif self.basis_type == 'fourier':
                basis_vec.append(Fourier(n_basis=self.nbasis))
            elif self.basis_type == 'fPCA':
                basis_vec.append(BSpline(n_basis=7))

        basis = VectorValued(basis_vec)
        fd_basis = fd.to_basis(basis)
        if self.basis_type == 'fPCA':
            if fit_fPCA:
                self.fpca_basis = self.fpca_basis.fit(fd_basis)
            fd_basis = self.fpca_basis.transform(fd_basis)
        return fd_basis
Ejemplo n.º 27
0
    def test_evaluation_derivative_bspline(self):
        """Test the evaluation of the derivative of a FDataBasis"""
        bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)

        t = np.linspace(0, 1, 4)

        np.testing.assert_array_almost_equal(
            f(t, derivative=1).round(3),
            np.array([[2.927, 0.453, -1.229, 0.6], [4.3, -1.599, 1.016,
                                                    -2.52]]))
Ejemplo n.º 28
0
    def test_bspline_penalty_special_case(self):
        basis = BSpline(n_basis=5)

        res = np.array([[1152., -2016., 1152., -288., 0.],
                        [-2016., 3600., -2304., 1008., -288.],
                        [1152., -2304., 2304., -2304., 1152.],
                        [-288., 1008., -2304., 3600., -2016.],
                        [0., -288., 1152., -2016., 1152.]])

        operator = LinearDifferentialOperator(basis.order - 1)
        penalty = gramian_matrix(operator, basis)
        numerical_penalty = gramian_matrix_numerical(operator, basis)

        np.testing.assert_allclose(penalty, res)

        np.testing.assert_allclose(numerical_penalty, res)
Ejemplo n.º 29
0
    def test_evaluation_simple_bspline(self):
        """Test the evaluation of FDataBasis"""
        bspline = BSpline(domain_range=(0, 1), nbasis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)

        t = np.linspace(0, 1, 4)

        res = np.array([[0.001, 0.564, 0.435, 0.33],
                        [0.018, 0.468, 0.371, 0.12]])

        np.testing.assert_array_almost_equal(f(t).round(3), res)
        np.testing.assert_array_almost_equal(f.evaluate(t).round(3), res)
Ejemplo n.º 30
0
    def test_evaluation_point_bspline(self):
        """Test the evaluation of a single point FDataBasis"""
        bspline = BSpline(domain_range=(0, 1), nbasis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)

        # Test different ways of call f with a point
        res = np.array([[0.5696], [0.3104]])

        np.testing.assert_array_almost_equal(f([0.5]).round(4), res)
        np.testing.assert_array_almost_equal(f((0.5,)).round(4), res)
        np.testing.assert_array_almost_equal(f(0.5).round(4), res)
        np.testing.assert_array_almost_equal(f(np.array([0.5])).round(4), res)