Skip to content
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

Expand integral powers of complex nos #264

Merged
merged 4 commits into from Jul 26, 2014
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/add.cpp
Expand Up @@ -120,11 +120,18 @@ std::string Add::__str__() const
// TODO: extend this for Rationals as well:
if (is_a<Integer>(*p.second) &&
rcp_static_cast<const Integer>(p.second)->is_negative()) {
if (counter >= 1)
if (counter >= 1) {
o << " - ";
else
} else {
o << "-";
}
o << -(rcp_static_cast<const Integer>(p.second))->i;
} else if (is_a<Complex>(*p.second)) {
if (!(rcp_static_cast<const Complex>(p.second)->is_reim_zero())) {
o << "(" << *(p.second) <<")";
} else {
o << *(p.second);
}
} else {
o << *(p.second);
}
Expand Down
2 changes: 1 addition & 1 deletion src/complex.h
Expand Up @@ -192,7 +192,7 @@ class Complex : public Number {
if (conjugate.get_num() == 0) {
throw std::runtime_error("Divide by zero.");
} else {
return from_mpq((this->real_ * (-other.i)) / conjugate, (this->imaginary_ * other.i) / conjugate);
return from_mpq((this->real_ * other.i) / conjugate, (this->imaginary_ * (-other.i)) / conjugate);
}
}
//! Converts the param `other` appropriately and then calls `addcomp`
Expand Down
88 changes: 48 additions & 40 deletions src/pow.cpp
Expand Up @@ -115,6 +115,48 @@ std::string Pow::__str__() const
return o.str();
}

RCP<const Number> pow_number(const RCP<const Number> &x, long n)
{
RCP<const Number> r, p;
long mask = 1;
r = one;
p = x;
while (mask > 0 && n >= mask) {
if (n & mask)
r = mulnum(r, p);
mask = mask << 1;
p = mulnum(p, p);
}
return r;
}

void pow_complex(const Ptr<RCP<const Number>> &self,
const RCP<const Complex> &base_,
const RCP<const Integer> &exp_)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The base_ is passed into functions that expect RCP, but exp_ isn't, so you should just declare it as
const Integer &exp_. Essentially, the rule of thumb is: don't use RCP, unless you have to (as in general, things are faster if you don't use RCP).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sure, I'll keep note of that next time onwards.

{
if (base_->is_re_zero()) {
// Imaginary Number raised to an integer power.
RCP<const Number> im = Rational::from_mpq(base_->imaginary_);
RCP<const Number> res;
mod(outArg(res), *exp_, *integer(4));
if (eq(res, zero)) {
res = one;
} else if (eq(res, one)) {
res = I;
} else if (eq(res, integer(2))) {
res = minus_one;
} else {
res = mulnum(I, minus_one);
}
*self = mulnum(im->pow(*exp_), res);
} else if (exp_->is_positive()) {
*self = pow_number(base_, exp_->as_int());
} else {
*self = pow_number(divnum(one, base_), -1 * exp_->as_int());
}

}

RCP<const Basic> pow(const RCP<const Basic> &a, const RCP<const Basic> &b)
{
if (eq(b, zero)) return one;
Expand Down Expand Up @@ -142,26 +184,9 @@ RCP<const Basic> pow(const RCP<const Basic> &a, const RCP<const Basic> &b)
} else if (is_a<Complex>(*a)) {
RCP<const Complex> exp_new = rcp_static_cast<const Complex>(a);
RCP<const Integer> pow_new = rcp_static_cast<const Integer>(b);
if (pow_new->is_minus_one()) {
return exp_new->rdivcomp(*pow_new);
} else if (exp_new->is_re_zero()) {
// Imaginary Number raised to an integer power.
RCP<const Number> im = Rational::from_mpq(exp_new->imaginary_);
RCP<const Number> res;
mod(outArg(res), *pow_new, *integer(4));
if (eq(res, zero)) {
res = one;
} else if (eq(res, one)) {
res = I;
} else if (eq(res, integer(2))) {
res = minus_one;
} else {
res = mulnum(I, minus_one);
}
return mul(im->pow(*pow_new), res);
} else {
return rcp(new Pow(a, b));
}
RCP<const Number> res;
pow_complex(outArg(res), exp_new, pow_new);
return res;
} else {
throw std::runtime_error("Not implemented");
}
Expand Down Expand Up @@ -371,27 +396,10 @@ RCP<const Basic> pow_expand(const RCP<const Pow> &self)
pownum(i2->second,
rcp_static_cast<const Number>(exp)));
} else if (is_a<Complex>(*(i2->second))) {
if (exp->is_one()) {
imulnum(outArg(overall_coeff), i2->second);
} else if (exp->is_minus_one()) {
idivnum(outArg(overall_coeff), i2->second);
} else if (rcp_static_cast<const Complex>(i2->second)->is_re_zero() &&
is_a<Integer>(*exp)) {
// Imaginary Number raised to an integer power.
RCP<const Number> im = Rational::from_mpq(rcp_static_cast<const Complex>(i2->second)->imaginary_);
if (is_a<Integer>(*exp)) {
RCP<const Integer> pow_new = rcp_static_cast<const Integer>(exp);
RCP<const Number> res;
mod(outArg(res), *pow_new, *integer(4));
if (eq(res, zero)) {
res = one;
} else if (eq(res, one)) {
res = I;
} else if (eq(res, integer(2))) {
res = minus_one;
} else {
res = mulnum(I, minus_one);
}
imulnum(outArg(overall_coeff), mulnum(im->pow(*pow_new), res));
RCP<const Complex> base_new = rcp_static_cast<const Complex>(i2->second);
pow_complex(outArg(overall_coeff), base_new, pow_new);
} else {
Mul::dict_add_term_new(outArg(overall_coeff), d, exp, i2->second);
}
Expand Down
29 changes: 29 additions & 0 deletions src/tests/basic/test_arit.cpp
Expand Up @@ -731,6 +731,35 @@ void test_expand3()
<< "ms" << std::endl;
std::cout << "number of terms: "
<< rcp_dynamic_cast<const Add>(r)->dict_.size() << std::endl;

RCP<const Number> rc1, rc2, c1;
rc1 = Rational::from_two_ints(integer(2), integer(1));
rc2 = Rational::from_two_ints(integer(3), integer(1));

c1 = Complex::from_two_nums(*rc1, *rc2);
e = pow(add(x, c1), integer(40));

t1 = std::chrono::high_resolution_clock::now();
r = expand(e);
t2 = std::chrono::high_resolution_clock::now();

std::cout << *r << std::endl;
std::cout
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
<< "ms" << std::endl;
std::cout << "number of terms: "
<< rcp_dynamic_cast<const Add>(r)->dict_.size() << std::endl;

e = pow(c1, integer(-40));

t1 = std::chrono::high_resolution_clock::now();
r = expand(e);
t2 = std::chrono::high_resolution_clock::now();

std::cout << *r << std::endl;
std::cout
<< std::chrono::duration_cast<std::chrono::milliseconds>(t2-t1).count()
<< "ms" << std::endl;
}

int main(int argc, char* argv[])
Expand Down