#  Approximate exp(t X+t Y) by
#
#   exp(xi t X) exp( gamm t Y) ... exp(xi   tX) .... exp(gamm t Y) exp(xi t X)
#         1              1               m+1                  1           1
#
#  It has (m+1)+m free parameters xi_1, ..., xi_{m+1} and gamm_1, ..., gamm_m
#
#  This is the same with s9odr4 except xi_1=0.
#
#                              Ren-Cang Li, June 1, 1996
#                              na.rcli@na-net.ornl.gov

with(linalg): 
m := 2:
cxs:=vector(2*m+1,[0,cx[1],1-cx[1],1,1]): 

# Notice: the index of cx[i] here differs from cx_j in Ren-Cang Li's
# thesis by 1, i.e., cx_{i+1} (thesis) = cx[i] (here) and cx_1 (thesis) = 0.

cys:=vector(2*m,[cy[1],1/2,1-cy[1],1]):

xi[1]:=cxs[1]: gamm[1]:=cys[1]:
for i from 2 to m do
    xi[i]:=cxs[i]-cxs[i-1]: gamm[i]:=cys[i]-cys[i-1]:
od:
xi[m+1]:=cxs[m+1]-cxs[m]:
#
#  Get exp(xi   t X)
#            m+1
#
  a1:=xi[m+1]: b1:=0: 
  a3:=0: b3:=0:
#
# Successive applications
#
  for i from m by -1 to 1 do
#
#     exp(gamm t Y) (***) exp(gamm t Y)
#             i                   i
#
      Newb1:=b1 + 2*gamm[i]:
      Newa3:=a3 + a1^2*gamm[i]/6:
      Newb3:=b3 + a1*b1*gamm[i]/6 + a1*gamm[i]^2/6:
      b1:=simplify(expand( Newb1 )):
      a3:=simplify(expand( Newa3 )):
      b3:=simplify(expand( Newb3 )):
#
#     exp( xi t X) (***) exp( xi t X)
#            i                  i
      Newa1:=a1 + 2*xi[i]:
      Newa3:=a3 - a1*b1*xi[i]/6 - b1*xi[i]^2/6:
      Newb3:=b3 - b1^2*xi[i]/6:
      a1:=simplify(expand( Newa1 )):
      a3:=simplify(expand( Newa3 )):
      b3:=simplify(expand( Newb3 )):
od:

f[1]:=a3: f[2]:=b3:
