#####################################################################
# c05.mpl
#
# A Maple program demonstrating how Matlab's Genmex extension
# can be run on a different computer directly from Maple
#
# Kenrick Bingham, 1996
# http://www.hut.fi/~kenny/
#####################################################################

c05 := proc(f, x, x0) 
  local iii,jjj,jac,xn,xp,fvecn,fvecp,rcparg; 
  global _c05_remotedir_,_c05_remotehost_,_c05_remoteuser_,\
        _c05_scriptfile_,_c05_fjfile_,_c05_funfile_;
  read `c05config.mpl`;
  c05config();
  rcparg:=cat(_c05_remoteuser_,`@`,_c05_remotehost_,`:`,_c05_remotedir_);

#####################################################################
# write argument subprogram (ASP) 
# for evaluating the function and its jacobian in matlab
#####################################################################
  writeto(_c05_fjfile_);
  lprint(`function [n,xv,fvec,fjac,ldfjac,iflag]=c05fj(n,xv,fvec,fjac,ldfjac,iflag)`);
  for iii from 1 by 1  to nops(x) do lprint(x[iii]='xv'(iii),`;`) od;
  lprint(`if iflag ~=2`);
  for iii from 1 by 1 to nops(f) do
    lprint(`  fvec`(iii)=f[iii],`;`);
  od;
  lprint(`else`);
  jac:=linalg[jacobian](f,x);
  for iii from 1 by 1 to nops(f) do
    for jjj from 1 by 1 to nops(x) do
       lprint(`  fjac`(iii,jjj)=jac[iii,jjj],`;`);
    od
  od;
  lprint(`end`);
  writeto(terminal);
  print(cat(`ASP for c05 (function and jacobian) generated in file `, \
            _c05_fjfile_,`.`));

#####################################################################
# write argument subprogram for evaluating the function in matlab
#####################################################################
  writeto(_c05_funfile_);
  lprint(`function [n,xv,fvec,iflag]=c05fj(n,xv,fvev,iflag)`);
  for iii from 1 by 1  to nops(x) do lprint(x[iii]='xv'(iii),`;`) od;
  lprint(`  fvec`=f,`;`);
  writeto(terminal);
  print(cat(`ASP for c05 (function only) generated in file `,\
             _c05_funfile_,`.`));

#####################################################################
# write matlab script file to do the calculations
#####################################################################
  writeto(_c05_scriptfile_);
  lprint(`p=path;`);
  lprint(`path(p,'/l/genmex');`);
  lprint(`genmex`);
  for iii from 1 by 1 to nops(x0) do 
    lprint(`alkuarvaus`(iii)=x0[iii],`;`) 
  od;
  lprint(`[xn,fvecn]=c05nbf('c05fun',alkuarvaus);`);
  lprint(`[xp,fvecp]=c05pbf('c05fj',alkuarvaus);`);
  lprint(cat(`save `,_c05_remotedir_,`xn.out xn -ascii;`));
  lprint(cat(`save `,_c05_remotedir_,`xp.out xp -ascii;`));
  lprint(cat(`save `,_c05_remotedir_,`fvecn.out fvecn -ascii;`));
  lprint(cat(`save `,_c05_remotedir_,`fvecp.out fvecp -ascii;`));
  lprint(`quit`);

#####################################################################
# copy the files generated to the remote host,
# perform the calculation in matlab at the remote host
# copy the output files back and read them in
#####################################################################
  writeto(terminal);
  system(cat(`rcp `,_c05_fjfile_,` `,_c05_funfile_,` `,\
             _c05_scriptfile_,` `,rcparg));
  writeto(terminal);
  print(cat(`Files `,_c05_fjfile_,`,`,_c05_funfile_,` and `,_c05_scriptfile_, \
            ` copied to `,_c05_remotehost_,`.`));
  system(cat(`rsh `,_c05_remotehost_,` -l `,_c05_remoteuser_,` 'cd `,\
             _c05_remotedir_,`; matlab <`,_c05_scriptfile_,` >/dev/null'`));
  print(cat(`Matlab calculations done in `,_c05_remotehost_,`.`));
  system(cat(`rcp `,rcparg,`xn.out `,rcparg,`xp.out `,rcparg,`fvecn.out `,\
                    rcparg,`fvecp.out .`));
  print(cat(`Output files copied from `,_c05_remotehost_,`.`));
  readlib(readdata);
  xn:=readdata(`xn.out`,nops(x));
  xp:=readdata(`xp.out`,nops(x));
  fvecn:=readdata(`fvecn.out`,nops(f));
  fvecp:=readdata(`fvecp.out`,nops(f));

#####################################################################
# return and array with the following components:
# 1. xn,    the roots calculated with C05NBF (no jacobian needed)
# 2. fvecn, the value of the function at xn
# 3. xp,    the roots calculated with C05PBF (jacobian needed)
# 4. fvecp, the value of the function at xp
#####################################################################
  print([xn,fvecn,xp,fvecp]);
  end:
