Interface to NumPy¶
Code author: Daniel G. A. Smith
Section author: Daniel G. A. Smith
Module: Keywords, psi4/psi4/driver/p4util/numpy_helper.py
Converting between the PSI4 Data classes and a NumPy array is easy through various helper functions as detailed in this section. A quick overview NumPy functionality can be found here. In addition, numerous example of hybrid NumPy and Psi4 can be found at the `Psi4Numpy project <https://github.com/dgasmith/psi4numpy`_. Currently only the Matrix and Vector objects support NumPy interfacing. Let us begin with a simple conversion from these objects to a NumPy array:
1 2 3 4 5 6 7 8 9 10
>>> import psi4 >>> import numpy as np # Build the Psi4 data objects >>> mat = psi4.Matrix(3, 3) >>> vec = psi4.Vector(3) # Convert to a NumPy array >>> numpy_mat = np.array(mat) >>> numpy_vec = np.array(vec)
Here the data is copied into new NumPy arrays. NumPy arrays can be converted
back to PSI4 objects using the
>>> new_mat = psi4.Matrix.from_array(mat) >>> new_vec = psi4.Vector.from_array(vec)
Copying the data between NumPy and Psi4 objects can lead to excessive data movement and convoluted code. Here we introduce the idea of “Views” where the same data can be viewed by multiple objects. However, this can lead to very subtle errors if used incorrectly and care needs to be taken when using these views. Views can be created in two ways:
1 2 3 4 5 6 7 8 9
>>> numpy_mat_view = np.asarray(mat) # Access the NumPy object and set all values to 1 through broadcasting >>> numpy_mat_view[:] = 1 >>> print(np.array(mat)) [[ 1. 1. 1.] [ 1. 1. 1.] [ 1. 1. 1.]]
Secondly, these objects have a
.np attribute for easy access to the underlying data:
>>> mat.np[:] = 1
this operation is identical to the above.
The following will lead to reference errors:
psi4.Matrix(3, 3).np. Here, the Python garbage collection deletes the Matrix
object, the view then points to deleted data resulting in the view effectively
reading random data. As a general rule, never assign the
PSI4 Data Objects with Irreps¶
PSI4 data objects natively support multiple irreducible representations which is quite useful for Quantum Chemistry. However, this is not fundamental to NumPy and some work around are required to natively support these operations. Take the following irreped Matrix:
1 2 3 4 5 6 7 8
>>> dim = psi4.Dimension.from_list([1, 2, 3]) >>> irreped_mat = psi4.Matrix("New Matrix", dim, dim) # Create a list of Psi4 arrays >>> list_of_arrays = irreped_mat.to_array() # Or, use the .nph irreped accessor >>> irreped_mat.nph[:] = 1
.nph is the irreped accessor form. If
called on irreped Matrices or Vectors an error will be thrown; however, the
irreped form is always valid for non-irreped matrices.
Array to Matrix¶
A general function that converts PSI4 data objects to NumPy arrays.
array_to_matrix(self, arr, name='New Matrix', dim1=None, dim2=None)¶
Converts a numpy array or list of numpy arrays into a Psi4 Matrix (irreped if list).
- arr (array or list of arrays) – Numpy array or list of arrays to use as the data for a new core.Matrix
- name (str) – Name to give the new core.Matrix
- dim1 (list, tuple, or core.Dimension (optional)) – If a single dense numpy array is given, a dimension can be supplied to apply irreps to this array. Note that this discards all extra information given in the matrix besides the diagonal blocks determined by the passed dimension.
- dim2 – Same as dim1 only if using a Psi4.Dimension object.
matrix – Returns the given Psi4 object
This is a generalized function to convert a NumPy array to a Psi4 object
>>> data = np.random.rand(20) >>> vector = array_to_matrix(data)
1 2 3 4
>>> irrep_data = [np.random.rand(2, 2), np.empty(shape=(0,3)), np.random.rand(4, 4)] >>> matrix = array_to_matrix(irrep_data) >>> print matrix.rowspi().to_tuple() (2, 0, 4)
Matrix to Array¶
A general function that converts NumPy arrays to PSI4 data objects.
_to_array(matrix, copy=True, dense=False)¶
Converts a Psi4 Matrix or Vector to a numpy array. Either copies the data or simply consturcts a view.
array – Returns either a list of np.array’s or the base array depending on options.
np.array or list of of np.array
This is a generalized function to convert a Psi4 object to a NumPy array
1 2 3 4 5
>>> data = psi4.Matrix(3, 3) >>> data._to_array() [[ 0. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]]