kill(all)$

maxima :fluid :Levi-Civita symbol (sub,position,value),version 1.0
peter.vlasschaert@gmail.com,27/08/2019

a:[4,5,2,1,3];

e:copy(a);

b:length(a);

first element : array 'a'

a[1];

first(a);

init : values
----------------

k:0;

d:genmatrix(lambda([i,j], 0), b, 2);

init : matrix
---------------

  c:genmatrix(lambda([i,j], 0), 2*b, 2);

How to to find change position : permutation matrix.
=======================================

    for i:1  thru b do(
        if a[i] ≠ i then(
                     j:i+1,
            while (  j ≤ b and a[j] ≠ i ) do
            (j:j+1,print(i,j)),
            if b < j then print (" no permutation"),
                        a[j] :a[i],                      
                        a[i] : i,                        
                        k:k+1,
                    c[k,1]:i,
                    c[k,2]:j
                               )  
                          );                
                      

      
     
        
        
        
;

c;

result : k columns (position)
=====================
see: below how to use.

d:genmatrix(lambda([i,j], 0), k, 2);

for i:1 thru k do(
   for j:1 thru 2 do(
d[i,j]: c[i,j]
    ));

matrix d : position matrix for swap
==========================
see : below definition.

d;

array
====

f1:[x[4],x[5],x[2],x[1],x[3]];

f:f1;

how to swap   (two numbers of array)
============================

temp:f[1];
f[1] :f[4];
f[4]:temp;

f;

temp:f[2];
f[2] :f[3];
f[3]:temp;

f;

temp:f[3];
f[3] :f[5];
f[5]:temp;

f;

How to use : matrix 'd'
------------------------------

g:[x[4],x[5],x[2],x[1],x[3]];

p1:g[1];

1e) command :length 'number of rows'
-----------------------------------------------------

q:length(d);

2e) matrix 'd'
-----------------
first element array : position = 1

g;
for i:1 thru q do(    
temp1:g[d[i,1]] ,
g[d[i,1]] :g[d[i,2]] ,
g[d[i,2]]:temp1,
print(g)
);



;

use :sub

definition : to swap two elements
---------------------------------------------

swap_el(s1,s2,ss):=
(

temp2:ss[s1],
ss[s1] :ss[s2],
ss[s2]:temp2
);

same as above ' use definition'
------------------------------------------

v:[x[4],x[5],x[2],x[1],x[3]];

v;
for i:1 thru q do(        
 ss:v,           /*matrix for interchange */
 s1:d[i,1]  ,  /*position matrix :source*/
 s2:d[i,2]  ,  /*position matrx : destination */
swap_el(s1,s2,ss), /*swap of two elements*/
print(v)       /*new matrix after use of swap */
);

w:[4,5,2,1,3];

algopermut(w,w1,w2,k):=block([],
     w1 :length(w),
     w2:genmatrix(lambda([ii,jj], 0), 2*w1, 2),
         for i:1  thru w1 do(
        if w[i] ≠ i then(
                     j:i+1,
            while (  j ≤ w1 and w[j] ≠ i ) do
            (j:j+1),
            if w1 < j then print (" no permutation"),
                       w[j] :w[i],                      
                        w[i] : i,                        
                        k:k+1,                        
                    w2[k,1]:i,
                    w2[k,2]:j
                                      ) 
                          ), return([[w2],k])
);            

w2;

p1:algopermut(w,w1,w2,0);

p1a:flatten(p1);

p1b:p1a[1];
p1c:p1a[2];

 d1:genmatrix(lambda([ii,jj], 0), p1c, 2);

for i:1 thru p1c do(
   for j:1 thru 2 do(
d1[i,j]: p1b[i,j]
    ));

p1d:d1;

w3:[4,5,2,1,3];

copy of w

output : w3
========

w3;
for i:1 thru p1c do(        
 ss:w3,           /*matrix for interchange */
 s1:p1d[i,1]  ,  /*position matrix :source*/
 s2:p1d[i,2]  ,  /*position matrx : destination */
swap_el(s1,s2,ss), /*swap of two elements*/
print(w3)     /*new matrix after use of swap */    
);

p1e:k;

sign :   k : even  = >  1
            k : odd    =>  -1

p1f:mod(k,2);

? how to understand the code.

sigε :    ε(4,5,2,1,3)

sigε(k) :=block([],
if mod(k,2) = 0 then
 return(1)
else
   return(-1));

value
====

p1g:sigε(k);