**> ** |
**InequalityGraphics := module()
**
export inequalityplot, complexinequalityplot;
option package;
inequalityplot:=proc()
local ineq,ineqset,xx,yy,phaseSelection,phase1,phase2,phases,var1,
var2,xmin,xmax,ymin,ymax,dx,dy,jumpPrecision,
findJump,reg,c,p,x1,y1,p1List,p2List,aux1,aux2,jump1,
jump2,eq,cc,ops,fc,ec,ls,fb,fp,lp,llss;
if nargs<3 then
ERROR(`expecting 3 arguments, got 0.
This commmand also incorporates the options:
(1) exludedcolor=yellow,
(2) feasiblecolor=red,
(3) linespoints=350,
(4) feasiblepoints=15 and
(5) thickness=1.
Examples:
<1> inequalityplot(x^2+y^2<1,x=-1..1,y=-1..1),
<2> inequalityplot(x^2+y^2<1,x=-1..1,y=-1..1,
excludedcolor=white,feasiblecolor=cyan)`)
fi:
ineq:=`if`(whattype(args[1])=set,convert(args[1],`and`),args[1]):
xx:=args[2]:yy:=args[3]:
if whattype(args[1])=set
then ineqset:=args[1]
elif whattype(ineq)=`<` or whattype(ineq)=`>` or
whattype(ineq)=`<=` or whattype(ineq)=`>=`
then ineqset:={ineq}
else ineqset:=convert(ineq,set)
fi:
var1:=lhs(xx):var2:=lhs(yy):
if not(depends(ineq,var1) and depends(ineq,var2)) then
ERROR(ineq,`doesn't depend on`,var1,`and`,var2)
fi:
ops:=[args[4..nargs]]:
if not hasoption(ops,feasiblecolor,fc,'ops')
then fc:=COLOR(RGB,.70,.70,1.0) fi:
if not hasoption(ops,excludedcolor,ec,'ops')
then ec:=COLOR(RGB,.20,.20,.20) fi:
if not hasoption(ops,feasiblepoints,fp,'ops')
then fp:=15 fi:
if not hasoption(ops,linespoints,lp,'ops')
then lp:=350 fi:
phaseSelection:=unapply(piecewise(ineq,1,2),var1,var2):
phase1:=(a,b)->1:phase2:=(a,b)->0:phases:=[phase1,phase2]:
xmin:=lhs(rhs(xx)):
xmax:=rhs(rhs(xx)):
ymin:=lhs(rhs(yy)):
ymax:=rhs(rhs(yy)):
dx:=(xmax-xmin)/(fp-1):
dy:=(ymax-ymin)/(fp-1):
jumpPrecision:=.02*min(dx,dy):
findJump:=(l,r)->
`if`(evalm((l-r)&*(l-r))<jumpPrecision^2,.5*(l+r),
`if`(phaseSelection(op(l))=phaseSelection(op(.5*(l+r))),
findJump(.5*(l+r),r),findJump(l,.5*(l+r))
)
):
reg:=[]:
for y1 from ymin by dy while y1<=ymax-dy do
for x1 from xmin by dx while x1<=xmax-dx do
c(1):=[x1,y1]:
c(2):=[x1+dx,y1]:
c(3):=[x1+dx,y1+dy]:
c(4):=[x1,y1+dy]:
p(1):=phaseSelection(op(c(1))):
p(2):=phaseSelection(op(c(2))):
p(3):=phaseSelection(op(c(3))):
p(4):=phaseSelection(op(c(4))):
if p(1)=p(2) and p(2)=p(3) and p(3)=p(4)
then
reg:=[op(reg),
POLYGONS(
[seq(`if`(phases[p(1)](op(c(n)))=1,c(n),NULL),
n=1..4)],
op(convert(color=fc,PLOToptions)))]
else
p1List:=[1]:
if p(2)=p(1) then
p1List:=[op(p1List),2]:
if p(3)=p(1) then
p1List:=[op(p1List),3]
fi
fi:
if p(4)=p(1) then
p1List:=[4,op(p1List)]:
if p(3)=p(1) then
p1List:=[3,op(p1List)]
fi
fi:
p2List:=convert({1,2,3,4} minus {op(p1List)},list):
jump1:=findJump(c(p1List[nops(p1List)]),
c(p2List[1])):
jump2:=findJump(c(p2List[nops(p2List)]),
c(p1List[1])):
aux1:=[jump2,op(map(c,p1List)),jump1]:
aux2:=[jump1,op(map(c,p2List)),jump2]:
reg:=[op(reg),
POLYGONS(
[seq(
`if`(phases[p(1)](op(aux1[i]))=1,aux1[i],NULL),
i=1..nops(aux1))],
op(convert(color=fc,PLOToptions))),
POLYGONS(
[seq(
`if`(phases[p(p2List[1])](op(aux2[i]))=1,aux2[i],NULL),
i=1..nops(aux2))],
op(convert(color=fc,PLOToptions)))
]
fi
od
od:
ls:=(is)->
piecewise(
whattype(is)=`<` or whattype(is)=`>`,2,
whattype(is)=`<=` or whattype(is)=`>=`,1
):
llss:=map(ls,[op(ineqset)]):
eq:=
`if`(whattype(ineqset)=set,
[seq(convert(ineqset[i],equality),i=1..nops(ineqset))],
[convert(ineq,equality)]
):
cc:=[seq(implicitplot(eq[i],xx,yy,color=black,numpoints=lp),
i=1..nops(eq))]:
PLOT(op(reg),
seq(
CURVES(op(op(cc[i])[1]),LINESTYLE(llss[i])),i=1..nops(cc)),
op(convert(ops,PLOToptions)),
POLYGONS([[xmin,ymin],[xmin,ymax],[xmax,ymax],
[xmax,ymin],[xmin,ymin]],
op(convert(color=ec,PLOToptions))),
SCALING(CONSTRAINED),STYLE(PATCHNOGRID)):
end proc;
complexinequalityplot:=proc()
local ineq,var,Revar,Imvar,ops;
if nargs<2 then
ERROR(`expecting 2 arguments, got 0.
This commmand also incorporates the options:
(1) exludedcolor=yellow,
(2) feasiblecolor=red,
(3) linespoints=350,
(4) feasiblepoints=15,
(5) linescolor=black and
(6) thickness=1.
Examples:
<1> complexinequalityplot(abs(z)<1,z=(-1..1,-1..1)),
<2> complexinequalityplot(abs(z)<1,z=(-1..1,-1..1),
excludedcolor=white,feasiblecolor=cyan)`)
fi:
var:=lhs(args[2]):
Revar:=rhs(args[2])[1]:
Imvar:=rhs(args[2])[2]:
ineq:=subs(var=x+I*y,args[1]):
ops:=args[3..nargs]:
inequalityplot(ineq,x=Revar,y=Imvar,ops)
end proc;
end module: |