Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/interacts/library_cython.pyx
4069 views
1
r"""
2
Library of cythonized methods
3
4
AUTHORS:
5
6
- Harald Schilly (2011-01-16): initial version (#9623) partially based on work by Lauri Ruotsalainen
7
8
"""
9
10
#*****************************************************************************
11
# Copyright (C) 2011 Harald Schilly <[email protected]>
12
#
13
# Distributed under the terms of the GNU General Public License (GPL)
14
# as published by the Free Software Foundation; either version 2 of
15
# the License, or (at your option) any later version.
16
# http://www.gnu.org/licenses/
17
#*****************************************************************************
18
19
from sage.misc.misc import prod
20
21
include '../ext/interrupt.pxi'
22
include '../ext/cdefs.pxi'
23
include "../ext/stdsage.pxi"
24
25
cpdef julia(ff_j, z, int iterations):
26
"""
27
Helper function for the Julia Fractal interact example.
28
29
INPUT:
30
31
- `ff_j` -- fast callable for the inner iteration
32
- `z` -- complex number
33
- `iterations` -- number of loops
34
35
TESTS::
36
37
sage: from sage.interacts.library_cython import julia
38
sage: z = var('z')
39
sage: c_real, c_imag = 1, 1
40
sage: f = symbolic_expression(z**2 + c_real + c_imag * CDF.gen()).function(z)
41
sage: ff_m = fast_callable(f, vars=[z], domain=CDF)
42
sage: julia(ff_m, CDF(1,1), 3)
43
1.0 + 3.0*I
44
"""
45
for i in range(iterations):
46
z = ff_j(z)
47
if z.abs() > 2: break
48
return z
49
50
cpdef mandel(ff_m, z, int iterations):
51
"""
52
Helper function for the Mandelbrot Fractal interact example.
53
54
INPUT:
55
56
- `ff_m` -- fast callable for the inner iteration
57
- `z` -- complex number
58
- `iterations` -- number of loops
59
60
TESTS::
61
62
sage: from sage.interacts.library_cython import mandel
63
sage: z, c = var('z, c')
64
sage: f = symbolic_expression(z**2 + c).function(z,c)
65
sage: ff_m = fast_callable(f, vars=[z,c], domain=CDF)
66
sage: mandel(ff_m, CDF(1,1), 3)
67
1.0 + 3.0*I
68
69
"""
70
c = z
71
for i in range(iterations):
72
z = ff_m(z, c)
73
if z.abs() > 2: break
74
return z
75
76
77
cpdef cellular(rule, int N):
78
"""
79
Cythonized helper function for the cellular_automata fractal.
80
Yields a matrix showing the evolution of a Wolfram's cellular automaton.
81
Based on work by Pablo Angulo.
82
http://wiki.sagemath.org/interact/misc#CellularAutomata
83
84
INPUT:
85
86
- `rule` -- determines how a cell's value is updated, depending on its neighbors
87
- `N` -- number of iterations
88
89
TESTS::
90
91
sage: from sage.interacts.library_cython import cellular
92
sage: rule = [1, 0, 1, 0, 0, 1, 1, 0]
93
sage: N = 3
94
sage: print cellular(rule, N)
95
[[0 0 0 1 0 0 0 0]
96
[1 1 0 1 0 1 0 0]
97
[0 1 1 1 1 1 0 0]]
98
"""
99
from numpy import zeros
100
cdef int j, k, l
101
M=zeros((N, 2*N+2), dtype=int)
102
M[0,N]=1
103
104
for j in range(1, N):
105
for k in range(0, 2*N):
106
l = 4 * M[j-1, k-1] + 2 * M[j-1, k] + M[j-1, k+1]
107
M[j,k] = rule[l]
108
return M
109
110