CoCalc
Shareddemo notebook.ipynbOpen in CoCalc
Description: a simuw scratch worksheet
License: Apache License 2.0
Authors: Kelly Emmrich, Shea Engle, James Fu, Tanvi Ganapathy, Chris Jeong, Ivan Karp, Steve Klee, smallp^2man Kuntz, Andrew Lebedinsky, Zehan Li, and 19 more authors...
Views : 13
var('x y') plot3d(sin(x) * cos(y), (x, -10, 10), (y, -10, 10))

Icosahedron

var('x y') plot3d(tan(1/x) * sin(y^2) - cos(x), (x, -10, 10), (y, -5, 5))
plot(sin(1/x)*cos(x^2), (x,0,10))
show(icosahedron(color='green', opacity=.5, mesh=3), spin=1)
plot(cos(x)*x^2, (x,-15,15))
def f(x): print("x squared is", x^2) f(10)

Icosahedron

show(icosahedron(color='green', opacity=.5, mesh=3), spin=1)
plot(x^3 * sin(x), (x,100,108))

Show a colored cube

thecube = cube( color=['red', 'blue', 'green'], frame_thickness=2, frame_color='brown', opacity=0.8 ) show(thecube, frame=False)
factor(2020)
2^2 * 5 * 101

Show a colored cube

thecube = cube( color=['red', 'blue', 'green'], frame_thickness=2, frame_color='brown', opacity=0.8 ) show(thecube, frame=False)

Torus

from sage.plot.plot3d.shapes import Torus inner_radius = .3; outer_radius = 1 show(Torus(outer_radius, inner_radius, color='orange'), aspect_ratio=1, spin=3)
plot(sin(x^2)*cos(x), (x,0,10))

Symbolic expressions are also Python objects. This means, there are ways to inspect and take them apart.

In this example here, we start with an equation and "zoom in" to a small part of it.

Tipp: Combine this technique with substitutions to build new expressions out of existing ones.

Transitions and a plot of a Finite State Machine

fsm = Automaton([(0, 0, 0), (0, 1, 1), (0, 0, 2), (1, 2, 2), (2, 0, 0)], initial_states=[0, 1, 2], final_states=[0, 1]) for transition in fsm.accessible_components().transitions(): print(transition) fsm.plot()
Transition from 0 to 0: 0|- Transition from 0 to 1: 1|- Transition from 0 to 0: 2|- Transition from 1 to 2: 2|- Transition from 2 to 0: 0|-

Show a colored cube

thecube = cube( color=['red', 'blue', 'green'], frame_thickness=2, frame_color='brown', opacity=0.8 ) show(thecube, frame=False)

SageMath's preparser makes it easy to define functions.

from sage.plot.plot3d.shapes import Torus inner_radius = .3; outer_radius = 1 show(Torus(outer_radius, inner_radius, color='blue'), aspect_ratio=1, spin=3)
plot(x^2)
plot(cos((1/2)*x)+sin(x), (x,0,10))
sage: from sage.symbolic.function_factory import deprecated_custom_evalf_wrapper as dcew sage: def old_func(x, prec=0): print("x: %s, prec: %s" % (x, prec)) sage: new_func = dcew(old_func) sage: new_func(5, parent=RR) x: 5, prec: 53 sage: new_func(0r, parent=ComplexField(100)) x: 0, prec: 100
G = Cylinder(1, .5) + Cylinder(.25, 3).translate(0, 0, -3)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-20-5b4a735ee13e> in <module>() ----> 1 G = Cylinder(Integer(1), RealNumber('.5')) + Cylinder(RealNumber('.25'), Integer(3)).translate(Integer(0), Integer(0), -Integer(3)) NameError: name 'Cylinder' is not defined
sum(Cone(.1, sin(n)).translate(n, sin(n), 0) for n in [0..10, step=.1])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-19-fc0dca25130a> in <module>() ----> 1 sum(Cone(RealNumber('.1'), sin(n)).translate(n, sin(n), Integer(0)) for n in (ellipsis_range(Integer(0),Ellipsis,Integer(10), step=RealNumber('.1')))) /ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/misc/functional.pyc in symbolic_sum(expression, *args, **kwds) 575 return expression.sum(*args, **kwds) 576 elif len(args) <= 1: --> 577 return sum(expression, *args) 578 else: 579 from sage.symbolic.ring import SR <ipython-input-19-fc0dca25130a> in <genexpr>((n,)) ----> 1 sum(Cone(RealNumber('.1'), sin(n)).translate(n, sin(n), Integer(0)) for n in (ellipsis_range(Integer(0),Ellipsis,Integer(10), step=RealNumber('.1')))) /ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/geometry/cone.pyc in Cone(rays, lattice, check, normalize) 427 # Cone from rays 428 if check or normalize: --> 429 rays = normalize_rays(rays, lattice) 430 if lattice is None: 431 if rays: /ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/geometry/cone.pyc in normalize_rays(rays, lattice) 643 raise TypeError( 644 "rays must be given as a list or a compatible structure!" --> 645 "\nGot: %s" % rays) 646 if rays: 647 if lattice is None: TypeError: rays must be given as a list or a compatible structure! Got: 0.100000000000000
sum(Cone(.1, sin(n), color='yellow').translate(n, sin(n), 0) for n in [0..10, step=.1])
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-18-be3c25c908c1> in <module>() ----> 1 sum(Cone(RealNumber('.1'), sin(n), color='yellow').translate(n, sin(n), Integer(0)) for n in (ellipsis_range(Integer(0),Ellipsis,Integer(10), step=RealNumber('.1')))) /ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/misc/functional.pyc in symbolic_sum(expression, *args, **kwds) 575 return expression.sum(*args, **kwds) 576 elif len(args) <= 1: --> 577 return sum(expression, *args) 578 else: 579 from sage.symbolic.ring import SR <ipython-input-18-be3c25c908c1> in <genexpr>((n,)) ----> 1 sum(Cone(RealNumber('.1'), sin(n), color='yellow').translate(n, sin(n), Integer(0)) for n in (ellipsis_range(Integer(0),Ellipsis,Integer(10), step=RealNumber('.1')))) TypeError: Cone() got an unexpected keyword argument 'color'
var('x') f(x) = 3 * x -1 print(f) g(x) = (1 - x)^2 print(g) # h is a composition of f and g h = f(g) print(h) # evaluate h print(h(x = 3)) print(h.subs(x = pi))
plot(1/x, (x,0,50))

Icosahedron

show(icosahedron(color='green', opacity=.5, mesh=3), spin=1)
plot(x^x, (x,0,10))

SageMath's preparser makes it easy to define functions.

plot(tan(x), (x,0,1000))
factor(1)

plot(sin(x)^(x^3),(x,-5,5))

factor(420420420)
factor(3)
3
plot(x, (x,0,10))
plot(2*x, (x,0,10))
plot(x, (x,0,10))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-26-d1d15f9d8156> in <module>() ----> 1 plot() /ext/sage/sage-8.7_1804/local/lib/python2.7/site-packages/sage/misc/decorators.pyc in wrapper(*args, **kwds) 490 options['__original_opts'] = kwds 491 options.update(kwds) --> 492 return func(*args, **options) 493 494 #Add the options specified by @options to the signature of the wrapped TypeError: plot() takes at least 1 argument (0 given)
plot(3*x, (0,10))

3D plot of f(x,y)=sin(x)cos(y)f(x,y) = \sin(x)\cos(y).

var('x y') plot3d(sin(x) * cos(y), (x, -10, 10), (y, -10, 10))