File: complex_dynamics.lisp

package info (click to toggle)
maxima 5.47.0-9
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 193,104 kB
  • sloc: lisp: 434,678; fortran: 14,665; tcl: 10,990; sh: 4,577; makefile: 2,763; ansic: 447; java: 328; python: 262; perl: 201; xml: 60; awk: 28; sed: 15; javascript: 2
file content (226 lines) | stat: -rw-r--r-- 10,362 bytes parent folder | download | duplicates (4)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
;; complex_dynamics.lisp - functions julia, mandelbrot and rk
;;   
;; Copyright (C) 2006-2021 Jaime E. Villate <villate@fe.up.pt>
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
;; MA 02110-1301, USA.
;;

(in-package :maxima)
(macsyma-module dynamics)

;; Function $mandelbrot displays the Mandelbrot set
;; on the region: xmin < x <xmax, ymin < y <ymax.

(defmfun $mandelbrot (&rest extra-options)
  (let (plot output-file (options (copy-tree *plot-options*))
        a b c d e num  dx dy x xmax xmin y ymax ymin m nx ny)
    (setf (getf options :type) "plot2d")
    (unless (getf options :x) (setf (getf options :x) '(-2 2)))
    (unless (getf options :y) (setf (getf options :y) '(-2 2)))
    (unless (getf options :iterations) (setf (getf options :iterations) 9))
    (unless (getf options :xlabel) (setf (getf options :xlabel) "x"))
    (unless (getf options :ylabel) (setf (getf options :ylabel) "y"))
    (unless (member :color_bar options) (setf (getf options :color_bar) t))
    (setf (getf options :palette)
          '(((mlist) $gradient $magenta $violet $blue $cyan $green $yellow
            $orange $red $brown $black)))
                                          
    ;; Parses the options given in the command line
    (setq options (plot-options-parser extra-options options))
    (unless (getf options :yx_ratio) (setf (getf options :same_xy) t))
    (setq xmin (car (getf options :x))) 
    (setq xmax (cadr (getf options :x)))
    (setq ymin (car (getf options :y))) 
    (setq ymax (cadr (getf options :y)))
    (setq m (getf options :iterations))
    (setq nx (or (car (getf options :grid)) 30))
    (setq ny (or (cadr (getf options :grid)) 30))
    (setq dx (/ (rationalize (- xmax xmin)) nx)) ; x incr. per pixel
    (setq dy (/ (rationalize (- ymax ymin)) ny)) ; y incr. per pixel

    ;; Creates the object that will be passed to the external graphic program
    (setq plot (make-instance 'gnuplot-plot))
    (if (eq (getf options :plot_format) '$gnuplot_pipes)
        (setf (slot-value plot 'pipe) T)
        (setf (getf options :plot_format) '$gnuplot))

    (setq output-file (plot-preamble plot options))
    (setf
     (slot-value plot 'data)
     (concatenate
      'string
      (slot-value plot 'data)
      (with-output-to-string (st)            
        (format st "set palette ~a~%"
                (gnuplot-palette (rest (first (getf options :palette)))))
        (format st "unset key~%")
        (format st "plot '-' with image~%")
        ;; iterates through all grid points
        (dotimes (i ny) 
          (setq y (+ ymin (/ dy 2) (* i dy)))
          (dotimes (j nx)
            (setq x (+ xmin (/ dx 2) (* j dx)))
            (setq a 0) (setq b 0)
            (setq num m)
            (dotimes (l m)
              (setq c (* a a))
              (setq d (* b b))
              (setq e (* 2 a b))
              (when (> (+ c d) 4) (progn (setq num l) (return)))
              (setq a (+ (float x) (- c d)))
              (setq b (+ (float y) e)))
            (format st "~f ~f ~d~%" x y num)))
        (format st "e~%"))))
    (plot-shipout plot options output-file)))

;; Function $julia(x,y) displays the Julia set for the
;; point (x,y) of the complex plane, on the region: xmin < x <xmax,
;; ymin < y <ymax.

(defmfun $julia (x y &rest extra-options)
  (let (plot output-file (options (copy-tree *plot-options*))
        num dx dy xmax xmin ymax ymin a b c d e a0 b0 m nx ny)
    (setf (getf options :type) "plot2d")
    (unless (getf options :x) (setf (getf options :x) '(-2 2)))
    (unless (getf options :y) (setf (getf options :y) '(-2 2)))
    (unless (getf options :iterations) (setf (getf options :iterations) 9))
    (unless (getf options :xlabel) (setf (getf options :xlabel) "x"))
    (unless (getf options :ylabel) (setf (getf options :ylabel) "y"))
    (unless (member :color_bar options) (setf (getf options :color_bar) t))
    (setf (getf options :palette)
          '(((mlist) $gradient $magenta $violet $blue $cyan $green $yellow
            $orange $red $brown $black)))
                                          
    ;; Parses the options given in the command line
    (setq options (plot-options-parser extra-options options))
    (unless (getf options :yx_ratio) (setf (getf options :same_xy) t))
    (setq xmin (car (getf options :x))) 
    (setq xmax (cadr (getf options :x)))
    (setq ymin (car (getf options :y))) 
    (setq ymax (cadr (getf options :y)))
    (setq m (getf options :iterations))
    (setq nx (or (car (getf options :grid)) 30))
    (setq ny (or (cadr (getf options :grid)) 30))
    (setq dx (/ (rationalize (- xmax xmin)) nx)) ; x incr. per pixel
    (setq dy (/ (rationalize (- ymax ymin)) ny)) ; y incr. per pixel

    ;; Creates the object that will be passed to the external graphic program
    (setq plot (make-instance 'gnuplot-plot))
    (if (eq (getf options :plot_format) '$gnuplot_pipes)
        (setf (slot-value plot 'pipe) T)
        (setf (getf options :plot_format) '$gnuplot))

    (setq output-file (plot-preamble plot options))
    (setf
     (slot-value plot 'data)
     (concatenate
      'string
      (slot-value plot 'data)
      (with-output-to-string (st)
        (format st "set palette ~a~%"
                (gnuplot-palette (rest (first (getf options :palette)))))
        (format st "unset key~%")
        (format st "plot '-' with image~%")
        
        ;; iterates through all grid points
        (dotimes (i ny) 
          (setq b0 (+ ymin (/ dy 2) (* i dy)))
          (dotimes (j nx)
            (setq a0 (+ xmin (/ dx 2) (* j dx)))
            (setq a (float a0))
            (setq b (float b0))
            (setq num m)
            (dotimes (l m)
              (setq c (* a a))
              (setq d (* b b))
              (setq e (* 2 a b))
              (when (> (+ c d) 4) (progn (setq num l) (return)))
              (setq a (+ x (- c d)))
              (setq b (+ y e)))
            (format st "~f ~f ~d~%" a0 b0 num)))
        (format st "e~%"))))
    (plot-shipout plot options output-file)))

;; Function $rk implements the 4th order Runge-Kutta numerical method.
;;  exprs:   an expression or maxima list with n expressions
;;  vars:    name of (or list of names of) the independent variable(s)
;;  initial: initial value for the variable (or list of initial values)
;;  domain:  maxima list with four elements (name of the independent
;;           variable, its initial and final values and its increment)
(defmfun $rk (exprs vars initial domain
            &aux d u fun k1 k2 k3 k4 r1 r2 r3 traj r
              (it (mapcar #'coerce-float (cddr domain))))
  (unless ($listp exprs) (setq exprs `((mlist) ,exprs)))
  (unless ($listp initial) (setq initial `((mlist) ,initial)))
  (unless ($listp vars) (setq vars `((mlist) ,vars)))
  (dolist (var (cdr vars))
    (unless (symbolp var)
      (merror (intl:gettext "rk: variable name expected; found: ~M") var)))
  (unless (symbolp (cadr domain))
    (merror (intl:gettext "rk: variable name expected; found: ~M")
            (cadr domain)))
  (setq vars (concatenate 'list '((mlist)) (list (cadr domain)) (cdr vars)))
  (setq r (concatenate 'list `(,(car it)) (mapcar #'coerce-float (cdr initial))))
  (setq fun (mapcar #'(lambda (x) (coerce-float-fun x vars)) (cdr exprs)))
  (setq d (/ (- (cadr it) (car it)) (caddr it)))
  (setq traj (list (cons '(mlist) r)))
  (do ((m 1 (1+ m))) ((> m d))
    (ignore-errors
      (setq k1 (mapcar #'(lambda (x) (apply x r)) fun))
      (setq r1 (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* (/ (caddr it) 2) x)) k1)))
      (push (+ (car r) (/ (caddr it) 2)) r1)
      (setq k2 (mapcar #'(lambda (x) (apply x r1)) fun))
      (setq r2 (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* (/ (caddr it) 2) x)) k2)))
      (push (+ (car r) (/ (caddr it) 2)) r2)
      (setq k3 (mapcar #'(lambda (x) (apply x r2)) fun))
      (setq r3 (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* (caddr it) x)) k3)))
      (push (+ (car r) (caddr it)) r3)
      (setq k4 (mapcar #'(lambda (x) (apply x r3)) fun))
      (setq u (map 'list #'+
                   (mapcar #'(lambda (x) (* 1/6 x)) k1)
                   (mapcar #'(lambda (x) (* 1/3 x)) k2)
                   (mapcar #'(lambda (x) (* 1/3 x)) k3)
                   (mapcar #'(lambda (x) (* 1/6 x)) k4)))
      (setq r 
            (concatenate 'list
                         `(,(+ (car it) (* m (caddr it))))
                         (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* (caddr it) x)) u))))
      (push (cons '(mlist) r) traj)))
  (when (< (car r) (cadr it))
    (let ((s (- (cadr it) (car r))))
      (ignore-errors
        (setq k1 (mapcar #'(lambda (x) (apply x r)) fun))
        (setq r1 (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* (/ s 2) x)) k1)))
        (push (+ (car r) (/ s 2)) r1)
        (setq k2 (mapcar #'(lambda (x) (apply x r1)) fun))
        (setq r2 (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* (/ s 2) x)) k2)))
        (push (+ (car r) (/ s 2)) r2)
        (setq k3 (mapcar #'(lambda (x) (apply x r2)) fun))
        (setq r3 (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* s x)) k3)))
        (push (+ (car r) s) r3)
        (setq k4 (mapcar #'(lambda (x) (apply x r3)) fun))
        (setq u (map 'list #'+
                     (mapcar #'(lambda (x) (* 1/6 x)) k1)
                     (mapcar #'(lambda (x) (* 1/3 x)) k2)
                     (mapcar #'(lambda (x) (* 1/3 x)) k3)
                     (mapcar #'(lambda (x) (* 1/6 x)) k4)))
        (setq r 
              (concatenate 'list
                           `(,(cadr it))
                           (map 'list #'+ (cdr r) (mapcar #'(lambda (x) (* s x)) u))))
        (push (cons '(mlist) r) traj))))
  (cons '(mlist) (nreverse traj)))