Path: blob/master/src/sage/interacts/library_cython.pyx
8817 views
r"""1Library of cythonized methods23AUTHORS:45- Harald Schilly (2011-01-16): initial version (#9623) partially based on work by Lauri Ruotsalainen67"""89#*****************************************************************************10# Copyright (C) 2011 Harald Schilly <[email protected]>11#12# Distributed under the terms of the GNU General Public License (GPL)13# as published by the Free Software Foundation; either version 2 of14# the License, or (at your option) any later version.15# http://www.gnu.org/licenses/16#*****************************************************************************1718from sage.misc.misc import prod1920include 'sage/ext/interrupt.pxi'21include 'sage/ext/cdefs.pxi'22include "sage/ext/stdsage.pxi"2324cpdef julia(ff_j, z, int iterations):25"""26Helper function for the Julia Fractal interact example.2728INPUT:2930- `ff_j` -- fast callable for the inner iteration31- `z` -- complex number32- `iterations` -- number of loops3334TESTS::3536sage: from sage.interacts.library_cython import julia37sage: z = var('z')38sage: c_real, c_imag = 1, 139sage: f = symbolic_expression(z**2 + c_real + c_imag * CDF.gen()).function(z)40sage: ff_m = fast_callable(f, vars=[z], domain=CDF)41sage: julia(ff_m, CDF(1,1), 3)421.0 + 3.0*I43"""44for i in range(iterations):45z = ff_j(z)46if z.abs() > 2: break47return z4849cpdef mandel(ff_m, z, int iterations):50"""51Helper function for the Mandelbrot Fractal interact example.5253INPUT:5455- `ff_m` -- fast callable for the inner iteration56- `z` -- complex number57- `iterations` -- number of loops5859TESTS::6061sage: from sage.interacts.library_cython import mandel62sage: z, c = var('z, c')63sage: f = symbolic_expression(z**2 + c).function(z,c)64sage: ff_m = fast_callable(f, vars=[z,c], domain=CDF)65sage: mandel(ff_m, CDF(1,1), 3)661.0 + 3.0*I6768"""69c = z70for i in range(iterations):71z = ff_m(z, c)72if z.abs() > 2: break73return z747576cpdef cellular(rule, int N):77"""78Cythonized helper function for the cellular_automata fractal.79Yields a matrix showing the evolution of a Wolfram's cellular automaton.80Based on work by Pablo Angulo.81http://wiki.sagemath.org/interact/misc#CellularAutomata8283INPUT:8485- `rule` -- determines how a cell's value is updated, depending on its neighbors86- `N` -- number of iterations8788TESTS::8990sage: from sage.interacts.library_cython import cellular91sage: rule = [1, 0, 1, 0, 0, 1, 1, 0]92sage: N = 393sage: print cellular(rule, N)94[[0 0 0 1 0 0 0 0]95[1 1 0 1 0 1 0 0]96[0 1 1 1 1 1 0 0]]97"""98from numpy import zeros99cdef int j, k, l100M=zeros((N, 2*N+2), dtype=int)101M[0,N]=1102103for j in range(1, N):104for k in range(0, 2*N):105l = 4 * M[j-1, k-1] + 2 * M[j-1, k] + M[j-1, k+1]106M[j,k] = rule[l]107return M108109110