rhai_sci/validate.rs
1use rhai::plugin::*;
2
3#[export_module]
4pub mod validation_functions {
5 use crate::matrix::{matrix_dimensions, numeric_vector_data};
6 use rhai::Array;
7
8 /// Tests whether the input is a simple list or a numeric vector.
9 /// ```typescript
10 /// let x = [1, 2, 3, 4];
11 /// assert_eq(is_list(x), true);
12 /// ```
13 /// ```typescript
14 /// let x = [[[1, 2], [3, 4]]];
15 /// assert_eq(is_list(x), false);
16 /// ```
17 #[rhai_fn(name = "is_list", pure)]
18 pub fn is_list(arr: &mut Array) -> bool {
19 arr.iter().all(|value| !value.is_array()) || numeric_vector_data(arr).is_ok()
20 }
21
22 /// Determines if the entire array is numeric (ints or floats).
23 /// ```typescript
24 /// let x = [1, 2, 3.0, 5.0];
25 /// assert_eq(is_numeric_array(x), true);
26 /// ```
27 /// ```typescript
28 /// let x = [1, 2, 3.0, 5.0, "a"];
29 /// assert_eq(is_numeric_array(x), false);
30 /// ```
31 #[rhai_fn(name = "is_numeric_array", pure)]
32 pub fn is_numeric_array(arr: &mut Array) -> bool {
33 let (ints, floats, total) = crate::int_and_float_totals(arr);
34 return if ints + floats - total == 0 {
35 true
36 } else {
37 false
38 };
39 }
40
41 /// Tests whether the input in a simple list array composed of floating point values.
42 /// ```typescript
43 /// let x = [1.0, 2.0, 3.0, 4.0];
44 /// assert_eq(is_float_list(x), true)
45 /// ```
46 /// ```typescript
47 /// let x = [1, 2, 3, 4];
48 /// assert_eq(is_float_list(x), false)
49 /// ```
50 #[rhai_fn(name = "is_float_list", pure)]
51 pub fn is_float_list(arr: &mut Array) -> bool {
52 let (_, floats, total) = crate::int_and_float_totals(arr);
53 return if (floats == total) && is_list(arr) {
54 true
55 } else {
56 false
57 };
58 }
59
60 /// Tests whether the input in a simple list array composed of integer values.
61 /// ```typescript
62 /// let x = [1.0, 2.0, 3.0, 4.0];
63 /// assert_eq(is_int_list(x), false)
64 /// ```
65 /// ```typescript
66 /// let x = [1, 2, 3, 4];
67 /// assert_eq(is_int_list(x), true)
68 /// ```
69 #[rhai_fn(name = "is_int_list", pure)]
70 pub fn is_int_list(arr: &mut Array) -> bool {
71 let (ints, _, total) = crate::int_and_float_totals(arr);
72 return if (ints == total) && is_list(arr) {
73 true
74 } else {
75 false
76 };
77 }
78
79 /// Tests whether the input in a simple list array composed of either floating point or integer values
80 /// or a numeric row/column vector.
81 /// ```typescript
82 /// let x = [1.0, 2.0, 3.0, 4.0];
83 /// assert_eq(is_numeric_list(x), true)
84 /// ```
85 /// ```typescript
86 /// let x = [1, 2, 3, 4];
87 /// assert_eq(is_numeric_list(x), true)
88 /// ```
89 /// ```typescript
90 /// let x = ["a", "b", "c", "d"];
91 /// assert_eq(is_numeric_list(x), false)
92 /// ```
93 #[rhai_fn(name = "is_numeric_list", pure)]
94 pub fn is_numeric_list(arr: &mut Array) -> bool {
95 numeric_vector_data(arr).is_ok()
96 }
97
98 /// Tests whether the input is a row vector.
99 /// ```typescript
100 /// let row = [[1, 2, 3]];
101 /// assert_eq(is_row_vector(row), true);
102 /// ```
103 /// ```typescript
104 /// let column = [[1], [2], [3]];
105 /// assert_eq(is_row_vector(column), false);
106 /// ```
107 #[rhai_fn(name = "is_row_vector", pure)]
108 pub fn is_row_vector(arr: &mut Array) -> bool {
109 matches!(matrix_dimensions(arr), Some((1, _)))
110 }
111
112 /// Tests whether the input is a column vector.
113 /// ```typescript
114 /// let column = [[1], [2], [3]];
115 /// assert_eq(is_column_vector(column), true);
116 /// ```
117 /// ```typescript
118 /// let row = [[1, 2, 3]];
119 /// assert_eq(is_column_vector(row), false);
120 /// ```
121 #[rhai_fn(name = "is_column_vector", pure)]
122 pub fn is_column_vector(arr: &mut Array) -> bool {
123 matches!(matrix_dimensions(arr), Some((_, 1)))
124 }
125
126 /// Tests whether the input is a matrix
127 /// ```typescript
128 /// let x = ones([3, 5]);
129 /// assert_eq(is_matrix(x), true)
130 /// ```
131 /// ```typescript
132 /// let x = ones([5, 5, 5]);
133 /// assert_eq(is_matrix(x), false)
134 /// ```
135 #[rhai_fn(name = "is_matrix", pure)]
136 pub fn is_matrix(arr: &mut Array) -> bool {
137 matrix_dimensions(arr).is_some()
138 }
139}