Custom Functions

Example of a Custom Function

Custom functions can be easily added to greatly improve functionality. Functions Generally Look Like This (Example is to Implement Sine Function)

from ArrayExpressions import arrex
import math

def sin_func(**kwargs):
    #Gets the arguments
    k = kwargs['k'] #Parameter
    array = kwargs['array'] #Scope array
    tokens = kwargs['tokens'] #Tokens
    token_types = kwargs['token_types'] #Token Types
    index = kwargs['index'] #Index
    a = kwargs['a'] #The a
    b = kwargs['b'] #The b

    # Verify type of k (not needed here)
    # arrex.verify_type(k, "NUMBER") #Can be "STRING", "BOOL", "ARRAY". Can set optional parameter to True to allow something like tn to count

    #Do any checks that need to be made for k, array, etc.

    #Interprets answer
    tokens_info = arrex.interpret_code(tokens, token_types, array, index, a, b)
    
    #Converts answer to python equivalent type as tokens_info[0][0] is the token and [0][1] is the token type
    answer = arrex.convert_token_with_token_type(tokens_info[0][0], tokens_info[1][0])

    #Do any needed checks
    if not isinstance(answer, (int, float)):
        arrex.call_error("Tokens inside are not of type float")
    
    #Return value
    return round(math.sin(math.radians(answer)), 10)

func_dict = {
    "sin": sin_func
}

lst = [90, 180, 270, 360, 45, 30]

arrex.evaluate(lst, "e(sin(x))", [func_dict]) #[1.0, 0.0, -1.0, -0.0, 0.7071067812, 0.5]

Essentially, a custom function is defined by making a python function, giving it a name using a dictionary, and putting the list of dictionary(s) in the optional argument

Operators

eK()