runmat-runtime 0.6.0

Core runtime for RunMat with builtins, BLAS/LAPACK integration, and execution APIs
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
//! MATLAB-compatible `num2cell` builtin.

use std::collections::HashSet;

use runmat_builtins::{
    BuiltinCompletionPolicy, BuiltinDescriptor, BuiltinErrorDescriptor, BuiltinOutputMode,
    BuiltinParamArity, BuiltinParamDescriptor, BuiltinParamType, BuiltinSignatureDescriptor,
    CellArray, CharArray, ComplexTensor, LogicalArray, StringArray, Tensor, Value,
};
use runmat_macros::runtime_builtin;

use crate::{build_runtime_error, gather_if_needed_async, BuiltinResult, RuntimeError};

const BUILTIN_NAME: &str = "num2cell";

const OUTPUT: [BuiltinParamDescriptor; 1] = [BuiltinParamDescriptor {
    name: "C",
    ty: BuiltinParamType::Any,
    arity: BuiltinParamArity::Required,
    default: None,
    description: "Cell array result.",
}];

const INPUTS_ONE: [BuiltinParamDescriptor; 1] = [BuiltinParamDescriptor {
    name: "A",
    ty: BuiltinParamType::Any,
    arity: BuiltinParamArity::Required,
    default: None,
    description: "Input array.",
}];

const INPUTS_DIMS: [BuiltinParamDescriptor; 2] = [
    BuiltinParamDescriptor {
        name: "A",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "Input array.",
    },
    BuiltinParamDescriptor {
        name: "dims",
        ty: BuiltinParamType::Any,
        arity: BuiltinParamArity::Required,
        default: None,
        description: "Dimensions to preserve inside each cell.",
    },
];

const SIGNATURES: [BuiltinSignatureDescriptor; 2] = [
    BuiltinSignatureDescriptor {
        label: "C = num2cell(A)",
        inputs: &INPUTS_ONE,
        outputs: &OUTPUT,
    },
    BuiltinSignatureDescriptor {
        label: "C = num2cell(A, dims)",
        inputs: &INPUTS_DIMS,
        outputs: &OUTPUT,
    },
];

const ERROR_INVALID_INPUT: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
    code: "RM.NUM2CELL.INVALID_INPUT",
    identifier: Some("RunMat:num2cell:InvalidInput"),
    when: "Input value or dimension selector is unsupported.",
    message: "num2cell: invalid input",
};

const ERROR_INTERNAL: BuiltinErrorDescriptor = BuiltinErrorDescriptor {
    code: "RM.NUM2CELL.INTERNAL",
    identifier: Some("RunMat:num2cell:Internal"),
    when: "Internal cell or slice allocation failed.",
    message: "num2cell: internal error",
};

const ERRORS: [BuiltinErrorDescriptor; 2] = [ERROR_INVALID_INPUT, ERROR_INTERNAL];

pub const NUM2CELL_DESCRIPTOR: BuiltinDescriptor = BuiltinDescriptor {
    signatures: &SIGNATURES,
    output_mode: BuiltinOutputMode::Fixed,
    completion_policy: BuiltinCompletionPolicy::Public,
    errors: &ERRORS,
};

#[runtime_builtin(
    name = "num2cell",
    category = "cells/core",
    summary = "Convert an array into a cell array, optionally grouping dimensions.",
    keywords = "num2cell,cell,conversion,array,dimensions",
    accel = "gather",
    descriptor(crate::builtins::cells::core::num2cell::NUM2CELL_DESCRIPTOR),
    builtin_path = "crate::builtins::cells::core::num2cell"
)]
async fn num2cell_builtin(value: Value, rest: Vec<Value>) -> BuiltinResult<Value> {
    if rest.len() > 1 {
        return Err(error(
            &ERROR_INVALID_INPUT,
            "num2cell: expected A or A,dims",
        ));
    }
    let value = gather_if_needed_async(&value).await?;
    let dims = rest
        .first()
        .map(parse_dims)
        .transpose()?
        .unwrap_or_default();
    num2cell_value(value, &dims)
}

