Working with vectors using Numpy

1.3. Working with vectors using Numpy#

This simple exercises allow you to connect what we have seen before to some basic coding problem. In particular, we will use the NumPy Python library to perform basic operations with vectors.

import numpy as np

The wavefunction, \(\psi(x)\) can be represented as a column vector, \(|\psi\rangle\) . The complex conjugate of the wavefunction, \(\psi^*(x)\) is also represented by a vector which is the complex conjugate transpose of \(|\psi\rangle\).

\[\begin{align*} \psi(x)&\rightarrow|\psi\rangle\quad\text{column vector}\\ \psi^*(x)&\rightarrow\langle\psi|\quad\text{row vector} \end{align*}\]

The normalization of a wavefunction \(\psi(x)\) is an integral

\[\int\psi^*(x)\psi(x)\;\mathrm{d}x=1.\]

In Dirac notation, it is replaced with a vector equation

\[\langle\psi|\psi\rangle=1.\]

The orthogonality of two wavefunctions, \(\psi_i(x)\) and \(\psi_j(x)\), which, in integral notation, is

\[\int\psi_i^*(x)\psi_j(x)\;\mathrm{d}x=0,\]

becomes, in Dirac notation,

\[\langle\psi_i|\psi_j\rangle=0.\]

Exercise 21

Define two vectors, ϕ1 and ϕ2, with two elements each, that are normalized, in the sense \(\langle\phi_i|\phi_i\rangle=1\), and orthogonal in the sense that \(\langle\phi_i|\phi_j\rangle=0\).

Hint: In numpy a vector v with the two elements 1 and 2 is defined through the command

v=np.array([1,2])
ϕ1 = np.array([]) # replace with your choice for ϕ1
ϕ2 = np.array([]) # replace with your choice for ϕ2 

print(f'ϕ1: {ϕ1}')
print(f'ϕ2: {ϕ2}')

Exercise 22

Show that \(\phi_1\) and \(\phi_2\) are normalized and orthonormal

Hint: Here are reported some useful numpy functions to work with vectors:

  • v.dot(w) - inner product (scalar product) of two vectors v, w

  • v.conj() - complex conjugate of a vector v

  • v.conj().dot(w) - inner product of \(v^\dagger w\)

# Check Normalization

ϕ1_norm = 0 # Replace with vector operation

print(f'<ϕ1|ϕ1> = {ϕ1_norm}')
# Check Orthogonality

ϕ1ϕ2 = 0 # Replace with vector operation

print(f'<ϕ1|ϕ2> = {ϕ1ϕ2}')