use crate::fractaledge::FractalEdge;
use crate::signature::FractalSignature;
use crate::traits::{Fractal, FractalQuantumSpace};
use std::any::Any;
use std::f32::consts::PI;
pub trait Resonance {
fn as_any(&self) -> &dyn Any;
fn resonance_score(&self) -> f64;
fn resonance_similarity(&self, other: &dyn Resonance) -> f64;
fn is_resonant_with(&self, other: &dyn Resonance) -> bool {
self.resonance_similarity(other) > 0.8
}
fn resonance_law(&self) -> ResonanceLaw;
fn resonance_signature(&self) -> Option<Vec<f64>> {
None
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ResonanceLaw {
Echo,
FractalGrowth,
Harmony,
Dissonance,
EntropyPulse,
Invariant,
ChaoticBeat,
Null,
Other(&'static str),
}
impl std::fmt::Display for ResonanceLaw {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let description = match self {
ResonanceLaw::Echo => "Echo (recursive self-similarity)",
ResonanceLaw::FractalGrowth => "Fractal Growth (self-similar expansion)",
ResonanceLaw::Harmony => "Harmony (constructive alignment)",
ResonanceLaw::Dissonance => "Dissonance (destructive misalignment)",
ResonanceLaw::EntropyPulse => "Entropy Pulse (modulated divergence)",
ResonanceLaw::Invariant => "Invariant (transform-preserving)",
ResonanceLaw::ChaoticBeat => "Chaotic Beat (emergent quasi-periodicity)",
ResonanceLaw::Null => "Null (no resonance)",
ResonanceLaw::Other(label) => return write!(f, "Other ({})", label),
};
write!(f, "{}", description)
}
}
impl<T: Default + 'static> Resonance for T {
fn as_any(&self) -> &dyn Any { self }
fn resonance_score(&self) -> f64 { 0.0 }
fn resonance_similarity(&self, _other: &dyn Resonance) -> f64 { 0.0 }
fn resonance_law(&self) -> ResonanceLaw { ResonanceLaw::Null }
}
impl Resonance for FractalEdge {
fn as_any(&self) -> &dyn Any { self }
fn resonance_score(&self) -> f64 {
let amp = self.amplitude.norm() as f64;
let phase_alignment = (1.0 - (self.phase % (2.0 * PI)).cos() as f64).abs();
amp * phase_alignment
}
fn resonance_similarity(&self, other: &dyn Resonance) -> f64 {
if let Some(other_edge) = other.as_any().downcast_ref::<FractalEdge>() {
let phase_diff = (self.phase - other_edge.phase).abs();
let phase_similarity = 1.0 - (phase_diff % (2.0 * PI)).cos().abs() as f64;
let amp_self = self.amplitude.norm() as f64;
let amp_other = other_edge.amplitude.norm() as f64;
let amp_ratio = if amp_self.max(amp_other) == 0.0 {
1.0
} else {
amp_self.min(amp_other) / amp_self.max(amp_other)
};
0.6 * phase_similarity + 0.4 * amp_ratio
} else {
0.0
}
}
fn resonance_law(&self) -> ResonanceLaw {
let amp = self.amplitude.norm();
let phase = self.phase % (2.0 * PI);
if amp < 0.01 { ResonanceLaw::Null }
else if phase.abs() < 0.1 { ResonanceLaw::Harmony }
else if (phase - PI).abs() < 0.1 { ResonanceLaw::Dissonance }
else if amp > 10.0 { ResonanceLaw::EntropyPulse }
else { ResonanceLaw::Echo }
}
fn resonance_signature(&self) -> Option<Vec<f64>> {
Some(vec![
self.amplitude.re as f64,
self.amplitude.im as f64,
self.phase as f64,
self.location as f64,
])
}
}
pub trait ResonantTransform<T: Resonance> {
fn apply(&self, input: &T) -> T;
fn resonance_delta(&self, input: &T) -> f64 {
let before = input.resonance_score();
let after = self.apply(input).resonance_score();
after - before
}
fn transform_law(&self, input: &T) -> TransformResonanceLaw {
let delta = self.resonance_delta(input);
if delta.abs() < 0.01 { TransformResonanceLaw::Invariant }
else if delta > 0.0 { TransformResonanceLaw::Amplifying }
else { TransformResonanceLaw::Dampening }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TransformResonanceLaw {
Invariant,
Amplifying,
Dampening,
Chaotic,
}
pub struct PhaseShift {
pub delta: f32,
}
impl ResonantTransform<FractalEdge> for PhaseShift {
fn apply(&self, input: &FractalEdge) -> FractalEdge {
FractalEdge { phase: input.phase + self.delta, ..*input }
}
}
pub trait ResonanceFilter {
fn apply(&self, units: &[SemanticUnit]) -> Vec<SemanticUnit>;
fn passes(&self, fractal: &dyn Fractal) -> bool;
fn as_any(&self) -> &dyn Any;
}
#[derive(Clone, Debug)]
pub struct SemanticUnit {
pub label: String,
pub depth: usize,
pub phase: f64,
pub fractal: Box<dyn Fractal>,
}
impl PartialEq for SemanticUnit {
fn eq(&self, other: &Self) -> bool {
self.label == other.label
&& self.depth == other.depth
&& self.phase == other.phase
&& self.fractal.is_equal(&*other.fractal) }
}
impl Eq for SemanticUnit {}
pub struct ResonanceRule {
pub transformation: fn(&SemanticUnit) -> Vec<SemanticUnit>,
}
pub struct SemanticLattice {
pub units: Vec<SemanticUnit>,
pub depth: usize,
}
impl FractalQuantumSpace for SemanticLattice {
type SemanticUnit = SemanticUnit;
fn resonance_depth(&self) -> usize { self.depth }
fn project(&self, depth: usize) -> Vec<Self::SemanticUnit> {
self.units
.iter()
.filter(|u| u.depth == depth)
.cloned()
.collect()
}
fn transform(&mut self, rule: &ResonanceRule) {
let new_units: Vec<SemanticUnit> = self
.units
.iter()
.filter(|u| u.depth == self.depth)
.flat_map(|u| (rule.transformation)(u))
.collect();
self.units.extend(new_units);
self.depth += 1;
}
fn filter(&self, filter: &dyn ResonanceFilter) -> Vec<Self::SemanticUnit> {
filter.apply(&self.units)
}
fn fractal_signature(&self) -> FractalSignature {
FractalSignature::from_units(&self.units)
}
}