User:Mate2code/code
From Wikiversity
< User:Mate2code(Redirected from User:Lipedia/code)
When I have self written programs that are of possible interest to others I make them available here.
Contents |
bin2svg [edit]
perm2invset [edit]
function [y] = perm2invset(x) % Input: permutation of 1...n as row vector % Output: binary row vector with nchoosek(n,2) places describing the inversion set n = size(x,2) ; m = nchoosek(n,2) ; M = sortrows( nchoosek(1:n,2) , 2 ) ; Y = zeros(1,m) ; for a=1:m if x(M(a,1)) > x(M(a,2)) Y(a) = 1 ; end end y = Y ; end
| example |
|---|
>> perm2invset([4 3 2 1]) ans = 1 1 1 1 1 1 >> perm2invset([1 2 3 4 5 6 8 7]) ans = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 For the last example compare Permutations by cycle type. |
perm2inv [edit]
function [y] = perm2inv(x) % Permutation to inversion vector wide = size(x,2) ; Y = zeros(1,wide) ; for m=1:wide for n=1:m-1 if x(n) > x(m) Y(m) = Y(m)+1 ; end end end y = Y ; end
| example |
|---|
>> perm2inv([4 3 2 1]) ans = 0 1 2 3 >> perm2inv([1 2 3 4 5 6 8 7]) ans = 0 0 0 0 0 0 0 1 For the last example compare Permutations by cycle type. |
cv2wp [edit]
See Walsh permutation.
function [y] = cv2wp(x) % Input: Compression vector (row) % Output: Walsh permutation (row) % x = 7 5 6 Small = size(x,2) ; Big = 2^Small ; % Small = 3 % Big = 8 X = cv2cm(x) ; % X = 1 1 0 % 1 0 1 % 1 1 1 Bincount = ones(Small,Big) ; for m=1:Small for n=1:Big if mod( floor( (n-1)/2^(m-1) ) , 2) == 0 Bincount(m,n) = 0 ; end end end % Bincount = 0 1 0 1 0 1 0 1 % 0 0 1 1 0 0 1 1 % 0 0 0 0 1 1 1 1 Binwalsh = zeros(Small,Big) ; for n=1:Small N = zeros(1,Big) ; for m=1:Small if X(m,n) == 1 N = xor ( N , Bincount(m,:) ) ; end end Binwalsh(n,1:Big) = N ; end % Binwalsh = 0 1 1 0 1 0 0 1 % 0 1 0 1 1 0 1 0 % 0 0 1 1 1 1 0 0 WP = zeros(1,Big) ; for n=1:Big N = 0 ; for m=1:Small if Binwalsh(m,n) == 1 N = N + 2^(m-1) ; end end WP(n) = N ; end % WP = 0 3 5 6 7 4 2 1 y = WP ; end
| example |
|---|
>> cv2wp([8 12 6 3]) ans = 0 8 12 4 6 14 10 2 3 11 15 7 5 13 9 1 Compare Walsh permutation#Gray code permutation * bit reversal permutation |
wp2cv [edit]
function [y] = wp2cv(x) % Walsh permutation to compression vector % Used: char2mat, walshf2num Bin = char2mat(dec2bin(x)) ; Short = size(Bin,2) ; Y = zeros(1,Short) ; for m=1:Short Y(Short+1-m) = walshf2num( Bin(:,m)' ) ; end y = Y ; end
| example |
|---|
>> wp2cv([0 3 5 6 7 4 2 1]) ans = 7 5 6 |
seq2mat [edit]
function [y] = seq2mat(x) % puts sequence x in the antidiagonals of a square matrix y if size(x,1) > 1 x = x' ; end s = size(x,2) ; % s is the length of the string m = 1 ; while (m^2+m)/2 <= s m = m+1 ; end m = m-1 ; % m is the size of the square matrix if (m^2+m)/2 < s % maybe s is no triangular number s = (m^2+m)/2 ; % now it is end I = zeros(s,2) ; % I lists the places in the matrix M where the elements of the sequence x go. i=1; j=0; for a=1:m j=j+a; A = 1:a ; I(i:j,1:2) = [ A' rot90(A) ] ; i=i+a; end M = zeros(m) ; % create mxm matrix M for m=1:s M( I(m,1) , I(m,2) ) = x(m) ; end y = M ; end
| example |
|---|
>> seq2mat(1:21) ans = 1 2 4 7 11 16 3 5 8 12 17 0 6 9 13 18 0 0 10 14 19 0 0 0 15 20 0 0 0 0 21 0 0 0 0 0 |
mat2seq [edit]
function [y] = mat2seq(x) % orders the antidiagonals of the matrix in a sequence high=size(x,1); long=size(x,2); if high<long m=high; M=x(1:m,1:m); elseif long<high m=long; M=x(1:m,1:m); else m=high; M=x; end % m is the size of the square matrix M s = (m^2+m)/2 ; % s is the length of the sequence S S = zeros(1,s) ; I = zeros(s,2) ; % I lists the places in the matrix M where the elements of the antidiagonals are found i=1; j=0; for a=1:m j=j+a; A = 1:a ; I(i:j,1:2) = [ A' rot90(A) ] ; i=i+a; end for a=1:s S(a) = M( I(a,1) , I(a,2) ) ; % creation of sequence S end y = S ; end
| example |
|---|
M =
1 2 3
4 5 6
7 8 9
>> mat2seq(M)
ans =
1 2 4 3 5 7
|
A function mat2seq_sym for symbolic inputs can be created by changing S=zeros(1,s); to S=sym(zeros(1,s));.
walshf2num [edit]
function [y] = walshf2num(x) % Binary Walsh function (row vector) to row number in Walsh matrix % Function doesn't test if the input actually is a Walsh function, % only counts changes between 0 and 1 and uses the fact that % Gray code permutation * bit reversal permutation % permutes the natural ordered Hadamard matrix (Walsh matrix) % in the sequency ordered Hadamard matrix. Long = size(x,2) ; Short = log2(Long) ; Changes = zeros(1,Long-1) ; for m = 1:Long-1 Changes(m) = xor( x(m) , x(m+1) ) ; end Changes = sum(Changes) ; Gray = bitxor( Changes , floor(Changes/2) ) ; % element on place "Changes" in Gray code permutation y = bin2dec( fliplr( dec2bin(Gray,Short) ) ) ; % bit reversal of "Gray" end
| example |
|---|
>> walshf2num( [0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0] ) ans = 12 |
char2mat [edit]
function [y] = char2mat(x) % Turns a matrix of type CHAR without spaces % into a matrix of type DOUBLE. high = size(x,1) ; wide = size(x,2) ; Y = zeros(high,wide) ; for m = 1:high for n = 1:wide Y(m,n) = str2double( x(m,n) ) ; end end y = Y ; end
OEIS files [edit]
- https://oeis.org/A195467/a195467_5.txt - Gray code permutation powers
A195467 and binary array
A197819 - https://oeis.org/A209805/a209805_1.txt -
A209805, compare Partition related number triangles