Last active
April 14, 2020 08:21
-
-
Save sminez/ea49ae4edaeff4b5bf2d23e9144f6471 to your computer and use it in GitHub Desktop.
checking inverses with arthroprod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! See https://github.com/sminez/arthroprod for the main library | |
//! This program was written at the following hash: 05237efac5f49abb935a0dd62cbbd88b31759c5d | |
//! No further crates are required | |
#[macro_use] | |
extern crate arthroprod; | |
use std::collections::HashSet; | |
use arthroprod::algebra::operations::{full, AR}; | |
use arthroprod::algebra::types::{Form, MultiVector}; | |
/// Helper for forming products and simplifying the resulting terms | |
fn simplified_product(m: &MultiVector, f: impl Fn(&MultiVector) -> MultiVector) -> MultiVector { | |
let mut res: MultiVector = full(&m.clone(), &f(&m.clone())); | |
res.simplify(); | |
res | |
} | |
/// display the alpha values contained within a multivector | |
fn simple_form_rep(m: &MultiVector) -> String { | |
let forms: HashSet<Form> = m.as_alphas().iter().map(|a| a.form()).collect(); | |
let mut fs: Vec<&Form> = forms.iter().collect(); | |
fs.sort(); | |
fs[1..fs.len()] | |
.iter() | |
.fold(format!("a{}", fs[0]), |acc, a| format!("{} a{}", acc, a)) | |
} | |
/// Print a MultiVector if it is a pure scalar | |
fn print_if_scalar(name: &str, m: MultiVector) { | |
if m.is_scalar() { | |
println!("{}: {}", name, m) | |
} | |
} | |
fn main() { | |
// MultiVector product functions | |
let squared = |m: &MultiVector| simplified_product(m, |n| n.clone()); | |
let phi = |m: &MultiVector| simplified_product(m, |n| n.hermitian()); | |
let ddaggered = |m: &MultiVector| simplified_product(m, |n| n.double_dagger()); | |
let vdm_scalar = |m: &MultiVector| simplified_product(&phi(&m), |n| n.diamond()); | |
let time_like = vec![term!(), term!(0), term!(1 2 3), term!(0 1 2 3)]; | |
let space_like = vec![ | |
mvec![term!(1), term!(2), term!(3)], // space :: vectors | |
mvec![term!(2 3), term!(3 1), term!(1 2)], // magnetic field :: space-space bivectors | |
mvec![term!(0 1), term!(0 2), term!(0 3)], // electric field :: space-time bivectors | |
mvec![term!(0 2 3), term!(0 3 1), term!(0 1 2)], // spin :: time-space-space trivectors | |
]; | |
for t in time_like.iter() { | |
for s in space_like.iter() { | |
let mvec = s.clone() + t.clone(); | |
println!("MultiVector = {{ {} }}", simple_form_rep(&mvec)); | |
print_if_scalar("squared", squared(&mvec)); | |
print_if_scalar("phi", phi(&mvec)); | |
print_if_scalar("double_dagger", ddaggered(&mvec)); | |
print_if_scalar("VdM_scalar", vdm_scalar(&mvec)); | |
println!("\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This is the original python version of this program | |
See https://github.com/sminez/arpy for the main python library | |
""" | |
from itertools import product | |
from arpy import B, T, A, E, MultiVector, Alpha | |
from arpy.algebra.operations import full, hermitian, diamond, project | |
p = MultiVector("p") | |
t = MultiVector("0") | |
h = MultiVector("123") | |
q = MultiVector("0123") | |
four_things = product([p, t, h, q], [B, T, A, E]) | |
alpha_p = Alpha("p") | |
def simple_rep(m): | |
alphas = set(t._alpha for t in m._terms) | |
return sorted(list(alphas)) | |
def scalar_value(m): | |
return str(m)[11 : str(m).index(")") - 1] | |
def double_dagger(m): | |
fields = project(m, 2) | |
return (fields + fields - m).cancel_terms() | |
def squared(m): | |
"""psi squared""" | |
return full(m, m).cancel_terms() | |
def psi_psi_dagger(m): | |
"""psi psi^{!}""" | |
return full(m, hermitian(m)).cancel_terms() | |
def psi_psi_ddagger(m): | |
"""psi psi^{!!}""" | |
return full(m, double_dagger(m)).cancel_terms() | |
def vdm_scalar(m): | |
"""vdM scalar""" | |
phi = full(m, hermitian(m)).cancel_terms() | |
return full(phi, diamond(phi)).cancel_terms() | |
for time_like, space_like in four_things: | |
mvec = time_like + space_like | |
# check simplest to most complex | |
for f in [squared, psi_psi_dagger, psi_psi_ddagger, vdm_scalar]: | |
res = f(mvec) | |
alphas = simple_rep(res) | |
if len(alphas) == 1 and alphas[0] == alpha_p: | |
print(f"{f.__doc__}: {simple_rep(mvec)}\n {scalar_value(res)}") | |
print() | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment