{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "0b82ab56-4995-480e-93bd-9780abcfe64b", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\frac{a^{2} \\operatorname{asinh}{\\left(\\frac{x}{a} \\right)}}{2} + \\frac{x \\sqrt{a^{2} + x^{2}}}{2}$" ], "text/plain": [ " 2 /x\\ _________\n", "a *asinh|-| / 2 2 \n", " \\a/ x*\\/ a + x \n", "----------- + --------------\n", " 2 2 " ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "using SymPy\n", "using QuadGK\n", "using BenchmarkTools\n", "\n", "@vars x real=true\n", "@vars a positive=true\n", "\n", "f(a, x) = √(a^2 + x^2)\n", "expr_sym = sympy.Integral(f(a, x), x).doit()" ] }, { "cell_type": "code", "execution_count": 2, "id": "5f1a61ac-d3fe-4030-88e2-1b354c7d1f3a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"a^2*asinh(x/a)/2 + x*sqrt(a^2 + x^2)/2\"" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str_sym = string(expr_sym)" ] }, { "cell_type": "code", "execution_count": 3, "id": "294b1c22-1222-416b-b60c-2d612bdf2dda", "metadata": {}, "outputs": [ { "data": { "text/plain": [ ":(F(a, x) = begin\n", " \u001b[90m#= none:1 =#\u001b[39m\n", " (a ^ 2 * asinh(x / a)) / 2 + (x * sqrt(a ^ 2 + x ^ 2)) / 2\n", " end)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "expr_julia = Meta.parse(\"F(a, x) = $expr_sym\")" ] }, { "cell_type": "code", "execution_count": 4, "id": "eb9f3db3-1d73-466f-a8ac-01455d6a98bb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "F (generic function with 1 method)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "@eval $expr_julia" ] }, { "cell_type": "code", "execution_count": 5, "id": "4d900e83-5a34-436f-a380-2ab4ab5aaf69", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 6.352 ms (19995 allocations: 3.58 MiB)\n", " 314.800 μs (5 allocations: 78.27 KiB)\n" ] }, { "data": { "text/plain": [ "true" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f(x) = f(2, x)\n", "F_quadgk(t) = quadgk(f, 0, t)[1]\n", "F(t) = F(2, t) - F(2, 0)\n", "t = 1:10^4\n", "A = @btime F_quadgk.(t)\n", "B = @btime F.(t);\n", "A ≈ B" ] }, { "cell_type": "code", "execution_count": null, "id": "7e533e6c-e255-4265-b7a5-b393c3788fcf", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "jupytext": { "formats": "ipynb,jl:hydrogen" }, "kernelspec": { "display_name": "Julia 1.9.2", "language": "julia", "name": "julia-1.9" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.9.2" } }, "nbformat": 4, "nbformat_minor": 5 }