{{{id=9| from sage.misc.citation import get_systems get_systems('plot(x^2,0,1)') /// ['MPFI', 'MPFR', 'ginac'] }}} {{{id=10| R = RealField(30*log(10,2)) pi30 = R(pi) strpi30 = str(pi30) print '%d-digit approximation for pi : %s' % (len(strpi30),strpi30) print type(pi) print type(pi30) tan(pi30), tan(pi) /// 30-digit approximation for pi : 3.1415926535897932384626433833 (-1.6956855320737799287917402939e-31, 0) }}} {{{id=46| #RDF RealDoubleField -- double precision floating numbers of the processor #RR RealField() -- Floating number with arbitrary (still fixed) many bits of precision mprf print RDF(1/10)*10 == RDF(1) print RDF(1/10)*10 - RDF(1) /// True 0.0 }}} {{{id=14| timeit("(RDF(2)^(1/10))^10 == 2") timeit("(RR(2)^(1/10))^10 == 2") /// Traceback (most recent call last): File "", line 1, in File "_sage_input_9.py", line 10, in exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dGltZWl0KCIoUkRGKDIpXigxLzEwKSleMTAgPT0gMiIpCgp0aW1laXQoIihSUigyKV4oMS8xMCkpXjEwID09IDIiKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in File "/tmp/u/husekr/tmprajCJW/___code___.py", line 2, in timeit("(RDF(2)^(1/10))^10 == 2") File "sage/misc/sage_timeit_class.pyx", line 118, in sage.misc.sage_timeit_class.SageTimeit.__call__ (/var/tmp/portage/sci-mathematics/sage-7.4-r2/work/sage-7.4/src-python2_7/build/cythonized/sage/misc/sage_timeit_class.c:1242) File "sage/misc/sage_timeit_class.pyx", line 82, in sage.misc.sage_timeit_class.SageTimeit.eval (/var/tmp/portage/sci-mathematics/sage-7.4-r2/work/sage-7.4/src-python2_7/build/cythonized/sage/misc/sage_timeit_class.c:1030) File "/usr/lib64/python2.7/site-packages/sage/misc/sage_timeit.py", line 221, in sage_timeit 'setup': "pass"} KeyError: 'init' }}} {{{id=15| a=RLF(sqrt(3)+pi) print a, type(a) print RealField(2000)(a) a.n(digits=400) /// 4.87364346115867? 4.873643461158670531990089724785375251139974653185486449030751571759749423195009035709181012099365643657774227928689009677063789704549534710537014920222854765978377334604244103173534464724033629853646979741154242569256332779147902902708621136498249040806016428511479398596991706507602861257607755717324481205354735921689122483708111287912399921239324200890380184519619245012653383868608310936529367007101015334184113258063935359898276310927895193424072976132403483460711640581743681257025025351510022029326969846723677429494625383685092762319508020011256936685778377920053347090121947986922174933424381 4.873643461158670531990089724785375251139974653185486449030751571759749423195009035709181012099365643657774227928689009677063789704549534710537014920222854765978377334604244103173534464724033629853646979741154242569256332779147902902708621136498249040806016428511479398596991706507602861257607755717324481205354735921689122483708111287912399921239324200890380184519619245012653383868608310936529367007 }}} {{{id=16| a=RIF(sqrt(2)) print a print a^2 type(a) a.absolute_diameter() /// 1.414213562373095? 2.000000000000000? 2.22044604925031e-16 }}} {{{id=55| GF(5).multiplication_table() /// * a b c d e +---------- a| a a a a a b| a b c d e c| a c e b d d| a d b e c e| a e d c b }}} {{{id=11| K. = NumberField(x^2 - 2); print K print c^2 - 2 print c^3 /// Number Field in c with defining polynomial x^2 - 2 0 2*c }}} {{{id=13| R. = PolynomialRing(K) p = x^2 - 2 factor(p) /// (x - c) * (x + c) }}} {{{id=1| f(x) = x^3 - x df(x) = diff(f(x), x) @interact def tangent_at_point(x0 = slider(-2, 2, 0.1, -1.5, label='x-coordinate')): """ Shows the tangent line at the point x0, using f and its derivative as global variables. """ y0 = f(x0) slope = df(x0) # y - y0 = slope*(x - x0) implies # y = slope*x - slope*x0 + y0 b = y0 - slope*x0 P1 = plot(f, -2, 2, color='blue', ymin=-6, ymax=6, gridlines='minor') P2 = plot(slope*x + b, -2, 2, color='tan', ymin=-6, ymax=6) P3 = point((x0, y0), color='red', size=50) P = P1+P2+P3 P.show() /// }}} {{{id=7| var('x') /// x }}} {{{id=5| @interact def myplot(f=x^2): show(plot(f,(x,-3,3))) /// }}} {{{id=6| fun = x^3 - x rng = (x, -2, 2) @interact def colorplot(colrgb=selector(['red', 'green', 'blue'], label='color: ')): """ Plots the expression defined by fun over the range in rng. The parameters fun and rng must be defined before calling colorplot. The user can select the color. """ P = plot(fun, rng, color=colrgb) P.show() /// }}} {{{id=49| sage: f(x) = sin(x) sage: g(x) = sin(2*x) sage: h(x) = sin(4*x) sage: p1 = plot(g,(-2*pi,2*pi),color=hue(0.5)) # long time sage: p2 = plot(h,(-2*pi,2*pi),color=hue(0.9)) # long time sage: p3 = parametric_plot((f,g),(0,2*pi),color=hue(0.6)) # long time sage: p4 = parametric_plot((f,h),(0,2*pi),color=hue(1.0)) # long time sage: graphics_array(((p1,p2),(p3,p4))) /// }}} {{{id=50| maxima.load('interpol') def f(x): return RDF(1 / (1 + 25 * x^2)) def runge(): g = plot(f, -1, 1, rgbcolor='red', thickness=1) polynom = [] for i, n in enumerate([6, 8, 10, 12]): #AWESOME TRICK!!! data = [(x, f(x)) for x in xsrange(-1, 1, 2 / (n - 1), include_endpoint=True)] polynom.append(maxima.lagrange(data).sage()) g += list_plot(data, rgbcolor='black', pointsize=5) g += plot(polynom, -1, 1, fill=f, fillalpha=0.2, thickness=0) return g runge().show(ymin=0, ymax=1, figsize=(6,4)) /// }}} {{{id=53| from sage.plot.plot3d.parametric_surface import MoebiusStrip ms = MoebiusStrip(3,1,plot_points=200) show(ms) /// }}} {{{id=51| import operator # The colors for various elements of the plot: class color: stylus = (1, 0, 0) outer = (.8, .8, .8) inner = (0, 0, 1) plot = (0, 0, 0) center = (0, 0, 0) tip = (1, 0, 0) # and the corresponding line weights: class weight: stylus = 1 outer = 1 inner = 1 plot = 1 center = 5 tip = 5 scale = 1 # The scale of the image animation_delay = .1 # The delay between frames, in seconds # Starting and ending t values t_i = 0 t_f = 2*pi # The t values of the animation frames tvals = srange(t_i, t_f, (t_f-t_i)/60) r_o = 8 # Outer circle radius r_i = 2 # Inner circle radius r_s = 3 # Stylus radius # Coordinates of the center of the inner circle x_c = lambda t: (r_o - r_i)*cos(t) y_c = lambda t: (r_o - r_i)*sin(t) # Parametric coordinates for the plot x = lambda t: x_c(t) + r_s*cos(t*(r_o/r_i)) y = lambda t: y_c(t) - r_s*sin(t*(r_o/r_i)) # Maximum x and y values of the plot x_max = r_o - r_i + r_s y_max = find_local_maximum(y, t_i, t_f)[0] # The plots of the individual elements. Order is important; plots # are stacked from bottom to top as they appear. elements = ( # The outer circle lambda t_f: circle((0, 0), r_o, rgbcolor=color.outer, thickness=weight.outer), # The plot itself lambda t_f: parametric_plot((x, y), t_i, t_f, rgbcolor=color.plot, thickness=weight.plot), # The inner circle lambda t_f: circle((x_c(t_f), y_c(t_f)), r_i, rgbcolor=color.inner, thickness=weight.inner), # The inner circle's center lambda t_f: point((x_c(t_f), y_c(t_f)), rgbcolor=color.center,pointsize=weight.center), # The stylus lambda t_f: line([(x_c(t_f), y_c(t_f)), (x(t_f), y(t_f))], rgbcolor=color.stylus, thickness=weight.stylus), # The stylus' tip lambda t_f: point((x_c(t_f), y_c(t_f)), rgbcolor=color.tip, pointsize=weight.tip), ) # Create the plots and animate them. The animate function renders an # animated gif from the frames provided as its first argument. animation = animate([sum(f(t) for f in elements) for t in tvals[1:]], xmin=-x_max, xmax=x_max, ymin=-y_max, ymax=y_max, figsize=(x_max*scale, y_max*scale * y_max/x_max)) animation.show(delay=animation_delay) /// /home/samal/local/sage-7.3/local/lib/python2.7/site-packages/sage/misc/decorators.py:554: DeprecationWarning: variable ranges to parametric_plot must be given as tuples, like (2,4) or (t,2,3) See http://trac.sagemath.org/7008 for details. return func(*args, **options) }}} {{{id=54| var('x k') frames = [plot(exp(-x^2)*sin(2*k*pi*x), (x, -2, 2)) for k in range(1,11)] frames[1].show() a=animate(frames) /// }}} {{{id=56| a.save('a.avi') /// }}} {{{id=52| dim = 32 rm = Matrix(dim, dim, [GF(2).random_element() for k in range(dim*dim)]) print rm print rm.str() matrix_plot(rm) /// 32 x 32 dense matrix over Finite Field of size 2 [1 1 1 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 0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47| from scipy.misc import derivative as numdif var('x') numdif(func=sin, x0=0, dx=0.01, order=15) /// 1.0000000000000002 }}} {{{id=8| var('x') y = function('y')(x) dy = y.diff(x) print dy, type(dy) /// diff(y(x), x) }}} {{{id=18| circle = x^2 + y^2 - 1 == 0 print circle, type(circle) /// x^2 + y(x)^2 - 1 == 0 }}} {{{id=19| dc = circle.diff(x) print dc /// 2*y(x)*diff(y(x), x) + 2*x == 0 }}} {{{id=20| yx = solve(dc, dy) print yx /// [ diff(y(x), x) == -x/y(x) ] }}} {{{id=21| solve(circle.diff().diff(),y.diff(x,2)) /// [diff(y(x), x, x) == -(diff(y(x), x)^2 + 1)/y(x)] }}} {{{id=22| var('x') q = x^3 + 3*x + 8 print q, type(q) pq = q.power_series(QQ) print pq, type(pq) /// x^3 + 3*x + 8 8 + 3*x + x^3 + O(x^4) }}} {{{id=23| pq*(x + 0*x^5).power_series(QQ) /// 8*x + O(x^2) }}} {{{id=48| p = MixedIntegerLinearProgram(maximization=True) x = p.new_variable(nonnegative=True) p.set_objective(x[1] + 5*x[2]) p.add_constraint(x[1] + 0.2*x[2], max=4) p.add_constraint(1.5*x[1] + 3*x[2], max=4) p.show() p.solve() /// Traceback (most recent call last): p.show() File "", line 1, in File "/tmp/u/husekr/tmpF037Kc/___code___.py", line 3, in p = MixedIntegerLinearProgram(maximization=True) File "sage/misc/lazy_import.pyx", line 389, in sage.misc.lazy_import.LazyImport.__call__ (/var/tmp/portage/sci-mathematics/sage-7.4-r2/work/sage-7.4/src-python2_7/build/cythonized/sage/misc/lazy_import.c:3648) File "sage/numerical/mip.pyx", line 453, in sage.numerical.mip.MixedIntegerLinearProgram.__init__ (/var/tmp/portage/sci-mathematics/sage-7.4-r2/work/sage-7.4/src-python2_7/build/cythonized/sage/numerical/mip.c:3477) File "sage/numerical/backends/generic_backend.pyx", line 1628, in sage.numerical.backends.generic_backend.get_solver (/var/tmp/portage/sci-mathematics/sage-7.4-r2/work/sage-7.4/src-python2_7/build/cythonized/sage/numerical/backends/generic_backend.c:18076) File "sage/numerical/backends/generic_backend.pyx", line 1770, in sage.numerical.backends.generic_backend.get_solver (/var/tmp/portage/sci-mathematics/sage-7.4-r2/work/sage-7.4/src-python2_7/build/cythonized/sage/numerical/backends/generic_backend.c:17480) File "sage/numerical/backends/glpk_backend.pyx", line 26, in init sage.numerical.backends.glpk_backend (/var/tmp/portage/sci-mathematics/sage-7.4-r2/work/sage-7.4/src-python2_7/build/cythonized/sage/numerical/backends/glpk_backend.cpp:21110) ImportError: /usr/lib64/python2.7/site-packages/sage/libs/glpk/error.so: undefined symbol: glp_at_error }}} {{{id=36| #Rubic cube gap.set('front', '(17,19,24,22)(18,21,23,20)( 6,25,43,16)( 7,28,42,13)( 8,30,41,11)') gap.set('top', '( 1, 3, 8, 6)( 2, 5, 7, 4)( 9,33,25,17)(10,34,26,18)(11,35,27,19)') gap.set('left', '( 9,11,16,14)(10,13,15,12)( 1,17,41,40)( 4,20,44,37)( 6,22,46,35)') gap.set('right', '(25,27,32,30)(26,29,31,28)( 3,38,43,19)( 5,36,45,21)( 8,33,48,24)') gap.set('rear', '(33,35,40,38)(34,37,39,36)( 3, 9,46,32)( 2,12,47,29)( 1,14,48,27)') gap.set('bottom', '(41,43,48,46)(42,45,47,44)(14,22,30,38)(15,23,31,39)(16,24,32,40)') gap.set('cube', 'Group( front, top, left, right, rear, bottom)') print gap.get('cube') /// Group( [ ( 6,25,43,16)( 7,28,42,13)( 8,30,41,11)(17,19,24,22)(18,21,23,20), ( 1, 3, 8, 6)( 2, 5, 7, 4)( 9,33,25,17)(10,34,26,18)(11,35,27,19), ( 1,17,41,40)( 4,20,44,37)( 6,22,46,35)( 9,11,16,14)(10,13,15,12), ( 3,38,43,19)( 5,36,45,21)( 8,33,48,24)(25,27,32,30)(26,29,31,28), ( 1,14,48,27)( 2,12,47,29)( 3, 9,46,32)(33,35,40,38)(34,37,39,36), (14,22,30,38)(15,23,31,39)(16,24,32,40)(41,43,48,46)(42,45,47,44) ] ) }}} {{{id=37| size = gap('Size(cube)') print size print size.sage().factor() /// 43252003274489856000 2^27 * 3^14 * 5^3 * 7^2 * 11 }}} {{{id=39| @parallel(10) def f(n): sleep(1) # make function seem slow return n*(n+1)/2 /// }}} {{{id=40| %time for n in [1..10]: print n, f(n) /// 1 1 2 3 3 6 4 10 5 15 6 21 7 28 8 36 9 45 10 55 CPU time: 0.00 s, Wall time: 10.01 s }}} {{{id=41| %time for X in f([1..10]): print X /// (((1,), {}), 1) (((2,), {}), 3) (((3,), {}), 6) (((4,), {}), 10) (((5,), {}), 15) (((6,), {}), 21) (((7,), {}), 28) (((8,), {}), 36) (((9,), {}), 45) (((10,), {}), 55) CPU time: 0.06 s, Wall time: 1.25 s }}} {{{id=43| class MyRational: def __init__(self, n, d): """ My attempt at reimplementing rationals. """ self._n = Integer(n); self._d = Integer(d) def __repr__(self): return 'Muj zlomek %s:%s'%(self._n, self._d) def __add__(self, right): return MyRational(self._n*right._d + self._d*right._n, self._d*right._d) def __sub__(self, right): return MyRational(self._n*right._d - self._d*right._n, self._d*right._d) def __neg__(self): return MyRational(self._n, -self._d) def __mul__(self, right): return MyRational(self._n*right._n, self._d*right._d) def reduced_form(self): """Return the reduced form of this rational number.""" a = self._n/self._d return MyRational(a.numerator(), a.denominator()) /// }}} {{{id=44| a=MyRational(10,4); b=MyRational(7,3); -b a /// Muj zlomek 10:4 }}} {{{id=45| a.reduced_form() /// Muj zlomek 5:2 }}} {{{id=57| G=graphs.ButterflyGraph(); G /// }}} {{{id=58| G.chromatic_number() /// 3 }}} {{{id=59| G.chromatic_polynomial() /// x^5 - 6*x^4 + 13*x^3 - 12*x^2 + 4*x }}} {{{id=60| G.graph6_string() /// 'DK{' }}} {{{id=61| H=Graph('DK{') /// }}} {{{id=62| H.set_pos( {0:(1,2), 1:(2,3), 2:(3,4), 3:(-1,-1), 4:(-2,-2)}); H /// }}} {{{id=65| H.set_pos({}); H /// }}} {{{id=66| H = Graph({0:[3,4],1:[2,4],2:[1,4],3:[0,4],4:[0,1,2,3]}); H.set_pos({0:[337.2101529132654,161.21965776249894],1:[4.595718460572102,235.16732914538483],2:[29.377542755458965,62.1108036070687],3:[256.35824956948716,316.223294319094],4:[164.72828505892167,169.32355068866835]}); graph_editor(H); ///
}}} {{{id=67| graphs.RandomRegular(3,20).plot() /// }}} {{{id=68| /// }}}