alpha
Login
or
Join now
utensil.tngl.sh
/
julia-playground
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
A series of experiments with Julia and Python
Star
0
Fork
0
Atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
Overview
Issues
Pulls
Pipelines
Begin experimenting @rules
author
Utensil
date
3 years ago
(May 19, 2023, 2:18 PM UTC)
commit
abb3c65b
abb3c65b3b30de98947a9051a59cd1b6a9a556ec
parent
ed988ec6
ed988ec624ed11941882e98bb14fc2d40b2386aa
+637
1 changed file
Expand all
Collapse all
Unified
Split
GARules.jl
scratch.ipynb
+637
GARules.jl/scratch.ipynb
View file
Reviewed
···
1
1
+
{
2
2
+
"cells": [
3
3
+
{
4
4
+
"cell_type": "code",
5
5
+
"execution_count": 2,
6
6
+
"metadata": {},
7
7
+
"outputs": [],
8
8
+
"source": [
9
9
+
"using GARules"
10
10
+
]
11
11
+
},
12
12
+
{
13
13
+
"cell_type": "code",
14
14
+
"execution_count": 3,
15
15
+
"metadata": {},
16
16
+
"outputs": [
17
17
+
{
18
18
+
"name": "stdout",
19
19
+
"output_type": "stream",
20
20
+
"text": [
21
21
+
"Hello GARules!"
22
22
+
]
23
23
+
}
24
24
+
],
25
25
+
"source": [
26
26
+
"GARules.greet()"
27
27
+
]
28
28
+
},
29
29
+
{
30
30
+
"cell_type": "code",
31
31
+
"execution_count": 4,
32
32
+
"metadata": {},
33
33
+
"outputs": [],
34
34
+
"source": [
35
35
+
"using SymbolicUtils"
36
36
+
]
37
37
+
},
38
38
+
{
39
39
+
"cell_type": "code",
40
40
+
"execution_count": 5,
41
41
+
"metadata": {},
42
42
+
"outputs": [
43
43
+
{
44
44
+
"data": {
45
45
+
"text/plain": [
46
46
+
"(w, z, α, β)"
47
47
+
]
48
48
+
},
49
49
+
"metadata": {},
50
50
+
"output_type": "display_data"
51
51
+
}
52
52
+
],
53
53
+
"source": [
54
54
+
"@syms w z α::Real β::Real"
55
55
+
]
56
56
+
},
57
57
+
{
58
58
+
"cell_type": "code",
59
59
+
"execution_count": 6,
60
60
+
"metadata": {},
61
61
+
"outputs": [],
62
62
+
"source": [
63
63
+
"using SymbolicUtils: symtype"
64
64
+
]
65
65
+
},
66
66
+
{
67
67
+
"cell_type": "code",
68
68
+
"execution_count": 7,
69
69
+
"metadata": {},
70
70
+
"outputs": [
71
71
+
{
72
72
+
"data": {
73
73
+
"text/plain": [
74
74
+
"(Number, Number, Real, Real)"
75
75
+
]
76
76
+
},
77
77
+
"metadata": {},
78
78
+
"output_type": "display_data"
79
79
+
}
80
80
+
],
81
81
+
"source": [
82
82
+
"symtype(w), symtype(z), symtype(α), symtype(β)"
83
83
+
]
84
84
+
},
85
85
+
{
86
86
+
"cell_type": "code",
87
87
+
"execution_count": 8,
88
88
+
"metadata": {},
89
89
+
"outputs": [
90
90
+
{
91
91
+
"name": "stdout",
92
92
+
"output_type": "stream",
93
93
+
"text": [
94
94
+
"w isa Number = false\n",
95
95
+
"α isa Real = false\n"
96
96
+
]
97
97
+
},
98
98
+
{
99
99
+
"data": {
100
100
+
"text/plain": [
101
101
+
"false"
102
102
+
]
103
103
+
},
104
104
+
"metadata": {},
105
105
+
"output_type": "display_data"
106
106
+
}
107
107
+
],
108
108
+
"source": [
109
109
+
"@show w isa Number\n",
110
110
+
"@show α isa Real"
111
111
+
]
112
112
+
},
113
113
+
{
114
114
+
"cell_type": "code",
115
115
+
"execution_count": 9,
116
116
+
"metadata": {},
117
117
+
"outputs": [
118
118
+
{
119
119
+
"name": "stdout",
120
120
+
"output_type": "stream",
121
121
+
"text": [
122
122
+
"typeof(w) = SymbolicUtils.BasicSymbolic{Number}\n",
123
123
+
"typeof(α) = SymbolicUtils.BasicSymbolic{Real}\n"
124
124
+
]
125
125
+
},
126
126
+
{
127
127
+
"data": {
128
128
+
"text/plain": [
129
129
+
"SymbolicUtils.BasicSymbolic{Real}"
130
130
+
]
131
131
+
},
132
132
+
"metadata": {},
133
133
+
"output_type": "display_data"
134
134
+
}
135
135
+
],
136
136
+
"source": [
137
137
+
"@show typeof(w)\n",
138
138
+
"@show typeof(α)"
139
139
+
]
140
140
+
},
141
141
+
{
142
142
+
"cell_type": "code",
143
143
+
"execution_count": 10,
144
144
+
"metadata": {},
145
145
+
"outputs": [
146
146
+
{
147
147
+
"data": {
148
148
+
"text/plain": [
149
149
+
"α*(sin(w)^2) + β*(sin(w)^2) + α*(cos(z)^2) + β*(cos(z)^2)"
150
150
+
]
151
151
+
},
152
152
+
"metadata": {},
153
153
+
"output_type": "display_data"
154
154
+
}
155
155
+
],
156
156
+
"source": [
157
157
+
"expr1 = α*sin(w)^2 + β*cos(z)^2\n",
158
158
+
"expr2 = α*cos(z)^2 + β*sin(w)^2\n",
159
159
+
"\n",
160
160
+
"expr1 + expr2"
161
161
+
]
162
162
+
},
163
163
+
{
164
164
+
"cell_type": "code",
165
165
+
"execution_count": 11,
166
166
+
"metadata": {},
167
167
+
"outputs": [
168
168
+
{
169
169
+
"data": {
170
170
+
"text/plain": [
171
171
+
"α + 5w - 3z"
172
172
+
]
173
173
+
},
174
174
+
"metadata": {},
175
175
+
"output_type": "display_data"
176
176
+
}
177
177
+
],
178
178
+
"source": [
179
179
+
"2w + 3w - 3z + α"
180
180
+
]
181
181
+
},
182
182
+
{
183
183
+
"cell_type": "code",
184
184
+
"execution_count": 12,
185
185
+
"metadata": {},
186
186
+
"outputs": [
187
187
+
{
188
188
+
"data": {
189
189
+
"text/plain": [
190
190
+
"(w + z)*(α + β)"
191
191
+
]
192
192
+
},
193
193
+
"metadata": {},
194
194
+
"output_type": "display_data"
195
195
+
}
196
196
+
],
197
197
+
"source": [
198
198
+
"(z + w)*(α + β)"
199
199
+
]
200
200
+
},
201
201
+
{
202
202
+
"cell_type": "code",
203
203
+
"execution_count": 13,
204
204
+
"metadata": {},
205
205
+
"outputs": [
206
206
+
{
207
207
+
"data": {
208
208
+
"text/plain": [
209
209
+
"(4//3) + α + 5w + (1//2)*β - 3z"
210
210
+
]
211
211
+
},
212
212
+
"metadata": {},
213
213
+
"output_type": "display_data"
214
214
+
}
215
215
+
],
216
216
+
"source": [
217
217
+
"5 + 2w - 3z + α - (β + 5//3) + 3w - 2 + 3//2 * β"
218
218
+
]
219
219
+
},
220
220
+
{
221
221
+
"cell_type": "code",
222
222
+
"execution_count": 14,
223
223
+
"metadata": {},
224
224
+
"outputs": [
225
225
+
{
226
226
+
"data": {
227
227
+
"text/plain": [
228
228
+
"(f, g)"
229
229
+
]
230
230
+
},
231
231
+
"metadata": {},
232
232
+
"output_type": "display_data"
233
233
+
}
234
234
+
],
235
235
+
"source": [
236
236
+
"using SymbolicUtils\n",
237
237
+
"@syms f(x) g(x::Real, y::Real)::Real"
238
238
+
]
239
239
+
},
240
240
+
{
241
241
+
"cell_type": "code",
242
242
+
"execution_count": 15,
243
243
+
"metadata": {},
244
244
+
"outputs": [
245
245
+
{
246
246
+
"data": {
247
247
+
"text/plain": [
248
248
+
"sin(w) + f(z) + g(1, α)"
249
249
+
]
250
250
+
},
251
251
+
"metadata": {},
252
252
+
"output_type": "display_data"
253
253
+
}
254
254
+
],
255
255
+
"source": [
256
256
+
"f(z) + g(1, α) + sin(w)"
257
257
+
]
258
258
+
},
259
259
+
{
260
260
+
"cell_type": "code",
261
261
+
"execution_count": 16,
262
262
+
"metadata": {},
263
263
+
"outputs": [
264
264
+
{
265
265
+
"ename": "ErrorException",
266
266
+
"evalue": "Tuple{Int64, Number} is not a subtype of Tuple{Real, Real}.",
267
267
+
"output_type": "error",
268
268
+
"traceback": [
269
269
+
"Tuple{Int64, Number} is not a subtype of Tuple{Real, Real}.\n",
270
270
+
"\n",
271
271
+
"Stacktrace:\n",
272
272
+
" [1] error(s::String)\n",
273
273
+
" @ Base ./error.jl:35\n",
274
274
+
" [2] promote_symtype(::SymbolicUtils.BasicSymbolic{SymbolicUtils.FnType{Tuple{Real, Real}, Real}}, ::Type, ::Type)\n",
275
275
+
" @ SymbolicUtils ~/.julia/packages/SymbolicUtils/H684H/src/types.jl:901\n",
276
276
+
" [3] (::SymbolicUtils.BasicSymbolic{SymbolicUtils.FnType{Tuple{Real, Real}, Real}})(::Int64, ::Vararg{Any})\n",
277
277
+
" @ SymbolicUtils ~/.julia/packages/SymbolicUtils/H684H/src/types.jl:879\n",
278
278
+
" [4] top-level scope\n",
279
279
+
" @ /workspaces/julia-playground/GARules.jl/scratch.ipynb:1"
280
280
+
]
281
281
+
}
282
282
+
],
283
283
+
"source": [
284
284
+
"g(1, z)"
285
285
+
]
286
286
+
},
287
287
+
{
288
288
+
"cell_type": "code",
289
289
+
"execution_count": 17,
290
290
+
"metadata": {},
291
291
+
"outputs": [
292
292
+
{
293
293
+
"data": {
294
294
+
"text/plain": [
295
295
+
"g(2//5, g(1, β))"
296
296
+
]
297
297
+
},
298
298
+
"metadata": {},
299
299
+
"output_type": "display_data"
300
300
+
}
301
301
+
],
302
302
+
"source": [
303
303
+
"g(2//5, g(1, β))"
304
304
+
]
305
305
+
},
306
306
+
{
307
307
+
"cell_type": "code",
308
308
+
"execution_count": 18,
309
309
+
"metadata": {},
310
310
+
"outputs": [
311
311
+
{
312
312
+
"data": {
313
313
+
"text/plain": [
314
314
+
"2(α + β + 2w + cos(z)^2 + sin(z)^2 - 1)"
315
315
+
]
316
316
+
},
317
317
+
"metadata": {},
318
318
+
"output_type": "display_data"
319
319
+
}
320
320
+
],
321
321
+
"source": [
322
322
+
"ex1 = 2 * (w+w+α+β + sin(z)^2 + cos(z)^2 - 1)"
323
323
+
]
324
324
+
},
325
325
+
{
326
326
+
"cell_type": "code",
327
327
+
"execution_count": 19,
328
328
+
"metadata": {},
329
329
+
"outputs": [
330
330
+
{
331
331
+
"data": {
332
332
+
"text/plain": [
333
333
+
"2(α + β + 2w)"
334
334
+
]
335
335
+
},
336
336
+
"metadata": {},
337
337
+
"output_type": "display_data"
338
338
+
}
339
339
+
],
340
340
+
"source": [
341
341
+
"ex1 |> simplify"
342
342
+
]
343
343
+
},
344
344
+
{
345
345
+
"cell_type": "code",
346
346
+
"execution_count": 20,
347
347
+
"metadata": {},
348
348
+
"outputs": [
349
349
+
{
350
350
+
"data": {
351
351
+
"text/plain": [
352
352
+
"false"
353
353
+
]
354
354
+
},
355
355
+
"metadata": {},
356
356
+
"output_type": "display_data"
357
357
+
}
358
358
+
],
359
359
+
"source": [
360
360
+
"SymbolicUtils.show_simplified[] = false"
361
361
+
]
362
362
+
},
363
363
+
{
364
364
+
"cell_type": "code",
365
365
+
"execution_count": 21,
366
366
+
"metadata": {},
367
367
+
"outputs": [
368
368
+
{
369
369
+
"data": {
370
370
+
"text/plain": [
371
371
+
"α + 5w - 3z"
372
372
+
]
373
373
+
},
374
374
+
"metadata": {},
375
375
+
"output_type": "display_data"
376
376
+
}
377
377
+
],
378
378
+
"source": [
379
379
+
"2w + 3w - 3z + α"
380
380
+
]
381
381
+
},
382
382
+
{
383
383
+
"cell_type": "code",
384
384
+
"execution_count": 22,
385
385
+
"metadata": {},
386
386
+
"outputs": [
387
387
+
{
388
388
+
"data": {
389
389
+
"text/plain": [
390
390
+
"(w + z)*(α + β)"
391
391
+
]
392
392
+
},
393
393
+
"metadata": {},
394
394
+
"output_type": "display_data"
395
395
+
}
396
396
+
],
397
397
+
"source": [
398
398
+
"(z + w)*(α + β)"
399
399
+
]
400
400
+
},
401
401
+
{
402
402
+
"cell_type": "code",
403
403
+
"execution_count": 23,
404
404
+
"metadata": {},
405
405
+
"outputs": [
406
406
+
{
407
407
+
"data": {
408
408
+
"text/plain": [
409
409
+
"sin(2 * ~x) => (2 * sin(~x)) * cos(~x)"
410
410
+
]
411
411
+
},
412
412
+
"metadata": {},
413
413
+
"output_type": "display_data"
414
414
+
}
415
415
+
],
416
416
+
"source": [
417
417
+
"r1 = @rule sin(2(~x)) => 2sin(~x)*cos(~x)"
418
418
+
]
419
419
+
},
420
420
+
{
421
421
+
"cell_type": "code",
422
422
+
"execution_count": 25,
423
423
+
"metadata": {},
424
424
+
"outputs": [
425
425
+
{
426
426
+
"data": {
427
427
+
"text/plain": [
428
428
+
"2cos(z)*sin(z)"
429
429
+
]
430
430
+
},
431
431
+
"metadata": {},
432
432
+
"output_type": "display_data"
433
433
+
}
434
434
+
],
435
435
+
"source": [
436
436
+
"r1(sin(2z))"
437
437
+
]
438
438
+
},
439
439
+
{
440
440
+
"cell_type": "code",
441
441
+
"execution_count": 26,
442
442
+
"metadata": {},
443
443
+
"outputs": [
444
444
+
{
445
445
+
"data": {
446
446
+
"text/plain": [
447
447
+
"true"
448
448
+
]
449
449
+
},
450
450
+
"metadata": {},
451
451
+
"output_type": "display_data"
452
452
+
}
453
453
+
],
454
454
+
"source": [
455
455
+
"r1(sin(3z)) === nothing"
456
456
+
]
457
457
+
},
458
458
+
{
459
459
+
"cell_type": "code",
460
460
+
"execution_count": 27,
461
461
+
"metadata": {},
462
462
+
"outputs": [
463
463
+
{
464
464
+
"data": {
465
465
+
"text/plain": [
466
466
+
"2cos(w - z)*sin(w - z)"
467
467
+
]
468
468
+
},
469
469
+
"metadata": {},
470
470
+
"output_type": "display_data"
471
471
+
}
472
472
+
],
473
473
+
"source": [
474
474
+
"r1(sin(2*(w-z)))"
475
475
+
]
476
476
+
},
477
477
+
{
478
478
+
"cell_type": "code",
479
479
+
"execution_count": 28,
480
480
+
"metadata": {},
481
481
+
"outputs": [
482
482
+
{
483
483
+
"data": {
484
484
+
"text/plain": [
485
485
+
"true"
486
486
+
]
487
487
+
},
488
488
+
"metadata": {},
489
489
+
"output_type": "display_data"
490
490
+
}
491
491
+
],
492
492
+
"source": [
493
493
+
"r1(sin(2*(w+z)*(α+β))) === nothing"
494
494
+
]
495
495
+
},
496
496
+
{
497
497
+
"cell_type": "code",
498
498
+
"execution_count": 29,
499
499
+
"metadata": {},
500
500
+
"outputs": [
501
501
+
{
502
502
+
"data": {
503
503
+
"text/plain": [
504
504
+
"sin(~x + ~y) => sin(~x) * cos(~y) + cos(~x) * sin(~y)"
505
505
+
]
506
506
+
},
507
507
+
"metadata": {},
508
508
+
"output_type": "display_data"
509
509
+
}
510
510
+
],
511
511
+
"source": [
512
512
+
"r2 = @rule sin(~x + ~y) => sin(~x)*cos(~y) + cos(~x)*sin(~y)"
513
513
+
]
514
514
+
},
515
515
+
{
516
516
+
"cell_type": "code",
517
517
+
"execution_count": 31,
518
518
+
"metadata": {},
519
519
+
"outputs": [
520
520
+
{
521
521
+
"data": {
522
522
+
"text/plain": [
523
523
+
"sin(α)*cos(β) + cos(α)*sin(β)"
524
524
+
]
525
525
+
},
526
526
+
"metadata": {},
527
527
+
"output_type": "display_data"
528
528
+
}
529
529
+
],
530
530
+
"source": [
531
531
+
"r2(sin(α+β))"
532
532
+
]
533
533
+
},
534
534
+
{
535
535
+
"cell_type": "code",
536
536
+
"execution_count": 34,
537
537
+
"metadata": {},
538
538
+
"outputs": [
539
539
+
{
540
540
+
"data": {
541
541
+
"text/plain": [
542
542
+
"(x, y, z)"
543
543
+
]
544
544
+
},
545
545
+
"metadata": {},
546
546
+
"output_type": "display_data"
547
547
+
}
548
548
+
],
549
549
+
"source": [
550
550
+
"@syms x y z"
551
551
+
]
552
552
+
},
553
553
+
{
554
554
+
"cell_type": "code",
555
555
+
"execution_count": 36,
556
556
+
"metadata": {},
557
557
+
"outputs": [
558
558
+
{
559
559
+
"ename": "LoadError",
560
560
+
"evalue": "LoadError: UndefVarError: @slots not defined\nin expression starting at /workspaces/julia-playground/GARules.jl/scratch.ipynb:1",
561
561
+
"output_type": "error",
562
562
+
"traceback": [
563
563
+
"LoadError: UndefVarError: @slots not defined\n",
564
564
+
"in expression starting at /workspaces/julia-playground/GARules.jl/scratch.ipynb:1\n"
565
565
+
]
566
566
+
}
567
567
+
],
568
568
+
"source": [
569
569
+
"r3 = @slots x y z @rule x(y + z) --> x*y + x*z"
570
570
+
]
571
571
+
},
572
572
+
{
573
573
+
"cell_type": "code",
574
574
+
"execution_count": 41,
575
575
+
"metadata": {},
576
576
+
"outputs": [
577
577
+
{
578
578
+
"data": {
579
579
+
"text/plain": [
580
580
+
"+(~(~xs)) => ~(~xs)"
581
581
+
]
582
582
+
},
583
583
+
"metadata": {},
584
584
+
"output_type": "display_data"
585
585
+
}
586
586
+
],
587
587
+
"source": [
588
588
+
"r4 = @rule +(~~xs) => ~~xs"
589
589
+
]
590
590
+
},
591
591
+
{
592
592
+
"cell_type": "code",
593
593
+
"execution_count": 42,
594
594
+
"metadata": {},
595
595
+
"outputs": [
596
596
+
{
597
597
+
"data": {
598
598
+
"text/plain": [
599
599
+
"3-element view(::Vector{Any}, 1:3) with eltype Any:\n",
600
600
+
" x\n",
601
601
+
" y\n",
602
602
+
" z"
603
603
+
]
604
604
+
},
605
605
+
"metadata": {},
606
606
+
"output_type": "display_data"
607
607
+
}
608
608
+
],
609
609
+
"source": [
610
610
+
"r4(x + y + z)"
611
611
+
]
612
612
+
},
613
613
+
{
614
614
+
"cell_type": "code",
615
615
+
"execution_count": null,
616
616
+
"metadata": {},
617
617
+
"outputs": [],
618
618
+
"source": []
619
619
+
}
620
620
+
],
621
621
+
"metadata": {
622
622
+
"kernelspec": {
623
623
+
"display_name": "Julia 1.8.4",
624
624
+
"language": "julia",
625
625
+
"name": "julia-1.8"
626
626
+
},
627
627
+
"language_info": {
628
628
+
"file_extension": ".jl",
629
629
+
"mimetype": "application/julia",
630
630
+
"name": "julia",
631
631
+
"version": "1.8.4"
632
632
+
},
633
633
+
"orig_nbformat": 4
634
634
+
},
635
635
+
"nbformat": 4,
636
636
+
"nbformat_minor": 2
637
637
+
}