RustedSciThe 0.4.0

Rust framework for symbolic and numerical computing written under the influence of SciPy and Sympy:symbolic calculations,nonlinear systems,IVP and BVP,optimization,fitting,etc
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
/// Structural context for tracking expression hierarchy and preventing layout conflicts.
///
/// Each layout element belongs to a structural family to enable conflict detection
/// and resolution during the rendering process. This prevents mathematical elements
/// from overlapping when complex expressions are combined.
#[derive(Debug, Clone)]
pub struct StructuralContext {
    /// Optional parent structure ID for hierarchical tracking.
    /// Used to determine structural families and resolve layout conflicts.
    parent_id: Option<usize>,
}

/// A single text element in the mathematical expression layout.
///
/// Represents one piece of text (variable, operator, function name, etc.) with its
/// positioning information. Multiple elements at different levels combine to form
/// the complete mathematical expression.
#[derive(Debug, Clone)]
pub struct LayoutElement {
    /// The actual text content to be rendered (e.g., "x", "+", "sin", "─")
    content: String,

    /// Vertical level where this element should be placed.
    /// - Positive levels: above baseline (exponents, numerators)
    /// - Zero level: baseline (main expression)
    /// - Negative levels: below baseline (denominators, subscripts)
    level: i32,

    /// Horizontal position (column) where this element starts.
    /// Used for proper alignment when combining multiple elements on the same level.
    position: usize,

    /// Unique identifier linking this element to its structural context.
    /// Enables conflict detection and resolution during rendering.
    structure_id: usize,
}

/// Multi-level layout system for mathematical expression rendering.
///
/// Manages the positioning and rendering of mathematical expressions across multiple
/// vertical levels. Handles complex structures like fractions (numerator/denominator),
/// exponents, and nested functions while maintaining proper mathematical formatting.
///
/// ## Layout Strategy
///
/// - **Level-based positioning**: Each part of the expression is assigned a vertical level
/// - **Structure isolation**: Different mathematical constructs maintain separate structural contexts
/// - **Conservative gap management**: Minimal spacing between levels while preventing overlap
/// - **Conflict resolution**: Structural awareness prevents elements from interfering with each other
#[derive(Debug, Clone)]
pub struct LeveledLayout {
    /// Collection of all text elements that make up the expression.
    /// Elements are positioned across multiple levels and will be rendered
    /// into a multi-line string representation.
    elements: Vec<LayoutElement>,

    /// The reference level (typically 0) representing the main expression line.
    /// Positive levels are above baseline, negative levels are below.
    pub baseline: i32,

    /// Total horizontal width needed to accommodate all elements.
    /// Used for proper alignment and spacing calculations.
    total_width: usize,

    /// Maps each level to its height requirement (typically 1 for text lines).
    /// Used during rendering to determine which levels actually contain content.
    pub level_heights: std::collections::HashMap<i32, usize>,

    /// Maps structure IDs to their contexts for hierarchical tracking.
    /// Enables conflict detection and resolution between different mathematical structures.
    contexts: std::collections::HashMap<usize, StructuralContext>,

    /// Counter for generating unique structure IDs.
    /// Ensures each mathematical construct gets a unique identifier.
    next_structure_id: usize,
}

impl LeveledLayout {
    /// Creates a new empty layout system.
    ///
    /// Initializes all collections and sets baseline to 0 (main expression level).
    /// The layout is ready to accept elements and perform mathematical formatting.
    pub fn new() -> Self {
        Self {
            elements: Vec::new(),
            baseline: 0,
            total_width: 0,
            level_heights: std::collections::HashMap::new(),
            contexts: std::collections::HashMap::new(),
            next_structure_id: 0,
        }
    }

    /// Creates a new structural context for tracking expression hierarchy.
    ///
    /// Each mathematical construct (variable, operator, function, etc.) gets its own
    /// structural context to enable conflict detection and proper rendering isolation.
    ///
    /// # Arguments
    /// * `_path` - Structural path (currently unused but reserved for future enhancements)
    /// * `parent_id` - Optional parent structure for hierarchical relationships
    /// * `_depth` - Nesting depth (currently unused but reserved for future enhancements)
    ///
    /// # Returns
    /// Unique structure ID for the created context
    pub fn create_structure_context(
        &mut self,
        _path: Vec<String>,
        parent_id: Option<usize>,
        _depth: usize,
    ) -> usize {
        let structure_id = self.next_structure_id;
        self.next_structure_id += 1;

        let context = StructuralContext { parent_id };

        self.contexts.insert(structure_id, context.clone());
        structure_id
    }

    /// Adds a text element to the layout with specified structural context.
    ///
    /// Places the element at the current total width position and updates layout metrics.
    /// The element will be rendered at the specified level with proper structural tracking.
    ///
    /// # Arguments
    /// * `content` - Text content to be rendered
    /// * `level` - Vertical level for positioning (0=baseline, +above, -below)
    /// * `structure_id` - ID linking this element to its structural context
    pub fn add_element_with_context(&mut self, content: String, level: i32, structure_id: usize) {
        let position = self.total_width;

        self.elements.push(LayoutElement {
            content: content.clone(),
            level,
            position,
            structure_id,
        });
        self.total_width += content.chars().count();
        *self.level_heights.entry(level).or_insert(0) =
            (*self.level_heights.get(&level).unwrap_or(&0)).max(1);
    }

    /// Wraps the entire current layout in parentheses.
    ///
    /// Shifts all existing elements to the right and adds opening/closing brackets
    /// at the baseline level. Used when operator precedence requires bracketing
    /// for mathematical correctness.
    ///
    /// # Layout Changes
    /// - All existing elements are shifted right by 1 position
    /// - Opening bracket "(" is inserted at position 0
    /// - Closing bracket ")" is added at the end
    /// - Total width increases by 2
    pub fn add_brackets(&mut self) {
        let bracket_structure_id =
            self.create_structure_context(vec!["bracket".to_string()], None, 0);

        // Shift all existing elements to make room for opening bracket
        for element in &mut self.elements {
            element.position += 1;
        }

        // Add opening bracket at the beginning
        self.elements.insert(
            0,
            LayoutElement {
                content: "(".to_string(),
                level: self.baseline,
                position: 0,
                structure_id: bracket_structure_id,
            },
        );

        // Add closing bracket at the end
        self.elements.push(LayoutElement {
            content: ")".to_string(),
            level: self.baseline,
            position: self.total_width + 1,
            structure_id: bracket_structure_id,
        });

        self.total_width += 2;
    }

    /// Merges another layout horizontally with an operator between them.
    ///
    /// Combines two mathematical expressions side-by-side with an operator (like +, -, *, /)
    /// in between. Handles bracket insertion for the right operand when required by
    /// operator precedence rules.
    ///
    /// # Arguments
    /// * `other` - The right-hand layout to merge
    /// * `operator` - The operator string to place between layouts (e.g., "+", "*")
    /// * `op_level` - Vertical level where the operator should be placed
    /// * `right_needs_brackets` - Whether to wrap the right operand in brackets
    ///
    /// # Layout Process
    /// 1. Optionally add brackets to right operand
    /// 2. Add operator with spacing at current width
    /// 3. Merge structural contexts from both layouts
    /// 4. Offset and add all elements from right layout
    /// 5. Update total width and level heights
    pub fn merge_horizontal(
        &mut self,
        mut other: LeveledLayout,
        operator: &str,
        op_level: i32,
        right_needs_brackets: bool,
    ) {
        if right_needs_brackets {
            other.add_brackets();
        }

        let current_width = self.total_width;
        let op_width = operator.len() + 2; // operator + spaces

        // Create operator structure context
        let op_structure_id = self.create_structure_context(
            vec!["operator".to_string(), operator.to_string()],
            None,
            0,
        );

        // Add operator with spacing
        self.elements.push(LayoutElement {
            content: format!(" {} ", operator),
            level: op_level,
            position: current_width,
            structure_id: op_structure_id,
        });

        // Merge contexts from other layout
        for (id, context) in other.contexts {
            self.contexts.insert(id, context);
        }

        // Add other elements with proper position offset
        for mut element in other.elements {
            element.position += current_width + op_width;
            self.elements.push(element);
        }

        self.total_width += op_width + other.total_width;

        // Merge level heights
        for (level, height) in other.level_heights {
            *self.level_heights.entry(level).or_insert(0) =
                *self.level_heights.get(&level).unwrap_or(&0).max(&height);
        }
    }

