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
16 changes: 12 additions & 4 deletions graph_framework/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,24 @@ namespace graph {
ap2->get_right(), ap2->get_y_scale(), ap2->get_y_offset());
}

// Handle cases like sqrt(c*x) where c is constant or cases like
// sqrt((x^a)*y).
// Handle cases like sqrt(c*x) where c is constant or cases like sqrt((x^a)*y).
// Note that we need to disable this reduction C is a negative real.
auto am = multiply_cast(this->arg);
if (am.get()) {
if (pow_cast(am->get_left()).get() ||
am->get_left()->is_constant() ||
pow_cast(am->get_right()).get() ||
am->get_right()->is_constant()) {
return sqrt(am->get_left()) *
sqrt(am->get_right());
if constexpr (jit::complex_scalar<T>) {
return sqrt(am->get_left()) *
sqrt(am->get_right());
} else {
if (am->get_left()->is_constant() &&
!am->get_left()->evaluate().is_negative()) {
return sqrt(am->get_left()) *
sqrt(am->get_right());
}
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions graph_tests/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ void test_sqrt() {
assert(!sqrt_var->is_constant() && "Did not expect a constant.");
assert(sqrt_var->is_all_variables() && "Expected a variable.");
assert(sqrt_var->is_power_like() && "Expected a power like.");

if constexpr (!jit::complex_scalar<T>) {
auto sqrtnegvar = graph::sqrt(-x_var);
assert(graph::sqrt_cast(sqrtnegvar) &&
"Expected no reduction.");
}
}

//------------------------------------------------------------------------------
Expand Down
Loading