Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save motchy869/bf5d0cda61da35c4d50dd628029ac32a to your computer and use it in GitHub Desktop.
Save motchy869/bf5d0cda61da35c4d50dd628029ac32a to your computer and use it in GitHub Desktop.
Tricks in SymbolicNumericalIntegration package
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tricks in `SymbolicNumericalIntegration` package"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Julia Version 1.8.5\n",
"Commit 17cfb8e65ea (2023-01-08 06:45 UTC)\n",
"Platform Info:\n",
" OS: Linux (x86_64-linux-gnu)\n",
" CPU: 8 × Intel(R) Core(TM) i5-1035G7 CPU @ 1.20GHz\n",
" WORD_SIZE: 64\n",
" LIBM: libopenlibm\n",
" LLVM: libLLVM-13.0.1 (ORCJIT, icelake-client)\n",
" Threads: 1 on 8 virtual cores\n",
"Environment:\n",
" JULIA_NUM_THREADS = \n"
]
}
],
"source": [
"versioninfo()"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"using Symbolics\n",
"using SymbolicNumericIntegration"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
"\\left[\n",
"\\begin{array}{c}\n",
"x \\\\\n",
"y \\\\\n",
"\\end{array}\n",
"\\right]\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"2-element Vector{Num}:\n",
" x\n",
" y"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"@variables x y"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"f1 (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"f1(x) = sin(x)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
"\\sin\\left( x \\right)\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"sin(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"f1(x)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(-cos(x), 0, 0)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"temp = integrate(f1(x), x)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Failure example"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"g1 (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g1(x) = temp[1]"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"`g1` doesn't work at all."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
" - \\cos\\left( x \\right)\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"-cos(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g1(pi)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
" - \\cos\\left( x \\right)\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"-cos(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g1(y)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Successful example"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"g2 (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g2(y) = substitute(g1(x), Dict(x=>y))"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"`g2` works well."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
" - \\cos\\left( x \\right)\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"-cos(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g2(x)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
" - \\cos\\left( y \\right)\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"-cos(y)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g2(y)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g2(pi)"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Matrix version"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"f2(x) = cos(x); f3(x) = exp(2x); f4(x) = sinh(x);"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"A (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"A(x) = [f1(x) f2(x); f3(x) f4(x)]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
"\\left[\n",
"\\begin{array}{cc}\n",
"\\sin\\left( x \\right) & \\cos\\left( x \\right) \\\\\n",
"e^{2 x} & \\sinh\\left( x \\right) \\\\\n",
"\\end{array}\n",
"\\right]\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"2×2 Matrix{Num}:\n",
" sin(x) cos(x)\n",
" exp(2x) sinh(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"A(x)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{Tuple{SymbolicUtils.Symbolic{Real}, Int64, Int64}}:\n",
" (-cos(x), 0, 0) (sin(x), 0, 0)\n",
" ((1//2)*exp(2x), 0, 0) (cosh(x), 0, 0)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Note: `integrate(A(x), x)` somehow fails. Element-wise integration works.\n",
"temp = integrate.(A(x), x)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{SymbolicUtils.Symbolic{Real}}:\n",
" -cos(x) sin(x)\n",
" (1//2)*exp(2x) cosh(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"temp2 = (e -> e[1]).(temp)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"B (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Note: `substitute(temp2, Dict(x => y))` somehow fails. Element-wise substitution works.\n",
"B(y) = (e -> substitute(e, Dict(x => y))).(temp2)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$$ \\begin{equation}\n",
"\\left[\n",
"\\begin{array}{cc}\n",
" - \\cos\\left( x \\right) & \\sin\\left( x \\right) \\\\\n",
"\\frac{1}{2} e^{2 x} & \\cosh\\left( x \\right) \\\\\n",
"\\end{array}\n",
"\\right]\n",
"\\end{equation}\n",
" $$"
],
"text/plain": [
"2×2 Matrix{Num}:\n",
" -cos(x) sin(x)\n",
" (1//2)*exp(2x) cosh(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"B(x)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.8.5",
"language": "julia",
"name": "julia-1.8"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.8.5"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment