Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/canon/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,15 @@ impl CanonContext {
return CanonExpr::Linear(cx);
}

if p.abs() < 1e-10 {
// x^0 = 1 elementwise.
return CanonExpr::Linear(LinExpr::constant(DMatrix::from_element(
cx.shape.rows(),
cx.shape.cols(),
1.0,
)));
}

// Create auxiliary variable t for the result
let (t_var_id, t) = self.new_nonneg_aux_var(cx.shape.clone());
let _ = t_var_id;
Expand Down
24 changes: 24 additions & 0 deletions tests/canon_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,30 @@ fn test_quad_over_lin_objective_variable_denominator() {
assert!((sol.value.unwrap() - 2.0).abs() < TOL);
}

#[test]
fn test_power_zero_is_constant_one() {
let x = variable(());

let sol = Problem::minimize(power(&x, 0.0))
.subject_to([x.ge(1.0)])
.solve()
.expect("problem should solve");

assert!((sol.value.unwrap() - 1.0).abs() < TOL);
}

#[test]
fn test_power_zero_vector_is_constant_one_elementwise() {
let x = variable(3);

let sol = Problem::minimize(sum(&power(&x, 0.0)))
.subject_to([x.ge(1.0)])
.solve()
.expect("problem should solve");

assert!((sol.value.unwrap() - 3.0).abs() < TOL);
}

#[test]
fn test_power_two_is_elementwise() {
let x = variable(2);
Expand Down
Loading