How to resolve the algorithm Determinant and permanent step by step in the zkl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determinant and permanent step by step in the zkl programming language

Table of Contents

Problem Statement

For a given matrix, return the determinant and the permanent of the matrix. The determinant is given by while the permanent is given by In both cases the sum is over the permutations

σ

{\displaystyle \sigma }

of the permutations of 1, 2, ..., n. (A permutation's sign is 1 if there are an even number of inversions and -1 otherwise; see parity of a permutation.) More efficient algorithms for the determinant are known: LU decomposition, see for example wp:LU decomposition#Computing the determinant. Efficient methods for calculating the permanent are not known.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Determinant and permanent step by step in the zkl programming language

Source code in the zkl programming language

var [const] GSL=Import("zklGSL");	// libGSL (GNU Scientific Library)
fcn perm(A){  // should verify A is square
   numRows:=A.rows;
   Utils.Helpers.permute(numRows.toList()).reduce(  // permute(0,1,..numRows)
      'wrap(s,pm){ s + numRows.reduce('wrap(x,i){ x*A[i,pm[i]] },1.0) },
      0.0)
}
test:=fcn(A){
   println(A.format());
   println("Permanent: %.2f, determinant: %.2f".fmt(perm(A),A.det()));
};

A:=GSL.Matrix(2,2).set(1,2, 3,4);
B:=GSL.Matrix(4,4).set(1,2,3,4, 4,5,6,7, 7,8,9,10, 10,11,12,13);
C:=GSL.Matrix(5,5).set( 0, 1, 2, 3, 4,  5, 6, 7, 8, 9, 10,11,12,13,14, 
		       15,16,17,18,19, 20,21,22,23,24);
T(A,B,C).apply2(test);

  

You may also check:How to resolve the algorithm Create a file on magnetic tape step by step in the UNIX Shell programming language
You may also check:How to resolve the algorithm Text processing/1 step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Arithmetic/Integer step by step in the Lua programming language
You may also check:How to resolve the algorithm Date format step by step in the RPL programming language
You may also check:How to resolve the algorithm Digital root/Multiplicative digital root step by step in the Red programming language