Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagemath.github.io
Path: blob/master/eval/db1.json
2457 views
1
{
2
"Taylor Series" : {
3
"cat" : [ "Calculus", "Basics" ],
4
"descr" : "Interactive Taylor Series",
5
"code": [
6
"var('x')",
7
"x0 = 0",
8
"f = sin(x)*e^(-x)",
9
"p = plot(f,-1,5, thickness=2)",
10
"dot = point((x0,f(x=x0)),pointsize=80,rgbcolor=(1,0,0))",
11
"@interact",
12
"def _(order=(1..12)):",
13
" ft = f.taylor(x,x0,order)",
14
" pt = plot(ft,-1, 5, color='green', thickness=2)",
15
" html('$f(x)\\;=\\;%s$'%latex(f))",
16
" html('$\\hat{f}(x;%s)\\;=\\;%s+\\mathcal{O}(x^{%s})$'%(x0,latex(ft),order+1))",
17
" show(dot + p + pt, ymin = -.5, ymax = 1)"
18
]
19
},
20
21
"Integral" : {
22
"cat" : ["Calculus", "Basics"],
23
"descr" : "Integration example",
24
"code" : [
25
"f = sin(2*x)*(x+cos(x))",
26
"g = integrate(f, x)",
27
"html('$f(x) = %s$' % latex(f))",
28
"print()",
29
"html('$\\int f(x) d\\mathrm{x} = %s$' % latex(g))",
30
"",
31
"iv = (-2*pi,4*pi)",
32
"p1 = plot(f, x, iv)",
33
"p2 = plot(g, x, iv, color='red')",
34
"show(p1+p2)"
35
]
36
},
37
38
"Differential" : {
39
"cat" : ["Calculus", "Basics"],
40
"descr" : "differentiation",
41
"code" : [
42
"f = sin(2*x)*(x+cos(x))",
43
"g = diff(f, x)",
44
"html('$f(x) = %s$' % latex(f))",
45
"print()",
46
"html('$\\\\frac{\\partial}{\\partial x} f(x) = %s$' % latex(g))",
47
"",
48
"iv = (-2*pi,4*pi)",
49
"p1 = plot(f, x, iv)",
50
"p2 = plot(g, x, iv, color='red')",
51
"show(p1+p2)"
52
]
53
},
54
55
"RPy2" : {
56
"cat" : [ "Statistics", "R" ],
57
"descr":"Just a generic rpy2 test, see <a href='http://rpy.sourceforge.net/'>RPy2</a>",
58
"code" : [
59
"import rpy2.robjects as robjects",
60
"r = robjects.r",
61
"ctl = robjects.FloatVector([4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14])",
62
"trt = robjects.FloatVector([4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69])",
63
"group = r.gl(2r, 10r, 20r, labels = ['Ctl','Trt'])",
64
"weight = ctl + trt",
65
"robjects.globalEnv['weight'] = weight",
66
"robjects.globalEnv['group'] = group",
67
"lm_D9 = r.lm('weight ~ group')",
68
"print(r.anova(lm_D9))",
69
"print()",
70
"print(lm_D9.r['fitted.values'])"
71
]
72
},
73
74
"Normal Subgroup?" : {
75
"cat" : [ "Algebra", "Groups" ],
76
"descr" : "Checking normality (<a href='https://sagemath.org/doc/thematic_tutorials/group_theory.html#normal-subgroups'>read more here</a>)",
77
"code" : [
78
"# see https://sagemath.org/doc/thematic_tutorials/group_theory.html#normal-subgroups",
79
"A4 = AlternatingGroup(4)",
80
"print(A4)",
81
"print('Elements of A4:')",
82
"print(A4.list())",
83
"r1 = A4('(1,2) (3,4)')",
84
"r2 = A4('(1,3) (2,4)')",
85
"r3 = A4('(1,4) (2,3)')",
86
"H = A4.subgroup([r1, r2, r3])",
87
"print(H)",
88
"print('Elements of H:')",
89
"print(H.list())",
90
"H.is_normal(A4)"
91
]
92
},
93
94
"Wheel Graph" : {
95
"cat" : [ "Graph Theory", "Graph" ],
96
"descr" : "Order of a modified Wheel-Graph and neighbors of a vertex.",
97
"code" : [
98
"N = 20",
99
"G = graphs.WheelGraph(N)",
100
"for idx, i in enumerate(range(0, N, 2)):",
101
" G.add_edge(idx + 1, i)",
102
"print('order:', G.order())",
103
"ngbs = sorted(G.neighbors(N-1))",
104
"print('neighbors of %d: %s' % (N-1, ngbs))",
105
"G.show()"
106
]
107
},
108
109
"Density Plot" : {
110
"cat" : [ "Graphics", "Plot 2D" ],
111
"descr" : "This is a density plot.",
112
"code" : [
113
"var('x, y')",
114
"f = sin(x^2 + y^2) * cos(x+y^2) * sin(y)",
115
"p = density_plot(f, (x, -4, 4), (y, -4, 4), cmap='jet', plot_points=100)",
116
"p.show(figsize=(6,6), frame=True)"
117
]
118
},
119
"Induced Subgraph": {
120
"cat" : [ "Graph Theory", "Interact" ],
121
"descr" : "by Jason Grout",
122
"code" : [
123
"m=random_matrix(ZZ,10,density=.5)",
124
"a=DiGraph(m) ",
125
"",
126
"@interact",
127
"def show_subgraph(to_delete=selector(range(10),buttons=True)):",
128
" vertices=set(range(10))",
129
" subgraph_vertices=vertices.difference([to_delete])",
130
" plot(a,save_pos=True,aspect_ratio=1).show(figsize=[3,3])",
131
" plot(a.subgraph(subgraph_vertices),aspect_ratio=1).show(figsize=[3,3])"
132
]
133
},
134
"Mandelbrot" : {
135
"cat" : [ "Graphics", "Fractals" ],
136
"descr" : "This is an interactive complex plot of the mandelbrot fractal. Make sure to play around with the first slider 'expo'!",
137
"code" : [
138
"@interact",
139
"def mandel_plot(expo = slider(-10,10,0.1,2), \\",
140
" formula = ['mandel','ff'],\\",
141
" iterations=slider(1,100,1,30), \\",
142
" zoom_x = range_slider(-2,2,0.01,(-2,1)), \\",
143
" zoom_y = range_slider(-2,2,0.01,(-1.5,1.5))):",
144
" var('z c')",
145
" f(z,c) = z^expo + c",
146
" ff_m = fast_callable(f, vars=[z,c], domain=CDF)",
147
" ",
148
" # fast_callable",
149
" for i in range(int(iterations)/3):",
150
" f(z,c) = f(z,c)^expo+c",
151
" ff = fast_callable(f, vars=[z,c], domain=CDF) ",
152
" ",
153
" def mandel(z):",
154
" c = z",
155
" for i in range(iterations):",
156
" z = ff_m(z,c)",
157
" if abs(z) > 2:",
158
" return z",
159
" return z",
160
" print('z <- z^%s + c' % expo)",
161
" ",
162
" # calling ff three times, otherwise it fast_callable exceeds a recursion limit",
163
" if formula is 'ff':",
164
" func = lambda z: ff(ff(ff(z,z),z),z)",
165
" elif formula is 'mandel':",
166
" func = mandel ",
167
" ",
168
" p=complex_plot(func, zoom_x,zoom_y, plot_points=200, dpi=100)",
169
" p.show(frame=True, aspect_ratio=1)"
170
]
171
},
172
173
"Permutation Group" : {
174
"cat" : ["Algebra" , "Groups" ],
175
"descr" : "<a href='https://sagemath.org/doc/reference/sage/groups/perm_gps/permgroup.html'>see documentation</a>",
176
"code" : [
177
"G = PermutationGroup([[(1,5,7),(3,2)], [(1,3,5)]])",
178
"html('<h3>Elements of %s</h3>' % G)",
179
"html('%s' % G.list())",
180
"",
181
"html('<h3>Properties and more</h3>')",
182
"print('order:', G.order())",
183
"print('degree:', G.degree())",
184
"print('center:', G.center())",
185
"print('non-fixed points:', G.non_fixed_points())",
186
"",
187
"n = G([(2,5)])",
188
"html( '<h3>Normalizer of %s in G:</h3>'%n)",
189
"print(G.normalizer(n))",
190
"",
191
"html('<h3>Character Table</h3>')",
192
"print(G.character_table())"
193
]
194
},
195
196
"Polynomial Ring" : {
197
"cat" : ["Algebra", "Polynomials" ],
198
"descr" : "Just some examples of <a href='http://sagemath.org/doc/reference/sage/rings/polynomial/multi_polynomial_ring_generic.html'>this</a>",
199
"code": [
200
"P = PolynomialRing(RDF, 'x')",
201
"x = P.gen(0)",
202
"p1 = 2*x+x^2-1",
203
"print(p1, 'evaluated at x=1.:', p1.subs({x : 1.}))",
204
"",
205
"P2.<x> = PolynomialRing(GF(2))",
206
"print('3*x^2+2*x+1 in GF(2) =', 3*x^2+2*x+1)",
207
"print('P2.characteristic() = ', P2.characteristic())",
208
"",
209
"P3 = P2.change_ring(ZZ)",
210
"y = P3.gen(0)",
211
"p3 = 3*y^2+2*y+1",
212
"print('in ZZ:', p3)",
213
"",
214
"print('p1(p3) =', p1(p3))"
215
]
216
},
217
"Infinite Polynomial Ring" : {
218
"cat" : ["Algebra", "Polynomials" ],
219
"descr" : "<a href='https://sagemath.org/doc/reference/polynomial_rings_infinite.html'>see documentation</a>",
220
"code" : [
221
"A.<alpha,beta> = InfinitePolynomialRing(QQ,implementation='sparse')",
222
"print(A)",
223
"p1 = 2*alpha[3]-3*alpha[11]*beta[1]^2",
224
"p2 = beta[11]^4+1",
225
"print('p1:', p1)",
226
"print('p2:', p2)",
227
"print('p1+p2 =', p1+p2)",
228
"print('p1*p2 =', p1*p2)",
229
"print('p1*alpha[11]^(-1) = ', p1 * alpha[11]^(-1))"
230
]
231
},
232
233
"Points on an Elliptic Curve" : {
234
"cat" : ["Algebra", "Elliptic Curves"],
235
"descr" : "see <a href='https://sagemath.org/doc/reference/sage/schemes/elliptic_curves/ell_point.html'>documentation</a>",
236
"code" :[
237
"R.<x> = GF(101^3)",
238
"E = EllipticCurve(R, [1,0,3,-1,1])",
239
"print(E)",
240
"print(E.cardinality())",
241
"print('point with x=1:', E.lift_x(1))",
242
"print('on E?', E(1,43,1) in E)"
243
]
244
},
245
246
"Plot Elliptic Curve" : {
247
"cat" : ["Algebra", "Elliptic Curves"],
248
"descr" : "",
249
"code" : [
250
"E = EllipticCurve(RDF, [-2,2,0,-1,0])",
251
"print(E)",
252
"@interact",
253
"def _(x=slider(-3,2,0.1,1)):",
254
" x1 = E.lift_x(x)",
255
" print('point at x=%.2f:'%x, x1)",
256
" p1 = E.plot()",
257
" p2 = point2d(x1.xy(), color='red', size=30)",
258
" show(p1+p2)"
259
]
260
},
261
262
"Point Separation" : {
263
"cat" : [ "Numerics" , "Optimization" ],
264
"descr" : "This linear program separates a number of points with a polynomial of given degree",
265
"code" : [
266
"'''",
267
"This linear program separates a set of n",
268
"points with a polynomial of degree 'degree'.",
269
"'''",
270
"",
271
"def random_points(n=5):",
272
" '''",
273
" generate some random points",
274
" '''",
275
" greens = [(6*random(), 2*random()) for i in range(n)]",
276
" reds = [(6*random(), 2*random() + 4) for i in range(n)]",
277
" return greens, reds",
278
"",
279
"var('t')",
280
"greens, reds = random_points()",
281
"",
282
"@interact",
283
"def LinProg(degree = slider(1,10,1,3,label='Degree of Poly')):",
284
" separa = MixedIntegerLinearProgram()",
285
" delta = separa.new_variable()",
286
" ap = separa.new_variable()",
287
" an = separa.new_variable()",
288
" separa.set_objective(delta[0])",
289
" J = range(1,degree +1)",
290
" for red in reds:",
291
" l = [ (ap[j]-an[j])*(red[0])^j for j in J]",
292
" separa.add_constraint(ap[0]-an[0] + sum(l) + delta[0], max = red[1])",
293
" for green in greens:",
294
" l = [ (ap[j]-an[j])*green[0]^j for j in J]",
295
" separa.add_constraint(ap[0]-an[0] + sum(l) - delta[0], min = green[1])",
296
" plt = point(greens,color='green') + point(reds,color='red')",
297
" try:",
298
" separa.solve()",
299
" vals = separa.get_values",
300
" curvaSep = sum([(vals(ap)[i]-vals(an)[i])*t^i for i in range(degree+1)] )",
301
" html('$p(t)=%s$' % latex(curvaSep.expand()))",
302
" plt += plot(curvaSep,0,6)",
303
" except:",
304
" html('''<span style='color: red; font-weight: bold'>",
305
" There seems to be no polynomial of degree %d that separates these points.",
306
" </span>''' % degree)",
307
" plt.show(ymax=6,ymin=-4,figsize=[4,4])"
308
]
309
}
310
}
311
312