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
//! Stdlib numpy method converters
//!
//! DEPYLER-REFACTOR: Extracted from expr_gen/mod.rs
//!
//! Contains converters for numpy module calls:
//! - `try_convert_numpy_call` — Maps numpy API to trueno (SIMD-accelerated tensor library)
//! - `try_convert_numpy_call_nasa_mode` — Maps numpy API to std-only Vec<f64> operations
use super::ExpressionConverter;
use crate::hir::*;
use crate::rust_gen::context::ToRustExpr;
use crate::rust_gen::numpy_gen;
use anyhow::{bail, Result};
use quote;
use syn::parse_quote;
impl<'a, 'b> ExpressionConverter<'a, 'b> {
/// Try to convert numpy module calls to trueno equivalents.
///
/// Phase 3: NumPy→Trueno codegen
///
/// Maps numpy API calls to trueno (SIMD-accelerated tensor library):
/// - np.array([...]) → Vector::from_slice(&[...])
/// - np.dot(a, b) → a.dot(&b)?
/// - np.sum(a) → a.sum()?
/// - np.mean(a) → a.mean()?
/// - np.sqrt(a) → a.sqrt()?
///
/// Returns None if the method is not a recognized numpy function.
pub(crate) fn try_convert_numpy_call(
&mut self,
method: &str,
args: &[HirExpr],
) -> Result<Option<syn::Expr>> {
// Check if this is a recognized numpy function
if numpy_gen::parse_numpy_function(method).is_none() {
return Ok(None);
}
// DEPYLER-1121: In NASA mode, generate std-only numpy emulation
// NASA mode requires single-shot rustc compilation without external crates
if self.ctx.type_mapper.nasa_mode {
return self.try_convert_numpy_call_nasa_mode(method, args);
}
// Mark that we need trueno dependency
self.ctx.needs_trueno = true;
// Convert arguments to syn::Expr
let arg_exprs: Vec<syn::Expr> = args
.iter()
.map(|arg| arg.to_rust_expr(self.ctx))
.collect::<Result<Vec<_>>>()?;
// Generate trueno code based on the numpy function
let result = match method {
"array" => {
// np.array([1.0, 2.0, 3.0]) → Vector::from_slice(&[1.0f32, 2.0, 3.0])
// The argument should be a list literal
if let Some(HirExpr::List(elements)) = args.first() {
let element_exprs: Vec<proc_macro2::TokenStream> = elements
.iter()
.map(|e| {
let expr = e.to_rust_expr(self.ctx)?;
Ok(quote::quote! { #expr })
})
.collect::<Result<Vec<_>>>()?;
let call = numpy_gen::NumpyCall::Array {
elements: element_exprs,
};
let tokens = numpy_gen::generate_trueno_code(&call);
return Ok(Some(syn::parse2(tokens)?));
}
// Fallback: pass through as vec!
if let Some(arg) = arg_exprs.first() {
parse_quote! { Vector::from_vec(#arg) }
} else {
parse_quote! { Vector::new() }
}
}
"dot" => {
// np.dot(a, b) → a.dot(&b).unwrap()
if arg_exprs.len() >= 2 {
let a = &arg_exprs[0];
let b = &arg_exprs[1];
parse_quote! { #a.dot(&#b).expect("dot product failed") }
} else {
bail!("np.dot() requires 2 arguments");
}
}
"sum" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.sum().expect("operation failed") }
} else {
bail!("np.sum() requires 1 argument");
}
}
"mean" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.mean().expect("operation failed") }
} else {
bail!("np.mean() requires 1 argument");
}
}
// DEPYLER-0657: Scalar vs Vector numpy methods
// f64::sqrt()/abs() returns f64 directly (no Result)
// Vector::sqrt()/abs() returns Result (needs unwrap)
"sqrt" => {
if args.is_empty() {
bail!("np.sqrt() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.sqrt().expect("operation failed") }
} else {
parse_quote! { #arr.sqrt() }
}
}
"abs" => {
if args.is_empty() {
bail!("np.abs() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.abs().expect("operation failed") }
} else {
// f64 uses .abs() directly
parse_quote! { #arr.abs() }
}
}
"min" | "amin" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.min().expect("empty collection") }
} else {
bail!("np.min() requires 1 argument");
}
}
"max" | "amax" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.max().expect("empty collection") }
} else {
bail!("np.max() requires 1 argument");
}
}
// DEPYLER-0657: exp/log/sin/cos scalar vs vector
"exp" => {
if args.is_empty() {
bail!("np.exp() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.exp().expect("operation failed") }
} else {
parse_quote! { #arr.exp() }
}
}
"log" => {
if args.is_empty() {
bail!("np.log() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.ln().expect("operation failed") }
} else {
// f64 uses .ln() for natural log
parse_quote! { #arr.ln() }
}
}
"sin" => {
if args.is_empty() {
bail!("np.sin() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.sin().expect("operation failed") }
} else {
// f64::sin() returns f64 directly
parse_quote! { #arr.sin() }
}
}
"cos" => {
if args.is_empty() {
bail!("np.cos() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.cos().expect("operation failed") }
} else {
// f64::cos() returns f64 directly
parse_quote! { #arr.cos() }
}
}
"clip" => {
// DEPYLER-0920: Cast min/max to f32 for trueno::Vector::clamp compatibility
if arg_exprs.len() >= 3 {
let arr = &arg_exprs[0];
let min = &arg_exprs[1];
let max = &arg_exprs[2];
parse_quote! { #arr.clamp(#min as f32, #max as f32).expect("operation failed") }
} else {
bail!("np.clip() requires 3 arguments (array, min, max)");
}
}
"argmax" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.argmax().expect("empty collection") }
} else {
bail!("np.argmax() requires 1 argument");
}
}
"argmin" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.argmin().expect("empty collection") }
} else {
bail!("np.argmin() requires 1 argument");
}
}
"std" => {
// trueno uses stddev(), not std()
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.stddev().expect("operation failed") }
} else {
bail!("np.std() requires 1 argument");
}
}
"var" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.variance().expect("operation failed") }
} else {
bail!("np.var() requires 1 argument");
}
}
"zeros" => {
if let Some(size) = arg_exprs.first() {
parse_quote! { Vector::zeros(#size) }
} else {
bail!("np.zeros() requires 1 argument");
}
}
"ones" => {
if let Some(size) = arg_exprs.first() {
parse_quote! { Vector::ones(#size) }
} else {
bail!("np.ones() requires 1 argument");
}
}
"norm" => {
if let Some(arr) = arg_exprs.first() {
// DEPYLER-0583: trueno uses norm_l2() for L2 (Euclidean) norm
// DEPYLER-0667: Wrap arg in parens so `a - b` becomes `(a - b).norm_l2()`
// Without parens, `a - b.norm_l2()` parses as `a - (b.norm_l2())`
parse_quote! { (#arr).norm_l2().expect("operation failed") }
} else {
bail!("np.norm() requires 1 argument");
}
}
_ => return Ok(None),
};
Ok(Some(result))
}
/// DEPYLER-1121: NASA mode numpy conversion using std-only types
/// Maps numpy operations to Vec<f64> operations without external crates.
///
/// # Mappings (NASA mode):
/// | NumPy | Rust std-only |
/// |-------|---------------|
/// | `np.array([1.0, 2.0])` | `vec![1.0, 2.0]` |
/// | `np.exp(a)` | `a.iter().map(\|x\| x.exp()).collect()` |
/// | `np.sum(a)` | `a.iter().sum::<f64>()` |
/// | `np.dot(a, b)` | `a.iter().zip(b.iter()).map(\|(x, y)\| x * y).sum::<f64>()` |
fn try_convert_numpy_call_nasa_mode(
&mut self,
method: &str,
args: &[HirExpr],
) -> Result<Option<syn::Expr>> {
// Convert arguments to syn::Expr
let arg_exprs: Vec<syn::Expr> = args
.iter()
.map(|arg| arg.to_rust_expr(self.ctx))
.collect::<Result<Vec<_>>>()?;
let result: syn::Expr = match method {
"array" => {
// np.array([1.0, 2.0, 3.0]) → vec![1.0, 2.0, 3.0]
if let Some(HirExpr::List(elements)) = args.first() {
let element_exprs: Vec<syn::Expr> = elements
.iter()
.map(|e| e.to_rust_expr(self.ctx))
.collect::<Result<Vec<_>>>()?;
parse_quote! { vec![#(#element_exprs),*] }
} else if let Some(arg) = arg_exprs.first() {
// Fallback: pass through as vec!
parse_quote! { #arg.to_vec() }
} else {
parse_quote! { Vec::<f64>::new() }
}
}
"dot" => {
// DEPYLER-1135: np.dot(a, b) → numeric coercion for mixed int/float arrays
// Convert elements to f64 before multiplication to handle integer arrays
if arg_exprs.len() >= 2 {
let a = &arg_exprs[0];
let b = &arg_exprs[1];
parse_quote! {
#a.iter().zip(#b.iter()).map(|(x, y)| (*x as f64) * (*y as f64)).sum::<f64>()
}
} else {
bail!("np.dot() requires 2 arguments");
}
}
"sum" => {
// DEPYLER-1135: np.sum(a) → coerce elements to f64 for universal numeric promotion
// This handles both Vec<i32> and Vec<f64> by converting to f64
if let Some(arr) = arg_exprs.first() {
parse_quote! { #arr.iter().map(|&x| x as f64).sum::<f64>() }
} else {
bail!("np.sum() requires 1 argument");
}
}
"mean" => {
// DEPYLER-1135: np.mean(a) → coerce elements to f64 for proper averaging
if let Some(arr) = arg_exprs.first() {
parse_quote! {
(#arr.iter().map(|&x| x as f64).sum::<f64>() / #arr.len() as f64)
}
} else {
bail!("np.mean() requires 1 argument");
}
}
"sqrt" => {
if args.is_empty() {
bail!("np.sqrt() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
// Vector: map sqrt over elements
parse_quote! { #arr.iter().map(|x| x.sqrt()).collect::<Vec<_>>() }
} else {
// Scalar
parse_quote! { #arr.sqrt() }
}
}
"abs" => {
if args.is_empty() {
bail!("np.abs() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.iter().map(|x| x.abs()).collect::<Vec<_>>() }
} else {
parse_quote! { #arr.abs() }
}
}
"min" | "amin" => {
// DEPYLER-1135: Coerce to f64 for numeric promotion
if let Some(arr) = arg_exprs.first() {
parse_quote! {
#arr.iter().map(|&x| x as f64).fold(f64::INFINITY, f64::min)
}
} else {
bail!("np.min() requires 1 argument");
}
}
"max" | "amax" => {
// DEPYLER-1135: Coerce to f64 for numeric promotion
if let Some(arr) = arg_exprs.first() {
parse_quote! {
#arr.iter().map(|&x| x as f64).fold(f64::NEG_INFINITY, f64::max)
}
} else {
bail!("np.max() requires 1 argument");
}
}
"exp" => {
if args.is_empty() {
bail!("np.exp() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.iter().map(|x| x.exp()).collect::<Vec<_>>() }
} else {
parse_quote! { #arr.exp() }
}
}
"log" => {
if args.is_empty() {
bail!("np.log() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.iter().map(|x| x.ln()).collect::<Vec<_>>() }
} else {
parse_quote! { #arr.ln() }
}
}
"sin" => {
if args.is_empty() {
bail!("np.sin() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.iter().map(|x| x.sin()).collect::<Vec<_>>() }
} else {
parse_quote! { #arr.sin() }
}
}
"cos" => {
if args.is_empty() {
bail!("np.cos() requires 1 argument");
}
let arr = &arg_exprs[0];
if self.is_numpy_array_expr(&args[0]) {
parse_quote! { #arr.iter().map(|x| x.cos()).collect::<Vec<_>>() }
} else {
parse_quote! { #arr.cos() }
}
}
"argmax" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! {
#arr.iter().enumerate()
.max_by(|(_, a), (_, b)| a.partial_cmp(b).expect("operation failed"))
.map(|(i, _)| i as i64)
.unwrap_or(0)
}
} else {
bail!("np.argmax() requires 1 argument");
}
}
"argmin" => {
if let Some(arr) = arg_exprs.first() {
parse_quote! {
#arr.iter().enumerate()
.min_by(|(_, a), (_, b)| a.partial_cmp(b).expect("operation failed"))
.map(|(i, _)| i as i64)
.unwrap_or(0)
}
} else {
bail!("np.argmin() requires 1 argument");
}
}
"std" => {
// DEPYLER-1135: std = sqrt(variance) with numeric coercion
if let Some(arr) = arg_exprs.first() {
parse_quote! {{
let data: Vec<f64> = #arr.iter().map(|&x| x as f64).collect();
let mean = data.iter().sum::<f64>() / data.len() as f64;
let variance = data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / data.len() as f64;
variance.sqrt()
}}
} else {
bail!("np.std() requires 1 argument");
}
}
"var" => {
// DEPYLER-1135: variance with numeric coercion
if let Some(arr) = arg_exprs.first() {
parse_quote! {{
let data: Vec<f64> = #arr.iter().map(|&x| x as f64).collect();
let mean = data.iter().sum::<f64>() / data.len() as f64;
data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / data.len() as f64
}}
} else {
bail!("np.var() requires 1 argument");
}
}
"zeros" => {
if let Some(size) = arg_exprs.first() {
parse_quote! { vec![0.0f64; #size as usize] }
} else {
bail!("np.zeros() requires 1 argument");
}
}
"ones" => {
if let Some(size) = arg_exprs.first() {
parse_quote! { vec![1.0f64; #size as usize] }
} else {
bail!("np.ones() requires 1 argument");
}
}
"norm" => {
// DEPYLER-1135: L2 norm with numeric coercion
if let Some(arr) = arg_exprs.first() {
parse_quote! {
(#arr.iter().map(|&x| { let v = x as f64; v * v }).sum::<f64>()).sqrt()
}
} else {
bail!("np.norm() requires 1 argument");
}
}
_ => return Ok(None),
};
Ok(Some(result))
}
}