for i in range(n+1):
    x_axis.append(x)
    val.append(f(x))
    print x, f(x)
    x=x+h

##plt.plot(x_axis,val)
##plt.show()
    
    

# now we use interval arithmetic
from sympy.mpmath import iv
print 'using 35 decimal places ...'
iv.dps = 35
iv_f = lambda x: 1/(iv.mpf(x))
a=-1;b=1;n=10;x=a
h=(b-a)/float(n)
x_axis=[]
val2=[]
for i in range(n+1):
    x_axis.append(x)
    #c=float(str(iv_f(x).mid).split()[0].replace('[','').replace(',',''))
    c=float(str(iv_f(x)).split()[0].replace('[','').replace(',',''))
    val2.append(c)
    print x, f(x)
    x=x+h

plt.plot(x_axis,val,'*',x_axis,val2,'^')
plt.show()
import sympy as sp
x,y = sp.var('x,y')
g = (sp.Rational(33375)/100 - x**2)*y**6 \
+ x**2*(11*x**2*y**2 - 121*y**4 - 2) \
+ sp.Rational(55)/10*y**8 \
+ sp.Rational(1)*x/(2*y);
a = 77617; b = 33096
print 'evaluating', g, 'at', (a,b)
e = sp.Subs(g,(x,y),(a,b)).doit()
e15 = e.evalf(15)
print 'exact value :', e, '~', e15
# now we use interval arithmetic
from sympy.mpmath import iv
print 'using 35 decimal places ...'
iv.dps = 35
iv_f = lambda x,y: (iv.mpf('333.75') \
- x**2)*y**6 \
+ x**2*(iv.mpf('11')*x**2*y**2 \
- iv.mpf('121')*y**4 - iv.mpf('2')) \
+ iv.mpf('5.5')*y**8 + x/(iv.mpf('2')*y);
iv_a = iv.mpf(str(a))
iv_b = iv.mpf(str(b))
iv_z = iv_f(iv_a,iv_b)
print iv_z
print 'using 36 decimal places ...'
iv.dps = 36
iv_f = lambda x,y: (iv.mpf('333.75') \
- x**2)*y**6 \
+ x**2*(iv.mpf('11')*x**2*y**2 \
- iv.mpf('121')*y**4 - iv.mpf('2')) \
+ iv.mpf('5.5')*y**8 + x/(iv.mpf('2')*y);
# L-25 MCS 507 Wed 24 Oct 2012 : usempmathiv.py

# Illustration of interval arithmetic in the mpmath package of SymPy

import sympy as sp
from sympy.mpmath import iv
iv.dps = 15
x = iv.mpf(3)
print x, 'has type', type(x)
y = iv.mpf([3,4])
z = x/y
print x, '/', y, '=', z
# 0.1 /= '0.1'
a = iv.mpf(0.1)
b = iv.mpf('0.1')
print 'Observe strings!'
print a
print b
print 'some properties of', b
print 'middle :', b.mid
print 'width :', b.delta
print 'left bound :', b.a
print 'right bound :', b.b
print 'internal representation of', b
print b.__dict__
fraction = b.__dict__['_mpi_'][0][1]
exponent = b.__dict__['_mpi_'][0][2]
print 'fraction =', fraction
print 'exponent =', exponent
lb = fraction*2.0**exponent
print lb
# L-25 MCS 507 Wed 24 Oct 2012 : intvalnewton.py

# First we show a naive use of interval arithmetic in Newton's method.
# with the mpmath package of SymPy.  Then we do it in proper manner.

import sympy as sp
from sympy.mpmath import iv
iv.dps = 15
print 'naive interval Newton :'
x = iv.mpf(['1.4','1.5'])
for i in xrange(5):
   x = x - (x**2 - 2)/(2*x)
   print x
print 'proper interval Newton :'
x = iv.mpf(['1.4','1.5'])
for i in xrange(5):
   x = x.mid
   x = x - (x**2 - 2)/(2*x)
   print x