Ask Your Question

Sage equivalent to Mathematica Chop function?

asked 2018-03-13 05:17:06 -0500

joaoff gravatar image

updated 2018-03-26 09:29:43 -0500

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-13 21:47:05 -0500 )edit

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

joaoff gravatar imagejoaoff ( 2018-03-14 09:37:02 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-03-14 09:19:25 -0500

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 12:07:59 -0500 )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 12:19:56 -0500 )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 15:39:24 -0500 )edit

Yes, this is exactly what that is for.

kcrisman gravatar imagekcrisman ( 2018-03-27 20:55:16 -0500 )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 05:17:06 -0500

Seen: 80 times

Last updated: Mar 26