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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Determinant and permanent step by step in the Tcl 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 Tcl programming language

Source code in the tcl programming language

package require math::linearalgebra
package require struct::list

proc permanent {matrix} {
    for {set plist {};set i 0} {$i<[llength $matrix]} {incr i} {
	lappend plist $i
    }
    foreach p [::struct::list permutations $plist] {
	foreach i $plist j $p {
	    lappend prod [lindex $matrix $i $j]
	}
	lappend sum [::tcl::mathop::* {*}$prod[set prod {}]]
    }
    return [::tcl::mathop::+ {*}$sum]
}


set mat {
    {1 2 3 4}
    {4 5 6 7}
    {7 8 9 10}
    {10 11 12 13}
}
puts [::math::linearalgebra::det $mat]
puts [permanent $mat]


  

You may also check:How to resolve the algorithm FizzBuzz step by step in the BrightScript (for Roku) programming language
You may also check:How to resolve the algorithm CSV to HTML translation step by step in the Fortran programming language
You may also check:How to resolve the algorithm Spelling of ordinal numbers step by step in the Nim programming language
You may also check:How to resolve the algorithm Closures/Value capture step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Evolutionary algorithm step by step in the SequenceL programming language