radiate_gp/ops/
primitives.rs

1use crate::{Op, ops::op_names};
2use std::{fmt::Display, ops::Range};
3
4impl<T> Op<T> {
5    pub fn var(index: usize) -> Self {
6        let name = radiate_core::intern!(format!("X{}", index));
7        Op::Var(name, index)
8    }
9
10    pub fn vars(range: Range<usize>) -> Vec<Self> {
11        range.map(Op::var).collect()
12    }
13
14    pub fn constant(value: T) -> Self
15    where
16        T: Display,
17    {
18        let name = radiate_core::intern!(format!("{}", value));
19        Op::Const(name, value)
20    }
21
22    pub fn named_constant(name: &'static str, value: T) -> Self {
23        Op::Const(name, value)
24    }
25
26    pub fn identity() -> Self
27    where
28        T: Clone,
29    {
30        Op::Fn(op_names::IDENTITY, 1.into(), |inputs: &[T]| {
31            inputs[0].clone()
32        })
33    }
34}