A series of experiments with Julia and Python
0

Configure Feed

Select the types of activity you want to include in your feed.

Begin experimenting @rules

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