.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples_generated/python/02_numeric-arrays.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_examples_generated_python_02_numeric-arrays.py: .. _numpy: ======================== Numeric arrays in Python ======================== Examples demonstrating NumPy usage in ASE documentation. Links to NumPy's webpage: * `Numpy and Scipy Documentation`_ * `Numpy user guide `_ .. _Numpy and Scipy Documentation: https://docs.scipy.org/doc/ ASE makes heavy use of an extension to Python called NumPy. The NumPy module defines an ``ndarray`` type that can hold large arrays of uniform multidimensional numeric data. An array is similar to a ``list`` or a ``tuple``, but it is a lot more powerful and efficient. Some examples from everyday ASE-life here ... .. GENERATED FROM PYTHON SOURCE LINES 24-34 .. code-block:: Python import numpy as np a = np.zeros((3, 2)) a[:, 1] = 1.0 a[1] = 2.0 print(a) print(a.shape) print(a.ndim) .. rst-class:: sphx-glr-script-out .. code-block:: none [[0. 1.] [2. 2.] [0. 1.]] (3, 2) 2 .. GENERATED FROM PYTHON SOURCE LINES 35-36 The conventions of numpy's linear algebra package: .. GENERATED FROM PYTHON SOURCE LINES 36-71 .. code-block:: Python # --- Basic array example --- a = np.zeros((3, 2)) a[:, 1] = 1.0 a[1] = 2.0 print('Array a:\n', a) print('Shape:', a.shape) print('Number of dimensions:', a.ndim) # --- Linear algebra example --- # Make a random Hermitian matrix H = np.random.rand(6, 6) + 1.0j * np.random.rand(6, 6) H = H + H.T.conj() # Eigenvalues and eigenvectors eps, U = np.linalg.eigh(H) # Sort eigenvalues and corresponding eigenvectors sorted_indices = eps.real.argsort() eps = eps[sorted_indices] U = U[:, sorted_indices] # Verify diagonalization print( 'Check diagonalization:\n', np.dot(np.dot(U.T.conj(), H), U) - np.diag(eps) ) print('All close?', np.allclose(np.dot(np.dot(U.T.conj(), H), U), np.diag(eps))) # Check eigenvectors individually print( 'Eigenvector check (one column):', np.allclose(np.dot(H, U[:, 3]), eps[3] * U[:, 3]), ) print('Eigenvector check (all):', np.allclose(np.dot(H, U), eps * U)) .. rst-class:: sphx-glr-script-out .. code-block:: none Array a: [[0. 1.] [2. 2.] [0. 1.]] Shape: (3, 2) Number of dimensions: 2 Check diagonalization: [[-1.33226763e-15+1.29518632e-16j 2.90204679e-16-6.75141076e-18j -9.71653234e-17+6.94544756e-17j 3.50809391e-16+2.17073075e-16j 2.20309881e-16+2.22044605e-16j -2.22044605e-16-6.93889390e-18j] [ 4.81634112e-16+4.41046382e-18j -1.11022302e-16+5.48509700e-17j 2.01558667e-16-1.20068608e-16j 7.98217891e-17-5.25424834e-18j -1.94289029e-16+2.63677968e-16j -4.57966998e-16-1.59594560e-16j] [-1.69281747e-16-7.18211613e-17j 1.69072305e-16+1.85945013e-16j -2.77555756e-16-7.29402571e-17j -5.14170378e-17-2.18856179e-16j 5.68989300e-16+2.49800181e-16j -3.85975973e-16-9.54097912e-17j] [ 3.31630074e-16-1.42805675e-16j 1.33682924e-16-1.85761292e-17j -4.44102314e-17+1.69434086e-16j 1.11022302e-16-2.59881910e-17j -3.33066907e-16-9.71445147e-17j 3.74700271e-16-2.49800181e-16j] [ 3.62145347e-16-2.83515989e-16j -8.52743680e-17-7.50416943e-17j 5.46126560e-16-1.72806208e-16j -3.30619207e-16+1.52806908e-16j -1.33226763e-15-1.11022302e-16j 2.01227923e-16-3.60822483e-16j] [ 4.15980619e-17+3.45822421e-17j -6.57122041e-17+7.90349220e-17j -3.25804616e-16+8.74934353e-18j 5.78045211e-17+1.86204031e-16j 2.77555756e-16+1.66533454e-16j -7.10542736e-15-2.22044605e-16j]] All close? True Eigenvector check (one column): True Eigenvector check (all): True .. GENERATED FROM PYTHON SOURCE LINES 72-85 The rules for multiplying 1D arrays with 2D arrays: * 1D arrays and treated like shape (1, N) arrays (row vectors). * left and right multiplications are treated identically. * A length :math:`m` *row* vector can be multiplied with an :math:`n \times m` matrix, producing the same result as if replaced by a matrix with :math:`n` copies of the vector as rows. * A length :math:`n` *column* vector can be multiplied with an :math:`n \times m` matrix, producing the same result as if replaced by a matrix with :math:`m` copies of the vector as columns. Thus, for the arrays below: .. GENERATED FROM PYTHON SOURCE LINES 85-93 .. code-block:: Python # --- 1D vs 2D multiplication rules --- M = np.arange(5 * 6).reshape(5, 6) # A matrix of shape (5, 6) v5 = np.arange(5) + 10 # A vector of length 5 v51 = v5[:, None] # Column vector (5, 1) v6 = np.arange(6) - 12 # A vector of length 6 v16 = v6[None, :] # Row vector (1, 6) .. GENERATED FROM PYTHON SOURCE LINES 94-101 The following identities hold:: v6 * M == v16 * M == M * v6 == M * v16 == M * v16.repeat(5, 0) v51 * M == M * v51 == M * v51.repeat(6, 1) The same rules apply for adding and subtracting 1D arrays to from 2D arrays. .. GENERATED FROM PYTHON SOURCE LINES 101-106 .. code-block:: Python # Identities print('v6 * M == M * v6?', np.allclose(v6 * M, M * v6)) print('v16 * M == M * v16?', np.allclose(v16 * M, M * v16)) print('v51 * M == M * v51?', np.allclose(v51 * M, M * v51)) .. rst-class:: sphx-glr-script-out .. code-block:: none v6 * M == M * v6? True v16 * M == M * v16? True v51 * M == M * v51? True .. _sphx_glr_download_examples_generated_python_02_numeric-arrays.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 02_numeric-arrays.ipynb <02_numeric-arrays.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 02_numeric-arrays.py <02_numeric-arrays.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 02_numeric-arrays.zip <02_numeric-arrays.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_