summaryrefslogtreecommitdiff
path: root/basis.py
blob: 759ebfeb52fc19d55adef188cdfba4bdf6dbdf21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import abc
import math
import numpy as np

class ExpansionBasis1D(object):
    """
    A family of one-dimensional functions that make up a basis.
    """

    __metaclass__ = abc.ABCMeta

    @abc.abstractmethod
    def eval(self, idx, coord, diff_order = 0):
        """
        Evaluate the diff_order-th derivative of the idx-th basis function at
        the specified coordinates (where zeroth derivative means evaluating the
        function itself).
        """
        pass

    @abc.abstractmethod
    def colloc_grid(self, order):
        """
        Get the coordinates of the optimal collocation grid of the specified
        order (i.e. exactly order coordinates will be returned).
        """
        pass

class CosBasis(ExpansionBasis1D):
    PARITY_NONE = 0
    PARITY_EVEN = 1
    PARITY_ODD  = 2

    _diff_fact = [(1, np.cos), (-1, np.sin), (-1, np.cos), (1, np.sin)]
    _parity = None

    def __init__(self, parity = PARITY_NONE):
        self._parity = parity

    def eval(self, idx, coord, diff_order = 0):
        fact, f = self._diff_fact[diff_order % 4]

        if self._parity == self.PARITY_EVEN:
            idx *= 2
        elif self._parity == self.PARITY_ODD:
            idx = 2 * idx + 1

        fact *= idx ** diff_order
        return fact * f(idx * coord)

    def colloc_grid(self, order):
        if self._parity == self.PARITY_NONE:
            return (np.array(range(0, order)) + 1) * 2 * np.pi / (order + 1)
        else:
            return (np.array(range(0, order)) + 1) * np.pi / (2 * (order + 1))