Maxima さん,bug ってますよ.

Maxima 5.39.0 http://maxima.sourceforge.net
using Lisp CMU Common Lisp 21b (21B Unicode)
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) assume(a>0,b>0);
(%o1)                           [a > 0, b > 0]
(%i2) assume(a*b<0);
(%o2)                           [inconsistent]
(%i3) forget(a>0,b>0);
(%o3)                           [a > 0, b > 0]
(%i4) assume(a*b<0);
(%o4)                              [a b < 0]
(%i5) assume(a>0,b>0);
(%o5)                           [a > 0, b > 0]
(%i6) facts();
(%o6)                       [0 > a b, a > 0, b > 0]
(%i7) forget(a>0,b>0,a*b<0);
(%o7)                       [a > 0, b > 0, a b < 0]
(%i8) sqrt(x)>0;
(%o8)                             sqrt(x) > 0
(%i9) is(%);
(%o9)                               unknown
(%i10) sqrt(x)>=0;
(%o10)                           sqrt(x) >= 0
(%i11) is(%);
(%o11)                               true

nns シリーズ更新

・一変数半代数系の高速ソルバー cineqs2 の正式公開.
・solvex から呼ぶ不等式ソルバー sineq も一変数なら高速になりました(cineqs2 の一世代前のアルゴリズムなので少し遅いですが).

load(qepmax)$
showtime:on$
display2d:false$
prtoff():=kill(prt)$
prton():=(prt([x]):=apply(print,x))$
/*********/
/* tools */
/*********/
/* compare length function */
comparelength(f,g):=length(f)<=length(g)$
comparelength2(f,g):=length(g)<=length(f)$
/* cnf dnf */
infix(Or,60,40)$
infix(And,60,40)$
matchdeclare([aa,bb,cc,dd,ee,ff,gg,aa1,bb1,cc1,dd1,ee1,ff1,gg1],true)$
defrule(ru05r, (aa) Or ((bb) And (cc)),
 ((aa) Or (bb)) And ((aa) Or (cc)))$
defrule(ru08r, ((bb) And (cc)) Or (aa),
 ((aa) Or (bb)) And ((aa) Or (cc)))$
defrule(ru05a, (aa) And ((bb) Or (cc)),
 ((aa) And (bb)) Or ((aa) And (cc)))$
defrule(ru08a, ((bb) Or (cc)) And (aa),
 ((aa) And (bb)) Or ((aa) And (cc)))$
defrule(ru00r, (aa) Or (bb), (aa) %or (bb))$
defrule(ru00a, (aa) And (bb), (aa) %and (bb))$
orand2orand(f):=block([g],g:f,
if atom(g) then return(g),
if op(g)="%or" then g:xreduce("Or",args(g)),
if op(g)="%and" then g:xreduce("And",args(g)),g)$
cnf(f):=applyb2(applyb2(scanmap(orand2orand,f),ru05r,ru08r),ru00r,ru00a)$
dnf(f):=applyb2(applyb2(scanmap(orand2orand,f),ru05a,ru08a),ru00r,ru00a)$
cnfb(f):=applyb2(applyb2(scanmap(orand2orand,f,bottomup),ru05r,ru08r),ru00r,ru00a)$
dnfb(f):=applyb2(applyb2(scanmap(orand2orand,f,bottomup),ru05a,ru08a),ru00r,ru00a)$
/* necessary condition */
defrule(ne2inq, (aa) # (bb), "%or"((aa)<(bb),(bb)<(aa)))$
defrule(ru00n, (aa) # (bb), "%not"((aa)=(bb)))$
deli2(f):=(applyb2(f,ru001,ru002,ru003,ru004))$
ncondeq(f):=block([exs,L,exk],
if is(ncondtype=off) then return(f),
econd:[],L:true,
prt("necessary eq. condition"),
if is(ncondtype=x) then
(
exs:fullmap(lambda([x],[E,x]),
sort(rest(full_listify(powerset(setify(ncondvars)))),comparelength2)),
for exk in exs do
 (q:qe(exk,(f)),prt([L,q]),
  if is(not(qe([],(L)%implies(q))=true)) then L:(L)%and(q))
)
else
(
exs:fullmap(lambda([x],[E,x]),
sort(rest(full_listify(powerset(setify(listofvars(f))))),comparelength2)),
for exk in exs do
 (if is(ncondtype=full) then q:qe(exk,(f)) else q:deli2(qe(exk,(f))),
  prt([L,q]),
  if is(not(qe([],(L)%implies(q))=true)) then L:(L)%and(q))
),
prt(L),
if is(not(ncondsimp=off)) then L:nnscandc(L),
/* if is(qe([],(L)%implies(f))=true) and (tdeg(L)=1) then (prt("equivalent condition"),econd:[dnf(L)],return(dnf(L))) else */
return((L)%and(f))
)$
ncond(f):=block([exs,L,exk],L:true,
prt("necessary condition"),
exs:fullmap(lambda([x],[E,x]),
sort(rest(full_listify(powerset(setify(listofvars(f))))),comparelength2)),
for exk in exs do
 (q:qe(exk,f),prt([L,q]),
  if not(qe([],(L)%implies(q))=true) then L:(L)%and(nnscan(q))),
prt(L),
return((L)%and(f))
)$
/***************/
/* nns section */
/***************/
/* non-nonsense */
nns(f):=block([F,L,M],
if atom(f) then return(f)
elseif op(f)="%and" then
 (F:sort(args(f),comparelength2),L:setify(F),
 for g in F do
 (M:disjoin(g,L),prt(M),
 if p2emt((substpart("%and",M,0))%implies(g))=true then L:M),
 return(substpart("%and",L,0)))
elseif op(f)="%or" then
 (F:sort(args(f),comparelength2),L:setify(F),
 for g in F do
 (M:disjoin(g,L),prt(M),
 if p2emt((g)%implies(substpart("%or",M,0)))=true then L:M),
 return(substpart("%or",L,0)))
else f)$
/* non-nonsense scan */
nnscan(f):=(prt("nnscanning..."),scanmap(nns,f))$
nnscanb(f):=(prt("bottomup nnscanning..."),scanmap(nns,f,bottomup))$
/* non-nonsense scan with cnf */
nnscanc(f):=(prt("cnf nnscanning..."),scanmap(nns,cnf(f)))$
nnscancb(f):=(prt("cnf bottomup nnscanning..."),scanmap(nns,cnf(f),bottomup))$
/* non-nonsense scan with dnf */
nnscand(f):=(prt("dnf nnscanning..."),scanmap(nns,dnf(f)))$
nnscandb(f):=(prt("dnf bottomup nnscanning..."),scanmap(nns,dnf(f),bottomup))$
nnscand2(f):=(prt("dnf nnscanning..."),nns(map(nns,dnf(f))))$
/* non-nonsense scan with cnf,dnf */
nnscancd(f):=nnscanc(nnscand(f))$
nnscancdb(f):=nnscancb(nnscandb(f))$
nnscandc(f):=nnscand(nnscanc(f))$
nnscandcb(f):=nnscandb(nnscancb(f))$
/* non-nonsense set */
nnss(f):=block([f1,f2,f3,q,nns4],
if mode=d then nns4(g):=nnscand(g) else nns4(g):=nns(g),
prt("pre-simplified formula"),f1: dnf (qe ([],f) ) ,
if atom(f1) then return(f1),
prt(length(f1)),prt(f1),prt("necessary condition"),
if op(f1)="%or" then
 (f2:[],
  for k:1 thru length(f1) do
   (q:ncond(part(f1,k)),prt(k),prt(q),f2:append(f2,[q])),
  f2:substpart("%or",f2,0))
else
 (f2:dnf(ncond(f1)),prt(f2)),
prt("simplifing..."),
if op(f2)="%or" then
 (f3:[],
  for k:1 thru length(f2) do
   (q:qe([],part(f2,k)),prt(k),prt(q),f3:append(f3,[q])),
  prt("nnscanning..."),f3:substpart("%or",f3,0),prt(f3),return(nns4(f3)))
else q:qe([],f2),prt(q),prt("nnscanning..."),return(nns4(q))
)$
/* nnss with double negation */
nnssdn(f):=qe([],%not(nnss(%not(f))))$
/**************/
/* p2t section */
/**************/
defrule(powertk1, (aa)^(bb), makeprtk(aa,num(bb),denom(bb)))$
defrule(powertk2, abs(aa), makeprtk(aa^2,1,2))$
domain:complex$
defrule(powertk2, abs(aa), (aa^2)^(1/2))$
defrule(powertk3, max2(aa,bb), (aa+bb+abs(aa-bb))/2)$
defrule(powertk4, min2(aa,bb), (aa+bb-abs(aa-bb))/2)$
p2e0(f):=block([L,makeprtk,g,q,rt,gg],L:[],
makeprtk(x,a,b):=block([rtk],
 if b=1 then return(x^a)
 else (rtk:concat(rt,length(L)+1),
       if oddp(b) then L:append(L,[(rtk^b=x^a)])
       else L:append(L,[(rtk^b=x^a)%and(rtk>=0)]),
       return(rtk))),
g:applyb1(f,powertk4,powertk3,powertk2,powertk1),
q:makelist([E,concat(rt,k)],k,length(L)),
if L=[] then return(f)
else (gg:(substpart("%and",L,0))%and(g),
     ev(qe(q,gg),noeval))
)$
/* 1-1 type */
p2e1(f):=block([L,M,makeprtk,g,q,rt,gg],M:[],L:[],
makeprtk(x,a,b):=block([rtk],
 if b=1 then return(x^a)
 elseif is(assoc(x^(a/b),M)#false) then return(concat(rt,assoc(x^(a/b),M)))
 else (M:append(M,[[x^(a/b),length(L)+1]]),rtk:concat(rt,length(L)+1),
       if oddp(b) then L:append(L,[(rtk^b=x^a)])
       else L:append(L,[(rtk^b=x^a)%and(rtk>=0)]),
       return(rtk))),
g:applyb1(f,powertk4,powertk3,powertk2,powertk1),
q:makelist([E,concat(rt,k)],k,length(L)),
if L=[] then return(f)
else (gg:(substpart("%and",L,0))%and(g),
     ev(qe(q,gg),noeval))
)$
/* base64 ver */
p2e64(f):=block([sL,L,makeprtk,g,q,gg,rt],sL:[],L:[],
makeprtk(x,a,b):=block([strg,rtk],
 if b=1 then return(x^a)
 else (
 strg:ssubst("equal","=",base64(string(x^(a/b)))),
 strg:ssubst("plus","+",strg),
 strg:ssubst("slash","/",strg),
 rtk:eval_string(strg),
 if not(member(rtk,sL)) then sL:append(sL,[rtk]),
       if oddp(b) then L:append(L,[(rtk^b=x^a)])
       else L:append(L,[(rtk^b=x^a)%and(rtk>=0)]),
       return(rtk))),
g:applyb1(f,powertk4,powertk3,powertk2,powertk1),
q:makelist([E,s],s,sL),/* print([g,sL,q]), 
if is(is(p2etime>0)=true) then p2etime:p2etime+1 else p2etime:1, */
if L=[] then return(f)
else (gg:(substpart("%and",L,0))%and(g),prt(sL),
      subst(makelist(part(sL,k)=concat(rt,k),k,length(sL)),
            ev(qe(q,gg),noeval))
     )
)$
p2e(f):=if is(p2etype=64) then p2e64(f)
    elseif is(p2etype=0) then p2e0(f) else p2e1(f)$
/* p2e map */
p2em(f):=block([p2etlocal],
if atom(f) then return(f)
else
p2etlocal(g):=block([],
 if atom(g) then g
 else 
  if atom(g) then g
  elseif op(g)="#" then "%not"(p2e(substpart("=",g,0)))
  elseif (op(g)="=" or op(g)="<" or op(g)=">" or op(g)="<=" or op(g)=">=")
   then p2e(g)
  else g),
scanmap(p2etlocal,f)
/*qe([],scanmap(p2etlocal,f))*/
)$
/* p2e map tarski */
p2emt(f):=qe([],ev(p2em(f),eval))$
p2t(f):=p2emt(f)$
/*******************/
/* nnsolve section */
/*******************/
/* nnsolvexx */
algexact:true$
/* is imaginary */
imgp(x):=not(is(x>=0) or is(x<0)) or (listofvars(x)=[] and imagpart(x)#0)$
/* del formulas containnig imginary unit */
delimg(f):=block([sel3],
if atom(f) then return(f),
sel3(g):=block([],
if atom(g) then g
elseif (op(g)="#" or op(g)="=" or op(g)="<" or op(g)=">" or op(g)="<=" or op(g)=">=") and (ratsimp(imagpart(lhs(g)))=0) and (ratsimp(imagpart(rhs(g)))=0) then ratsimp(realpart(g))
elseif (op(g)="#" or op(g)="=" or op(g)="<" or op(g)=">" or op(g)="<=" or op(g)=">=") and (imgp(lhs(g))=true or imgp(rhs(g))=true) then false else g),
scanmap(sel3,f))$
Lintersection(l,m):=listify(intersection(setify(l),setify(m)))$
Lminus(l,m):=listify(setdifference(setify(l),setify(m)))$
/* nnsolve: solver for cnj. of eq. */
/* new ver */
delp(L):=block([LL,ek],LL:L,
for ek in L do
 (if member(rhs(ek),%rnum_list) then LL:subst(rhs(ek)=lhs(ek),LL)),
LL
)$
/* new ver */
nnsolve(f,[vv]):=block([fs,v,g,gg],
linsolvewarn:false,
if atom(f) then return(f)
elseif op(f)="%and" then fs:args(f)
elseif op(f)="=" then fs:[f] else return(f),
prt("nnsolving..."),prt(fs),%rnum_list:[],
v:Lintersection(vv,listofvars(fs)),
if v=[] then v:listofvars(fs),
g:delimg(algsys(fs,v)),prt([v,g]),
if g=[] then (v:listofvars(fs),g:delimg(algsys(fs,v)),prt([v,g])),
if g=[] or setify(g)={[false]} then return(false),
solvar:v,
return(substpart("%or",map(lambda([l],substpart("%and",l,0)),map(delp,g)),0))
)$
defrule(ru000, (aa) # (bb), true)$
defrule(ru001, (aa) > (bb), true)$
defrule(ru002, (aa) >= (bb), true)$
defrule(ru003, (aa) < (bb), true)$
defrule(ru004, (aa) <= (bb), true)$
defrule(ru006, (aa) = (bb), true)$
/* delete ineq. if f has no eq. then return true */
deli1(f):=(applyb2(f,ru000,ru001,ru002,ru003,ru004))$
deli(f):=block([g],
if atom(f) then return(f),
g:dnf(f),
if op(g)="%or" then substpart("%or",delete(true,map(deli1,args(g))),0) else deli1(g))$
/* delete eq. if f has no ineq. then return true */
dele1(f):=(applyb2(f,ru006))$
dele(f):=block([g],
if atom(f) then return(f),
g:dnf(f),
if op(g)="%or" then substpart("%or",delete(true,map(dele1,args(g))),0) else dele1(g))$
mapply(s,t):=if atom(t) then s(t) elseif op(t)="%or" then map(s,t) else s(t)$
/* solution selector for eq.s and ineq.s */
defrule(ru007, (aa) = (bb), 
 if Lintersection(v,listofvars(expand(aa-bb)))=[] then true else (aa)=(bb))$
defrule(ru008, (aa) = (bb), 
 if Lintersection(v,listofvars(expand(aa-bb)))=[] then (aa)=(bb) else true)$
delec1(f):=(applyb1(f,ru007))$
delec(f):=block([g],
if atom(f) then return(f),
g:dnf(f),
if op(g)="%or" then substpart("%or",delete(true,map(delec1,args(g))),0) else delec1(g))$
delev1(f):=(applyb1(f,ru008))$
delev(f):=block([g],
if atom(f) then return(f),
g:dnf(f),
if op(g)="%or" then substpart("%or",delete(true,map(delev1,args(g))),0) else delev1(g))$
/* new ver for ineq. and conj. of eq., separate ng type */
solsel(f,[v]):=block([eqss,eqs,ins,sel2,ng],
eqss:deli(f),
if v=[] or length(listofvars(eqss))=1 then v:listofvars(eqss),
eqs:delec(eqss),
prt(eqs),
if atom(eqs) then return(f),
if op(eqs)="%or" then (prt("I can't solve disj. of eq."),return(f)),
ins:(dele(f))%and(delev(eqss)),
 sel2(g):=block([gg],
 /*(g)%and(qe(map(lambda([x],[E,x]),listofvars(g)),p2et((g)%and(ins))))*/
 if /* ins=true then g
 elseif */ solvar=[] or atom(g) then (g)%and(ins)
 else (prt("verifing..."),prt(g),
       solvar:map(lhs,(if op(g)="%and" then args(g) else [g])), 
       gg:(g)%and(qe(map(lambda([x],[E,x]),solvar),p2t((g)%and(ins)))),
       ng:(ng)%and(qe([],"%not"(qe(map(lambda([x],[E,x]),solvar),p2t(g))))),
       prt("sel2 returned..."),prt(gg),gg
      )
 ),
qq:apply(nnsolve,flatten([eqs,v])),prt(qq),
if ins=true then prt("no ins"),
ng:f,
qq:mapply(sel2,qq),
if is((ng)#false) then 
 (prt("other type..."),prt(ng),
  /* ncondtype:full,ncondsimp:off, */
  inner:true,
  qq:(qq)%or(ora(apply(nnsolvexx,flatten([ng,solvar])))),
  /* ncondtype:eq,ncondsimp:nn, */
  inner:false),
qq /* for nnsolvexx */
/* nnscan(qq) */ /* for stand alone */
)$
/* nnsolvexx: solver for semi-algebraic system */
nnsolvexx0(f,[v]):=block([g,gg],
prt("pre-simplified formula"),
g:dnf(qe([],p2t(f))),prt(g),
if atom(g) then return(g)
else (prt("necessary condition"),g:dnf(ncond(g)),prt(length(g)),prt(g)),
 if op(g)="%or" then (prt("simplifing..."),gg:dnf(map(ncond,g)),prt(length(gg)),prt(gg),nnscan(map(lambda([t],apply(solsel,flatten([t,v]))),gg)))
 else nnscan(apply(solsel,flatten([g,v])))
)$
/* total deg */
tdeg0(f):=block([tdegx],
hipow(expand(subst(map(lambda([s],s=tdegx),listofvars(f)),f)),tdegx)
)$
tdeg(f):=block([g],g:expand( lhs(f)-rhs(f)),
if atom(g) then tdeg0(g) else apply(max,map(tdeg0,args(g)) )
)$
/* new ver */
nnsolvexx(f,[vv]):=block([t0,g,v,gg,q,dele1q],
if not(inner=true) then (thisin:f,t0:elapsed_real_time()),
if is(presimp=off) then prt("pre-simplify:off")
 elseif is(presimp=l) then prt("pre-simplify:lineq with nnscan")
 else prt("pre-simplify:p2t->qe"),
if is(ncondtype=off) then prt("ncond-type:off")
 elseif is(ncondtype=full) then prt("ncond-type:full formula")
 elseif is(ncondtype=x) then prt("ncond-type:x")
 else prt("ncond-type:eq. only"),
if is(ncondsimp=off) then prt("ncond-post-simplify:off")
 else prt("ncond-post-simplify:nnscandc"),
if is(sineqsimp=nn) then prt("sineq-simplify:nnscan")
 else prt("sineq-simplify:lineq"),
if is(postsimp=off) then prt("post-simplify:off")
 else prt("post-simplify:nnscanc->nnscand->nnscan"),
prt(""),prt("given formula"),prt(f),
if is(presimp=off) then g:dnf(f)
 elseif is(presimp=l) then
  (prt("pre-simplified dnf"),g:dnf(applyb1(lineq(f),ne2inq)))
 else
  (prt("pre-simplified dnf"),g:dnf(applyb1(qe([],p2t(f)),ne2inq))),prt(g),
if atom(g) then return(bra(g)),prt("var for solve"),
v:vv,for xxx in Lminus(vv,listofvars(g)) do v:delete(xxx,v),
if v=[] then v:listofvars(f),prt(v),ncondvars:v,
if atom(g) then return(bra(g))
else
 (if is(ncondsimp=off) then gg:dnf(mapply(ncondeq,g))
  else gg:nnscan(dnf(mapply(ncondeq,g))),
  prt(length(gg)),prt(gg),prt("solving..."),
  q:scanmap(lambda([t],apply(fineq,flatten([t,last(v)]))),
     mapply(lambda([t],apply(solsel,flatten([t,v]))),gg)
           ,bottomup),
  prt("solved formula"),prt(q),prt(elapsed_real_time()-t0),
collect(ff):=expand(ff),
/*
collect(ff):=block([fff,vc],fff:expand(f),vc:listofvars(fff),
 sum(factor(coeff(fff,vc,d))*x^d,d,0,hipow(fff,vc))),
*/
  deleq:dele(q),
/*  if length(Lminus(listofvars(dele1q),listofvars(facts())))=1 then */
  if length(listofvars(deleq))=1 then
       (thisout:mapply(lambda([g],(cineqs(dele1(g))) %and (deli1(g))),dnf(q)),
        if is(deleq=dnf(q)) then thisout:(cineqs(thisout)),
        thisout:bra(l2r(thisout)),thisout)
  elseif is(postsimp=off) then
       (thisout:scanmap(collect,l2rx(bra(l2r(q)),last(v))),thisout)
  else (q:mapply(nnscanc,q),prt("reduced formula"),prt(q),
        q:mapply(nnscand,q),prt("reduced formula"),prt(q),
        q:nnscan(q),
        thisout:scanmap(collect,l2rx(bra(l2r(q)),last(v))),thisout)
 )
)$
thischeck():=fullall((qex(thisin))%eq(qex(ora(thisout))))$
/* for scanmap */
sineq(ineq,[vv]):=block([p,v,xxx,sineqx,q,r,s,lc,sol,sols,sk,rt,qq,excond],
if atom(ineq) then return(ineq),
if not(op(ineq)=">" or op(ineq)="<" or op(ineq)=">=" or op(ineq)="<=") then return(ineq),
p:ratsimp(lhs(ineq)-rhs(ineq)),
if imgp(p)=true then return(ineq),
if tdeg(num(factor(p)))<=1 and listofvars(denom(factor(p)))=[] then return(ineq),
if length(vv)=1 then (if hipow(p,last(vv))=1 then return(ineq)),
v:vv,for xxx in Lminus(vv,listofvars(p)) do v:delete(xxx,v),
if v=[] then v:listofvars(p),sineqx:last(v),
q:1,r:1,sols:[],
sol:delimg(ratsimp(solve(lhs(ineq)-rhs(ineq),sineqx))),
for k:1 thru length(sol) do
 (sk:part(sol,k),if not(sk=false) then
  (q:q*(lhs(sk)-rhs(sk))^(part(multiplicities,k)),
   r:r*(lhs(sk)-concat(rt,k))^(part(multiplicities,k)),
   sols:append(sols,makelist(lhs(sk)=rhs(sk),j,part(multiplicities,k))))),
s:factor(quotient(p,q,sineqx)),
lc:ratcoef(expand(s),sineqx,hipow(expand(s),sineqx)),/* lc:lc/abs(lc), */
prt("factor type"),prt([p,sineqx,r,s,lc]),
if listofvars(q)=[sineqx] then qq:cineq(sols,lc,op(ineq))
 else
(
if s=lc then
 qq:qe([],qe([],apply(op(ineq),[r*lc,0])))
elseif ((op(ineq)=">") or (op(ineq)="<")) then
 (prt("nnsolving..."),
  qq:(qe([],qe([],apply(op(ineq),[r*lc,0]))) %and nnsolvexx0(s#0)))
elseif ((op(ineq)=">=") or (op(ineq)="<=")) then
 (prt("nnsolving..."),
  qq:(qe([],qe([],apply(op(ineq),[r*lc,0]))) %or nnsolvexx0(s=0))),
prt(qq),
/* prt(qe([],cnf(qq))),*/
for k:1 thru length(sol) do
 (sk:part(sol,k),if not(sk=false) then
  qq:subst(concat(rt,k)=rhs(sk),qq)),
if is(sineqsimp=off) then qq:qq
elseif is(sineqsimp=ex) then qq:(map(lambda([t],(t)%and(fullex(t))),qq))
elseif is(sineqsimp=l) then qq:lineq(qq)
else qq:nnscan(qq),
excond:qe([[E,sineqx]],p2t(qq)),prt(excond),
if not(is(excond=true)) then
 (inner:true,
  qq:(qq) %or (ora(nnsolvexx((ineq)%and(%not(excond)),sineqx))),
  inner:false)
),
prt("sineq returned..."),prt(qq),
qq
)$
/* with cineq */
sineq(ineq,[vv]):=block([p,v,xxx,sineqx,q,r,s,lc,sol,sols,sk,rt,qq,excond],
if atom(ineq) then return(ineq),
if not(op(ineq)=">" or op(ineq)="<" or op(ineq)=">=" or op(ineq)="<=") then return(ineq),
p:ratsimp(lhs(ineq)-rhs(ineq)),
if imgp(p)=true then return(ineq),
if listofvars(p)=[] then (prt("const."),return("%and"(apply(op(ineq),[p,0])))),
if (tdeg(num(factor(p)))=1 and listofvars(denom(factor(p)))=[]
    and length(listofvars(expand(p)))=1) then
 (prt("linear"),/*********************/
  v:first(listofvars(p)),lc:ratcoef(expand(p),v,hipow(expand(p),v)),
  if lc>0 then return(apply(op(ineq),[v,expand((lc*v-p)/lc)]))
          else return(apply(op(ineq),[expand((lc*v-p)/lc),v]))),
/* if length(vv)=1 then (if hipow(p,last(vv))=1 then return(ineq)), */
v:vv,for xxx in Lminus(vv,listofvars(p)) do v:delete(xxx,v),
if v=[] then v:listofvars(p),sineqx:last(v),
q:1,r:1,sols:[],
sol:delimg(ratsimp(solve(lhs(ineq)-rhs(ineq),sineqx))),
for k:1 thru length(sol) do
 (sk:part(sol,k),if not(sk=false) then
  (q:q*(lhs(sk)-rhs(sk))^(part(multiplicities,k)),
   r:r*(lhs(sk)-concat(rt,k))^(part(multiplicities,k)),
   sols:append(sols,makelist(lhs(sk)=rhs(sk),j,part(multiplicities,k))))),
s:factor(quotient(p,q,sineqx)),
lc:ratcoef(expand(s),sineqx,hipow(expand(s),sineqx)),/* lc:lc/abs(lc), */
prt("factor type"),prt([p,sineqx,r,s,lc]),
if listofvars(q)=[sineqx] then qq:cineq(sols,lc,op(ineq))
/*
elseif length(delete(sineqx,listofvars(q)))=1 then qq:tineq(sols,lc,op(ineq))
*/
 else
(
if s=lc then
 qq:qe([],qe([],apply(op(ineq),[r*lc,0])))
elseif ((op(ineq)=">") or (op(ineq)="<")) then
 (prt("nnsolving..."),
  qq:(qe([],qe([],apply(op(ineq),[r*lc,0]))) %and nnsolvexx0(s#0)))
elseif ((op(ineq)=">=") or (op(ineq)="<=")) then
 (prt("nnsolving..."),
  qq:(qe([],qe([],apply(op(ineq),[r*lc,0]))) %or nnsolvexx0(s=0))),
prt(qq),
/* prt(qe([],cnf(qq))),*/
for k:1 thru length(sol) do
 (sk:part(sol,k),if not(sk=false) then
  qq:subst(concat(rt,k)=rhs(sk),qq)),
if is(sineqsimp=off) then qq:qq
elseif is(sineqsimp=ex) then qq:(map(lambda([t],(t)%and(fullex(t))),qq))
elseif is(sineqsimp=l) then qq:lineq(qq)
else qq:nnscan(qq),
excond:qe([[E,sineqx]],p2t(qq)),prt(excond),
if not(is(excond=true)) then
 (inner:true,
  qq:(qq) %or (ora(nnsolvexx((ineq)%and(%not(excond)),sineqx))),
  inner:false)
),
prt("sineq returned..."),prt(qq),
qq
)$
defrule(delsame, ((aa) < (bb)) %and ((bb) < (aa)), false)$
defrule(eqsame, ((aa) <= (bb)) %and ((bb) <= (aa)), (bb)=(aa))$
sineq2(ineq,sineqx):=block([p,sol,sols,conds,cfs,cqs,lc,crs,css,cts],
/* sineq2(ineq,sineqx):=block([], */
p:(lhs(ineq)-rhs(ineq)),prt(p),
sol:delimg(ratsimp(solve(lhs(ineq)-rhs(ineq),sineqx))),prt(sol),
sol:makelist([part(sol,k),part(multiplicities,k),qex([[E,sineqx]],part(sol,k))],k,length(sol)),prt(sol),
sols:[],for c in sol do if not(last(c)=false) then
 sols:append(sols,[
  [(lhs(first(c))-rhs(first(c)))^(second(c)),
   (lhs(first(c))-concat(rt,length(sols)+1))^(second(c)),
   concat(rt,length(sols)+1)=rhs(first(c)),
   last(c)]]),prt(sols),
conds:listify(setify(makelist(last(s),s,sols))),prt(conds),
conds:delete(false,map(qex, delete(false,flatten(subst("Or"="[",applyb2(applyb2(xreduce("And",map(lambda([f],(f) Or (qex(%not(f)))),append(conds,[true]))),ru05a,ru08a),ru00a)))))),prt("case conds (conds):"),prt(l2r(conds)),
cfs:makelist([c,append([[[1,1],t0=t0]],delete(false,makelist((if (fullall((c)%implies(last(s)))) then [rest(s,-2),part(s,3)]),s,sols)))],c,conds),prt(cfs),
cqs:map(lambda([s],([first(s),substpart("*",map(first,last(s)),0),delete(t0=t0,map(last,last(s)))])),cfs),prt(cqs),
lc:ratcoef(expand(p),sineqx,hipow(expand(p),sineqx)),
crs:map(lambda([s],[first(s), lc*second(second(s)) ,factor(quotient(p,first(second(s)),sineqx))/lc  ,last(s)]),cqs),prt(crs),
css:map(lambda([s],apply(if op(ineq)="<" or op(ineq)=">" then "%and" else "%or",[(first(s)) %and (subst(last(s), dele( subst(if op(ineq)="<" or op(ineq)=">" then [] else [">"=">=","<"="<="], qex(apply(op(ineq),[second(s),0])))))), if op(ineq)="<" or op(ineq)=">" then ora(nnsolvexx((first(s))%and(third(s)#0))) else ora(nnsolvexx((first(s))%and(third(s)=0))) ])),crs),prt("raw sols:"),prt(css),
cts:map(nns,l2rx(map(nnscand,css),sineqx)),prt("reduced sols (cts):"),prt(cts),
substpart("%or",cts,0)
)$
/* new ver */
/* sineq3(ineq,sineqx):=block([], */
sineq3(ineq,sineqx):=block([p,k,c,sol,sols,conds,cfs,crs,cqs,lc,css,cts],
p:(lhs(ineq)-rhs(ineq)),prt(p),
sol:delimg(ratsimp(solve(lhs(ineq)-rhs(ineq),sineqx))),prt(sol),
sol:makelist([part(sol,k),part(multiplicities,k),qex([[E,sineqx]],part(sol,k))],k,length(sol)),prt(sol),
sols:[],for c in sol do if not(last(c)=false) then
 sols:append(sols,[
  [[(lhs(first(c))-rhs(first(c)))^(second(c))],
   [if second(c)=1 then lhs(first(c))=rhs(first(c)) else
      makelist(lhs(first(c))=rhs(first(c)),second(c))],
   last(c)]]),prt(sols),
conds:listify(setify(makelist(last(s),s,sols))),prt(conds),
conds:delete(false,map(qex, delete(false,flatten(subst("Or"="[",applyb2(applyb2(xreduce("And",map(lambda([f],(f) Or (qex(%not(f)))),append(conds,[true]))),ru05a,ru08a),ru00a)))))),prt("case conds (conds):"),prt(l2r(conds)),
cfs:makelist([c,append([append,[[1],[]]],delete(false,makelist((if (fullall((c)%implies(last(s)))) then rest(s,-1)),s,sols)))],c,conds),
cqs:map(lambda([s],([first(s), substpart("*",first(apply(map,last(s))),0), last(apply(map,last(s)))])),cfs),prt(cqs),
lc:ratcoef(expand(p),sineqx,hipow(expand(p),sineqx)),
crs:map(lambda([s],[first(s), factor(quotient(p,second(s),sineqx))/lc, last(s)]),cqs),prt("crs:"),prt(crs),
ncondtype_out:ncondtype,postsimp_out:postsimp,ncondtype:off,postsimp:off,
css:(apply(append,l2r(map(lambda([s],
if op(ineq)="<" or op(ineq)=">" then
 block([a1],a1:nnssdn(p2t((first(s))%and(second(s)>0))),
 brand(
       tineq2(third(s),lc,op(ineq)),
       sepaxa(if member(sineqx,listofvars(a1)) then bra(a1) else [[a1]]
              ,sineqx),
       sineqx
      )
      )
else
 block([a1],a1:nnss(p2t((first(s))%and(second(s)=0))),
 append(
       brand(tineq2(third(s),lc,op(ineq)), [[ first(s) ]], sineqx),
       sepaxa(if member(sineqx,listofvars(a1)) then bra(a1) else [[a1]]
              ,sineqx)
       )
       )
),crs)))),
ncondtype:ncondtype_out,postsimp:postsimp_out,
prt("raw sols:"),prt(css),css
/*cts:map(nns,l2r(map(nnscand,css))),prt("reduced sols (cts):"),prt(cts),
substpart("%or",cts,0)*/
)$
/*
sineq3(x^4-2*a*x^2<b,x);
*/
sepaxa(braform,xxx):=
map(lambda([l],
  block([L,M,e],L:[],M:[],for e in l do
  if member(xxx,listofvars(e)) then L:append(L,[e]) else M:append(M,[e]),
  [L,M])),map(flatten,braform))$
brand(bra1,bra2,v):=block([s1,s2],
sepaxa(apply(append,makelist(makelist(append(s1,s2),s2,bra2),s1,bra1)),v)
)$
braor(bra1,bra2):=append(bra1,bra2)$

/* sineq5(ineq,[vv]):=block([], */
sineq5(ineq,[vv]):=block([sineqx,p,k,c,sol,sols,conds,cfs,crs,cqs,lc,css,cts],
if length(listofvars(ineq))<=1 then
 return(map(lambda([s],[s,[]]),cineqs2("%and"(ineq)))),
if vv#[] then sineqx:first(vv),p:(lhs(ineq)-rhs(ineq)),prt(p),
sol:delimg(ratsimp(solve(lhs(ineq)-rhs(ineq),sineqx))),prt(sol),
sol:makelist([part(sol,k),part(multiplicities,k),qex([[E,sineqx]],part(sol,k))],k,length(sol)),prt(sol),
sols:[],for c in sol do if not(last(c)=false) then
 sols:append(sols,[
  [[(lhs(first(c))-rhs(first(c)))^(second(c))],
   [if second(c)=1 then lhs(first(c))=rhs(first(c)) else
      makelist(lhs(first(c))=rhs(first(c)),second(c))],
   last(c)]]),prt(sols),
conds:listify(setify(makelist(last(s),s,sols))),prt(conds),
conds:delete(false,map(qex, delete(false,flatten(subst("Or"="[",applyb2(applyb2(xreduce("And",map(lambda([f],(f) Or (qex(%not(f)))),append(conds,[true]))),ru05a,ru08a),ru00a)))))),prt("case conds (conds):"),prt(l2r(conds)),
crs:makelist([c,append([],delete(false,makelist((if (fullall((c)%implies(last(s)))) then second(s)),s,sols)))],c,conds),
prt("crs"),prt(crs),
lc:ratcoef(expand(p),sineqx,hipow(expand(p),sineqx)),
ncondtype_out:ncondtype,postsimp_out:postsimp,ncondtype:off,postsimp:off,
css:(apply(append,l2r(
map(lambda([s],brand(tineq2(second(s),lc,op(ineq)),[[first(s)]],sineqx))
   ,crs)))),
ncondtype:ncondtype_out,postsimp:postsimp_out,
prt("raw sols:"),prt(css),css
)$

/* sineq4(ineq,[vv]):=block([], */
sineq4(ineq,[vv]):=block([p,v,sineqx,hd,k,c,sol,sols,conds,cfs,cqs,lc,crs,css,cts],
/* pre-checks */
if atom(ineq) then return([[ineq]]),
if not(op(ineq)=">" or op(ineq)="<" or op(ineq)=">=" or op(ineq)="<=")
 then return([[ineq]]),
p:(lhs(ineq)-rhs(ineq)),prt(p),
if imgp(p)=true then return([[ineq]]),
if listofvars(p)=[]
 then (prt("const."),return([["%and"(apply(op(ineq),[p,0]))]])),
/* def. of sineqx */
v:vv,for xxx in Lminus(vv,listofvars(p)) do v:delete(xxx,v),
if v=[] then v:listofvars(p),sineqx:first(v),
/* linear */
hd:hipow(expand(p),sineqx),lc:ratcoef(expand(p),sineqx,hd),
if hd=1 and listofvars(lc)=[] then
 (prt("linear"),
  if lc>0 then return([[apply(op(ineq),[sineqx,expand((lc*sineqx-p)/lc)])]])
          else return([[apply(op(ineq),[expand((lc*sineqx-p)/lc),sineqx])]])),
/* main */
sol:delimg(ratsimp(solve(lhs(ineq)-rhs(ineq),sineqx))),prt(sol),
if listofvars(p)=[sineqx] then
 return(bra(cineqs(cineq(
  delete(false,flatten(
   makelist(if not(part(sol,k)=false) then
    makelist(lhs(part(sol,k))=rhs(part(sol,k)),part(multiplicities,k)),
    k,length(sol)))),
 lc,op(ineq))))),
sol:makelist([part(sol,k),part(multiplicities,k),qex([[E,sineqx]],part(sol,k))],k,length(sol)),prt(sol),
sols:[],for c in sol do if not(last(c)=false) then
 sols:append(sols,[
  [[(lhs(first(c))-rhs(first(c)))^(second(c))],
   [if second(c)=1 then lhs(first(c))=rhs(first(c)) else
      makelist(lhs(first(c))=rhs(first(c)),second(c))],
   last(c)]]),prt(sols),
conds:listify(setify(makelist(last(s),s,sols))),prt(conds),
conds:delete(false,map(qex, delete(false,flatten(subst("Or"="[",applyb2(applyb2(xreduce("And",map(lambda([f],(f) Or (qex(%not(f)))),append(conds,[true]))),ru05a,ru08a),ru00a)))))),prt("case conds (conds):"),prt(l2r(conds)),
cfs:makelist([c,append([append,[[1],[]]],delete(false,makelist((if (fullall((c)%implies(last(s)))) then rest(s,-1)),s,sols)))],c,conds),
cqs:map(lambda([s],([first(s), substpart("*",first(apply(map,last(s))),0), last(apply(map,last(s)))])),cfs),prt(cqs),
crs:map(lambda([s],[first(s), factor(quotient(p,second(s),sineqx))/lc, last(s)]),cqs),prt(crs),
ncondtype:off,postsimp:off,
css:l2r(map(lambda([s],
if op(ineq)="<" or op(ineq)=">" then
 (ora(tineq2(third(s),lc,op(ineq)))) %and ora(nnsolvexx((first(s))%and(second(s)#0)))
 else ( (first(s)) %and (ora(tineq2(third(s),lc,op(ineq)))) )
      %or (ora(nnsolvexx((first(s))%and(second(s)=0))))),crs)),
kill(ncondtype,postsimp),prt("raw sols:"),prt(css),
map("[",css)
/*cts:map(nns,l2r(map(nnscand,css))),prt("reduced sols (cts):"),prt(cts),
substpart("%or",cts,0)*/
)$

/* one type */
cineq(sols,lc,gl):=block([L,M],
L:sort(sols,lambda([s,t],rhs(s)>=rhs(t))),
if (lc>0 and (gl="<" or gl="<=")) or (lc<0 and (gl=">" or gl=">="))
 then L:append([first(L)],L),
L:makelist(
if k=1 then rhs(part(L,k))<lhs(part(L,k))
elseif evenp(k) and k<length(L)
 then (rhs(part(L,k+1))<lhs(part(L,k+1))) %and (lhs(part(L,k))<rhs(part(L,k)))
elseif evenp(k) and k=length(L)
 then lhs(part(L,k))<rhs(part(L,k))
else false,
k,length(L)),
if (lc>0 and (gl="<" or gl="<=")) or (lc<0 and (gl=">" or gl=">="))
 then L:rest(L),
if gl=">=" or gl="<=" then L:subst("<"="<=",L),
L:reverse(L),prt(L),
apply1(substpart("%or",L,0),delsame,eqsame)
)$
cineqsd(l):=block([v,ll,lr],
if listofvars(l)=[] then return(first(l)),
v:first(listofvars(l)),ll:{},lr:{},
for f in l2r(l) do
 (if rhs(f)=v then ll:append(ll,{f}) else lr:append(lr,{f})),
ll:sort(listify(ll),lambda([s,t],lhs(s)<lhs(t) or (lhs(s)=lhs(t) and op(s)="<=" and op(t)="<"))),
lr:sort(listify(lr),lambda([s,t],rhs(s)<rhs(t) or (rhs(s)=rhs(t) and op(s)="<" and op(t)="<="))),
prt([ll,lr]),
if ll=[] then return(first(lr))
elseif lr=[] then return(last(ll))
elseif lhs(last(ll))<rhs(first(lr)) then (last(ll)) %and (first(lr))
elseif (op(last(ll))="<=" and op(first(lr))="<=" and lhs(last(ll))=rhs(first(lr))) then lhs(first(lr))=rhs(first(lr)) /* (last(ll)) %and (first(lr)) */
else false)$
cineqsc(l):=block([v,ll,lr],
if listofvars(l)=[] then return(first(l)),
v:first(listofvars(l)),ll:{},lr:{},
for f in l2r(l) do
 (if rhs(f)=v then ll:append(ll,{f}) else lr:append(lr,{f})),
ll:sort(listify(ll),lambda([s,t],lhs(s)<lhs(t) or (lhs(s)=lhs(t) and op(s)="<=" and op(t)="<"))),
lr:sort(listify(lr),lambda([s,t],rhs(s)<rhs(t) or (rhs(s)=rhs(t) and op(s)="<" and op(t)="<="))),
prt([ll,lr]),
if ll=[] then return(last(lr))
elseif lr=[] then return(first(ll))
elseif rhs(last(lr))<lhs(first(ll)) then (last(lr)) %or (first(ll))
elseif (op(last(lr))="<" and op(first(ll))="<" and rhs(last(lr))=lhs(first(ll))) then (last(lr)) %or (first(ll)) /* lhs(last(lr))#rhs(last(lr)) */
else true)$
cineqsdm(L):=(prt("cineqs-dnf returned..."),
 substpart("%or",map(cineqsd,bra(L)),0))$
cineqscm(L):=(prt("cineqs-cnf returned..."),
 substpart("%and",map(cineqsc,brac(ora(L))),0))$
defrule(eq2inq, (aa) = (bb), (((aa)<=(bb)) %and ((bb)<=(aa))) )$
cineqs(L):=cineqsdm(cineqscm(apply1(L,ne2inq,eq2inq)))$
cineqs2(F):=block([lv,v,imagunit,L,M,N,k,K,J],
lv:listofvars(F),if length(lv)#1 then return([[F]]) else v:first(lv),
L:sort(listify(setify(flatten(scanmap(
 lambda([e],
if atom(e) then e
elseif member(op(e),["%and","%or"]) then substpart("[",e,0)
elseif member(op(e),["<",">","<=",">=","=","#"]) then map(rhs,delete(false,delimg(solve(substpart("=",e,0),v))))
else e),
F)))),"<"),
if L=[] then return([[is(subst(v=0,F))]]),
L:sort(append(L,(append([first(L)-1],L)+append(L,[last(L)+1]))/2),"<"),
/* */
M:map(lambda([e],is(ratsimp((subst(v=e,F))))),L),prt([L,M]),
/*
fpprec:64,
M:map(lambda([e],is(rationalize(bfloat(subst(v=e,F))))),L),prt([L,M]),
fpprec:16,
M:map(lambda([e],is(subst(v=float(e),F))),L),prt([L,M]), */
if length(setify(M))=1 then return([[is(subst(v=0,F))]]),
L:append([0,0],L,[0,0]),M:append([true,true],M,[true,true]),
K:[],for k:3 thru length(M)-2 do (
if oddp(k) then
 if part(M,k) then
 (if not(part(M,k-1)) then K:append(K,[part(L,k-1)<v])
  elseif not(part(M,k-2)) then K:append(K,[part(L,k-1)<=v]) else K:K,
  if not(part(M,k+1)) then K:append(K,[v<part(L,k+1)])
  elseif not(part(M,k+2)) then K:append(K,[v<=part(L,k+1)]) else K:K)
 else K:K
else 
 if not(part(M,k-1)) and part(M,k) and not(part(M,k+1)) then
 K:append(K,[v=part(L,k)]) else K:K),
N:[],J:[],for k in K do
 if rhs(k)=v then J:[k]
 else (N:append(N,[append(J,[k])]),J:[]),
delete([],append(N,[J]))
)$
ratprint:false$
/*
asksign
*/
cineqs2f(F):=block([lv,v,imagunit,L,M,N,k,K],
lv:listofvars(F),if length(lv)#1 then return(F) else v:first(lv),
L:sort(listify(setify(flatten(scanmap(
 lambda([e],
if atom(e) then e
elseif member(op(e),["%and","%or"]) then substpart("[",e,0)
elseif member(op(e),["<",">","<=",">=","=","#"]) then map(lambda([t],if member(imagunit,listofvars(t)) then [] else rhs(t)),subst(%i=imagunit,solve(substpart("=",e,0),v)))
else e),
F)))),"<"),
if L=[] then return([[is(subst(v=0,F))]]),
L:sort(append(L,(append([first(L)-1],L)+append(L,[last(L)+1]))/2),"<"),
M:map(lambda([e],is(subst(v=float(e),F))),L),prt([L,M]),
if length(setify(M))=1 then return([[is(subst(v=0,F))]]),
L:append([0,0],L,[0,0]),M:append([true,true],M,[true,true]),
N:[],for k:3 thru length(M)-2 do (K:[],
if oddp(k) then
 if part(M,k) then
 (if not(part(M,k-1)) then K:append(K,[part(L,k-1)<v])
  elseif not(part(M,k-2)) then K:append(K,[part(L,k-1)<=v]) else K:K,
  if not(part(M,k+1)) then K:append(K,[v<part(L,k+1)])
  elseif not(part(M,k+2)) then K:append(K,[v<=part(L,k+1)]) else K:K)
 else K:K
else 
 if not(part(M,k-1)) and part(M,k) and not(part(M,k+1)) then
 K:append(K,[v=part(L,k)]) else K:K,
N:delete([],append(N,[K]))),
N)$

/* factor for polynomial inequation */
/* new ver */
fineq(ineq,[vv]):=block([pp,v,xx,yy,dg,lc],
if atom(ineq) or listofvars(ineq)=[] then return("%and"(ineq)),
if not(op(ineq)=">" or op(ineq)="<" or op(ineq)=">=" or op(ineq)="<=") then return(ineq),
pp:ratsimp(lhs(ineq)-rhs(ineq)),
if atom(pp) then return(ineq),
prt(vv),
v:vv,for yy in Lminus(vv,listofvars(pp)) do v:delete(yy,v),
if v=[] then v:listofvars(pp),xx:last(v),dg:hipow(pp,xx),prt(xx),
if imgp(pp)=true or dg=0 then return(ineq),lc:ratcoef(pp,xx,dg),/*prt(lc),*/
if listofvars(lc)=[] then (prt("case1"),sineq(apply(op(ineq),[lhs(ineq)-rhs(ineq),0]),xx))
elseif is(fineqfull=on) then
((prt("case2-1"),(nnsolvexx(lc=0) %and fineq(apply(op(ineq),[expand(pp-lc*xx^dg),0]),xx)))
 %or (prt("case2-2"),(fineq(lc>0) %and (sineq(apply(op(ineq),[pp/lc,0]),xx))))
 %or (prt("case2-3"),(fineq(lc<0) %and (sineq(apply(op(ineq),[(-1)*pp/lc,0]),xx)))))
else ((prt("case2-1"),((lc=0) %and fineq(apply(op(ineq),[expand(pp-lc*xx^dg),0]),xx)))
 %or (prt("case2-2"),((lc>0) %and (sineq(apply(op(ineq),[pp/lc,0]),xx))))
 %or (prt("case2-3"),((lc<0) %and (sineq(apply(op(ineq),[(-1)*pp/lc,0]),xx)))))
)$
/* automatic quantify */
fullex(f):=qe(map(lambda([x],[E,x]),listofvars(f)),p2t(f))$
fullall(f):=qe(map(lambda([x],[A,x]),listofvars(f)),p2t(f))$
/* lineq */
defrule(tolineq1, (aa)^(bb), lineqdec(1,aa^bb,bb))$
defrule(tolineq2, (aa)/(bb), lineqdec(2,aa/bb,bb))$
lineq(f):=block([L,lineqdec,q],
if is(lsimp=off) then (prt("lineq off"),return(f)),
prt("linear simplifing..."),
L:[],
lineqdec(m,g,bb):=block([strg],
 if (m=1 and denom(bb)=1) or (m=2 and listofvars(bb)=[]) then g
 else (
 strg:ssubst("equal","=",base64(string(g))),
 strg:ssubst("plus","+",strg),
 strg:ssubst("slash","/",strg),
 L:append(L,[eval_string(strg)=g]),eval_string(strg))),
q:qe([],apply1(f,tolineq1,tolineq2)),L:listify(setify(L)),
prt([q,L]),
/* nnscan(nnscan(subst(L,q)))*/
/* nnscan(subst(L,q)) */
nnscan(mapply(lambda([t],(t)%and(fullex(t))),dnf(subst(L,q))))
/* subst(L,q) */
)$
/* l2r */
defrule(l2r1, (aa) > (bb),block([p,l,v],p:expand(bb-aa),l:listofvars(p),
if length(l)=1 then
 (v:first(l),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<v
  else bb<aa)
else bb<aa
))$
defrule(l2r2, (aa) >= (bb),block([p,l,v],p:expand(bb-aa),l:listofvars(p),
if length(l)=1 then
 (v:first(l),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<=-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<=v
  else bb<=aa)
else bb<=aa
))$
defrule(l2r3, (bb) < (aa),block([p,l,v],p:expand(bb-aa),l:listofvars(p),
if length(l)=1 then
 (v:first(l),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<v
  else bb<aa)
else bb<aa
))$
defrule(l2r4, (bb) <= (aa),block([p,l,v],p:expand(bb-aa),l:listofvars(p),
if length(l)=1 then
 (v:first(l),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<=-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<=v
  else bb<=aa)
else bb<=aa
))$
defrule(l2r5, (bb) = (aa),block([p,l,v],p:expand(bb-aa),l:listofvars(p),
if length(l)=1 then
 (v:first(l),
  if hipow(p,v)=1 then v=-coeff(p,v,0)/coeff(p,v,1)
  else bb=aa)
else bb=aa
))$
defrule(l2r1x, (aa) > (bb),block([p,v],p:expand(bb-aa),
if length(vv)=1 then
 (v:first(vv),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<v
  else bb<aa)
else bb<aa
))$
defrule(l2r2x, (aa) >= (bb),block([p,v],p:expand(bb-aa),
if length(vv)=1 then
 (v:first(vv),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<=-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<=v
  else bb<=aa)
else bb<=aa
))$
defrule(l2r3x, (bb) < (aa),block([p,v],p:expand(bb-aa),
if length(vv)=1 then
 (v:first(vv),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<v
  else bb<aa)
else bb<aa
))$
defrule(l2r4x, (bb) <= (aa),block([p,v],p:expand(bb-aa),
if length(vv)=1 then
 (v:first(vv),
  if hipow(p,v)=1 and coeff(p,v,1)>0 then v<=-coeff(p,v,0)/coeff(p,v,1)
  elseif hipow(p,v)=1 and coeff(p,v,1)<0 then -coeff(p,v,0)/coeff(p,v,1)<=v
  else bb<=aa)
else bb<=aa
))$
defrule(l2r5x, (bb) = (aa),block([p,v],p:expand(bb-aa),
if length(vv)=1 then
 (v:first(vv),
  if hipow(p,v)=1 then v=-coeff(p,v,0)/coeff(p,v,1)
  else bb=aa)
else bb=aa
))$
defrule(l2r6, (bb) < (aa) %and (cc) < (bb), (cc) < (bb) %and (bb) < (aa))$
l2r(f):=block([],apply1(f,l2r1,l2r2,l2r3,l2r4,l2r5))$
l2rx(f,[vv]):=block([],apply1(f,l2r1x,l2r2x,l2r3x,l2r4x,l2r5x,l2r6))$
l2rxsort(f,g):=member(x,listofvars(f)) or not(member(x,listofvars(g)))$

/* bra, ora */
bra(f):=block([g],
if listp(f) then return(f),
if atom(f) then return([[f]]),
g:dnf(f),
if op(g)="%or" then g:args(g) else g:[g],
map(lambda([t],if op(t)="%and" then args(t) else [t]),g)
)$
brac(f):=block([g],
if listp(f) then return(f),
if atom(f) then return([[f]]),
g:cnf(f),
if op(g)="%and" then g:args(g) else g:[g],
map(lambda([t],if op(t)="%or" then args(t) else [t]),g)
)$
ora(f):=block([],
if not(listp(f)) then return(f),
if atom(f) then return(f)
else substpart("%or",map(lambda([g],substpart("%and",g,0)),f),0)
)$
orac(f):=block([],
if not(listp(f)) then return(f),
if atom(f) then return(f)
else substpart("%and",map(lambda([g],substpart("%or",g,0)),f),0)
)$
/* lfactor */
lfactor0(ff):=block([f,L,M,cf],L:[],M:[],
if not(listp(ff)) then f:bra(ff) else f:ff,
cf:delete({},map(lambda([t],apply(intersection,args(t))),sort(rest(listify( setdifference(powerset(setify(map(setify,f))),map(lambda([s],{s}),fullsetify(f))) )),comparelength2))),
if cf=[] then return(f),
cf:first(cf),
for fk in f do
 if subsetp(cf,setify(fk)) then
 L:append([substpart("%and",setdifference(setify(fk),cf),0)],L)
 else M:append([fk],M),
append(M,[flatten([substpart("%or",L,0),args(cf)])])
)$
lfactor(ff):=block([f,afr,bfr],
if not(listp(ff)) then f:bra(ff) else f:ff,
afr:f,bfr:[],
for cc:1 unless length(bfr)=length(afr) do (bfr:afr,afr:lfactor0(bfr)),
factor(afr))$

/* plineq */
plineq(f,[p]):=sort(append(Lminus(f,part(f,p)),bra(lineq(ora(part(f,p))))))$
/* addex */
addex(L,[v]):=l2r(map(
 lambda([Lk],append(Lk,first(bra(qe(makelist([E,vk],vk,v),p2t(substpart("%and",Lk,0))))))),
L))$
/* qex, solvex */
qex(L,[f]):=if listp(L) then qe(L,apply(p2t,f)) else p2t(L)$
solvex(f,[v]):=apply(nnsolvexx,flatten([ora(bra(f)),v]))$
/* tineq */
sorted_cineq(sols,lc,gl):=block([L,M],
L:sols,
if (lc>0 and (gl="<" or gl="<=")) or (lc<0 and (gl=">" or gl=">="))
 then L:append([first(L)],L),
L:makelist(
if k=1 then rhs(part(L,k))<lhs(part(L,k))
elseif evenp(k) and k<length(L)
 then (rhs(part(L,k+1))<lhs(part(L,k+1))) %and (lhs(part(L,k))<rhs(part(L,k)))
elseif evenp(k) and k=length(L)
 then lhs(part(L,k))<rhs(part(L,k))
else false,
k,length(L)),
if (lc>0 and (gl="<" or gl="<=")) or (lc<0 and (gl=">" or gl=">="))
 then L:rest(L),
if gl=">=" or gl="<=" then L:subst("<"="<=",L),
L:reverse(L),prt(L),
apply1(substpart("%or",L,0),delsame,eqsame)
)$
sorted_cineq5(sols,lc,gl):=block([L,M],
L:sols,
if (lc>0 and (gl="<" or gl="<=")) or (lc<0 and (gl=">" or gl=">="))
 then L:append([first(L)],L),
L:makelist(
if k=1 then rhs(part(L,k))<lhs(part(L,k))
elseif evenp(k) and k<length(L)
 then (rhs(part(L,k+1))<lhs(part(L,k+1))) %and (lhs(part(L,k))<rhs(part(L,k)))
elseif evenp(k) and k=length(L)
 then lhs(part(L,k))<rhs(part(L,k))
else false,
k,length(L)),
if (lc>0 and (gl="<" or gl="<=")) or (lc<0 and (gl=">" or gl=">="))
 then L:rest(L),
if gl=">=" or gl="<=" then L:subst("<"="<=",L),
L:reverse(L),prt(L),
apply1(substpart("%or",L,0),delsame,eqsame)
)$

tineq2(sols,lc,gl):=block([x,PLx,delx,PL,IL,CL,M,N],
/*tineq2(sols,lc,gl):=block([],*/
if sols=[] then return([[["%and"(apply(gl,[lc,0]))],[ ]]]),
x:lhs(first(sols)),
PLx:map(flatten,listify(permutations(sols))),prt(PLx),
delx(l):=map(lambda([xa],rhs(xa)),l),PL:map(delx,PLx),prt(PL),
IL:flatten(map(lambda([s],substpart("%and",makelist(part(s,k)-part(s,k+1)>=0,k,length(s)-1),0)),PL)),prt(IL),
ncondtype_out:ncondtype,postsimp_out:postsimp,ncondtype:off,postsimp:off,
CL:map(lambda([s],ora(l2r(solvex(s)))),IL),
ncondtype:ncondtype_out,postsimp:postsimp_out,prt("case condition:"),prt(CL),
CL:map(lambda([s],if atom(s) then s elseif op(s)="=" then false else s),CL),
if not(casecheck=off) then (
M:[],CL:makelist(if member(e,M) then false elseif is(substpart("%or",makelist(qex((e)%implies(m)),m,M),0)=true) then false else (M:append(M,[e]),e),e,CL),
M:[],CL:reverse(makelist(if member(e,M) then false elseif is(substpart("%or",makelist(qex((e)%implies(m)),m,M),0)=true) then false else (M:append(M,[e]),e),e,reverse(CL)))
),prt(CL),
N:delete(false,makelist(if part(CL,j)#false then [[sorted_cineq(part(PLx,j),lc,gl)],[part(CL,j)]],j,length(CL))),
/* N:sepaxa(map(flatten,bra(ora(N))),x), */
thistineq2:[PLx,PL,IL,CL,N],
prt("tineq2 returned..."),prt(N),N
/* M:map(lambda([l],sorted_cineq(l,lc,gl)),PLx),
prt(M),map("[",map("%and",M,CL))*/
)$

solvex 原理

solvex の処理手順は,人間が計算機を用いて解く場合と同じです.すなわち,...

(ステップ1)presimp,つまり,入力を qex で半代数系に変換
(オプションスイッチ)presimp:l で qex ではなく,p2t を通さない簡約 lineq を利用.sqrt が係数のみに現れる場合など,それを残したまま処理できる.presimp:off で処理を無効化

(ステップ2)結果を dnf に変換

各連言項に対して

(ステップ3)ncondeq で必要条件のうち等式のみを添加
(オプションスイッチ)ncondtype:x でターゲット変数の存在条件のみを添加.ncondtype:full で等式以外も添加.ncondtype:off で処理を無効化.ncondsimp:off で ncondeq 内部での簡約を無効化

(ステップ4)solsel,nnsolve で等式系を処理

(ステップ5)fineq,sineq で不等式系を処理
(オプションスイッチ)sineqsimp:off で sineq 内部での簡約を無効化.sineqsimp:l で sineq 内部での簡約を lineq に変更

結果をまとめて

(ステップ6)postsimp,つまり,cnf,nnscan,dnf,nnscan,再度,nnscan で簡約
(オプションスイッチ)postsimp:off で処理を無効化

(ステップ7)bracket dnf 形式で出力

toy problems

最後の簡約は重いので無効化します(笑).

(%i1) postsimp:off$
Evaluation took 0.0000 seconds (0.0000 elapsed) using 32 bytes.

放物線と直線との交わり.

(%i2) solvex(y=x^2 %and x+y=1,x,y);
Evaluation took 5.4600 seconds (11.3400 elapsed) using 315.180 MB.
(%o2) [[x = (-sqrt(5)/2)-1/2,y = sqrt(5)/2+3/2],
       [x = sqrt(5)/2-1/2,y = 3/2-sqrt(5)/2]]

領域と直線との交わり.

(%i3) solvex(y>x^2 %and x+y=1,y);
Evaluation took 2.1600 seconds (4.7800 elapsed) using 120.922 MB.
(%o3) [[(-sqrt(5)/2)-1/2 < x,x < sqrt(5)/2-1/2,y = 1-x]]

x について解くと.

(%i4) solvex(y>x^2 %and x+y=1,x);
Evaluation took 2.2100 seconds (4.9300 elapsed) using 122.540 MB.
(%o4) [[3/2-sqrt(5)/2 < y,x = 1-y,y < sqrt(5)/2+3/2]]

放物線と領域との交わり.

(%i5) solvex(y=x^2 %and x+y<1,y);
Evaluation took 2.4200 seconds (5.0200 elapsed) using 146.358 MB.
(%o5) [[(-sqrt(5)/2)-1/2 < x,x < sqrt(5)/2-1/2,y = x^2]]

x について解くと.

(%i6) solvex(y=x^2 %and x+y<1,x);
Evaluation took 6.7700 seconds (11.4300 elapsed) using 462.988 MB.
(%o6) [[0 <= y,x = -sqrt(y),y < 3/2],[0 <= y,x = sqrt(y),y < 3/2-sqrt(5)/2],
       [3/2-sqrt(5)/2 < y,x = -sqrt(y),y < sqrt(5)/2+3/2]]

結果が見難いので,x を含む式とその他とに分離.

(%i7) sepaxa(%,x);
Evaluation took 0.0000 seconds (0.0000 elapsed) using 8.836 KB.
(%o7) [[[x = -sqrt(y)],[0 <= y,y < 3/2]],
       [[x = sqrt(y)],[0 <= y,y < 3/2-sqrt(5)/2]],
       [[x = -sqrt(y)],[3/2-sqrt(5)/2 < y,y < sqrt(5)/2+3/2]]]

解の選択.

(%i8) solvex(3*x^2=2 %and 1<x);
Evaluation took 0.2100 seconds (0.5500 elapsed) using 9.020 MB.
(%o8) [[false]]

空のようなので,改めて.

(%i9) solvex(2*x^2=3 %and 1<x);
Evaluation took 2.9600 seconds (6.4500 elapsed) using 211.316 MB.
(%o9) [[x = sqrt(3)/sqrt(2)]]

不等式の連言.

(%i10) solvex(2*x^2<=3 %and 1<x);
Evaluation took 2.6700 seconds (3.7200 elapsed) using 174.558 MB.
(%o10) [[1 < x,x <= sqrt(3)/sqrt(2)]]

いきなり 3 変数ですが.

(%i11) solvex(a*x^2=b %and a<x);
Evaluation took 2.7200 seconds (7.5900 elapsed) using 215.724 MB.
(%o11) [[0 < x-a,b = a*x^2]]

変数の指定が必要です.

(%i12) solvex(a*x^2=b %and a<x,x);
Evaluation took 6.7700 seconds (19.4000 elapsed) using 487.579 MB.
(%o12) [[0 < a,a^3-b < 0,x = sqrt(b/a)],[a < x,a = 0,b = 0],
        [a < 0,a^3-b < 0,b <= 0,x = -sqrt(b/a)],[a < 0,b <= 0,x = sqrt(b/a)]]

見易く整理.

(%i13) sepaxa(%,x);
Evaluation took 0.0000 seconds (0.0000 elapsed) using 12.602 KB.
(%o13) [[[x = sqrt(b/a)],[0 < a,a^3-b < 0]],[[a < x],[a = 0,b = 0]],
        [[x = -sqrt(b/a)],[a < 0,a^3-b < 0,b <= 0]],
        [[x = sqrt(b/a)],[a < 0,b <= 0]]]

http://www.cybernet.co.jp/maple/documents/pdf/product/maple/maple17/maple17_new_feature31.pdf
の 2 つ目の例.

(%i14) solvex(a*x^2<b %and a<x,x);
Evaluation took 31.4200 seconds (47.6300 elapsed) using 2388.419 MB.
(%o14) [[0 < a,a < x,a < 0,b < 0],[0 < a,a < x,-sqrt(b/a) < x,x < sqrt(b/a)],
        [0 < b,a < x,a = 0],[0 < b,a < x,a < 0],[x < -sqrt(b/a),a < x,a < 0],
        [a < x,sqrt(b/a) < x,a < 0]]

やはり簡約は必要ですね.

(%i15) plineq(%,1);
Evaluation took 0.2200 seconds (0.7700 elapsed) using 10.936 MB.
(%o15) [[0 < a,a < x,-sqrt(b/a) < x,x < sqrt(b/a)],[0 < b,a < x,a = 0],
        [0 < b,a < x,a < 0],[a < x,sqrt(b/a) < x,a < 0],
        [x < -sqrt(b/a),a < x,a < 0],[false]]

見易く整理.

(%i16) sepaxa(bra(ora(%)),x);
Evaluation took 0.4100 seconds (0.4100 elapsed) using 30.975 MB.
(%o16) [[[a < x,-sqrt(b/a) < x,x < sqrt(b/a)],[0 < a]],
        [[a < x],[0 < b,a = 0]],[[a < x],[0 < b,a < 0]],
        [[a < x,sqrt(b/a) < x],[a < 0]],[[a < x,x < -sqrt(b/a)],[a < 0]]]

一変数半代数系の高速ソルバー cineqs2

多変数半代数系のソルバーの基礎部分として作りました.かなり速いです.

以下のものは,パッケージのロードなしで動くように,真偽判定を簡素化,また,論理結合子も and, or になっています.なお,maxima の solve が,虚数なのに %i を使わなかったり,実数なのに %i を用いて根を表示する場合は,等価でない式を返すかも知れません.

cineqs2(F):=block([lv,v,imagunit,L,M,N,k,K,J],
lv:listofvars(F),if length(lv)#1 then return([[is(F)]]) else v:first(lv),
L:sort(listify(setify(flatten(scanmap(
 lambda([e],
if atom(e) then e
elseif member(op(e),["and","or"]) then substpart("[",e,0)
elseif member(op(e),["<",">","<=",">=","=","#"])
 then map(lambda([t],if member(imagunit,listofvars(t))
  then [] else rhs(t)),subst(%i=imagunit,solve(substpart("=",e,0),v)))
else e),
F)))),"<"),
if L=[] then return([[is(subst(v=0,F))]]),
L:sort(append(L,(append([first(L)-1],L)+append(L,[last(L)+1]))/2),"<"),
M:map(lambda([e],if member(op(F),["and","or"]) then map(is,rat(subst(v=e,F)))
 else is(rat(subst(v=e,F)))),L),
if length(setify(M))=1 then return([[is(subst(v=0,F))]]),
L:append([0,0],L,[0,0]),M:append([true,true],M,[true,true]),
K:[],for k:3 thru length(M)-2 do (
if oddp(k) then
 if part(M,k) then
 (if not(part(M,k-1)) then K:append(K,[part(L,k-1)<v])
  elseif not(part(M,k-2)) then K:append(K,[part(L,k-1)<=v]) else K:K,
  if not(part(M,k+1)) then K:append(K,[v<part(L,k+1)])
  elseif not(part(M,k+2)) then K:append(K,[v<=part(L,k+1)]) else K:K)
 else K:K
else 
 if not(part(M,k-1)) and part(M,k) and not(part(M,k+1)) then
 K:append(K,[v=part(L,k)]) else K:K),
N:[],J:[],for k in K do
 if rhs(k)=v then J:[k]
 else (N:append(N,[append(J,[k])]),J:[]),
delete([],append(N,[J]))
)$

実行例.

(%i21) showtime:on$display2d:false$
Evaluation took 0.0000 seconds (0.0000 elapsed) using 8 bytes.
Evaluation took 0.0000 seconds (0.0000 elapsed) using 8 bytes.
(%i22) x*(x^2-2)*(x^2-3)<0;
Evaluation took 0.0000 seconds (0.0000 elapsed) using 1.211 KB.
(%o22) x*(x^2-3)*(x^2-2) < 0
(%i23) cineqs2(%);
Evaluation took 0.0100 seconds (0.0100 elapsed) using 336.141 KB.
(%o23) [[x < -sqrt(3)],[-sqrt(2) < x,x < 0],[sqrt(2) < x,x < sqrt(3)]]
(%i24) (((x-1 > 0) and (x^2+x-3 < 0)) or ((x-1 < 0) and (x^2+x-3 > 0)) 
 or ((x < 0) and (x+2 > 0))) and (x-1 # 0) and (x # 0) and (x+2 # 0) and (x^2+x-3 # 0);
Evaluation took 0.0000 seconds (0.0000 elapsed) using 41.609 KB.
(%o24) x-1 > 0 and x^2+x-3 < 0 or x-1 < 0 and x^2+x-3 > 0 or x < 0 and x+2 > 0
(%i25) cineqs2(%);
Evaluation took 0.0100 seconds (0.0000 elapsed) using 388.031 KB.
(%o25) [[x <= -(sqrt(13)+1)/2],[-2 < x,x < 0],[1 < x,x < (sqrt(13)-1)/2]]
(%i26) product((x-sqrt(k))^(mod(k^3,5)),k,1,10)<=0;
Evaluation took 0.0000 seconds (0.0000 elapsed) using 45.141 KB.
(%o26) (x-3)^4*(x-2)^4*(x-1)*(x-sqrt(2))^3*(x-2^(3/2))^2*(x-sqrt(3))^2
              *(x-sqrt(6))*(x-sqrt(7))^3
 <= 0
(%i27) cineqs2(%);
Evaluation took 0.0200 seconds (0.0200 elapsed) using 1.219 MB.
(%o27) [[1 <= x,x <= sqrt(2)],[x = sqrt(3)],[x = 2],
        [sqrt(6) <= x,x <= sqrt(7)],[x = 2^(3/2)],[x = 3]]

qex 原理(その2)

p2t では abs,max2,min2 も

defrule(powertk2, abs(aa), (aa^2)^(1/2))$
defrule(powertk3, max2(aa,bb), (aa+bb+abs(aa-bb))/2)$
defrule(powertk4, min2(aa,bb), (aa+bb-abs(aa-bb))/2)$

により,冪乗関数に変換して処理しています.流れを例示します.

次の式は恒真です.

(%i1) f:max2(min2(a,b),c)=min2(max2(a,c),max2(b,c));
Evaluation took 0.0000 seconds (0.0000 elapsed) using 560 bytes.
(%o1) max2(min2(a,b),c) = min2(max2(a,c),max2(b,c))

実際

(%i2) qex(f);
Evaluation took 1.9800 seconds (2.7400 elapsed) using 265.126 MB.
(%o2) true

となります.前回同様,量化の具合を見ると

(%i3) p2em(f);
Evaluation took 0.2200 seconds (0.2200 elapsed) using 22.855 MB.
(%o3) qe([[E,rt1],[E,rt2],[E,rt3],[E,rt4],[E,rt5]],
         (rt1 >= 0) %and (rt1^2 = (b-a)^2) %and (rt2 >= 0)
                    %and ((rt2+((-rt1)+b+a)/2+c)/2
                     = ((-rt5)+(rt4+c+b)/2+(rt3+c+a)/2)/2)
                    %and (rt2^2 = (c-((-rt1)+b+a)/2)^2) %and (rt3 >= 0)
                    %and (rt3^2 = (c-a)^2) %and (rt4 >= 0)
                    %and (rt4^2 = (c-b)^2) %and (rt5 >= 0)
                    %and (rt5^2 = ((rt4+c+b)/2-(rt3+c+a)/2)^2))

のように5変数です.

これに至る過程は,まず,powertk4 で,f における min2 を abs で

(%i4) applyb1(f,powertk4);
Evaluation took 0.0000 seconds (0.0000 elapsed) using 88.875 KB.
(%o4) max2(((-abs(b-a))+b+a)/2,c) = ((-abs(max2(b,c)-max2(a,c)))+max2(b,c)+max2(a,c))/2

powertk3 で max2 も abs で

(%i5) applyb1(f,powertk4,powertk3);
Evaluation took 0.0000 seconds (0.0100 elapsed) using 809.602 KB.
(%o5) (abs(c-((-abs(b-a))+b+a)/2)+c+((-abs(b-a))+b+a)/2)/2
  = ((-abs((abs(c-b)+c+b)/2-(abs(c-a)+c+a)/2))
  +(abs(c-b)+c+b)/2+(abs(c-a)+c+a)/2)/2

さらに,powertk2 で abs を sqrt で表し,前回述べた方法で束縛変数に置き換えています.

(%i6) applyb1(f,powertk4,powertk3,powertk2);
Evaluation took 0.0200 seconds (0.0100 elapsed) using 1.960 MB.
(%o6) (sqrt((c-((-sqrt((b-a)^2))+b+a)/2)^2)+c+((-sqrt((b-a)^2))+b+a)/2)/2
  = ((-sqrt(((sqrt((c-b)^2)+c+b)/2-(sqrt((c-a)^2)+c+a)/2)^2))
  +(sqrt((c-b)^2)+c+b)/2+(sqrt((c-a)^2)+c+a)/2)/2

なお,%o6 を見ると,sqrt の出現は8箇所ですが,sqrt((c-a)^2),sqrt((b-a)^2),sqrt((c-b)^2) は2箇所ずつあるので,5変数に納まっています.これが8変数だと

(%i7) p2em(f),p2etype:0;
Evaluation took 0.2800 seconds (0.2900 elapsed) using 30.215 MB.
(%o7) qe([[E,rt1],[E,rt2],[E,rt3],[E,rt4],[E,rt5],[E,rt6],[E,rt7],[E,rt8]],
         (rt1 >= 0) %and (rt1^2 = (b-a)^2) %and (rt2 >= 0)
                    %and (rt2^2 = (b-a)^2) %and (rt3 >= 0)
                    %and ((rt3+((-rt1)+b+a)/2+c)/2
                     = ((-rt8)+(rt5+c+b)/2+(rt4+c+a)/2)/2)
                    %and (rt3^2 = (c-((-rt2)+b+a)/2)^2) %and (rt4 >= 0)
                    %and (rt4^2 = (c-a)^2) %and (rt5 >= 0)
                    %and (rt5^2 = (c-b)^2) %and (rt6 >= 0)
                    %and (rt6^2 = (c-a)^2) %and (rt7 >= 0)
                    %and (rt7^2 = (c-b)^2) %and (rt8 >= 0)
                    %and (rt8^2 = ((rt7+c+b)/2-(rt6+c+a)/2)^2))

となり,これを評価するのは,...

qex 原理(その1)

qex は入力を関数 p2t を通して,qe に渡すだけの関数なので,以下は,p2t のお話です.

p2t (rational power to Tarski) とは,有理数乗を含む入力を特称量化することで多項式による系に変換する関数で,その原理は,原始式 A と x の既約分数 a/b 乗とに対して

 A(x^(a/b)) ⇔ ∃y(y=x^(a/b) ∧ A(y)) ⇔ ∃y( (if b is even then y>=0 ∧) y^b=x^a ∧ A(y) )

となる性質であり,この変換後の式を qe に処理させています.

実際の量化を内部コマンド p2em (power to ex, scanmap ver.) で確認してみましょう.

(%i1) p2em(x<sqrt(2));
Evaluation took 0.0600 seconds (0.0600 elapsed) using 1.698 MB.
(%o1) qe([[E,rt1]],(rt1 >= 0) %and (rt1^2 = 2) %and (x < rt1))

次の例では,sqrt(2) の出現は2箇所ですが,束縛変数は rt1 にまとめられています.出現が少ない場合は,出現毎に新たな変数を設ける方が処理が簡潔ですが,式が大きくなると後述のように変数の個数の節減は必須となるので,このようにしました.

(%i2) p2em(x<sqrt(2)/(3^(2/3)-sqrt(2)));
Evaluation took 0.1000 seconds (0.1300 elapsed) using 7.201 MB.
(%o2) qe([[E,rt1],[E,rt2]],
          (rt1 >= 0) %and (rt1^2 = 2) %and (rt2^3 = 9)
                     %and (x < rt1/(rt2-rt1)))

一方,次の例では,変数の個数を減らすには量化を選言全体に施した方が良さそうです.

(%i3) p2em(x<-sqrt(3) %or sqrt(3)<x);
Evaluation took 0.0700 seconds (0.0800 elapsed) using 2.432 MB.
(%o3) (qe([[E,rt1]],(rt1 >= 0) %and (rt1 < x) %and (rt1^2 = 3)))
  %or (qe([[E,rt1]],(rt1 >= 0) %and (rt1^2 = 3) %and (x < -rt1)))
(%i4) qe([],ev(%,eval));
Evaluation took 0.5200 seconds (1.1200 elapsed) using 28.026 MB.
(%o4) x^2-3 > 0

実際,この場合には

(%i5) qe([[E,rt1]],(rt1 >= 0) %and (rt1^2 = 3) %and ((x < -rt1) %or (rt1 < x)));
Evaluation took 0.2900 seconds (0.4700 elapsed) using 19.952 MB.
(%o5) x^2-3 > 0

が得られます.しかし,...

(%i6) p2em(1+sqrt(3)<x %implies 4+2*sqrt(3)<x^2);
Evaluation took 0.0400 seconds (0.0400 elapsed) using 1.239 MB.
(%o6) qe([[E,rt1]],(rt1 >= 0) %and (rt1+1 < x) %and (rt1^2 = 3))
  %implies qe([[E,rt1]],(rt1 >= 0) %and (2*rt1+4 < x^2) %and (rt1^2 = 3))
(%i7) qe([],ev(%,eval));
Evaluation took 0.6100 seconds (1.1800 elapsed) using 28.977 MB.
(%o7) true

に対しては

(%i8) qe([[E,rt1]],(rt1 >= 0) %and (rt1^2 = 2) %and ((rt1+1 < x) %implies (2*rt1+4 < x^2)));
Evaluation took 0.1700 seconds (0.3700 elapsed) using 3.884 MB.
(%o8) (x-1 < 0) %or (x^2-2*x-1 <= 0) %or (x^4-8*x^2+8 > 0)

となり,これは

(%i9) fullall(%);
Evaluation took 0.7300 seconds (1.1000 elapsed) using 31.596 MB.
(%o9) false

のように恒真にはなりません.その理由は,特称量化が否定を潜ると全称量化となることであり,それが生じるか形か否かを判定させるより,各原始式に量化を map する(安易な?)設計になっています.