Skip to content

Commit

Permalink
Trac #27157: py3 fixes in interacts
Browse files Browse the repository at this point in the history
Together with #27117, this fixes all failures in `src/sage/interacts`

URL: https://trac.sagemath.org/27157
Reported by: jdemeyer
Ticket author(s): Jeroen Demeyer
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager authored and vbraun committed Feb 3, 2019
2 parents 3cd33bf + 1ba117f commit 6505206
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 37 deletions.
42 changes: 20 additions & 22 deletions src/sage/interacts/library.py
Expand Up @@ -35,6 +35,8 @@
# http://www.gnu.org/licenses/
#*****************************************************************************

from __future__ import absolute_import, division

from sage.all import *
x = SR.var('x')

Expand Down Expand Up @@ -446,12 +448,12 @@ def distance(x1_y1, x2_y2):
# are adjacent and the side c is opposite to the angle
def angle(a, b, c):
a,b,c = map(float,[a,b,c])
return acos((b**2 + c**2 - a**2)/(2.0*b*c))
return acos(0.5 * (b**2 + c**2 - a**2) / (b * c))

# Returns the area of a triangle when an angle alpha
# and adjacent sides a and b are known
def area(alpha, a, b):
return 1.0/2.0*a*b*sin(alpha)
return 0.5 * a * b * sin(alpha)

xy = [0]*3
html('<h2>Trigonometric Properties of a Triangle</h2>')
Expand Down Expand Up @@ -482,14 +484,10 @@ def area(alpha, a, b):
labels = a_label + b_label + c_label

show(unit_circle + triangle + triangle_points + labels, figsize=[5, 5], xmin=-1, xmax=1, ymin=-1, ymax=1)
angl_txt = r"$\angle A = {%s}^{\circ},$ $\angle B = {%s}^{\circ},$ $\angle C = {%s}^{\circ}$" % (
math.degrees(ak[0]),
math.degrees(ak[1]),
math.degrees(ak[2])
)
html(angl_txt)
html(r"$AB = %s,$ $BC = %s,$ $CA = %s$"%(al[2], al[0], al[1]))
html(r"Area of triangle $ABC = %s$"%A)
html(r"$\angle A = {%.3f}^{\circ},$ $\angle B = {%.3f}^{\circ},$ $\angle C = {%.3f}^{\circ}$"
% (math.degrees(ak[0]), math.degrees(ak[1]), math.degrees(ak[2])))
html(r"$AB = %.6f$, $BC = %.6f$, $CA = %.6f$" % (al[2], al[0], al[1]))
html(r"Area of triangle $ABC = %.6f$" % A)

