Skip to content

Instantly share code, notes, and snippets.

@hameerabbasi
Last active June 21, 2018 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hameerabbasi/7301e2895f5540745daa65365b5a8890 to your computer and use it in GitHub Desktop.
Save hameerabbasi/7301e2895f5540745daa65365b5a8890 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import functools\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def wrap(func):\n",
" @functools.wraps(func)\n",
" def wrapped(*args, **kwargs):\n",
" return func(*args, **kwargs)\n",
" \n",
" return wrapped\n",
"\n",
"new_exp = wrap(np.exp)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<ufunc 'exp'>\n",
"<function wrap.<locals>.wrapped at 0x107572840>\n"
]
}
],
"source": [
"print(np.exp)\n",
"print(new_exp)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<ufunc 'exp'>\n",
"<function wrap.<locals>.wrapped at 0x107572840>\n"
]
}
],
"source": [
"print(repr(np.exp))\n",
"print(repr(new_exp))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])\n",
"\n",
"Calculate the exponential of all elements in the input array.\n",
"\n",
"Parameters\n",
"----------\n",
"x : array_like\n",
" Input values.\n",
"out : ndarray, None, or tuple of ndarray and None, optional\n",
" A location into which the result is stored. If provided, it must have\n",
" a shape that the inputs broadcast to. If not provided or `None`,\n",
" a freshly-allocated array is returned. A tuple (possible only as a\n",
" keyword argument) must have length equal to the number of outputs.\n",
"where : array_like, optional\n",
" Values of True indicate to calculate the ufunc at that position, values\n",
" of False indicate to leave the value in the output alone.\n",
"**kwargs\n",
" For other keyword-only arguments, see the\n",
" :ref:`ufunc docs <ufuncs.kwargs>`.\n",
"\n",
"Returns\n",
"-------\n",
"out : ndarray\n",
" Output array, element-wise exponential of `x`.\n",
"\n",
"See Also\n",
"--------\n",
"expm1 : Calculate ``exp(x) - 1`` for all elements in the array.\n",
"exp2 : Calculate ``2**x`` for all elements in the array.\n",
"\n",
"Notes\n",
"-----\n",
"The irrational number ``e`` is also known as Euler's number. It is\n",
"approximately 2.718281, and is the base of the natural logarithm,\n",
"``ln`` (this means that, if :math:`x = \\ln y = \\log_e y`,\n",
"then :math:`e^x = y`. For real input, ``exp(x)`` is always positive.\n",
"\n",
"For complex arguments, ``x = a + ib``, we can write\n",
":math:`e^x = e^a e^{ib}`. The first term, :math:`e^a`, is already\n",
"known (it is the real argument, described above). The second term,\n",
":math:`e^{ib}`, is :math:`\\cos b + i \\sin b`, a function with\n",
"magnitude 1 and a periodic phase.\n",
"\n",
"References\n",
"----------\n",
".. [1] Wikipedia, \"Exponential function\",\n",
" http://en.wikipedia.org/wiki/Exponential_function\n",
".. [2] M. Abramovitz and I. A. Stegun, \"Handbook of Mathematical Functions\n",
" with Formulas, Graphs, and Mathematical Tables,\" Dover, 1964, p. 69,\n",
" http://www.math.sfu.ca/~cbm/aands/page_69.htm\n",
"\n",
"Examples\n",
"--------\n",
"Plot the magnitude and phase of ``exp(x)`` in the complex plane:\n",
"\n",
">>> import matplotlib.pyplot as plt\n",
"\n",
">>> x = np.linspace(-2*np.pi, 2*np.pi, 100)\n",
">>> xx = x + 1j * x[:, np.newaxis] # a + ib over complex plane\n",
">>> out = np.exp(xx)\n",
"\n",
">>> plt.subplot(121)\n",
">>> plt.imshow(np.abs(out),\n",
"... extent=[-2*np.pi, 2*np.pi, -2*np.pi, 2*np.pi], cmap='gray')\n",
">>> plt.title('Magnitude of exp(x)')\n",
"\n",
">>> plt.subplot(122)\n",
">>> plt.imshow(np.angle(out),\n",
"... extent=[-2*np.pi, 2*np.pi, -2*np.pi, 2*np.pi], cmap='hsv')\n",
">>> plt.title('Phase (angle) of exp(x)')\n",
">>> plt.show()\n"
]
}
],
"source": [
"print(np.exp.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"exp(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj])\n",
"\n",
"Calculate the exponential of all elements in the input array.\n",
"\n",
"Parameters\n",
"----------\n",
"x : array_like\n",
" Input values.\n",
"out : ndarray, None, or tuple of ndarray and None, optional\n",
" A location into which the result is stored. If provided, it must have\n",
" a shape that the inputs broadcast to. If not provided or `None`,\n",
" a freshly-allocated array is returned. A tuple (possible only as a\n",
" keyword argument) must have length equal to the number of outputs.\n",
"where : array_like, optional\n",
" Values of True indicate to calculate the ufunc at that position, values\n",
" of False indicate to leave the value in the output alone.\n",
"**kwargs\n",
" For other keyword-only arguments, see the\n",
" :ref:`ufunc docs <ufuncs.kwargs>`.\n",
"\n",
"Returns\n",
"-------\n",
"out : ndarray\n",
" Output array, element-wise exponential of `x`.\n",
"\n",
"See Also\n",
"--------\n",
"expm1 : Calculate ``exp(x) - 1`` for all elements in the array.\n",
"exp2 : Calculate ``2**x`` for all elements in the array.\n",
"\n",
"Notes\n",
"-----\n",
"The irrational number ``e`` is also known as Euler's number. It is\n",
"approximately 2.718281, and is the base of the natural logarithm,\n",
"``ln`` (this means that, if :math:`x = \\ln y = \\log_e y`,\n",
"then :math:`e^x = y`. For real input, ``exp(x)`` is always positive.\n",
"\n",
"For complex arguments, ``x = a + ib``, we can write\n",
":math:`e^x = e^a e^{ib}`. The first term, :math:`e^a`, is already\n",
"known (it is the real argument, described above). The second term,\n",
":math:`e^{ib}`, is :math:`\\cos b + i \\sin b`, a function with\n",
"magnitude 1 and a periodic phase.\n",
"\n",
"References\n",
"----------\n",
".. [1] Wikipedia, \"Exponential function\",\n",
" http://en.wikipedia.org/wiki/Exponential_function\n",
".. [2] M. Abramovitz and I. A. Stegun, \"Handbook of Mathematical Functions\n",
" with Formulas, Graphs, and Mathematical Tables,\" Dover, 1964, p. 69,\n",
" http://www.math.sfu.ca/~cbm/aands/page_69.htm\n",
"\n",
"Examples\n",
"--------\n",
"Plot the magnitude and phase of ``exp(x)`` in the complex plane:\n",
"\n",
">>> import matplotlib.pyplot as plt\n",
"\n",
">>> x = np.linspace(-2*np.pi, 2*np.pi, 100)\n",
">>> xx = x + 1j * x[:, np.newaxis] # a + ib over complex plane\n",
">>> out = np.exp(xx)\n",
"\n",
">>> plt.subplot(121)\n",
">>> plt.imshow(np.abs(out),\n",
"... extent=[-2*np.pi, 2*np.pi, -2*np.pi, 2*np.pi], cmap='gray')\n",
">>> plt.title('Magnitude of exp(x)')\n",
"\n",
">>> plt.subplot(122)\n",
">>> plt.imshow(np.angle(out),\n",
"... extent=[-2*np.pi, 2*np.pi, -2*np.pi, 2*np.pi], cmap='hsv')\n",
">>> plt.title('Phase (angle) of exp(x)')\n",
">>> plt.show()\n"
]
}
],
"source": [
"print(new_exp.__doc__)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"def thing():\n",
" return 'thing'\n",
"\n",
"new_thing = wrap(thing)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<function thing at 0x107572598>\n",
"<function thing at 0x1075729d8>\n"
]
}
],
"source": [
"print(thing)\n",
"print(new_thing)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<function thing at 0x107572598>\n",
"<function thing at 0x1075729d8>\n"
]
}
],
"source": [
"print(repr(thing))\n",
"print(repr(new_thing))"
]
},
{
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment