(*******************************************************************
This file was generated automatically by the Mathematica front end.
It contains Initialization cells from a Notebook file, which
typically will have the same name as this file except ending in
".nb" instead of ".m".

This file is intended to be loaded into the Mathematica kernel using
the package loading commands Get or Needs.  Doing so is equivalent
to using the Evaluate Initialization Cells menu command in the front
end.

DO NOT EDIT THIS FILE.  This entire file is regenerated
automatically each time the parent Notebook file is saved in the
Mathematica front end.  Any changes you make to this file will be
overwritten.
***********************************************************************)

BeginPackage["rqdecomposition`"]

(*Copyright \[Copyright] Frank Hespeler 2010*)



rqdecomposition::usage=
  "Computes an rq decomposition for a numerical input matrix m. Thus R.Transpose[Q] = m where R is a upper triangular matrix and Q is a column orthonormal matrix. Within the computation the standard QRDecomposition from Mathematica is employed. In doing this pivotation is yet not available. Input: matrix m Output: matrix pair {R, ConjugateTranspose[Q]}"

Begin["`Private`"]

rqdecomposition[m_,tol_:10^(-15)]:=(n1=Dimensions[m][[1]];
    n2=Dimensions[m][[2]];
    If[n1\[LessEqual]n2,
      perm=Reverse[IdentityMatrix[n1]];{qtemp,rtemp}=
        QRDecomposition[ConjugateTranspose[perm.m]];
      rfin=perm.ConjugateTranspose[rtemp].perm;qfin=perm.qtemp;
      If[Abs[Max[rfin.qfin-m]]\[LessEqual]tol,{rfin,
          ConjugateTranspose[qfin]},{{rfin,
            ConjugateTranspose[qfin]},{ToString[
              "Warning: the absolue value of the  maximal difference in elements between r.ConjugateTranspose[q] and m exceeds the tolerance level tol:"]\
,tol}}],perm=Reverse[IdentityMatrix[n2]];{qtemp,rtemp}=
        QRDecomposition[ConjugateTranspose[m.perm]];
      rfin=ConjugateTranspose[rtemp].perm;qfin=perm.qtemp.perm;
      If[Abs[Max[rfin.qfin-m]]\[LessEqual]tol,{rfin,
          ConjugateTranspose[qfin]},{{rfin,
            ConjugateTranspose[qfin]},{ToString[
              "Warning: the absolue value of the  maximal difference in elements between r.ConjugateTranspose[q] and m exceeds the tolerance level tol:"]\
,tol}}]])
End[]
EndPackage[]