<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<!DOCTYPE MAN SYSTEM "../../manrev.dtd">
<MAN>
  <LANGUAGE>eng</LANGUAGE>
  <TITLE>aff2ab</TITLE>
  <TYPE>Scilab Function</TYPE>
  <DATE>April 1993</DATE>
  <SHORT_DESCRIPTION name="aff2ab"> linear (affine) function to A,b conversion</SHORT_DESCRIPTION>
  <CALLING_SEQUENCE>
    <CALLING_SEQUENCE_ITEM>[A,b]=aff2ab(afunction,dimX,D [,flag])  </CALLING_SEQUENCE_ITEM>
  </CALLING_SEQUENCE>
  <PARAM>
    <PARAM_INDENT>
      <PARAM_ITEM>
        <PARAM_NAME>afunction</PARAM_NAME>
        <PARAM_DESCRIPTION>
          <SP>: a scilab function <VERB> Y =fct(X,D) </VERB> where <VERB>X, D, Y</VERB> are <VERB>list</VERB> of matrices</SP>
        </PARAM_DESCRIPTION>
      </PARAM_ITEM>
      <PARAM_ITEM>
        <PARAM_NAME>dimX</PARAM_NAME>
        <PARAM_DESCRIPTION>
          <SP>: a p x 2 integer matrix (<VERB>p</VERB> is the number of matrices in <VERB>X</VERB>)</SP>
        </PARAM_DESCRIPTION>
      </PARAM_ITEM>
      <PARAM_ITEM>
        <PARAM_NAME>D</PARAM_NAME>
        <PARAM_DESCRIPTION>
          <SP>: a <VERB>list</VERB> of real matrices (or any other valid Scilab object).</SP>
        </PARAM_DESCRIPTION>
      </PARAM_ITEM>
      <PARAM_ITEM>
        <PARAM_NAME>flag</PARAM_NAME>
        <PARAM_DESCRIPTION>
          <SP>: optional parameter (<VERB>flag='f'</VERB> or <VERB>flag='sp'</VERB>)</SP>
        </PARAM_DESCRIPTION>
      </PARAM_ITEM>
      <PARAM_ITEM>
        <PARAM_NAME>A</PARAM_NAME>
        <PARAM_DESCRIPTION>
          <SP>: a real matrix</SP>
        </PARAM_DESCRIPTION>
      </PARAM_ITEM>
      <PARAM_ITEM>
        <PARAM_NAME>b</PARAM_NAME>
        <PARAM_DESCRIPTION>
          <SP>: a real vector having same row dimension as <VERB>A</VERB></SP>
        </PARAM_DESCRIPTION>
      </PARAM_ITEM>
    </PARAM_INDENT>
  </PARAM>
  <DESCRIPTION>
    <P><VERB>aff2ab</VERB>  returns the matrix representation of an affine
    function (in the canonical basis).</P>
    <P><VERB>afunction</VERB> is a function with imposed syntax:
    <VERB> Y=afunction(X,D) </VERB> where <VERB> X=list(X1,X2,...,Xp) </VERB> is
    a list of p real matrices, and <VERB> Y=list(Y1,...,Yq) </VERB> is
    a list of q real real matrices which depend linearly of
    the <VERB> Xi</VERB>'s. The (optional) input <VERB> D</VERB> contains 
    parameters needed to compute Y as a function of X. 
    (It is generally a list of matrices).</P>
    <P><VERB> dimX</VERB> is a p x 2 matrix: <VERB>dimX(i)=[nri,nci]</VERB>
    is the actual number of rows and columns of matrix <VERB>Xi</VERB>.
    These dimensions determine <VERB>na</VERB>, the column dimension of 
    the resulting matrix <VERB>A</VERB>: <VERB>na=nr1*nc1 +...+ nrp*ncp</VERB>.</P>
    <P>
    If the optional parameter <VERB>flag='sp'</VERB> the resulting <VERB>A</VERB>
    matrix is returned as a sparse matrix.</P>
    <P>
    This function is useful to solve a system of linear equations
    where the unknown variables are matrices.</P>
  </DESCRIPTION>
  <EXAMPLE>
<![CDATA[
// Lyapunov equation solver (one unknown variable, one constraint)
deff('Y=lyapunov(X,D)','[A,Q]=D(:);Xm=X(:); Y=list(A''*Xm+Xm*A-Q)')
A=rand(3,3);Q=rand(3,3);Q=Q+Q';D=list(A,Q);dimX=[3,3];
[Aly,bly]=aff2ab(lyapunov,dimX,D);
[Xl,kerA]=linsolve(Aly,bly); Xv=vec2list(Xl,dimX); lyapunov(Xv,D)
Xm=Xv(:); A'*Xm+Xm*A-Q

// Lyapunov equation solver with redundant constraint X=X'
// (one variable, two constraints) D is global variable
deff('Y=ly2(X,D)','[A,Q]=D(:);Xm=X(:); Y=list(A''*Xm+Xm*A-Q,Xm''-Xm)')
A=rand(3,3);Q=rand(3,3);Q=Q+Q';D=list(A,Q);dimX=[3,3];
[Aly,bly]=aff2ab(ly2,dimX,D);
[Xl,kerA]=linsolve(Aly,bly); Xv=vec2list(Xl,dimX); ly2(Xv,D)

// Francis equations
// Find matrices X1 and X2 such that:
// A1*X1 - X1*A2 + B*X2 -A3 = 0
// D1*X1 -D2 = 0 
deff('Y=bruce(X,D)','[A1,A2,A3,B,D1,D2]=D(:),...
[X1,X2]=X(:);Y=list(A1*X1-X1*A2+B*X2-A3,D1*X1-D2)')
A1=[-4,10;-1,2];A3=[1;2];B=[0;1];A2=1;D1=[0,1];D2=1;
D=list(A1,A2,A3,B,D1,D2);
[n1,m1]=size(A1);[n2,m2]=size(A2);[n3,m3]=size(B);
dimX=[[m1,n2];[m3,m2]];
[Af,bf]=aff2ab(bruce,dimX,D);
[Xf,KerAf]=linsolve(Af,bf);Xsol=vec2list(Xf,dimX)
bruce(Xsol,D)

// Find all X which commute with A
deff('y=f(X,D)','y=list(D(:)*X(:)-X(:)*D(:))')
A=rand(3,3);dimX=[3,3];[Af,bf]=aff2ab(f,dimX,list(A));
[Xf,KerAf]=linsolve(Af,bf);[p,q]=size(KerAf);
Xsol=vec2list(Xf+KerAf*rand(q,1),dimX);
C=Xsol(:); A*C-C*A
 ]]>
  </EXAMPLE>
  <SEE_ALSO>
    <SEE_ALSO_ITEM>
      <LINK>linsolve</LINK>
    </SEE_ALSO_ITEM>
  </SEE_ALSO>
</MAN>
