First time here? Check out the FAQ!

Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

answered 5 years ago

dsejas gravatar image

Hello, @john-bao! You could change the polynomial ring of the Taylor Polynomial. For example,

f(x) = sin(x)
T(x) = taylor(f, x, 1.0, 2)
R = PolynomialRing(RealIntervalField(10), 'x')
R(T)

This will give you something like

 -0.421?*x^2 + 1.39?*x - 0.120?

The ? sign indicates you have a loss of precision. I am working with 10 bits, because 3 gives you very poor precision. If you don't like the ?s, you can change the RealIntervalField(10) with Reals(10), for example.

I hope this helps!

click to hide/show revision 2
No.2 Revision

Hello, @john-bao! You could change the polynomial ring of the Taylor Polynomial. For example,

f(x) = sin(x)
T(x) = taylor(f, x, 1.0, 2)
R = PolynomialRing(RealIntervalField(10), 'x')
R(T)

This will give you something like

 -0.421?*x^2 + 1.39?*x - 0.120?

The ? sign indicates you have a loss of precision. I am working with 10 bits, because 3 gives you very poor precision. If you don't like the ?s, you can change the RealIntervalField(10) with Reals(10), for example.

example. If you want to know how many bits you need in order to have 4 digits, for example, you can use the digits_to_bits function:

from sage.arith.numerical_approx import digits_to_bits
R = PolynomialRing(RealIntervalField(digits_to_bits(4)), 'x')
R(T)

Here is an alternative approach (although a little slower). This allows you to fix the number of bits or the number of digits (I'll use digits). Define the function:

def poly_prec(p, digits=4):
    pol = 0
    for op in p.operands():
        if not op.is_constant():
            ex = op.operands()
            pol += ex[0] * N(ex[1], digits)
        else:
            pol += N(op, digits)
    return pol

I hope this helps!