Esempio n. 1
0
def exponential_roots_initial_value(first_constant, second_constant, initial_value, precision = 4):
    # Handle input errors
    three_scalars(first_constant, second_constant, initial_value)
    positive_integer(precision)

    # Create list to return
    result = []

    # Create intermediary variables
    numerator = log(abs(initial_value / first_constant))
    denominator = log(abs(second_constant))

    # Circumvent division by zero
    if denominator == 0:
        denominator = 10**(-precision)

    # Determine root given an initial value
    ratio = numerator / denominator

    # Round root
    rounded_ratio = rounded_value(ratio, precision)

    # Return result
    result.append(rounded_ratio)
    return result
Esempio n. 2
0
def logarithmic_roots_derivative_initial_value(first_constant,
                                               second_constant,
                                               initial_value,
                                               precision=4):
    # Handle input errors
    three_scalars(first_constant, second_constant, initial_value)
    positive_integer(precision)

    # Determine roots of derivative given an initial value
    result = hyperbolic_roots(first_constant, -1 * initial_value, precision)
    return result
Esempio n. 3
0
def linear_roots_initial_value(first_constant,
                               second_constant,
                               initial_value,
                               precision=4):
    # Handle input errors
    three_scalars(first_constant, second_constant, initial_value)
    positive_integer(precision)

    # Determine roots given an initial value
    result = linear_roots(first_constant, second_constant - initial_value,
                          precision)
    return result
Esempio n. 4
0
def exponential_roots_derivative_initial_value(first_constant, second_constant, initial_value, precision = 4):
    # Handle input errors
    three_scalars(first_constant, second_constant, initial_value)
    positive_integer(precision)

    # Circumvent division by zero
    denominator = log(abs(second_constant))
    if denominator == 0:
        denominator = 10**(-precision)

    # Determine root of derivative given an initial value
    result = exponential_roots_initial_value(first_constant * denominator, second_constant, initial_value, precision)
    return result
Esempio n. 5
0
def quadratic_roots_first_derivative(first_constant,
                                     second_constant,
                                     third_constant,
                                     precision=4):
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)

    # Generate coefficients of first derivative
    constants = quadratic_derivatives(first_constant, second_constant,
                                      third_constant)['first']['constants']

    # Determine roots of first derivative
    result = linear_roots(*constants, precision)
    return result
Esempio n. 6
0
def quadratic_roots_second_derivative(first_constant,
                                      second_constant,
                                      third_constant,
                                      precision=4):
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)

    # Create list to return
    result = []

    # Determine root
    root = None

    # Return result
    result.append(root)
    return result
Esempio n. 7
0
def logistic_roots_second_derivative(first_constant,
                                     second_constant,
                                     third_constant,
                                     precision=4):
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)

    # Create list to return
    result = []

    # Determine root of second derivative
    root = rounded_value(third_constant)

    # Return root
    result.append(root)
    return result
Esempio n. 8
0
def linear_roots_derivative_initial_value(first_constant,
                                          second_constant,
                                          initial_value,
                                          precision=4):
    # Handle input errors
    three_scalars(first_constant, second_constant, initial_value)
    positive_integer(precision)

    # Create list to return
    result = []

    # Handle general case
    if initial_value == first_constant:
        result.append('All')

    # Handle exception
    else:
        result.append(None)

    # Return result
    return result
Esempio n. 9
0
def hyperbolic_roots_derivative_initial_value(first_constant,
                                              second_constant,
                                              initial_value,
                                              precision=4):
    # Handle input errors
    three_scalars(first_constant, second_constant, initial_value)
    positive_integer(precision)

    # Create list to return
    result = []

    # Create intermediary variable
    ratio = -1 * first_constant / initial_value

    # Handle no roots
    if ratio < 0:
        result.append(None)

    # Determine roots of derivative given an initial value
    else:
        radical = ratio**(1 / 2)
        rounded_radical = rounded_value(radical, precision)
        result.append(rounded_radical)
    return result
