spcl / dace

DaCe - Data Centric Parallel Programming
http://dace.is/fast
BSD 3-Clause "New" or "Revised" License
497 stars 129 forks source link

Ambiguity for symbols in the sympy layer #1417

Open BenWeber42 opened 1 year ago

BenWeber42 commented 1 year ago

In our sympy layer, there exist multiple implementations to query symbols in a symbolic expression. They give vastly different results for various expressions:

from ast import parse
from dace.symbolic import pystr_to_symbolic, symbols_in_ast, symlist

pystr_to_symbolic("x").free_symbols
# -> {x}
symbols_in_ast(parse("x"))
# -> ['x']
symlist(pystr_to_symbolic("x"))
# -> {'x': x}

pystr_to_symbolic("a[i]").free_symbols
# -> {i}
symbols_in_ast(parse("a[i]"))
# -> ['a', 'i']
symlist(pystr_to_symbolic("a[i]"))
# -> {'i': i}

pystr_to_symbolic("a.b").free_symbols
# -> {a.b}
symbols_in_ast(parse("a.b"))
# -> ['a']
symlist(pystr_to_symbolic("a.b"))
# -> {'a': a, 'b': b}

pystr_to_symbolic("a.b[i]").free_symbols
# -> {a.b(i)}
symbols_in_ast(parse("a.b[i]"))
# -> ['i', 'a']
symlist(pystr_to_symbolic("a.b[i]"))
# -> {'a': a, 'i': i}

We should:

  1. clearly define different concepts around symbolic expressions (symbolic variables, array variables, attribute variables or similar)
  2. precisely specify the return values of these functions w.r.t. to these definitions.

For consistent specifications there must be consistent return values.

tbennun commented 1 year ago

see #1423

tbennun commented 1 year ago

updated results:

In [2]: from ast import parse
   ...: from dace.symbolic import pystr_to_symbolic, names_in_ast, symlist

In [3]: pystr_to_symbolic("a[i]").free_symbols
Out[3]: {i}

In [4]: names_in_ast(parse("a[i]"))
Out[4]: ['a', 'i']

In [5]: symlist(pystr_to_symbolic("a[i]"))
Out[5]: {'i': i}

In [6]: pystr_to_symbolic("a.b").free_symbols
Out[6]: {a.b}

In [7]: names_in_ast(parse("a.b"))
Out[7]: ['a']

In [8]: symlist(pystr_to_symbolic("a.b"))
Out[8]: {'a.b': a.b}

In [9]: pystr_to_symbolic("a.b[i]").free_symbols
Out[9]: {a.b(i)}

In [10]: names_in_ast(parse("a.b[i]"))
Out[10]: ['a', 'i']

In [11]: symlist(pystr_to_symbolic("a.b[i]"))
Out[11]: {'a.b(i)': a.b(i)}