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
use rhai::plugin::*;
#[export_module]
pub mod validation_functions {
use crate::matrix::{matrix_dimensions, numeric_vector_data};
use rhai::Array;
/// Tests whether the input is a simple list or a numeric vector.
/// ```typescript
/// let x = [1, 2, 3, 4];
/// assert_eq(is_list(x), true);
/// ```
/// ```typescript
/// let x = [[[1, 2], [3, 4]]];
/// assert_eq(is_list(x), false);
/// ```
#[rhai_fn(name = "is_list", pure)]
pub fn is_list(arr: &mut Array) -> bool {
arr.iter().all(|value| !value.is_array()) || numeric_vector_data(arr).is_ok()
}
/// Determines if the entire array is numeric (ints or floats).
/// ```typescript
/// let x = [1, 2, 3.0, 5.0];
/// assert_eq(is_numeric_array(x), true);
/// ```
/// ```typescript
/// let x = [1, 2, 3.0, 5.0, "a"];
/// assert_eq(is_numeric_array(x), false);
/// ```
#[rhai_fn(name = "is_numeric_array", pure)]
pub fn is_numeric_array(arr: &mut Array) -> bool {
let (ints, floats, total) = crate::int_and_float_totals(arr);
return if ints + floats - total == 0 {
true
} else {
false
};
}
/// Tests whether the input in a simple list array composed of floating point values.
/// ```typescript
/// let x = [1.0, 2.0, 3.0, 4.0];
/// assert_eq(is_float_list(x), true)
/// ```
/// ```typescript
/// let x = [1, 2, 3, 4];
/// assert_eq(is_float_list(x), false)
/// ```
#[rhai_fn(name = "is_float_list", pure)]
pub fn is_float_list(arr: &mut Array) -> bool {
let (_, floats, total) = crate::int_and_float_totals(arr);
return if (floats == total) && is_list(arr) {
true
} else {
false
};
}
/// Tests whether the input in a simple list array composed of integer values.
/// ```typescript
/// let x = [1.0, 2.0, 3.0, 4.0];
/// assert_eq(is_int_list(x), false)
/// ```
/// ```typescript
/// let x = [1, 2, 3, 4];
/// assert_eq(is_int_list(x), true)
/// ```
#[rhai_fn(name = "is_int_list", pure)]
pub fn is_int_list(arr: &mut Array) -> bool {
let (ints, _, total) = crate::int_and_float_totals(arr);
return if (ints == total) && is_list(arr) {
true
} else {
false
};
}
/// Tests whether the input in a simple list array composed of either floating point or integer values
/// or a numeric row/column vector.
/// ```typescript
/// let x = [1.0, 2.0, 3.0, 4.0];
/// assert_eq(is_numeric_list(x), true)
/// ```
/// ```typescript
/// let x = [1, 2, 3, 4];
/// assert_eq(is_numeric_list(x), true)
/// ```
/// ```typescript
/// let x = ["a", "b", "c", "d"];
/// assert_eq(is_numeric_list(x), false)
/// ```
#[rhai_fn(name = "is_numeric_list", pure)]
pub fn is_numeric_list(arr: &mut Array) -> bool {
numeric_vector_data(arr).is_ok()
}
/// Tests whether the input is a row vector.
/// ```typescript
/// let row = [[1, 2, 3]];
/// assert_eq(is_row_vector(row), true);
/// ```
/// ```typescript
/// let column = [[1], [2], [3]];
/// assert_eq(is_row_vector(column), false);
/// ```
#[rhai_fn(name = "is_row_vector", pure)]
pub fn is_row_vector(arr: &mut Array) -> bool {
matches!(matrix_dimensions(arr), Some((1, _)))
}
/// Tests whether the input is a column vector.
/// ```typescript
/// let column = [[1], [2], [3]];
/// assert_eq(is_column_vector(column), true);
/// ```
/// ```typescript
/// let row = [[1, 2, 3]];
/// assert_eq(is_column_vector(row), false);
/// ```
#[rhai_fn(name = "is_column_vector", pure)]
pub fn is_column_vector(arr: &mut Array) -> bool {
matches!(matrix_dimensions(arr), Some((_, 1)))
}
/// Tests whether the input is a matrix
/// ```typescript
/// let x = ones([3, 5]);
/// assert_eq(is_matrix(x), true)
/// ```
/// ```typescript
/// let x = ones([5, 5, 5]);
/// assert_eq(is_matrix(x), false)
/// ```
#[rhai_fn(name = "is_matrix", pure)]
pub fn is_matrix(arr: &mut Array) -> bool {
matrix_dimensions(arr).is_some()
}
}