Spaces:
Running
Running
Better explain pure-Julia function
Browse files- examples/pysr_demo.ipynb +23 -9
examples/pysr_demo.ipynb
CHANGED
|
@@ -846,7 +846,7 @@
|
|
| 846 |
"source": [
|
| 847 |
"jl.eval(\"\"\"\n",
|
| 848 |
"function p(i::T) where T\n",
|
| 849 |
-
" if
|
| 850 |
" return T(Primes.prime(round(Int, i)))\n",
|
| 851 |
" else\n",
|
| 852 |
" return T(NaN)\n",
|
|
@@ -861,12 +861,26 @@
|
|
| 861 |
"metadata": {},
|
| 862 |
"source": [
|
| 863 |
"\n",
|
| 864 |
-
"We have created a
|
| 865 |
"`p` first checks whether the input is between 0.5 and 1000.\n",
|
| 866 |
"If out-of-bounds, it returns `NaN`.\n",
|
| 867 |
-
"If in-bounds, it rounds it to the nearest integer,
|
| 868 |
"converts it to the same type as input.\n",
|
| 869 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 870 |
"Next, let's generate a list of primes for our test dataset.\n",
|
| 871 |
"Since we are using PyJulia, we can just call `p` directly to do this:\n"
|
| 872 |
]
|
|
@@ -1357,14 +1371,14 @@
|
|
| 1357 |
"name": "pysr_demo.ipynb",
|
| 1358 |
"provenance": []
|
| 1359 |
},
|
| 1360 |
-
"
|
| 1361 |
-
"name": "python"
|
| 1362 |
-
},
|
| 1363 |
"kernelspec": {
|
| 1364 |
-
"
|
| 1365 |
-
"
|
| 1366 |
},
|
| 1367 |
-
"
|
|
|
|
|
|
|
| 1368 |
},
|
| 1369 |
"nbformat": 4,
|
| 1370 |
"nbformat_minor": 0
|
|
|
|
| 846 |
"source": [
|
| 847 |
"jl.eval(\"\"\"\n",
|
| 848 |
"function p(i::T) where T\n",
|
| 849 |
+
" if 0.5 < i < 1000\n",
|
| 850 |
" return T(Primes.prime(round(Int, i)))\n",
|
| 851 |
" else\n",
|
| 852 |
" return T(NaN)\n",
|
|
|
|
| 861 |
"metadata": {},
|
| 862 |
"source": [
|
| 863 |
"\n",
|
| 864 |
+
"We have created a function `p`, which takes a number `i` of type `T` (e.g., `T=Float64`).\n",
|
| 865 |
"`p` first checks whether the input is between 0.5 and 1000.\n",
|
| 866 |
"If out-of-bounds, it returns `NaN`.\n",
|
| 867 |
+
"If in-bounds, it rounds it to the nearest integer, computes the corresponding prime number, and then\n",
|
| 868 |
"converts it to the same type as input.\n",
|
| 869 |
"\n",
|
| 870 |
+
"The equivalent function in Python would be:\n",
|
| 871 |
+
"\n",
|
| 872 |
+
"```python\n",
|
| 873 |
+
"import sympy\n",
|
| 874 |
+
"\n",
|
| 875 |
+
"def p(i):\n",
|
| 876 |
+
" if 0.5 < i < 1000:\n",
|
| 877 |
+
" return float(sympy.prime(int(round(i))))\n",
|
| 878 |
+
" else:\n",
|
| 879 |
+
" return float(\"nan\")\n",
|
| 880 |
+
"```\n",
|
| 881 |
+
"\n",
|
| 882 |
+
"(However, note that this version assumes 64-bit float input, rather than any input type `T`)\n",
|
| 883 |
+
"\n",
|
| 884 |
"Next, let's generate a list of primes for our test dataset.\n",
|
| 885 |
"Since we are using PyJulia, we can just call `p` directly to do this:\n"
|
| 886 |
]
|
|
|
|
| 1371 |
"name": "pysr_demo.ipynb",
|
| 1372 |
"provenance": []
|
| 1373 |
},
|
| 1374 |
+
"gpuClass": "standard",
|
|
|
|
|
|
|
| 1375 |
"kernelspec": {
|
| 1376 |
+
"display_name": "Python 3",
|
| 1377 |
+
"name": "python3"
|
| 1378 |
},
|
| 1379 |
+
"language_info": {
|
| 1380 |
+
"name": "python"
|
| 1381 |
+
}
|
| 1382 |
},
|
| 1383 |
"nbformat": 4,
|
| 1384 |
"nbformat_minor": 0
|