There is a project in which you need to have a lot of relatively simple mathematical transformations. Also for each transformation it is necessary to create and store the inverse transformation. I want to have a tool that would automatically build the inverse transform. For example, so that the 5 * x + 10 transformation is written as

transformation = Transformation(MultiplyBy(5), Add(10)) transformed_4 = transformation(4) 

but at the same time, so that the inverse transformation could be done like this

 restored_4 = transformation.restore(transformed_4) assert restored_4 == 4 # не будем учитывать пока ошибки округления 

Perhaps there are ready-made solutions for such a task?

  • Nonlinear transformation too. All are reversible in the definition workspace. - griddic

2 answers 2

The sympy module allows you to work with mathematical expressions in an analytical form (with symbols).

Example:

 from sympy import symbols, solve, simplify, factor from sympy.parsing.sympy_parser import ( parse_expr, standard_transformations, implicit_multiplication, implicit_multiplication_application) trf = ( standard_transformations + (implicit_multiplication_application,)) x, y, z, t = symbols('xyz t') formula_str = "5*x + 10" formula = parse_expr(formula_str, transformations=trf) print(formula) #5*x + 10 # подставим 4 вместо `x`: res = formula.subs(x, 4) print(res) #30 # решение уравнения: `5*x + 10 = 0` print(solve(formula)) #[-2] # решение уравнения: `5*x + 10 = 30` или `5*x + 10 - 30 = 0` print(solve(formula_str + f" - {res}")) #[4] 

Other examples:

 In [94]: [r for r in solve('x**4-(x-2)**2') if r.is_real] Out[94]: [-2, 1] In [95]: simplify('sin(x)**2 + cos(x)**2') Out[95]: 1 In [96]: factor('x**4-(x-2)**2') Out[96]: (x - 1)*(x + 2)*(x**2 - x + 2) In [100]: simplify('sin(x)**2 / (1 - sin(x)**2)') Out[100]: tan(x)**2 

derivatives and integrals:

 In [128]: from sympy import diff, integrate In [129]: diff('log(x**2)') Out[129]: 2/x In [130]: integrate('2/x') Out[130]: 2*log(x) 
  • one
    Cool stuff! After it has been solved, it remains only to filter the answers in accordance with the initial domain of definition. - griddic
  • @griddic, yes, sympy is a powerful module. You can do interesting things ...;) - MaxU

Isn't it easier to write the class yourself and work with it, since the needs are not so great? For example, so (x = 5,2x + 20):

 class Transform(): def __init__(self,x=0,mul=1,add=0): self.x = x self.mul = mul self.add = add def calculate (self): self.res = self.x*self.mul + self.add return self.res def reverse (self): self.reverse = (self.res-self.add)/self.mul return self.reverse def expose (self): return "{}x{}+{}".format(self.x, self.mul, self.add) eq1 = Transform(5, 2, 20) print(eq1.calculate()) print(eq1.expose()) print(eq1.reverse()) 

We get:

 30 5x2+20 5.0 
  • one
    and what to write a class for each formula that cannot be represented as a linear function? - MaxU
  • one
    @MaxU In the first approximation, I decided not to consider non-linear functions to be “relatively simple mathematical transformations”. But, here the owner is the master. - strawdog
  • In general, yes, if the transformations were only linear, then, indeed, everything could be reduced to a single formula. But nonlinear transformations are also present. - griddic