From e099fbf30bb65130d70b19f862e7aaa4360a7eb0 Mon Sep 17 00:00:00 2001
From: Daniel Waggoner <dwaggoner@frbatlanta.org>
Date: Mon, 23 Apr 2012 18:56:24 -0400
Subject: [PATCH] added permutation routine (cherry picked from commit
 9db5ba7a4483581a203df01888367897ec4369f9)

---
 include/dw_matrix.h | 10 +++++++---
 matrix/dw_matrix.c  | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 3 deletions(-)

diff --git a/include/dw_matrix.h b/include/dw_matrix.h
index 07c7c0f..61a6de2 100644
--- a/include/dw_matrix.h
+++ b/include/dw_matrix.h
@@ -125,19 +125,22 @@
  ElementP(x,i) - x      : TPermutation
                  i      : int
                  Returns: L-value int containing the ith element.  The index i
-                          is zero-based.  It must be case that 
+                          is zero-based.  It must be case that  i < UseP(x) and
 
-                                   i <= ElementP(x,i) <= DimP(x).
+                                    i <= ElementP(x,i) < DimP(x).
                                     
  pElementP(x)  - x      : TPermutation
                  Returns: pointer to 0th element of the array storing the 
                           permutation.
 
-
  The representation as a product of transpositions used by TPermutation
      
            (0,ElementP(x,0))*...*(UseP(x)-1,ElementP(x,UseP(x)-1)
 
+ In order to have a unique representation, we require that
+
+           UseP(x) - 1 < ElementP(x,UseP(x) - 1)
+
 *******************************************************************************/
 
 /*******************************************************************************
@@ -552,6 +555,7 @@ TMatrix InMatrix(FILE *f, TMatrix X);
 TPermutation CreatePermutation(int m);
 void FreePermutation(TPermutation X);
 TPermutation InitializePermutation(TPermutation X, int *p, int m);
+int* ExtractPermutation(int *p, TPermutation X);
 TPermutation TranspositionPermutation(TPermutation X, int i, int j, int m);
 TPermutation EquatePermutation(TPermutation X, TPermutation Y);
 TMatrix PermutationMatrix(TMatrix X, TPermutation Y);
diff --git a/matrix/dw_matrix.c b/matrix/dw_matrix.c
index 128fd7e..bb96e93 100644
--- a/matrix/dw_matrix.c
+++ b/matrix/dw_matrix.c
@@ -5251,6 +5251,39 @@ TPermutation InitializePermutation(TPermutation X, int *p, int m)
   return X;
 }
 
+/*
+   Assumes:
+     p : integer array of length m or null pointer
+     X : m-permutation
+
+   Results:
+     Allocates the array p if it was null.  Fills p with the permutation which
+     maps i to p[i].
+*/
+int* ExtractPermutation(int *p, TPermutation X)
+{
+  int i, j;
+  if (!X) 
+    { 
+      dw_Error(NULL_ERR);
+      return (int*)NULL;
+    }
+  if (!p && !(p=dw_malloc(DimP(X)*sizeof(int))))
+    {
+      dw_Error(MEM_ERR);
+      return (int*)NULL;
+    }
+  for (i=DimP(X)-1; i >= 0; i--) p[i]=i;
+  for (i=UseP(X)-1; i >= 0; i--)
+    if (X[i] != i)
+      {
+	j=p[i];
+	p[i]=p[X[i]];
+	p[X[i]]=j;
+      }
+  return p;
+}
+
 /*
    Assumes
      X : m-permutation or null pointer
-- 
GitLab