mathhook_core/functions/
number_theory.rs

1//! Number Theory Function Intelligence
2//!
3//! Complete mathematical intelligence for number theory functions:
4//! gcd, lcm, mod, prime operations with existing algorithm integration.
5
6use crate::core::Expression;
7use crate::expr;
8use crate::functions::properties::*;
9use std::collections::HashMap;
10
11/// Number Theory Function Intelligence
12///
13/// Dedicated intelligence system for number theory functions
14/// with integration to existing mathematical algorithms.
15pub struct NumberTheoryIntelligence {
16    /// Function properties for each number theory function
17    properties: HashMap<String, FunctionProperties>,
18}
19
20impl Default for NumberTheoryIntelligence {
21    fn default() -> Self {
22        Self::new()
23    }
24}
25
26impl NumberTheoryIntelligence {
27    /// Create new number theory intelligence system
28    pub fn new() -> Self {
29        let mut intelligence = Self {
30            properties: HashMap::with_capacity(8),
31        };
32
33        intelligence.initialize_gcd_lcm();
34        intelligence.initialize_modular_arithmetic();
35        intelligence.initialize_prime_functions();
36
37        intelligence
38    }
39
40    /// Get all number theory function properties
41    pub fn get_all_properties(&self) -> HashMap<String, FunctionProperties> {
42        self.properties.clone()
43    }
44
45    /// Check if function has number theory intelligence
46    pub fn has_intelligence(&self, name: &str) -> bool {
47        self.properties.contains_key(name)
48    }
49
50    fn initialize_gcd_lcm(&mut self) {
51        self.properties.insert(
52            "gcd".to_owned(),
53            FunctionProperties::Elementary(Box::new(ElementaryProperties {
54                derivative_rule: None, // GCD is not differentiable
55                antiderivative_rule: None,
56                special_values: vec![SpecialValue {
57                    input: "0".to_owned(),
58                    output: expr!(b),
59                    latex_explanation: "\\gcd(0, b) = |b|".to_owned(),
60                }],
61                identities: Box::new(vec![]),
62                domain_range: Box::new(DomainRangeData {
63                    domain: Domain::Integer,
64                    range: Range::PositiveInteger,
65                    singularities: vec![],
66                }),
67                wolfram_name: None,
68                periodicity: None, // Uses existing symbolic implementation
69            })),
70        );
71
72        // LCM function - integrates with existing Expression::lcm implementation
73        self.properties.insert(
74            "lcm".to_owned(),
75            FunctionProperties::Elementary(Box::new(ElementaryProperties {
76                derivative_rule: None, // LCM is not differentiable
77                antiderivative_rule: None,
78                special_values: vec![SpecialValue {
79                    input: "1".to_owned(),
80                    output: expr!(b),
81                    latex_explanation: "\\text{lcm}(1, b) = |b|".to_owned(),
82                }],
83                identities: Box::new(vec![]),
84                domain_range: Box::new(DomainRangeData {
85                    domain: Domain::Integer,
86                    range: Range::PositiveInteger,
87                    singularities: vec![],
88                }),
89                wolfram_name: None,
90                periodicity: None, // Uses existing symbolic implementation
91            })),
92        );
93    }
94
95    /// Initialize modular arithmetic functions
96    fn initialize_modular_arithmetic(&mut self) {
97        // MOD function for modular reduction
98        self.properties.insert(
99            "mod".to_owned(),
100            FunctionProperties::Elementary(Box::new(ElementaryProperties {
101                derivative_rule: None,
102                antiderivative_rule: None,
103                special_values: vec![SpecialValue {
104                    input: "0".to_owned(),
105                    output: Expression::integer(0),
106                    latex_explanation: "a \\bmod m = 0 \\text{ when } a = 0".to_owned(),
107                }],
108                identities: Box::new(vec![]),
109                domain_range: Box::new(DomainRangeData {
110                    domain: Domain::Integer,
111                    range: Range::Integer,
112                    singularities: vec![],
113                }),
114                wolfram_name: None,
115                periodicity: None, // Uses existing modular arithmetic
116            })),
117        );
118    }
119
120    /// Initialize prime-related functions
121    fn initialize_prime_functions(&mut self) {
122        // IS_PRIME function
123        self.properties.insert(
124            "is_prime".to_owned(),
125            FunctionProperties::Elementary(Box::new(ElementaryProperties {
126                derivative_rule: None,
127                antiderivative_rule: None,
128                special_values: vec![SpecialValue {
129                    input: "2".to_owned(),
130                    output: Expression::integer(1), // Use 1 for true, 0 for false
131                    latex_explanation: "2 \\text{ is prime}".to_owned(),
132                }],
133                identities: Box::new(vec![]),
134                domain_range: Box::new(DomainRangeData {
135                    domain: Domain::PositiveInteger,
136                    range: Range::Integer,
137                    singularities: vec![],
138                }),
139                wolfram_name: None,
140                periodicity: None, // Uses existing prime testing algorithms
141            })),
142        );
143    }
144}