<commit_before>#!/usr/bin/env python
# -*- encoding: utf-8 -*-

""" Optimal trajectory builder (<NAME>'s thesis work)."""

# imports go here

__author__ = "<NAME>"
__copyright__ = "Copyright (c) 2015 <NAME>"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"



if __name__ == '__main__':
    pass<commit_msg>Draft for a spline builder<commit_after>#!/usr/bin/env python
# -*- encoding: utf-8 -*-

""" Optimal trajectory builder (<NAME>'s thesis work)."""

from copy import deepcopy
from bisect import bisect_left
import numpy as np
import matplotlib.pyplot as plt

__author__ = "<NAME>"
__copyright__ = "Copyright (c) 2015 <NAME>"
__credits__ = ["<NAME>"]
__license__ = "MIT"
__version__ = "1.0.0"
__maintainer__ = "<NAME>"
__email__ = "<EMAIL>"
__status__ = "Development"

class SplineBuilder:
    """Spline builder class.
    This class helps to build a cubic spline based on finite set of points,
    function values in these points, and boundary conditions of first type
    given on left and right sides of the interval."""

    def __init__(self):
        # Amount of points
        self._n = 0
        # Coordinates and function values
        self._x = []
        self._y = []
        # Cubic spline parameters
        self._a = []
        self._b = []
        self._c = []
        self._d = []

    def build(self, x, y, der_left = 0, der_right = 0):
        """Builds a spline."""

        if len(x) != len(y):
            raise ValueError("x and y have to be of the same size.")
        if len(x) < 2:
            raise ValueError("x must be at least of size 2.")

        self._n = len(x)
        self._x = deepcopy(x)
        self._y = deepcopy(y)

        # TODO: implement a, b, c, d computation

    def f(self, x):
        """Calculates the value of a spline approximation in a given point."""

        if self._n == 0:
            raise Exception("Spline not built yet.")
        if x < self._x[0] or self._x[-1] <= x:
            raise ValueError("Given point is out of interval.")

        index = bisect_left(self._x, x)
        x0 = self._x[index]
        a = self._a[index]
        b = self._b[index]
        c = self._c[index]
        d = self._d[index]
        value = a + b * (x - x0) + c * (x - x0) ** 2 + d * (x - x0) ** 3

        return value



if __name__ == '__main__':
    x = np.linspace(0, 2 * np.pi, 95)
    y = np.sin(x)

    subx = [x[i] for i in range(len(x)) if i % 10 == 0 and i > 0]
    sb = SplineBuilder()
    sb.build(x, y)
    z = [sb.f(point) for point in subx]


    plt.plot(x, y, subx, z)
    plt.show()

































{'fraction_non_alphanumeric': 0.13997760358342665, 'fraction_numerical': 0.014184397163120567, 'mean_word_length': 2.2289156626506026, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '990849', 'n_tokens_mistral': 941, 'n_tokens_neox': 886, 'n_words': 294}