You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/manual/rewrite.md
+14-5Lines changed: 14 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -35,15 +35,24 @@ r1(sin(3z))
35
35
nothing
36
36
```
37
37
38
-
Slot variable (matcher) is not necessary a single variable
39
-
38
+
Slot variable (matcher) is not necessary a single variable:
40
39
```jldoctest rewrite
41
40
r1(sin(2*(w-z)))
42
41
43
42
# output
44
43
2cos(w - z)*sin(w - z)
45
44
```
46
45
46
+
And can also match a function:
47
+
```julia
48
+
r =@rule (~f)(z+1) =>~f
49
+
50
+
r(sin(z+1))
51
+
52
+
# output
53
+
sin (generic function with 20 methods)
54
+
55
+
```
47
56
Rules are of course not limited to single slot variable
48
57
49
58
```jldoctest rewrite
@@ -55,7 +64,7 @@ r2(sin(α+β))
55
64
sin(β)*cos(α) + cos(β)*sin(α)
56
65
```
57
66
58
-
Let's say you want to catch the coefficients of a second degree polynomial in z. You can do that with:
67
+
Now let's say you want to catch the coefficients of a second degree polynomial in z. You can do that with:
59
68
```jldoctest rewrite
60
69
c2d = @rule ~a + ~b*z + ~c*z^2 => (~a, ~b, ~c)
61
70
@@ -70,15 +79,15 @@ c2d(3 + 2z + z^2)
70
79
#output
71
80
nothing
72
81
```
73
-
the rule is not applied. This is because in the input polynomial there isnt a multiplication in front of the `z^2`. If you want to solve this you must use **defslot variables**, with syntax `~!a`:
82
+
the rule is not applied. This is because in the input polynomial there isn't a multiplication in front of the `z^2`. For this you can use **defslot variables**, with syntax `~!a`:
74
83
```jldoctest rewrite
75
84
c2d = @rule ~!a + ~!b*z + ~!c*z^2 => (~a, ~b, ~c)
76
85
77
86
c2d(3 + 2z + z^2)
78
87
# output
79
88
(3, 2, 1)
80
89
```
81
-
They work like normal slot variables, but if not present take a default value depending on the operation they are in, in the above example `~b = 1`. Currently defslot variables can be defined in:
90
+
They work like normal slot variables, but if they are not present they take a default value depending on the operation they are in, in the above example `~b = 1`. Currently defslot variables can be defined in:
0 commit comments