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
//! Comprehensive content validation tests for limit educational explanations
//!
//! These tests validate that limit educational explanations contain correct
//! mathematical content and follow proper pedagogical structure.

use mathhook_core::calculus::limits::educational::LimitEducation;
use mathhook_core::{symbol, Expression};
use std::sync::Arc;

// Helper function to check if any step contains the target text
fn has_step_containing(
    explanation: &mathhook_core::educational::enhanced_steps::EnhancedStepExplanation,
    target: &str,
) -> bool {
    let target_lower = target.to_lowercase();
    explanation.steps.iter().any(|step| {
        step.human_message.to_lowercase().contains(&target_lower)
            || step.title.to_lowercase().contains(&target_lower)
    })
}

// Helper function to count steps in an explanation
fn count_steps(
    explanation: &mathhook_core::educational::enhanced_steps::EnhancedStepExplanation,
) -> usize {
    explanation.steps.len()
}

#[test]
fn test_direct_substitution_explained() {
    let x = symbol!(x);
    let expr = Expression::pow(Expression::symbol(x.clone()), Expression::integer(2));
    let point = Expression::integer(2);
    let result = Expression::integer(4);

    let explanation = LimitEducation::direct_substitution_explanation(&expr, &x, &point, &result);

    assert!(
        count_steps(&explanation) >= 3,
        "Direct substitution should have at least 3 steps"
    );

    assert!(
        has_step_containing(&explanation, "direct substitution")
            || has_step_containing(&explanation, "substitute"),
        "Should mention direct substitution"
    );

    assert!(
        has_step_containing(&explanation, "2") && has_step_containing(&explanation, "4"),
        "Should show the substitution and result"
    );

    assert!(
        has_step_containing(&explanation, "indeterminate")
            || has_step_containing(&explanation, "well-defined"),
        "Should verify the form is not indeterminate"
    );
}

#[test]
fn test_direct_substitution_polynomial() {
    let x = symbol!(x);
    let expr = Expression::add(vec![
        Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
        Expression::mul(vec![Expression::integer(3), Expression::symbol(x.clone())]),
        Expression::integer(1),
    ]);
    let point = Expression::integer(2);
    let result = Expression::integer(11);

    let explanation = LimitEducation::direct_substitution_explanation(&expr, &x, &point, &result);

    assert!(count_steps(&explanation) >= 3);
    assert!(has_step_containing(&explanation, "substitute"));
    assert!(has_step_containing(&explanation, "11"));
}

#[test]
fn test_indeterminate_form_detected() {
    let x = symbol!(x);
    let numerator = Expression::add(vec![
        Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
        Expression::integer(-1),
    ]);
    let denominator = Expression::add(vec![Expression::symbol(x.clone()), Expression::integer(-1)]);
    let expr = Expression::mul(vec![
        numerator.clone(),
        Expression::pow(denominator.clone(), Expression::integer(-1)),
    ]);
    let point = Expression::integer(1);

    let explanation = LimitEducation::indeterminate_form_explanation(&expr, &x, &point, "0/0");

    assert!(
        count_steps(&explanation) >= 3,
        "Indeterminate form detection should have at least 3 steps"
    );

    assert!(
        has_step_containing(&explanation, "0/0")
            || has_step_containing(&explanation, "indeterminate")
            || has_step_containing(&explanation, "form"),
        "Should identify indeterminate form"
    );

    assert!(
        has_step_containing(&explanation, "factorization")
            || has_step_containing(&explanation, "l'hopital")
            || has_step_containing(&explanation, "resolution"),
        "Should suggest resolution strategy"
    );
}

#[test]
fn test_indeterminate_form_components_evaluated() {
    let x = symbol!(x);
    let numerator = Expression::symbol(x.clone());
    let denominator = Expression::symbol(x.clone());
    // Use raw Mul constructor to bypass simplification (x/x = 1)
    // This preserves the fraction structure for educational analysis
    let expr = Expression::Mul(Arc::new(vec![
        numerator,
        Expression::Pow(Arc::new(denominator), Arc::new(Expression::integer(-1))),
    ]));
    let point = Expression::integer(0);

    let explanation = LimitEducation::indeterminate_form_explanation(&expr, &x, &point, "0/0");

    assert!(count_steps(&explanation) >= 4);
    assert!(
        has_step_containing(&explanation, "numerator")
            && has_step_containing(&explanation, "denominator")
    );
}

