Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagemath.github.io
Path: blob/master/eval/db2.json
2457 views
1
{
2
"Simple Plot" : {
3
"cat" : ["Calculus", "Plot"],
4
"code" : [ "plot(sin(x), x, (-10, 10))" ],
5
"descr" : "a simple sin plot"
6
},
7
8
"Funny Plot" : {
9
"cat" : ["Calculus", "Plot"],
10
"code" : [ "plot(sin(x) / (2+cos(pi*x)), (-2*pi, 6*pi))" ],
11
"descr" : "this is just a funny plot"
12
},
13
14
"ODE Plot" : {
15
"cat" : [ "Calculus", "Plot"],
16
"descr" : "A plot of an ODE",
17
"code" : [
18
"x, y, t = var('x y t')",
19
"P=desolve_system_rk4([x+y, x-y], [x,y], ics=[0,1,-1], ivar=t, end_points=2)",
20
"p1 = list_plot([[i,j] for i,j,k in P], plotjoined=True)",
21
"p2 = list_plot([[i,k] for i,j,k in P], plotjoined=True, color='red')",
22
"show(p1+p2)"
23
]
24
},
25
26
"Gaussian Distribution" : {
27
"cat" : [ "Statistics", "Distributions" ],
28
"descr" : "A plot of (x,y) gaussian distributed points.",
29
"code" : [
30
"N = RealDistribution('gaussian', 5)",
31
"re = N.get_random_element",
32
"list_plot([(re(), re()) for _ in range(5000)])"
33
]
34
},
35
36
"Differential EQ" :{
37
"cat" : [ "Calculus", "ODE" ],
38
"descr" : "These examples solve ordinary differential equations. <a href='https://sagemath.org/doc/constructions/calculus.html#ordinary-differential-equations'>see documentation</a>",
39
"code" : [
40
"y=function('y',x)",
41
"print(desolve(diff(y,x,2) + 3*x == y, dvar = y, ics = [1,1,1]))",
42
"print(desolve(diff(y,x,2) + 3*x == y, dvar = y))",
43
"print(desolve(diff(y,x) + 3*x == y, dvar = y))",
44
"",
45
"f=function('f',x);",
46
"print(desolve_laplace(diff(f,x,2) == 2*diff(f,x)-f, dvar = f, ics = [0,1,2]))",
47
"print(desolve_laplace(diff(f,x,2) == 2*diff(f,x)-f, dvar = f))"
48
]
49
},
50
51
"Sympy Basics" : {
52
"cat" : [ "Libs", "Sympy" ],
53
"descr" : "You can also access <a href='http://www.sympy.org/'>SymPy</a>",
54
"code" : [
55
"import sympy",
56
"from sympy import exp,log,Symbol,Rational,sin,limit,oo",
57
"x=sympy.Symbol('x')",
58
"y=sympy.Symbol('y')",
59
"print('Limit:', limit(sqrt(x**2-5*x+6)-x, x, oo))",
60
"print('Differentiate:', ((x**2+2*y*sin(x))**2).diff(x))",
61
"print('Expansion:', ((x**2+y**3)**4).expand())"
62
]
63
},
64
65
"MILP Program": {
66
"cat" : [ "Numerics", "Optimization" ],
67
"descr": "Mixed Integer Linear Program. <a href='https://sagemath.org/doc/reference/sage/numerical/mip.html'>see documentation</a>",
68
"code" : [
69
"@interact",
70
"def _(w3min = slider(-5,15,1,1), f2=slider(10,30,1,14)):",
71
" p = MixedIntegerLinearProgram(maximization=False) ",
72
" w = p.new_variable(integer=True) ",
73
" p.add_constraint(w[0] + w[1] + w[2] - f2*w[3] == 0) ",
74
" p.add_constraint(w[1] + 2*w[2] - 8*w[3] == 0) ",
75
" p.add_constraint(2*w[2] - 3*w[3] == 0) ",
76
" p.add_constraint(w[0] - w[1] - w[2] >= 0)",
77
" p.add_constraint(w[3] >= w3min) ",
78
" # default min is 0 !",
79
" _ = [ p.set_min(w[i], None) for i in range(1,4) ] ",
80
" p.set_objective(w[3]) ",
81
" p.show() ",
82
" try:",
83
" sol = p.solve()",
84
" print('Objective Value:', sol)",
85
" for i, v in p.get_values(w).items():",
86
" print('w_%s = %s' % (i, int(round(v))))",
87
" except Exception as e:",
88
" print('ERROR:', e)"
89
]
90
},
91
92
"Arbitrary Precision": {
93
"cat" : [ "Numerics", "Numbers" ],
94
"descr" : "Example for arbitrary precision real numbers. <a href='https://sagemath.org/doc/reference/sage/rings/real_mpfr.html'>documentation</a>",
95
"code" : [
96
"R = RealField(300)",
97
"x = R(1) + R(10)^-50",
98
"print('x: ', x)",
99
"print('x*pi:', x * R.pi())",
100
"y = x.sin() / R(1e25) + 1",
101
"print('y: ', y)",
102
"print('precision of y:', y.prec())",
103
"z = y^1000",
104
"print('z: ', z)",
105
"a = z.nth_root(1000)",
106
"print('a: ', a)",
107
"print('y-a: ', y-a)"
108
]
109
},
110
111
"Hidden Markov Model" : {
112
"cat" : [ "Statistics" , "Bayesian Network" ],
113
"descr" : "Some fun with <a href='https://sagemath.org/doc/reference/sage/stats/hmm/hmm.html'>HMMs</a>",
114
"code" : [
115
"A = random_matrix(RDF,4,min=.1, max=.9)",
116
"B = random_matrix(RDF,4,2,min=.1, max=.9)",
117
"m = hmm.DiscreteHiddenMarkovModel(A, B, [.2,.1,.1,.6])",
118
"print(m)",
119
"print('Log Likelihood:', m.log_likelihood([0,1,0,1,0,1]))",
120
"print('Viterbi:', m.viterbi([1,1,0,1]))",
121
"print('Baum-Welch:', m.baum_welch([1,1,0,1]))",
122
"print('20 Samples:', m.sample(20))",
123
"print(m)",
124
"m.graph().show()"
125
]
126
}
127
}
128
129