From e53cc0d11fdab082a1b3548d667a6f7f0dfff0d4 Mon Sep 17 00:00:00 2001 From: Julian Valentin Date: Sun, 1 Apr 2018 17:03:17 +0200 Subject: [PATCH] Add missing basis files --- py/helper/basis/hierarchical_basis.py | 13 ++++++++ ...hierarchical_basis_from_parent_function.py | 20 +++++++++++++ py/helper/basis/hierarchical_bspline.py | 30 +++++++++++++++++++ py/helper/basis/non_uniform_bspline.py | 28 +++++++++++++++++ py/helper/basis/parent_function.py | 13 ++++++++ 5 files changed, 104 insertions(+) create mode 100644 py/helper/basis/hierarchical_basis.py create mode 100644 py/helper/basis/hierarchical_basis_from_parent_function.py create mode 100644 py/helper/basis/hierarchical_bspline.py create mode 100644 py/helper/basis/non_uniform_bspline.py create mode 100644 py/helper/basis/parent_function.py diff --git a/py/helper/basis/hierarchical_basis.py b/py/helper/basis/hierarchical_basis.py new file mode 100644 index 0000000..02d34cb --- /dev/null +++ b/py/helper/basis/hierarchical_basis.py @@ -0,0 +1,13 @@ +#!/usr/bin/python3 + +import abc + +class HierarchicalBasis(abc.ABC): + def __init__(self, nu=0): + self.nu = nu + + @abc.abstractmethod + def evaluate(self, l, i, xx): pass + + @abc.abstractmethod + def getSupport(self, l, i): pass diff --git a/py/helper/basis/hierarchical_basis_from_parent_function.py b/py/helper/basis/hierarchical_basis_from_parent_function.py new file mode 100644 index 0000000..f19f770 --- /dev/null +++ b/py/helper/basis/hierarchical_basis_from_parent_function.py @@ -0,0 +1,20 @@ +#!/usr/bin/python3 + +from .hierarchical_basis import HierarchicalBasis + +class HierarchicalBasisFromParentFunction(HierarchicalBasis): + def __init__(self, parentFunction): + super().__init__(nu=parentFunction.nu) + self.parentFunction = parentFunction + + def evaluate(self, l, i, xx): + tt = 2**l * xx - i + yy = self.parentFunction.evaluate(tt) + yy *= 2**(l*self.nu) + return yy + + def getSupport(self, l, i): + lb, ub = self.parentFunction.getSupport() + lb = max((lb + i) / 2**l, 0) + ub = min((ub + i) / 2**l, 1) + return lb, ub diff --git a/py/helper/basis/hierarchical_bspline.py b/py/helper/basis/hierarchical_bspline.py new file mode 100644 index 0000000..bfeb144 --- /dev/null +++ b/py/helper/basis/hierarchical_bspline.py @@ -0,0 +1,30 @@ +#!/usr/bin/python3 + +import helper.grid + +from .centralized_cardinal_bspline import CentralizedCardinalBSpline +from .hierarchical_basis_from_parent_function import HierarchicalBasisFromParentFunction + +def restrictKnots(p, hInv, i, I): + offset = 0 + + if i < 0: + I = [I[0] - j * (I[1] - I[0]) for j in range(-i, 0, -1)] + I + offset = -i + elif i > hInv: + I = I + [I[-1] + j * (I[-1] - I[-2]) for j in range(1, i-hInv+1)] + + I = I[i+offset:i+offset+p+2] + return I + +class HierarchicalBSpline(HierarchicalBasisFromParentFunction): + def __init__(self, p, nu=0): + super().__init__(CentralizedCardinalBSpline(p, nu=nu)) + self.p = p + + def getKnots(self, l, i=None): + hInv = 2**l + I = list(range(-(self.p+1)//2, hInv + (self.p+1)//2 + 1)) + if i is not None: I = restrictKnots(self.p, hInv, i, I) + xi = helper.grid.getCoordinates(l, I) + return xi diff --git a/py/helper/basis/non_uniform_bspline.py b/py/helper/basis/non_uniform_bspline.py new file mode 100644 index 0000000..a4235cc --- /dev/null +++ b/py/helper/basis/non_uniform_bspline.py @@ -0,0 +1,28 @@ +#!/usr/bin/python3 + +import numpy as np +import scipy.interpolate + +from .parent_function import ParentFunction + +class NonUniformBSpline(ParentFunction): + def __init__(self, p, xi, k=None, nu=0): + if k is None: + super().__init__(nu) + fakeXi = np.hstack([p * [xi[0]], xi, p * [xi[-1]]]) + fakeC = np.zeros((2*p+1,)) + fakeC[p] = 1 + self.bSpline = scipy.interpolate.BSpline(fakeXi, fakeC, p) + self.support = [xi[0], xi[-1]] + else: + self.__init__(p, xi[k:k+p+2], nu=nu) + + def evaluate(self, xx): + xx = np.array(xx).flatten().astype(float) + K = np.logical_and(xx >= self.support[0], xx < self.support[1]) + yy = np.zeros_like(xx) + yy[K] = self.bSpline(xx[K], nu=self.nu) + return yy + + def getSupport(self): + return self.support diff --git a/py/helper/basis/parent_function.py b/py/helper/basis/parent_function.py new file mode 100644 index 0000000..0b6d82c --- /dev/null +++ b/py/helper/basis/parent_function.py @@ -0,0 +1,13 @@ +#!/usr/bin/python3 + +import abc + +class ParentFunction(abc.ABC): + def __init__(self, nu=0): + self.nu = nu + + @abc.abstractmethod + def evaluate(self, xx): pass + + @abc.abstractmethod + def getSupport(self): pass