#[test]
fn test_lhopital_rule_applied() {
    let x = symbol!(x);
    let numerator = Expression::symbol(x.clone());
    let denominator = Expression::symbol(x.clone());
    let point = Expression::integer(0);

    let explanation =
        LimitEducation::lhopital_rule_explanation(&numerator, &denominator, &x, &point);

    assert!(
        count_steps(&explanation) >= 6,
        "L'Hopital's rule should have at least 6 steps"
    );

    assert!(
        has_step_containing(&explanation, "l'hopital")
            || has_step_containing(&explanation, "l'hospital"),
        "Should mention L'Hopital's rule"
    );

    assert!(
        has_step_containing(&explanation, "differentiate numerator")
            || has_step_containing(&explanation, "derivative of numerator"),
        "Should differentiate numerator"
    );

    assert!(
        has_step_containing(&explanation, "differentiate denominator")
            || has_step_containing(&explanation, "derivative of denominator"),
        "Should differentiate denominator"
    );

    assert!(
        has_step_containing(&explanation, "0/0")
            || has_step_containing(&explanation, "indeterminate"),
        "Should identify indeterminate form"
    );
}

#[test]
fn test_lhopital_rule_complete_process() {
    let x = symbol!(x);
    let numerator = Expression::function("sin", vec![Expression::symbol(x.clone())]);
    let denominator = Expression::symbol(x.clone());
    let point = Expression::integer(0);

    let explanation =
        LimitEducation::lhopital_rule_explanation(&numerator, &denominator, &x, &point);

    assert!(count_steps(&explanation) >= 6);
    assert!(
        has_step_containing(&explanation, "state") || has_step_containing(&explanation, "rule")
    );
    assert!(has_step_containing(&explanation, "apply"));
    assert!(has_step_containing(&explanation, "evaluate"));
}

#[test]
fn test_limit_laws_explained() {
    let x = symbol!(x);
    let expr = Expression::add(vec![
        Expression::mul(vec![
            Expression::integer(3),
            Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
        ]),
        Expression::mul(vec![Expression::integer(2), Expression::symbol(x.clone())]),
    ]);
    let point = Expression::integer(2);

    let explanation = LimitEducation::limit_laws_explanation(&expr, &x, &point);

    assert!(
        count_steps(&explanation) >= 4,
        "Limit laws should have at least 4 steps"
    );

    assert!(
        has_step_containing(&explanation, "sum law")
            || has_step_containing(&explanation, "product law")
            || has_step_containing(&explanation, "constant multiple"),
        "Should mention limit laws"
    );

    assert!(
        has_step_containing(&explanation, "combine")
            || has_step_containing(&explanation, "individual"),
        "Should combine or evaluate individual limits"
    );
}

#[test]
fn test_limit_laws_sum_explained() {
    let x = symbol!(x);
    let expr = Expression::add(vec![Expression::symbol(x.clone()), Expression::integer(5)]);
    let point = Expression::integer(3);

    let explanation = LimitEducation::limit_laws_explanation(&expr, &x, &point);

    assert!(count_steps(&explanation) >= 4);
    assert!(has_step_containing(&explanation, "sum") || has_step_containing(&explanation, "add"));
}

#[test]
fn test_limit_laws_product_explained() {
    let x = symbol!(x);
    let expr = Expression::mul(vec![
        Expression::symbol(x.clone()),
        Expression::add(vec![Expression::symbol(x.clone()), Expression::integer(1)]),
    ]);
    let point = Expression::integer(2);

    let explanation = LimitEducation::limit_laws_explanation(&expr, &x, &point);

    assert!(count_steps(&explanation) >= 4);
    assert!(
        has_step_containing(&explanation, "product")
            || has_step_containing(&explanation, "constant multiple")
            || has_step_containing(&explanation, "factor")
    );
}

#[test]
fn test_limit_at_infinity_technique() {
    let x = symbol!(x);
    let numerator = Expression::add(vec![
        Expression::mul(vec![
            Expression::integer(3),
            Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
        ]),
        Expression::mul(vec![Expression::integer(2), Expression::symbol(x.clone())]),
    ]);
    let denominator = Expression::add(vec![
        Expression::pow(Expression::symbol(x.clone()), Expression::integer(2)),
        Expression::integer(1),
    ]);
    let expr = Expression::mul(vec![
        numerator,
        Expression::pow(denominator, Expression::integer(-1)),
    ]);

    let explanation = LimitEducation::limit_at_infinity_explanation(&expr, &x);

    assert!(
        count_steps(&explanation) >= 2,
        "Limits at infinity should have at least 2 steps"
    );

    assert!(
        has_step_containing(&explanation, "divide by highest power")
            || has_step_containing(&explanation, "highest power")
            || has_step_containing(&explanation, "dominant")
            || has_step_containing(&explanation, "infinity"),
        "Should mention technique for limits at infinity"
    );

    assert!(
        has_step_containing(&explanation, "infinity"),
        "Should explain behavior as x approaches infinity"
    );
}

#[test]
fn test_limit_at_infinity_polynomial() {
    let x = symbol!(x);
    let expr = Expression::pow(Expression::symbol(x.clone()), Expression::integer(3));

    let explanation = LimitEducation::limit_at_infinity_explanation(&expr, &x);

    assert!(count_steps(&explanation) >= 4);
    assert!(has_step_containing(&explanation, "infinity"));
    assert!(
        has_step_containing(&explanation, "dominant") || has_step_containing(&explanation, "power")
    );
}