Esempio n. 10
0
def logistic_equation(first_constant, second_constant, third_constant, precision = 4):
    """
    Generates a logistic function to provide evaluations at variable inputs

    Parameters
    ----------
    first_constant : int or float
        Carrying capacity of the resultant logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Growth rate of the resultant logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Value of the sigmoid's midpoint of the resultant logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    evaluation : func
        Function for evaluating a logistic equation when passed any integer or float argument

    See Also
    --------
    :func:`~regressions.analyses.derivatives.logistic.logistic_derivatives`, :func:`~regressions.analyses.integrals.logistic.logistic_integral`, :func:`~regressions.analyses.roots.logistic.logistic_roots`, :func:`~regressions.models.logistic.logistic_model`

    Notes
    -----
    - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}`
    - |logistic_functions|

    Examples
    --------
    Import `logistic_equation` function from `regressions` library
        >>> from regressions.analyses.equations.logistic import logistic_equation
    Create a logistic function with coefficients 2, 3, and 5, then evaluate it at 10
        >>> evaluation_first = logistic_equation(2, 3, 5)
        >>> print(evaluation_first(10))
        2.0
    Create a logistic function with coefficients 100, 5, and 11, then evaluate it at 10
        >>> evaluation_second = logistic_equation(100, 5, 11)
        >>> print(evaluation_second(10))
        0.6693
    Create a logistic function with all inputs set to 0, then evaluate it at 10
        >>> evaluation_zero = logistic_equation(0, 0, 0)
        >>> print(evaluation_zero(10))
        0.0001
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant], precision)

    # Create evaluation
    def logistic_evaluation(variable):
        evaluation = coefficients[0] * (1 + exp(-1 * coefficients[1] * (variable - coefficients[2])))**(-1)
        result = rounded_value(evaluation, precision)
        return result
    return logistic_evaluation
Esempio n. 11
0
def quadratic_derivatives(first_constant,
                          second_constant,
                          third_constant,
                          precision=4):
    """
    Calculates the first and second derivatives of a quadratic function

    Parameters
    ----------
    first_constant : int or float
        Coefficient of the quadratic term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Coefficient of the linear term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Coefficient of the constant term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    derivatives['first']['constants'] : list of float
        Coefficients of the resultant first derivative
    derivatives['first']['evaluation'] : func
        Function for evaluating the resultant first derivative at any float or integer argument
    derivatives['second']['constants'] : list of float
        Coefficients of the resultant second derivative
    derivatives['second']['evaluation'] : func
        Function for evaluating the resultant second derivative at any float or integer argument

    See Also
    --------
    :func:`~regressions.analyses.equations.quadratic.quadratic_equation`, :func:`~regressions.analyses.integrals.quadratic.quadratic_integral`, :func:`~regressions.analyses.roots.quadratic.quadratic_roots`, :func:`~regressions.models.quadratic.quadratic_model`

    Notes
    -----
    - Standard form of a quadratic function: :math:`f(x) = a\\cdot{x^2} + b\\cdot{x} + c`
    - First derivative of a quadratic function: :math:`f'(x) = 2a\\cdot{x} + b`
    - Second derivative of a quadratic function: :math:`f''(x) = 2a`
    - |differentiation_formulas|

    Examples
    --------
    Import `quadratic_derivatives` function from `regressions` library
        >>> from regressions.analyses.derivatives.quadratic import quadratic_derivatives
    Generate the derivatives of a quadratic function with coefficients 2, 3, and 5, then display the coefficients of its first and second derivatives
        >>> derivatives_constants = quadratic_derivatives(2, 3, 5)
        >>> print(derivatives_constants['first']['constants'])
        [4.0, 3.0]
        >>> print(derivatives_constants['second']['constants'])
        [4.0]
    Generate the derivatives of a quadratic function with coefficients 7, -5, and 3, then evaluate its first and second derivatives at 10
        >>> derivatives_evaluation = quadratic_derivatives(7, -5, 3)
        >>> print(derivatives_evaluation['first']['evaluation'](10))
        135.0
        >>> print(derivatives_evaluation['second']['evaluation'](10))
        14.0
    Generate the derivatives of a quadratic function with all inputs set to 0, then display the coefficients of its first and second derivatives
        >>> derivatives_zeroes = quadratic_derivatives(0, 0, 0)
        >>> print(derivatives_zeroes['first']['constants'])
        [0.0002, 0.0001]
        >>> print(derivatives_zeroes['second']['constants'])
        [0.0002]
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant],
                             precision)

    # Create first derivative
    first_constants = [2 * coefficients[0], coefficients[1]]

    def first_derivative(variable):
        evaluation = first_constants[0] * variable + first_constants[1]
        rounded_evaluation = rounded_value(evaluation, precision)
        return rounded_evaluation

    first_dictionary = {
        'constants': first_constants,
        'evaluation': first_derivative
    }

    # Create second derivative
    second_constants = [first_constants[0]]

    def second_derivative(variable):
        evaluation = second_constants[0]
        rounded_evaluation = rounded_value(evaluation, precision)
        return rounded_evaluation

    second_dictionary = {
        'constants': second_constants,
        'evaluation': second_derivative
    }

    # Package both derivatives in single dictionary
    results = {'first': first_dictionary, 'second': second_dictionary}
    return results
Esempio n. 12
0
def quadratic_equation(first_constant,
                       second_constant,
                       third_constant,
                       precision=4):
    """
    Generates a quadratic function to provide evaluations at variable inputs

    Parameters
    ----------
    first_constant : int or float
        Coefficient of the quadratic term of the resultant quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Coefficient of the linear term of the resultant quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Coefficient of the constant term of the resultant quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    evaluation : func
        Function for evaluating a quadratic equation when passed any integer or float argument

    See Also
    --------
    :func:`~regressions.analyses.derivatives.quadratic.quadratic_derivatives`, :func:`~regressions.analyses.integrals.quadratic.quadratic_integral`, :func:`~regressions.analyses.roots.quadratic.quadratic_roots`, :func:`~regressions.models.quadratic.quadratic_model`

    Notes
    -----
    - Standard form of a quadratic function: :math:`f(x) = a\\cdot{x^2} + b\\cdot{x} + c`
    - |quadratic_functions|

    Examples
    --------
    Import `quadratic_equation` function from `regressions` library
        >>> from regressions.analyses.equations.quadratic import quadratic_equation
    Create a quadratic function with coefficients 2, 3, and 5, then evaluate it at 10
        >>> evaluation_first = quadratic_equation(2, 3, 5)
        >>> print(evaluation_first(10))
        235.0
    Create a quadratic function with coefficients 7, -5, and 3, then evaluate it at 10
        >>> evaluation_second = quadratic_equation(7, -5, 3)
        >>> print(evaluation_second(10))
        653.0
    Create a quadratic function with all inputs set to 0, then evaluate it at 10
        >>> evaluation_zero = quadratic_equation(0, 0, 0)
        >>> print(evaluation_zero(10))
        0.0111
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant],
                             precision)

    # Create evaluation
    def quadratic_evaluation(variable):
        evaluation = coefficients[0] * variable**2 + coefficients[
            1] * variable + coefficients[2]
        result = rounded_value(evaluation, precision)
        return result

    return quadratic_evaluation
Esempio n. 13
0
def logistic_roots(first_constant,
                   second_constant,
                   third_constant,
                   precision=4):
    """
    Calculates the roots of a logistic function

    Parameters
    ----------
    first_constant : int or float
        Carrying capacity of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Growth rate of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Value of the sigmoid's midpoint of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    roots : list of float
        List of the x-coordinates of all of the x-intercepts of the original function; if the function never crosses the x-axis, then it will return a list of `None`

    See Also
    --------
    :func:`~regressions.analyses.equations.logistic.logistic_equation`, :func:`~regressions.analyses.derivatives.logistic.logistic_derivatives`, :func:`~regressions.analyses.integrals.logistic.logistic_integral`, :func:`~regressions.models.logistic.logistic_model`

    Notes
    -----
    - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}`
    - Logistic formula: :math:`x = \\varnothing`

    Examples
    --------
    Import `logistic_roots` function from `regressions` library
        >>> from regressions.analyses.roots.logistic import logistic_roots
    Calculate the roots of a logistic function with coefficients 2, 3, and 5
        >>> roots_first = logistic_roots(2, 3, 5)
        >>> print(roots_first)
        [None]
    Calculate the roots of a logistic function with coefficients 100, 5, and 11
        >>> roots_second = logistic_roots(100, 5, 11)
        >>> print(roots_second)
        [None]
    Calculate the roots of a logistic function with all inputs set to 0
        >>> roots_zeroes = logistic_roots(0, 0, 0)
        >>> print(roots_zeroes)
        [None]
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant],
                             precision)

    # Create list to return
    result = []

    # Determine root
    root = None

    # Return result
    result.append(root)
    return result
Esempio n. 14
0
def logistic_derivatives(first_constant,
                         second_constant,
                         third_constant,
                         precision=4):
    """
    Calculates the first and second derivatives of a logistic function

    Parameters
    ----------
    first_constant : int or float
        Carrying capacity of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Growth rate of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Value of the sigmoid's midpoint of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    derivatives['first']['constants'] : list of float
        Coefficients of the resultant first derivative
    derivatives['first']['evaluation'] : func
        Function for evaluating the resultant first derivative at any float or integer argument
    derivatives['second']['constants'] : list of float
        Coefficients of the resultant second derivative
    derivatives['second']['evaluation'] : func
        Function for evaluating the resultant second derivative at any float or integer argument

    See Also
    --------
    :func:`~regressions.analyses.equations.logistic.logistic_equation`, :func:`~regressions.analyses.integrals.logistic.logistic_integral`, :func:`~regressions.analyses.roots.logistic.logistic_roots`, :func:`~regressions.models.logistic.logistic_model`

    Notes
    -----
    - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}`
    - First derivative of a logistic function: :math:`f'(x) = \\frac{ab\\cdot{\\text{e}^{-b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^2}`
    - Second derivative of a logistic function: :math:`f''(x) = \\frac{2ab^2\\cdot{\\text{e}^{-2b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^3} - \\frac{ab^2\\cdot{\\text{e}^{-b\\cdot(x - c)}}}{(1 + \\text{e}^{-b\\cdot(x - c)})^2}`
    - |differentiation_formulas|
    - |chain_rule|
    - |exponential|

    Examples
    --------
    Import `logistic_derivatives` function from `regressions` library
        >>> from regressions.analyses.derivatives.logistic import logistic_derivatives
    Generate the derivatives of a logistic function with coefficients 2, 3, and 5, then display the coefficients of its first and second derivatives
        >>> derivatives_constants = logistic_derivatives(2, 3, 5)
        >>> print(derivatives_constants['first']['constants'])
        [6.0, 3.0, 5.0]
        >>> print(derivatives_constants['second']['constants'])
        [18.0, 3.0, 5.0]
    Generate the derivatives of a logistic function with coefficients 100, 5, and 11, then evaluate its first and second derivatives at 10
        >>> derivatives_evaluation = logistic_derivatives(100, 5, 11)
        >>> print(derivatives_evaluation['first']['evaluation'](10))
        3.324
        >>> print(derivatives_evaluation['second']['evaluation'](10))
        16.3977
    Generate the derivatives of a logistic function with all inputs set to 0, then display the coefficients of its first and second derivatives
        >>> derivatives_zeroes = logistic_derivatives(0, 0, 0)
        >>> print(derivatives_zeroes['first']['constants'])
        [0.0001, 0.0001, 0.0001]
        >>> print(derivatives_zeroes['second']['constants'])
        [0.0001, 0.0001, 0.0001]
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant],
                             precision)

    # Create first derivative
    first_coefficients = [
        coefficients[0] * coefficients[1], coefficients[1], coefficients[2]
    ]
    first_constants = rounded_list(first_coefficients, precision)

    def first_derivative(variable):
        exponential = exp(-1 * first_constants[1] *
                          (variable - first_constants[2]))
        evaluation = first_constants[0] * exponential * (1 + exponential)**(-2)
        rounded_evaluation = rounded_value(evaluation, precision)
        return rounded_evaluation

    first_dictionary = {
        'constants': first_constants,
        'evaluation': first_derivative
    }

    # Create second derivative
    second_coefficients = [
        first_constants[0] * first_constants[1], first_constants[1],
        first_constants[2]
    ]
    second_constants = rounded_list(second_coefficients, precision)

    def second_derivative(variable):
        exponential = exp(-1 * second_constants[1] *
                          (variable - second_constants[2]))
        evaluation = second_constants[0] * exponential * (1 + exponential)**(
            -2) * (2 * exponential / (1 + exponential) - 1)
        rounded_evaluation = rounded_value(evaluation, precision)
        return rounded_evaluation

    second_dictionary = {
        'constants': second_constants,
        'evaluation': second_derivative
    }

    # Package both derivatives in single dictionary
    results = {'first': first_dictionary, 'second': second_dictionary}
    return results
Esempio n. 15
0
def logistic_integral(first_constant,
                      second_constant,
                      third_constant,
                      precision=4):
    """
    Generates the integral of a logistic function

    Parameters
    ----------
    first_constant : int or float
        Carrying capacity of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Growth rate of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Value of the sigmoid's midpoint of the original logistic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    integral['constants'] : list of float
        Coefficients of the resultant integral
    integral['evaluation'] : func
        Function for evaluating the resultant integral at any float or integer argument

    See Also
    --------
    :func:`~regressions.analyses.equations.logistic.logistic_equation`, :func:`~regressions.analyses.derivatives.logistic.logistic_derivatives`, :func:`~regressions.analyses.roots.logistic.logistic_roots`, :func:`~regressions.models.logistic.logistic_model`

    Notes
    -----
    - Standard form of a logistic function: :math:`f(x) = \\frac{a}{1 + \\text{e}^{-b\\cdot(x - c)}}`
    - Integral of a logistic function: :math:`F(x) = \\frac{a}{b}\\cdot{\\ln|\\text{e}^{b\\cdot(x - c)} + 1|}`
    - |indefinite_integral|
    - |integration_formulas|
    - |substitution_rule|

    Examples
    --------
    Import `logistic_integral` function from `regressions` library
        >>> from regressions.analyses.integrals.logistic import logistic_integral
    Generate the integral of a logistic function with coefficients 2, 3, and 5, then display its coefficients
        >>> integral_constants = logistic_integral(2, 3, 5)
        >>> print(integral_constants['constants'])
        [0.6667, 3.0, 5.0]
    Generate the integral of a logistic function with coefficients 100, 5, and 11, then evaluate its integral at 10
        >>> integral_evaluation = logistic_integral(100, 5, 11)
        >>> print(integral_evaluation['evaluation'](10))
        0.1343
    Generate the integral of a logistic function with all inputs set to 0, then display its coefficients
        >>> integral_zeroes = logistic_integral(0, 0, 0)
        >>> print(integral_zeroes['constants'])
        [1.0, 0.0001, 0.0001]
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant],
                             precision)

    # Create constants
    integral_coefficients = [
        coefficients[0] / coefficients[1], coefficients[1], coefficients[2]
    ]
    constants = rounded_list(integral_coefficients, precision)

    # Create evaluation
    def logistic_evaluation(variable):
        evaluation = constants[0] * log(
            abs(exp(constants[1] * (variable - constants[2])) + 1))
        rounded_evaluation = rounded_value(evaluation, precision)
        return rounded_evaluation

    # Package constants and evaluation in single dictionary
    results = {'constants': constants, 'evaluation': logistic_evaluation}
    return results
Esempio n. 16
0
 def test_three_scalars_integer_float_whole(self):
     three_scalars_integer_float_whole = three_scalars(
         good_integer, good_float, good_whole)
     self.assertEqual(
         three_scalars_integer_float_whole,
         'First, second, and third arguments are all integers or floats')
Esempio n. 17
0
 def test_three_scalars_integer_float_string_raises(self):
     with self.assertRaises(Exception) as context:
         three_scalars(good_integer, good_float, bad_scalar)
     self.assertEqual(type(context.exception), TypeError)
     self.assertEqual(str(context.exception),
                      'Third argument must be an integer or a float')
Esempio n. 18
0
def quadratic_roots(first_constant,
                    second_constant,
                    third_constant,
                    precision=4):
    """
    Calculates the roots of a quadratic function

    Parameters
    ----------
    first_constant : int or float
        Coefficient of the quadratic term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Coefficient of the linear term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Coefficient of the constant term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    roots : list of float
        List of the x-coordinates of all of the x-intercepts of the original function; if the function never crosses the x-axis, then it will return a list of `None`

    See Also
    --------
    :func:`~regressions.analyses.equations.quadratic.quadratic_equation`, :func:`~regressions.analyses.derivatives.quadratic.quadratic_derivatives`, :func:`~regressions.analyses.integrals.quadratic.quadratic_integral`, :func:`~regressions.models.quadratic.quadratic_model`

    Notes
    -----
    - Standard form of a quadratic function: :math:`f(x) = a\\cdot{x^2} + b\\cdot{x} + c`
    - Quadratic formula: :math:`x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}`
    - |quadratic_formula|

    Examples
    --------
    Import `quadratic_roots` function from `regressions` library
        >>> from regressions.analyses.roots.quadratic import quadratic_roots
    Calculate the roots of a quadratic function with coefficients 2, 7, and 5
        >>> roots_first = quadratic_roots(2, 7, 5)
        >>> print(roots_first)
        [-2.5, -1.0]
    Calculate the roots of a quadratic function with coefficients 2, -5, and 3
        >>> roots_second = quadratic_roots(2, -5, 3)
        >>> print(roots_second)
        [1.0, 1.5]
    Calculate the roots of a quadratic function with all inputs set to 0
        >>> roots_zeroes = quadratic_roots(0, 0, 0)
        >>> print(roots_zeroes)
        [None]
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant],
                             precision)

    # Create intermediary list and list to return
    roots = []
    result = []

    # Create intermediary variable
    discriminant = coefficients[1]**2 - 4 * coefficients[0] * coefficients[2]

    # Create roots
    first_root = (-1 * coefficients[1] +
                  discriminant**(1 / 2)) / (2 * coefficients[0])
    second_root = (-1 * coefficients[1] -
                   discriminant**(1 / 2)) / (2 * coefficients[0])

    # Eliminate duplicate roots
    if first_root == second_root:
        roots.append(first_root)

    # Eliminate complex roots
    else:
        if not isinstance(first_root, complex):
            roots.append(first_root)
        if not isinstance(second_root, complex):
            roots.append(second_root)

    # Handle no roots
    if not roots:
        roots.append(None)

    # Sort roots
    sorted_roots = sorted_list(roots)

    # Round roots
    rounded_roots = rounded_list(sorted_roots, precision)

    # Return result
    result.extend(rounded_roots)
    return result
Esempio n. 19
0
def quadratic_integral(first_constant,
                       second_constant,
                       third_constant,
                       precision=4):
    """
    Generates the integral of a quadratic function

    Parameters
    ----------
    first_constant : int or float
        Coefficient of the quadratic term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    second_constant : int or float
        Coefficient of the linear term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    third_constant : int or float
        Coefficient of the constant term of the original quadratic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
    precision : int, default=4
        Maximum number of digits that can appear after the decimal place of the resultant roots

    Raises
    ------
    TypeError
        First three arguments must be integers or floats
    ValueError
        Last argument must be a positive integer

    Returns
    -------
    integral['constants'] : list of float
        Coefficients of the resultant integral
    integral['evaluation'] : func
        Function for evaluating the resultant integral at any float or integer argument

    See Also
    --------
    :func:`~regressions.analyses.equations.quadratic.quadratic_equation`, :func:`~regressions.analyses.derivatives.quadratic.quadratic_derivatives`, :func:`~regressions.analyses.roots.quadratic.quadratic_roots`, :func:`~regressions.models.quadratic.quadratic_model`

    Notes
    -----
    - Standard form of a quadratic function: :math:`f(x) = a\\cdot{x^2} + b\\cdot{x} + c`
    - Integral of a quadratic function: :math:`F(x) = \\frac{a}{3}\\cdot{x^3} + \\frac{b}{2}\\cdot{x^2} + c\\cdot{x}`
    - |indefinite_integral|
    - |integration_formulas|

    Examples
    --------
    Import `quadratic_integral` function from `regressions` library
        >>> from regressions.analyses.integrals.quadratic import quadratic_integral
    Generate the integral of a quadratic function with coefficients 2, 3, and 5, then display its coefficients
        >>> integral_constants = quadratic_integral(2, 3, 5)
        >>> print(integral_constants['constants'])
        [0.6667, 1.5, 5.0]
    Generate the integral of a quadratic function with coefficients 7, -5, and 3, then evaluate its integral at 10
        >>> integral_evaluation = quadratic_integral(7, -5, 3)
        >>> print(integral_evaluation['evaluation'](10))
        2113.3
    Generate the integral of a quadratic function with all inputs set to 0, then display its coefficients
        >>> integral_zeroes = quadratic_integral(0, 0, 0)
        >>> print(integral_zeroes['constants'])
        [0.0001, 0.0001, 0.0001]
    """
    # Handle input errors
    three_scalars(first_constant, second_constant, third_constant)
    positive_integer(precision)
    coefficients = no_zeroes([first_constant, second_constant, third_constant],
                             precision)

    # Create constants
    integral_coefficients = [(1 / 3) * coefficients[0],
                             (1 / 2) * coefficients[1], coefficients[2]]
    constants = rounded_list(integral_coefficients, precision)

    # Create evaluation
    def quadratic_evaluation(variable):
        evaluation = constants[0] * variable**3 + constants[
            1] * variable**2 + constants[2] * variable
        rounded_evaluation = rounded_value(evaluation, precision)
        return rounded_evaluation

    # Package constants and evaluation in single dictionary
    results = {'constants': constants, 'evaluation': quadratic_evaluation}
    return results