fractal_algebra 0.3.8

A library for fractal algebra experimentation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Defines the core traits and architectural patterns for the library.
//!
//! This module establishes the central abstractions for the entire system, creating a
//! clear separation between low-level algebra and high-level fractal composition.
//!
//! ## Core Concepts
//!
//! - **`VectorSpace` Trait**: Defines the basic mathematical properties for types that can be
//!   treated as vectors (e.g., `FractalField`). It relies on standard operator traits
//!   like `std::ops::Add` for idiomatic usage.
//!
//! - **`Fractal` Trait**: The primary trait for all high-level fractal objects. It provides a
//!   common, dynamically-dispatchable interface for things like `Mandelbrot` and `IFS`.
//!
//! - **`FractalType` Enum**: A type-safe enum that wraps all concrete `Fractal` implementations.
//!   This is the main type used for storing and combining different kinds of fractals.
//!
//! - **CSG Functions**: A set of functions (`add_fractals`, `sub_fractals`, etc.) that perform
//!   Constructive Solid Geometry-like operations on `FractalType`s, resulting in a
//!   `FractalCollection` that represents the combined object tree.

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};

// --- Low-Level Algebra ---

/// A trait for types that behave like elements in a vector space.
///
/// By requiring the standard operator traits as bounds, we ensure that any type
/// implementing `VectorSpace` can be used with `+`, `-`, and `*` in a predictable way.
pub trait VectorSpace:
    Sized
    + Clone
    + Neg<Output = Self>
    + Add<Self, Output = Self>
    + Sub<Self, Output = Self>
    + Mul<Complex<f32>, Output = Self> // For scalar multiplication
{
    /// Returns the additive identity element (the "zero" vector).
    fn zero() -> Self;
}

/// A private, unused trait for defining algebraic law tests. Can be removed or implemented.
trait _AlgebraicLaws {
    fn test_commutativity(&self) -> bool;
    fn test_associativity(&self) -> bool;
}

// --- Generative System Traits ---

/// A trait for objects that can produce a condensed, descriptive signature.
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(); // crude entropy proxy
            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),
        }
    }
}

/// A trait for critics that evaluate a `FractalField` and assign it a score.
pub trait Critic {
    /// Scores a field, typically based on its signature. Higher is usually better.
    fn score(&self, field: &FractalField) -> f32;

    /// Classifies a field into a category like "symmetric" or "chaotic".
    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() }
    }
}

/// A simple critic that rewards symmetry and penalizes high entropy.
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
    }
}

/// A simple critic that rewards high entropy.
pub struct EntropyCritic;
impl Critic for EntropyCritic {
    fn score(&self, field: &FractalField) -> f32 {
        field.signature().entropy
    }
}

/// A trait for generators that produce new `FractalField` candidates.
pub trait Generator {
    /// Produces an initial set of candidates from scratch.
    fn generate(&self) -> Vec<FractalField>;
    /// Mutates an existing field to produce a new set of candidates.
    fn mutate(&self, field: &FractalField) -> Vec<FractalField>;
}

/// A trait for a single, specific mutation algorithm.
pub trait MutationStrategy {
    fn mutate(&self, field: &FractalField) -> FractalField;
}

// --- High-Level Fractal System ---

/// A helper trait to enable cloning of `Box<dyn Fractal>`.
/// This is the standard pattern for making trait objects cloneable.
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() }
}

/// The base trait for all high-level, dynamically-typed fractal objects.
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>];
}

/// Implements the `Fractal` trait for the base `FractalAtom` type.
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 {
        // Check if the other trait object can be downcast to our concrete type, then compare.
        other.as_any().downcast_ref::<FractalAtom<T>>() == Some(self)
    }

    fn resonance_score(&self) -> f64 {
        // The resonance of a base atom is simply its underlying value.
        self.value.clone().into()
    }

    fn resonance_law(&self) -> ResonanceLaw {
        ResonanceLaw::Echo // A base atom is a simple echo of its value.
    }

    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>] { &[] } // Atoms have no children.
}

/// The operation connecting a member to a `FractalCollection`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Operation {
    Union,        // Additive
    Difference,   // Subtractive
    Intersection, // Multiplicative
}

/// An enum that wraps all supported concrete fractal types.
///
/// This is the primary way to work with different kinds of fractals in a type-safe
/// manner. It acts as a "dispatcher", forwarding any `Fractal` trait calls to the
/// specific variant it holds.
#[derive(Debug, Clone)]
pub enum FractalType {
    Mandelbrot(Mandelbrot),
    IFS(IFS),
}

/// The `Fractal` implementation for the `FractalType` enum.
/// It delegates each method call to the active variant inside it.
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 {
        // Downcast the `other` trait object to see if it's also a FractalType enum.
        if let Some(other_ft) = other.as_any().downcast_ref::<FractalType>() {
            // If so, compare the variants. They are only equal if they are the same variant
            // and their inner values are equal.
            match (self, other_ft) {
                (FractalType::Mandelbrot(m1), FractalType::Mandelbrot(m2)) => m1 == m2,
                (FractalType::IFS(i1), FractalType::IFS(i2)) => i1 == i2,
                _ => false, // Variants are different types (e.g., Mandelbrot vs IFS).
            }
        } else {
            false // `other` was not a FractalType enum.
        }
    }

    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(),
        }
    }
}

// --- CSG (Constructive Solid Geometry) System ---

/// A member of a `FractalCollection`, pairing a fractal with an operation.
#[derive(Debug, Clone)]
pub struct CollectionMember {
    pub fractal: FractalType,
    pub operation: Operation,
}

/// Represents a complex object created by combining multiple fractals.
#[derive(Debug, Clone, Default)]
pub struct FractalCollection {
    pub members: Vec<CollectionMember>,
}

// --- Concrete Fractal Type Definitions ---

/// A concrete implementation of a Mandelbrot set fractal.
#[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() // Higher score for less chaotic regions.
    }
    fn resonance_law(&self) -> ResonanceLaw { ResonanceLaw::Echo }
}

/// A concrete implementation of an Iterated Function System fractal.
#[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 }
}

// --- Type-Specific Operations ---

impl Mandelbrot {
    /// A specialized multiplication representing a geometric transformation.
    /// This is kept separate from general traits to avoid ambiguity and allow for
    /// type-specific logic that wouldn't fit in a generic `mul` operation.
    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 {
    /// A specialized multiplication representing function composition.
    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(),
        }
    }
}

// --- CSG Function Implementations ---

/// Combines two fractals into a `FractalCollection` representing their union.
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 },
        ],
    }
}

/// Combines two fractals to represent `A / B`, modeled as a Union with an Intersection.
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 },
        ],
    }
}

/// Combines two fractals to represent `A - B`.
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 },
        ],
    }
}

/// Combines two fractals to represent `A * B` at the CSG level (intersection).
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 },
        ],
    }
}

// --- Quantum Space & Semantic Traits ---

/// Represents a fractal, infinite-dimensional quantum space.
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;
}

/// A trait for comparing objects based on their meaning rather than strict equality.
pub trait SemanticEq {
    fn semantic_eq(&self, other: &Self) -> bool;
}