def exponential(data, precision): independent_variable = dimension(data, 1) dependent_variable = dimension(data, 2) independent_matrix = [] dependent_matrix = [] for i in range(len(data)): independent_matrix.append([independent_variable[i], 1]) dependent_matrix.append([log(dependent_variable[i])]) solution = solve(independent_matrix, dependent_matrix, precision) constants = [exp(solution[1]), exp(solution[0])] coefficients = [ rounding(constants[0], precision), rounding(constants[1], precision) ] equation = exponential_equation(*coefficients) derivative = exponential_derivative(*coefficients) integral = exponential_integral(*coefficients)['evaluation'] first_derivative = derivative['first']['evaluation'] second_derivative = derivative['second']['evaluation'] points = key_points('exponential', solution, equation, first_derivative, second_derivative, precision) five_numbers = five_number_summary(independent_variable, precision) min_value = five_numbers['minimum'] max_value = five_numbers['maximum'] q1 = five_numbers['q1'] q3 = five_numbers['q3'] accumulated_range = accumulation(integral, min_value, max_value, precision) accumulated_iqr = accumulation(integral, q1, q3, precision) averages_range = average_values('exponential', equation, integral, min_value, max_value, coefficients, precision) averages_iqr = average_values('exponential', equation, integral, q1, q3, coefficients, precision) predicted = [] for i in range(len(data)): predicted.append(equation(independent_variable[i])) accuracy = correlation(dependent_variable, predicted, precision) evaluations = { 'equation': equation, 'derivative': first_derivative, 'integral': integral } points = { 'roots': points['roots'], 'maxima': points['maxima'], 'minima': points['minima'], 'inflections': points['inflections'] } accumulations = {'range': accumulated_range, 'iqr': accumulated_iqr} averages = {'range': averages_range, 'iqr': averages_iqr} result = { 'constants': coefficients, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def mean_values_integral(equation_type, equation, start, end, constants, precision): result = [] average = average_value_integral(equation, start, end, precision) if equation_type == 'linear': value = linear_roots(constants[0], constants[1] - average, precision) result = value elif equation_type == 'quadratic': values = quadratic_roots(constants[0], constants[1], constants[2] - average, precision) result = values elif equation_type == 'cubic': values = cubic_roots(constants[0], constants[1], constants[2], constants[3] - average, precision) result = values elif equation_type == 'hyperbolic': value = hyperbolic_roots(constants[0], constants[1] - average, precision) result = value elif equation_type == 'exponential': value = log(average / constants[0]) / log(constants[1]) result.append(value) elif equation_type == 'logarithmic': value = logarithmic_roots(constants[0], constants[1] - average, precision) result = value elif equation_type == 'logistic': ratio = constants[0] / average if ratio <= 1: pass else: value = constants[2] - log(ratio - 1) / constants[1] result.append(value) elif equation_type == 'sinusoidal': values = sinusoidal_roots(constants[0], constants[1], constants[2], constants[3] - average, precision) result = values if not result: result = [None] return result numerical_results = [] other_results = [] for item in result: if isinstance(item, (int, float)): numerical_results.append(item) else: other_results.append(item) selected_results = [x for x in numerical_results if x > start and x < end] if not selected_results: final = [None] return final sorted_results = sort(selected_results) rounded_results = [] for number in sorted_results: rounded_results.append(rounding(number, precision)) final_result = rounded_results + other_results return final_result
def cubic(first_constant, second_constant, third_constant, fourth_constant, precision): roots = [] xi = (-1 + (-3)**(1/2)) / 2 delta_first = second_constant**2 - 3 * first_constant * third_constant delta_second = 2 * second_constant**3 - 9 * first_constant * second_constant * third_constant + 27 * first_constant**2 * fourth_constant discriminant = delta_second**2 - 4 * delta_first**3 zeta_first = ((delta_second + discriminant**(1/2)) / 2)**(1/3) zeta_second = ((delta_second - discriminant**(1/2)) / 2)**(1/3) zeta = 0 if zeta_first == 0: zeta = zeta_second else: zeta = zeta_first first_root = (-1 / (3 * first_constant)) * (second_constant + zeta * xi**0 + delta_first / (zeta * xi**0)) second_root = (-1 / (3 * first_constant)) * (second_constant + zeta * xi**1 + delta_first / (zeta * xi**1)) third_root = (-1 / (3 * first_constant)) * (second_constant + zeta * xi**2 + delta_first / (zeta * xi**2)) first_real = first_root.real second_real = second_root.real third_real = third_root.real first_imag = first_root.imag second_imag = second_root.imag third_imag = third_root.imag size_first_imag = (first_imag**2)**(1/2) size_second_imag = (second_imag**2)**(1/2) size_third_imag = (third_imag**2)**(1/2) if size_first_imag < 0.0001: first_root = first_real roots.append(first_root) if size_second_imag < 0.0001: second_root = second_real roots.append(second_root) if size_third_imag < 0.0001: third_root = third_real roots.append(third_root) unique_roots = list(set(roots)) if not unique_roots: unique_roots = [None] sorted_roots = sort(unique_roots) result = [] for number in sorted_roots: result.append(rounding(number, precision)) return result
def quadratic(first_constant, second_constant, third_constant, precision): roots = [] discriminant = second_constant**2 - 4 * first_constant * third_constant first_root = (-1 * second_constant + discriminant**(1 / 2)) / (2 * first_constant) second_root = (-1 * second_constant - discriminant**(1 / 2)) / (2 * first_constant) if first_root == second_root: roots.append(first_root) else: if not isinstance(first_root, complex): roots.append(first_root) if not isinstance(second_root, complex): roots.append(second_root) if not roots: roots = [None] sorted_roots = sort(roots) result = [] for number in sorted_roots: result.append(rounding(number, precision)) return result
def linear(first_constant, second_constant, precision): root = -1 * second_constant / first_constant result = [rounding(root, precision)] return result
def sinusoidal(first_constant, second_constant, third_constant, fourth_constant, precision): roots = [] ratio = -1 * fourth_constant / first_constant if ratio > 1 or ratio < -1: roots = [None] else: radians = asin(ratio) periodic_radians = radians / second_constant if ratio == 0: periodic_unit = pi / second_constant initial_value = third_constant + periodic_radians first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit third_value = initial_value + 3 * periodic_unit fourth_value = initial_value + 4 * periodic_unit rounded_initial_value = rounding(initial_value, precision) rounded_periodic_unit = rounding(periodic_unit, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' roots = [ initial_value, first_value, second_value, third_value, fourth_value, general_form ] elif ratio == 1 or ratio == -1: periodic_unit = 2 * pi / second_constant initial_value = third_constant + periodic_radians first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit rounded_initial_value = rounding(initial_value, precision) rounded_periodic_unit = rounding(periodic_unit, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' roots = [initial_value, first_value, second_value, general_form] else: periodic_unit = 2 * pi / second_constant initial_value = third_constant + periodic_radians first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit rounded_initial_value = rounding(initial_value, precision) rounded_periodic_unit = rounding(periodic_unit, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' alternative_initial_value = third_constant + pi / second_constant - periodic_radians alternative_first_value = alternative_initial_value + 1 * periodic_unit alternative_second_value = alternative_initial_value + 2 * periodic_unit rounded_alternative_initial_value = rounding( alternative_initial_value, precision) alternative_general_form = str( rounded_alternative_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' roots = [ initial_value, first_value, second_value, alternative_initial_value, alternative_first_value, alternative_second_value, general_form, alternative_general_form ] if not roots: roots = [None] numerical_roots = [] other_roots = [] for item in roots: if isinstance(item, (int, float)): numerical_roots.append(item) else: other_roots.append(item) sorted_roots = sort(numerical_roots) rounded_roots = [] for number in sorted_roots: rounded_roots.append(rounding(number, precision)) result = rounded_roots + other_roots return result
def mean_values_derivative(equation_type, equation, start, end, constants, precision): result = [] average = average_value_derivative(equation, start, end, precision) if equation_type == 'linear': result.append('All') return result elif equation_type == 'quadratic': value = linear_roots(2 * constants[0], constants[1] - average, precision) result = value elif equation_type == 'cubic': values = quadratic_roots(3 * constants[0], 2 * constants[1], constants[2] - average, precision) result = values elif equation_type == 'hyperbolic': ratio = -1 * constants[0] / average root = ratio**(1 / 2) first_value = root second_value = -1 * root result.extend([first_value, second_value]) elif equation_type == 'exponential': base_log = log(constants[1]) numerator = log(average / (constants[0] * base_log)) denominator = base_log value = numerator / denominator result.append(value) elif equation_type == 'logarithmic': value = hyperbolic_roots(constants[0], -1 * average, precision) result = value elif equation_type == 'logistic': quadratic_values = quadratic_roots( average, 2 * average - constants[0] * constants[1], average, precision) if quadratic_values[0] == None: result = [None] return result else: values = [] for i in range(len(quadratic_values)): values.append(constants[2] - log(quadratic_values[i]) / constants[1]) result = values elif equation_type == 'sinusoidal': ratio = average / (constants[0] * constants[1]) radians = acos(ratio) periodic_radians = radians / constants[1] if ratio == 0: periodic_unit = pi / constants[1] initial_value = constants[2] + periodic_radians first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit third_value = initial_value + 3 * periodic_unit fourth_value = initial_value + 4 * periodic_unit rounded_initial_value = rounding(initial_value, precision) rounded_periodic_unit = rounding(periodic_unit, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' result = [ initial_value, first_value, second_value, third_value, fourth_value, general_form ] elif ratio == 1 or ratio == -1: periodic_unit = 2 * pi / constants[1] initial_value = constants[2] + periodic_radians first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit rounded_initial_value = rounding(initial_value, precision) rounded_periodic_unit = rounding(periodic_unit, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' result = [initial_value, first_value, second_value, general_form] else: periodic_unit = 2 * pi / constants[1] initial_value = constants[2] + periodic_radians first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit rounded_initial_value = rounding(initial_value, precision) rounded_periodic_unit = rounding(periodic_unit, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' alternative_initial_value = constants[ 2] + pi / constants[1] - periodic_radians alternative_first_value = alternative_initial_value + 1 * periodic_unit alternative_second_value = alternative_initial_value + 2 * periodic_unit rounded_alternative_initial_value = rounding( alternative_initial_value, precision) alternative_general_form = str( rounded_alternative_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' result = [ initial_value, first_value, second_value, alternative_initial_value, alternative_first_value, alternative_second_value, general_form, alternative_general_form ] if not result: result = [None] return result numerical_results = [] other_results = [] for item in result: if isinstance(item, (int, float)): numerical_results.append(item) else: other_results.append(item) selected_results = [x for x in numerical_results if x > start and x < end] if not selected_results: final = [None] return final sorted_results = sort(selected_results) rounded_results = [] for number in sorted_results: rounded_results.append(rounding(number, precision)) final_result = rounded_results + other_results return final_result
def average_value_integral(equation, start, end, precision): accumulated_value = accumulation(equation, start, end, precision) change = end - start ratio = accumulated_value / change result = rounding(ratio, precision) return result
def average_value_derivative(equation, start, end, precision): vertical_change = equation(end) - equation(start) horizontal_change = end - start ratio = vertical_change / horizontal_change result = rounding(ratio, precision) return result
def key_points(equation_type, coefficients, equation, first_derivative, second_derivative, precision): intercepts_inputs = intercepts(equation_type, coefficients, precision) extrema_inputs = extrema(equation_type, coefficients, first_derivative, precision) maxima_inputs = extrema_inputs['maxima'] minima_inputs = extrema_inputs['minima'] inflections_inputs = inflections(equation_type, coefficients, second_derivative, precision) intercepts_outputs = [] maxima_outputs = [] minima_outputs = [] inflections_outputs = [] intercepts_coordinates = [] maxima_coordinates = [] minima_coordinates = [] inflections_coordinates = [] if intercepts_inputs[0] == None: intercepts_coordinates = [None] else: for i in range(len(intercepts_inputs)): intercepts_outputs.append(0) intercepts_coordinates = unify(intercepts_inputs, intercepts_outputs) if maxima_inputs[0] == None: maxima_coordinates = [None] else: for i in range(len(maxima_inputs)): if isinstance(maxima_inputs[i], (int, float)): output = equation(maxima_inputs[i]) rounded_output = rounding(output, precision) maxima_outputs.append(rounded_output) else: periodic_unit = 2 * float(maxima_inputs[i][:-1]) initial_value = maxima_inputs[0] general_form = str(initial_value) + ' + ' + str( periodic_unit) + 'k' maxima_inputs[i] = general_form maxima_outputs.append(maxima_outputs[0]) maxima_coordinates = unify(maxima_inputs, maxima_outputs) if minima_inputs[0] == None: minima_coordinates = [None] else: for i in range(len(minima_inputs)): if isinstance(minima_inputs[i], (int, float)): output = equation(minima_inputs[i]) rounded_output = rounding(output, precision) minima_outputs.append(rounded_output) else: periodic_unit = 2 * float(minima_inputs[i][:-1]) initial_value = minima_inputs[0] general_form = str(initial_value) + ' + ' + str( periodic_unit) + 'k' minima_inputs[i] = general_form minima_outputs.append(minima_outputs[0]) minima_coordinates = unify(minima_inputs, minima_outputs) if inflections_inputs[0] == None: inflections_coordinates = [None] else: for i in range(len(inflections_inputs)): if isinstance(inflections_inputs[i], (int, float)): output = equation(inflections_inputs[i]) rounded_output = rounding(output, precision) inflections_outputs.append(rounded_output) else: inflections_outputs.append(inflections_outputs[0]) inflections_coordinates = unify(inflections_inputs, inflections_outputs) result = { 'roots': intercepts_coordinates, 'maxima': maxima_coordinates, 'minima': minima_coordinates, 'inflections': inflections_coordinates } return result
def hyperbolic(first_constant, second_constant, precision): root = -1 * first_constant / second_constant result = [rounding(root, precision)] return result
def sinusoidal(data, precision): independent_variable = dimension(data, 1) dependent_variable = dimension(data, 2) dependent_max = max(dependent_variable) dependent_min = min(dependent_variable) dependent_range = dependent_max - dependent_min solution = [] def sinusoidal_fit(variable, first_constant, second_constant, third_constant, fourth_constant): evaluation = first_constant * sin( second_constant * (variable - third_constant)) + fourth_constant return evaluation parameters, covariance = curve_fit(sinusoidal_fit, independent_variable, dependent_variable, bounds=[(-1 * dependent_range, -inf, -inf, dependent_min), (dependent_range, inf, inf, dependent_max)]) solution = list(parameters) constants = [] for number in solution: constants.append(rounding(number, precision)) equation = sinusoidal_equation(*solution) derivative = sinusoidal_derivative(*solution) integral = sinusoidal_integral(*solution)['evaluation'] first_derivative = derivative['first']['evaluation'] second_derivative = derivative['second']['evaluation'] points = key_points('sinusoidal', solution, equation, first_derivative, second_derivative, precision) five_numbers = five_number_summary(independent_variable, precision) min_value = five_numbers['minimum'] max_value = five_numbers['maximum'] q1 = five_numbers['q1'] q3 = five_numbers['q3'] accumulated_range = accumulation(integral, min_value, max_value, precision) accumulated_iqr = accumulation(integral, q1, q3, precision) averages_range = average_values('sinusoidal', equation, integral, min_value, max_value, solution, precision) averages_iqr = average_values('sinusoidal', equation, integral, q1, q3, solution, precision) predicted = [] for i in range(len(data)): predicted.append(equation(independent_variable[i])) accuracy = correlation(dependent_variable, predicted, precision) evaluations = { 'equation': equation, 'derivative': first_derivative, 'integral': integral } points = { 'roots': points['roots'], 'maxima': points['maxima'], 'minima': points['minima'], 'inflections': points['inflections'] } accumulations = {'range': accumulated_range, 'iqr': accumulated_iqr} averages = {'range': averages_range, 'iqr': averages_iqr} result = { 'constants': constants, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def logistic(data, precision): independent_variable = dimension(data, 1) dependent_variable = dimension(data, 2) halved_data = halve_dimension(data, 1) dependent_lower = dimension(halved_data['lower'], 2) dependent_upper = dimension(halved_data['upper'], 2) mean_lower = mean(dependent_lower) mean_upper = mean(dependent_upper) dependent_max = max(dependent_variable) dependent_min = min(dependent_variable) dependent_range = dependent_max - dependent_min solution = [] def logistic_fit(variable, first_constant, second_constant, third_constant): evaluation = first_constant / (1 + exp(-1 * second_constant * (variable - third_constant))) return evaluation if mean_upper >= mean_lower: parameters, covariance = curve_fit( logistic_fit, independent_variable, dependent_variable, bounds=[(dependent_max - dependent_range, 0, -inf), (dependent_max + dependent_range, inf, inf)]) solution = list(parameters) else: parameters, covariance = curve_fit( logistic_fit, independent_variable, dependent_variable, bounds=[(dependent_max - dependent_range, -inf, -inf), (dependent_max + dependent_range, 0, inf)]) solution = list(parameters) constants = [] for number in solution: constants.append(rounding(number, precision)) equation = logistic_equation(*solution) derivative = logistic_derivative(*solution) integral = logistic_integral(*solution)['evaluation'] first_derivative = derivative['first']['evaluation'] second_derivative = derivative['second']['evaluation'] points = key_points('logistic', solution, equation, first_derivative, second_derivative, precision) five_numbers = five_number_summary(independent_variable, precision) min_value = five_numbers['minimum'] max_value = five_numbers['maximum'] q1 = five_numbers['q1'] q3 = five_numbers['q3'] accumulated_range = accumulation(integral, min_value, max_value, precision) accumulated_iqr = accumulation(integral, q1, q3, precision) averages_range = average_values('logistic', equation, integral, min_value, max_value, solution, precision) averages_iqr = average_values('logistic', equation, integral, q1, q3, solution, precision) predicted = [] for i in range(len(data)): predicted.append(equation(independent_variable[i])) accuracy = correlation(dependent_variable, predicted, precision) evaluations = { 'equation': equation, 'derivative': first_derivative, 'integral': integral } points = { 'roots': points['roots'], 'maxima': points['maxima'], 'minima': points['minima'], 'inflections': points['inflections'] } accumulations = {'range': accumulated_range, 'iqr': accumulated_iqr} averages = {'range': averages_range, 'iqr': averages_iqr} result = { 'constants': constants, 'evaluations': evaluations, 'points': points, 'accumulations': accumulations, 'averages': averages, 'correlation': accuracy } return result
def critical_points(equation_type, derivative_level, coefficients, precision): results = [] if derivative_level == 1: if equation_type == 'linear': results = [None] elif equation_type == 'quadratic': constants = quadratic_derivatives( coefficients[0], coefficients[1], coefficients[2])['first']['constants'] results = linear_roots(constants[0], constants[1], precision) elif equation_type == 'cubic': constants = cubic_derivatives( coefficients[0], coefficients[1], coefficients[2], coefficients[3])['first']['constants'] results = quadratic_roots(constants[0], constants[1], constants[2], precision) elif equation_type == 'hyperbolic': results = [0] elif equation_type == 'exponential': results = [None] elif equation_type == 'logarithmic': results = [None] elif equation_type == 'logistic': results = [None] elif equation_type == 'sinusoidal': periodic_unit = pi / coefficients[1] initial_value = coefficients[2] + pi / (2 * coefficients[1]) first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit third_value = initial_value + 3 * periodic_unit fourth_value = initial_value + 4 * periodic_unit values = [ initial_value, first_value, second_value, third_value, fourth_value ] sorted_values = sort(values) rounded_values = [] for number in sorted_values: rounded_values.append(rounding(number, precision)) rounded_periodic_unit = rounding(periodic_unit, precision) rounded_initial_value = rounding(initial_value, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' results = [*rounded_values, general_form] elif derivative_level == 2: if equation_type == 'linear': results = [None] elif equation_type == 'quadratic': results = [None] elif equation_type == 'cubic': constants = cubic_derivatives( coefficients[0], coefficients[1], coefficients[2], coefficients[3])['second']['constants'] results = linear_roots(constants[0], constants[1], precision) elif equation_type == 'hyperbolic': results = [0] elif equation_type == 'exponential': results = [None] elif equation_type == 'logarithmic': results = [None] elif equation_type == 'logistic': results = [rounding(coefficients[2], precision)] elif equation_type == 'sinusoidal': periodic_unit = pi / coefficients[1] initial_value = coefficients[2] first_value = initial_value + 1 * periodic_unit second_value = initial_value + 2 * periodic_unit third_value = initial_value + 3 * periodic_unit fourth_value = initial_value + 4 * periodic_unit values = [ initial_value, first_value, second_value, third_value, fourth_value ] sorted_values = sort(values) rounded_values = [] for number in sorted_values: rounded_values.append(rounding(number, precision)) rounded_periodic_unit = rounding(periodic_unit, precision) rounded_initial_value = rounding(initial_value, precision) general_form = str(rounded_initial_value) + ' + ' + str( rounded_periodic_unit) + 'k' results = [*rounded_values, general_form] return results
def accumulation(integral, start, end, precision): area = integral(end) - integral(start) result = rounding(area, precision) return result
def logarithmic(first_constant, second_constant, precision): root = exp(-1 * second_constant / first_constant) result = [rounding(root, precision)] return result