Ticket #9623: 9623-interact-examples2.patch

File 9623-interact-examples2.patch, 29.0 KB (added by robert.marik, 11 years ago)

apply after 9623-interact-examples.patch

  • sage/interacts/calculus.py

    # HG changeset patch
    # User Robert Marik <marik@mendelu.cz>
    # Date 1282047361 25200
    # Node ID a0f7631c1929c3ae35cd5f8d7f12ba6c2190d2be
    # Parent  510cda682570e93d08470ad221e57cc8b513b73d
    Trac #9623: Added titles to interacts, two new interacts (Riemann integral and bisection method), alternative input and output forms for simpson and trapezoidal rule and some minor improvements.
    
    diff -r 510cda682570 -r a0f7631c1929 sage/interacts/calculus.py
    a b  
    1414from library import taylor_polynomial
    1515from library import definite_integral
    1616from library import function_derivative, difference_quotient, quadratic_equation
    17 from library import practice_differentiating_polynomials, trigonometric_properties_triangle
    18 from library import secant_method, newton_method, trapezoid_integration, simpson_integration
     17from library import trigonometric_properties_triangle
     18from library import secant_method, newton_method, trapezoid_integration, simpson_integration, bisection_method
     19from library import riemann_sum
    1920from library import function_tool
  • sage/interacts/library.py

    diff -r 510cda682570 -r a0f7631c1929 sage/interacts/library.py
    a b  
    2121#                  http://www.gnu.org/licenses/
    2222#*****************************************************************************
    2323
    24 from sagenb.notebook.interact import interact, slider, range_slider, input_box, selector, checkbox
     24from sagenb.notebook.interact import interact, slider, range_slider, input_box, selector, checkbox, input_grid, text_control
    2525from sage.all import sin, cos, asin, acos, tan, sqrt, SR, exp, symbolic_expression, N, derivative, pi
    2626from sage.all import factor, srange, Integer, points
    27 from sage.all import point, line2d, text, Graphics, plot, point, html, show, latex, circle, line, parametric_plot
     27from sage.all import point, line2d, text, Graphics, plot, point, html, show, latex, circle, line, parametric_plot, polygon2d
    2828from sage.all import implicit_plot3d, plot3d, cube, integral_numerical, CDF, complex_plot, matrix_plot, disk
    2929
    3030from sage.ext.fast_callable import fast_callable
     
    7171
    7272x=SR.var('x')
    7373@library_interact
    74 def taylor_polynomial(f=input_box(sin(x)*exp(-x)), order=slider(range(1,13))):
     74def taylor_polynomial(   
     75    title = text_control('<h2>Taylor polynomial</h2>'),
     76    f=input_box(sin(x)*exp(-x),label="$f(x)=$"), order=slider(range(1,13))):
    7577    """
    7678    An interact which illustrates the Taylor polynomial approximation
    7779    of various orders around `x=0`.
     
    9597
    9698@library_interact
    9799def definite_integral(
    98     f = input_box(default = "3*x", label = 'f(x)'),
    99     g = input_box(default = "x^2", label = 'g(x)'),
     100    title = text_control('<h2>Definite integral</h2>'),
     101    f = input_box(default = "3*x", label = '$f(x)=$'),
     102    g = input_box(default = "x^2", label = '$g(x)=$'),
    100103    interval = range_slider(-10,10,default=(0,3), label="Interval"),
    101104    x_range = range_slider(-10,10,default=(0,3), label = "plot range (x)"),
    102105    selection = selector(["f", "g", "f and g", "f - g"], default="f and g", label="Select")):
     
    129132    # Color and calculate the area between f and the horizontal axis. 
    130133    if selection == "f" or selection == "f and g":
    131134        f_plot += plot(f(x), x, interval, color="blue", fill=True, fillcolor="blue", fillalpha=0.15)
    132         text += r"$\int_{%.2f}^{%.2f}(\color{Blue}{f(x)})\,dx=\int_{%.2f}^{%.2f}(%s)\,dx=%.2f$" % (
     135        text += r"$\int_{%.2f}^{%.2f}(\color{Blue}{f(x)})\,\mathrm{d}x=\int_{%.2f}^{%.2f}(%s)\,\mathrm{d}x=%.2f$" % (
    133136            interval[0], interval[1],
    134137            interval[0], interval[1],
    135138            latex(f(x)),
     
    143146    if selection == "g" or selection == "f and g":
    144147        g_plot = plot(g(x), x, x_range, color="green", thickness=1.5)
    145148        g_plot += plot(g(x), x, interval, color="green", fill=True, fillcolor="yellow", fillalpha=0.5)
    146         text += r"$\int_{%.2f}^{%.2f}(\color{Green}{g(x)})\,dx=\int_{%.2f}^{%.2f}(%s)\,dx=%.2f$" % (
     149        text += r"$\int_{%.2f}^{%.2f}(\color{Green}{g(x)})\,\mathrm{d}x=\int_{%.2f}^{%.2f}(%s)\,\mathrm{d}x=%.2f$" % (
    147150            interval[0], interval[1],
    148151            interval[0], interval[1],
    149152            latex(g(x)),
     
    155158        g_plot = plot(g(x), x, x_range, color="green", thickness=1.5)
    156159        g_plot += plot(g(x), x, interval, color="green", fill=f(x), fillcolor="red", fillalpha=0.15)
    157160        h_plot = plot(f(x)-g(x), x, interval, color="red", thickness=1.5, fill=True, fillcolor="red", fillalpha=0.15)
    158         text = r"$\int_{%.2f}^{%.2f}(\color{Red}{f(x)-g(x)})\,dx=\int_{%.2f}^{%.2f}(%s)\,dx=%.2f$" % (
     161        text = r"$\int_{%.2f}^{%.2f}(\color{Red}{f(x)-g(x)})\,\mathrm{d}x=\int_{%.2f}^{%.2f}(%s)\,\mathrm{d}x=%.2f$" % (
    159162            interval[0], interval[1],
    160163            interval[0], interval[1],
    161164            latex(f(x)-g(x)),
     
    167170
    168171@library_interact
    169172def function_derivative(
     173    title = text_control('<h2>Derivative grapher</h2>'),
    170174    function = input_box(default="x^5-3*x^3+1", label="Function:"),
    171175    x_range  = range_slider(-15,15,0.1, default=(-2,2), label="Range (x)"),
    172176    y_range  = range_slider(-15,15,0.1, default=(-8,6), label="Range (y)")):
     
    201205
    202206@library_interact
    203207def difference_quotient(
     208    title = text_control('<h2>Difference quotient</h2>'),
    204209    f = input_box(default="sin(x)", label='f(x)'),
    205210    interval= range_slider(0, 10, 0.1, default=(0.0,10.0), label="Range"),
    206211    a = slider(0, 10, None, 5.5, label = 'a'),
     
    304309           r"\frac{-%s\pm\sqrt{%s}}{%s} = %s$"
    305310    html(calc % (B, dis1, A, B, dis2, (2*A), sol))
    306311
    307 #R.<x> = ZZ[]
    308 new_ex_state = True
    309 prac_function = None
    310 
    311 @library_interact
    312 def practice_differentiating_polynomials(
    313     answer = input_box(label="Answer"),
    314     new_exercise = checkbox(default=False, label="New exercise"),
    315     show_solution = checkbox(default=False, label="Show solution")):
    316     """
    317     This is an interact for practicing differentiating random polynomials
    318     based on work by Lauri Ruotsalainen, 2010.
    319 
    320     INPUT:
    321 
    322         - ``answer`` -- enter your answer here
    323         - ``new_exercise`` -- generate a new exercise
    324         - ``show_solution`` -- show the solution
    325 
    326     EXAMPLES::
    327 
    328         sage: interacts.calculus.practice_differentiating_polynomials()
    329         <html>...</html>
    330     """
    331     import sage
    332     from sage.interacts.library import prac_function, new_ex_state
    333     from sage.all import ZZ
    334     R = ZZ['x']; (x,) = R._first_ngens(1)
    335     if prac_function is None:
    336         prac_function = R.random_element(3, -10,10)
    337         sage.interacts.library.prac_function = prac_function
    338 
    339     if answer == derivative(prac_function(x)) or new_exercise == new_ex_state:
    340         new_ex_state = not new_exercise
    341         sage.interacts.library.new_ex_state = new_ex_state
    342         prac_function = R.random_element(3, -10,10)
    343         sage.interacts.library.prac_function = prac_function
    344 
    345     if answer == derivative(prac_function(x)):
    346         html(r"$\text{Very good! New exercise:}$")
    347         prac_function = R.random_element(3, -10,10)
    348         sage.interacts.library.prac_function = prac_function
    349 
    350     elif answer and show_solution is False and new_exercise != new_ex_state:
    351         html(r"$\text{You've made an error. Try again!}$")
    352 
    353     html(r"$\text{Calculate the derivative: }\;\;\;\;%s$<br>"%latex(prac_function(x)))
    354 
    355     if show_solution:
    356         html(r"$\text{Solution: }\;\;\;\;%s$<br>"%latex(derivative(prac_function(x))))
    357 
    358 
    359312@library_interact
    360313def trigonometric_properties_triangle(
    361314    a0 = slider(0, 360, 1, 30, label="A"),
     
    531484
    532485@library_interact
    533486def special_points(
     487    title = text_control('<h2>Special points in triangle</h2>'),
    534488    a0 = slider(0, 360, 1, 30, label="A"),
    535489    a1 = slider(0, 360, 1, 180, label="B"),
    536490    a2 = slider(0, 360, 1, 300, label="C"),
     
    714668
    715669
    716670@library_interact
     671def bisection_method(
     672    title = text_control('<h2>Bisection method</h2>'),
     673    f = input_box("x^2-2", label='f(x)'),
     674    interval = range_slider(-5,5,default=(0, 4), label="range"),
     675    d = slider(1, 8, 1, 3, label="10^-d precision"),
     676    maxn = slider(0,50,1,10, label="max iterations")):
     677    """
     678    Interact explaining the bisection method, based on similar interact
     679    explaining secant method and Wiliam Stein's example from wiki.
     680
     681    INPUT:
     682
     683      - ``f`` -- function
     684      - ``interval`` -- range slider for the search interval
     685      - ``d`` -- slider for the precision (10^-d)
     686      - ``maxn`` -- max number of iterations
     687
     688    EXAMPLES::
     689
     690        sage: interacts.calculus.secant_method()
     691        <html>...</html>
     692    """
     693    def _bisection_method(f, a, b, maxn, eps):
     694        intervals = [(a,b)]
     695        round = 1
     696        two = float(2)
     697        while True:
     698            c = (b+a)/two
     699            if abs(f(c)) < h or round >= maxn:
     700                break
     701            fa = f(a); fb = f(b); fc = f(c)
     702            if abs(fc) < eps:
     703                return c, intervals
     704            if fa*fc < 0:
     705                a, b = a, c
     706            elif fc*fb < 0:
     707                a, b = c, b
     708            else:
     709                raise ValueError, "f must have a sign change in the interval (%s,%s)"%(a,b)
     710            intervals.append((a,b))
     711            round += 1
     712        return c, intervals
     713
     714    x = SR.var('x')
     715    f = symbolic_expression(f).function(x)
     716    a, b = interval
     717    h = 10**(-d)
     718    try:
     719        c, intervals = _bisection_method(f, float(a), float(b), maxn, h)
     720    except ValueError:
     721        print "f must have opposite sign at the endpoints of the interval"
     722        show(plot(f, a, b, color='red'), xmin=a, xmax=b)
     723    else:
     724        html(r"$\text{Precision }h = 10^{-d}=10^{-%s}=%.5f$"%(d, float(h)))
     725        html(r"${c = }%s$"%latex(c))
     726        html(r"${f(c) = }%s"%latex(f(c)))
     727        html(r"$%s \text{ iterations}"%len(intervals))
     728        P = plot(f, a, b, color='red')
     729        k = (P.ymax() - P.ymin())/ (1.5*len(intervals))
     730        L = sum(line([(c,k*i), (d,k*i)]) for i, (c,d) in enumerate(intervals) )
     731        L += sum(line([(c,k*i-k/4), (c,k*i+k/4)]) for i, (c,d) in enumerate(intervals) )
     732        L += sum(line([(d,k*i-k/4), (d,k*i+k/4)]) for i, (c,d) in enumerate(intervals) )
     733        show(P + L, xmin=a, xmax=b)
     734
     735@library_interact
    717736def secant_method(
     737    title = text_control('<h2>Secant method</h2>'),
    718738    f = input_box("x^2-2", label='f(x)'),
    719739    interval = range_slider(-5,5,default=(0, 4), label="range"),
    720740    d = slider(1, 16, 1, 3, label="10^-d precision"),
     
    728748
    729749      - ``f`` -- function
    730750      - ``interval`` -- range slider for the search interval
    731       - ``slider`` -- slider for the precision (10^-d)
     751      - ``d`` -- slider for the precision (10^-d)
    732752      - ``maxn`` -- max number of iterations
    733753
    734754    EXAMPLES::
     
    752772    f = symbolic_expression(f).function(x)
    753773    a, b = interval
    754774    h = 10**(-d)
    755     c, intervals = _secant_method(f, float(a), float(b), maxn, h)
    756     html(r"$\text{Precision h =} 10^{-d}=10^{-%s}=%.5f$"%(d, float(h)))
    757     html(r"$\text{c = }%s$"%c)
    758     html(r"$\text{f(c) = }%s"%f(c))
    759     html(r"$\text{Iterations = }%s"%len(intervals))
    760     P = plot(f, a, b, color='red')
    761     k = (P.ymax() - P.ymin())/ (1.5*len(intervals))
    762     L = sum(line([(c,k*i), (d,k*i)]) for i, (c,d) in enumerate(intervals) )
    763     L += sum(line([(c,k*i-k/4), (c,k*i+k/4)]) for i, (c,d) in enumerate(intervals) )
    764     L += sum(line([(d,k*i-k/4), (d,k*i+k/4)]) for i, (c,d) in enumerate(intervals) )
    765     S = sum(line([(c,f(c)), (d,f(d)), (d-(d-c)*f(d)/(f(d)-f(c)), 0)], color="green") for  (c,d) in intervals)
    766     show(P + L + S, xmin=a, xmax=b)
     775    if float(f(a)*f(b)) > 0:
     776        print "f must have opposite sign at the endpoints of the interval"
     777        show(plot(f, a, b, color='red'), xmin=a, xmax=b)
     778    else:
     779        c, intervals = _secant_method(f, float(a), float(b), maxn, h)
     780        html(r"$\text{Precision }h = 10^{-d}=10^{-%s}=%.5f$"%(d, float(h)))
     781        html(r"${c = }%s$"%latex(c))
     782        html(r"${f(c) = }%s"%latex(f(c)))
     783        html(r"$%s \text{ iterations}"%len(intervals))
     784        P = plot(f, a, b, color='red')
     785        k = (P.ymax() - P.ymin())/ (1.5*len(intervals))
     786        L = sum(line([(c,k*i), (d,k*i)]) for i, (c,d) in enumerate(intervals) )
     787        L += sum(line([(c,k*i-k/4), (c,k*i+k/4)]) for i, (c,d) in enumerate(intervals) )
     788        L += sum(line([(d,k*i-k/4), (d,k*i+k/4)]) for i, (c,d) in enumerate(intervals) )
     789        S = sum(line([(c,f(c)), (d,f(d)), (d-(d-c)*f(d)/(f(d)-f(c)), 0)], color="green") for  (c,d) in intervals)
     790        show(P + L + S, xmin=a, xmax=b)
    767791
    768792@library_interact
    769793def newton_method(
     794    title = text_control('<h2>Newton method</h2>'),
    770795    f = input_box("x^2 - 2"),
    771796    c = slider(-10,10, default=6, label='Start (x)'),
    772     hh = slider(-16, -1, 1, -3, label="Precision 2h"),
     797    d = slider(1, 16, 1, 3, label="10^-d precision"),
    773798    maxn = slider(0, 15, 1, 10, label="max iterations"),
    774799    interval = range_slider(-10,10, default = (0,6), label="Interval"),
    775800    list_steps = checkbox(default=False, label="List steps")):
     
    782807
    783808      - ``f`` -- function
    784809      - ``c`` -- starting position (x)
     810      - ``d`` -- slider for the precision (10^-d)
     811      - ``maxn`` -- max number of iterations
    785812      - ``interval`` -- range slider for the search interval
    786       - ``slider`` -- slider for the precision (10^-d)
    787       - ``maxn`` -- max number of iterations
    788813      - ``list_steps`` -- checkbox, if true shows the steps numerically
    789814
    790815    EXAMPLES::
     
    806831    x = SR.var('x')
    807832    f = symbolic_expression(f).function(x)
    808833    a, b = interval
    809     h = 10**hh
     834    h = 10**(-d)
    810835    c, midpoints = _newton_method(f, float(c), maxn, h/2.0)
    811     html(r"$\text{Precision 2h = }%.5f$"%float(h))
    812     html(r"$\text{c = }%s$"%c)
    813     html(r"$\text{f(c) = }%s"%f(c))
    814     html(r"$\text{Iterations = }%s"%len(midpoints))
     836    html(r"$\text{Precision } 2h = %s$"%latex(float(h)))
     837    html(r"${c = }%s$"%c)
     838    html(r"${f(c) = }%s"%latex(f(c)))
     839    html(r"$%s \text{ iterations}"%len(midpoints))
    815840    if list_steps:
    816         s = "<br><br><table border=1 cellpadding=5>"
    817         s += "<tr><td>$n$</td><td>$x_n$</td><td>$f(x_n)$</td><td>$f(x_n-h)f(x_n+h)$</td></tr>"
     841        s = [["$n$","$x_n$","$f(x_n)$", "$f(x_n-h)\,f(x_n+h)$"]]
    818842        for i, c in enumerate(midpoints):
    819             s += "<tr><td>$%d$</td><td>$%.4f$</td><td>$%.4f$</td><td>$%.4f$</td></tr>"%(i, c, f(c), f(c-h)*f(c+h))
    820         s += "</table>"
    821         html(s)
     843            s.append([i+1, c, f(c), (c-h)*f(c+h)])
     844        html.table(s,header=True)
    822845    else:
    823846        P = plot(f, x, interval, color="blue")
    824847        L = sum(line([(c, 0), (c, f(c))], color="green") for c in midpoints[:-1])
     
    828851
    829852@library_interact
    830853def trapezoid_integration(
    831     f = input_box(default = "x^2-5*x + 10", label='f(x)'),
     854    title = text_control('<h2>Trapeziod integration</h2>'),
     855    f = input_box(default = "x^2-5*x + 10", label='$f(x)=$'),
    832856    n = slider(1,100,1,5, label='# divisions'),
    833     interval = range_slider(-10,10,default=(0,8), label="Integral interval")
     857    interval_input = selector(['from slider','from keyboard'], label='Integration interval', buttons=True),
     858    interval_s = range_slider(-10,10,default=(0,8), label="slider: "),
     859    interval_g = input_grid(1,2,default=[[0,8]], label="keyboard: "),
     860    output_form = selector(['traditional','table','none'], label='Computations form', buttons=True)
    834861    ):
    835862    """
    836863    Interact explaining the trapezoid method for definite integrals, based on work by
     
    841868   
    842869      - ``f`` -- function of variable x to integrate
    843870      - ``n`` -- number of divisions
    844       - ``interval`` -- interval to integrate
     871      - ``interval_input`` -- swithes the input for interval between slider and keyboard
     872      - ``interval_s`` -- slider for interval to integrate
     873      - ``interval_g`` -- input grid for interval to integrate
     874      - ``output_form`` -- the computation is formatted in a traditional form, in a table or missing
    845875
    846876    EXAMPLES::
    847877
     
    850880    """
    851881    xs = []
    852882    ys = []
    853     h = (interval[1]-interval[0])/n
     883    if interval_input == 'from slider':
     884        interval = interval_s
     885    else:
     886        interval = interval_g[0]
     887    h = float(interval[1]-interval[0])/n
    854888    x = SR.var('x')
    855889    f = symbolic_expression(f).function(x)
    856890
     
    865899    xs.append(xi + h)
    866900    ys.append(f(xi + h))
    867901
     902    html(r'Function $f(x)=%s$'%latex(f(x)))
    868903    show(plot(f, interval[0], interval[1]) + trapezoids, xmin = interval[0], xmax = interval[1])
    869904   
    870905    numeric_value = integral_numerical(f, interval[0], interval[1])[0]
    871906    approx = h *(ys[0]/2 + sum([ys[i] for i in range(1,n)]) + ys[n]/2)
    872    
    873     sum_formula_html = r"d \cdot \left[\frac{f(x_0)}{2} + %s + \frac{f(x_{%s})}{2}\right]" % (
    874         ' + '.join([ "f(x_{%s})"%i for i in range(1,n)]),
    875         n
    876     )
    877     sum_placement_html = r"%.2f \cdot \left[\frac{f(%.2f)}{2} + %s + \frac{f(%.2f)}{2}\right]" % (
    878         h,
    879         N(xs[0], digits=5),
    880         ' + '.join([ "f(%.2f)" %N(i, digits=5) for i in xs[1:-1]]),
    881         N(xs[n], digits=5)
    882     )
    883     sum_values_html = r"%.2f \cdot \left[\frac{%.2f}{2} + %s + \frac{%.2f}{2}\right]" % (
    884         h,
    885         N(ys[0], digits=5),
    886         ' + '.join([ "%.2f" % N(i, digits=5) for i in ys[1:-1]]),
    887         N(ys[n], digits=5)
    888     )
    889907
    890     html(r'''
    891         <div class="math">
    892         \begin{align*}
    893         \int_{%.2f}^{%.2f} {f(x) \, dx} & = %.6f \\\
    894         \\
    895        \int_{%.2f}^{%.2f} {f(x) \, dx}
    896             & \approx %s \\
    897             & = %s \\
    898             & = %s \\
    899             & = %s
    900         \end{align*}
    901         </div>
    902     ''' % (
    903         interval[0], interval[1],
    904         N(numeric_value, digits=7),
    905         interval[0], interval[1],
    906         sum_formula_html, sum_placement_html, sum_values_html,
    907         N(approx, digits=7)
    908     ))
     908    html(r'$\displaystyle\int_{%.2f}^{%.2f} {f(x) \, \mathrm{d}x} = %.6f$'%(           
     909            interval[0], interval[1], N(numeric_value, digits=7))
     910         )
    909911
     912    if output_form == 'traditional':
     913        sum_formula_html = r"\frac {d}{2} \cdot \left[f(x_0) + %s + f(x_{%s})\right]" % (
     914            ' + '.join([ "2 f(x_{%s})"%i for i in range(1,n)]),
     915            n
     916            )
     917        sum_placement_html = r"\frac{%.2f}{2} \cdot \left[f(%.2f) + %s + f(%.2f)\right]" % (
     918            h,
     919            N(xs[0], digits=5),
     920            ' + '.join([ "2 f(%.2f)" %N(i, digits=5) for i in xs[1:-1]]),
     921            N(xs[n], digits=5)
     922            )
     923        sum_values_html = r"\frac{%.2f}{2} \cdot \left[%.2f + %s + %.2f\right]" % (
     924            h,
     925            N(ys[0], digits=5),
     926            ' + '.join([ "2\cdot %.2f" % N(i, digits=5) for i in ys[1:-1]]),
     927            N(ys[n], digits=5)
     928            )
     929
     930        html(r'''
     931            <div class="math">
     932            \begin{align*}
     933            \int_{%.2f}^{%.2f} {f(x) \, \mathrm{d}x}
     934                & \approx %s \\
     935                & = %s \\
     936                & = %s \\
     937                & = %s
     938            \end{align*}
     939            </div>
     940        ''' % (
     941                interval[0], interval[1],
     942                sum_formula_html, sum_placement_html, sum_values_html,
     943                N(approx, digits=7)
     944        ))
     945    elif output_form == 'table':
     946        s = [['$i$','$x_i$','$f(x_i)$','$m$','$m\cdot f(x_i)$']]
     947        for i in range(0,n+1):
     948            if i==0 or i==n:
     949                j = 1
     950            else:
     951                j = 2
     952            s.append([i, xs[i], ys[i],j,N(j*ys[i])])
     953        html.table(s,header=True)
    910954
    911955@library_interact
    912956def simpson_integration(
    913     f = input_box(default = 'x*sin(x)+x+1', label='f(x)'),
    914     n=slider(2,100,2,6, label='# divisions'),
    915     interval=range_slider(-10,10,default=(0,10), label="Integration Interval")):
     957    title = text_control('<h2>Simpson integration</h2>'),
     958    f = input_box(default = 'x*sin(x)+x+1', label='$f(x)=$'),
     959    n = slider(2,100,2,6, label='# divisions'),
     960    interval_input = selector(['from slider','from keyboard'], label='Integration interval', buttons=True),
     961    interval_s = range_slider(-10,10,default=(0,10), label="slider: "),
     962    interval_g = input_grid(1,2,default=[[0,10]], label="keyboard: "),
     963    output_form = selector(['traditional','table','none'], label='Computations form', buttons=True)):
    916964    """
    917965    Interact explaining the simpson method for definite integrals, based on work by
    918966    Lauri Ruotsalainen, 2010 (based on the application "Numerical integrals with various rules"
     
    922970   
    923971      - ``f`` -- function of variable x to integrate
    924972      - ``n`` -- number of divisions (mult. of 2)
    925       - ``interval`` -- interval to integrate
     973      - ``interval_input`` -- swithes the input for interval between slider and keyboard
     974      - ``interval_s`` -- slider for interval to integrate
     975      - ``interval_g`` -- input grid for interval to integrate
     976      - ``output_form`` -- the computation is formatted in a traditional form, in a table or missing
    926977
    927978    EXAMPLES::
    928979
     
    931982    """
    932983    x = SR.var('x')
    933984    f = symbolic_expression(f).function(x)
    934 
     985    if interval_input == 'from slider':
     986        interval = interval_s
     987    else:
     988        interval = interval_g[0]
    935989    def parabola(a, b, c):
    936990        from sage.all import solve
    937991        A, B, C = SR.var("A, B, C")
     
    939993        f = K[A]*x**2+K[B]*x+K[C]
    940994        return f
    941995    xs = []; ys = []
    942     dx = (interval[1]-interval[0])/n
     996    dx = float(interval[1]-interval[0])/n
    943997   
    944998    for i in range(n+1):
    945999        xs.append(interval[0] + i*dx)
    946         ys.append(f(xs[-1]))
     1000        ys.append(f(x=xs[-1]))
    9471001
    9481002    parabolas = Graphics()
    9491003    lines = Graphics()
    9501004   
    9511005    for i in range(0, n-1, 2):
    9521006        p = parabola((xs[i],ys[i]),(xs[i+1],ys[i+1]),(xs[i+2],ys[i+2]))
    953         parabolas += plot(p(x), x, xmin=xs[i], xmax=xs[i+2], color="red")
     1007        parabolas += plot(p(x=x), (x, xs[i], xs[i+2]), color="red")
    9541008        lines += line([(xs[i],ys[i]), (xs[i],0), (xs[i+2],0)],color="red")
    9551009        lines += line([(xs[i+1],ys[i+1]), (xs[i+1],0)], linestyle="-.", color="red")
    9561010       
    9571011    lines += line([(xs[-1],ys[-1]), (xs[-1],0)], color="red")
    9581012   
     1013    html(r'Function $f(x)=%s$'%latex(f(x)))
     1014
    9591015    show(plot(f(x),x,interval[0],interval[1]) + parabolas + lines, xmin = interval[0], xmax = interval[1])
    9601016   
    9611017    numeric_value = integral_numerical(f,interval[0],interval[1])[0]
    9621018    approx = dx/3 *(ys[0] + sum([4*ys[i] for i in range(1,n,2)]) + sum([2*ys[i] for i in range(2,n,2)]) + ys[n])
    9631019   
    964     sum_formula_html = r"\frac{d}{3} \cdot \left[ f(x_0) + %s + f(x_{%s})\right]" % (
    965         ' + '.join([ r"%s \cdot f(x_{%s})" %(i%2*(-2)+4, i+1) for i in range(0,n-1)]),
    966         n
    967     )
     1020    html(r'$\displaystyle\int_{%.2f}^{%.2f} {f(x) \, \mathrm{d}x} = %.6f$'%
     1021         (interval[0],interval[1],
     1022         N(numeric_value,digits=7)))
     1023
     1024    if output_form == 'traditional':
     1025        sum_formula_html = r"\frac{d}{3} \cdot \left[ f(x_0) + %s + f(x_{%s})\right]" % (
     1026            ' + '.join([ r"%s \cdot f(x_{%s})" %(i%2*(-2)+4, i+1) for i in range(0,n-1)]),
     1027            n
     1028            )
    9681029   
    969     sum_placement_html = r"\frac{%.2f}{3} \cdot \left[ f(%.2f) +  %s + f(%.2f)\right]" % (
    970         dx,
    971         N(xs[0],digits=5),
    972         ' + '.join([ r"%s \cdot f(%.2f)" %(i%2*(-2)+4, N(xk, digits=5)) for i, xk in enumerate(xs[1:-1])]),
    973         N(xs[n],digits=5)
    974     )
     1030        sum_placement_html = r"\frac{%.2f}{3} \cdot \left[ f(%.2f) +  %s + f(%.2f)\right]" % (
     1031            dx,
     1032            N(xs[0],digits=5),
     1033            ' + '.join([ r"%s \cdot f(%.2f)" %(i%2*(-2)+4, N(xk, digits=5)) for i, xk in enumerate(xs[1:-1])]),
     1034            N(xs[n],digits=5)
     1035            )
    9751036   
    976     sum_values_html = r"\frac{%.2f}{3} \cdot \left[ %s %s %s\right]" %(
    977         dx,
    978         "%.2f + "%N(ys[0],digits=5),
    979         ' + '.join([ r"%s \cdot %.2f" %(i%2*(-2)+4, N(yk, digits=5)) for i, yk in enumerate(ys[1:-1])]),
    980         " + %.2f"%N(ys[n],digits=5)
    981     )
     1037        sum_values_html = r"\frac{%.2f}{3} \cdot \left[ %s %s %s\right]" %(
     1038            dx,
     1039            "%.2f + "%N(ys[0],digits=5),
     1040            ' + '.join([ r"%s \cdot %.2f" %(i%2*(-2)+4, N(yk, digits=5)) for i, yk in enumerate(ys[1:-1])]),
     1041            " + %.2f"%N(ys[n],digits=5)
     1042            )
     1043       
     1044        html(r'''
     1045        <div class="math">
     1046        \begin{align*}
     1047        \int_{%.2f}^{%.2f} {f(x) \, \mathrm{d}x}
     1048            & \approx %s \\
     1049            & = %s \\
     1050            & = %s \\
     1051            & = %.6f
     1052        \end{align*}
     1053        </div>
     1054        ''' % (
     1055                interval[0], interval[1],
     1056                sum_formula_html, sum_placement_html, sum_values_html,
     1057                N(approx,digits=7)
     1058                ))
     1059    elif output_form == 'table':
     1060        s = [['$i$','$x_i$','$f(x_i)$','$m$','$m\cdot f(x_i)$']]
     1061        for i in range(0,n+1):
     1062            if i==0 or i==n:
     1063                j = 1
     1064            else:
     1065                j = (i+1)%2*(-2)+4
     1066            s.append([i, xs[i], ys[i],j,N(j*ys[i])])
     1067        s.append(['','','','$\sum$','$%s$'%latex(3/dx*approx)])
     1068        html.table(s,header=True)
     1069        html(r'$\int_{%.2f}^{%.2f} {f(x) \, \mathrm{d}x}\approx\frac {%.2f}{3}\cdot %s=%s$'%
     1070             (interval[0], interval[1],dx,latex(3/dx*approx),latex(approx)))
     1071       
     1072@library_interact
     1073def riemann_sum(
     1074    title = text_control('<h2>Riemann integral</h2>'),
     1075    f = input_box("x^2+1", label = "$f(x)=$", width=40),
     1076    n = slider(1,30,1,5, label='# divisions'),
     1077    hr1 = text_control('<hr>'),
     1078    interval_input = selector(['from slider','from keyboard'], label='Integration interval', buttons=True),
     1079    interval_s = range_slider(-5,10,default=(0,2), label="slider: "),
     1080    interval_g = input_grid(1,2,default=[[0,2]], label="keyboard: "),
     1081    hr2 = text_control('<hr>'),
     1082    list_table = checkbox(default=False, label="List table"),   
     1083    auto_update = False):
     1084    """
     1085    Interact explaining the definition of Riemann integral
    9821086
    983     html(r'''
    984     <div class="math">
    985     \begin{align*}
    986     \int_{%.2f}^{%.2f} {f(x) \, dx} & = %.6f \\
    987     \\
    988     \int_{%.2f}^{%.2f} {f(x) \, dx}
    989         & \approx %s \\
    990         & = %s \\
    991         & = %s \\
    992         & = %.6f
    993     \end{align*}
    994     </div>
    995     ''' % (
    996         interval[0],interval[1],
    997         N(numeric_value,digits=7),
    998         interval[0], interval[1],
    999         sum_formula_html, sum_placement_html, sum_values_html,
    1000         N(approx,digits=7)
    1001     ))
     1087    INPUT:
     1088   
     1089    - ``f`` -- function of variable x to integrate
     1090    - ``n`` -- number of divisions
     1091      - ``interval_input`` -- swithes the input for interval between slider and keyboard
     1092      - ``interval_s`` -- slider for interval to integrate
     1093      - ``interval_g`` -- input grid for interval to integrate
     1094    - ``list_table`` -- print table with values of the function
     1095
     1096    EXAMPLES::
     1097
     1098        sage: interacts.calculus.riemann_sum()
     1099        <html>...</html>
     1100
     1101    AUTHORS:
     1102
     1103    - Robert Marik (08-2010)
     1104    """
     1105    x = SR.var('x')
     1106    from random import random
     1107    if interval_input == 'from slider':
     1108        a = interval_s[0]
     1109        b = interval_s[1]
     1110    else:
     1111        a = interval_g[0][0]
     1112        b = interval_g[0][1]
     1113    func = symbolic_expression(f).function(x)
     1114    division = [a]+[a+random()*(b-a) for i in range(n-1)]+[b]
     1115    division = [i for i in division]
     1116    division.sort()
     1117    xs = [division[i]+random()*(division[i+1]-division[i]) for i in range(n)]
     1118    ys = [func(x_val) for x_val in xs]
     1119    rects = Graphics()
     1120    for i in range(n):
     1121        body=[[division[i],0],[division[i],ys[i]],[division[i+1],ys[i]],[division[i+1],0]]
     1122        if ys[i].n()>0:
     1123            color_rect='green'
     1124        else:
     1125            color_rect='red'
     1126        rects = rects +polygon2d(body, rgbcolor = color_rect,alpha=0.1)\
     1127         + point((xs[i],ys[i]), rgbcolor = (1,0,0))\
     1128         + line(body,rgbcolor='black',zorder=-1)
     1129    html('<small>Adjust your data and click Update button. Click repeatedly for another random values.</small>')
     1130   
     1131    show(plot(func(x),(x,a,b),zorder=5) + rects)
     1132    delka_intervalu=[division[i+1]-division[i] for i in range(n)]
     1133    if list_table:
     1134        html.table([["$i$","$[x_{i-1},x_i]$","$\eta_i$","$f(\eta_i)$","$x_{i}-x_{i-1}$"]]\
     1135        +[[i+1,[division[i],division[i+1]],xs[i],ys[i],delka_intervalu[i]] for i in range(n)],\
     1136        header=True)
     1137
     1138    html('Riemann sum: $\displaystyle\sum_{i=1}^{%s} f(\eta_i)(x_i-x_{i-1})=%s$ '%
     1139         (latex(n),latex(sum([ys[i]*delka_intervalu[i] for i in range(n)]))))       
     1140    html('Exact value of the integral $\displaystyle\int_{%s}^{%s}%s\,\mathrm{d}x=%s$'%
     1141         (latex(a),latex(b),latex(func(x)),latex(integral_numerical(func(x),a,b)[0])))
     1142
    10021143
    10031144x = SR.var('x')
    10041145@library_interact
     
    10441185        lbl = 'f'
    10451186    elif action == 'df/dx':
    10461187        h = f.derivative(x)
    1047         lbl = r'\frac{df}{dx}'
     1188        lbl = r'\frac{\mathrm{d}f}{\mathrm{d}x}'
    10481189    elif action == 'int f':
    10491190        h = f.integrate(x)
    1050         lbl = r'\int f dx'
     1191        lbl = r'\int f \,\mathrm{d}x'
    10511192    elif action == 'num f':
    10521193        h = f.numerator()
    10531194        lbl = r'\text{numer(f)}'