Skip to content

Type check

check_functions(expression, function_test)

Example

from sympy import sympify expr = sympify('sin(x)') check_functions(expr, lambda x: x in ['sin']) True

expr = sympify('x + y') check_functions(expr, lambda x: x in ['sin']) True

expr = sympify('sin(x) + cos(y)') check_functions(expr, lambda x: x in ['sin', 'cos']) True

check_functions(expr, lambda x: x in ['sin']) False

Source code in src/equation_tree/util/type_check.py
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def check_functions(expression, function_test):
    """
    Example:
        >>> from sympy import sympify
        >>> expr = sympify('sin(x)')
        >>> check_functions(expr, lambda x: x in ['sin'])
        True

        >>> expr = sympify('x + y')
        >>> check_functions(expr, lambda x: x in ['sin'])
        True

        >>> expr = sympify('sin(x) + cos(y)')
        >>> check_functions(expr, lambda x: x in ['sin', 'cos'])
        True

        >>> check_functions(expr, lambda x: x in ['sin'])
        False

    """

    def apply_test(node):
        if isinstance(node, Function):
            return function_test(str(node.func).lower()) or str(node.func) == "re"
        elif isinstance(node, (Add, Mul, Pow)):
            return all(apply_test(arg) or str(node.func) == "re" for arg in node.args)
        return True

    return apply_test(expression)

is_constant_formatted(s)

Tests weather the input is in standard format for a constant

Examples:

>>> is_constant_formatted('c_4')
True
>>> is_constant_formatted('c_42')
True
>>> is_constant_formatted('c_a')
False
>>> is_constant_formatted('abc')
False
>>> is_constant_formatted('x_3')
False
Source code in src/equation_tree/util/type_check.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def is_constant_formatted(s: str):
    """
    Tests weather the input is in standard format for a constant

    Examples:
        >>> is_constant_formatted('c_4')
        True
        >>> is_constant_formatted('c_42')
        True
        >>> is_constant_formatted('c_a')
        False
        >>> is_constant_formatted('abc')
        False
        >>> is_constant_formatted('x_3')
        False
    """
    pattern = r"^c_\d+$"
    return re.match(pattern, s) is not None

is_numeric(s)

Tests weather the input is a number

Examples:

>>> is_numeric('0')
True
>>> is_numeric('10')
True
>>> is_numeric('hallo')
False
>>> is_numeric('.1')
True
>>> is_numeric('+.1')
False
Source code in src/equation_tree/util/type_check.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def is_numeric(s):
    """
    Tests weather the input is a number

    Examples:
        >>> is_numeric('0')
        True
        >>> is_numeric('10')
        True
        >>> is_numeric('hallo')
        False
        >>> is_numeric('.1')
        True
        >>> is_numeric('+.1')
        False

    """
    if isinstance(s, str) and (not s[0].isnumeric() and s[0] != "."):
        return False
    try:
        float(s)
        return True
    except ValueError:
        return False

is_variable_formatted(s)

Tests weather the input is in standard format for a variable

Examples:

>>> is_variable_formatted('x_1')
True
>>> is_variable_formatted('x_42')
True
>>> is_variable_formatted('x_a')
False
>>> is_variable_formatted('abc')
False
>>> is_variable_formatted('c_3')
False
Source code in src/equation_tree/util/type_check.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def is_variable_formatted(s: str):
    """
    Tests weather the input is in standard format for a variable

    Examples:
        >>> is_variable_formatted('x_1')
        True
        >>> is_variable_formatted('x_42')
        True
        >>> is_variable_formatted('x_a')
        False
        >>> is_variable_formatted('abc')
        False
        >>> is_variable_formatted('c_3')
        False
    """
    pattern = r"^x_\d+$"
    return re.match(pattern, s) is not None

parse_string_list_int(lst)

Example

a = '[1, 2, 3, 4]' parse_string_list_int(a) [1, 2, 3, 4] b = '[10, 2, 3]' parse_string_list_int(b) [10, 2, 3]

Source code in src/equation_tree/util/type_check.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def parse_string_list_int(lst):
    """
    Example:
        >>> a = '[1, 2, 3, 4]'
        >>> parse_string_list_int(a)
        [1, 2, 3, 4]
        >>> b = '[10, 2, 3]'
        >>> parse_string_list_int(b)
        [10, 2, 3]
    """
    res = []
    i = 0
    while i < len(lst) - 1:
        start = i
        end = i
        while is_numeric(lst[i]):
            end += 1
            i += 1
        if start != end:
            i = end
            res.append(int(lst[start:end]))
        i += 1
    return res