    /// Merges two layouts vertically to create a fraction (numerator/denominator).
    ///
    /// Creates a mathematical fraction by placing the numerator above a division line
    /// and the denominator below. Both numerator and denominator are centered relative
    /// to the division line for proper mathematical formatting.
    ///
    /// # Arguments
    /// * `num_layout` - Layout for the numerator (top part)
    /// * `den_layout` - Layout for the denominator (bottom part)
    /// * `division_level` - Level where the division line should be placed (typically baseline)
    ///
    /// # Layout Structure
    /// ```text
    ///   numerator     <- above division_level
    /// ─────────────   <- at division_level
    ///  denominator    <- below division_level
    /// ```
    ///
    /// # Centering Logic
    /// - Division line width = max(numerator_width, denominator_width, 3)
    /// - Numerator and denominator are centered relative to division line
    /// - All elements maintain their original structural contexts
    pub fn merge_vertical(
        &mut self,
        num_layout: LeveledLayout,
        den_layout: LeveledLayout,
        division_level: i32,
    ) {
        let max_width = num_layout.total_width.max(den_layout.total_width).max(3);

        // Create division structure context
        let div_structure_id = self.create_structure_context(vec!["division".to_string()], None, 1);

        self.elements.clear();

        // Merge contexts from both layouts
        for (id, context) in num_layout.contexts {
            self.contexts.insert(id, context);
        }
        for (id, context) in den_layout.contexts {
            self.contexts.insert(id, context);
        }

        // Calculate centering offsets for numerator and denominator
        let num_offset = if num_layout.total_width < max_width {
            (max_width - num_layout.total_width) / 2
        } else {
            0
        };

        let den_offset = if den_layout.total_width < max_width {
            (max_width - den_layout.total_width) / 2
        } else {
            0
        };

        // Add numerator elements with centering and structure isolation
        for mut element in num_layout.elements {
            element.position = num_offset + element.position;
            // Preserve original structure context to prevent interference
            self.elements.push(element);
        }

        // Add division line with its own structure
        self.elements.push(LayoutElement {
            content: "─".repeat(max_width),
            level: division_level,
            position: 0,
            structure_id: div_structure_id,
        });

        // Add denominator elements with centering and structure isolation
        for mut element in den_layout.elements {
            element.position = den_offset + element.position;
            // Preserve original structure context to prevent interference
            self.elements.push(element);
        }

        // Merge level heights properly
        self.level_heights.clear();

        // Add numerator levels as-is (they should already be positioned correctly)
        for (&level, &height) in &num_layout.level_heights {
            *self.level_heights.entry(level).or_insert(0) = height;
        }

        // Add denominator levels as-is (they should already be positioned correctly)
        for (&level, &height) in &den_layout.level_heights {
            *self.level_heights.entry(level).or_insert(0) = height;
        }

        // Add division line level
        *self.level_heights.entry(division_level).or_insert(0) = 1;

        self.total_width = max_width;
        self.baseline = division_level;
    }

    /// Merges an exponent layout directly (unused helper method).
    ///
    /// This is a simple merge that adds exponent elements without level adjustment.
    /// Currently unused in favor of the more sophisticated `merge_power_exponent` method.
    ///
    /// # Arguments
    /// * `exp_layout` - The exponent layout to merge
    #[allow(dead_code)]
    fn _merge_exponent(&mut self, exp_layout: LeveledLayout) {
        // Add exponent elements
        self.elements.extend(exp_layout.elements);

        // Merge level heights
        for (level, height) in exp_layout.level_heights {
            *self.level_heights.entry(level).or_insert(0) =
                *self.level_heights.get(&level).unwrap_or(&0).max(&height);
        }

        self.total_width += exp_layout.total_width;
    }

    /// Merges an exponent layout as a superscript to the current base.
    ///
    /// Positions the exponent above the baseline and to the right of the base expression.
    /// Handles level conflicts by adjusting exponent levels to ensure they appear above
    /// the base without interfering with existing layout elements.
    ///
    /// # Arguments
    /// * `exp_layout` - Layout containing the exponent expression
    ///
    /// # Layout Process
    /// 1. Calculate base width and baseline for positioning
    /// 2. Merge structural contexts from exponent
    /// 3. Find minimum level in exponent for offset calculation
    /// 4. Adjust all exponent levels to appear above base
    /// 5. Position exponent elements to the right of base
    /// 6. Update total width and level heights
    ///
    /// # Example
    /// ```text
    /// Before: "x"     Exponent: "2"
    /// After:  "x²"    (if Unicode) or "x" with "2" at higher level
    /// ```
    pub fn merge_power_exponent(&mut self, exp_layout: LeveledLayout) {
        let base_width = self.total_width;
        let base_baseline = self.baseline;

        // Create power structure context
        let _power_structure_id = self.create_structure_context(
            vec!["power".to_string(), "exponent".to_string()],
            None,
            2,
        );

        // Merge contexts from exponent layout
        for (id, context) in exp_layout.contexts {
            self.contexts.insert(id, context);
        }

        // Find min level in exponent to calculate proper offset
        let exp_min_level = *exp_layout.level_heights.keys().min().unwrap_or(&0);

        // Calculate offset to move all exponent levels above the base
        // Ensures exponent appears as superscript without level conflicts
        let level_offset = (base_baseline + 1) - exp_min_level;

        // Add exponent elements with level adjustment and structure isolation
        for mut element in exp_layout.elements {
            element.level += level_offset;
            element.position += base_width;
            // Preserve original structure context but update level
            self.elements.push(element);
        }

        // Merge level heights with offset
        for (level, height) in exp_layout.level_heights {
            *self.level_heights.entry(level + level_offset).or_insert(0) = height;
        }

        // Update total width to accommodate both base and exponent
        self.total_width = base_width + exp_layout.total_width;
    }

    /// Merges another layout inline (horizontally) without any operator or spacing.
    ///
    /// Directly concatenates the other layout to the right of the current layout.
    /// Used for combining function arguments, parenthetical content, or other
    /// expressions that should appear immediately adjacent.
    ///
    /// # Arguments
    /// * `other` - Layout to merge inline
    ///
    /// # Layout Process
    /// 1. Merge structural contexts
    /// 2. Offset all elements by current width
    /// 3. Add all elements to current layout
    /// 4. Merge level heights (taking maximum for each level)
    /// 5. Update total width
    pub fn merge_inline(&mut self, other: LeveledLayout) {
        let current_width = self.total_width;

        // Merge contexts from other layout
        for (id, context) in other.contexts {
            self.contexts.insert(id, context);
        }

        for mut element in other.elements {
            element.position += current_width;
            self.elements.push(element);
        }

        for (level, height) in other.level_heights {
            *self.level_heights.entry(level).or_insert(0) =
                *self.level_heights.get(&level).unwrap_or(&0).max(&height);
        }

        self.total_width += other.total_width;
    }

    /// Merges a function with potentially multi-line content inside parentheses.
    ///
    /// Handles mathematical functions like sin(x), ln(complex_expression), etc.
    /// Automatically detects whether the inner content spans multiple levels and
    /// formats accordingly - simple inline for single-level content, or structured
    /// multi-line layout for complex expressions.
    ///
    /// # Arguments
    /// * `func_name` - Name of the function (e.g., "sin", "ln", "exp")
    /// * `inner_layout` - Layout of the function's argument/content
    ///
    /// # Layout Strategies
    ///
    /// ## Single-line content:
    /// ```text
    /// sin(x + y)  <- All on baseline
    /// ```
    ///
    /// ## Multi-line content:
    /// ```text
    /// sin( x + y )  <- Function name and brackets at baseline
    ///      ───      <- Inner content preserves its level structure
    ///       z
    /// ```
    ///
    /// # Implementation Details
    /// - Detects multi-line by comparing min/max levels in inner layout
    /// - Single-line: Concatenates everything into one element
    /// - Multi-line: Preserves inner structure with proper offset positioning
    /// - Maintains structural isolation to prevent layout conflicts
    pub fn merge_function_with_multiline(&mut self, func_name: &str, inner_layout: LeveledLayout) {
        // Create function structure context
        let func_structure_id = self.create_structure_context(
            vec!["function".to_string(), func_name.to_string()],
            None,
            1,
        );

        // For multi-line content inside functions, we need special handling
        let min_level = *inner_layout.level_heights.keys().min().unwrap_or(&0);
        let max_level = *inner_layout.level_heights.keys().max().unwrap_or(&0);

        // Merge contexts from inner layout
        for (id, context) in inner_layout.contexts {
            self.contexts.insert(id, context);
        }

        // Check if inner layout has multiple levels (is truly multi-line)
        if min_level == max_level {
            // Single line - use simple inline approach
            let content = inner_layout
                .elements
                .iter()
                .map(|e| e.content.clone())
                .collect::<Vec<_>>()
                .join("");
            let func_with_content = format!("{}({})", func_name, content);

            self.elements.push(LayoutElement {
                content: func_with_content.clone(),
                level: self.baseline,
                position: self.total_width,
                structure_id: func_structure_id,
            });

            self.total_width += func_with_content.chars().count();
            self.level_heights.extend(inner_layout.level_heights);
            return;
        }

        // Multi-line case: preserve function structure with isolation
        let current_pos = self.total_width;

        // Add function name and opening parenthesis at baseline
        let opening = format!("{}(", func_name);
        self.elements.push(LayoutElement {
            content: opening.clone(),
            level: self.baseline,
            position: current_pos,
            structure_id: func_structure_id,
        });

        // Add inner elements with proper offset and structure isolation
        let func_offset = opening.chars().count();
        for mut element in inner_layout.elements {
            element.position = current_pos + func_offset + element.position;
            // Preserve original structure context to prevent interference
            self.elements.push(element);
        }

        // Add closing parenthesis at baseline
        let inner_width = inner_layout.total_width;
        self.elements.push(LayoutElement {
            content: ")".to_string(),
            level: self.baseline,
            position: current_pos + func_offset + inner_width,
            structure_id: func_structure_id,
        });

        // Update layout properties
        self.level_heights.extend(inner_layout.level_heights);
        self.total_width = current_pos + func_offset + inner_width + 1;
    }

    /// Renders the layout into a vector of strings representing the final mathematical expression.
    ///
    /// This is the core rendering method that converts the multi-level layout structure
    /// into human-readable mathematical notation. It handles proper alignment, spacing,
    /// and conflict resolution between different structural elements.
    ///
    /// # Returns
    /// Vector of strings where each string represents one line of the mathematical expression
    ///
    /// # Rendering Process
    ///
    /// ## 1. Level Collection and Sorting
    /// - Identifies all levels that contain actual content (non-empty elements)
    /// - Sorts levels from top to bottom (descending order: +2, +1, 0, -1, -2)
    ///
    /// ## 2. Element Positioning
    /// - Groups elements by structural families to prevent conflicts
    /// - Sorts elements within each level by structure family, then by position
    /// - Creates character arrays for each line with proper width
    ///
    /// ## 3. Conflict Resolution
    /// - Detects when multiple elements try to occupy the same position
    /// - Uses structural compatibility checking to resolve conflicts
    /// - Skips conflicting characters when structures are incompatible
    ///
    /// ## 4. Gap Management (Conservative Approach)
    /// - Only adds empty lines between levels with gaps > 2
    /// - Ensures tight rendering for fractions (numerator/line/denominator)
    /// - Preserves spacing for complex nested expressions
    ///
    /// # Example Output
    /// ```text
    /// For expression: x² + sin(y) / e^x + 1
    ///
    ///     2        x
    /// x  + sin(y) / e  + 1
    /// ```
    ///
    /// # Gap Reduction Logic
    /// - gap ≤ 2: No empty lines (tight mathematical formatting)
    /// - gap > 2: One empty line (prevents visual confusion)
    pub fn render(&self) -> Vec<String> {
        if self.elements.is_empty() {
            return vec![String::new()];
        }

        let _min_level = *self.level_heights.keys().min().unwrap_or(&0);
        let _max_level = *self.level_heights.keys().max().unwrap_or(&0);

        let mut lines = Vec::new();

        // Calculate the maximum width needed for proper alignment
        let max_width = self
            .elements
            .iter()
            .map(|e| e.position + e.content.chars().count())
            .max()
            .unwrap_or(0);

        // Group elements by structural families to prevent interference
        let _structural_families = self.group_by_structural_families();

        // Collect all levels that actually have content
        let mut levels_with_content: Vec<i32> = self
            .level_heights
            .keys()
            .filter(|&&level| {
                self.elements
                    .iter()
                    .any(|e| e.level == level && !e.content.trim().is_empty())
            })
            .cloned()
            .collect();
        levels_with_content.sort_by(|a, b| b.cmp(a)); // Sort descending (top to bottom)

        // Render only levels that have content, with conservative gap reduction
        for (i, level) in levels_with_content.iter().enumerate() {
            // Collect elements at this level, sorted by position and structure
            let mut level_elements: Vec<&LayoutElement> = self
                .elements
                .iter()
                .filter(|e| e.level == *level && !e.content.trim().is_empty())
                .collect();

            // Sort by structure family first, then by position to prevent conflicts
            level_elements.sort_by(|a, b| {
                let family_a = self.get_structure_family(a.structure_id);
                let family_b = self.get_structure_family(b.structure_id);
                family_a.cmp(&family_b).then(a.position.cmp(&b.position))
            });

            if level_elements.is_empty() {
                continue;
            }

            // Build line with structure-aware positioning
            let mut line_chars: Vec<char> = " ".repeat(max_width).chars().collect();

            for element in level_elements {
                let content = &element.content;

                if content.is_empty() {
                    continue;
                }

                let start_pos = element.position;
                let content_chars: Vec<char> = content.chars().collect();

                // Safely place each character with conflict detection
                for (i, &ch) in content_chars.iter().enumerate() {
                    let pos = start_pos + i;
                    if pos < line_chars.len() {
                        // Check for conflicts with different structural families
                        if line_chars[pos] != ' '
                            && !self.are_compatible_structures(element.structure_id, pos)
                        {
                            // Handle conflict by adjusting position or skipping
                            continue;
                        }
                        line_chars[pos] = ch;
                    }
                }
            }

            let line_str: String = line_chars.into_iter().collect();
            lines.push(line_str.trim_end().to_string());

            // DIVISION-AWARE GAP MANAGEMENT: Tight spacing for divisions, conservative for others
            if i < levels_with_content.len() - 1 {
                let current_level = *level;
                let next_level = levels_with_content[i + 1];
                let gap = (current_level - next_level).abs();

                // Check if we're in a division context by looking for division lines
                let has_division_line = self.elements.iter().any(|e| {
                    e.content.contains('─')
                        && (e.level == current_level
                            || e.level == next_level
                            || (e.level > next_level && e.level < current_level))
                });

                // For divisions: no gaps (tight formatting)
                // For other expressions: gaps only when > 2
                if !has_division_line && gap > 2 {
                    lines.push(String::new());
                }
            }
        }

        // Remove trailing empty lines
        while lines.last().map_or(false, |line| line.trim().is_empty()) {
            lines.pop();
        }

        if lines.is_empty() {
            vec![String::new()]
        } else {
            lines
        }
    }

