Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Thank you, @dan_fulea!

I ended by taking a different approach. I created two functions, one for cropping the real and/or imaginary parts that are below a threshold, as the Mathematica function, and another that apply this crop function to generate a new polynomial with coefficients approximated to a specified number of digits. This function also converts the real numbers with zero fractional part (e.g. 1.0000) to the equivalent integer, so that the coefficient of any term that is exactly 1, do not show.

e = 1/46656*(9*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)*(4*s^2 + 1) - 7*(4*(9*s^2 + 1)*(6*s + 1/tan(55/72)) + 5*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))))*(36*s^2 + 25) - 11/46656*(4*(4*(9*s^2 + 1)*(6*s + 1/tan(55/72)) + 5*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))*(9*s^2 + 4) + 9*(9*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)*(4*s^2 + 1) - 7*(4*(9*s^2 + 1)*(6*s + 1/tan(55/72)) + 5*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))))/(16/(7/(8/(5/tan(295/72) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))) - 7/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))))/(20/(27/(32/(35/(6/tan(5) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))) - 7/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))) - 9/(16/(7/(8/(5/tan(295/72) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))) - 7/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))))
f = (s + 0.267263403945694 + 0.717946300381457*I)*(s + 0.267263403945694 - 0.717946300381457*I)*(s + 0.0662957565747883 + 0.945082838443603*I)*(s + 0.0662957565747883 - 0.945082838443603*I)*(s + 1.58872258534575e-15 + 0.323790242740243*I)*(s + 1.58872258534575e-15 - 0.323790242740243*I)
p = -481858107122483/34956364583988030*(32*s^6 + 48*s^4 + 18*s^2 + 1)/pi + 421794849676389/2806321699992460*(8*s^4 + 8*s^2 + 1)/pi - 3217349902914616/4621771216286985*(2*s^2 + 1)/pi + 732820056818024/435368484578163/pi

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

def print_poly(SymbolicExpression_poly, ZZ_digits, RR_threshold):
    coeff = []
    for coefficient in SymbolicExpression_poly.coefficients():
        coeff.append((Integer(int(coefficient[0])),coefficient[1]) if frac(coefficient[0]) == 0 else (__crop(coefficient[0].n(digits=ZZ_digits),RR_threshold),coefficient[1]))
    return sum([a*s^b for a,b in coeff])

print(print_poly(e, 3, 10^-10))
print(print_poly(f, 4, 10^-10))
print(print_poly(p, 5, 10^-10))

Surely the code is not optimized and may contain errors, but it worked fine in my tests. So, I will accept my own answer :-/.

Thank you, @dan_fulea!

I ended by taking a different approach. I created two functions, one for cropping the real and/or imaginary parts that are below a threshold, as the Mathematica function, and another that apply this crop function to generate a new polynomial with coefficients approximated to a specified number of digits. This function also converts the real numbers with zero fractional part (e.g. 1.0000) to the equivalent integer, so that the coefficient of any term that is exactly 1, do not show.

e = 1/46656*(9*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)*(4*s^2 + 1) - 7*(4*(9*s^2 + 1)*(6*s + 1/tan(55/72)) + 5*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))))*(36*s^2 + 25) - 11/46656*(4*(4*(9*s^2 + 1)*(6*s + 1/tan(55/72)) + 5*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))*(9*s^2 + 4) + 9*(9*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)*(4*s^2 + 1) - 7*(4*(9*s^2 + 1)*(6*s + 1/tan(55/72)) + 5*(36*s^2 - 3*(6*s + 1/tan(55/72))/(2/tan(14/9) - 1/tan(55/72)) + 1)/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))))/(16/(7/(8/(5/tan(295/72) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))) - 7/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))))/(20/(27/(32/(35/(6/tan(5) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))) - 7/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))) - 9/(16/(7/(8/(5/tan(295/72) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72)))) - 7/(4/(5/(4/tan(29/9) - 1/tan(55/72)) - 1/(2/tan(14/9) - 1/tan(55/72))) - 5/(8/(3/tan(19/8) - 1/tan(55/72)) - 3/(2/tan(14/9) - 1/tan(55/72))))))
f = (s + 0.267263403945694 + 0.717946300381457*I)*(s + 0.267263403945694 - 0.717946300381457*I)*(s + 0.0662957565747883 + 0.945082838443603*I)*(s + 0.0662957565747883 - 0.945082838443603*I)*(s + 1.58872258534575e-15 + 0.323790242740243*I)*(s + 1.58872258534575e-15 - 0.323790242740243*I)
p = -481858107122483/34956364583988030*(32*s^6 + 48*s^4 + 18*s^2 + 1)/pi + 421794849676389/2806321699992460*(8*s^4 + 8*s^2 + 1)/pi - 3217349902914616/4621771216286985*(2*s^2 + 1)/pi + 732820056818024/435368484578163/pi

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

def print_poly(SymbolicExpression_poly, ZZ_digits, RR_threshold):
    coeff = []
    for coefficient in SymbolicExpression_poly.coefficients():
        coeff.append((Integer(int(coefficient[0])),coefficient[1]) if frac(coefficient[0]) == 0 else (__crop(coefficient[0].n(digits=ZZ_digits),RR_threshold),coefficient[1]))
    return sum([a*s^b for a,b in coeff])

print(print_poly(e, 3, 10^-10))
print(print_poly(f, 4, 10^-10))
print(print_poly(p, 5, 10^-10))

The output is

s^6 + 2.54*s^5 + 4.59*s^4 + 4.98*s^3 + 3.73*s^2 + 1.66*s + 0.364
s^6 + 0.6671*s^5 + 1.660*s^4 + 0.6275*s^3 + 0.6898*s^2 + 0.05846*s + 0.05523
-0.14041*s^6 + 0.17213*s^4 - 0.13941*s^2 + 0.35765

Surely the code is not optimized and may contain errors, but it worked fine in my tests. So, I will accept my own answer :-/.