fn num2cell_value(value: Value, grouped_dims: &[usize]) -> BuiltinResult<Value> {
    match value {
        Value::Tensor(tensor) => num2cell_array(
            tensor.shape.clone(),
            grouped_dims,
            |coords| Value::Num(tensor.data[linear_col_major(coords, &tensor.shape)]),
            |shape, coords| {
                let data = coords
                    .iter()
                    .map(|coord| tensor.data[linear_col_major(coord, &tensor.shape)])
                    .collect();
                Tensor::new(data, shape.to_vec())
                    .map(Value::Tensor)
                    .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}")))
            },
        ),
        Value::ComplexTensor(tensor) => num2cell_array(
            tensor.shape.clone(),
            grouped_dims,
            |coords| {
                let (re, im) = tensor.data[linear_col_major(coords, &tensor.shape)];
                Value::Complex(re, im)
            },
            |shape, coords| {
                let data = coords
                    .iter()
                    .map(|coord| tensor.data[linear_col_major(coord, &tensor.shape)])
                    .collect();
                ComplexTensor::new(data, shape.to_vec())
                    .map(Value::ComplexTensor)
                    .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}")))
            },
        ),
        Value::LogicalArray(array) => num2cell_array(
            array.shape.clone(),
            grouped_dims,
            |coords| Value::Bool(array.data[linear_col_major(coords, &array.shape)] != 0),
            |shape, coords| {
                let data = coords
                    .iter()
                    .map(|coord| array.data[linear_col_major(coord, &array.shape)])
                    .collect();
                LogicalArray::new(data, shape.to_vec())
                    .map(Value::LogicalArray)
                    .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}")))
            },
        ),
        Value::StringArray(array) => num2cell_array(
            array.shape.clone(),
            grouped_dims,
            |coords| Value::String(array.data[linear_col_major(coords, &array.shape)].clone()),
            |shape, coords| {
                let data = coords
                    .iter()
                    .map(|coord| array.data[linear_col_major(coord, &array.shape)].clone())
                    .collect();
                StringArray::new(data, shape.to_vec())
                    .map(Value::StringArray)
                    .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}")))
            },
        ),
        Value::CharArray(array) => {
            let shape = vec![array.rows, array.cols];
            num2cell_array(
                shape.clone(),
                grouped_dims,
                |coords| {
                    Value::CharArray(
                        CharArray::new(vec![array.data[coords[0] * array.cols + coords[1]]], 1, 1)
                            .unwrap(),
                    )
                },
                |slice_shape, coords| {
                    let rows = *slice_shape.first().unwrap_or(&1);
                    let cols = *slice_shape.get(1).unwrap_or(&1);
                    let data = coords
                        .iter()
                        .map(|coord| array.data[coord[0] * array.cols + coord[1]])
                        .collect();
                    CharArray::new(data, rows, cols)
                        .map(Value::CharArray)
                        .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}")))
                },
            )
        }
        scalar if grouped_dims.is_empty() => CellArray::new(vec![scalar], 1, 1)
            .map(Value::Cell)
            .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}"))),
        _ => Err(error(
            &ERROR_INVALID_INPUT,
            "num2cell: grouped dimensions require an array input",
        )),
    }
}

fn num2cell_array<F, G>(
    shape: Vec<usize>,
    grouped_dims: &[usize],
    scalar_at: F,
    slice_at: G,
) -> BuiltinResult<Value>
where
    F: Fn(&[usize]) -> Value,
    G: Fn(&[usize], &[Vec<usize>]) -> BuiltinResult<Value>,
{
    if grouped_dims.is_empty() {
        return scalar_cell_array(shape, scalar_at);
    }

    let grouped = grouped_dim_set(grouped_dims, shape.len());
    if grouped.is_empty() {
        return scalar_cell_array(shape, scalar_at);
    }

    let mut output_shape = Vec::with_capacity(shape.len());
    let mut slice_shape = Vec::with_capacity(shape.len());
    for (axis, &extent) in shape.iter().enumerate() {
        if grouped.contains(&axis) {
            output_shape.push(1);
            slice_shape.push(extent);
        } else {
            output_shape.push(extent);
            slice_shape.push(1);
        }
    }

    let total = output_shape.iter().product::<usize>();
    let mut cells = Vec::with_capacity(total);
    for row_major in 0..total {
        let output_coords = coords_row_major(row_major, &output_shape);
        let slice_coords = slice_coords_for_output(&shape, &grouped, &output_coords)?;
        cells.push(slice_at(&slice_shape, &slice_coords)?);
    }
    CellArray::new_with_shape(cells, output_shape)
        .map(Value::Cell)
        .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}")))
}

fn scalar_cell_array<F>(shape: Vec<usize>, scalar_at: F) -> BuiltinResult<Value>
where
    F: Fn(&[usize]) -> Value,
{
    let total = shape.iter().product::<usize>();
    let mut cells = Vec::with_capacity(total);
    for row_major in 0..total {
        let coords = coords_row_major(row_major, &shape);
        cells.push(scalar_at(&coords));
    }
    CellArray::new_with_shape(cells, shape)
        .map(Value::Cell)
        .map_err(|err| error(&ERROR_INTERNAL, format!("num2cell: {err}")))
}

fn grouped_dim_set(grouped_dims: &[usize], rank: usize) -> HashSet<usize> {
    grouped_dims
        .iter()
        .filter_map(|dim| dim.checked_sub(1))
        .filter(|&dim| dim < rank)
        .collect()
}

fn slice_coords_for_output(
    shape: &[usize],
    grouped: &HashSet<usize>,
    output_coords: &[usize],
) -> BuiltinResult<Vec<Vec<usize>>> {
    let slice_shape: Vec<usize> = shape
        .iter()
        .enumerate()
        .map(|(axis, &extent)| if grouped.contains(&axis) { extent } else { 1 })
        .collect();
    let total = slice_shape.iter().product::<usize>();
    let mut coords = Vec::with_capacity(total);
    for slice_linear in 0..total {
        let slice_coords = coords_col_major(slice_linear, &slice_shape);
        let mut full = Vec::with_capacity(shape.len());
        for axis in 0..shape.len() {
            if grouped.contains(&axis) {
                full.push(slice_coords[axis]);
            } else {
                full.push(output_coords[axis]);
            }
        }
        coords.push(full);
    }
    Ok(coords)
}

fn parse_dims(value: &Value) -> BuiltinResult<Vec<usize>> {
    let mut dims = Vec::new();
    match value {
        Value::Num(value) => dims.push(parse_dim(*value)?),
        Value::Int(value) => dims.push(parse_dim(value.to_f64())?),
        Value::Tensor(tensor) => {
            for &value in &tensor.data {
                dims.push(parse_dim(value)?);
            }
        }
        _ => {
            return Err(error(
                &ERROR_INVALID_INPUT,
                "num2cell: dims must be a positive integer scalar or vector",
            ))
        }
    }
    dims.sort_unstable();
    dims.dedup();
    Ok(dims)
}

fn parse_dim(value: f64) -> BuiltinResult<usize> {
    if !value.is_finite() || value < 1.0 || value.fract() != 0.0 {
        return Err(error(
            &ERROR_INVALID_INPUT,
            "num2cell: dims must contain positive integers",
        ));
    }
    Ok(value as usize)
}

fn coords_row_major(mut linear: usize, shape: &[usize]) -> Vec<usize> {
    let mut coords = vec![0usize; shape.len()];
    for axis in (0..shape.len()).rev() {
        let extent = shape[axis];
        coords[axis] = if extent == 0 { 0 } else { linear % extent };
        if extent != 0 {
            linear /= extent;
        }
    }
    coords
}

fn coords_col_major(mut linear: usize, shape: &[usize]) -> Vec<usize> {
    let mut coords = Vec::with_capacity(shape.len());
    for &extent in shape {
        coords.push(if extent == 0 { 0 } else { linear % extent });
        if extent != 0 {
            linear /= extent;
        }
    }
    coords
}

fn linear_col_major(coords: &[usize], shape: &[usize]) -> usize {
    let mut linear = 0usize;
    let mut stride = 1usize;
    for (&coord, &extent) in coords.iter().zip(shape.iter()) {
        linear += coord * stride;
        stride *= extent;
    }
    linear
}

fn error(desc: &'static BuiltinErrorDescriptor, message: impl Into<String>) -> RuntimeError {
    let mut builder = build_runtime_error(message).with_builtin(BUILTIN_NAME);
    if let Some(identifier) = desc.identifier {
        builder = builder.with_identifier(identifier);
    }
    builder.build()
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::executor::block_on;

    #[test]
    fn converts_numeric_matrix_to_scalar_cells() {
        let matrix = Tensor::new(vec![1.0, 2.0, 3.0, 4.0], vec![2, 2]).unwrap();
        let out = block_on(num2cell_builtin(Value::Tensor(matrix), Vec::new())).unwrap();
        let Value::Cell(cell) = out else {
            panic!("expected cell");
        };
        assert_eq!(cell.shape, vec![2, 2]);
        assert_eq!(cell.get(0, 0).unwrap(), Value::Num(1.0));
        assert_eq!(cell.get(0, 1).unwrap(), Value::Num(3.0));
        assert_eq!(cell.get(1, 0).unwrap(), Value::Num(2.0));
        assert_eq!(cell.get(1, 1).unwrap(), Value::Num(4.0));
    }

    #[test]
    fn groups_columns_or_rows_by_dimension() {
        let matrix = Tensor::new(vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0], vec![2, 3]).unwrap();
        let by_cols = block_on(num2cell_builtin(
            Value::Tensor(matrix.clone()),
            vec![Value::Num(2.0)],
        ))
        .unwrap();
        let Value::Cell(by_cols) = by_cols else {
            panic!("expected grouped cells");
        };
        assert_eq!(by_cols.shape, vec![2, 1]);
        match by_cols.get(0, 0).unwrap() {
            Value::Tensor(t) => {
                assert_eq!(t.shape, vec![1, 3]);
                assert_eq!(t.data, vec![1.0, 3.0, 5.0]);
            }
            other => panic!("expected row slice tensor, got {other:?}"),
        }

        let by_rows = block_on(num2cell_builtin(
            Value::Tensor(matrix),
            vec![Value::Num(1.0)],
        ))
        .unwrap();
        let Value::Cell(by_rows) = by_rows else {
            panic!("expected grouped cells");
        };
        assert_eq!(by_rows.shape, vec![1, 3]);
        match by_rows.get(0, 1).unwrap() {
            Value::Tensor(t) => {
                assert_eq!(t.shape, vec![2, 1]);
                assert_eq!(t.data, vec![3.0, 4.0]);
            }
            other => panic!("expected column slice tensor, got {other:?}"),
        }

        let ignored_dim = block_on(num2cell_builtin(
            Value::Tensor(Tensor::new(vec![1.0, 2.0], vec![1, 2]).unwrap()),
            vec![Value::Num(3.0)],
        ))
        .unwrap();
        let Value::Cell(ignored_dim) = ignored_dim else {
            panic!("expected scalar cells");
        };
        assert_eq!(ignored_dim.shape, vec![1, 2]);
        assert_eq!(ignored_dim.get(0, 1).unwrap(), Value::Num(2.0));
    }
}