ries 1.1.1

Find algebraic equations given their solution - Rust implementation
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
//! Fast-path exact match detection
//!
//! Before doing expensive expression generation, check if the target
//! is a simple exact match (like pi, e, sqrt(2), etc.) that can be
//! found instantly.

use crate::eval::{evaluate_with_context, EvalContext};
use crate::expr::{EvaluatedExpr, Expression};
use crate::profile::UserConstant;
use crate::search::Match;
use crate::symbol::{NumType, Symbol};
use crate::symbol_table::SymbolTable;
use std::collections::HashSet;

/// Tolerance for exact match detection
const EXACT_TOLERANCE: f64 = 1e-14;

/// Build an expression from symbols using table-based weights
fn expr_from_symbols_with_table(symbols: &[Symbol], table: &SymbolTable) -> Expression {
    let mut expr = Expression::new();
    for &sym in symbols {
        expr.push_with_table(sym, table);
    }
    expr
}

/// Get the num_type of an expression based on its symbols
/// This is a simplified type inference for the fast_match candidates
fn get_num_type(symbols: &[Symbol]) -> NumType {
    // For fast match candidates, we use simplified type inference:
    // - Integer constants → Integer
    // - Rational operations (division) → Rational
    // - Sqrt of integer → Algebraic (constructible)
    // - Transcendental constants → Transcendental
    // - Any transcendental function → Transcendental

    use Symbol::*;

    // Handle simple patterns
    if symbols.len() == 1 {
        return symbols[0].inherent_type();
    }

    // Check for sqrt of integer (like 2q = sqrt(2))
    if symbols.len() == 2 {
        if matches!(symbols[1], Sqrt) {
            if matches!(
                symbols[0],
                One | Two | Three | Four | Five | Six | Seven | Eight | Nine
            ) {
                return NumType::Algebraic; // sqrt of integer is algebraic
            }
            // sqrt of transcendental constant is transcendental
            if matches!(symbols[0], Pi | E | Gamma | Apery | Catalan) {
                return NumType::Transcendental;
            }
        }
        // Check for reciprocal of integer
        if matches!(symbols[1], Recip)
            && matches!(
                symbols[0],
                One | Two | Three | Four | Five | Six | Seven | Eight | Nine
            )
        {
            return NumType::Rational;
        }
        // Division: integer / integer = rational
        if matches!(symbols[1], Div)
            && matches!(
                symbols[0],
                One | Two | Three | Four | Five | Six | Seven | Eight | Nine
            )
            && symbols.len() >= 3
        {
            // This is more complex, but for simple cases it's rational
            return NumType::Rational;
        }
    }

    // Check for division pattern (3 symbols: num, denom, /)
    if symbols.len() == 3 && matches!(symbols[2], Div) {
        // Integer / Integer = Rational
        if matches!(
            symbols[0],
            One | Two | Three | Four | Five | Six | Seven | Eight | Nine
        ) && matches!(
            symbols[1],
            One | Two | Three | Four | Five | Six | Seven | Eight | Nine
        ) {
            return NumType::Rational;
        }
    }

    // Default: check if any symbol is transcendental
    for &sym in symbols {
        let sym_type = sym.inherent_type();
        if sym_type == NumType::Transcendental {
            return NumType::Transcendental;
        }
    }

    // If we have any algebraic constants (phi, plastic), result is algebraic
    for &sym in symbols {
        if matches!(sym, Phi | Plastic) {
            return NumType::Algebraic;
        }
    }

    // Default to transcendental (most general)
    NumType::Transcendental
}

/// Check if any symbol in the expression is excluded
fn contains_excluded(symbols: &[Symbol], excluded: &HashSet<u8>) -> bool {
    symbols.iter().any(|s| excluded.contains(&(*s as u8)))
}

/// A candidate for a fast exact match
struct FastCandidate {
    /// The expression (as symbols)
    symbols: &'static [Symbol],
}

/// Generate fast candidates for common constants and simple expressions
fn get_constant_candidates() -> Vec<FastCandidate> {
    vec![
        // Integers
        FastCandidate {
            symbols: &[Symbol::One],
        },
        FastCandidate {
            symbols: &[Symbol::Two],
        },
        FastCandidate {
            symbols: &[Symbol::Three],
        },
        FastCandidate {
            symbols: &[Symbol::Four],
        },
        FastCandidate {
            symbols: &[Symbol::Five],
        },
        FastCandidate {
            symbols: &[Symbol::Six],
        },
        FastCandidate {
            symbols: &[Symbol::Seven],
        },
        FastCandidate {
            symbols: &[Symbol::Eight],
        },
        FastCandidate {
            symbols: &[Symbol::Nine],
        },
        // Named constants
        FastCandidate {
            symbols: &[Symbol::Pi],
        },
        FastCandidate {
            symbols: &[Symbol::E],
        },
        FastCandidate {
            symbols: &[Symbol::Phi],
        },
        FastCandidate {
            symbols: &[Symbol::Gamma],
        },
        FastCandidate {
            symbols: &[Symbol::Plastic],
        },
        FastCandidate {
            symbols: &[Symbol::Apery],
        },
        FastCandidate {
            symbols: &[Symbol::Catalan],
        },
        // Simple rationals (common ones)
        FastCandidate {
            symbols: &[Symbol::One, Symbol::Two, Symbol::Div],
        },
        FastCandidate {
            symbols: &[Symbol::One, Symbol::Three, Symbol::Div],
        },
        FastCandidate {
            symbols: &[Symbol::Two, Symbol::Three, Symbol::Div],
        },
        FastCandidate {
            symbols: &[Symbol::One, Symbol::Four, Symbol::Div],
        },
        FastCandidate {
            symbols: &[Symbol::Three, Symbol::Four, Symbol::Div],
        },
        // Simple roots
        FastCandidate {
            symbols: &[Symbol::Two, Symbol::Sqrt],
        },
        FastCandidate {
            symbols: &[Symbol::Three, Symbol::Sqrt],
        },
        FastCandidate {
            symbols: &[Symbol::Five, Symbol::Sqrt],
        },
        FastCandidate {
            symbols: &[Symbol::Six, Symbol::Sqrt],
        },
        FastCandidate {
            symbols: &[Symbol::Seven, Symbol::Sqrt],
        },
        FastCandidate {
            symbols: &[Symbol::Eight, Symbol::Sqrt],
        },
        FastCandidate {
            symbols: &[Symbol::Pi, Symbol::Sqrt],
        },
        FastCandidate {
            symbols: &[Symbol::E, Symbol::Sqrt],
        },
        // Simple logs
        FastCandidate {
            symbols: &[Symbol::Two, Symbol::Ln],
        },
        FastCandidate {
            symbols: &[Symbol::Pi, Symbol::Ln],
        },
        // e ± small integers
        FastCandidate {
            symbols: &[Symbol::E, Symbol::One, Symbol::Sub],
        },
        FastCandidate {
            symbols: &[Symbol::E, Symbol::One, Symbol::Add],
        },
        // pi ± small integers
        FastCandidate {
            symbols: &[Symbol::Pi, Symbol::One, Symbol::Sub],
        },
        FastCandidate {
            symbols: &[Symbol::Pi, Symbol::One, Symbol::Add],
        },
        FastCandidate {
            symbols: &[Symbol::Pi, Symbol::Two, Symbol::Sub],
        },
        // Common combinations
        FastCandidate {
            symbols: &[Symbol::One, Symbol::Two, Symbol::Add],
        },
        FastCandidate {
            symbols: &[Symbol::One, Symbol::Sqrt, Symbol::One, Symbol::Add],
        },
        FastCandidate {
            symbols: &[Symbol::Two, Symbol::Sqrt, Symbol::One, Symbol::Add],
        },
        // phi combinations (golden ratio)
        FastCandidate {
            symbols: &[Symbol::Phi, Symbol::One, Symbol::Add],
        },
        FastCandidate {
            symbols: &[Symbol::Phi, Symbol::Two, Symbol::Add],
        },
        FastCandidate {
            symbols: &[Symbol::Phi, Symbol::Square],
        },
        // Reciprocals of constants
        FastCandidate {
            symbols: &[Symbol::Pi, Symbol::Recip],
        },
        FastCandidate {
            symbols: &[Symbol::E, Symbol::Recip],
        },
        FastCandidate {
            symbols: &[Symbol::Phi, Symbol::Recip],
        },
    ]
}

/// Check if target matches a simple integer
fn check_integer(target: f64) -> Option<(i64, f64)> {
    let rounded = target.round();
    let error = (target - rounded).abs();
    if error < EXACT_TOLERANCE && rounded.abs() < 1000.0 {
        Some((rounded as i64, error))
    } else {
        None
    }
}

/// Configuration for fast match filtering
pub struct FastMatchConfig<'a> {
    /// Symbols that are excluded (via -N flag)
    pub excluded_symbols: &'a HashSet<u8>,
    /// Symbols that are explicitly allowed (all symbols must be in set)
    pub allowed_symbols: Option<&'a HashSet<u8>>,
    /// Minimum numeric type required (via -a, -r, -i flags)
    pub min_num_type: NumType,
}

#[inline]
fn passes_symbol_filters(symbols: &[Symbol], config: &FastMatchConfig<'_>) -> bool {
    if contains_excluded(symbols, config.excluded_symbols) {
        return false;
    }
    if let Some(allowed) = config.allowed_symbols {
        if symbols.iter().any(|s| !allowed.contains(&(*s as u8))) {
            return false;
        }
    }
    true
}

/// Try to find a fast exact match for the target value
///
/// Returns a Match if found, or None if no simple exact match exists.
/// This function is designed to be called before expensive generation.
pub fn find_fast_match(
    target: f64,
    user_constants: &[UserConstant],
    config: &FastMatchConfig<'_>,
    table: &SymbolTable,
) -> Option<Match> {
    let context = EvalContext::from_slices(user_constants, &[]);
    find_fast_match_with_context(target, &context, config, table)
}

