{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## numpy universal functions are fast one-sweep operations for arrays\n", "1. They are instances of the numpy.ufunc class. Many of which are implemented in compiled C code.\n", "2. There are over 60 ufuncs in numpy 1.7. We will focus on the math ones here. \n", "3. You can define custom ufunc instances using the frompyfunc factory function" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Math operations\n", "\n", "\n", "- add(x1, x2, /[, out, where, casting, order, …]) Add arguments element-wise. \n", "\n", "\n", "- subtract(x1, x2, /[, out, where, casting, …])对应元素相\n", "Subtract arguments, element-wise.\n", "\n", "\n", "- multiply(x1, x2, /[, out, where, casting, …]) 对应元素相乘\n", "Multiply arguments element-wise.\n", "\n", "\n", "- divide(x1, x2, /[, out, where, casting, …]) 对应元素相除\n", "Returns a true division of the inputs, element-wise.\n", "\n", "\n", "- logaddexp(x1, x2, /[, out, where, casting, …])\n", "Logarithm of the sum of exponentiations of the inputs.\n", "\n", "\n", "- logaddexp2(x1, x2, /[, out, where, casting, …])\n", "Logarithm of the sum of exponentiations of the inputs in base-2.\n", "\n", "\n", "- true_divide(x1, x2, /[, out, where, …])\n", "Returns a true division of the inputs, element-wise.\n", "\n", "\n", "- floor_divide(x1, x2, /[, out, where, …]) 对应元素相除,对返回值取整\n", "Return the largest integer smaller or equal to the division of the inputs. \n", "\n", "\n", "- negative(x, /[, out, where, casting, order, …]) \n", "Numerical negative, element-wise.\n", "\n", "\n", "- positive(x, /[, out, where, casting, order, …])\n", "Numerical positive, element-wise.\n", "\n", "\n", "- power(x1, x2, /[, out, where, casting, …])\n", "First array elements raised to powers from second array, element-wise.\n", "\n", "\n", "- remainder(x1, x2, /[, out, where, casting, …]) 余数\n", "Return element-wise remainder of division.\n", "\n", "\n", "- mod(x1, x2, /[, out, where, casting, order, …])\n", "Return element-wise remainder of division.\n", "\n", "\n", "- fmod(x1, x2, /[, out, where, casting, …])\n", "Return the element-wise remainder of division.\n", "\n", "\n", "- divmod(x1, x2[, out1, out2], / [[, out, …])\n", "Return element-wise quotient and remainder simultaneously.\n", "\n", "\n", "- absolute(x, /[, out, where, casting, order, …])\n", "Calculate the absolute value element-wise.\n", "\n", "\n", "- fabs(x, /[, out, where, casting, order, …])\n", "Compute the absolute values element-wise.\n", "\n", "\n", "- rint(x, /[, out, where, casting, order, …])\n", "Round elements of the array to the nearest integer.\n", "\n", "\n", "- sign(x, /[, out, where, casting, order, …])\n", "Returns an element-wise indication of the sign of a number.\n", "\n", "\n", "- heaviside(x1, x2, /[, out, where, casting, …])\n", "Compute the Heaviside step function.\n", "\n", "\n", "- conj(x, /[, out, where, casting, order, …])\n", "Return the complex conjugate, element-wise.\n", "\n", "\n", "- exp(x, /[, out, where, casting, order, …])\n", "Calculate the exponential of all elements in the input array.\n", "\n", "\n", "- exp2(x, /[, out, where, casting, order, …])\n", "Calculate 2**p for all p in the input array.\n", "\n", "\n", "- log(x, /[, out, where, casting, order, …])\n", "Natural logarithm, element-wise." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "ufunc is super convenient because it does not need loops to things that are very intuitive to us, such as operations element-to-element. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 12, 14])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "#np.arange(start, stop, step)\n", "a = np.arange(10,15,2)\n", "a" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([5, 6, 7])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b= np.array([5,6,7])\n", "b" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([2., 2., 2.])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a/b" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([50, 72, 98])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a*b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 加法\n", "It is addition with broadcasting/sharing. " ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.add(1,4)" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can also do this. \n", "1+4" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]])" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a =np.arange(9).reshape((3,3))\n", "a" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.arange(3)\n", "b" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4],\n", " [ 3, 5, 7],\n", " [ 6, 8, 10]])" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+b" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 2, 4],\n", " [ 3, 5, 7],\n", " [ 6, 8, 10]])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.add(a,b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 减法" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-3" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.subtract(1,4)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-3" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# We can also do this. \n", "1-4" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0],\n", " [3, 3, 3],\n", " [6, 6, 6]])" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# But the magic is the following\n", "a - b" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 0, 0],\n", " [3, 3, 3],\n", " [6, 6, 6]])" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.subtract(a,b)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": { "collapsed": true }, "outputs": [], "source": [ "np.subtract?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 乘法" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2*4" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.multiply(2,4)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8]])" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Remember what a is?\n", "a" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2])" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Remember what b is?\n", "b" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 4],\n", " [ 0, 4, 10],\n", " [ 0, 7, 16]])" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# it seems that the first column of b got shared by the first column of a, and so on. \n", "a*b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 除法 " ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0. , 0.5 , 0.66666667],\n", " [3. , 2. , 1.66666667],\n", " [6. , 3.5 , 2.66666667]])" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a/(b+1)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [default]", "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.3" } }, "nbformat": 4, "nbformat_minor": 2 }