use crate::atom::{FractalAtom, Metadata, TagSet};
use crate::field::FractalField;
use crate::resonance::{ResonanceFilter, ResonanceLaw, ResonanceRule};
use crate::signature::FractalSignature;
use num_complex::Complex;
use std::any::Any;
use std::fmt::Debug;
use std::hash::Hash;
use std::ops::{Add, Sub, Neg, Mul};
pub trait VectorSpace:
Sized
+ Clone
+ Neg<Output = Self>
+ Add<Self, Output = Self>
+ Sub<Self, Output = Self>
+ Mul<Complex<f32>, Output = Self> {
fn zero() -> Self;
}
trait _AlgebraicLaws {
fn test_commutativity(&self) -> bool;
fn test_associativity(&self) -> bool;
}
pub trait HasSignature {
fn signature(&self) -> FractalSignature;
}
impl HasSignature for FractalField {
fn signature(&self) -> FractalSignature {
if self.edges.is_empty() {
return FractalSignature {
total_amplitude: 0.0,
average_phase: 0.0,
entropy: 0.0,
edge_count: 0,
depth_range: (0, 0),
};
}
let mut total_amp = 0.0;
let mut total_phase = 0.0;
let mut entropy = 0.0;
let mut min_depth = u32::MAX;
let mut max_depth = 0;
for edge in &self.edges {
let (amp, phase) = edge.data.to_polar();
total_amp += amp;
total_phase += phase;
entropy += amp * phase.abs(); min_depth = min_depth.min(edge.depth);
max_depth = max_depth.max(edge.depth);
}
let count = self.edges.len() as f32;
FractalSignature {
total_amplitude: total_amp,
average_phase: total_phase / count,
entropy,
edge_count: self.edges.len(),
depth_range: (min_depth, max_depth),
}
}
}
pub trait Critic {
fn score(&self, field: &FractalField) -> f32;
fn classify(&self, signature: &FractalSignature) -> String {
if signature.is_symmetric() { "symmetric".to_string() }
else if signature.entropy > 10.0 { "chaotic".to_string() }
else { "structured".to_string() }
}
}
pub struct SymmetryCritic;
impl Critic for SymmetryCritic {
fn score(&self, field: &FractalField) -> f32 {
let sig = field.signature();
let symmetry_bonus = if sig.is_symmetric() { 1.0 } else { 0.0 };
let entropy_penalty = sig.entropy * 0.1;
symmetry_bonus - entropy_penalty
}
}
pub struct EntropyCritic;
impl Critic for EntropyCritic {
fn score(&self, field: &FractalField) -> f32 {
field.signature().entropy
}
}
pub trait Generator {
fn generate(&self) -> Vec<FractalField>;
fn mutate(&self, field: &FractalField) -> Vec<FractalField>;
}
pub trait MutationStrategy {
fn mutate(&self, field: &FractalField) -> FractalField;
}
pub trait FractalClone {
fn clone_box(&self) -> Box<dyn Fractal>;
}
impl<T: 'static + Fractal + Clone> FractalClone for T {
fn clone_box(&self) -> Box<dyn Fractal> { Box::new(self.clone()) }
}
impl Clone for Box<dyn Fractal> {
fn clone(&self) -> Box<dyn Fractal> { self.clone_box() }
}
pub trait Fractal: FractalClone + Debug + Any + 'static {
fn as_any(&self) -> &dyn Any;
fn is_equal(&self, other: &dyn Fractal) -> bool;
fn resonance_law(&self) -> ResonanceLaw;
fn resonance_score(&self) -> f64;
fn tags(&self) -> &TagSet;
fn metadata(&self) -> &Metadata;
fn id(&self) -> &str;
fn children(&self) -> &[Box<dyn Fractal>];
}
impl<T> Fractal for FractalAtom<T>
where
T: Clone + Eq + Hash + Debug + 'static + Into<f64>,
{
fn as_any(&self) -> &dyn Any { self }
fn is_equal(&self, other: &dyn Fractal) -> bool {
other.as_any().downcast_ref::<FractalAtom<T>>() == Some(self)
}
fn resonance_score(&self) -> f64 {
self.value.clone().into()
}
fn resonance_law(&self) -> ResonanceLaw {
ResonanceLaw::Echo }
fn tags(&self) -> &TagSet { &self.tags }
fn metadata(&self) -> &Metadata { &self.metadata }
fn id(&self) -> &str { self.metadata.description.as_deref().unwrap_or("unnamed_atom") }
fn children(&self) -> &[Box<dyn Fractal>] { &[] } }
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Operation {
Union, Difference, Intersection, }
#[derive(Debug, Clone)]
pub enum FractalType {
Mandelbrot(Mandelbrot),
IFS(IFS),
}
impl Fractal for FractalType {
fn as_any(&self) -> &dyn Any { self }
fn children(&self) -> &[Box<dyn Fractal>] {
match self {
FractalType::Mandelbrot(m) => m.children(),
FractalType::IFS(i) => i.children(),
}
}
fn id(&self) -> &str {
match self {
FractalType::Mandelbrot(m) => m.id(),
FractalType::IFS(i) => i.id(),
}
}
fn is_equal(&self, other: &dyn Fractal) -> bool {
if let Some(other_ft) = other.as_any().downcast_ref::<FractalType>() {
match (self, other_ft) {
(FractalType::Mandelbrot(m1), FractalType::Mandelbrot(m2)) => m1 == m2,
(FractalType::IFS(i1), FractalType::IFS(i2)) => i1 == i2,
_ => false, }
} else {
false }
}
fn metadata(&self) -> &Metadata {
match self {
FractalType::Mandelbrot(m) => m.metadata(),
FractalType::IFS(i) => i.metadata(),
}
}
fn resonance_law(&self) -> ResonanceLaw {
match self {
FractalType::Mandelbrot(m) => m.resonance_law(),
FractalType::IFS(i) => i.resonance_law(),
}
}
fn resonance_score(&self) -> f64 {
match self {
FractalType::Mandelbrot(m) => m.resonance_score(),
FractalType::IFS(i) => i.resonance_score(),
}
}
fn tags(&self) -> &TagSet {
match self {
FractalType::Mandelbrot(m) => m.tags(),
FractalType::IFS(i) => i.tags(),
}
}
}
#[derive(Debug, Clone)]
pub struct CollectionMember {
pub fractal: FractalType,
pub operation: Operation,
}
#[derive(Debug, Clone, Default)]
pub struct FractalCollection {
pub members: Vec<CollectionMember>,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Mandelbrot {
pub center_re: f64,
pub center_im: f64,
pub zoom: f64,
pub metadata: Metadata,
pub tags: TagSet,
}
impl Fractal for Mandelbrot {
fn as_any(&self) -> &dyn Any { self }
fn children(&self) -> &[Box<dyn Fractal>] { &[] }
fn id(&self) -> &str { "Mandelbrot" }
fn is_equal(&self, other: &dyn Fractal) -> bool {
other.as_any().downcast_ref::<Mandelbrot>() == Some(self)
}
fn metadata(&self) -> &Metadata { &self.metadata }
fn tags(&self) -> &TagSet { &self.tags }
fn resonance_score(&self) -> f64 {
let chaos = (self.center_re.abs() + self.center_im.abs()).log(self.zoom);
1.0 / chaos.abs() }
fn resonance_law(&self) -> ResonanceLaw { ResonanceLaw::Echo }
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct IFS {
pub transform_count: u32,
pub metadata: Metadata,
pub tags: TagSet,
}
impl Fractal for IFS {
fn as_any(&self) -> &dyn Any { self }
fn children(&self) -> &[Box<dyn Fractal>] { &[] }
fn id(&self) -> &str { "IFS" }
fn is_equal(&self, other: &dyn Fractal) -> bool {
other.as_any().downcast_ref::<IFS>() == Some(self)
}
fn metadata(&self) -> &Metadata { &self.metadata }
fn tags(&self) -> &TagSet { &self.tags }
fn resonance_score(&self) -> f64 { self.transform_count as f64 }
fn resonance_law(&self) -> ResonanceLaw { ResonanceLaw::FractalGrowth }
}
impl Mandelbrot {
pub fn transform_by(&self, other: &Mandelbrot) -> Mandelbrot {
Mandelbrot {
center_re: self.center_re + other.center_re,
center_im: self.center_im + other.center_im,
zoom: self.zoom * other.zoom,
metadata: self.metadata.clone(),
tags: self.tags.clone(),
}
}
}
impl IFS {
pub fn compose_with(&self, other: &IFS) -> IFS {
IFS {
transform_count: self.transform_count * other.transform_count,
metadata: self.metadata.clone(),
tags: self.tags.clone(),
}
}
}
pub fn add_fractals(a: &FractalType, b: &FractalType) -> FractalCollection {
FractalCollection {
members: vec![
CollectionMember { fractal: a.clone(), operation: Operation::Union },
CollectionMember { fractal: b.clone(), operation: Operation::Union },
],
}
}
pub fn divide_fractals(a: &FractalType, b: &FractalType) -> FractalCollection {
FractalCollection {
members: vec![
CollectionMember { fractal: a.clone(), operation: Operation::Union },
CollectionMember { fractal: b.clone(), operation: Operation::Intersection },
],
}
}
pub fn sub_fractals(a: &FractalType, b: &FractalType) -> FractalCollection {
FractalCollection {
members: vec![
CollectionMember { fractal: a.clone(), operation: Operation::Union },
CollectionMember { fractal: b.clone(), operation: Operation::Difference },
],
}
}
pub fn mul_fractals(a: &FractalType, b: &FractalType) -> FractalCollection {
FractalCollection {
members: vec![
CollectionMember { fractal: a.clone(), operation: Operation::Union },
CollectionMember { fractal: b.clone(), operation: Operation::Intersection },
],
}
}
pub trait FractalQuantumSpace {
type SemanticUnit;
fn resonance_depth(&self) -> usize;
fn project(&self, depth: usize) -> Vec<Self::SemanticUnit>;
fn transform(&mut self, rule: &ResonanceRule);
fn filter(&self, filter: &dyn ResonanceFilter) -> Vec<Self::SemanticUnit>;
fn fractal_signature(&self) -> FractalSignature;
}
pub trait SemanticEq {
fn semantic_eq(&self, other: &Self) -> bool;
}