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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#![warn(clippy::all)]
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
#![warn(clippy::missing_docs_in_private_items)]
#![doc = include_str!("../README.md")]
//! # Constants
//! This package provides a few constants that are common and useful.
//! ## `pi`
//! The ratio of a circle's circumference to its diameter.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! pi // => 3.141592653589793
//! # ").unwrap();
//! # assert_eq!(result, std::f64::consts::PI);
//! ```
//!
//! ## `e`
//! Euler's number.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! e // => 2.718281828459045
//! # ").unwrap();
//! # assert_eq!(result, std::f64::consts::E);
//! ```
//!
//! ## `g`
//! Acceleration due to gravity on Earth in m/s^2.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! g // => 9.80665
//! # ").unwrap();
//! # assert_eq!(result, 9.80665);
//! ```
//!
//! # Functions
//! This package provides a large variety of functions to help with scientific computing. Each one
//! of these is written in [`Rhai`](https://rhai.rs/) itself! The source code is [here](https://github.com/cmccomb/rhai-sci/tree/master/scripts).
//!
//! ## `argmax`
//! Returns the argument of the largest element in a 1-D array.
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! argmax([43, 42, 500]) // => 2
//! # ").unwrap();
//! # assert_eq!(result, 2);
//! ```
//!
//! ## `argmin`
//! Returns the argument of the smallest element in a 1-D array.
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! argmin([43, 42, -500]) // => 2
//! # ").unwrap();
//! # assert_eq!(result, 2);
//! ```
//!
//! ## `bounds`
//! Returns the bounds (smallest and largest elements) of a 1-D array.
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! bounds([32, 15, -7, 10, 1000, 41, 42]) // => [-7, 1000]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<i64>()).collect::<Vec<i64>>(), vec![-7, 1000]);
//! ```
//!
//! ## `diag`
//! This function can be used in two distinct ways.
//! 1. If the argument is an 2-D array, `diag` returns an array containing the diagonal of the array.
//! 2. If the argument is a 1-D array, `diag` returns a matrix containing the argument along the
//! diagonal and zeros elsewhere.
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! diag([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) // => [1, 5, 9]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<i64>()).collect::<Vec<i64>>(), vec![1, 5, 9]);
//! ```
//! ```
//! # use rhai::{Array, serde::from_dynamic};
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! diag([1, 2, 3]) // => [[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]]
//! # ").unwrap();
//! # let vecresult = result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #     ).collect::<Vec<Vec<f64>>>();
//! # assert_eq!(vecresult, vec![vec![1.0, 0.0, 0.0], vec![0.0, 2.0, 0.0], vec![0.0, 0.0, 3.0]]);
//! ```
//!
//! ## `eye`
//! Create an identity matrix with ones along the diagonal and zeros elsewhere. Can be called with
//! either one argument (creating a square matrix) or two arguments (specifying the number of rows
//! and columns separately).
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! eye(3) // => [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
//! # ").unwrap();
//! # let vecresult = result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #     ).collect::<Vec<Vec<f64>>>();
//! # let sum: f64 = vecresult.into_iter().map(|x| x.into_iter().sum()).collect::<Vec<f64>>().into_iter().sum();
//! # assert_eq!(sum, 3.0);
//! ```
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! eye(3, 3) // => [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]
//! # ").unwrap();
//! # let vecresult = result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #     ).collect::<Vec<Vec<f64>>>();
//! # let sum: f64 = vecresult.into_iter().map(|x| x.into_iter().sum()).collect::<Vec<f64>>().into_iter().sum();
//! # assert_eq!(sum, 3.0);
//! ```
//!
//! ## `interp1`
//! Given reference data, perform linear interpolation.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! interp1([0, 1], [1, 2], 0.5) // => 1.5
//! # ").unwrap();
//! # assert_eq!(result, 1.5);
//! ```
//!
//! ## `iqr`
//! Returns the inter-quartile range for a 1-D array.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! iqr([1, 1, 1, 1, 1, 1, 1, 5, 6, 9, 9, 9, 9, 9, 9, 9, 9])" // => 8.0
//! # ).unwrap();
//! # assert_eq!(result, 8.0);
//! ```
//!
//! ## `linspace`
//! Returns an array containing a number of elements linearly spaced between two bounds.
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! linspace(1, 2, 5) // => [1.0, 1.25, 1.5, 1.75, 2.0]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<f64>()).collect::<Vec<f64>>(), vec![1.0, 1.25, 1.5, 1.75, 2.0]);
//! ```
//!
//! ## `logspace`
//! Returns an array containing a number of elements logarithmically spaced between two bounds.  
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! logspace(1, 3, 3) // => [10.0, 100.0, 1000.0]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<f64>()).collect::<Vec<f64>>(), vec![10.0, 100.0, 1000.0]);
//! ```
//!
//! ## `max`
//! Returns the highest value between a pair of numbers (if called with two arguments) or in a 1-D
//! array (if called with a single `Array`-type argument).
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! max(41, 42) // => 42
//! # ").unwrap();
//! # assert_eq!(result, 42);
//! ```
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! max([41, 42, -1, 7, 2]) // => 42
//! # ").unwrap();
//! # assert_eq!(result, 42);
//! ```
//!
//! ## `min`
//! Returns the lowest value between a pair of numbers (if called with two arguments) or in a 1-D
//! array (if called with a single `Array`-type argument).
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! min(43, 42) // => 42
//! # ").unwrap();
//! # assert_eq!(result, 42);
//! ```
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! min([43, 42, 500]) // => 42
//! # ").unwrap();
//! # assert_eq!(result, 42);
//! ```
//!
//! ## `maxk`
//! Returns the k highest values from a 1-D array.
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! maxk([32, 15, -7, 10, 1000, 41, 42], 3) // => [41, 42, 1000]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<i64>()).collect::<Vec<i64>>(), vec![41, 42, 1000]);
//! ```
//!
//! ## `mean`
//! Returns the average of a 1-D array.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! mean([1, 2, 3]) // => 2.0
//! # ").unwrap();
//! # assert_eq!(result, 2.0);
//! ```
//!
//! ## `median`
//! Returns the median of a 1-D array.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! median([1, 1, 1, 1, 2, 5, 6, 7, 8]) // => 2.0
//! # ").unwrap();
//! # assert_eq!(result, 2.0);
//! ```
//!
//! ## `mink`
//! Returns the k smallest values in a 1-D array.
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! mink([32, 15, -7, 10, 1000, 41, 42], 3) // => [-7, 10, 15]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<i64>()).collect::<Vec<i64>>(), vec![-7, 10, 15]);
//! ```
//!
//! ## `mode`
//! Returns the mode of a 1-D array.
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! mode([1, 2, 2, 2, 2, 3]) // => 2
//! # ").unwrap();
//! # assert_eq!(result, 2);
//! ```
//!
//! ## `ndims`
//! Returns the number of dimensions in an array.
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! ndims(ones(4, 6)) // => 2
//! # ").unwrap();
//! # assert_eq!(result, 2);
//! ```
//!
//! ## `numel`
//! Returns the number of elements in an array.
//! ```
//! # use rhai::INT;
//! # use rhai_sci::eval;
//! # let result: INT = eval("
//! numel(ones(4, 6)) // => 24
//! # ").unwrap();
//! # assert_eq!(result, 24);
//! ```
//!
//! ## `ones`
//! Create an matrix filled with ones. Can be called with either one argument (creating a square
//! matrix) or two arguments (specifying the number of rows and columns separately).
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! ones(3) // => [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #    ).collect::<Vec<Vec<f64>>>(), vec![vec![1.0; 3]; 3]);
//! ```
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! ones(3, 3) // => [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #     ).collect::<Vec<Vec<f64>>>(), vec![vec![1.0; 3]; 3]);
//! ```
//!
//! ## `prctile`
//! Returns a given percentile value for a 1-D array of data.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! prctile([1, 2, 0, 3, 4], 50) // => 2.0
//! # ").unwrap();
//! # assert_eq!(result, 2.0);
//! ```
//!
//! ## `rand`
//! Create a matrix filled with random values between 0 and 1. Can be called with either zero
//! arguments (returning a single random value), one argument (creating a square matrix) or two
//! arguments (specifying the number of rows and columns separately).
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! rand() // => 0.44392202188914254
//! # ").unwrap();
//! # assert!(result < 1.0 && result > 0.0);
//! ```
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! rand(3) // => [[0.7333405150571339, 0.3597611759299407, 0.8809543481098305], [0.5327545327750203, 0.9185256001032435, 0.7226084132391764], [0.14803039057912748, 0.8924466624235429, 0.40943835774171167]]
//! # ").unwrap();
//! # let vecresult = result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #     ).collect::<Vec<Vec<f64>>>();
//! # let sum: f64 = vecresult.into_iter().map(|x| x.into_iter().sum()).collect::<Vec<f64>>().into_iter().sum();
//! # assert!(sum < 9.0 && sum > 0.0);
//! ```
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! rand(3, 3) // => [[0.7333405150571339, 0.3597611759299407, 0.8809543481098305], [0.5327545327750203, 0.9185256001032435, 0.7226084132391764], [0.14803039057912748, 0.8924466624235429, 0.40943835774171167]]
//! # ").unwrap();
//! # let vecresult = result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #     ).collect::<Vec<Vec<f64>>>();
//! # let sum: f64 = vecresult.into_iter().map(|x| x.into_iter().sum()).collect::<Vec<f64>>().into_iter().sum();
//! # assert!(sum < 9.0 && sum > 0.0);
//! ```
//!
//! ## `size`
//! Returns the size along each dimension of an array.
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! size(ones(3, 5)) // => [3, 5]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<i64>()).collect::<Vec<i64>>(), vec![3, 5]);
//! ```
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # let result: Array = eval("
//! size([[[1, 2]]]) // => [1, 1, 2]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(|x|x.cast::<i64>()).collect::<Vec<i64>>(), vec![1, 1, 2]);
//! ```
//!
//! ## `std`
//! Returns the standard deviation of a 1-D array.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! std([1, 2, 3]) // => 1.0
//! # ").unwrap();
//! # assert_eq!(result, 1.0);
//! ```
//!
//! ## `variance`
//! Returns the variance of a 1-D array.
//! ```
//! # use rhai::FLOAT;
//! # use rhai_sci::eval;
//! # let result: FLOAT = eval("
//! variance([1, 2, 3]) // => 1.0
//! # ").unwrap();
//! # assert_eq!(result, 1.0);
//! ```
//!
//! ## `zeros`
//! Create an matrix filled with ones. Can be called with either one argument (creating a square
//! matrix) or two arguments (specifying the number of rows and columns separately).
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! zeros(3) // => [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #     ).collect::<Vec<Vec<f64>>>(), vec![vec![0.0; 3]; 3]);
//! ```
//! ```
//! # use rhai::Array;
//! # use rhai_sci::eval;
//! # use rhai::serde::from_dynamic;
//! # let result: Array = eval("
//! zeros(3, 3) // => [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]
//! # ").unwrap();
//! # assert_eq!(result.into_iter().map(
//! #         |x| from_dynamic(&x).unwrap()
//! #    ).collect::<Vec<Vec<f64>>>(), vec![vec![0.0; 3]; 3]);
//! ```
//!

use rhai::{def_package, packages::Package, plugin::*, Array, EvalAltResult, Position, FLOAT, INT};
use rhai_rand::RandomPackage;
use std::ops::{Range, RangeInclusive};

def_package! {
    /// Package for scientific computing
    pub SciPackage(lib) {

        RandomPackage::init(lib);

        //
        let engine = Engine::new();
        let ast = engine.compile(aggregate_functions()).unwrap();
        let my_module = Module::eval_ast_as_new(rhai::Scope::new(), &ast, &engine).unwrap();
        lib.fill_with(&my_module);

        // combine_with_exported_module!(lib, "rand", rand_functions);
    }
}

// #[export_module]
// mod rand_functions {
//     //! Generate a random boolean value with a probability of being `true`.
//     //!
//     //! `probability` must be between `0.0` and `1.0` (inclusive).
//     //!
//     //! # Example
//     //!
//     //! ```rhai
//     //! let decision = rand(0.01);  // 1% probability
//     //!
//     //! if decision {
//     //!     print("You hit the Jackpot!")
//     //! }
//     //! ```
//     #[rhai_fn(return_raw)]
//     pub fn rand_bool_with_probability(probability: FLOAT) -> Result<bool, Box<EvalAltResult>> {
//         if probability < 0.0 || probability > 1.0 {
//             Err(EvalAltResult::ErrorArithmetic(
//                 format!(
//                     "Invalid probability (must be between 0.0 and 1.0): {}",
//                     probability
//                 ),
//                 Position::NONE,
//             )
//             .into())
//         } else {
//             Ok(rand::thread_rng().gen_bool(probability as f64))
//         }
//     }
// }

fn aggregate_functions() -> String {
    String::new()
        + include_str!("../scripts/max.rhai")
        + include_str!("../scripts/maxk.rhai")
        + include_str!("../scripts/argmax.rhai")
        + include_str!("../scripts/min.rhai")
        + include_str!("../scripts/argmin.rhai")
        + include_str!("../scripts/eye.rhai")
        + include_str!("../scripts/mink.rhai")
        + include_str!("../scripts/diag.rhai")
        + include_str!("../scripts/bounds.rhai")
        + include_str!("../scripts/size.rhai")
        + include_str!("../scripts/mean.rhai")
        + include_str!("../scripts/numel.rhai")
        + include_str!("../scripts/ndims.rhai")
        + include_str!("../scripts/variance.rhai")
        + include_str!("../scripts/std.rhai")
        + include_str!("../scripts/mode.rhai")
        + include_str!("../scripts/median.rhai")
        + include_str!("../scripts/iqr.rhai")
        + include_str!("../scripts/prctile.rhai")
        + include_str!("../scripts/interp1.rhai")
        + include_str!("../scripts/linspace.rhai")
        + include_str!("../scripts/logspace.rhai")
        + include_str!("../scripts/zeros.rhai")
        + include_str!("../scripts/ones.rhai")
        + include_str!("../scripts/myrand.rhai")
        + include_str!("../scripts/constants.rhai")
}

/// This provides the ability to easily evaluate a line (or lines) of code without explicitly
/// setting up a script engine
/// ```
/// use rhai_sci::eval;
/// use rhai::FLOAT;
/// print!("{:?}", eval::<FLOAT>("let x = max(5, 2); x + min(3, 72)"));
/// ```
pub fn eval<T: Clone + 'static>(script: &str) -> Result<T, Box<EvalAltResult>> {
    let mut engine = Engine::new();
    engine.register_global_module(SciPackage::new().as_shared_module());
    engine.eval::<T>(script)
}