Skip to content

Math parsing of fit function #7

@ckoerber

Description

@ckoerber

The function document method uses sympy to generate a latex expression for the fit function and returns None if it fails to generate the expression.

See also from lsqfitgui.util.function import parse_function_expression.

Unfortunately, parsing functions using numpy fail. This is due to

from sympy import S
import numpy as np

x = S("x")
np.exp(x)

fails.

A workaround is overloading the Symbol class

from sympy import Symbol, exp
import numpy as np

class MySymbol(Symbol):

   def exp(self):
       return exp(self)

x = MySymbol("x")
np.exp(x)

which may also work for other functions (maybe there is a more elegant way by looking up matching module attributes).

The main issue, I have however encountered is that, for example

x = MySymbol("x")
y = MySymbol("y")

np.exp(x + y)

fails again because this is now not a MySymbol but rather an Add class.

So, I believe, if it is somehow possible, it would be more effective to replace all the numpy expressions by sympy expressions within the function call. Maybe this possible in the spirit of unittest.mock.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureAn optional addition to the projecthelp wantedExtra attention is needed

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions