mabuchilab / QNET

Computer algebra package for quantum mechanics and photonic quantum networks
https://qnet.readthedocs.io/
MIT License
71 stars 23 forks source link

Add an "apply" method to Expressions #67

Closed goerz closed 6 years ago

goerz commented 6 years ago

Expression should have a method apply, where

 expr.apply(func, *args, **kwargs)

is equivalent to

func(expr, *args, **kwargs)

The reasoning behind this is that we will be able to keep the number of methods we implement as small as possibly, instead relying on a more functional (and easier to extend) approach. Since in a symbolic computation we often want to chain a long series of manipulations, having the apply method allows the syntax

new_expr = (
    expr
    .apply(func1, 1, a=0)
    .apply(func2, 2, a=1)
    .apply(func3, 3, a=2))

which is much more readable than

new_expr = func3(
    func2(
        func1(expr, 1, a=0),
        2, a=1),
    3, a=2)

The name apply is still open for discussion.