xdl_matlab/
function_map.rs

1//! MATLAB to XDL function name mapping
2
3use once_cell::sync::Lazy;
4use std::collections::HashMap;
5
6/// Mapping of MATLAB function names to XDL equivalents
7pub static MATLAB_FUNCTION_MAP: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
8    let mut map = HashMap::new();
9
10    // Array creation
11    map.insert("zeros", "FLTARR"); // zeros(n) -> FLTARR(n)
12    map.insert("ones", "FLTARR"); // ones(n) -> FLTARR(n) + 1 (handled specially)
13    map.insert("eye", "IDENTITY"); // eye(n) -> IDENTITY(n)
14    map.insert("rand", "RANDOMU"); // rand(n) -> RANDOMU(seed, n)
15    map.insert("randn", "RANDOMN"); // randn(n) -> RANDOMN(seed, n)
16
17    // Array info
18    map.insert("size", "SIZE");
19    map.insert("length", "N_ELEMENTS");
20    map.insert("numel", "N_ELEMENTS");
21
22    // Math functions
23    map.insert("sin", "SIN");
24    map.insert("cos", "COS");
25    map.insert("tan", "TAN");
26    map.insert("asin", "ASIN");
27    map.insert("acos", "ACOS");
28    map.insert("atan", "ATAN");
29    map.insert("atan2", "ATAN");
30    map.insert("exp", "EXP");
31    map.insert("log", "ALOG");
32    map.insert("log10", "ALOG10");
33    map.insert("sqrt", "SQRT");
34    map.insert("abs", "ABS");
35    map.insert("floor", "FLOOR");
36    map.insert("ceil", "CEIL");
37    map.insert("round", "ROUND");
38    map.insert("mod", "MOD");
39
40    // Statistics
41    map.insert("mean", "MEAN");
42    map.insert("median", "MEDIAN");
43    map.insert("std", "STDDEV");
44    map.insert("var", "VARIANCE");
45    map.insert("min", "MIN");
46    map.insert("max", "MAX");
47    map.insert("sum", "TOTAL");
48
49    // Linear algebra
50    map.insert("transpose", "TRANSPOSE");
51    map.insert("inv", "INVERT");
52    map.insert("det", "DETERM");
53
54    // Plotting
55    map.insert("plot", "PLOT");
56    map.insert("xlabel", "XTITLE");
57    map.insert("ylabel", "YTITLE");
58    map.insert("zlabel", "ZTITLE"); // Z-axis label (may not be supported in 2D plots)
59    map.insert("title", "TITLE");
60    map.insert("legend", "LEGEND"); // legend -> LEGEND (XDL may not support, will be ignored)
61    map.insert("figure", "WINDOW");
62    map.insert("hold", "OPLOT"); // hold on -> use OPLOT
63    map.insert("clf", "ERASE");
64    map.insert("close", "WDELETE");
65
66    // MATLAB compatibility functions
67    map.insert("linspace", "LINSPACE");
68    map.insert("logspace", "LOGSPACE");
69    map.insert("meshgrid", "MESHGRID");
70    map.insert("ndgrid", "NDGRID");
71    map.insert("repmat", "REPMAT");
72    map.insert("squeeze", "SQUEEZE");
73    map.insert("interp1", "INTERP1");
74
75    // Array manipulation
76    map.insert("reshape", "REFORM");
77    map.insert("sort", "SORT");
78    map.insert("find", "WHERE");
79
80    // I/O
81    map.insert("disp", "PRINT");
82    map.insert("fprintf", "PRINTF");
83    map.insert("sprintf", "STRING");
84
85    // Logical
86    map.insert("all", "MIN"); // all -> MIN (non-zero check)
87    map.insert("any", "MAX"); // any -> MAX (non-zero check)
88
89    // Type conversion
90    map.insert("double", "DOUBLE");
91    map.insert("single", "FLOAT");
92    map.insert("int32", "LONG");
93    map.insert("uint32", "ULONG");
94
95    map
96});
97
98/// Get XDL equivalent for a MATLAB function name
99pub fn get_xdl_function(matlab_func: &str) -> Option<&'static str> {
100    MATLAB_FUNCTION_MAP.get(matlab_func).copied()
101}
102
103/// Check if a MATLAB function needs special handling during transpilation
104pub fn needs_special_handling(matlab_func: &str) -> bool {
105    matches!(matlab_func, "ones" | "rand" | "randn" | "eye")
106}
107
108#[cfg(test)]
109mod tests {
110    use super::*;
111
112    #[test]
113    fn test_function_mapping() {
114        assert_eq!(get_xdl_function("zeros"), Some("FLTARR"));
115        assert_eq!(get_xdl_function("sin"), Some("SIN"));
116        assert_eq!(get_xdl_function("plot"), Some("PLOT"));
117        assert_eq!(get_xdl_function("nonexistent"), None);
118    }
119}