# About the Second Curve (ref. to my note).
#
# With (c1,c2,...,c9) := (w,z,y,x,1-x,1-y,1-z,1-w,1),
# we ask when 
#
#       sum_{i=2,...,9} |ci-c_{i-1}|        (*)
#
# achieves its minimum on the first curve and what is it.
#
# Lookin at the curve, one finds that the quantity (*) is
# equals to
#
#       2(w+z-w+y-z+y-x)+1-2x=4(y-x)+1.
#
# Now think y,z,w are functions of x, so we are looking for
# the point for which
#      
#              y'-1=0.
#
# Let g(x,y) be defined by Fxy. It can be shown that the following
# systems of equations has to be solved.
#
#          g(x,y)=0,
#          g + g = 0,
#           x   y
#
# We will solve it with Newton iterations near the
# point
#
#         x0=.28467183943928598030e-1 and
#         y0=.87156040292818742103.
#
# OneStep2(x0,y0,x1,y1) is one step of Newton Iterations that
# advance from (x0,y0) to (x,y).
#
#                                        Ren-Cang Li, June 1, 1996
#                                        na.rcli@na-net.ornl.gov

uxy:=diff(fxy,x)+diff(fxy,y):
uxyh:=convert(uxy,horner,x):

xuxyh:=convert(diff(uxy,x),horner,x):
yuxyh:=convert(diff(uxy,y),horner,x):

gets9odr6d:=proc(x0,y0,x1,y1)
           local A1, B1, xi0, yi0, xi1, yi1, eps, niter;
           eps:=10^(-Digits+20):
	   xi0:=x0: yi0:=y0: 
           A1:=matrix(2,2,[subs(x=xi0,y=yi0,xfxyh),
			   subs(x=xi0,y=yi0,yfxyh),
                           subs(x=xi0,y=yi0,xuxyh),
                           subs(x=xi0,y=yi0,yuxyh)]):
           B1:=linsolve(A1,vector([subs(x=xi0,y=yi0,fxyh),
				   subs(x=xi0,y=yi0,uxyh)])):
           xi1:=xi0-B1[1]: yi1:=yi0-B1[2]:

           niter:=1:
           while ( abs(xi1-xi0)>eps*abs(xi1) or abs(yi1-yi0)>eps*abs(yi1) ) 
		  and niter < 101 do
                  niter:=niter+1;
                  xi0:=xi1: yi0:=yi1:
                  A1:=matrix(2,2,[subs(x=xi0,y=yi0,xfxyh),
                                  subs(x=xi0,y=yi0,yfxyh),
                                  subs(x=xi0,y=yi0,xuxyh),
                                  subs(x=xi0,y=yi0,yuxyh)]):
                  B1:=linsolve(A1,vector([subs(x=xi0,y=yi0,fxyh),
              			          subs(x=xi0,y=yi0,uxyh)])):
                  xi1:=xi0-B1[1]: yi1:=yi0-B1[2]:
           od:
           if niter=101 or niter>101 then
              print(`over 100 iterations in gets9odr6d`);
           fi;
	   x1:=xi1: y1:=yi1:
	   end:
