Skip to content

Instantly share code, notes, and snippets.

@saimn
Created September 29, 2020 22:35
Show Gist options
  • Save saimn/f79197fe45bb96d90f6ca3fa54910ac2 to your computer and use it in GitHub Desktop.
Save saimn/f79197fe45bb96d90f6ca3fa54910ac2 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Moffat2D"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sympy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"amplitude, x_0, y_0, alpha, gamma, x, y = sympy.symbols('amplitude x_0 y_0 alpha gamma x y')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}$"
],
"text/plain": [
"((x - x_0)**2 + (y - y_0)**2)/gamma**2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rr_gg = ((x - x_0) ** 2 + (y - y_0) ** 2) / gamma ** 2\n",
"\n",
"rr_gg"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha}$"
],
"text/plain": [
"amplitude*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"moffat = amplitude * (1 + rr_gg) ** (-alpha)\n",
"moffat"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"d_A = sympy.diff(moffat, amplitude)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha}$"
],
"text/plain": [
"(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_A"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_A == (1 + rr_gg) ** (-alpha)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\log{\\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}} \\right)}$"
],
"text/plain": [
"-amplitude*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)*log(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_alpha = sympy.diff(moffat, alpha)\n",
"d_alpha"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_alpha == -amplitude * d_A * sympy.log(1 + rr_gg)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2 \\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}\\right)}{\\gamma^{3} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"2*alpha*amplitude*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)*((x - x_0)**2 + (y - y_0)**2)/(gamma**3*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2))"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_gamma = sympy.diff(moffat, gamma)\n",
"d_gamma"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# OLD VERSION\n",
"d_gamma == 2 * amplitude * alpha * d_A * (rr_gg / (gamma * (1 + rr_gg)))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2 \\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}\\right)}{\\gamma^{3} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"2*alpha*amplitude*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)*((x - x_0)**2 + (y - y_0)**2)/(gamma**3*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2))"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 * amplitude * alpha * d_A * (rr_gg / (gamma * (1 + rr_gg)))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NEW VERSION\n",
"d_gamma == (2 * amplitude * alpha * d_A * rr_gg / (gamma ** 3 * (1 + rr_gg)))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2 \\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}\\right)}{\\gamma^{5} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"2*alpha*amplitude*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)*((x - x_0)**2 + (y - y_0)**2)/(gamma**5*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2))"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(2 * amplitude * alpha * d_A * rr_gg / (gamma ** 3 * (1 + rr_gg)))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - \\frac{\\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(- 2 x + 2 x_{0}\\right)}{\\gamma^{2} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"-alpha*amplitude*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)*(-2*x + 2*x_0)/(gamma**2*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2))"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_x_0 = sympy.diff(moffat, x_0)\n",
"d_x_0"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2 \\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(x - x_{0}\\right)}{\\gamma^{2} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2} + \\left(y - y_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"2*alpha*amplitude*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2)**(-alpha)*(x - x_0)/(gamma**2*(1 + ((x - x_0)**2 + (y - y_0)**2)/gamma**2))"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(2 * amplitude * alpha * d_A * (x - x_0) / (gamma ** 2 * (1 + rr_gg)))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_x_0 == (2 * amplitude * alpha * d_A * (x - x_0) / (gamma ** 2 * (1 + rr_gg)))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sympy is confused by 2 * (x - x_0) versus - (2 * x_0 - 2 * x)\n",
"d_x_0 == ( - amplitude * alpha * d_A * (2 * x_0 - 2 * x) / (gamma ** 2 * (1 + rr_gg)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Moffat1D"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sympy"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"amplitude, x_0, alpha, gamma, x = sympy.symbols('amplitude x_0 alpha gamma x')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha}$"
],
"text/plain": [
"amplitude*(1 + (x - x_0)**2/gamma**2)**(-alpha)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"moffat1d = amplitude * (1 + ((x - x_0) / gamma) ** 2) ** (-alpha)\n",
"moffat1d"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"fac = (1 + (x - x_0) ** 2 / gamma ** 2)\n",
"d_A = fac ** (-alpha)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - \\frac{\\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(- 2 x + 2 x_{0}\\right)}{\\gamma^{2} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"-alpha*amplitude*(1 + (x - x_0)**2/gamma**2)**(-alpha)*(-2*x + 2*x_0)/(gamma**2*(1 + (x - x_0)**2/gamma**2))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_x_0 = sympy.diff(moffat1d, x_0)\n",
"d_x_0"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NEW\n",
"d_x_0 == (2 * amplitude * alpha * (x - x_0) * d_A / (fac * gamma ** 2))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2 \\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(x - x_{0}\\right)}{\\gamma^{2} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"2*alpha*amplitude*(1 + (x - x_0)**2/gamma**2)**(-alpha)*(x - x_0)/(gamma**2*(1 + (x - x_0)**2/gamma**2))"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(2 * amplitude * alpha * (x - x_0) * d_A / (fac * gamma ** 2))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# OLD\n",
"d_x_0 == (-amplitude * alpha * d_A * (-2 * x + 2 * x_0) / (gamma ** 2 * d_A ** alpha))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - \\frac{\\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(- 2 x + 2 x_{0}\\right) \\left(\\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha}\\right)^{- \\alpha}}{\\gamma^{2}}$"
],
"text/plain": [
"-alpha*amplitude*(1 + (x - x_0)**2/gamma**2)**(-alpha)*(-2*x + 2*x_0)*((1 + (x - x_0)**2/gamma**2)**(-alpha))**(-alpha)/gamma**2"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(-amplitude * alpha * d_A * (-2 * x + 2 * x_0) / (gamma ** 2 * d_A ** alpha))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{2 \\alpha amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\left(x - x_{0}\\right)^{2}}{\\gamma^{3} \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)}$"
],
"text/plain": [
"2*alpha*amplitude*(1 + (x - x_0)**2/gamma**2)**(-alpha)*(x - x_0)**2/(gamma**3*(1 + (x - x_0)**2/gamma**2))"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_gamma = sympy.diff(moffat1d, gamma)\n",
"d_gamma"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# OLD\n",
"d_gamma == (2 * amplitude * alpha * d_A * (x - x_0) ** 2 / (gamma ** 3 * d_A ** alpha))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NEW \n",
"d_gamma == (2 * amplitude * alpha * (x - x_0) ** 2 * d_A / (fac * gamma ** 3))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - amplitude \\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}}\\right)^{- \\alpha} \\log{\\left(1 + \\frac{\\left(x - x_{0}\\right)^{2}}{\\gamma^{2}} \\right)}$"
],
"text/plain": [
"-amplitude*(1 + (x - x_0)**2/gamma**2)**(-alpha)*log(1 + (x - x_0)**2/gamma**2)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d_alpha = sympy.diff(moffat1d, alpha)\n",
"d_alpha"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# OLD\n",
"d_alpha == -amplitude * d_A * sympy.log(1 + (x - x_0) ** 2 / gamma ** 2)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NEW \n",
"d_alpha == -amplitude * d_A * sympy.log(fac)"
]
},
{
"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.8.1"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment