problemreductions/rules/
partition_cosineproductintegration.rs1use crate::models::misc::{CosineProductIntegration, Partition};
14use crate::reduction;
15use crate::rules::traits::{ReduceTo, ReductionResult};
16
17#[derive(Debug, Clone)]
19pub struct ReductionPartitionToCPI {
20 target: CosineProductIntegration,
21}
22
23impl ReductionResult for ReductionPartitionToCPI {
24 type Source = Partition;
25 type Target = CosineProductIntegration;
26
27 fn target_problem(&self) -> &Self::Target {
28 &self.target
29 }
30
31 fn extract_solution(
32 &self,
33 target_solution: &<Self::Target as crate::traits::Problem>::Solution,
34 ) -> crate::rules::ExtractionResult<<Self::Source as crate::traits::Problem>::Solution> {
35 crate::rules::traits::validate_target_witness(
36 self.target_problem(),
37 target_solution,
38 |value| value.0,
39 "target witness does not satisfy the target problem",
40 )?;
41
42 Ok(target_solution.to_vec())
43 }
44}
45
46#[crate::aggregate_reduction(identity)]
47impl crate::rules::AggregateReductionResult for ReductionPartitionToCPI {}
48
49#[reduction(
50 transform = exact {
51 num_coefficients = "num_elements",
52 })]
53impl ReduceTo<CosineProductIntegration> for Partition {
54 type Result = ReductionPartitionToCPI;
55
56 fn reduce_to(&self) -> Result<Self::Result, crate::rules::ReductionError> {
57 let coefficients = self.sizes().to_vec();
58 Ok(ReductionPartitionToCPI {
59 target: CosineProductIntegration::new(coefficients),
60 })
61 }
62}
63
64#[cfg(feature = "example-db")]
65pub(crate) fn canonical_rule_example_specs() -> Vec<crate::example_db::specs::RuleExampleSpec> {
66 use crate::export::SolutionPair;
67
68 vec![crate::example_db::specs::RuleExampleSpec {
69 id: "partition_to_cosineproductintegration",
70 build: || {
71 crate::example_db::specs::rule_example_with_witness::<_, CosineProductIntegration>(
78 Partition::new(vec![3, 1, 1, 2, 2, 1]).unwrap(),
79 SolutionPair {
80 source_config: serde_json::json!(vec![true, false, false, true, false, false]),
81 target_config: serde_json::json!(vec![true, false, false, true, false, false]),
82 },
83 )
84 },
85 }]
86}
87
88#[cfg(test)]
89#[path = "../unit_tests/rules/partition_cosineproductintegration.rs"]
90mod tests;