#[test]
fn test_all_methods_produce_valid_json() {
    let x = symbol!(x);
    let expr = Expression::pow(Expression::symbol(x.clone()), Expression::integer(2));
    let point = Expression::integer(2);
    let result = Expression::integer(4);

    let direct_explanation =
        LimitEducation::direct_substitution_explanation(&expr, &x, &point, &result);
    assert!(
        direct_explanation.to_json().is_ok(),
        "Direct substitution should produce valid JSON"
    );

    let indet_expr = Expression::mul(vec![
        Expression::symbol(x.clone()),
        Expression::pow(Expression::symbol(x.clone()), Expression::integer(-1)),
    ]);
    let indet_explanation = LimitEducation::indeterminate_form_explanation(
        &indet_expr,
        &x,
        &Expression::integer(0),
        "0/0",
    );
    assert!(
        indet_explanation.to_json().is_ok(),
        "Indeterminate form should produce valid JSON"
    );

    let numerator = Expression::symbol(x.clone());
    let denominator = Expression::symbol(x.clone());
    let lhopital_explanation =
        LimitEducation::lhopital_rule_explanation(&numerator, &denominator, &x, &point);
    assert!(
        lhopital_explanation.to_json().is_ok(),
        "L'Hopital should produce valid JSON"
    );

    let laws_explanation = LimitEducation::limit_laws_explanation(&expr, &x, &point);
    assert!(
        laws_explanation.to_json().is_ok(),
        "Limit laws should produce valid JSON"
    );

    let infinity_explanation = LimitEducation::limit_at_infinity_explanation(&expr, &x);
    assert!(
        infinity_explanation.to_json().is_ok(),
        "Limit at infinity should produce valid JSON"
    );
}

#[test]
fn test_explanations_use_correct_variable_names() {
    let theta = symbol!(theta);
    let expr = Expression::pow(Expression::symbol(theta.clone()), Expression::integer(2));
    let point = Expression::rational(1, 2);
    let result = Expression::rational(1, 4);

    let explanation =
        LimitEducation::direct_substitution_explanation(&expr, &theta, &point, &result);

    assert!(
        has_step_containing(&explanation, "theta"),
        "Should use correct variable name 'theta'"
    );
}

#[test]
fn test_step_ordering_logical() {
    let x = symbol!(x);
    let numerator = Expression::symbol(x.clone());
    let denominator = Expression::symbol(x.clone());
    let point = Expression::integer(0);

    let explanation =
        LimitEducation::lhopital_rule_explanation(&numerator, &denominator, &x, &point);

    let steps = &explanation.steps;
    assert!(steps.len() >= 6);

    assert!(
        steps[0].title.to_lowercase().contains("check")
            || steps[0].title.to_lowercase().contains("condition")
    );

    let has_state_before_apply = steps.windows(2).any(|window| {
        window[0].title.to_lowercase().contains("state")
            && window[1].title.to_lowercase().contains("differentiate")
    });
    assert!(
        has_state_before_apply || steps[1].title.to_lowercase().contains("state"),
        "Should state rule before applying it"
    );
}

#[test]
fn test_all_explanations_have_minimum_steps() {
    let x = symbol!(x);
    let expr = Expression::pow(Expression::symbol(x.clone()), Expression::integer(2));
    let point = Expression::integer(2);
    let result = Expression::integer(4);

    let direct = LimitEducation::direct_substitution_explanation(&expr, &x, &point, &result);
    assert!(
        count_steps(&direct) >= 3,
        "Direct substitution needs 3+ steps"
    );

    let indet_expr = Expression::mul(vec![
        Expression::symbol(x.clone()),
        Expression::pow(Expression::symbol(x.clone()), Expression::integer(-1)),
    ]);
    let indet = LimitEducation::indeterminate_form_explanation(
        &indet_expr,
        &x,
        &Expression::integer(0),
        "0/0",
    );
    assert!(
        count_steps(&indet) >= 3,
        "Indeterminate form needs 3+ steps"
    );

    let numerator = Expression::symbol(x.clone());
    let denominator = Expression::symbol(x.clone());
    let lhopital = LimitEducation::lhopital_rule_explanation(&numerator, &denominator, &x, &point);
    assert!(count_steps(&lhopital) >= 6, "L'Hopital needs 6+ steps");

    let laws = LimitEducation::limit_laws_explanation(&expr, &x, &point);
    assert!(count_steps(&laws) >= 3, "Limit laws need 3+ steps");

    let infinity = LimitEducation::limit_at_infinity_explanation(&expr, &x);
    assert!(
        count_steps(&infinity) >= 2,
        "Limit at infinity needs 2+ steps"
    );
}