# Sage equivalent to Mathematica Chop function?

Does anyone know a function in Sage equivalent to Mathematica _Chop()_ function ( http://reference.wolfram.com/language... )? 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.

Cordially

edit retag close merge delete

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

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

Sort by » oldest newest most voted

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
else:
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))


Output

0.667100000000000 + 1.66000000000000*I
1.66000000000000*I
0.666666666666667
0
1.00000000000000e-11

more

1

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

etc.

1

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

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 (https://ask.sagemath.org/question/416...).

In Mathematica, when plotting a very big and complex symbolic expression, I generally use the Evaluate[] function (e.g. http://reference.wolfram.com/language...). 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!