mathlex-eval 0.1.1

Numerical evaluator for mathlex ASTs with broadcasting support
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
//! Integration tests for broadcasting semantics.
//!
//! Broadcasting rules:
//! - Scalar inputs contribute no axis to the output shape.
//! - Array inputs each contribute one axis; axis order follows argument
//!   declaration order (first appearance in the AST, left-to-right DFS).
//! - The output is the Cartesian product over all array axes, row-major.
//! - An empty array input produces an empty output.

use std::collections::HashMap;

use approx::assert_abs_diff_eq;
use mathlex::{BinaryOp, Expression};

use mathlex_eval::{EvalError, EvalInput, NumericResult, compile, eval};

// ---------------------------------------------------------------------------
// AST helpers
// ---------------------------------------------------------------------------

fn var(name: &str) -> Expression {
    Expression::Variable(name.into())
}

fn int(v: i64) -> Expression {
    Expression::Integer(v)
}

/// Build the AST for `x^2 + y`.
fn ast_x_sq_plus_y() -> Expression {
    let x_sq = Expression::Binary {
        op: BinaryOp::Pow,
        left: Box::new(var("x")),
        right: Box::new(int(2)),
    };
    Expression::Binary {
        op: BinaryOp::Add,
        left: Box::new(x_sq),
        right: Box::new(var("y")),
    }
}

/// Build the AST for `x + y`.
fn ast_x_plus_y() -> Expression {
    Expression::Binary {
        op: BinaryOp::Add,
        left: Box::new(var("x")),
        right: Box::new(var("y")),
    }
}

/// Build the AST for `x * 2`.
fn ast_x_times_two() -> Expression {
    Expression::Binary {
        op: BinaryOp::Mul,
        left: Box::new(var("x")),
        right: Box::new(int(2)),
    }
}

fn no_constants() -> HashMap<&'static str, NumericResult> {
    HashMap::new()
}

// ---------------------------------------------------------------------------
// 1. All scalar args → 0-d output (shape empty, len 1)
// ---------------------------------------------------------------------------

#[test]
fn all_scalars_produce_zero_d_output() {
    let ast = ast_x_sq_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::Scalar(3.0));
    args.insert("y", EvalInput::Scalar(1.0));

    let handle = eval(&compiled, args).unwrap();
    assert!(
        handle.shape().is_empty(),
        "expected empty shape for all-scalar args, got {:?}",
        handle.shape()
    );
    assert_eq!(handle.len(), 1);
}

#[test]
fn all_scalars_scalar_value_correct() {
    // x^2 + y = 3^2 + 1 = 10
    let ast = ast_x_sq_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::Scalar(3.0));
    args.insert("y", EvalInput::Scalar(1.0));

    let result = eval(&compiled, args).unwrap().scalar().unwrap();
    assert_abs_diff_eq!(result.to_f64().unwrap(), 10.0, epsilon = 1e-10);
}

// ---------------------------------------------------------------------------
// 2. One array arg → 1-d output
// ---------------------------------------------------------------------------

#[test]
fn one_array_arg_produces_1d_output() {
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));

    let handle = eval(&compiled, args).unwrap();
    assert_eq!(handle.shape(), &[3]);
    assert_eq!(handle.len(), 3);
}

#[test]
fn one_array_arg_values_correct() {
    // x * 2 over [1, 2, 3] → [2, 4, 6]
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));

    let arr = eval(&compiled, args).unwrap().to_array().unwrap();
    assert_eq!(arr.shape(), &[3]);
    assert_eq!(*arr.get([0]).unwrap(), NumericResult::Real(2.0));
    assert_eq!(*arr.get([1]).unwrap(), NumericResult::Real(4.0));
    assert_eq!(*arr.get([2]).unwrap(), NumericResult::Real(6.0));
}

// ---------------------------------------------------------------------------
// 3. Two array args → 2-d Cartesian product (verify exact grid values)
// ---------------------------------------------------------------------------

#[test]
fn two_array_args_produce_2d_output() {
    // x + y, x in [1, 2], y in [10, 20, 30]
    let ast = ast_x_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0]));
    args.insert("y", EvalInput::from(vec![10.0, 20.0, 30.0]));

    let handle = eval(&compiled, args).unwrap();
    // axis 0 = x (2 elements), axis 1 = y (3 elements)
    assert_eq!(handle.shape(), &[2, 3]);
    assert_eq!(handle.len(), 6);
}

