lib_xalg/
lib.rs

1// Copyright 2019 LEXUGE
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16// Documentation
17//! A library for generating random formulas.
18//!
19//! # Features
20//!
21//! - Export to LaTeX.
22//!
23//! - Control the operators which are involved in generating process.
24//!
25//! # Getting Started
26//! ```
27//! use {
28//!    lib_xalg::{
29//!        formula::{OperatorFlag, OperatorFlag::*},
30//!        generate,
31//!    },
32//!    std::collections::HashSet,
33//! };
34//! let hashset = [Add, Sub, Mul, Div, Pow].iter().copied().collect::<HashSet<OperatorFlag>>();
35//! println!("{}", generate(5, 3, 3, &hashset).unwrap());
36//! ```
37
38#![deny(missing_docs)]
39
40// mod(s)
41/// Module of formula AST.
42pub mod formula;
43mod monomial;
44mod traits;
45
46// use(s)
47use {
48    crate::{
49        formula::{Formula, Info, OperatorFlag},
50        traits::Required,
51    },
52    std::collections::HashSet,
53};
54
55/// The enum for denoting error(s).
56#[derive(Debug)]
57pub enum ErrorKind {
58    /// At least one of the arguments can't meet criterion.
59    WrongNumber,
60    /// No operator flags provided
61    NoFlag,
62}
63
64/// Generate the formula AST.
65///
66/// - `depth`: the depth of the AST of the formula. (depth >= 0)
67/// - `exponent_limit`: the max of the exponent of the monomials. Generates in [0..max). (exponent_limit >= 1)
68/// - `coefficient_limit`: the max of the coefficient of the monomials. Generates in [0..max). (coefficient_limit >= 2)
69/// - `operator_flags`: A set of flags, which controls the operators in the generating process.
70pub fn generate<T: Required, S: std::hash::BuildHasher>(
71    depth: T,
72    exponent_limit: T,
73    coefficient_limit: T,
74    operator_flags: &HashSet<OperatorFlag, S>,
75) -> Result<Formula<T>, ErrorKind> {
76    if (depth < T::zero())
77        || (exponent_limit < T::one())
78        || (coefficient_limit < (T::one() + T::one()))
79    {
80        Err(ErrorKind::WrongNumber)
81    } else if operator_flags.is_empty() {
82        Err(ErrorKind::NoFlag)
83    } else {
84        let operator_flags = operator_flags
85            .iter()
86            .copied()
87            .collect::<Vec<OperatorFlag>>();
88        Ok(Formula::generate(Info::new(
89            depth,
90            exponent_limit,
91            coefficient_limit,
92            &operator_flags,
93        )))
94    }
95}