Skip to content

symbols

get_symbols(*symbolic_objects)

Returns a dictionary of symbols if no object is given, only returns FitSymbols otherwise returns dict of symbols in the object

Source code in slimfit/symbols.py
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
def get_symbols(*symbolic_objects) -> dict[str, Symbol]:
    """Returns a dictionary of symbols
    if no object is given, only returns FitSymbols
    otherwise returns dict of symbols in the object
    """
    if len(symbolic_objects) == 0:
        return FitSymbol._instances
    else:
        symbols = set()
        for symbolic_object in symbolic_objects:
            if isinstance(symbolic_object, dict):
                symbols = set()
                for entry in itertools.chain(symbolic_object.keys(), symbolic_object.values()):
                    try:
                        # entry is a sympy `Expr` and has `free_symbols` as a set
                        symbols |= entry.free_symbols
                    except TypeError:
                        # rhs is a slimfit `NumExpr` and has a `free_symbols` dictionary
                        symbols |= set(entry.free_symbols.values())
                return
            elif isinstance(symbolic_object, (Expr, MatrixBase)):
                symbols |= symbolic_object.free_symbols
                # return {symbol.name: symbol for symbol in sorted(symbolic_object.free_symbols, key=str)}
            else:
                raise TypeError(f"Invalid type {type(symbolic_object)!r}")

        return {symbol.name: symbol for symbol in sorted(symbols, key=str)}