Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Created April 27, 2018 15:32
Show Gist options
  • Save Foadsf/00185794133b22628f87a4a0f1f18933 to your computer and use it in GitHub Desktop.
Save Foadsf/00185794133b22628f87a4a0f1f18933 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"import sympy as sp\n",
"sp.init_printing()\n",
"from sympy.integrals.risch import NonElementaryIntegral"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"x = sp.Symbol('x')"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [],
"source": [
"#y=1/sp.sqrt(x)\n",
"#y=1/x\n",
"#y=(x**2)*sp.sin(1/x)\n",
"#y=1 / sp.cos(x**3)\n",
"#y=1/sp.cos(x**2)\n",
"#y=1/sp.exp(x**2)\n",
"#y=2/sp.cos(x**2)\n",
"#y=sp.exp(-(x-1)**2)\n",
"#y=1/(x**3 + 1)\n",
"#y=sp.sqrt(x+sp.sqrt(x))\n",
"y=sp.log(x)*sp.exp(-x**2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"test to see the correct answer"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAJgAAAAuBAMAAAAmf816AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAmYkQdqvvRN27IjJm\nVM1kwppwAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADWklEQVRIDa1VS0wTURQ9dAr9TEsJcSUuyicR\n44KRgHEFBUoI0QiJxA8LJSbGxE9kocYdXeCGqHRTEozKiPFDooGYsHIBCxaaGG3iRtxQ3RA/iZUK\nBBHrffPmtaV90NL0Ju/dc8899868efNmAG5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{- x^{2}} \\log{\\left (x \\right )}\\, dx$$"
],
"text/plain": [
"⌠ \n",
"⎮ 2 \n",
"⎮ -x \n",
"⎮ ℯ ⋅log(x) dx\n",
"⌡ "
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sp.integrate(y,x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Method 1"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 98,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"isinstance(sp.integrate(y, x, risch=True), NonElementaryIntegral)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Method 2"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not sp.integrate(y, x).has(sp.Integral)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Method 3"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"not isinstance(sp.integrate(y, x), sp.Integral)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"| formula             |analytical |1|2|3|\n",
"|---------------|-------|---|---|---|\n",
"| $ \\frac{1}{\\sqrt{x}} $ | true |$\\times$|$\\checkmark$|$\\checkmark$|\n",
"| $ \\frac{1}{x} $ | true |$\\times$|$\\checkmark$|$\\checkmark$|\n",
"| $ x^2 \\sin\\left(\\frac{1}{x}\\right) $ | true |$\\times$|$\\checkmark$|$\\checkmark$|\n",
"| $ \\frac{1}{\\cos\\left(x^3\\right)} $ | false |$\\checkmark$|$\\checkmark$|$\\checkmark$|\n",
"| $ \\frac{1}{\\cos\\left(x^2\\right)} $ | false |$\\checkmark$|$\\checkmark$|$\\checkmark$|\n",
"| $ \\frac{1}{e^{x^2}} $ | true |$\\checkmark$|$\\checkmark$|$\\checkmark$|\n",
"| $ \\frac{2}{\\cos\\left(x^2\\right)} $ | false |$\\checkmark$|$\\checkmark$|$\\times$|\n",
"|$e^{\\left(-\\left(x-1\\right)^2\\right)}$|true|$\\checkmark$|$\\checkmark$|$\\checkmark$|\n",
"|$\\frac{1}{x^3+1}$|true|$\\times$|$\\checkmark$|$\\checkmark$|\n",
"|$\\sqrt{x+\\sqrt{x}}$|false|$\\checkmark$|$\\checkmark$|$\\checkmark$|\n",
"|$e^{- x^{2}} \\log{\\left (x \\right )}$|false|$\\times$|$\\checkmark$|$\\checkmark$|"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@Foadsf
Copy link
Author

Foadsf commented Apr 27, 2018

The goal of this Jupyter/IPython Notebook is to test different methods suggested here in this StackOverflow post to see how we can check if sympy is able to analytically/symbolically solve a mathematical expression. It seems that the .has(Integral) is the most accurate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment