Skip to content

Problem with generic reduction formula containing a variable called "lambda" #426

Description

@gdurif

Hi,

I am working on the release of rkeops, and while debugging an example in rkeops documentation, I stumbled across the following problem: a formula that contain a variable named "lambda" generate a compilation error, but if I rename the variable otherwise it works just fine (MWE below).

My guess would be that since lambda is a keyword in Python, it gets interpreted somehow at some point.

Since I am working to finally release rkeops to CRAN, I have no time to investigate this at the moment.

With the following code, I get a compilation error about the formula (error below):

import numpy as np
from pykeops.numpy import Genred

# formula
formula = "Exp(lambda*SqNorm2(x-y))*beta"
variables = ["x=Vi(4)", "y=Vj(4)", "beta=Vj(4)", "lambda=Pm(1)"]

# reduction (compile corresponding operator)
op = Genred(formula, variables, reduction_op="Sum", axis=0)

# data
X = np.random.randn(10, 4)
Y = np.random.randn(15, 4)
Beta = np.random.randn(15, 4)
Lambda = np.array([1])

# compute
res = op(X, Y, Beta, Lambda, backend="CPU")

whereas it works just fine with:

# formula
formula = "Exp(sigma*SqNorm2(x-y))*beta"
variables = ["x=Vi(4)", "y=Vj(4)", "beta=Vj(4)", "sigma=Pm(1)"]

# reduction (compile corresponding operator)
op = Genred(formula, variables, reduction_op="Sum", axis=0)

# data
X = np.random.randn(10, 4)
Y = np.random.randn(15, 4)
Beta = np.random.randn(15, 4)
Lambda = np.array([1])

# compute
res = op(X, Y, Beta, Lambda, backend="CPU")

Error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/xxxx/lib/python3.12/site-packages/pykeops/numpy/generic/generic_red.py", line 313, in __call__
    self.myconv = keops_binder["nvrtc" if tagCPUGPU else "cpp"](
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/xxxx/lib/python3.12/site-packages/keopscore/utils/Cache.py", line 92, in __call__
    obj = self.cls(*args)
          ^^^^^^^^^^^^^^^
  File "/xxxx/lib/python3.12/site-packages/pykeops/common/keops_io/LoadKeOps_cpp.py", line 17, in __init__
    super().__init__(*args, fast_init=fast_init)
  File "/xxxx/lib/python3.12/site-packages/pykeops/common/keops_io/LoadKeOps.py", line 18, in __init__
    self.init(*args)
  File "/xxxx/lib/python3.12/site-packages/pykeops/common/keops_io/LoadKeOps.py", line 127, in init
    ) = get_keops_dll(
        ^^^^^^^^^^^^^^
  File "/xxxx/lib/python3.12/site-packages/keopscore/utils/Cache.py", line 33, in __call__
    self.library[str_id] = self.fun(*args)
                           ^^^^^^^^^^^^^^^
  File "/xxxx/lib/python3.12/site-packages/keopscore/get_keops_dll.py", line 114, in get_keops_dll_impl
    map_reduce_obj = map_reduce_class(red_formula_string, aliases, *args)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/xxxx/lib/python3.12/site-packages/keopscore/mapreduce/cpu/CpuReduc.py", line 18, in __init__
    MapReduce.__init__(self, *args)
  File "/xxxx/lib/python3.12/site-packages/keopscore/mapreduce/MapReduce.py", line 29, in __init__
    self.red_formula = GetReduction(red_formula_string, aliases=aliases)
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/xxxx/lib/python3.12/site-packages/keopscore/formulas/GetReduction.py", line 26, in __new__
    self.check_formula(red_formula_string)
  File "/xxxx/lib/python3.12/site-packages/keopscore/formulas/GetReduction.py", line 44, in check_formula
    parsed = ast.parse(string)
             ^^^^^^^^^^^^^^^^^
  File "/home/drg/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/ast.py", line 52, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<unknown>", line 1
    Sum_Reduction(Exp(lambda * SqNorm2(x-y)) * beta,1)
                                      ^
SyntaxError: invalid syntax

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions