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
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
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
//! Command-line argument parsing for RIES
//!
//! This module defines the CLI interface using clap, including all argument
//! definitions and parsing helpers.

use clap::{ArgAction, Parser};
use std::path::PathBuf;

use crate::profile;
use crate::symbol;
use crate::udf;

/// Find algebraic equations given their solution
#[derive(Parser, Debug)]
#[command(name = "ries-rs")]
#[command(author = "Maxwell Santoro")]
#[command(version)]
#[command(about = "Find algebraic equations given their solution", long_about = None)]
pub struct Args {
    /// Target value to find equations for (optional if using --eval-expression)
    pub target: Option<f64>,

    /// Search level (each increment ~10x more equations)
    /// Level 0 ~ 89M equations, Level 2 ~ 11B, Level 5 ~ 15T
    /// Legacy: "-l 2.5" (with no explicit target) means Liouvillian mode + target 2.5
    #[arg(short = 'l', long, default_value = "2")]
    pub level: String,

    /// Maximum number of matches to display
    #[arg(short = 'n', long = "max-matches", default_value = "16")]
    pub max_matches: usize,

    /// Show absolute x values instead of T +/- error
    #[arg(short = 'x', long, alias = "absolute-roots")]
    pub absolute: bool,

    /// Try to solve for x (show x = ... form)
    #[arg(short = 's', long, alias = "try-solve-for-x")]
    pub solve: bool,

    /// Disable solve-for-x presentation even if requested elsewhere
    #[arg(long = "no-solve-for-x")]
    pub no_solve: bool,

    /// Symbols to never use (e.g., "+-" to exclude add/subtract)
    #[arg(short = 'N', long)]
    pub exclude: Option<String>,

    /// Re-enable symbols disabled by exclude/only options, or enable all if no argument
    #[arg(short = 'E', long = "enable", num_args = 0..=1, default_missing_value = "all")]
    pub enable: Option<String>,

    /// Only use these symbols, or print symbol table if no argument
    /// Using -S alone prints the full symbol table and exits
    #[arg(short = 'S', long, num_args = 0..=1)]
    pub only_symbols: Option<String>,

    /// Operator/symbol count limits (C RIES -O semantics).
    /// Example: "-Ox" means at most one x per side; "-O2+" means at most two '+'.
    #[arg(short = 'O', long)]
    pub op_limits: Option<String>,

    /// Only use these symbols on RHS (right-hand side)
    #[arg(long = "S-RHS")]
    pub only_symbols_rhs: Option<String>,

    /// Exclude these symbols on RHS
    #[arg(long = "N-RHS")]
    pub exclude_rhs: Option<String>,

    /// Re-enable symbols on RHS
    #[arg(long = "E-RHS")]
    pub enable_rhs: Option<String>,

    /// RHS-only symbol count limits (like -O but RHS only)
    #[arg(long = "O-RHS")]
    pub op_limits_rhs: Option<String>,

    /// Custom symbol weights (e.g., ":W:20" sets Lambert W weight to 20)
    #[arg(long)]
    pub symbol_weights: Option<String>,

    /// Custom symbol names (e.g., ":p:PI" renames pi to PI in output)
    #[arg(long)]
    pub symbol_names: Option<String>,

    /// Restrict to algebraic solutions
    #[arg(short = 'a', long, alias = "algebraic-subexpressions")]
    pub algebraic: bool,

    /// Restrict to constructible solutions
    #[arg(short = 'c', long, alias = "constructible-subexpressions")]
    pub constructible: bool,

    /// Restrict to rational solutions
    #[arg(short = 'r', long, alias = "rational-subexpressions")]
    pub rational: bool,

    /// Restrict to integer solutions
    #[arg(short = 'i', long, alias = "integer-subexpressions")]
    pub integer: bool,

    /// Integer exact mode (equivalent to -i --stop-at-exact)
    /// Stops at first integer match
    #[arg(long = "ie")]
    pub integer_exact: bool,

    /// Rational exact mode (equivalent to -r --stop-at-exact)
    /// Stops at first rational match
    #[arg(long = "re")]
    pub rational_exact: bool,

    /// Restrict to Liouvillian subexpressions
    #[arg(long = "liouvillian-subexpressions")]
    pub liouvillian: bool,

    /// Use parallel search (default: true)
    #[arg(long, default_value = "true")]
    pub parallel: bool,

    /// Force deterministic output (disables parallelism, uses stable sorting)
    /// Required for reproducible results in academic papers
    #[arg(long)]
    pub deterministic: bool,

    /// Use streaming search for lower memory usage at high complexity levels
    /// Streaming processes expressions on-the-fly instead of accumulating in memory
    #[arg(long)]
    pub streaming: bool,

    /// Use adaptive search (like original RIES) for better precision
    /// Iteratively expands LHS/RHS complexity to match original RIES expression counts
    /// Generates ~500K expressions at level 2 (vs ~3K with default mode)
    #[arg(long)]
    pub adaptive: bool,

    /// Use report mode with categorized output (default: true)
    /// Shows top matches in each category: exact, best, elegant, interesting, stable
    #[arg(long, default_value_t = true, action = ArgAction::Set)]
    pub report: bool,

    /// Classic/sniper mode: single list output (like original RIES)
    /// Implies --stop-at-exact and aggressive early exit for speed
    #[arg(long)]
    pub classic: bool,

    /// Use original-RIES-like signed weight ranking for match ordering
    /// (exactness -> error -> legacy parity score -> complexity)
    #[arg(long, conflicts_with = "complexity_ranking")]
    pub parity_ranking: bool,

    /// Force complexity-first ranking
    /// (exactness -> error -> complexity)
    #[arg(long, conflicts_with = "parity_ranking")]
    pub complexity_ranking: bool,

    /// Output format for expressions
    /// Options: default, pretty (Unicode), mathematica, sympy
    #[arg(short = 'F', long, num_args = 0..=1, default_missing_value = "3", default_value = "2")]
    pub format: String,

    /// Compatibility diagnostics flag (-D, -Dy, etc.)
    #[arg(short = 'D', num_args = 0..=1, default_missing_value = "")]
    pub diagnostics: Option<String>,

    /// Number of matches per category in report mode
    #[arg(short = 'k', long = "top-k", default_value = "8")]
    pub top_k: usize,

    /// Exclude stability category from the report
    #[arg(long)]
    pub no_stable: bool,

    /// Show detailed search statistics
    #[arg(long)]
    pub stats: bool,

    /// Emit machine-readable JSON results to stdout (suppresses text report output)
    #[arg(long)]
    pub json: bool,

    /// Print list of supported options and exit
    #[arg(long = "list-options")]
    pub list_options: bool,

    /// Use a domain-specific preset for symbol weights and constants
    /// Available: analytic-nt, elliptic, combinatorics, physics, number-theory, calculus
    /// Use --list-presets to see descriptions
    #[arg(long)]
    pub preset: Option<String>,

    /// List available domain presets and exit
    #[arg(long)]
    pub list_presets: bool,

    /// Run stability analysis (impostor detection)
    /// Runs search at multiple tolerance levels and classifies candidates
    /// Use --stability-thorough for more precision levels
    #[arg(long)]
    pub stability_check: bool,

    /// Use thorough stability analysis (more precision levels, slower)
    #[arg(long)]
    pub stability_thorough: bool,

    /// Stop search when an exact match is found
    #[arg(long)]
    pub stop_at_exact: bool,

    /// Stop search when error goes below this threshold
    #[arg(long)]
    pub stop_below: Option<f64>,

    /// Load profile file for custom constants and symbol settings
    #[arg(short = 'p', long)]
    pub profile: Option<PathBuf>,

    /// Include additional profile file (can be used multiple times)
    #[arg(long)]
    pub include: Vec<PathBuf>,

    /// Add a user-defined constant
    /// Format: "weight:name:description:value"
    /// Example: -X "4:gamma:Euler's constant:0.5772156649"
    #[arg(short = 'X', long = "user-constant", alias = "constant")]
    pub user_constant: Vec<String>,

