.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\numpy\demo_operations.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_numpy_demo_operations.py: Operations ============= .. GENERATED FROM PYTHON SOURCE LINES 4-8 .. code-block:: Python import numpy as np .. GENERATED FROM PYTHON SOURCE LINES 10-11 **Arithmetic** .. GENERATED FROM PYTHON SOURCE LINES 11-15 .. code-block:: Python array = np.array([20, 30, 40, 50]) array2 = np.arange(4) array2 - array .. rst-class:: sphx-glr-script-out .. code-block:: none array([-20, -29, -38, -47]) .. GENERATED FROM PYTHON SOURCE LINES 16-18 .. code-block:: Python array2**2 .. rst-class:: sphx-glr-script-out .. code-block:: none array([0, 1, 4, 9]) .. GENERATED FROM PYTHON SOURCE LINES 19-21 .. code-block:: Python 10 * np.sin(array) .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 9.12945251, -9.88031624, 7.4511316 , -2.62374854]) .. GENERATED FROM PYTHON SOURCE LINES 22-24 .. code-block:: Python array < 35 .. rst-class:: sphx-glr-script-out .. code-block:: none array([ True, True, False, False]) .. GENERATED FROM PYTHON SOURCE LINES 25-28 **Matrix operations** elementwise product .. GENERATED FROM PYTHON SOURCE LINES 28-32 .. code-block:: Python A = np.array([[1, 1], [0, 1]]) B = np.array([[2, 0], [3, 4]]) A * B .. rst-class:: sphx-glr-script-out .. code-block:: none array([[2, 0], [0, 4]]) .. GENERATED FROM PYTHON SOURCE LINES 33-34 matrix product .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python A @ B A.dot(B) .. rst-class:: sphx-glr-script-out .. code-block:: none array([[5, 4], [3, 4]]) .. GENERATED FROM PYTHON SOURCE LINES 38-39 **Inline operation** .. GENERATED FROM PYTHON SOURCE LINES 39-45 .. code-block:: Python randVal = np.random.default_rng(1) a = np.ones((2, 3), dtype=int) b = randVal.random((2, 3)) a *= 3 a .. rst-class:: sphx-glr-script-out .. code-block:: none array([[3, 3, 3], [3, 3, 3]]) .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python b += a b .. rst-class:: sphx-glr-script-out .. code-block:: none array([[3.51182162, 3.9504637 , 3.14415961], [3.94864945, 3.31183145, 3.42332645]]) .. GENERATED FROM PYTHON SOURCE LINES 50-51 **Operations on all elements** .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python a = randVal.random((2, 3)) a.sum() .. rst-class:: sphx-glr-script-out .. code-block:: none 3.1057109529998157 .. GENERATED FROM PYTHON SOURCE LINES 55-57 .. code-block:: Python a.min() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.027559113243068367 .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python a.mean() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.5176184921666359 .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python a.max() .. rst-class:: sphx-glr-script-out .. code-block:: none 0.8277025938204418 .. GENERATED FROM PYTHON SOURCE LINES 64-69 .. code-block:: Python b = np.arange(12).reshape(3, 4) b.sum(axis=0) # sum of each column b.min(axis=1) # min of each row b.cumsum(axis=1) # cumulative sum along each row .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0, 1, 3, 6], [ 4, 9, 15, 22], [ 8, 17, 27, 38]]) .. GENERATED FROM PYTHON SOURCE LINES 70-71 **Universal functions** .. GENERATED FROM PYTHON SOURCE LINES 71-74 .. code-block:: Python B = np.arange(3) np.exp(B) .. rst-class:: sphx-glr-script-out .. code-block:: none array([1. , 2.71828183, 7.3890561 ]) .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python np.sqrt(B) .. rst-class:: sphx-glr-script-out .. code-block:: none array([0. , 1. , 1.41421356]) .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python C = np.array([2.0, -1.0, 4.0]) np.add(B, C) .. rst-class:: sphx-glr-script-out .. code-block:: none array([2., 0., 6.]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.019 seconds) .. _sphx_glr_download_11_demos_python_packages_numpy_demo_operations.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_operations.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_operations.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_operations.zip `