-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify Complex Arguments to Exp #1332
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,25 @@ int Pow::compare(const Basic &o) const | |
return base_cmp; | ||
} | ||
|
||
bool exp_mul_helper(const RCP<const Basic> &b) | ||
{ | ||
if (is_a_Complex(*down_cast<const Mul &>(*b).get_coef())) { | ||
const Mul &s = down_cast<const Mul &>(*b); | ||
const map_basic_basic &dict = s.get_dict(); | ||
RCP<const Number> coef | ||
= down_cast<const ComplexBase &>(*s.get_coef()).imaginary_part(); | ||
RCP<const Basic> arg = mul(coef, integer(2)); | ||
if (dict.size() == 1 and is_a<Integer>(*arg)) { | ||
for (const auto &p : dict) { | ||
if (eq(*p.first, *pi) and eq(*p.second, *one)) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
RCP<const Basic> pow(const RCP<const Basic> &a, const RCP<const Basic> &b) | ||
{ | ||
if (is_a_Number(*b) and down_cast<const Number &>(*b).is_zero()) { | ||
|
@@ -164,6 +183,39 @@ RCP<const Basic> pow(const RCP<const Basic> &a, const RCP<const Basic> &b) | |
RCP<const Pow> A = rcp_static_cast<const Pow>(a); | ||
return pow(A->get_base(), mul(A->get_exp(), b)); | ||
} | ||
if (eq(*a, *E)) { | ||
if (is_a<Mul>(*b) and exp_mul_helper(b)) { | ||
RCP<const Number> coef = down_cast<const ComplexBase &>( | ||
*down_cast<const Mul &>(*b).get_coef()) | ||
.imaginary_part(); | ||
RCP<const Basic> arg = mul(coef, integer(2)); | ||
long n = ((down_cast<const Integer &>(*arg).as_int() % 4) + 4) % 4; | ||
if (!n) { | ||
return one; | ||
} else if (n == 1) { | ||
return I; | ||
} else if (n == 2) { | ||
return minus_one; | ||
} else { | ||
return Complex::from_two_nums(*zero, *minus_one); | ||
} | ||
} else if (is_a<Add>(*b)) { | ||
const umap_basic_num &dict = down_cast<const Add &>(*b).get_dict(); | ||
umap_basic_num new_dict; | ||
RCP<const Number> coef = down_cast<const Add &>(*b).get_coef(); | ||
RCP<const Basic> s = one; | ||
for (const auto &p : dict) { | ||
if (eq(*p.first, *pi) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To do this, you don't need to iterate the dictionary. Just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way I can remove that particular entry of |
||
and exp_mul_helper(mul(p.first, p.second))) { | ||
s = exp(mul(p.first, p.second)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can a call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the expense of code duplication it can be. I can't seem to think of another way. |
||
} else { | ||
Add::dict_add_term(new_dict, p.second, p.first); | ||
} | ||
} | ||
return mul(s, make_rcp<const Pow>( | ||
a, Add::from_dict(coef, std::move(new_dict)))); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to carefully benchmark these two if branches, before and after this PR. So the first if clause would run for something like If so, then we need to benchmark these two things and similar things a lot. |
||
return make_rcp<const Pow>(a, b); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this function returning -1, 0, 1,2,3 where -1 is when it is not what we want and the others
n
in the function beloe