Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

r""" 

Library of cythonized methods 

  

AUTHORS: 

  

- Harald Schilly (2011-01-16): initial version (#9623) partially based on work by Lauri Ruotsalainen 

  

""" 

  

#***************************************************************************** 

# Copyright (C) 2011 Harald Schilly <harald.schilly@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

  

from sage.misc.all import prod 

  

  

cpdef julia(ff_j, z, int iterations): 

""" 

Helper function for the Julia Fractal interact example. 

  

INPUT: 

  

- ``ff_j`` -- fast callable for the inner iteration 

- ``z`` -- complex number 

- ``iterations`` -- number of loops 

  

TESTS:: 

  

sage: from sage.interacts.library_cython import julia 

sage: z = var('z') 

sage: c_real, c_imag = 1, 1 

sage: f = symbolic_expression(z**2 + c_real + c_imag * CDF.gen()).function(z) 

sage: ff_m = fast_callable(f, vars=[z], domain=CDF) 

sage: julia(ff_m, CDF(1,1), 3) 

1.0 + 3.0*I 

""" 

for i in range(iterations): 

z = ff_j(z) 

if z.abs() > 2: break 

return z 

  

cpdef mandel(ff_m, z, int iterations): 

""" 

Helper function for the Mandelbrot Fractal interact example. 

  

INPUT: 

  

- ``ff_m`` -- fast callable for the inner iteration 

- ``z`` -- complex number 

- ``iterations`` -- number of loops 

  

TESTS:: 

  

sage: from sage.interacts.library_cython import mandel 

sage: z, c = var('z, c') 

sage: f = symbolic_expression(z**2 + c).function(z,c) 

sage: ff_m = fast_callable(f, vars=[z,c], domain=CDF) 

sage: mandel(ff_m, CDF(1,1), 3) 

1.0 + 3.0*I 

  

""" 

c = z 

for i in range(iterations): 

z = ff_m(z, c) 

if z.abs() > 2: break 

return z 

  

  

cpdef cellular(rule, int N): 

""" 

Cythonized helper function for the cellular_automata fractal. 

  

Yields a matrix showing the evolution of a Wolfram's cellular automaton. 

Based on work by Pablo Angulo. 

http://wiki.sagemath.org/interact/misc#CellularAutomata 

  

INPUT: 

  

- ``rule`` -- determines how a cell's value is updated, depending 

on its neighbors 

- ``N`` -- number of iterations 

  

TESTS:: 

  

sage: from sage.interacts.library_cython import cellular 

sage: rule = [1, 0, 1, 0, 0, 1, 1, 0] 

sage: N = 3 

sage: print(cellular(rule, N)) 

[[0 0 0 1 0 0 0 0] 

[1 1 0 1 0 1 0 0] 

[0 1 1 1 1 1 0 0]] 

""" 

from numpy import zeros 

cdef int j, k, l 

M=zeros((N, 2*N+2), dtype=int) 

M[0,N]=1 

  

for j in range(1, N): 

for k in range(0, 2*N): 

l = 4 * M[j-1, k-1] + 2 * M[j-1, k] + M[j-1, k+1] 

M[j,k] = rule[l] 

return M