#! /bin/sh

     ###################################### 
     ##### Model Transformation Tools #####
     ######################################

# Bourne shell script: sm2smo_r
# state matrices to cobserver form
# P.J.Gawthrop  12 Jan 1997
# Copyright (c) P.J.Gawthrop 1998

###############################################################
## Version control history
###############################################################
## $Id$
## $Log$
## Revision 1.3  2000/09/11 10:53:54  peterg
## Uses 1st io of mimo to create siso
##
## Revision 1.2  1998/01/22 13:25:22  peterg
## Added END;; to output file.
##
## Revision 1.1  1998/01/22 13:16:43  peterg
## Initial revision
##
###############################################################

Nu=`mtt_getsize $1 u`
Ny=`mtt_getsize $1 y`

if [ "$Nu" = "1" ]; then
  if [ "$Ny" = "1" ]; then
    blurb=' for this siso system'    
  else
    blurb=" using first output of $Ny"
  fi
else
  if [ "$Ny" = "1" ]; then
    blurb=" using first input of $Nu"
  else
    blurb=" using first input of $Nu and using first output of $Ny"
  fi
fi
# Inform user
echo Creating $1_smo.r $blurb

# Remove the old log file
rm -f sm2smo_r.log

# Use reduce to accomplish the transformation
reduce >sm2smo_r.log << EOF

in "$1_def.r";
in "$1_sm.r";
in "$1_tf.r";

%Read the formatting function
in "$MTTPATH/trans/reduce_matrix.r";


OFF Echo;
OFF Nat;

% Find  observibility matrix.
MATRIX MTTObs(MTTNx,MTTNX);
MTTCA := MTTC;
FOR i := 1:MTTNx DO
  BEGIN
   FOR j := 1:MTTNx DO 
      MTTObs(i,j) := MTTCA(1,j);
   MTTCA := MTTCA*MTTA;
  END;

%Canonical forms:
% This statement makes Gs a scalar transfer function
Gs := MTTtf(1,1);

% Numerator and denominator polynomials
bs := num(gs);
as := den(gs);

% extract coeficients and divide by coeff of s^n
% reverse operator puts list with highest oder coeffs first
ai := reverse(coeff(as,s));
a0 := first(ai);
MTTn := length(ai) - 1;


% Normalised coeficients;
ai := reverse(coeff(as/a0,s));
bi := reverse(coeff(bs/a0,s));
MTTm := length(bi)-1;

% Zap the (unity) first element of ai list;
ai := rest(ai);

% System in observer form
% MTTA_o matrix
matrix MTTA_o(MTTNx,MTTNx);

% First column is ai coefficients
for i := 1:MTTNx do
  MTTA_o(i,1) := -part(ai,i);

% (MTTNx-1)x(MTTNx-1) unit matrix in upper right-hand corner (if n>1)
if MTTNx>1 then
  for i := 1:MTTNx-1 do
    MTTA_o(i,i+1) := 1;

% C_o vector;
matrix MTTC_o(1,MTTNx);
  MTTC_o(1,1) := 1;
MTTC_o;
% B_o vector;
matrix MTTB_o(MTTNx,1);
for i := 1:MTTm+1 do
  MTTB_o(i+MTTNx-MTTm-1,1) := part(bi,i);

% D_o
MTTD_o := MTTD;

%Observability matrix of observer form
MATRIX MTTObs_o(MTTNx,MTTNX);
MTTCA := MTTC_o;
FOR i := 1:MTTNx DO
  BEGIN
   FOR j := 1:MTTNx DO 
      MTTObs_o(i,j) := MTTCA(1,j);
   MTTCA := MTTCA*MTTA_o;
  END;

% Transformation matrix;
MTTT_o := MTTObs^(-1)*MTTObs_o;


%Create the output file
OUT "$1_smo.r";

%Write out the matrices.

% Observable form
MTT_Matrix := MTTA_o$ 
MTT_Matrix_name := "MTTA_o"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := MTTNx$
Reduce_Matrix()$

MTT_Matrix := MTTB_o$ 
MTT_Matrix_name := "MTTB_o"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := 1$
Reduce_Matrix()$

MTT_Matrix := MTTC_o$ 
MTT_Matrix_name := "MTTC_o"$
MTT_Matrix_n := 1$
MTT_Matrix_m := MTTNx$
Reduce_Matrix()$

MTT_Matrix := MTTD_o$ 
MTT_Matrix_name := "MTTD_o"$
MTT_Matrix_n := 1$
MTT_Matrix_m := 1$
Reduce_Matrix()$

write "%  -Observability matrix";
MTT_Matrix := MTTObs$ 
MTT_Matrix_name := "MTTObs"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := MTTNx$
Reduce_Matrix()$


write "%  -Observability matrix - Observer form";
MTT_Matrix := MTTObs_o$ 
MTT_Matrix_name := "MTTObs_o"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := MTTNx$
Reduce_Matrix()$

write "%  - Transformation matrix - Observer form";
MTT_Matrix := MTTT_o$ 
MTT_Matrix_name := "MTTT_o"$
MTT_Matrix_n := MTTNx$
MTT_Matrix_m := MTTNx$
Reduce_Matrix()$


write "END;;";
SHUT "$1_smo.r";
quit;

EOF

# Now invoke the standard error handling.
mtt_error_r sm2smo_r.log
