Skip to content

Logic expression #6

Open
Open
@dalek-who

Description

@dalek-who

What a nice tools! However, current codes has trouble dealing with logic expression with more than 2 terms, for example:

x, y, z = sympy.symbols("x,y,z")
expr =x  | y | z 
model = SymPyModule(expressions=[expr])
terms = {
        "x": torch.randint(0, 2, (10,)).bool(),
        "y": torch.randint(0, 2, (10,)).bool(),
        "z": torch.randint(0, 2, (10,)).bool(),
}
result = model(**terms)

Then it will raise an error:

TypeError: logical_or() takes 2 positional arguments but 3 were given

Here's the solution: modify

sympy.And: torch.logical_and,
sympy.Or: torch.logical_or,

to

sympy.And: _reduce(torch.logical_and),
sympy.Or: _reduce(torch.logical_or),

, which is the same as mul and add.

Also, besides the modification above, I suggest two more improvements:

  1. make this function public
    def _reduce(fn):
    def fn_(*args):
    return ft.reduce(fn, args)
    return fn_
  2. modify
    _func_lookup = co.ChainMap(_global_func_lookup, extra_funcs)

    to
_func_lookup = co.ChainMap(extra_funcs, _global_func_lookup)

so that users can overload default lookup tables.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions