#!/usr/bin/env python3
import numpy
import matplotlib.pyplot as plt


STEPS = 1


def gen_polynom(x, coefficients, derivativ=False, integral=False):
    if derivativ is True:
        n = len(coefficients) - 1
        deriv_coefficients = coefficients

        for index, coefficient in enumerate(coefficients):
            deriv_coefficients[index] = coefficient * n
            n -= 1

        coefficients = deriv_coefficients
        n = len(coefficients) - 2
    elif integral is True:
        n = len(coefficients) - 1
        integral_coefficients = coefficients

        for index, coefficient in enumerate(coefficients):
            integral_coefficients[index] = coefficient * (1 / (n + 1))
            n -= 1

        coefficients = integral_coefficients
        n = len(coefficients)
    else:
        n = len(coefficients) - 1

    y = 0
    for coefficient in coefficients:
        if n < 0:
            break
        elif n == 0:
            y += coefficient * x
        else:
            y += coefficient * (x ** n)
        n -= 1
    return y


def calc_func(vector, xmin, xmax, derivativ=False, integral=False):
    x = numpy.arange(xmin, xmax, STEPS)
    y = gen_polynom(x, vector)

    plt.plot(x, y, label="f(x)")

    if derivativ is True:
        y = gen_polynom(x, vector, derivativ=derivativ)
        plt.plot(x, y, label="f'(x)")

    if integral is True:
        y = gen_polynom(x, vector, integral=integral)
        plt.plot(x, y, label="F(x)")


def check_values(vector, xmin, xmax):
    if not vector or not xmin or not xmax:
        raise Exception("Fehler")

    if len(numpy.shape(vector)) > 1:
        raise Exception("Fehler")


if __name__ == "__main__":
    vector = [2, 1, 3]
    xmin = -5
    xmax = 5

    check_values(vector, xmin, xmax)
    calc_func(vector, xmin, xmax)

    plt.grid()
    plt.xlabel("x")
    plt.ylabel("y")
    plt.legend()
    plt.show()