    /// Define a custom operation/function
    /// Format: "weight:name:description:formula"
    /// Formula uses postfix notation with | for dup and @ for swap
    /// Example: --define "4:sinh:hyperbolic sine:E|r-2/"
    #[arg(long)]
    pub define: Vec<String>,

    /// Maximum acceptable error for matches (default: 1% of |target|)
    #[arg(long, alias = "mad")]
    pub max_match_distance: Option<f64>,

    /// Minimum error threshold (exclude matches closer than this)
    #[arg(long)]
    pub min_match_distance: Option<f64>,

    /// Use one-sided mode: only generate RHS expressions, compare directly to target
    #[arg(long)]
    pub one_sided: bool,

    /// Skip Newton-Raphson refinement of matches
    #[arg(long)]
    pub no_refinement: bool,

    /// Evaluate an expression at a given x value and exit
    /// Example: --eval-expression "xq" --at 2.5
    #[arg(long)]
    pub eval_expression: Option<String>,

    /// Evaluate and display a specific expression (compatibility option)
    #[arg(long)]
    pub find_expression: Option<String>,

    /// X value for --eval-expression
    #[arg(long)]
    pub at: Option<f64>,

    /// Maximum Newton-Raphson iterations for root refinement (default: 15)
    #[arg(long, default_value = "15")]
    pub newton_iterations: usize,

    /// Precision in bits for high-precision verification (e.g., 256 for ~77 digits)
    /// Re-evaluates top matches at higher precision to verify/impostor detection
    /// Requires --features highprec at compile time
    #[arg(long)]
    pub precision: Option<u32>,

    /// Threshold for pruning LHS expressions with near-zero values (default: 1e-4)
    #[arg(long)]
    pub zero_threshold: Option<f64>,

    /// Compatibility alias for wide output formatting (accepted for parity; no-op)
    #[arg(long)]
    pub wide: bool,

    /// Compatibility alias for wide output formatting (accepted for parity; no-op)
    #[arg(long = "wide-output")]
    pub wide_output: bool,

    /// Compatibility alias for relative roots display (accepted for parity; no-op)
    #[arg(long = "relative-roots")]
    pub relative_roots: bool,

    /// Disable rational-exponents filtering when combined with --rational-exponents
    #[arg(long = "any-exponents")]
    pub any_exponents: bool,

    /// Clear numeric-type subexpression restrictions (-a/-c/-r/-i/--liouvillian-subexpressions)
    #[arg(long = "any-subexpressions")]
    pub any_subexpressions: bool,

    /// Disable rational-trig-args filtering when combined with --rational-trig-args
    #[arg(long = "any-trig-args")]
    pub any_trig_args: bool,

    /// Canonical reduction mode used by compatibility dedupe pass
    #[arg(long = "canon-reduction")]
    pub canon_reduction: Option<String>,

    /// Enable canonical simplification pass for match deduplication
    #[arg(long = "canon-simplify")]
    pub canon_simplify: bool,

    /// Override Newton derivative threshold used to detect degenerate derivatives
    #[arg(long = "derivative-margin")]
    pub derivative_margin: Option<f64>,

    /// Force explicit '*' in infix display output
    #[arg(long = "explicit-multiply")]
    pub explicit_multiply: bool,

    /// Require matches to agree with target precision (uses target significant digits)
    #[arg(long = "match-all-digits")]
    pub match_all_digits: bool,

    /// Reject matches where either side exceeds this value
    #[arg(long = "max-equate-value")]
    pub max_equate_value: Option<f64>,

    /// Memory budget hint used by streaming fallback heuristics (e.g. 512M, 2G)
    #[arg(long = "max-memory")]
    pub max_memory: Option<String>,

    /// Threshold used with --max-memory to trigger streaming fallback
    #[arg(long = "memory-abort-threshold")]
    pub memory_abort_threshold: Option<f64>,

    /// Maximum number of trig operators allowed in accepted matches
    #[arg(long = "max-trig-cycles")]
    pub max_trig_cycles: Option<u32>,

    /// Reject matches where either side is below this value
    #[arg(long = "min-equate-value")]
    pub min_equate_value: Option<f64>,

    /// Lower memory bound hint used by streaming fallback heuristics
    #[arg(long = "min-memory")]
    pub min_memory: Option<String>,

    /// Disable canonical simplification even if requested elsewhere
    #[arg(long = "no-canon-simplify")]
    pub no_canon_simplify: bool,

    /// Suppress compatibility warnings and slow-path informational warnings
    #[arg(long = "no-slow-messages")]
    pub no_slow_messages: bool,

    /// Restrict matches to those sharing target digit-anagram signature
    #[arg(long = "numeric-anagram")]
    pub numeric_anagram: bool,

    /// Restrict accepted matches to rational exponent forms
    #[arg(long = "rational-exponents")]
    pub rational_exponents: bool,

    /// Restrict accepted matches to rational trig arguments
    #[arg(long = "rational-trig-args")]
    pub rational_trig_args: bool,

    /// Compatibility alias for diagnostics channel -Ds (show work)
    #[arg(long = "show-work")]
    pub show_work: bool,

    /// Legacy alias for derivative margin when --derivative-margin is not set
    #[arg(long = "significance-loss-margin")]
    pub significance_loss_margin: Option<f64>,

    /// Scale factor applied to trig arguments during evaluation
    #[arg(long = "trig-argument-scale")]
    pub trig_argument_scale: Option<f64>,

    /// Show verbose output with header and footer details
    #[arg(long)]
    pub verbose: bool,

    /// Run PSLQ integer relation detection on the target
    /// Searches for integer coefficients relating target to known constants
    /// Example: ries-rs 3.14159 --pslq might find x - pi ~= 0
    #[arg(long)]
    pub pslq: bool,

    /// Use extended constant set for PSLQ (includes sqrt(3), sqrt(5), ln(3), etc.)
    #[arg(long)]
    pub pslq_extended: bool,

    /// Maximum coefficient magnitude for PSLQ search (default: 1000)
    #[arg(long, default_value = "1000")]
    pub pslq_max_coeff: i64,

    /// Emit a run manifest JSON file for reproducibility
    /// Contains full configuration and results for academic verification
    #[arg(long, value_name = "FILE")]
    pub emit_manifest: Option<PathBuf>,
}

/// Parse a user constant from CLI argument.
/// Format: "weight:name:description:value"
///
/// This function uses Profile's centralized validation via [`profile::Profile::add_constant`].
pub fn parse_user_constant_from_cli(
    profile: &mut profile::Profile,
    spec: &str,
) -> Result<(), String> {
    let parts: Vec<&str> = spec.split(':').collect();
    if parts.len() != 4 {
        return Err(format!(
            "Expected 4 colon-separated parts, got {}",
            parts.len()
        ));
    }

    let weight: u32 = parts[0]
        .parse()
        .map_err(|_| format!("Invalid weight: {}", parts[0]))?;

    let name = parts[1].to_string();
    let description = parts[2].to_string();

    let value: f64 = parts[3]
        .parse()
        .map_err(|_| format!("Invalid value: {}", parts[3]))?;

    // Use Profile's centralized validation
    profile
        .add_constant(weight, name, description, value)
        .map_err(|e| e.to_string())
}

/// Parse a user-defined function from CLI argument.
/// Format: "weight:name:description:formula"
pub fn parse_user_function_from_cli(
    profile: &mut profile::Profile,
    spec: &str,
) -> Result<(), String> {
    let udf = udf::UserFunction::parse(spec)?;
    profile.functions.push(udf);
    Ok(())
}

/// Parse symbol names from CLI argument.
/// Format: ":p:PI :e:EULER"
pub fn parse_symbol_names_from_cli(
    profile: &mut profile::Profile,
    spec: &str,
) -> Result<(), String> {
    for part in spec.split_whitespace() {
        if !part.starts_with(':') {
            continue;
        }

        let inner = &part[1..];
        let Some(colon_pos) = inner.find(':') else {
            return Err(format!("Invalid symbol name format: {}", part));
        };

        let symbol_char = inner[..colon_pos]
            .chars()
            .next()
            .ok_or_else(|| "Empty symbol in --symbol-names".to_string())?;
        let display_name = inner[colon_pos + 1..].to_string();
        if display_name.is_empty() {
            return Err(format!(
                "Empty replacement name in --symbol-names: {}",
                part
            ));
        }

        let Some(symbol) = symbol::Symbol::from_byte(symbol_char as u8) else {
            return Err(format!("Unknown symbol in --symbol-names: {}", symbol_char));
        };

        profile.symbol_names.insert(symbol, display_name);
    }

    Ok(())
}

/// Parse symbol weights from CLI argument.
/// Format: ":W:20 :p:25"
pub fn parse_symbol_weights_from_cli(
    profile: &mut profile::Profile,
    spec: &str,
) -> Result<(), String> {
    for part in spec.split_whitespace() {
        if !part.starts_with(':') {
            continue;
        }

        let inner = &part[1..];
        let Some(colon_pos) = inner.find(':') else {
            return Err(format!("Invalid symbol weight format: {}", part));
        };

        let symbol_char = inner[..colon_pos]
            .chars()
            .next()
            .ok_or_else(|| "Empty symbol in --symbol-weights".to_string())?;
        let weight: u32 = inner[colon_pos + 1..]
            .parse()
            .map_err(|_| format!("Invalid weight in --symbol-weights: {}", part))?;

        let Some(symbol) = symbol::Symbol::from_byte(symbol_char as u8) else {
            return Err(format!(
                "Unknown symbol in --symbol-weights: {}",
                symbol_char
            ));
        };

        profile.symbol_weights.insert(symbol, weight);
    }

    Ok(())
}

/// Parse -O/--op-limits into per-symbol maximum counts.
pub fn parse_symbol_count_limits(
    spec: &str,
) -> Result<std::collections::HashMap<symbol::Symbol, u32>, String> {
    let mut limits = std::collections::HashMap::new();
    let chars: Vec<char> = spec.chars().collect();
    let mut i = 0;
    while i < chars.len() {
        if chars[i].is_whitespace() {
            i += 1;
            continue;
        }

        let mut saw_digits = false;
        let mut count: u32 = 0;
        while i < chars.len() && chars[i].is_ascii_digit() {
            saw_digits = true;
            count = count
                .saturating_mul(10)
                .saturating_add((chars[i] as u8 - b'0') as u32);
            i += 1;
        }

        if i >= chars.len() {
            return Err("Trailing count with no symbol in -O/--op-limits".to_string());
        }

        let symbol_char = chars[i];
        i += 1;
        if symbol_char.is_whitespace() {
            continue;
        }

        let max_count = if saw_digits { count } else { 1 };
        if max_count == 0 {
            return Err(format!("Invalid zero count for symbol '{}'", symbol_char));
        }

        let Some(sym) = symbol::Symbol::from_byte(symbol_char as u8) else {
            return Err(format!(
                "Unknown symbol '{}' in -O/--op-limits",
                symbol_char
            ));
        };
        limits.insert(sym, max_count);
    }

    Ok(limits)
}

/// Parse effective allowed/excluded symbol sets with optional re-enable set.
pub fn parse_symbol_sets(
    only_symbols: Option<&str>,
    exclude_symbols: Option<&str>,
    enable_symbols: Option<&str>,
) -> (
    Option<std::collections::HashSet<u8>>,
    Option<std::collections::HashSet<u8>>,
) {
    let mut allowed: Option<std::collections::HashSet<u8>> =
        only_symbols.map(|s| s.bytes().collect());
    let mut excluded: Option<std::collections::HashSet<u8>> =
        exclude_symbols.map(|s| s.bytes().collect());

    if let Some(enabled) = enable_symbols {
        if enabled == "all" {
            // Special value "all" clears all exclusions
            excluded = None;
        } else {
            for b in enabled.bytes() {
                if let Some(excl) = excluded.as_mut() {
                    excl.remove(&b);
                }
                if let Some(allow) = allowed.as_mut() {
                    allow.insert(b);
                }
            }
        }
    }

    (allowed, excluded)
}

/// Parse a memory size string into bytes.
///
/// Supports suffixes: K, M, G, T (case-insensitive).
/// Examples: "512M" -> 536870912, "2G" -> 2147483648
pub fn parse_memory_size_bytes(spec: &str) -> Option<u64> {
    let trimmed = spec.trim();
    if trimmed.is_empty() {
        return None;
    }

    let (num_part, suffix) = match trimmed.chars().last().filter(|c| c.is_ascii_alphabetic()) {
        Some(last) => (&trimmed[..trimmed.len() - last.len_utf8()], Some(last)),
        None => (trimmed, None),
    };

    let number: f64 = num_part.trim().parse().ok()?;
    if !number.is_finite() || number < 0.0 {
        return None;
    }

    let mult = match suffix.map(|c| c.to_ascii_uppercase()) {
        None => 1_f64,
        Some('K') => 1024_f64,
        Some('M') => 1024_f64 * 1024_f64,
        Some('G') => 1024_f64 * 1024_f64 * 1024_f64,
        Some('T') => 1024_f64 * 1024_f64 * 1024_f64 * 1024_f64,
        _ => return None,
    };

    Some((number * mult) as u64)
}

/// Check if canonical reduction is enabled from the option value.
pub fn canon_reduction_enabled(spec: Option<&str>) -> bool {
    let Some(value) = spec else {
        return false;
    };
    let lowered = value.trim().to_ascii_lowercase();
    !matches!(lowered.as_str(), "" | "off" | "none" | "0" | "false")
}

/// Print the list of supported options (for --list-options).
pub fn print_option_list() {
    let opts = [
        "--list-options",
        "-p",
        "--include",
        "--any-exponents",
        "--any-subexpressions",
        "--any-trig-args",
        "--canon-reduction",
        "--canon-simplify",
        "--derivative-margin",
        "--eval-expression",
        "--explicit-multiply",
        "--find-expression",
        "--match-all-digits",
        "--mad",
        "--max-equate-value",
        "--max-match-distance",
        "--min-match-distance",
        "--max-matches",
        "--max-memory",
        "--memory-abort-threshold",
        "-X",
        "--constant",
        "--define",
        "--min-equate-value",
        "--max-trig-cycles",
        "--min-memory",
        "--no-canon-simplify",
        "--no-refinement",
        "--no-slow-messages",
        "--no-solve-for-x",
        "--numeric-anagram",
        "--one-sided",
        "--complexity-ranking",
        "--parity-ranking",
        "--rational-exponents",
        "--rational-trig-args",
        "--show-work",
        "--significance-loss-margin",
        "--symbol-weights",
        "--symbol-names",
        "--trig-argument-scale",
        "-s",
        "--try-solve-for-x",
        "--version",
        "--wide",
        "--wide-output",
        "-a",
        "--algebraic-subexpressions",
        "-c",
        "--constructible-subexpressions",
        "-D",
        "-E",
        "-F",
        "-i",
        "--integer-subexpressions",
        "-l",
        "--liouvillian-subexpressions",
        "-N",
        "-O",
        "-r",
        "--rational-subexpressions",
        "-S",
        "-x",
        "--absolute-roots",
        "--relative-roots",
        "--N-RHS",
        "--O-RHS",
        "--S-RHS",
        "--E-RHS",
    ];
    for opt in opts {
        println!("{}", opt);
    }
}

/// Get a description for a symbol (for -S symbol table output).
pub fn sym_description(sym: symbol::Symbol) -> &'static str {
    use symbol::Symbol;
    match sym {
        Symbol::One => "integer",
        Symbol::Two => "integer",
        Symbol::Three => "integer",
        Symbol::Four => "integer",
        Symbol::Five => "integer",
        Symbol::Six => "integer",
        Symbol::Seven => "integer",
        Symbol::Eight => "integer",
        Symbol::Nine => "integer",
        Symbol::Pi => "pi = 3.14159...",
        Symbol::E => "e = base of natural logarithms, 2.71828...",
        Symbol::Phi => "phi = the golden ratio, (1+sqrt(5))/2",
        Symbol::Gamma => "Euler-Mascheroni constant gamma",
        Symbol::Plastic => "plastic constant",
        Symbol::Apery => "Apery's constant zeta(3)",
        Symbol::Catalan => "Catalan's constant",
        Symbol::X => "the variable of the equation",
        Symbol::Neg => "negate",
        Symbol::Recip => "reciprocal",
        Symbol::Sqrt => "sqrt(x) = square root",
        Symbol::Square => "^2 = square",
        Symbol::Ln => "ln(x) = natural logarithm or log base e",
        Symbol::Exp => "natural exponent function",
        Symbol::SinPi => "sinpi(X) = sin(pi * x)",
        Symbol::CosPi => "cospi(X) = cos(pi * x)",
        Symbol::TanPi => "tanpi(X) = tan(pi * x)",
        Symbol::LambertW => "Lambert W function",
        Symbol::Add => "add",
        Symbol::Sub => "subtract",
        Symbol::Mul => "multiply",
        Symbol::Div => "divide",
        Symbol::Pow => "power",
        Symbol::Root => "a-th root of b",
        Symbol::Log => "log base a of b",
        Symbol::Atan2 => "2-argument arctangent",
        _ => "",
    }
}

/// Print the symbol table (for -S without argument).
pub fn print_symbol_table() {
    println!("Explicit values:");
    println!(" sym seft wght name description");
    for sym in symbol::Symbol::constants() {
        let byte = *sym as u8;
        if byte < 128 {
            // Skip user constants
            println!(
                "  {:<2}   {:<1}   {:<3} {:<6} {}",
                byte as char,
                match sym.seft() {
                    symbol::Seft::A => "a",
                    symbol::Seft::B => "b",
                    symbol::Seft::C => "c",
                },
                sym.weight(),
                sym.name(),
                sym_description(*sym)
            );
        }
    }

    println!("\nFunctions of one argument:");
    println!(" sym seft wght name description");
    for sym in symbol::Symbol::unary_ops() {
        let byte = *sym as u8;
        println!(
            "  {:<2}   {:<1}   {:<3} {:<6} {}",
            byte as char,
            match sym.seft() {
                symbol::Seft::A => "a",
                symbol::Seft::B => "b",
                symbol::Seft::C => "c",
            },
            sym.weight(),
            sym.name(),
            sym_description(*sym)
        );
    }

    println!("\nFunctions of two arguments:");
    println!(" sym seft wght name description");
    for sym in symbol::Symbol::binary_ops() {
        let byte = *sym as u8;
        println!(
            "  {:<2}   {:<1}   {:<3} {:<6} {}",
            byte as char,
            match sym.seft() {
                symbol::Seft::A => "a",
                symbol::Seft::B => "b",
                symbol::Seft::C => "c",
            },
            sym.weight(),
            sym.name(),
            sym_description(*sym)
        );
    }
}

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

    #[test]
    fn test_parse_memory_size() {
        assert_eq!(parse_memory_size_bytes("512M"), Some(512 * 1024 * 1024));
        assert_eq!(parse_memory_size_bytes("2G"), Some(2 * 1024 * 1024 * 1024));
        assert_eq!(parse_memory_size_bytes("1024"), Some(1024));
        assert_eq!(parse_memory_size_bytes("1k"), Some(1024));
    }

    #[test]
    fn test_parse_symbol_sets() {
        let (allowed, excluded) = parse_symbol_sets(Some("abc"), Some("d"), None);
        assert_eq!(allowed, Some(vec![b'a', b'b', b'c'].into_iter().collect()));
        assert_eq!(excluded, Some(vec![b'd'].into_iter().collect()));

        // Test enable all
        let (_, excluded) = parse_symbol_sets(None, Some("abc"), Some("all"));
        assert!(excluded.is_none());
    }
}