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