File size: 2,017 Bytes
79402cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations
from src.main import Derivative
import numpy as np
import plotly.express as px
from varname import nameof


class Piecewise():
    def __init__(self, func_1: str, func_2: str, point: float, signs = ["<", ">="], start_stop_num: List[int, int, int] = [-10, 10, 200]):
        start, stop, num = start_stop_num
        x_values = np.linspace(start= start, stop= stop, num = num)
        left_x_values_range =  f"{nameof(x_values)} {signs[0]} {str(point)}"
        right_x_values_range =  f"{nameof(x_values)} {signs[1]} {str(point)}"
        left_x_values = x_values[eval(left_x_values_range)]
        right_x_values = x_values[eval(right_x_values_range)]

        self.left_func = Derivative(expression = func_1)
        self.right_func = Derivative(expression = func_2)

        self.left_func.x = left_x_values
        self.right_func.x = right_x_values

        self.left_func()
        self.right_func()
        
        self.piecewise_func_x = x_values
        self.piecewise_func_y = np.append(self.left_func.y, self.right_func.y)
        self.piecewise_func_derivative = np.append(self.left_func.derivative, self.right_func.derivative)

    def _plot_f(self):

        fig = px.scatter(x = self.piecewise_func_x, y = self.piecewise_func_y, title = "Piecewise Function")
        return fig
    
    def _plot_dev(self):
        fig = px.scatter(x = self.piecewise_func_x, y = self.piecewise_func_derivative, title = "Derivative")
        return fig
    
    def plot(self):
        return self._plot_f(), self._plot_dev()

def get_piecewise_fn(input_1: str, input_2: str, input_3: str, sign_1: str, sign_2: str):
    piecewise_obj = Piecewise(input_1, input_2, float(input_3), [sign_1, sign_2])
    f_fig, _ = piecewise_obj.plot()
    return f_fig

def get_piecewise_dev(input_1: str, input_2: str, input_3: str, sign_1: str, sign_2: str):
    piecewise_obj = Piecewise(input_1, input_2, float(input_3), [sign_1, sign_2])
    _, dev_fig = piecewise_obj.plot()
    return dev_fig