{ "cells": [ { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = [0 1 2 3]\n", "x + 5 = [5 6 7 8]\n", "x - 5 = [-5 -4 -3 -2]\n", "x * 2 = [0 2 4 6]\n", "x / 2 = [0. 0.5 1. 1.5]\n", "x // 2 = [0 0 1 1]\n", "-x = [ 0 -1 -2 -3]\n", "x ** 2 = [0 1 4 9]\n", "x % 2 = [0 1 0 1]\n", "[2 3 4 5]\n" ] } ], "source": [ "# Iam very sorry because I had to copy the problems given below directly from the textbook, because it is a redundant process.\n", "# Moreover the concept of numpy library is a never ending process, because there are a ton of different methods and functions.\n", "# I think this is more than enough to learn about numpy.\n", "import numpy as np\n", "x = np.arange(4)\n", "print(\"x =\", x)\n", "print(\"x + 5 =\", x + 5)\n", "print(\"x - 5 =\", x - 5)\n", "print(\"x * 2 =\", x * 2)\n", "print(\"x / 2 =\", x / 2)\n", "print(\"x // 2 =\", x // 2) # floor division\n", "print(\"-x = \", -x)\n", "print(\"x ** 2 = \", x ** 2)\n", "print(\"x % 2 = \", x % 2)\n", "print(np.add(x, 2))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 1 0 1 2]\n", "---------------------------\n", "[5. 5. 2. 1.]\n" ] } ], "source": [ "# Absolute value\n", "x = np.array([-2, -1, 0, 1, 2])\n", "print(abs(x))\n", "print(\"---------------------------\")\n", "x = np.array([3 - 4j, 4 - 3j, 2 + 0j, 0 + 1j])\n", "print(np.abs(x))\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "---------------------------\n", "theta = [0. 1.57079633 3.14159265]\n", "sin(theta) = [0.0000000e+00 1.0000000e+00 1.2246468e-16]\n", "cos(theta) = [ 1.000000e+00 6.123234e-17 -1.000000e+00]\n", "tan(theta) = [ 0.00000000e+00 1.63312394e+16 -1.22464680e-16]\n", "---------------------------\n", "x = [-1, 0, 1]\n", "arcsin(x) = [-1.57079633 0. 1.57079633]\n", "arccos(x) = [3.14159265 1.57079633 0. ]\n", "arctan(x) = [-0.78539816 0. 0.78539816]\n" ] } ], "source": [ "# Trigonometric functions\n", "\n", "theta = np.linspace(0, np.pi, 3)\n", "print(\"---------------------------\")\n", "print(\"theta = \", theta)\n", "print(\"sin(theta) = \", np.sin(theta))\n", "print(\"cos(theta) = \", np.cos(theta))\n", "print(\"tan(theta) = \", np.tan(theta))\n", "print(\"---------------------------\")\n", "\n", "x = [-1, 0, 1]\n", "print(\"x = \", x)\n", "print(\"arcsin(x) = \", np.arcsin(x))\n", "print(\"arccos(x) = \", np.arccos(x))\n", "print(\"arctan(x) = \", np.arctan(x))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x = [1, 2, 3]\n", "e^x = [ 2.71828183 7.3890561 20.08553692]\n", "2^x = [2. 4. 8.]\n", "3^x = [ 3 9 27]\n", "---------------------------\n", "x = [1, 2, 4, 10]\n", "ln(x) = [0. 0.69314718 1.38629436 2.30258509]\n", "log2(x) = [0. 1. 2. 3.32192809]\n", "log10(x) = [0. 0.30103 0.60205999 1. ]\n", "---------------------------\n", "exp(x) - 1 = [0. 0.0010005 0.01005017 0.10517092]\n", "log(1 + x) = [0. 0.0009995 0.00995033 0.09531018]\n" ] } ], "source": [ "# Exponents and logarithms\n", "x = [1, 2, 3]\n", "print(\"x =\", x)\n", "print(\"e^x =\", np.exp(x))\n", "print(\"2^x =\", np.exp2(x))\n", "print(\"3^x =\", np.power(3, x))\n", "print(\"---------------------------\")\n", "\n", "x = [1, 2, 4, 10]\n", "print(\"x =\", x)\n", "print(\"ln(x) =\", np.log(x))\n", "print(\"log2(x) =\", np.log2(x))\n", "print(\"log10(x) =\", np.log10(x))\n", "print(\"---------------------------\")\n", "\n", "x = [0, 0.001, 0.01, 0.1]\n", "print(\"exp(x) - 1 =\", np.expm1(x))\n", "print(\"log(1 + x) =\", np.log1p(x))" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "gamma(x) = [1.0000e+00 2.4000e+01 3.6288e+05]\n", "ln|gamma(x)| = [ 0. 3.17805383 12.80182748]\n", "beta(x, 2) = [0.5 0.03333333 0.00909091]\n", "---------------------------\n", "erf(x) = [0. 0.32862676 0.67780119 0.84270079]\n", "erfc(x) = [1. 0.67137324 0.32219881 0.15729921]\n", "erfinv(x) = [0. 0.27246271 0.73286908 inf]\n" ] } ], "source": [ "# Another excellent source for more specialized and obscure ufuncs is the submodule\n", "# scipy.special. If you want to compute some obscure mathematical function on\n", "# your data, chances are it is implemented in scipy.special.\n", "\n", "from scipy import special\n", "x = [1, 5, 10]\n", "print(\"gamma(x) =\", special.gamma(x))\n", "print(\"ln|gamma(x)| =\", special.gammaln(x))\n", "print(\"beta(x, 2) =\", special.beta(x, 2))\n", "print(\"---------------------------\")\n", "\n", "# Error function (integral of Gaussian)\n", "# its complement, and its inverse\n", "x = np.array([0, 0.3, 0.7, 1.0])\n", "print(\"erf(x) =\", special.erf(x))\n", "print(\"erfc(x) =\", special.erfc(x))\n", "print(\"erfinv(x) =\", special.erfinv(x))\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[ 0. 10. 20. 30. 40.]\n", "---------------------------\n", "[ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]\n" ] } ], "source": [ "# Advanced Ufunc Features\n", "# Specifying output\n", "\n", "x = np.arange(5)\n", "y = np.empty(5)\n", "np.multiply(x, 10, out=y)\n", "print(y)\n", "print(\"---------------------------\")\n", "\n", "y = np.zeros(10)\n", "np.power(2, x, out=y[::2])\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15\n", "---------------------------\n", "120\n", "---------------------------\n", "[ 1 3 6 10 15]\n", "---------------------------\n", "[ 1 2 6 24 120]\n" ] } ], "source": [ "# Aggregates\n", "x = np.arange(1, 6)\n", "print(np.add.reduce(x))\n", "print(\"---------------------------\")\n", "\n", "print(np.multiply.reduce(x))\n", "print(\"---------------------------\")\n", "\n", "print(np.add.accumulate(x))\n", "print(\"---------------------------\")\n", "\n", "print(np.multiply.accumulate(x))" ] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }