Ask Your Question

Sage equivalent to Mathematica Chop function?

asked 2018-03-13 11:17:06 +0200

joaoff gravatar image

updated 2018-03-26 16:29:43 +0200

slelievre gravatar image

Does anyone know a function in Sage equivalent to Mathematica _Chop()_ function ( )? I couldn't find any.

In fact, I would like to drop the imaginary parts that are close to zero of complex polynomial coefficients.

This shouldn't be difficult to implement but is something that I use a lot, it is strange not find an implementation.


edit retag flag offensive close merge delete


I guess you could just try real_part() if you are sure this is the case?

kcrisman gravatar imagekcrisman ( 2018-03-14 03:47:05 +0200 )edit

Thank you, but _real_part()_ will not work in all cases.

joaoff gravatar imagejoaoff ( 2018-03-14 15:37:02 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-03-14 15:19:25 +0200

joaoff gravatar image

I ended up by creating my own function.

def crop(CC_value, RR_threshold):
    if abs(CC_value.real()) > RR_threshold and abs(CC_value.imag()) > RR_threshold:
        return CC_value
    elif abs(CC_value.real()) > RR_threshold and abs(CC_value.imag()) < RR_threshold:
        return CC_value.real()
    elif abs(CC_value.real()) < RR_threshold and abs(CC_value.imag()) > RR_threshold:
        return CC_value.imag()*i
        return 0

print(crop(0.6671 + 1.660*i, 10^-10))
print(crop(0.6671*10^-15 + 1.660*i, 10^-10))
print(crop(2/3 - 1.660*10^-13*i, 10^-10))
print(crop(0.6671*10^-11 + 1.660*10^-17*i, 10^-10))
print(crop(1*10^-11 - 1.660*10^-17*i, 10^-15))


0.667100000000000 + 1.66000000000000*I
edit flag offensive delete link more



Tip: to make things easier to read (and faster), avoid mixing in rationals with floating-point numbers:

  • write 1e-10 instead 10^-10
  • write 0.6671e-11 instead of 0.6671*10^-11


slelievre gravatar imageslelievre ( 2018-03-16 18:07:59 +0200 )edit

Do you think using fast_callable would help here? (I don't really know how to use it properly, of course.)

kcrisman gravatar imagekcrisman ( 2018-03-19 18:19:56 +0200 )edit

Very interesting! I can't see the applicability of the mentioned function in this particular problem, but it probably can help with my other problem, plotting big symbolic expressions (

In Mathematica, when plotting a very big and complex symbolic expression, I generally use the Evaluate[] function (e.g. I was looking for a similar function in Sage and, apparently, fast_callable is this function. I will give it a try.

Thanks a lot!

joaoff gravatar imagejoaoff ( 2018-03-26 22:39:24 +0200 )edit

Yes, this is exactly what that is for.

kcrisman gravatar imagekcrisman ( 2018-03-28 03:55:16 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2018-03-13 11:17:06 +0200

Seen: 824 times

Last updated: Mar 26 '18