matten 0.22.3

A family-car multidimensional array (tensor) library for small numerical trials / PoCs.
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
//! Dynamic `impl Tensor` methods (M8 / RFC-011 + RFC-012).
//! This file is `#[cfg(feature = "dynamic")]` — compiled only under that feature.

use crate::{MattenError, Tensor};

// ---- Dynamic tensor (M8 / RFC-011 + RFC-012) ----------------------------
//
// Dynamic tensors store Element values in a DynamicTensor (Arc + view).
// The public Tensor struct is unchanged; dynamic storage lives alongside
// the Phase 1 Vec<f64> fields (which are empty for pure dynamic tensors).

#[cfg(feature = "dynamic")]
impl Tensor {
    /// Creates a dynamic tensor from a `Vec<Element>` and a shape.
    ///
    /// Panics on invalid shape. Use [`try_from_elements`](Tensor::try_from_elements)
    /// for recoverable construction.
    pub fn from_elements(data: Vec<crate::dynamic::Element>, shape: &[usize]) -> Tensor {
        Self::try_from_elements(data, shape).unwrap_or_else(|e| panic!("{e}"))
    }

    /// Creates a dynamic tensor from a `Vec<Element>` and a shape, returning
    /// an error instead of panicking.
    ///
    /// # Errors
    ///
    /// Returns [`MattenError::Shape`] or [`MattenError::Allocation`] for
    /// invalid shapes.
    pub fn try_from_elements(
        data: Vec<crate::dynamic::Element>,
        shape: &[usize],
    ) -> Result<Tensor, MattenError> {
        let expected = crate::shape::validate_shape(shape, "try_from_elements")?;
        if data.len() != expected {
            return Err(MattenError::Shape {
                operation: "try_from_elements",
                message: format!(
                    "data length {} does not match shape {shape:?}, which requires {expected} elements",
                    data.len()
                ),
            });
        }
        let dyn_tensor = crate::dynamic::storage::DynamicTensor::from_vec(data, shape.to_vec());
        Ok(Tensor {
            data: Vec::new(),
            shape: shape.to_vec(),
            dynamic: Some(Box::new(dyn_tensor)),
        })
    }

    /// Returns the element at the multidimensional coordinate, or `None`.
    ///
    /// Only meaningful on dynamic tensors.
    pub fn get_element(&self, coord: &[usize]) -> Option<crate::dynamic::Element> {
        let flat = crate::shape::coord_to_flat(coord, &self.shape)?;
        self.dynamic.as_ref()?.get_flat(flat).cloned()
    }

    /// Converts a dynamic tensor to a numeric Phase 1 tensor using an explicit
    /// [`NumericPolicy`](crate::NumericPolicy).
    ///
    /// This is the policy-aware version of [`try_numeric`](Tensor::try_numeric).
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "dynamic")] {
    /// use matten::{Element, NumericPolicy, Tensor};
    ///
    /// let t = Tensor::from_elements(
    ///     vec![Element::Float(1.0), Element::None, Element::Float(3.0)],
    ///     &[3],
    /// );
    ///
    /// let x = t
    ///     .try_numeric_with(NumericPolicy::default().none_as(0.0))
    ///     .unwrap();
    /// assert_eq!(x.as_slice(), &[1.0, 0.0, 3.0]);
    /// # }
    /// ```
    pub fn try_numeric_with(
        &self,
        policy: crate::dynamic::NumericPolicy,
    ) -> Result<Tensor, crate::MattenError> {
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("try_numeric_with called on a non-dynamic tensor");
        let mut floats = Vec::with_capacity(dyn_t.len);
        for i in 0..dyn_t.len {
            let elem = dyn_t.get_flat(i).unwrap_or(&crate::dynamic::Element::None);
            match policy.coerce(elem, i) {
                Ok(v) => floats.push(v),
                Err(msg) => {
                    return Err(crate::MattenError::Unsupported {
                        operation: "try_numeric_with",
                        message: msg,
                    });
                }
            }
        }
        Ok(Tensor {
            data: floats,
            shape: dyn_t.shape.clone(),
            dynamic: None,
        })
    }

    /// Parses a JSON string into a dynamic `Tensor`, mapping JSON values to
    /// `Element` variants.
    ///
    /// Accepts canonical `{"shape":[…],"data":[…]}` form and nested arrays.
    ///
    /// # Errors
    ///
    /// Returns [`MattenError::Parse`] for any parse or structure error.
    #[cfg(feature = "json")]
    pub fn from_json_dynamic(input: &str) -> Result<Tensor, MattenError> {
        crate::dynamic::parse::json::from_json_dynamic(input)
    }

    /// Parses a CSV string into a dynamic `Tensor`, inferring `Element` per
    /// field (int → `Int`, float → `Float`, empty → `None`, etc.).
    ///
    /// # Errors
    ///
    /// Returns [`MattenError::Parse`] for ragged rows or I/O errors.
    #[cfg(feature = "csv")]
    pub fn from_csv_dynamic(input: &str) -> Result<Tensor, MattenError> {
        crate::dynamic::parse::csv::from_csv_dynamic(input)
    }

    /// Returns all elements in logical row-major order.
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic (Phase 1 numeric) tensor.
    pub fn to_elements(&self) -> Vec<crate::dynamic::Element> {
        self.dynamic
            .as_ref()
            .expect("to_elements called on a non-dynamic tensor")
            .to_vec()
    }

    /// Returns a new tensor replacing all `Element::None` values with `value`.
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic tensor.
    pub fn fill_none(&self, value: impl Into<crate::dynamic::Element>) -> Tensor {
        let fill = value.into();
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("fill_none called on a non-dynamic tensor");
        let new_data: Vec<crate::dynamic::Element> = dyn_t
            .to_vec()
            .into_iter()
            .map(|e| {
                if e == crate::dynamic::Element::None {
                    fill.clone()
                } else {
                    e
                }
            })
            .collect();
        let new_dyn =
            crate::dynamic::storage::DynamicTensor::from_vec(new_data, dyn_t.shape.clone());
        Tensor {
            data: Vec::new(),
            shape: dyn_t.shape.clone(),
            #[cfg(feature = "dynamic")]
            dynamic: Some(Box::new(new_dyn)),
        }
    }

    /// Converts a dynamic tensor containing only numeric elements to a Phase 1
    /// `f64` tensor.
    ///
    /// # Errors
    ///
    /// Returns [`MattenError::Unsupported`] if any element is not numeric.
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic tensor.
    pub fn try_numeric(&self) -> Result<Tensor, MattenError> {
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("try_numeric called on a non-dynamic tensor");
        let mut floats = Vec::with_capacity(dyn_t.len);
        for i in 0..dyn_t.len {
            let elem = dyn_t.get_flat(i).unwrap_or(&crate::dynamic::Element::None);
            match elem.try_as_f64() {
                Some(v) => floats.push(v),
                None => {
                    return Err(MattenError::Unsupported {
                        operation: "try_numeric",
                        message: format!(
                            "element at position {i} is {elem:?} and cannot be coerced to f64; use fill_none or explicit conversion first"
                        ),
                    });
                }
            }
        }
        Tensor::try_new(floats, &dyn_t.shape)
    }
}

#[cfg(feature = "dynamic")]
impl Tensor {
    // ── Additional missing-value and numeric utilities (M9/M10) ──────────

    /// Returns a Phase 1 boolean-like tensor (`1.0` = None, `0.0` = not None)
    /// indicating which elements are `Element::None`.
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic tensor.
    #[cfg(feature = "dynamic")]
    pub fn none_mask(&self) -> Tensor {
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("none_mask called on a non-dynamic tensor");
        let data: Vec<f64> = dyn_t
            .to_vec()
            .iter()
            .map(|e| if e.is_none() { 1.0 } else { 0.0 })
            .collect();
        Tensor::new(data, &dyn_t.shape)
    }

    /// Alias for [`none_mask`](Tensor::none_mask) — returns a Phase 1 `f64` tensor
    /// where `1.0` marks `Element::None` positions and `0.0` marks all others.
    ///
    /// This name matches the RFC-011 §10 specified API `is_none(&self) -> Tensor`.
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic tensor.
    #[cfg(feature = "dynamic")]
    pub fn is_none_mask(&self) -> Tensor {
        self.none_mask()
    }

    /// Counts the number of `Element::None` values in the tensor.
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic tensor.
    #[cfg(feature = "dynamic")]
    pub fn count_none(&self) -> usize {
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("count_none called on a non-dynamic tensor");
        dyn_t.to_vec().iter().filter(|e| e.is_none()).count()
    }

    /// Replaces `Element::None` values by carrying the last non-None value
    /// forward along the flat (row-major) order. Leading None values that
    /// have no predecessor are replaced with `fallback`.
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic tensor.
    #[cfg(feature = "dynamic")]
    pub fn forward_fill_none(&self, fallback: impl Into<crate::dynamic::Element>) -> Tensor {
        use crate::dynamic::Element;
        let fallback = fallback.into();
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("forward_fill_none called on a non-dynamic tensor");
        let mut last: Element = fallback;
        let new_data: Vec<Element> = dyn_t
            .to_vec()
            .into_iter()
            .map(|e| {
                if e == Element::None {
                    last.clone()
                } else {
                    last = e.clone();
                    e
                }
            })
            .collect();
        let shape = dyn_t.shape.clone();
        let new_dyn = crate::dynamic::storage::DynamicTensor::from_vec(new_data, shape.clone());
        Tensor {
            data: Vec::new(),
            shape,
            dynamic: Some(Box::new(new_dyn)),
        }
    }

    /// Sums the numeric elements, skipping `None` values.
    ///
    /// Returns `0.0` if all elements are `None`. Panics on non-numeric,
    /// non-None elements (call `fill_none` or `try_numeric` first).
    ///
    /// # Panics
    ///
    /// Panics if called on a non-dynamic tensor or if any element is
    /// non-numeric and non-None.
    #[cfg(feature = "dynamic")]
    pub fn sum_skip_none(&self) -> f64 {
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("sum_skip_none called on a non-dynamic tensor");
        let mut acc = 0.0f64;
        for e in dyn_t.to_vec() {
            if e.is_none() {
                continue;
            }
            acc += e.try_as_f64().unwrap_or_else(|| {
                panic!("matten unsupported error in sum_skip_none: element {e:?} cannot be coerced to f64; use fill_none or filter non-numeric elements first")
            });
        }
        acc
    }
    // ---- RFC-016: dynamic inspection helpers --------------------------------

    /// Returns a numeric mask tensor where `1.0` indicates an element that
    /// can be coerced to `f64` by the default [`NumericPolicy`], and `0.0`
    /// indicates one that cannot.
    ///
    /// Mirrors [`none_mask`](Tensor::none_mask): the result is a Phase 1
    /// numeric tensor with the same shape.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "dynamic")] {
    /// use matten::{Element, Tensor};
    ///
    /// let t = Tensor::from_elements(
    ///     vec![Element::Float(1.0), Element::text("x"), Element::Int(2)],
    ///     &[3],
    /// );
    /// let mask = t.numeric_mask();
    /// assert_eq!(mask.as_slice(), &[1.0, 0.0, 1.0]);
    /// # }
    /// ```
    pub fn numeric_mask(&self) -> Tensor {
        use crate::dynamic::Element;
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("numeric_mask called on a non-dynamic tensor");
        let data: Vec<f64> = (0..dyn_t.len)
            .map(|i| {
                let elem = dyn_t.get_flat(i).unwrap_or(&Element::None);
                if elem.try_as_f64().is_some() {
                    1.0
                } else {
                    0.0
                }
            })
            .collect();
        Tensor {
            data,
            shape: dyn_t.shape.clone(),
            dynamic: None,
        }
    }

    /// Returns `true` if every element in this dynamic tensor can be coerced
    /// to `f64` by the default [`NumericPolicy`] (i.e., all `Float` or `Int`).
    ///
    /// Equivalent to `self.numeric_mask().min() == 1.0`.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "dynamic")] {
    /// use matten::{Element, Tensor};
    ///
    /// let ok = Tensor::from_elements(
    ///     vec![Element::Float(1.0), Element::Int(2)],
    ///     &[2],
    /// );
    /// assert!(ok.is_numeric_convertible());
    ///
    /// let not_ok = Tensor::from_elements(
    ///     vec![Element::Float(1.0), Element::None],
    ///     &[2],
    /// );
    /// assert!(!not_ok.is_numeric_convertible());
    /// # }
    /// ```
    pub fn is_numeric_convertible(&self) -> bool {
        use crate::dynamic::Element;
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("is_numeric_convertible called on a non-dynamic tensor");
        (0..dyn_t.len).all(|i| {
            dyn_t
                .get_flat(i)
                .unwrap_or(&Element::None)
                .try_as_f64()
                .is_some()
        })
    }

    /// Returns a compact human-readable summary of the element types present
    /// in this dynamic tensor.
    ///
    /// Useful for quickly understanding the content before cleanup and
    /// conversion. The summary shows counts of each [`Element`] variant.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "dynamic")] {
    /// use matten::{Element, Tensor};
    ///
    /// let t = Tensor::from_elements(
    ///     vec![
    ///         Element::Float(1.0), Element::None, Element::text("x"),
    ///         Element::Float(2.0), Element::Int(3),
    ///     ],
    ///     &[5],
    /// );
    /// let s = t.schema_summary();
    /// assert!(s.contains("Float: 2"));
    /// assert!(s.contains("None: 1"));
    /// # }
    /// ```
    pub fn schema_summary(&self) -> String {
        use crate::dynamic::Element;
        let dyn_t = self
            .dynamic
            .as_ref()
            .expect("schema_summary called on a non-dynamic tensor");
        let (mut n_float, mut n_int, mut n_text, mut n_bool, mut n_none) = (0usize, 0, 0, 0, 0);
        for i in 0..dyn_t.len {
            match dyn_t.get_flat(i).unwrap_or(&Element::None) {
                Element::Float(_) => n_float += 1,
                Element::Int(_) => n_int += 1,
                Element::Text(_) => n_text += 1,
                Element::Bool(_) => n_bool += 1,
                Element::None => n_none += 1,
            }
        }
        let total = dyn_t.len;
        let convertible = n_float + n_int;
        format!(
            "shape={:?} total={total} numeric={convertible} (Float: {n_float}, Int: {n_int}, Bool: {n_bool}, Text: {n_text}, None: {n_none})",
            dyn_t.shape
        )
    }
}