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
//! A function for creating sparse constraint systems.

use std::{fmt::Debug, sync::Arc};

use crate::{
    macros::{RawComponent, RawConstraint, RawMethod},
    model::ConstraintSystem,
};

/// Constructs a constraint system with few constraints between variables.
pub fn make_sparse_cs<T>(_: usize, n_variables: usize) -> ConstraintSystem<T>
where
    T: Debug + Clone + Default + Send + 'static,
{
    // Shared dummy apply function
    let apply = Arc::new(Ok);

    // Shared variable names
    let variable_names: Vec<String> = (0..n_variables)
        .map(|id| "var".to_string() + &id.to_string())
        .collect();

    let mut cs = ConstraintSystem::new();

    let mut constraints = Vec::new();

    const STEP_SIZE: usize = 5;
    for from_idx in (0..n_variables.saturating_sub(STEP_SIZE)).step_by(STEP_SIZE) {
        for offset in (1..=5).step_by(2) {
            let from = &variable_names[from_idx];
            let to = &variable_names[from_idx + offset];
            let constraint: RawConstraint<T> = RawConstraint::new(
                "constraint",
                vec![
                    RawMethod::new("from->to", vec![from], vec![to], apply.clone()),
                    RawMethod::new("to->from", vec![to], vec![from], apply.clone()),
                ],
            );
            constraints.push(constraint);
        }
    }

    // Construct component
    let name = "0".to_string();
    let comp = RawComponent::new(
        name,
        variable_names,
        vec![T::default(); n_variables],
        constraints,
    );

    cs.add_component(comp.into_component());

    cs
}