%mathpiper,def="MatrixRowAndColumnOps"

//Retract("MatrixRowReplace",*);
//Retract("MatrixRowSwap",*);
//Retract("MatrixRowStack",*);
//Retract("MatrixColumnReplace",*);
//Retract("MatrixColumnSwap",*);
//Retract("MatrixColumnAugment",*);

MatrixRowReplace( M_IsMatrix, iRow_IsPositiveInteger, v_IsVector )_(Length(v)=Length(M[1])) <--
[
    If( Not IsBound(iDebug), iDebug := False );
    If( iDebug, Tell(MatrixRowReplace,{M,iRow,v}) );
    Local(mRows,nCols);
    {mRows,nCols} := Dimensions(M);
    If( iRow <= mRows, DestructiveReplace(M,iRow,v) );
    M;
];


MatrixRowSwap( M_IsMatrix, iRow1_IsPositiveInteger, iRow2_IsPositiveInteger )_
                          (And(iRow1<=Dimensions(M)[1],iRow2<=Dimensions(M)[1])) <--
[
    If( Not IsBound(iDebug), iDebug := False );
    If( iDebug, Tell(MatrixRowSwap,{M,iRow1,iRow2}) );
    Local(row1,row2);
    If( iRow1 != iRow2, 
       [ 
         row1 := MatrixRow(M,iRow1);         row2 := MatrixRow(M,iRow2);
         DestructiveReplace(M,iRow1,row2);   DestructiveReplace(M,iRow2,row1);
       ]
    );
    M;
];


MatrixRowStack( M_IsMatrix, v_IsVector )_(Length(v)=Dimensions(M)[1]) <--
[
    If( Not IsBound(iDebug), iDebug := False );
    If( iDebug, Tell(MatrixRowStack,{M,v}) );
    Local(mRows,nCols,newMat,ir);
    {mRows,nCols} := Dimensions(M);
    newMat        := ZeroMatrix(mRows+1,nCols);
    For(ir:=1,ir<mRows+1,ir++)
       [  MatrixRowReplace(newMat,ir,MatrixRow(M,ir));  ];
    MatrixRowReplace(newMat,mRows+1,v);
    newMat;
];


MatrixColumnReplace( M_IsMatrix, jCol_IsPositiveInteger, v_IsVector )_(Length(v)=Dimensions(M)[2]) <--
[
    If( Not IsBound(iDebug), iDebug := False );
    If( iDebug, Tell(MatrixColumnReplace,{M,jCol,v}) );
    Local(mRows,nCols,MT);
    {mRows,nCols} := Dimensions(M);
    If( jCol <= nCols, 
       [ MT:=Transpose(M); DestructiveReplace(MT,jCol,v); M:=Transpose(MT); ]
    ); 
    M;
];


MatrixColumnSwap( M_IsMatrix, jCol1_IsPositiveInteger, jCol2_IsPositiveInteger )_
                          (And(jCol1<=Dimensions(M)[2],jCol2<=Dimensions(M)[2])) <--
[
    If( Not IsBound(iDebug), iDebug := False );
    If( iDebug, Tell(MatrixColumnSwap,{M,jCol1,jCol2}) );
    Local(MT);
    MT := Transpose(M);
    MT := MatrixRowSwap(MT,jCol1,jCol2);
    M  := Transpose(MT);
];


MatrixColumnAugment( M_IsMatrix, v_IsVector )_(Length(v)=Dimensions(M)[2]) <--
[
    If( Not IsBound(iDebug), iDebug := False );
    If( iDebug, Tell(MatrixColumnAugment,{M,v}) );
    Local(mRows,nCols,newMat,ir);
    Local(MT,MA);
    MT := Transpose(M);
    MT := MatrixRowStack(MT,v);
    MA := Transpose(MT);
];

%/mathpiper

    
    
.   




