Skip to content

Instantly share code, notes, and snippets.

@szabba
Created January 1, 2017 01:03
Show Gist options
  • Save szabba/5b1aa886b37860666eb7a962a3731243 to your computer and use it in GitHub Desktop.
Save szabba/5b1aa886b37860666eb7a962a3731243 to your computer and use it in GitHub Desktop.
Quantum Mechanics 101
extern crate num;
extern crate ndarray;
use num::{Zero, Complex};
use ndarray::prelude::*;
use ndarray::linalg::*;
fn main() {
let i = c64(0., 1.);
let h = 6.63e-23.to_complex();
let delta_t = 0.1.to_complex();
let I = Array2::<Complex<f64>>::eye(2);
let H = Array2::<Complex<f64>>::zeros((2, 2)); // FIXME: not an actual Hamiltonian...
let step = I - i / h * H * delta_t;
let mut x = arr1(&[1., 0.]).map(|r| r.to_complex());
for _ in 0..10 {
println!("{}", x);
x = step.dot(&x);
}
println!("{}", x);
}
trait ToComplex: Copy + Zero {
type Real;
fn to_complex(self) -> Complex<Self::Real>;
}
impl ToComplex for f64 {
type Real = f64;
fn to_complex(self) -> Complex<Self::Real> {
Complex {
re: self,
im: Zero::zero(),
}
}
}
fn c64(re: f64, im: f64) -> Complex<f64> {
Complex { re: re, im: im }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment