mathhook-core 0.2.0

Core mathematical engine for MathHook - expressions, algebra, and solving
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
//! Step generation methods including factories and builders

use crate::core::{Expression, Symbol};
use crate::educational::step_by_step::Step;
use serde::{Deserialize, Serialize};
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

use super::formatting::FormatContext;

/// Enhanced step with human and API data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnhancedStep {
    pub step_id: String,
    pub title: String,
    pub human_message: String,
    pub api_data: StepApiData,
    pub message_key: MessageKey,
    pub math_context: MathContext,
    pub presentation: super::formatting::PresentationHints,
}

/// Step API data for external applications
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepApiData {
    pub category: String,
    pub step_type: String,
    pub operation: String,
    pub inputs: HashMap<String, String>,
    pub outputs: HashMap<String, String>,
    pub properties: HashMap<String, serde_json::Value>,
}

/// Message key for external message systems
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageKey {
    pub category: String,
    pub message_type: String,
    pub variant: u32,
    pub hash: u64,
    pub template_params: Vec<String>,
}

/// Mathematical context information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MathContext {
    pub equation: String,
    pub variable: String,
    pub current_state: String,
    pub coefficients: HashMap<String, String>,
    pub progress: f64,
    pub equation_type: String,
}

/// Enhanced step builder
pub struct EnhancedStepBuilder {
    step_id: String,
    title: String,
    human_message: String,
    api_data: StepApiData,
    message_key: MessageKey,
    math_context: MathContext,
    presentation: super::formatting::PresentationHints,
}

impl EnhancedStepBuilder {
    /// Create new enhanced step builder
    pub fn new(step_id: &str) -> Self {
        Self {
            step_id: step_id.to_owned(),
            title: String::new(),
            human_message: String::new(),
            api_data: StepApiData {
                category: String::new(),
                step_type: String::new(),
                operation: String::new(),
                inputs: HashMap::new(),
                outputs: HashMap::new(),
                properties: HashMap::new(),
            },
            message_key: MessageKey {
                category: String::new(),
                message_type: String::new(),
                variant: 0,
                hash: 0,
                template_params: Vec::new(),
            },
            math_context: MathContext {
                equation: String::new(),
                variable: String::new(),
                current_state: String::new(),
                coefficients: HashMap::new(),
                progress: 0.0,
                equation_type: String::new(),
            },
            presentation: super::formatting::PresentationHints::default(),
        }
    }

    /// Set human-readable content
    pub fn with_human_message(mut self, title: &str, message: &str) -> Self {
        self.title = title.to_owned();
        self.human_message = message.to_owned();
        self
    }

    /// Set API data
    pub fn with_api_data(mut self, category: &str, step_type: &str, operation: &str) -> Self {
        self.api_data.category = category.to_owned();
        self.api_data.step_type = step_type.to_owned();
        self.api_data.operation = operation.to_owned();
        self
    }

    /// Add input data
    pub fn with_input(mut self, key: &str, value: &str) -> Self {
        self.api_data
            .inputs
            .insert(key.to_owned(), value.to_owned());
        self
    }

    /// Add output data
    pub fn with_output(mut self, key: &str, value: &str) -> Self {
        self.api_data
            .outputs
            .insert(key.to_owned(), value.to_owned());
        self
    }

    /// Set mathematical context
    pub fn with_math_context(mut self, equation: &str, variable: &str, progress: f64) -> Self {
        self.math_context.equation = equation.to_owned();
        self.math_context.variable = variable.to_owned();
        self.math_context.progress = progress;
        self
    }

    /// Set message key for external systems
    pub fn with_message_key(mut self, category: &str, message_type: &str, variant: u32) -> Self {
        self.message_key.category = category.to_owned();
        self.message_key.message_type = message_type.to_owned();
        self.message_key.variant = variant;

        let mut hasher = DefaultHasher::new();
        category.hash(&mut hasher);
        message_type.hash(&mut hasher);
        variant.hash(&mut hasher);
        self.message_key.hash = hasher.finish();

        self
    }

    /// Set presentation hints
    pub fn with_presentation(mut self, color: &str, importance: u8, animation: &str) -> Self {
        self.presentation.color_theme = color.to_owned();
        self.presentation.importance = importance;
        self.presentation.animation = animation.to_owned();
        self
    }

    /// Build the enhanced step
    pub fn build(self) -> EnhancedStep {
        EnhancedStep {
            step_id: self.step_id,
            title: self.title,
            human_message: self.human_message,
            api_data: self.api_data,
            message_key: self.message_key,
            math_context: self.math_context,
            presentation: self.presentation,
        }
    }
}

/// Smart step type (legacy compatibility)
pub type SmartStep = Step;

/// Smart step builder for legacy system
pub struct SmartStepBuilder {
    title: String,
}

impl SmartStepBuilder {
    pub fn new(title: &str) -> Self {
        Self {
            title: title.to_owned(),
        }
    }

    pub fn with_human_message(self, title: &str, message: &str) -> Self {
        Self {
            title: format!("{}: {}", title, message),
        }
    }

    pub fn with_api_data(self, _category: &str, _step_type: &str, _operation: &str) -> Self {
        self
    }

    pub fn with_input(self, _key: &str, _value: &str) -> Self {
        self
    }

    pub fn with_output(self, _key: &str, _value: &str) -> Self {
        self
    }

    pub fn with_math_context(self, _equation: &str, _variable: &str, _progress: f64) -> Self {
        self
    }

    pub fn with_message_key(self, _category: &str, _message_type: &str, _variant: u32) -> Self {
        self
    }

    pub fn with_presentation(self, _color: &str, _importance: u8, _animation: &str) -> Self {
        self
    }

    pub fn build(self) -> SmartStep {
        Step::new(self.title, "Enhanced step content".to_owned())
    }
}

/// Step factory for creating enhanced steps
pub struct StepFactory;

/// Smart step factory (alias)
pub type SmartStepFactory = StepFactory;

/// Enhanced step factory (alias)
pub type EnhancedStepFactory = StepFactory;

impl StepFactory {
    /// Generate linear equation introduction step
    pub fn linear_introduction(equation: &Expression, variable: &Symbol) -> EnhancedStep {
        Self::linear_introduction_with_format(equation, variable, &FormatContext::default())
    }

    /// Generate linear equation introduction step with custom format
    pub fn linear_introduction_with_format(
        equation: &Expression,
        variable: &Symbol,
        context: &FormatContext,
    ) -> EnhancedStep {
        let equation_formatted = context.format_expression_safe(equation);

        EnhancedStepBuilder::new("linear_intro_001")
            .with_human_message(
                "Given Equation",
                &format!("We need to solve: {} = 0\nThis is a linear equation because {} appears only to the first power.",
                        equation_formatted, variable.name())
            )
            .with_api_data("linear_equation", "introduction", "equation_analysis")
            .with_input("original_equation", &equation_formatted)
            .with_input("variable", variable.name())
            .with_output("equation_type", "linear")
            .with_output("complexity", "simple")
            .with_math_context(&equation_formatted, variable.name(), 0.1)
            .with_message_key("linear", "introduction", 0)
            .with_presentation("blue", 4, "slide-in")
            .build()
    }

    /// Generate linear equation strategy step
    pub fn linear_strategy(variable: &Symbol) -> EnhancedStep {
        Self::linear_strategy_with_format(variable, &FormatContext::default())
    }

    /// Generate linear equation strategy step with custom format
    pub fn linear_strategy_with_format(
        variable: &Symbol,
        _context: &FormatContext,
    ) -> EnhancedStep {
        EnhancedStepBuilder::new("linear_strategy_001")
            .with_human_message(
                "Solution Strategy",
                &format!("To solve for {}, we'll isolate it using inverse operations.\nWhatever operation is applied to {}, we'll undo it step by step.",
                        variable.name(), variable.name())
            )
            .with_api_data("linear_equation", "strategy", "isolation_method")
            .with_input("variable", variable.name())
            .with_output("method", "inverse_operations")
            .with_output("approach", "systematic_isolation")
            .with_math_context("", variable.name(), 0.2)
            .with_message_key("linear", "strategy", 0)
            .with_presentation("green", 3, "fade-in")
            .build()
    }

    /// Generate coefficient identification step
    pub fn linear_coefficient_identification(
        a: &Expression,
        b: &Expression,
        variable: &Symbol,
    ) -> SmartStep {
        Self::linear_coefficient_identification_with_format(
            a,
            b,
            variable,
            &FormatContext::default(),
        )
    }

    /// Generate coefficient identification step with custom format
    pub fn linear_coefficient_identification_with_format(
        a: &Expression,
        b: &Expression,
        variable: &Symbol,
        context: &FormatContext,
    ) -> SmartStep {
        let a_formatted = context.format_expression_safe(a);
        let b_formatted = context.format_expression_safe(b);

        SmartStepBuilder::new("linear_coeffs_001")
            .with_human_message(
                "Identify Components",
                &format!("In our equation, we can identify:\n• Coefficient of {}: {}\n• Constant term: {}\nForm: {}·{} + {} = 0",
                        variable.name(), a_formatted, b_formatted, a_formatted, variable.name(), b_formatted)
            )
            .with_api_data("linear_equation", "analysis", "coefficient_extraction")
            .with_input("variable", variable.name())
            .with_input("coefficient", &a_formatted)
            .with_input("constant", &b_formatted)
            .with_output("standard_form", &format!("{}x + {}", a_formatted, b_formatted))
            .with_math_context("", variable.name(), 0.4)
            .with_message_key("linear", "analysis", 0)
            .with_presentation("orange", 4, "highlight")
            .build()
    }

    /// Generate solution calculation step
    pub fn linear_solution_calculation(
        variable: &Symbol,
        solution: &Expression,
        a: &Expression,
        b: &Expression,
    ) -> SmartStep {
        Self::linear_solution_calculation_with_format(
            variable,
            solution,
            a,
            b,
            &FormatContext::default(),
        )
    }

    /// Generate solution calculation step with custom format
    pub fn linear_solution_calculation_with_format(
        variable: &Symbol,
        solution: &Expression,
        a: &Expression,
        b: &Expression,
        context: &FormatContext,
    ) -> SmartStep {
        let a_formatted = context.format_expression_safe(a);
        let b_formatted = context.format_expression_safe(b);
        let solution_formatted = context.format_expression_safe(solution);

        SmartStepBuilder::new("linear_calc_001")
            .with_human_message(
                "Calculate Solution",
                &format!("Using the linear equation formula:\n{} = -({}) ÷ ({})\n{} = {}\nThis gives us our solution.",
                        variable.name(), b_formatted, a_formatted, variable.name(), solution_formatted)
            )
            .with_api_data("linear_equation", "calculation", "division_operation")
            .with_input("numerator", &format!("-({})", b_formatted))
            .with_input("denominator", &a_formatted)
            .with_input("variable", variable.name())
            .with_output("solution", &solution_formatted)
            .with_output("calculation_method", "division")
            .with_math_context("", variable.name(), 0.8)
            .with_message_key("linear", "calculation", 0)
            .with_presentation("purple", 5, "calculate")
            .build()
    }

    /// Generate verification step
    pub fn linear_verification(
        equation: &Expression,
        variable: &Symbol,
        solution: &Expression,
    ) -> SmartStep {
        Self::linear_verification_with_format(
            equation,
            variable,
            solution,
            &FormatContext::default(),
        )
    }

    /// Generate verification step with custom format
    pub fn linear_verification_with_format(
        equation: &Expression,
        variable: &Symbol,
        solution: &Expression,
        context: &FormatContext,
    ) -> SmartStep {
        let equation_formatted = context.format_expression_safe(equation);
        let solution_formatted = context.format_expression_safe(solution);

        let verification_text = format!(
            "Substitute {} = {} into original equation",
            variable.name(),
            solution_formatted
        );

        SmartStepBuilder::new("linear_verify_001")
            .with_human_message(
                "Verify Solution",
                &format!(
                    "Let's check our answer:\n{}\nResult: The equation is satisfied.",
                    verification_text
                ),
            )
            .with_api_data("linear_equation", "verification", "substitution_check")
            .with_input("original_equation", &equation_formatted)
            .with_input("variable", variable.name())
            .with_input("solution", &solution_formatted)
            .with_output("verification_result", "success")
            .with_output("substitution", &verification_text)
            .with_math_context(&equation_formatted, variable.name(), 1.0)
            .with_message_key("linear", "verification", 0)
            .with_presentation("green", 4, "success")
            .build()
    }

    /// Generate no solution step
    pub fn linear_no_solution(equation: &Expression) -> SmartStep {
        Self::linear_no_solution_with_format(equation, &FormatContext::default())
    }

    /// Generate no solution step with custom format
    pub fn linear_no_solution_with_format(
        equation: &Expression,
        context: &FormatContext,
    ) -> SmartStep {
        let equation_formatted = context.format_expression_safe(equation);

        SmartStepBuilder::new("linear_no_solution_001")
            .with_human_message(
                "No Solution",
                &format!("This equation: {} = 0\nSimplifies to a contradiction (like 5 = 0).\nTherefore, no solution exists.",
                        equation_formatted)
            )
            .with_api_data("linear_equation", "error", "no_solution")
            .with_input("equation", &equation_formatted)
            .with_output("result_type", "no_solution")
            .with_output("reason", "contradiction")
            .with_math_context(&equation_formatted, "", 1.0)
            .with_message_key("linear", "error", 0)
            .with_presentation("red", 5, "alert")
            .build()
    }

    /// Generate infinite solutions step
    pub fn linear_infinite_solutions(equation: &Expression, variable: &Symbol) -> SmartStep {
        Self::linear_infinite_solutions_with_format(equation, variable, &FormatContext::default())
    }

    /// Generate infinite solutions step with custom format
    pub fn linear_infinite_solutions_with_format(
        equation: &Expression,
        variable: &Symbol,
        context: &FormatContext,
    ) -> SmartStep {
        let equation_formatted = context.format_expression_safe(equation);

        SmartStepBuilder::new("linear_infinite_001")
            .with_human_message(
                "Infinite Solutions",
                &format!("This equation: {} = 0\nSimplifies to 0 = 0, which is always true.\nTherefore, any value of {} is a solution.",
                        equation_formatted, variable.name())
            )
            .with_api_data("linear_equation", "result", "infinite_solutions")
            .with_input("equation", &equation_formatted)
            .with_input("variable", variable.name())
            .with_output("result_type", "infinite_solutions")
            .with_output("reason", "identity_equation")
            .with_math_context(&equation_formatted, variable.name(), 1.0)
            .with_message_key("linear", "infinite", 0)
            .with_presentation("blue", 4, "expand")
            .build()
    }
}

/// Difficulty level for educational content
#[derive(Debug, Clone, PartialEq)]
pub enum DifficultyLevel {
    Beginner,
    Intermediate,
    Advanced,
}

/// Educational result wrapper
#[derive(Debug, Clone, PartialEq)]
pub struct EducationalResult {
    pub result: crate::core::Expression,
    pub difficulty: DifficultyLevel,
}