-
Notifications
You must be signed in to change notification settings - Fork 1
/
vector.mac
316 lines (277 loc) · 10.5 KB
/
vector.mac
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/* Vectorplot */
kill(all);
load(draw);
load("hex.lisp");
/* define the names of the hexadecimal colors */
white: "#ffffff"$
black: "#000000"$
gray0: "#000000"$
grey0: "#000000"$
gray10: "#1a1a1a"$
grey10: "#1a1a1a"$
gray20: "#333333"$
grey20: "#333333"$
gray30: "#4d4d4d"$
grey30: "#4d4d4d"$
gray40: "#666666"$
grey40: "#666666"$
gray50: "#7f7f7f"$
grey50: "#7f7f7f"$
gray60: "#999999"$
grey60: "#999999"$
gray70: "#b3b3b3"$
grey70: "#b3b3b3"$
gray80: "#cccccc"$
grey80: "#cccccc"$
gray90: "#e5e5e5"$
grey90: "#e5e5e5"$
gray100: "#ffffff"$
grey100: "#ffffff"$
gray: "#bebebe"$
grey: "#c0c0c0"$
lightgray: "#d3d3d3"$
lightgrey: "#d3d3d3"$
darkgray: "#a0a0a0"$
darkgrey: "#a0a0a0"$
red: "#ff0000"$
lightred: "#f03232"$
darkred: "#8b0000"$
yellow: "#ffff00"$
darkyellow: "#c8c800"$
green: "#00ff00"$
lightgreen: "#90ee90"$
darkgreen: "#006400"$
springgreen: "#00ff7f"$
forestgreen: "#228b22"$
seagreen: "#2e8b57"$
blue: "#0000ff"$
lightblue: "#add8e6"$
darkblue: "#00008b"$
midnightblue: "#191970"$
navy: "#000080"$
mediumblue: "#0000cd"$
royalblue: "#4169e1"$
skyblue: "#87ceeb"$
cyan: "#00ffff"$
lightcyan: "#e0ffff"$
darkcyan: "#00eeee"$
magenta: "#ff00ff"$
lightmagenta: "#f055f0"$
darkmagenta: "#c000ff"$
turquoise: "#40e0d0"$
lightturquoise: "#afeeee"$
darkturquoise: "#00ced1"$
pink: "#ffc0c0"$
lightpink: "#ffb6c1"$
darkpink: "#ff1493"$
coral: "#ff7f50"$
lightcoral: "#f08080"$
orangered: "#ff4500"$
salmon: "#fa8072"$
lightsalmon: "#ffa070"$
darksalmon: "#e9967a"$
aquamarine: "#7fffd4"$
khaki: "#f0e68c"$
darkkhaki: "#bdb76b"$
goldenrod: "#ffc020"$
lightgoldenrod: "#eedd82"$
darkgoldenrod: "#b8860b"$
gold: "#ffd700"$
beige: "#f5f5dc"$
brown: "#a52a2a"$
orange: "#ffa500"$
darkorange: "#c04000"$
violet: "#ee82ee"$
darkviolet: "#9400d3"$
plum: "#dda0dd"$
purple: "#c080ff"$
/* define some palettes */
Palette_RGB : [red,green,blue]$
Palette_BGR : [blue,green,red]$
Palette_RB : [red,blue]$
Palette_BR : [blue,red]$
Palette_WB : [white,black]$
Palette_01 : ["#fffcf6","#fff7db","#fff4c2","#feecae","#f8ca8c","#f0a848","#c07860","#a86060","#784860","#604860"]$
/* default palette used */
GlobalPalette : Palette_RGB$
/* ------------------------------------------------------------------------- */
/* ----- convert rgb hex number (e.g. FFFFFF) to 3 rgb numbers [R,G,B] ----- */
/* ------------------------------------------------------------------------- */
splithex(hex):=block([],
if not stringp(hex) then hex:string(hex),
R:hex2dec(substring(hex,1,3)),
G:hex2dec(substring(hex,3,5)),
B:hex2dec(substring(hex,5,7)),
/*print("RGB = (",R,",",G,",",B,")"),*/
return([R,G,B])
)$
/* ------------------------------------------------------------------------- */
/* ----- interpolate hexadecimal colors ------------------------------------ */
/* ------------------------------------------------------------------------- */
colorinterpolate(rl,color1,color2):=block([],
if (color1 = color2) then return(color1),
/* split the hexadecimal color hRGB into R,G,B */
/* if first character is a "#", then strip it */
if (substring(color1,1,2)="#") then color1:substring(color1,2),
if (substring(color2,1,2)="#") then color2:substring(color2,2),
RGB1 : splithex(color1),
RGB2 : splithex(color2),
R1:RGB1[1],
G1:RGB1[2],
B1:RGB1[3],
R2:RGB2[1],
G2:RGB2[2],
B2:RGB2[3],
/* interpolate between the colors */
R:round(R1+rl*(R2-R1)),
G:round(G1+rl*(G2-G1)),
B:round(B1+rl*(B2-B1)),
/* convert the colors back to hex */
Rh:dec2hex(R),
Gh:dec2hex(G),
Bh:dec2hex(B),
if(slength(Rh)=1) then Rh:concat("0",Rh),
if(slength(Gh)=1) then Gh:concat("0",Gh),
if(slength(Bh)=1) then Bh:concat("0",Bh),
return(concat("#",Rh,Gh,Bh))
)$
/* ------------------------------------------------------------------------- */
/* ----- -------- */
/* ------------------------------------------------------------------------- */
v2hex(v) := block([p, nc],
/* write here your palette of colors */
/*1 2 3 4 5 6 7 8 9 10 */
p: GlobalPalette,
if not listp(p) then return(p), /* if there is only one color, and it's not in a list*/
nc: length(p),
if nc=1 then return(p[1]), /* there is only 1 color in the palette */
/* map range to color palette range */
ratio: 1 + (nc-1)*(v-minv)/(maxv-minv),
fr: floor(ratio),
fc: ceiling(ratio),
rl: ratio-fr,
color1:p[fr],
color2:p[fc],
colorinterpolate(rl,color1,color2)
)$
/* streamplot */
/* ------------------------------------------------------------------------- */
/* draw a vector plot*/
/* input: fxy: list [fx,fy] with the x and y parts of the vector */
/* xrange,yrange: list [a,b] with the range in x and y direction */
/* vectorpoints: nr of vectors in x and y direction */
/* optional argument : vectorscale */
/* ------------------------------------------------------------------------- */
Vectorplot(fxy,xrangeV,yrangeV,[opt]):=block([x,y,fx,fy,Nx,Ny,Nxy,x1,x2,y1,y2,v,vectorscale],
/* ----- which optional arguments do we want: */
/* make xynr optional, default 10x10 */
/* scale = 1.0 scales the final vector size scale=none means all vectors have the same size*/
/* we should also have a user-defined scaling from Vmin to Vmax */
/* we should also have a user-defined scaling for the colors */
/* color = black,red,green,blue, #ffffff, gives all arrows a constant color */
/* staggered, for staggered grid */
/* grid = list of gridpoints */
/*if length(opt) = 1 then scale:opt[1] else if length(opt)>1 then error("too many arguments found:",opt),*/
vectorscale : assoc(vectorscale,opt,1.0), /* scaling of vectors */
vectorpoints: assoc(vectorpoints,opt,[10,10]), /* [Nx,Ny] number of vectors in x and y direction*/
terminalsetting: assoc(terminal,opt,wxt), /* set terminal (screen, png file, etc)*/
filename : assoc(filename,opt,"maxima_out"),/* filename of figure when terminal is a file (png, etc) */
user_preamble: assoc(user_preamble,opt,"set style line 11 lc rgb '#555555' lt 1; set border back ls 11 lw 2 "),
xrange: assoc(xrange,opt,1.2*xrangeV), /* the actual xrange of the figure (shown on the axes), default=1.2*xrangeV */
yrange: assoc(yrange,opt,1.2*yrangeV), /* note that xrangeV is the range in which the vectors are being plotted. */
print("terminal = ",terminalsetting),
Lv: sort(listofvars(fxy)), /* we assume that the variable on the x-axis is also alphabetically first */
if length(Lv)#2 then error("Vectorplot needs 2 variables, found ",Lv),
x: assoc(xvar,opt,Lv[1]), /* xvar determines the variable on the x-axis*/
y: assoc(yvar,opt,Lv[2]), /* yvar determines the variable on the y-axis*/
/* split the list of vectors */
fx:fxy[1], fy:fxy[2],
/* define fx,fy as functions of x,y */
define(dx(x,y),fx),
define(dy(x,y),fy),
/* by default, color by norm */
colorfunction : assoc(colorfunction,opt,sqrt(fx*fx+fy*fy)),
print("colorfunction = ",colorfunction),
/* vecColors is the local norm(strength) of the vector */
define(vecColors(x,y),colorfunction),
print("veccolors = ",fundef(vecColors)),
colorpalette : assoc(colorpalette,opt,GlobalPalette),
print("palette = ",colorpalette),
oldGlobalPalette:GlobalPalette,
GlobalPalette : colorpalette,
/* the plot domain to plot the vectors in is (x1,x2) - (y1,y2) */
x1:xrangeV[1], x2:xrangeV[2],
y1:yrangeV[1], y2:yrangeV[2],
AspectRatio : (y2-y1)/(x2-x1),
Nx:vectorpoints[1]-1, Ny:vectorpoints[2]-1,
Nxy:(Nx+1)*(Ny+1),
/* smallest vector */
/* largest vector should have the size of a cell*/
if listp(vectorscale) then (
Vmin : vectorscale[1],
Vmax : vectorscale[2],
vectorscale : 1.0
) else (
Vmax : min( (x2-x1)/Nx, (y2-y1)/Ny),
Vmin : 0.05*Vmax /* nonzero Vmin*/
),
print("Vmin,Vmax=",Vmin,Vmax),
/* initialization of the minimum and maximum values of the norm */
Nmin:sqrt(dx(x1,y1)*dx(x1,y1)+dy(x1,y1)*dy(x1,y1)), Nmax:Nmin,
/* norm contains the norm of the vector, used as the size of the arrow */
norm:makelist((_x:(x2-x1)*(mod(i-1,(Nx+1))/Nx)+x1,
_y:(y2-y1)*(quotient(i-1,(Nx+1))/Ny)+y1,
tmpx:errcatch(dx(_x,_y)),
tmpy:errcatch(dy(_x,_y)),
if length(tmpx)=0 then _dx:0 else _dx:tmpx[1],
if length(tmpy)=0 then _dy:0 else _dy:tmpy[1],
R:max(float(sqrt(_dx*_dx + _dy*_dy)),1.0e-6), /* make sure that the norm is nonzero */
Nmin:min(Nmin,R),Nmax:max(Nmax,R),
R),i,1,Nxy),
/* Now scale the norm from [Nmin,Nmax] -> [0,Vmax] */
/* (norm-Nmin)/(Nmax-Nmin) = (p-Vmin)/(Vmax-Vmin) */
/*
print("Nmin,Nmax = ",Nmin,Nmax),
vectorclip : assoc(vectorclip,opt,[Nmin,Nmax]), /* clip the vector size */
("vectorclip = ",vectorclip[1],vectorclip[2]),
*/
normp:(norm-Nmin)*(Vmax-Vmin)/(Nmax-Nmin) + Vmin,
print("Nmin,Nmax = ",Nmin,Nmax,vectorscale),
dxdy:makelist((_x:(x2-x1)*(mod(i-1,(Nx+1))/Nx)+x1,
_y:(y2-y1)*(quotient(i-1,(Nx+1))/Ny)+y1,
tmpx:errcatch(dx(_x,_y)),
tmpy:errcatch(dy(_x,_y)),
if length(tmpx)=0 then _dx:0 else _dx:tmpx[1],
if length(tmpy)=0 then _dy:0 else _dy:tmpy[1],
[float(_dx),float(_dy)]),i,1,Nxy),
/* scale dxdy with the norm */
/* also, use scale factor in case you want larger/smaller than default */
/* also, clipping activated */
/*dxdy:vectorscale*min(max(normp*dxdy/norm,vectorclip[1]),vectorclip[2]),*/
dxdy:vectorscale*normp*dxdy/norm,
xy:makelist((_x:(x2-x1)*(mod(i-1,(Nx+1))/Nx)+x1,
_y:(y2-y1)*(quotient(i-1,(Nx+1))/Ny)+y1,
[float(_x),float(_y)]),i,1,Nxy),
v1:xy-dxdy/2.0,
v2:dxdy,
/* find minimum and maximum for colors */
v_values: makelist(apply(vecColors, xy[i]),i,1,Nxy),
minv: lmin(v_values),
maxv: lmax(v_values),
print("minv,maxv = ",minv,maxv),
veclist:makelist(['color = v2hex(apply(vecColors, xy[i])), vector(v1[i],v2[i])],i,1,Nxy),
GlobalPalette:oldGlobalPalette,
/* draw the list of vectors */
header : ['terminal = terminalsetting,
'file_name = filename,
'font_size = 16,
'dimensions = [800,AspectRatio*800],
'xrange = [xrange[1],xrange[2]],
'yrange = [yrange[1],yrange[2]],
'line_width = 2,
'head_angle = 20,
'head_length = 0.30*Vmax,
'user_preamble = concat("set term ",terminalsetting," enhance font \"Charter,16\"; ",user_preamble)
],
draw2d(header, veclist)
)$