Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
360 changes: 360 additions & 0 deletions Vectorplot_examples.wxm
Original file line number Diff line number Diff line change
@@ -0,0 +1,360 @@
/* [wxMaxima batch file version 1] [ DO NOT EDIT BY HAND! ]*/
/* [ Created with wxMaxima version 22.04.0 ] */
/* [wxMaxima: input start ] */
:lisp (defvar $hexversion 0.1)
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
:lisp (defun $hex2dec (h) (parse-integer h :radix 16))
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
:lisp (defun $dec2hex (h) (write-to-string h :base 16))
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
/* 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"$
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
/* 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"]$
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
/* default palette used */
GlobalPalette : Palette_RGB$
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
/* ------------------------------------------------------------------------- */
/* ----- 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])
)$
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
/* ------------------------------------------------------------------------- */
/* ----- 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))
)$
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
/* ------------------------------------------------------------------------- */
/* ----- -------- */
/* ------------------------------------------------------------------------- */
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)
)$
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
/* 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)
)$
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
Vectorplot([y,-x],[-2,2],[-2,2],vectorpoints=[10,10],colorpalette=[green,red,blue,black,gold],vectorscale=[0.3,0.3]);
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
Vectorplot([y,-x],[-2,2],[-2,2],vectorpoints=[10,10],colorpalette=Palette_BGR,vectorscale=[0.3,0.3]);
/* [wxMaxima: input end ] */


/* [wxMaxima: input start ] */
Vectorplot([y,-x],[-2,2],[-2,2],vectorpoints=[10,10],colorpalette=["#fffcf6","#fff7db","#fff4c2","#feecae","#f8ca8c","#f0a848","#c07860","#a86060","#784860","#604860"],vectorscale=[0.3,0.3]);
/* [wxMaxima: input end ] */



/* Old versions of Maxima abort on loading files that end in a comment. */
"Created with wxMaxima 22.04.0"$