-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.lisp
154 lines (129 loc) · 4.82 KB
/
example.lisp
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
;;; -*- coding:utf-8; mode:lisp -*-
(ql:quickload :clgplot)
;; Plot line
(clgp:plot '(1 2 3))
;; Plot sin function
(defparameter *x-list* (loop for i from (- pi) to pi by 0.1 collect i))
(clgp:plot (mapcar #'sin *x-list*))
;; Specify x values
(clgp:plot (mapcar #'sin *x-list*) :x-seq *x-list*)
;; Plot multiple functions
(clgp:plots (list (mapcar #'sin *x-list*)
(mapcar #'cos *x-list*))
:x-seqs (list *x-list* *x-list*))
;; Add tan
(clgp:plots (list (mapcar #'sin *x-list*)
(mapcar #'cos *x-list*)
(mapcar #'tan *x-list*))
:x-seqs (list *x-list* *x-list* *x-list*))
;; Add domain
(clgp:plots (list (mapcar #'sin *x-list*)
(mapcar #'cos *x-list*)
(mapcar #'tan *x-list*))
:x-seqs (list *x-list* *x-list* *x-list*)
:x-range (list (- pi) pi)
:y-range '(-1 1))
;; Add caption of axis
(clgp:plots (list (mapcar #'sin *x-list*)
(mapcar #'cos *x-list*)
(mapcar #'tan *x-list*))
:x-seqs (list *x-list* *x-list* *x-list*)
:x-range (list (- pi) pi)
:y-range '(-1 1)
:title-list '("sin" "cos" "tan")
:x-label "x"
:y-label "f(x)")
;; Output to PNG file
(clgp:plots (list (mapcar #'sin *x-list*)
(mapcar #'cos *x-list*)
(mapcar #'tan *x-list*))
:x-seqs (list *x-list* *x-list* *x-list*)
:x-range (list (- pi) pi)
:y-range '(-1 1)
:title-list '("sin" "cos" "tan")
:x-label "x"
:y-label "f(x)"
:output #P"/home/wiz/tmp/clgp-output2.png")
;; Other format
(clgp:plots (list (mapcar #'sin *x-list*)
(mapcar #'cos *x-list*)
(mapcar #'tan *x-list*))
:x-seqs (list *x-list* *x-list* *x-list*)
:x-range (list (- pi) pi)
:y-range '(-1 1)
:title-list '("sin" "cos" "tan")
:x-label "x"
:y-label "f(x)"
;; :PDF :EPS :EPS-MONOCHROME :PNG :PNG-1280X1024 :PNG-2560X1024 :PNG-MONOCHROME
:output-format :eps-monochrome
:output "/home/wiz/tmp/clgp-output.eps")
;; splot
(clgp:splot (lambda (x y) (+ (sin x) (cos y)))
*x-list* ; x
*x-list* ; y
)
;; splot from another view point
(clgp:splot (lambda (x y) (+ (sin x) (cos y)))
*x-list* ; x
*x-list* ; y
:view-point '(20 45) :z-scale 1.5)
;; splot map
(clgp:splot (lambda (x y) (+ (sin x) (cos y)))
*x-list* ; x
*x-list* ; y
:map t)
;; splot-matrix
(defparameter mat
(make-array '(20 20)
:initial-contents
(loop for i from (- pi) to (- pi 0.1) by (/ pi 10) collect
(loop for j from (- pi) to (- pi 0.1) by (/ pi 10) collect
(+ (sin i) (cos j))))))
(clgp:splot-matrix mat)
;; Histogram
;; Random sampling by Box-Muller method
(defun random-normal (&key (mean 0d0) (sd 1d0))
(let ((alpha (random 1.0d0))
(beta (random 1.0d0)))
(+ (* sd
(sqrt (* -2 (log alpha)))
(sin (* 2 pi beta)))
mean)))
(clgp:plot-histogram (loop repeat 3000 collect (random-normal)) ; samples
30 ; number of bin
)
;; Plot samples with probability density function
(defun pdf-normal (x &key (mu 0) (sd 1))
(flet ((square (x) (* x x)))
(/ (exp (- (/ (square (/ (- x mu) sd)) 2)))
(* (sqrt (* 2 pi)) sd))))
(clgp:plot-histogram-with-pdf (loop repeat 3000 collect (random-normal)) ; samples
30 ; number of bin
#'pdf-normal)
;; Style
(clgp:plot (mapcar #'sin *x-list*) :style 'lines)
(clgp:plot (mapcar #'sin *x-list*) :style 'points)
(clgp:plot (mapcar #'sin *x-list*) :style 'impulses)
;; Multiple styles
(let* ((rand-x-list (loop repeat 100 collect (- (random (* 2 pi)) pi)))
(rand-y-list (mapcar (lambda (x) (+ (sin x) (random-normal :sd 0.1d0))) rand-x-list)))
(clgp:plots (list (mapcar #'sin *x-list*)
rand-y-list)
:x-seqs (list *x-list* rand-x-list)
:style '(line point)))
;; Multiplot
(clgp:multiplot ()
(clgp:plot (mapcar #'sin *x-list*) :style 'lines)
(clgp:plot (mapcar #'sin *x-list*) :style 'points)
(clgp:plot (mapcar #'sin *x-list*) :style 'impulses))
(clgp:multiplot (:layout (2 2) :output "/tmp/multiplot.png" :output-format :png)
(clgp:plot (mapcar #'sin *x-list*) :style 'lines :key nil)
(clgp:plot (mapcar #'sin *x-list*) :style 'points :key nil)
(clgp:plot (mapcar #'sin *x-list*) :style 'impulses :key nil)
(clgp:plots (list (mapcar #'sin *x-list*)
(mapcar #'cos *x-list*)
(mapcar #'tan *x-list*))
:x-seqs (list *x-list* *x-list* *x-list*)
:x-range (list (- pi) pi)
:y-range '(-1 1)
:key nil))