@library_interact
def unit_circle(
Expand Down Expand Up @@ -617,9 +615,9 @@ def special_points(
# Return the intersection point of the bisector of the angle <(A[a],A[c],A[b]) and the unit circle. Angles given in radians.
def half(A, a, b, c):
if (A[a] < A[b] and (A[c] < A[a] or A[c] > A[b])) or (A[a] > A[b] and (A[c] > A[a] or A[c] < A[b])):
p = A[a] + (A[b] - A[a]) / 2.0
p = A[a] + 0.5 * (A[b] - A[a])
else:
p = A[b] + (2*pi - (A[b]-A[a])) / 2.0
p = A[b] + 0.5 * (2*pi - (A[b]-A[a]))
return (math.cos(p), math.sin(p))

# Returns the distance between points (x1,y1) and (x2,y2)
Expand Down Expand Up @@ -655,9 +653,9 @@ def line_to_points(x1_y1, x2_y2, **plot_kwargs):

# Midpoints of edges (bc, ca, ab)
a_middle = [
((xy[1][0] + xy[2][0])/2.0, (xy[1][1] + xy[2][1])/2.0),
((xy[2][0] + xy[0][0])/2.0, (xy[2][1] + xy[0][1])/2.0),
((xy[0][0] + xy[1][0])/2.0, (xy[0][1] + xy[1][1])/2.0)
(0.5 * (xy[1][0] + xy[2][0]), 0.5 * (xy[1][1] + xy[2][1])),
(0.5 * (xy[2][0] + xy[0][0]), 0.5 * (xy[2][1] + xy[0][1])),
(0.5 * (xy[0][0] + xy[1][0]), 0.5 * (xy[0][1] + xy[1][1]))
]

# Incircle
Expand All @@ -668,7 +666,7 @@ def line_to_points(x1_y1, x2_y2, **plot_kwargs):
)

if show_incircle:
s = perimeter/2.0
s = 0.5 * perimeter
incircle_r = math.sqrt((s - ad[0]) * (s - ad[1]) * (s - ad[2]) / s)
incircle_graph = circle(incircle_center, incircle_r) + point(incircle_center)
else:
Expand Down Expand Up @@ -980,7 +978,7 @@ def _newton_method(f, c, maxn, h):
f = symbolic_expression(f).function(x)
a, b = interval
h = 10**(-d)
c, midpoints = _newton_method(f, float(c), maxn, h/2.0)
c, midpoints = _newton_method(f, float(c), maxn, 0.5 * h)
html(r"$\text{Precision } 2h = %s$"%latex(float(h)))
html(r"${c = }%s$"%c)
html(r"${f(c) = }%s"%latex(f(c)))
Expand Down Expand Up @@ -1705,12 +1703,12 @@ def polar_prime_spiral(
t = SR.var('t')
a=1.0
b=0.0
if n > (floor(sqrt(n)))**2 and n <= (floor(sqrt(n)))**2 + floor(sqrt(n)):
c = -((floor(sqrt(n)))**2 - n)
c2= -((floor(sqrt(n)))**2 + floor(sqrt(n)) - n)
S = int(sqrt(n))
if n <= S * (S + 1):
c = n - S**2
else:
c = -((ceil(sqrt(n)))**2 - n)
c2= -((floor(sqrt(n)))**2 + floor(sqrt(n)) - n)
c = n - (S + 1)**2
c2 = n - S * (S + 1)
html('Pink Curve: $n^2 + %s$' % c)
html('Green Curve: $n^2 + n + %s$' % c2)
m = SR.var('m')
Expand Down
20 changes: 5 additions & 15 deletions src/sage/interacts/test_jupyter.rst
Expand Up @@ -47,8 +47,8 @@ Test all interacts from the Sage interact library::
dpi: IntSlider(value=100, description=u'dpi', max=300, min=10, step=10)
<h2>Polar Prime Spiral</h2> <div style="white-space: normal;"> For more information about the factors in the spiral, visit <a href="http://www.dcs.gla.ac.uk/~jhw/spirals/index.html" target="_blank"> Number Spirals by John Williamson</a>.</div>
<script type="math/tex">n = 89</script>
Pink Curve: <script type="math/tex">n^2 + 8.0</script>
Green Curve: <script type="math/tex">n^2 + n + -1.0</script>
Pink Curve: <script type="math/tex">n^2 + 8</script>
Green Curve: <script type="math/tex">n^2 + n + -1</script>

sage: test(interacts.calculus.taylor_polynomial)
Interactive function <function taylor_polynomial at ...> with 3 widgets
Expand Down Expand Up @@ -103,16 +103,6 @@ Test all interacts from the Sage interact library::
<script type="math/tex">Ax^2 + Bx + C = 0</script>
<script type="math/tex">x = \frac{-B\pm\sqrt{B^2-4AC}}{2A} = \frac{-1\pm\sqrt{1^2-4*1*-2}}{2*1} = \frac{-1\pm\sqrt{\color{Green}{9}}}{2} = \begin{cases}1\\-2\end{cases}</script>

sage: test(interacts.calculus.trigonometric_properties_triangle)
Interactive function <function trigonometric_properties_triangle at ...> with 3 widgets
a0: IntSlider(value=30, description=u'A', max=360)
a1: IntSlider(value=180, description=u'B', max=360)
a2: IntSlider(value=300, description=u'C', max=360)
<h2>Trigonometric Properties of a Triangle</h2>
<script type="math/tex">\angle A = {60.0}^{\circ},</script> <script type="math/tex">\angle B = {45.0}^{\circ},</script> <script type="math/tex">\angle C = {75.0}^{\circ}</script>
<script type="math/tex">AB = 1.93185165258,</script> <script type="math/tex">BC = 1.73205080757,</script> <script type="math/tex">CA = 1.41421356237</script>
Area of triangle <script type="math/tex">ABC = 1.18301270189</script>

sage: test(interacts.calculus.secant_method)
Interactive function <function secant_method at ...> with 5 widgets
title: HTMLText(value=u'<h2>Secant method for numerical root finding</h2>')
Expand Down Expand Up @@ -270,9 +260,9 @@ Test all interacts from the Sage interact library::
a1: IntSlider(value=180, description=u'B', max=360)
a2: IntSlider(value=300, description=u'C', max=360)
<h2>Trigonometric Properties of a Triangle</h2>
<script type="math/tex">\angle A = {60.0}^{\circ},</script> <script type="math/tex">\angle B = {45.0}^{\circ},</script> <script type="math/tex">\angle C = {75.0}^{\circ}</script>
<script type="math/tex">AB = 1.93185165258,</script> <script type="math/tex">BC = 1.73205080757,</script> <script type="math/tex">CA = 1.41421356237</script>
Area of triangle <script type="math/tex">ABC = 1.18301270189</script>
<script type="math/tex">\angle A = {60.000}^{\circ},</script> <script type="math/tex">\angle B = {45.000}^{\circ},</script> <script type="math/tex">\angle C = {75.000}^{\circ}</script>
<script type="math/tex">AB = 1.931852</script>, <script type="math/tex">BC = 1.732051</script>, <script type="math/tex">CA = 1.414214</script>
Area of triangle <script type="math/tex">ABC = 1.183013</script>

sage: test(interacts.geometry.special_points)
Interactive function <function special_points at ...> with 10 widgets
Expand Down

0 comments on commit 6505206

Please sign in to comment.