Skip to content

Commit

Permalink
Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidFCrouse committed Jan 28, 2019
1 parent d05ca86 commit 63854c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 20 deletions.
35 changes: 17 additions & 18 deletions Mathematical Functions/Combinatorics/perm.cpp
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
/**PERM A C++ implementation of a function to calculate the matrix
* permanent allowing for rows and columns to be skipped is desired
* (operate on a submatrix). The permanent is equivalent to
* calculating the determininant in the standard summation manner
* taught in school, except all of the minus signs are replaced with
* plus signs.
/**PERM A C++ implementation of a function to calculate the matrix
* permanent allowing for rows and columns to be skipped is desired
* (operate on a submatrix). The permanent is equivalent to
* calculating the determininant in the standard summation manner
* taught in school, except all of the minus signs are replaced with
* plus signs.
*
*INPUTS: A An mXn matrix of real doubles. If m<=n, then the standard matrix
* permanent is found. If m>n, then the permanent of A' is found to
* be consistent with the permanents of A and A' being equal in
* square matrices. Empty matrices have a permanent of one by
* definition.
* boolRowsSkip An optional list of rows in A that should be skipped when
* computing the matrix permanent. This is an mX1 or 1Xm
* boolean vector where a 1 means that row should be
* skipped. If omitted or an empty matrix is passed, no rows
* are skipped.
* boolRowsSkip An optional list of rows in A that should be skipped when
* computing the matrix permanent. This is an mX1 or 1Xm boolean
* vector where a 1 means that row should be skipped. If omitted or
* an empty matrix is passed, no rows are skipped.
* boolColsSkip An optional list of columsn in A that should be skipped
* when computing the matrix permanent. This is an nX1 or
* 1Xn boolean vector where a 1 means that column should be
* skipped. If omitted or an empty matrix is passed, no
* columns are skipped.
* when computing the matrix permanent. This is an nX1 or 1Xn
* boolean vector where a 1 means that column should be skipped. If
* omitted or an empty matrix is passed, no columns are skipped.
*
*OUTPUTS: val The matrix permanent of A.
*
Expand All @@ -32,7 +30,7 @@
* The algorithm is run in Matlab using the command format
* val=perm(A)
*
* October 2014 David F. Crouse, Naval Research Laboratory, Washington D.C.
*October 2014 David F. Crouse, Naval Research Laboratory, Washington D.C.
*/
/*(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.*/

Expand Down Expand Up @@ -135,7 +133,9 @@ void mexFunction(const int nlhs, mxArray *plhs[], const int nrhs, const mxArray
if(numRowsKept<=numColsKept) {
A=reinterpret_cast<double*>(mxGetData(prhs[0]));
} else {
std::swap(numRowsKept, numColsKept);
std::swap(numRowsKept, numColsKept);
std::swap(numRow, numCol);
std::swap(boolRowsSkip, boolColsSkip);

//This is freed using mxDestroyArray
AMat=mxCreateDoubleMatrix(numRowsKept,numColsKept,mxREAL);
Expand Down Expand Up @@ -163,7 +163,6 @@ void mexFunction(const int nlhs, mxArray *plhs[], const int nrhs, const mxArray
rows2Keep[curIdx]=curIdx+cumSkip;
curIdx++;
}

}
}

Expand Down
26 changes: 24 additions & 2 deletions Mathematical Functions/Combinatorics/perm.m
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
% computing the matrix permanent. This is an mX1 or 1Xm boolean
% vector where a 1 means that row should be skipped. If omitted or
% an empty matrix is passed, no rows are skipped.
% boolColsSkip An optional list of columsn in A that should be skipped when
% boolColsSkip An optional list of columns in A that should be skipped when
% computing the matrix permanent. This is an nX1 or 1Xn boolean
% vector where a 1 means that column should be skipped. If omitted
% or an empty matrix is passed, no columns are skipped.
Expand All @@ -33,7 +33,7 @@
%finite precision errors. When skipping rows or columns, only Ryser's
%algorithm is used.
%
%EXAMPLE:
%EXAMPLE 1:
%Suppose that one is given a boolean matrix where 1 indicates that a target
%gates with a measurement and a 0 indicates that it does not gate. To deal
%with the possibility of missed detections, one often adds dummy
Expand All @@ -50,6 +50,20 @@
% %Leading to 1546 possible assignments to measurements and missed
% %detections.
%
%EXAMPLE 2:
%This is an example with skip lists.
% numRow=12;
% numCol=4;
% A=magic(numRow);
% A=A(:,1:numCol);
% boolRowsSkip=false(numRow,1);
% boolColsSkip=false(numCol,1);
% boolRowsSkip(3)=true;
% boolColsSkip([1,4])=true;
% perm(A,boolRowsSkip,boolColsSkip)
% perm(A',boolColsSkip,boolRowsSkip)
%The permanent values should both be 494938.
%
%REFERENCES:
%[1] L. G. Valiant, "The complexity of computing the permanent,"
% Theoretical Computer Science, vol. 8, no. 2, pp. 189-201, 1979.
Expand Down Expand Up @@ -104,6 +118,14 @@
temp=m;
m=n;
n=temp;

temp=mTotal;
mTotal=nTotal;
nTotal=temp;

temp=boolRowsSkip;
boolRowsSkip=boolColsSkip;
boolColsSkip=temp;
end

%Set the mapping of indices of the rows in the submatrix to indices in
Expand Down

0 comments on commit 63854c9

Please sign in to comment.