use std::fmt;
use std::ops::Mul;
use anyhow::{self, format_err, Context};
use approx;
use derive_builder::Builder;
use itertools::Itertools;
use log;
use ndarray::{Array1, Array2, Axis, Ix2};
use ndarray_linalg::{
eig::Eig,
eigh::Eigh,
types::{Lapack, Scalar},
UPLO,
};
use num_complex::{Complex, ComplexFloat};
use num_traits::{Float, Zero};
use crate::analysis::{
fn_calc_xmat_complex, fn_calc_xmat_real, EigenvalueComparisonMode, Orbit, OrbitIterator,
Overlap, RepAnalysis,
};
use crate::auxiliary::misc::complex_modified_gram_schmidt;
use crate::chartab::chartab_group::CharacterProperties;
use crate::chartab::{DecompositionError, SubspaceDecomposable};
use crate::symmetry::symmetry_element::symmetry_operation::SpecialSymmetryTransformation;
use crate::symmetry::symmetry_group::SymmetryGroupProperties;
use crate::symmetry::symmetry_transformation::{SymmetryTransformable, SymmetryTransformationKind};
use crate::target::tensor::axialvector::AxialVector3;
impl<T> Overlap<T, Ix2> for AxialVector3<T>
where
T: Lapack
+ ComplexFloat<Real = <T as Scalar>::Real>
+ fmt::Debug
+ Mul<<T as ComplexFloat>::Real, Output = T>,
<T as ComplexFloat>::Real: fmt::Debug
+ approx::RelativeEq<<T as ComplexFloat>::Real>
+ approx::AbsDiffEq<Epsilon = <T as Scalar>::Real>,
{
fn complex_symmetric(&self) -> bool {
false
}
fn overlap(
&self,
other: &Self,
metric: Option<&Array2<T>>,
_: Option<&Array2<T>>,
) -> Result<T, anyhow::Error> {
let s_components = Array1::from_iter(self.components.iter().cloned());
let o_components = Array1::from_iter(other.components.iter().cloned());
let ov = if let Some(s) = metric {
s_components
.t()
.mapv(|x| x.conj())
.dot(s)
.dot(&o_components)
} else {
s_components.t().mapv(|x| x.conj()).dot(&o_components)
};
Ok(ov)
}
fn overlap_definition(&self) -> String {
let k = if self.complex_symmetric() { "κ " } else { "" };
format!("⟨{k}v_1|v_2⟩ = [{k}v_1]† g v_2 where g is an optional metric")
}
}
#[derive(Builder, Clone)]
pub struct AxialVector3SymmetryOrbit<'a, G, T>
where
G: SymmetryGroupProperties,
T: ComplexFloat + fmt::Debug + Lapack,
AxialVector3<T>: SymmetryTransformable,
{
group: &'a G,
origin: &'a AxialVector3<T>,
integrality_threshold: <T as ComplexFloat>::Real,
pub(crate) linear_independence_threshold: <T as ComplexFloat>::Real,
symmetry_transformation_kind: SymmetryTransformationKind,
#[builder(setter(skip), default = "None")]
smat: Option<Array2<T>>,
#[builder(setter(skip), default = "None")]
pub(crate) smat_eigvals: Option<Array1<T>>,
#[builder(setter(skip), default = "None")]
xmat: Option<Array2<T>>,
pub(crate) eigenvalue_comparison_mode: EigenvalueComparisonMode,
}
impl<'a, G, T> AxialVector3SymmetryOrbit<'a, G, T>
where
G: SymmetryGroupProperties + Clone,
T: ComplexFloat + fmt::Debug + Lapack,
AxialVector3<T>: SymmetryTransformable,
{
pub fn builder() -> AxialVector3SymmetryOrbitBuilder<'a, G, T> {
AxialVector3SymmetryOrbitBuilder::default()
}
}
impl<'a, G> AxialVector3SymmetryOrbit<'a, G, f64>
where
G: SymmetryGroupProperties,
{
fn_calc_xmat_real!(
pub calc_xmat
);
}
impl<'a, G, T> AxialVector3SymmetryOrbit<'a, G, Complex<T>>
where
G: SymmetryGroupProperties,
T: Float + Scalar<Complex = Complex<T>>,
Complex<T>: ComplexFloat<Real = T> + Scalar<Real = T, Complex = Complex<T>> + Lapack,
AxialVector3<Complex<T>>: SymmetryTransformable + Overlap<Complex<T>, Ix2>,
{
fn_calc_xmat_complex!(
pub calc_xmat
);
}
impl<'a, G, T> Orbit<G, AxialVector3<T>> for AxialVector3SymmetryOrbit<'a, G, T>
where
G: SymmetryGroupProperties,
T: ComplexFloat + fmt::Debug + Lapack,
AxialVector3<T>: SymmetryTransformable,
{
type OrbitIter = OrbitIterator<'a, G, AxialVector3<T>>;
fn group(&self) -> &G {
self.group
}
fn origin(&self) -> &AxialVector3<T> {
self.origin
}
fn iter(&self) -> Self::OrbitIter {
OrbitIterator::new(
self.group,
self.origin,
match self.symmetry_transformation_kind {
SymmetryTransformationKind::Spatial => |op, axvec| {
axvec.sym_transform_spatial(op).with_context(|| {
format!("Unable to apply `{op}` spatially on the origin axial vector")
})
},
SymmetryTransformationKind::SpatialWithSpinTimeReversal => |op, axvec| {
axvec.sym_transform_spatial_with_spintimerev(op).with_context(|| {
format!("Unable to apply `{op}` spatially (with potentially direction-reversing time reversal) on the origin axial vector")
})
},
SymmetryTransformationKind::Spin => |op, axvec| {
axvec.sym_transform_spin(op).with_context(|| {
format!("Unable to apply `{op}` spin-wise on the origin axial vector")
})
},
SymmetryTransformationKind::SpinSpatial => |op, axvec| {
axvec.sym_transform_spin_spatial(op).with_context(|| {
format!("Unable to apply `{op}` spin-spatially on the origin axial vector")
})
},
},
)
}
}
impl<'a, G, T> RepAnalysis<G, AxialVector3<T>, T, Ix2> for AxialVector3SymmetryOrbit<'a, G, T>
where
G: SymmetryGroupProperties,
G::CharTab: SubspaceDecomposable<T>,
T: Lapack
+ ComplexFloat<Real = <T as Scalar>::Real>
+ fmt::Debug
+ Mul<<T as ComplexFloat>::Real, Output = T>,
<T as ComplexFloat>::Real: fmt::Debug
+ Zero
+ approx::RelativeEq<<T as ComplexFloat>::Real>
+ approx::AbsDiffEq<Epsilon = <T as Scalar>::Real>,
AxialVector3<T>: SymmetryTransformable,
{
fn set_smat(&mut self, smat: Array2<T>) {
self.smat = Some(smat)
}
fn smat(&self) -> Option<&Array2<T>> {
self.smat.as_ref()
}
fn xmat(&self) -> &Array2<T> {
self.xmat
.as_ref()
.expect("Orbit overlap orthogonalisation matrix not found.")
}
fn norm_preserving_scalar_map(&self, i: usize) -> Result<fn(T) -> T, anyhow::Error> {
if self.origin.complex_symmetric() {
Err(format_err!("`norm_preserving_scalar_map` is currently not implemented for complex symmetric overlaps."))
} else {
if self
.group
.get_index(i)
.unwrap_or_else(|| panic!("Group operation index `{i}` not found."))
.contains_time_reversal()
{
Ok(ComplexFloat::conj)
} else {
Ok(|x| x)
}
}
}
fn integrality_threshold(&self) -> <T as ComplexFloat>::Real {
self.integrality_threshold
}
fn eigenvalue_comparison_mode(&self) -> &EigenvalueComparisonMode {
&self.eigenvalue_comparison_mode
}
fn analyse_rep(
&self,
) -> Result<
<<G as CharacterProperties>::CharTab as SubspaceDecomposable<T>>::Decomposition,
DecompositionError,
> {
let chis = self
.calc_characters()
.map_err(|err| DecompositionError(err.to_string()))?;
let res = self.group().character_table().reduce_characters(
&chis.iter().map(|(cc, chi)| (cc, *chi)).collect::<Vec<_>>(),
self.integrality_threshold(),
);
res
}
}