@@ -18,15 +18,15 @@ symbol, *x* in this case.
18
18
19
19
>>> x = pmbl.var(" x" )
20
20
>>> x
21
- Variable('x')
21
+ Variable(name= 'x')
22
22
23
23
Next, let's create an expression using *x *:
24
24
25
25
.. doctest ::
26
26
27
27
>>> u = (x+ 1 )** 5
28
28
>>> u
29
- Power(Sum((Variable('x'), 1)), 5)
29
+ Power(base= Sum(children= (Variable(name= 'x'), 1)), exponent= 5)
30
30
>>> print (u)
31
31
(x + 1)**5
32
32
@@ -69,20 +69,20 @@ You can also easily define your own objects to use inside an expression:
69
69
70
70
.. doctest ::
71
71
72
- >>> from pymbolic.primitives import Expression
73
- >>> class FancyOperator ( Expression ):
74
- ... def __init__ ( self , operand ):
75
- ... self .operand = operand
76
- ...
77
- ... def __getinitargs__ ( self ):
78
- ... return ( self . operand,)
72
+ >>> from pymbolic.primitives import Expression, augment_expression_dataclass
73
+ >>> from dataclasses import dataclass
74
+ >>>
75
+ >>> @ augment_expression_dataclass
76
+ ... @ dataclass( frozen = True )
77
+ ... class FancyOperator ( Expression ):
78
+ ... operand: Expression
79
79
...
80
80
... mapper_method = " map_fancy_operator"
81
81
...
82
82
>>> u
83
- Power(Sum((Variable('x'), 1)), 5)
83
+ Power(base= Sum(children= (Variable(name= 'x'), 1)), exponent= 5)
84
84
>>> 17 * FancyOperator(u)
85
- Product((17, FancyOperator(Power(Sum((..., 1)), 5))))
85
+ Product(children= (17, FancyOperator(operand= Power(base= Sum(children= (..., 1)), exponent= 5))))
86
86
87
87
As a final example, we can now derive from *MyMapper * to multiply all
88
88
*FancyOperator * instances by 2.
@@ -93,8 +93,8 @@ As a final example, we can now derive from *MyMapper* to multiply all
93
93
... def map_fancy_operator (self , expr ):
94
94
... return 2 * FancyOperator(self .rec(expr.operand))
95
95
...
96
- >>> MyMapper2()(FancyOperator(u))
97
- Product((2, FancyOperator(Power(Product((..., 1)), 5))))
96
+ >>> MyMapper2()(FancyOperator(operand = u))
97
+ Product(children= (2, FancyOperator(operand= Power(base= Product(children= (..., 1)), exponent= 5))))
98
98
99
99
.. automodule :: pymbolic
100
100
0 commit comments