#[test]
fn two_array_args_grid_values_correct() {
    // x^2 + y, x in [1, 2, 3], y in [10, 20]
    // Expected grid (row = x-index, col = y-index):
    //   (1^2+10, 1^2+20) = (11, 21)
    //   (2^2+10, 2^2+20) = (14, 24)
    //   (3^2+10, 3^2+20) = (19, 29)
    let ast = ast_x_sq_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));
    args.insert("y", EvalInput::from(vec![10.0, 20.0]));

    let arr = eval(&compiled, args).unwrap().to_array().unwrap();
    assert_eq!(arr.shape(), &[3, 2]);

    assert_eq!(*arr.get([0, 0]).unwrap(), NumericResult::Real(11.0));
    assert_eq!(*arr.get([0, 1]).unwrap(), NumericResult::Real(21.0));
    assert_eq!(*arr.get([1, 0]).unwrap(), NumericResult::Real(14.0));
    assert_eq!(*arr.get([1, 1]).unwrap(), NumericResult::Real(24.0));
    assert_eq!(*arr.get([2, 0]).unwrap(), NumericResult::Real(19.0));
    assert_eq!(*arr.get([2, 1]).unwrap(), NumericResult::Real(29.0));
}

// ---------------------------------------------------------------------------
// 4. Mixed scalar + array → broadcast scalar over every array position
// ---------------------------------------------------------------------------

#[test]
fn scalar_broadcasts_over_array() {
    // x^2 + y, x = array [1, 2, 3], y = scalar 0
    // Expected: [1, 4, 9]
    let ast = ast_x_sq_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));
    args.insert("y", EvalInput::Scalar(0.0));

    let handle = eval(&compiled, args).unwrap();
    assert_eq!(handle.shape(), &[3]);

    let arr = handle.to_array().unwrap();
    assert_eq!(*arr.get([0]).unwrap(), NumericResult::Real(1.0));
    assert_eq!(*arr.get([1]).unwrap(), NumericResult::Real(4.0));
    assert_eq!(*arr.get([2]).unwrap(), NumericResult::Real(9.0));
}

#[test]
fn scalar_broadcasts_over_2d_result() {
    // x + y + c, with c as a compile-time constant; x and y both arrays
    // Use AST: x + y where c = 100 supplied as a constant
    let ast = Expression::Binary {
        op: BinaryOp::Add,
        left: Box::new(ast_x_plus_y()),
        right: Box::new(var("c")),
    };
    let mut constants = HashMap::new();
    constants.insert("c", NumericResult::Real(100.0));
    let compiled = compile(&ast, &constants).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0]));
    args.insert("y", EvalInput::from(vec![10.0, 20.0]));

    let arr = eval(&compiled, args).unwrap().to_array().unwrap();
    assert_eq!(arr.shape(), &[2, 2]);
    // (1+10+100, 1+20+100) = (111, 121)
    // (2+10+100, 2+20+100) = (112, 122)
    assert_eq!(*arr.get([0, 0]).unwrap(), NumericResult::Real(111.0));
    assert_eq!(*arr.get([0, 1]).unwrap(), NumericResult::Real(121.0));
    assert_eq!(*arr.get([1, 0]).unwrap(), NumericResult::Real(112.0));
    assert_eq!(*arr.get([1, 1]).unwrap(), NumericResult::Real(122.0));
}

// ---------------------------------------------------------------------------
// 5. Shape inspection before consumption
// ---------------------------------------------------------------------------

#[test]
fn shape_inspectable_before_consumption() {
    let ast = ast_x_sq_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));
    args.insert("y", EvalInput::from(vec![10.0, 20.0]));

    let handle = eval(&compiled, args).unwrap();
    // Shape and len are observable before consuming the handle.
    let shape = handle.shape().to_vec();
    let len = handle.len();
    let is_empty = handle.is_empty();

    assert_eq!(shape, vec![3, 2]);
    assert_eq!(len, 6);
    assert!(!is_empty);

    // Now consume — values must still be correct.
    let arr = handle.to_array().unwrap();
    assert_eq!(arr.shape(), &[3, 2]);
}

// ---------------------------------------------------------------------------
// 6. Eager (to_array) and lazy (iter) produce identical results
// ---------------------------------------------------------------------------

#[test]
fn eager_and_lazy_produce_identical_results_1d() {
    let ast = ast_x_sq_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let make_args = || {
        let mut args = HashMap::new();
        args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));
        args.insert("y", EvalInput::Scalar(10.0));
        args
    };

    let eager: Vec<NumericResult> = eval(&compiled, make_args())
        .unwrap()
        .to_array()
        .unwrap()
        .iter()
        .copied()
        .collect();

    let lazy: Vec<NumericResult> = eval(&compiled, make_args())
        .unwrap()
        .iter()
        .map(|r| r.unwrap())
        .collect();

    assert_eq!(eager, lazy);
}

#[test]
fn eager_and_lazy_produce_identical_results_2d() {
    let ast = ast_x_sq_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let make_args = || {
        let mut args = HashMap::new();
        args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));
        args.insert("y", EvalInput::from(vec![10.0, 20.0]));
        args
    };

    let eager: Vec<NumericResult> = eval(&compiled, make_args())
        .unwrap()
        .to_array()
        .unwrap()
        .iter()
        .copied()
        .collect();

    let lazy: Vec<NumericResult> = eval(&compiled, make_args())
        .unwrap()
        .iter()
        .map(|r| r.unwrap())
        .collect();

    assert_eq!(eager, lazy);
    assert_eq!(eager.len(), 6);
}

// ---------------------------------------------------------------------------
// 7. Empty array input → empty output (is_empty true, shape [0])
// ---------------------------------------------------------------------------

#[test]
fn empty_array_produces_empty_output() {
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![] as Vec<f64>));

    let handle = eval(&compiled, args).unwrap();
    assert!(handle.is_empty());
    assert_eq!(handle.len(), 0);
    assert_eq!(handle.shape(), &[0]);
}

#[test]
fn empty_array_to_array_returns_empty_array() {
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![] as Vec<f64>));

    let arr = eval(&compiled, args).unwrap().to_array().unwrap();
    assert_eq!(arr.len(), 0);
}

#[test]
fn empty_array_iter_yields_no_elements() {
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![] as Vec<f64>));

    let items: Vec<_> = eval(&compiled, args).unwrap().iter().collect();
    assert!(items.is_empty());
}

#[test]
fn one_empty_array_with_nonempty_array_produces_empty_output() {
    // Cartesian product with an empty factor is always empty.
    let ast = ast_x_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![] as Vec<f64>));
    args.insert("y", EvalInput::from(vec![1.0, 2.0, 3.0]));

    let handle = eval(&compiled, args).unwrap();
    assert!(handle.is_empty());
}

// ---------------------------------------------------------------------------
// 8. Iterator input: single iterator, verify materialized correctly
// ---------------------------------------------------------------------------

#[test]
fn iter_input_materialized_to_correct_values() {
    // x * 2 where x comes from an iterator over [5, 6, 7]
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert(
        "x",
        EvalInput::Iter(Box::new(vec![5.0, 6.0, 7.0].into_iter())),
    );

    let arr = eval(&compiled, args).unwrap().to_array().unwrap();
    assert_eq!(arr.shape(), &[3]);
    assert_eq!(*arr.get([0]).unwrap(), NumericResult::Real(10.0));
    assert_eq!(*arr.get([1]).unwrap(), NumericResult::Real(12.0));
    assert_eq!(*arr.get([2]).unwrap(), NumericResult::Real(14.0));
}

#[test]
fn iter_input_shape_matches_array_input() {
    // An iterator over N elements should produce the same shape as a Vec of N elements.
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let values = vec![1.0, 2.0, 3.0, 4.0];

    let mut args_iter = HashMap::new();
    args_iter.insert("x", EvalInput::Iter(Box::new(values.clone().into_iter())));

    let mut args_arr = HashMap::new();
    args_arr.insert("x", EvalInput::from(values));

    let shape_from_iter = eval(&compiled, args_iter).unwrap().shape().to_vec();
    let shape_from_arr = eval(&compiled, args_arr).unwrap().shape().to_vec();

    assert_eq!(shape_from_iter, shape_from_arr);
}

#[test]
fn iter_input_combined_with_array_produces_2d() {
    // x + y where x comes from iterator, y from array
    let ast = ast_x_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::Iter(Box::new(vec![1.0, 2.0].into_iter())));
    args.insert("y", EvalInput::from(vec![10.0, 20.0]));

    let handle = eval(&compiled, args).unwrap();
    assert_eq!(handle.shape(), &[2, 2]);
    assert_eq!(handle.len(), 4);
}

// ---------------------------------------------------------------------------
// 9. scalar() on non-scalar output → ShapeMismatch error
// ---------------------------------------------------------------------------

#[test]
fn scalar_on_1d_output_returns_shape_mismatch() {
    let ast = ast_x_times_two();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0, 3.0]));

    let handle = eval(&compiled, args).unwrap();
    let err = handle.scalar().unwrap_err();
    assert!(
        matches!(err, EvalError::ShapeMismatch { .. }),
        "expected ShapeMismatch, got {:?}",
        err
    );
}

#[test]
fn scalar_on_2d_output_returns_shape_mismatch() {
    let ast = ast_x_plus_y();
    let compiled = compile(&ast, &no_constants()).unwrap();

    let mut args = HashMap::new();
    args.insert("x", EvalInput::from(vec![1.0, 2.0]));
    args.insert("y", EvalInput::from(vec![10.0, 20.0]));

    let handle = eval(&compiled, args).unwrap();
    let err = handle.scalar().unwrap_err();
    assert!(matches!(err, EvalError::ShapeMismatch { .. }));
}