/// Try to find a fast exact match for the target value using an explicit evaluation context.
pub fn find_fast_match_with_context(
    target: f64,
    context: &EvalContext<'_>,
    config: &FastMatchConfig<'_>,
    table: &SymbolTable,
) -> Option<Match> {
    // First check integers (fastest)
    if let Some((n, error)) = check_integer(target) {
        if (1..=9).contains(&n) {
            // We have a direct constant for 1-9
            let symbols: &[Symbol] = match n {
                1 => &[Symbol::One],
                2 => &[Symbol::Two],
                3 => &[Symbol::Three],
                4 => &[Symbol::Four],
                5 => &[Symbol::Five],
                6 => &[Symbol::Six],
                7 => &[Symbol::Seven],
                8 => &[Symbol::Eight],
                9 => &[Symbol::Nine],
                _ => return None,
            };
            // Check if excluded or wrong type
            if passes_symbol_filters(symbols, config)
                && get_num_type(symbols) >= config.min_num_type
            {
                if let Some(m) = make_match(symbols, target, error, table, context) {
                    return Some(m);
                }
            }
        }
        // For other integers, check if they match user constants
        for (idx, uc) in context.user_constants.iter().enumerate() {
            if idx < 16 && (uc.value - target).abs() < EXACT_TOLERANCE {
                if let Some(sym) = Symbol::from_byte(128 + idx as u8) {
                    let symbols = [sym];
                    if passes_symbol_filters(&symbols, config) && uc.num_type >= config.min_num_type
                    {
                        if let Some(m) =
                            make_match(&symbols, target, (uc.value - target).abs(), table, context)
                        {
                            return Some(m);
                        }
                    }
                }
            }
        }
    }

    // Check user constants first (they're explicitly defined)
    for (idx, uc) in context.user_constants.iter().enumerate() {
        if idx >= 16 {
            break;
        }
        if (uc.value - target).abs() < EXACT_TOLERANCE {
            if let Some(sym) = Symbol::from_byte(128 + idx as u8) {
                let symbols = [sym];
                if passes_symbol_filters(&symbols, config) && uc.num_type >= config.min_num_type {
                    if let Some(m) =
                        make_match(&symbols, target, (uc.value - target).abs(), table, context)
                    {
                        return Some(m);
                    }
                }
            }
        }
    }

    // Check known constant candidates
    let candidates = get_constant_candidates();
    for candidate in candidates {
        // Skip if contains excluded symbols
        if !passes_symbol_filters(candidate.symbols, config) {
            continue;
        }
        // Skip if type doesn't meet requirement
        if get_num_type(candidate.symbols) < config.min_num_type {
            continue;
        }

        let expr = expr_from_symbols_with_table(candidate.symbols, table);
        if let Ok(result) = evaluate_with_context(&expr, target, context) {
            let error = (result.value - target).abs();
            if error < EXACT_TOLERANCE {
                if let Some(m) = make_match(candidate.symbols, target, error, table, context) {
                    return Some(m);
                }
            }
        }
    }

    // Check user constants with simple operations
    for (idx, uc) in context.user_constants.iter().enumerate() {
        if idx >= 16 {
            break;
        }
        if let Some(sym) = Symbol::from_byte(128 + idx as u8) {
            // Check 1/constant
            if uc.value != 0.0 {
                let recip_val = 1.0 / uc.value;
                if (recip_val - target).abs() < EXACT_TOLERANCE {
                    let symbols = [sym, Symbol::Recip];
                    if passes_symbol_filters(&symbols, config) && uc.num_type >= config.min_num_type
                    {
                        if let Some(m) =
                            make_match(&symbols, target, (recip_val - target).abs(), table, context)
                        {
                            return Some(m);
                        }
                    }
                }
            }
            // Check sqrt(constant)
            if uc.value > 0.0 {
                let sqrt_val = uc.value.sqrt();
                if (sqrt_val - target).abs() < EXACT_TOLERANCE {
                    let symbols = [sym, Symbol::Sqrt];
                    if passes_symbol_filters(&symbols, config) && uc.num_type >= config.min_num_type
                    {
                        if let Some(m) =
                            make_match(&symbols, target, (sqrt_val - target).abs(), table, context)
                        {
                            return Some(m);
                        }
                    }
                }
            }
        }
    }

    None
}

/// Create a Match from symbols representing the RHS value
fn make_match(
    symbols: &[Symbol],
    target: f64,
    error: f64,
    table: &SymbolTable,
    context: &EvalContext<'_>,
) -> Option<Match> {
    let lhs_expr = expr_from_symbols_with_table(&[Symbol::X], table);
    let rhs_expr = expr_from_symbols_with_table(symbols, table);
    let complexity = lhs_expr.complexity() + rhs_expr.complexity();

    let lhs_eval = evaluate_with_context(&lhs_expr, target, context).ok()?;
    let rhs_eval = evaluate_with_context(&rhs_expr, target, context).ok()?;

    Some(Match {
        lhs: EvaluatedExpr {
            expr: lhs_expr,
            value: lhs_eval.value,
            derivative: lhs_eval.derivative,
            num_type: NumType::Transcendental,
        },
        rhs: EvaluatedExpr {
            expr: rhs_expr,
            value: rhs_eval.value,
            derivative: 0.0,
            num_type: rhs_eval.num_type,
        },
        x_value: target,
        error,
        complexity,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    fn default_config() -> FastMatchConfig<'static> {
        static EMPTY: std::sync::OnceLock<HashSet<u8>> = std::sync::OnceLock::new();
        let empty = EMPTY.get_or_init(HashSet::new);
        FastMatchConfig {
            excluded_symbols: empty,
            allowed_symbols: None,
            min_num_type: NumType::Transcendental,
        }
    }

    fn default_table() -> SymbolTable {
        SymbolTable::new()
    }

    #[test]
    fn test_pi_match() {
        let m = find_fast_match(
            std::f64::consts::PI,
            &[],
            &default_config(),
            &default_table(),
        );
        assert!(m.is_some());
        let m = m.unwrap();
        assert!(m.error.abs() < 1e-14);
        assert_eq!(m.rhs.expr.to_postfix(), "p");
    }

    #[test]
    fn test_pi_excluded() {
        let excluded: HashSet<u8> = vec![b'p'].into_iter().collect();
        let config = FastMatchConfig {
            excluded_symbols: &excluded,
            allowed_symbols: None,
            min_num_type: NumType::Transcendental,
        };
        let m = find_fast_match(std::f64::consts::PI, &[], &config, &default_table());
        assert!(m.is_none(), "Should not find pi when it's excluded");
    }

    #[test]
    fn test_pi_algebraic_only() {
        static EMPTY: std::sync::OnceLock<HashSet<u8>> = std::sync::OnceLock::new();
        let empty = EMPTY.get_or_init(HashSet::new);
        let config = FastMatchConfig {
            excluded_symbols: empty,
            allowed_symbols: None,
            min_num_type: NumType::Algebraic,
        };
        let m = find_fast_match(std::f64::consts::PI, &[], &config, &default_table());
        assert!(
            m.is_none(),
            "Should not find pi when only algebraic allowed"
        );
    }

    #[test]
    fn test_sqrt2_algebraic_ok() {
        static EMPTY: std::sync::OnceLock<HashSet<u8>> = std::sync::OnceLock::new();
        let empty = EMPTY.get_or_init(HashSet::new);
        let config = FastMatchConfig {
            excluded_symbols: empty,
            allowed_symbols: None,
            min_num_type: NumType::Algebraic,
        };
        let m = find_fast_match(2.0_f64.sqrt(), &[], &config, &default_table());
        assert!(m.is_some(), "sqrt(2) should be found with algebraic-only");
    }

    #[test]
    fn test_e_match() {
        let m = find_fast_match(
            std::f64::consts::E,
            &[],
            &default_config(),
            &default_table(),
        );
        assert!(m.is_some());
        let m = m.unwrap();
        assert!(m.error.abs() < 1e-14);
        assert_eq!(m.rhs.expr.to_postfix(), "e");
    }

    #[test]
    fn test_sqrt2_match() {
        let m = find_fast_match(2.0_f64.sqrt(), &[], &default_config(), &default_table());
        assert!(m.is_some());
        let m = m.unwrap();
        assert!(m.error.abs() < 1e-14);
        assert_eq!(m.rhs.expr.to_postfix(), "2q");
    }

    #[test]
    fn test_phi_match() {
        let phi = (1.0 + 5.0_f64.sqrt()) / 2.0;
        let m = find_fast_match(phi, &[], &default_config(), &default_table());
        assert!(m.is_some());
        let m = m.unwrap();
        assert!(m.error.abs() < 1e-14);
        assert_eq!(m.rhs.expr.to_postfix(), "f");
    }

    #[test]
    fn test_integer_match() {
        let m = find_fast_match(5.0, &[], &default_config(), &default_table());
        assert!(m.is_some());
        let m = m.unwrap();
        assert!(m.error.abs() < 1e-14);
        assert_eq!(m.rhs.expr.to_postfix(), "5");
    }

    #[test]
    fn test_no_match_for_random() {
        // 2.506314 is not a simple constant
        let m = find_fast_match(2.506314, &[], &default_config(), &default_table());
        assert!(m.is_none());
    }

    #[test]
    fn test_user_constant_match() {
        let uc = UserConstant {
            weight: 4,
            name: "myconst".to_string(),
            description: "Test constant".to_string(),
            value: std::f64::consts::E,
            num_type: NumType::Transcendental,
        };
        let m = find_fast_match(
            std::f64::consts::E,
            &[uc],
            &default_config(),
            &default_table(),
        );
        assert!(m.is_some());
    }
}