logo
down
shadow

Coq vector permutations


Coq vector permutations

By : Jordan Herman
Date : November 22 2020, 03:01 PM
I think the issue was by ths following , I need to reason about vectors' permutations in Coq. The standard library only includes permutation definitions for lists. As my first attempt, I tried to mimic it for vectors as: , I used these simple lemmas:
code :
Lemma list_of_vec_eq (A : Type) (n : nat) (v1 v2 : vector A n) :
  list_of_vec v1 = list_of_vec v2 -> v1 = v2.
Admitted.

Lemma list_of_vec_length {A : Type} {n : nat} {v : vector A n} :
  length (list_of_vec v) = n.
Admitted.

Lemma list_of_vec_vec_of_list {A : Type} {l : list A} :
  list_of_vec (vec_of_list l) = l.
Admitted.
Section VPermutation_properties.

  Require Import Sorting.Permutation.

  Variable A:Type.

  Lemma ListVecPermutation {n} {l1 l2} {v1 v2}:
    l1 = list_of_vec v1 ->
    l2 = list_of_vec v2 ->
    Permutation l1 l2 ->
    VPermutation A n v1 v2.
  Proof.
    intros H1 H2 P; revert n v1 v2 H1 H2.
    dependent induction P; intros n v1 v2 H1 H2.
    - dependent destruction v1; inversion H1; subst.
      dependent destruction v2; inversion H2; subst.
      apply vperm_nil.
    - dependent destruction v1; inversion H1; subst.
      dependent destruction v2; inversion H2; subst.
      apply vperm_skip.
      now apply IHP.
    - do 2 (dependent destruction v1; inversion H1; subst).
      do 2 (dependent destruction v2; inversion H2; subst).
      apply list_of_vec_eq in H5; subst.
      apply vperm_swap.
    - assert (n = length l').
      { pose proof (Permutation_length P1) as len.
        subst.
        now rewrite list_of_vec_length in len.
      }
      subst.
      apply vperm_trans with (l' := vec_of_list l').
      -- apply IHP1; auto.
         now rewrite list_of_vec_vec_of_list.
      -- apply IHP2; auto.
         now rewrite list_of_vec_vec_of_list.
  Qed.

End VPermutation_properties.


Share : facebook icon twitter icon
All permutations c++ with vector<int> and backtracking

All permutations c++ with vector<int> and backtracking


By : Trushna
Date : March 29 2020, 07:55 AM
it fixes the issue I'm trying generate all permutations of an vector to training backtracking technique but my code don't work for all vectors (works to size to vectors) , The simple way is to use standard algorithm: std::next_permutation
code :
void print(const std::vector<int>& v)
{
    for (int e : v) {
        std::cout << " " << e;
    }
    std::cout << std::endl;
}

int main()
{
    std::vector<int> v = {1,2,3};
    // vector should be sorted at the beginning.

    do {
        print(v);
    } while (std::next_permutation(v.begin(), v.end()));
}
unique permutations of zero-one-vector in R

unique permutations of zero-one-vector in R


By : ashi kr
Date : March 29 2020, 07:55 AM
may help you . Here's one way to walk the permutations. I still think there is a better way but haven't figured it out yet.
This function looks at an array of 1's an 0's and tries to move the right most 1 to the left if possible. (Basically thinking of the vector as a binary number and trying to find the next largest number with exactly n bits)
code :
next_x <- function(x) {
    i <- tail(which(diff(x)==1),1)
    if (length(i)>0) {
        x[c(i, i+1)]<-c(1,0)
        x[(i+1):length(x)] <- sort(x[(i+1):length(x)])
    } else {
        stop("no more moves")
    }
    x
}
x <- c(0,0,0,0,1,1,1)
while(!all(x==c(1,1,1,0,0,0,0))) {
    x <- next_x(x)
    print(x)
}
Print all permutations of a vector of characters in R

Print all permutations of a vector of characters in R


By : iamcharan
Date : March 29 2020, 07:55 AM
How to permute elements of a vector by another vector to obtain a matrix of permutations

How to permute elements of a vector by another vector to obtain a matrix of permutations


By : Vijay Sahayaraja
Date : March 29 2020, 07:55 AM
hope this fix your issue Actually, since A and B are always defined, respectively, as a vector of zeros and a vector of ones, this computation is much easier than you may think. The only constraints you should respect concerns B, which shoud not be empty and it's elements cannot be greater than or equal to the number of elements in A... because after that threshold A will become a vector of ones and calculating its permutations will be just a waste of CPU cycles.
Here is the core function of the script, which undertakes the creation of the unique permutations of 0 and 1 given the target vector X:
code :
function p = uperms(X)
    n = numel(X);
    k = sum(X);
    c = nchoosek(1:n,k);
    m = size(c,1);

    p = zeros(m,n);
    p(repmat((1-m:0)',1,k) + m*c) = 1;
end
clear();
clc();

% Define the main parameter: the number of elements in A...
A_len = 4;

% Compute the elements of B accordingly...
B_len = A_len - 1;
B_seq = 1:B_len;

% Compute the possible mixtures of A and B...
X = tril(ones(A_len));
X = X(B_seq,:);

% Compute the unique permutations...
p = [];

for i = B_seq
    p = [p; uperms(X(i,:).')];
end
p =
     1     0     0     0
     0     1     0     0
     0     0     1     0
     0     0     0     1
     1     1     0     0
     1     0     1     0
     1     0     0     1
     0     1     1     0
     0     1     0     1
     0     0     1     1
     1     1     1     0
     1     1     0     1
     1     0     1     1
     0     1     1     1
recursive permutations using vector

recursive permutations using vector


By : Vu Hai
Date : March 29 2020, 07:55 AM
wish helps you I have this function that is suppose to return all possible permutation of integers inside the vector. The code is based from an existing code that does a permutation of strings, I tried to remodeled it to work on vectors but apparently, they dont work similarly as I thought.. I'll appreciate any help that you could offer thanks; , Here you go:
shadow
Privacy Policy - Terms - Contact Us © voile276.org