    /// Groups layout elements by their structural families for conflict resolution.
    ///
    /// Elements belonging to the same structural family (sharing a common root structure)
    /// are grouped together. This enables the rendering system to detect and resolve
    /// conflicts between different mathematical constructs.
    ///
    /// # Returns
    /// HashMap mapping family root IDs to vectors of structure IDs in that family
    ///
    /// # Usage
    /// Used during rendering to ensure elements from different structural families
    /// don't interfere with each other's positioning and layout.
    fn group_by_structural_families(&self) -> std::collections::HashMap<usize, Vec<usize>> {
        let mut families = std::collections::HashMap::new();

        for element in &self.elements {
            let family_id = self.get_structure_family(element.structure_id);
            families
                .entry(family_id)
                .or_insert_with(Vec::new)
                .push(element.structure_id);
        }

        families
    }

    /// Finds the root structure ID for a given structure by traversing parent relationships.
    ///
    /// Walks up the structural hierarchy to find the topmost parent structure.
    /// This identifies which "family" a structure belongs to, enabling conflict
    /// detection between different mathematical constructs.
    ///
    /// # Arguments
    /// * `structure_id` - The structure ID to find the family root for
    ///
    /// # Returns
    /// The root structure ID of the family
    ///
    /// # Example
    /// For a complex expression like "sin(x/y)", the division structure and
    /// the sin function structure would have different family roots, preventing
    /// layout conflicts between them.
    fn get_structure_family(&self, structure_id: usize) -> usize {
        // Find the root structure ID by traversing parent relationships
        let mut current_id = structure_id;
        while let Some(context) = self.contexts.get(&current_id) {
            if let Some(parent_id) = context.parent_id {
                current_id = parent_id;
            } else {
                break;
            }
        }
        current_id
    }

    /// Checks if two structures can coexist at the same position without conflict.
    ///
    /// Currently implements a permissive policy allowing all structures to coexist.
    /// This method is designed to be enhanced with more sophisticated conflict
    /// detection logic as needed.
    ///
    /// # Arguments
    /// * `_structure_id` - ID of the structure being placed (currently unused)
    /// * `_position` - Position where the structure is being placed (currently unused)
    ///
    /// # Returns
    /// `true` if structures are compatible, `false` if they conflict
    ///
    /// # Future Enhancements
    /// Could be extended to:
    /// - Detect overlapping mathematical constructs
    /// - Prevent division lines from interfering with exponents
    /// - Resolve bracket placement conflicts
    /// - Handle complex nested expression conflicts
    fn are_compatible_structures(&self, _structure_id: usize, _position: usize) -> bool {
        // For now, allow all structures to coexist
        // This can be enhanced with more sophisticated conflict detection
        true
    }
}