Determinant and permanent: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
|||
Line 18: | Line 18: | ||
{{trans|Nim}} |
{{trans|Nim}} |
||
< |
<syntaxhighlight lang="11l">F s_permutations(seq) |
||
V items = [[Int]()] |
V items = [[Int]()] |
||
L(j) seq |
L(j) seq |
||
Line 66: | Line 66: | ||
print(‘perm: ’perm(a)‘ det: ’det(a)) |
print(‘perm: ’perm(a)‘ det: ’det(a)) |
||
print(‘perm: ’perm(b)‘ det: ’det(b)) |
print(‘perm: ’perm(b)‘ det: ’det(b)) |
||
print(‘perm: ’perm(c)‘ det: ’det(c))</ |
print(‘perm: ’perm(c)‘ det: ’det(c))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 79: | Line 79: | ||
and two ASSIST macros (XDECO,XPRNT) to keep it as short as possible. |
and two ASSIST macros (XDECO,XPRNT) to keep it as short as possible. |
||
It works on OS/360 family (MVS,z/OS), on DOS/360 family (z/VSE) use GETVIS,FREEVIS instead of GETMAIN,FREEMAIN. |
It works on OS/360 family (MVS,z/OS), on DOS/360 family (z/VSE) use GETVIS,FREEVIS instead of GETMAIN,FREEMAIN. |
||
< |
<syntaxhighlight lang="360asm">* Matrix arithmetic 13/05/2016 |
||
MATARI START |
MATARI START |
||
STM R14,R12,12(R13) save caller's registers |
STM R14,R12,12(R13) save caller's registers |
||
Line 230: | Line 230: | ||
Q DS F sub matrix q((n-1)*(n-1)+1) |
Q DS F sub matrix q((n-1)*(n-1)+1) |
||
YREGS |
YREGS |
||
END MATARI</ |
END MATARI</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 239: | Line 239: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">printMatrix: function [m][ |
||
loop m 'row -> print map row 'val [pad to :string .format:".2f" val 6] |
loop m 'row -> print map row 'val [pad to :string .format:".2f" val 6] |
||
print "--------------------------------" |
print "--------------------------------" |
||
Line 314: | Line 314: | ||
print ["A: perm ->" perm A "det ->" det A] |
print ["A: perm ->" perm A "det ->" det A] |
||
print ["B: perm ->" perm B "det ->" det B] |
print ["B: perm ->" perm B "det ->" det B] |
||
print ["C: perm ->" perm C "det ->" det C]</ |
print ["C: perm ->" perm C "det ->" det C]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 324: | Line 324: | ||
=={{header|C}}== |
=={{header|C}}== |
||
C99 code. By no means efficient or reliable. If you need it for serious work, go find a serious library. |
C99 code. By no means efficient or reliable. If you need it for serious work, go find a serious library. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <string.h> |
#include <string.h> |
||
Line 368: | Line 368: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
A method to calculate determinant that might actually be usable: |
A method to calculate determinant that might actually be usable: |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <tgmath.h> |
#include <tgmath.h> |
||
Line 445: | Line 445: | ||
printf("det: %19f\n", det(x, N)); |
printf("det: %19f\n", det(x, N)); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 542: | Line 542: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[[1, 2], [3, 4]] |
<pre>[[1, 2], [3, 4]] |
||
Line 556: | Line 556: | ||
A recursive version, no libraries required, it doesn't use much consing, only for the list of columns to skip |
A recursive version, no libraries required, it doesn't use much consing, only for the list of columns to skip |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun determinant (rows &optional (skip-cols nil)) |
(defun determinant (rows &optional (skip-cols nil)) |
||
(let* ((result 0) (sgn -1)) |
(let* ((result 0) (sgn -1)) |
||
Line 600: | Line 600: | ||
(dolist (m (list m2 m3 m4 m5)) |
(dolist (m (list m2 m3 m4 m5)) |
||
(format t "~a determinant: ~a, permanent: ~a~%" m (determinant m) (permanent m)) ) |
(format t "~a determinant: ~a, permanent: ~a~%" m (determinant m) (permanent m)) ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 612: | Line 612: | ||
This requires the modules from the [[Permutations#D|Permutations]] and [[Permutations_by_swapping#D|Permutations by swapping]] tasks. |
This requires the modules from the [[Permutations#D|Permutations]] and [[Permutations_by_swapping#D|Permutations by swapping]] tasks. |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">import std.algorithm, std.range, std.traits, permutations2, |
||
permutations_by_swapping1; |
permutations_by_swapping1; |
||
Line 666: | Line 666: | ||
a.permanent, a.determinant); |
a.permanent, a.determinant); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[[ 1, 2], |
<pre>[[ 1, 2], |
||
Line 687: | Line 687: | ||
{{libheader| System.SysUtils}} |
{{libheader| System.SysUtils}} |
||
{{Trans|Java}} |
{{Trans|Java}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Determinant_and_permanent; |
program Determinant_and_permanent; |
||
Line 816: | Line 816: | ||
writeln(#10'Permanent: ', perm(a): 3: 2); |
writeln(#10'Permanent: ', perm(a): 3: 2); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Enter with matrix size: |
<pre>Enter with matrix size: |
||
Line 837: | Line 837: | ||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
This requires the 'list' library for '''(in-permutations n)''' and the 'matrix' library for the built-in '''(determinant M)'''. |
This requires the 'list' library for '''(in-permutations n)''' and the 'matrix' library for the built-in '''(determinant M)'''. |
||
< |
<syntaxhighlight lang="lisp"> |
||
(lib 'list) |
(lib 'list) |
||
(lib 'matrix) |
(lib 'matrix) |
||
Line 866: | Line 866: | ||
(permanent M) → 6778800 |
(permanent M) → 6778800 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">USING: fry kernel math.combinatorics math.matrices sequences ; |
||
: permanent ( matrix -- x ) |
: permanent ( matrix -- x ) |
||
dup square-matrix? [ "Matrix must be square." throw ] unless |
dup square-matrix? [ "Matrix must be square." throw ] unless |
||
[ dim first <iota> ] keep |
[ dim first <iota> ] keep |
||
'[ [ _ nth nth ] map-index product ] map-permutations sum ;</ |
'[ [ _ nth nth ] map-index product ] map-permutations sum ;</syntaxhighlight> |
||
Example output: |
Example output: |
||
<pre> |
<pre> |
||
Line 891: | Line 891: | ||
{{works with|gforth|0.7.9_20170427}} |
{{works with|gforth|0.7.9_20170427}} |
||
Requiring a permute.fs file from the [[Permutations_by_swapping#Forth|Permutations by swapping]] task. |
Requiring a permute.fs file from the [[Permutations_by_swapping#Forth|Permutations by swapping]] task. |
||
< |
<syntaxhighlight lang="forth">S" fsl-util.fs" REQUIRED |
||
S" fsl/dynmem.seq" REQUIRED |
S" fsl/dynmem.seq" REQUIRED |
||
[UNDEFINED] defines [IF] SYNONYM defines IS [THEN] |
[UNDEFINED] defines [IF] SYNONYM defines IS [THEN] |
||
Line 928: | Line 928: | ||
m3{{ lmat lufact |
m3{{ lmat lufact |
||
lmat det F. 3 m3{{ permanent F. CR |
lmat det F. 3 m3{{ permanent F. CR |
||
lmat lu-free</ |
lmat lu-free</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 934: | Line 934: | ||
Please find the compilation and example run at the start of the comments in the f90 source. Thank you. |
Please find the compilation and example run at the start of the comments in the f90 source. Thank you. |
||
<syntaxhighlight lang="fortran"> |
|||
<lang FORTRAN> |
|||
!-*- mode: compilation; default-directory: "/tmp/" -*- |
!-*- mode: compilation; default-directory: "/tmp/" -*- |
||
!Compilation started at Sat May 18 23:25:42 |
!Compilation started at Sat May 18 23:25:42 |
||
Line 1,006: | Line 1,006: | ||
end program f |
end program f |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">sub make_S( M() as double, S() as double, i as uinteger, j as uinteger ) |
||
'removes row j, column i from the matrix, stores result in S() |
'removes row j, column i from the matrix, stores result in S() |
||
dim as uinteger ii, jj, size=ubound(M), ix, jx |
dim as uinteger ii, jj, size=ubound(M), ix, jx |
||
Line 1,055: | Line 1,055: | ||
print deperminant( A(), true ), deperminant( A(), false ) |
print deperminant( A(), true ), deperminant( A(), false ) |
||
print deperminant( B(), true ), deperminant( B(), false ) |
print deperminant( B(), true ), deperminant( B(), false ) |
||
print deperminant( C(), true ), deperminant( C(), false )</ |
print deperminant( C(), true ), deperminant( C(), false )</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
-2 10 |
-2 10 |
||
Line 1,064: | Line 1,064: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
From the task description: |
From the task description: |
||
< |
<syntaxhighlight lang="funl">def sgn( p ) = product( (if s(0) < s(1) xor i(0) < i(1) then -1 else 1) | (s, i) <- p.combinations(2).zip( (0:p.length()).combinations(2) ) ) |
||
def perm( m ) = sum( product(m(i, sigma(i)) | i <- 0:m.length()) | sigma <- (0:m.length()).permutations() ) |
def perm( m ) = sum( product(m(i, sigma(i)) | i <- 0:m.length()) | sigma <- (0:m.length()).permutations() ) |
||
def det( m ) = sum( sgn(sigma)*product(m(i, sigma(i)) | i <- 0:m.length()) | sigma <- (0:m.length()).permutations() )</ |
def det( m ) = sum( sgn(sigma)*product(m(i, sigma(i)) | i <- 0:m.length()) | sigma <- (0:m.length()).permutations() )</syntaxhighlight> |
||
Laplace expansion (recursive): |
Laplace expansion (recursive): |
||
< |
<syntaxhighlight lang="funl">def perm( m ) |
||
| m.length() == 1 and m(0).length() == 1 = m(0, 0) |
| m.length() == 1 and m(0).length() == 1 = m(0, 0) |
||
| otherwise = sum( m(i, 0)*perm(m(0:i, 1:m.length()) + m(i+1:m.length(), 1:m.length())) | i <- 0:m.length() ) |
| otherwise = sum( m(i, 0)*perm(m(0:i, 1:m.length()) + m(i+1:m.length(), 1:m.length())) | i <- 0:m.length() ) |
||
Line 1,077: | Line 1,077: | ||
def det( m ) |
def det( m ) |
||
| m.length() == 1 and m(0).length() == 1 = m(0, 0) |
| m.length() == 1 and m(0).length() == 1 = m(0, 0) |
||
| otherwise = sum( (-1)^i*m(i, 0)*det(m(0:i, 1:m.length()) + m(i+1:m.length(), 1:m.length())) | i <- 0:m.length() )</ |
| otherwise = sum( (-1)^i*m(i, 0)*det(m(0:i, 1:m.length()) + m(i+1:m.length(), 1:m.length())) | i <- 0:m.length() )</syntaxhighlight> |
||
Test using the first set of definitions (from task description): |
Test using the first set of definitions (from task description): |
||
< |
<syntaxhighlight lang="funl">matrices = [ |
||
( (1, 2), |
( (1, 2), |
||
(3, 4)), |
(3, 4)), |
||
Line 1,097: | Line 1,097: | ||
for m <- matrices |
for m <- matrices |
||
println( m, 'perm: ' + perm(m), 'det: ' + det(m) )</ |
println( m, 'perm: ' + perm(m), 'det: ' + det(m) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,109: | Line 1,109: | ||
=={{header|GLSL}}== |
=={{header|GLSL}}== |
||
< |
<syntaxhighlight lang="glsl"> |
||
mat4 m1 = mat3(1, 2, 3, 4, |
mat4 m1 = mat3(1, 2, 3, 4, |
||
5, 6, 7, 8 |
5, 6, 7, 8 |
||
Line 1,116: | Line 1,116: | ||
float d = det(m1); |
float d = det(m1); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
===Implementation=== |
===Implementation=== |
||
This implements a naive algorithm for each that follows from the definitions. It imports the permute packge from the [[Permutations_by_swapping#Go|Permutations by swapping]] task. |
This implements a naive algorithm for each that follows from the definitions. It imports the permute packge from the [[Permutations_by_swapping#Go|Permutations by swapping]] task. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,172: | Line 1,172: | ||
fmt.Println(determinant(m2), permanent(m2)) |
fmt.Println(determinant(m2), permanent(m2)) |
||
fmt.Println(determinant(m3), permanent(m3)) |
fmt.Println(determinant(m3), permanent(m3)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,179: | Line 1,179: | ||
</pre> |
</pre> |
||
===Ryser permanent=== |
===Ryser permanent=== |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,222: | Line 1,222: | ||
} |
} |
||
return |
return |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,230: | Line 1,230: | ||
===Library determinant=== |
===Library determinant=== |
||
'''go.matrix:''' |
'''go.matrix:''' |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,246: | Line 1,246: | ||
{7, 5, 3}, |
{7, 5, 3}, |
||
{6, 1, 8}}).Det()) |
{6, 1, 8}}).Det()) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,253: | Line 1,253: | ||
</pre> |
</pre> |
||
'''gonum/mat:''' |
'''gonum/mat:''' |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 1,269: | Line 1,269: | ||
7, 5, 3, |
7, 5, 3, |
||
6, 1, 8}))) |
6, 1, 8}))) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,277: | Line 1,277: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">sPermutations :: [a] -> [([a], Int)] |
||
sPermutations = flip zip (cycle [1, -1]) . foldl aux [[]] |
sPermutations = flip zip (cycle [1, -1]) . foldl aux [[]] |
||
where |
where |
||
Line 1,335: | Line 1,335: | ||
, [[2, 5], [4, 3]] |
, [[2, 5], [4, 3]] |
||
, [[4, 4], [2, 2]] |
, [[4, 4], [2, 2]] |
||
]</ |
]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Matrix: |
<pre>Matrix: |
||
Line 1,395: | Line 1,395: | ||
Here is code for computing the determinant and permanent very inefficiently, via [[wp:Cramer's rule|Cramer's rule]] (for the determinant, as well as its analog for the permanent): |
Here is code for computing the determinant and permanent very inefficiently, via [[wp:Cramer's rule|Cramer's rule]] (for the determinant, as well as its analog for the permanent): |
||
<syntaxhighlight lang="haskell"> |
|||
<lang Haskell> |
|||
outer :: (a->b->c) -> [a] -> [b] -> [[c]] |
outer :: (a->b->c) -> [a] -> [b] -> [[c]] |
||
outer f [] _ = [] |
outer f [] _ = [] |
||
Line 1,447: | Line 1,447: | ||
perm m = (mul m (padj m)) !! 0 !! 0 |
perm m = (mul m (padj m)) !! 0 !! 0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Line 1,455: | Line 1,455: | ||
For example, given the matrix: |
For example, given the matrix: |
||
< |
<syntaxhighlight lang="j"> i. 5 5 |
||
0 1 2 3 4 |
0 1 2 3 4 |
||
5 6 7 8 9 |
5 6 7 8 9 |
||
10 11 12 13 14 |
10 11 12 13 14 |
||
15 16 17 18 19 |
15 16 17 18 19 |
||
20 21 22 23 24</ |
20 21 22 23 24</syntaxhighlight> |
||
Its determinant is 0. When we use IEEE floating point, we only get an approximation of this result: |
Its determinant is 0. When we use IEEE floating point, we only get an approximation of this result: |
||
< |
<syntaxhighlight lang="j"> -/ .* i. 5 5 |
||
_1.30277e_44</ |
_1.30277e_44</syntaxhighlight> |
||
If we use exact (rational) arithmetic, we get a precise result: |
If we use exact (rational) arithmetic, we get a precise result: |
||
< |
<syntaxhighlight lang="j"> -/ .* i. 5 5x |
||
0</ |
0</syntaxhighlight> |
||
Meanwhile, the permanent does not have this problem in this example (the matrix contains no negative values and permanent does not use subtraction): |
Meanwhile, the permanent does not have this problem in this example (the matrix contains no negative values and permanent does not use subtraction): |
||
< |
<syntaxhighlight lang="j"> +/ .* i. 5 5 |
||
6778800</ |
6778800</syntaxhighlight> |
||
As an aside, note also that for specific verbs (like <code>-/ .*</code>) J uses an algorithm which is more efficient than the brute force approach implied by the [http://www.jsoftware.com/help/dictionary/d300.htm definition of <code> .</code>]. (In general, where there are common, useful, concise definitions where special code can improve resource use by more than a factor of 2, the implementors of J try to make sure that that special code gets used for those definitions.) |
As an aside, note also that for specific verbs (like <code>-/ .*</code>) J uses an algorithm which is more efficient than the brute force approach implied by the [http://www.jsoftware.com/help/dictionary/d300.htm definition of <code> .</code>]. (In general, where there are common, useful, concise definitions where special code can improve resource use by more than a factor of 2, the implementors of J try to make sure that that special code gets used for those definitions.) |
||
Line 1,481: | Line 1,481: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">import java.util.Scanner; |
||
public class MatrixArithmetic { |
public class MatrixArithmetic { |
||
Line 1,535: | Line 1,535: | ||
System.out.println("Permanent: "+perm(a)); |
System.out.println("Permanent: "+perm(a)); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Note that the first input is the size of the matrix. |
Note that the first input is the size of the matrix. |
||
Line 1,541: | Line 1,541: | ||
For example: |
For example: |
||
<lang>2 |
<syntaxhighlight lang="text">2 |
||
1 2 |
1 2 |
||
3 4 |
3 4 |
||
Line 1,556: | Line 1,556: | ||
Determinant: 0.0 |
Determinant: 0.0 |
||
Permanent: 6778800.0 |
Permanent: 6778800.0 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
< |
<syntaxhighlight lang="javascript">const determinant = arr => |
||
arr.length === 1 ? ( |
arr.length === 1 ? ( |
||
arr[0][0] |
arr[0][0] |
||
Line 1,587: | Line 1,587: | ||
]; |
]; |
||
console.log(determinant(M)); |
console.log(determinant(M)); |
||
console.log(permanent(M));</ |
console.log(permanent(M));</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>0 |
<pre>0 |
||
Line 1,595: | Line 1,595: | ||
{{Works with|jq|1.4}} |
{{Works with|jq|1.4}} |
||
====Recursive definitions==== |
====Recursive definitions==== |
||
< |
<syntaxhighlight lang="jq"># Eliminate row i and row j |
||
def except(i;j): |
def except(i;j): |
||
reduce del(.[i])[] as $row ([]; . + [$row | del(.[j]) ] ); |
reduce del(.[i])[] as $row ([]; . + [$row | del(.[j]) ] ); |
||
Line 1,612: | Line 1,612: | ||
| reduce range(0; length) as $i |
| reduce range(0; length) as $i |
||
(0; . + $m[0][$i] * ( $m | except(0;$i) | perm) ) |
(0; . + $m[0][$i] * ( $m | except(0;$i) | perm) ) |
||
end ;</ |
end ;</syntaxhighlight> |
||
'''Examples''' |
'''Examples''' |
||
< |
<syntaxhighlight lang="jq">def matrices: |
||
[ [1, 2], |
[ [1, 2], |
||
[3, 4]], |
[3, 4]], |
||
Line 1,635: | Line 1,635: | ||
"Determinants: ", (matrices | det), |
"Determinants: ", (matrices | det), |
||
"Permanents: ", (matrices | perm)</ |
"Permanents: ", (matrices | perm)</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="sh">$ jq -n -r -f Matrix_arithmetic.jq |
||
Determinants: |
Determinants: |
||
-2 |
-2 |
||
Line 1,647: | Line 1,647: | ||
10 |
10 |
||
29556 |
29556 |
||
6778800</ |
6778800</syntaxhighlight> |
||
====Determinant via LU Decomposition==== |
====Determinant via LU Decomposition==== |
||
The following uses the jq infrastructure at [[LU decomposition]] to achieve an efficient implementation of det/0: |
The following uses the jq infrastructure at [[LU decomposition]] to achieve an efficient implementation of det/0: |
||
< |
<syntaxhighlight lang="jq"># Requires lup/0 |
||
def det: |
def det: |
||
def product_diagonal: |
def product_diagonal: |
||
Line 1,658: | Line 1,658: | ||
| (.[0]|product_diagonal) as $l |
| (.[0]|product_diagonal) as $l |
||
| if $l == 0 then 0 else $l * (.[1]|product_diagonal) | tidy end ; |
| if $l == 0 then 0 else $l * (.[1]|product_diagonal) | tidy end ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''Examples''' |
'''Examples''' |
||
Using matrices/0 as defined above: |
Using matrices/0 as defined above: |
||
<lang |
<syntaxhighlight lang="jq">matrices | det</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
$ /usr/local/bin/jq -M -n -f LU.rc |
$ /usr/local/bin/jq -M -n -f LU.rc |
||
Line 1,671: | Line 1,671: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<lang |
<syntaxhighlight lang="julia"> using LinearAlgebra</syntaxhighlight> |
||
The determinant of a matrix <code>A</code> can be computed by the built-in function |
The determinant of a matrix <code>A</code> can be computed by the built-in function |
||
<lang |
<syntaxhighlight lang="julia">det(A)</syntaxhighlight> |
||
{{trans|Python}} |
{{trans|Python}} |
||
The following function computes the permanent of a matrix A from the definition: |
The following function computes the permanent of a matrix A from the definition: |
||
< |
<syntaxhighlight lang="julia">function perm(A) |
||
m, n = size(A) |
m, n = size(A) |
||
if m != n; throw(ArgumentError("permanent is for square matrices only")); end |
if m != n; throw(ArgumentError("permanent is for square matrices only")); end |
||
sum(σ -> prod(i -> A[i,σ[i]], 1:n), permutations(1:n)) |
sum(σ -> prod(i -> A[i,σ[i]], 1:n), permutations(1:n)) |
||
end</ |
end</syntaxhighlight> |
||
Example output: |
Example output: |
||
< |
<syntaxhighlight lang="julia">julia> A = [2 9 4; 7 5 3; 6 1 8] |
||
julia> det(A), perm(A) |
julia> det(A), perm(A) |
||
(-360.0,900)</ |
(-360.0,900)</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
typealias Matrix = Array<DoubleArray> |
typealias Matrix = Array<DoubleArray> |
||
Line 1,775: | Line 1,775: | ||
println(" permanent = ${permanent(m)}\n") |
println(" permanent = ${permanent(m)}\n") |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,797: | Line 1,797: | ||
=={{header|Lambdatalk}}== |
=={{header|Lambdatalk}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
{require lib_matrix} |
{require lib_matrix} |
||
Line 1,821: | Line 1,821: | ||
[7,8,-9]]}} |
[7,8,-9]]}} |
||
-> 216 |
-> 216 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">-- Johnson–Trotter permutations generator |
||
_JT={} |
_JT={} |
||
function JT(dim) |
function JT(dim) |
||
Line 1,910: | Line 1,910: | ||
matrix2:dump(); |
matrix2:dump(); |
||
print("det:",matrix2:det(), "permanent:",matrix2:perm()) |
print("det:",matrix2:det(), "permanent:",matrix2:perm()) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre>7 2 -2 4 |
<pre>7 2 -2 4 |
||
Line 1,924: | Line 1,924: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">M:=<<2|9|4>,<7|5|3>,<6|1|8>>: |
||
with(LinearAlgebra): |
with(LinearAlgebra): |
||
Determinant(M); |
Determinant(M); |
||
Permanent(M);</ |
Permanent(M);</syntaxhighlight> |
||
Output: |
Output: |
||
<pre> -360 |
<pre> -360 |
||
Line 1,938: | Line 1,938: | ||
[https://reference.wolfram.com/language/ref/Permanent.html Permanent] is also a built in function, but here is a way it could be implemented: |
[https://reference.wolfram.com/language/ref/Permanent.html Permanent] is also a built in function, but here is a way it could be implemented: |
||
< |
<syntaxhighlight lang="mathematica">Permanent[m_List] := |
||
With[{v = Array[x, Length[m]]}, |
With[{v = Array[x, Length[m]]}, |
||
Coefficient[Times @@ (m.v), Times @@ v] |
Coefficient[Times @@ (m.v), Times @@ v] |
||
]</ |
]</syntaxhighlight> |
||
=={{header|Maxima}}== |
=={{header|Maxima}}== |
||
< |
<syntaxhighlight lang="maxima">a: matrix([2, 9, 4], [7, 5, 3], [6, 1, 8])$ |
||
determinant(a); |
determinant(a); |
||
Line 1,950: | Line 1,950: | ||
permanent(a); |
permanent(a); |
||
900</ |
900</syntaxhighlight> |
||
=={{header|МК-61/52}}== |
=={{header|МК-61/52}}== |
||
Line 1,971: | Line 1,971: | ||
{{trans|Python}} |
{{trans|Python}} |
||
Using the permutationsswap module from [[Permutations by swapping#Nim|Permutations by swapping]]: |
Using the permutationsswap module from [[Permutations by swapping#Nim|Permutations by swapping]]: |
||
< |
<syntaxhighlight lang="nim">import sequtils, permutationsswap |
||
type Matrix[M,N: static[int]] = array[M, array[N, float]] |
type Matrix[M,N: static[int]] = array[M, array[N, float]] |
||
Line 2,007: | Line 2,007: | ||
echo "perm: ", a.perm, " det: ", a.det |
echo "perm: ", a.perm, " det: ", a.det |
||
echo "perm: ", b.perm, " det: ", b.det |
echo "perm: ", b.perm, " det: ", b.det |
||
echo "perm: ", c.perm, " det: ", c.det</ |
echo "perm: ", c.perm, " det: ", c.det</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>perm: 10.0 det: -2.0 |
<pre>perm: 10.0 det: -2.0 |
||
Line 2,014: | Line 2,014: | ||
=={{header|Ol}}== |
=={{header|Ol}}== |
||
< |
<syntaxhighlight lang="scheme"> |
||
; helper function that returns rest of matrix by col/row |
; helper function that returns rest of matrix by col/row |
||
(define (rest matrix i j) |
(define (rest matrix i j) |
||
Line 2,081: | Line 2,081: | ||
(20 21 22 23 24)))) |
(20 21 22 23 24)))) |
||
; ==> 6778800 |
; ==> 6778800 |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
The determinant is built in: |
The determinant is built in: |
||
<lang |
<syntaxhighlight lang="parigp">matdet(M)</syntaxhighlight> |
||
and the permanent can be defined as |
and the permanent can be defined as |
||
< |
<syntaxhighlight lang="parigp">matperm(M)=my(n=#M,t);sum(i=1,n!,t=numtoperm(n,i);prod(j=1,n,M[j,t[j]]))</syntaxhighlight> |
||
For better performance, here's a version using Ryser's formula: |
For better performance, here's a version using Ryser's formula: |
||
< |
<syntaxhighlight lang="parigp">matperm(M)= |
||
{ |
{ |
||
my(n=matsize(M)[1],innerSums=vectorv(n)); |
my(n=matsize(M)[1],innerSums=vectorv(n)); |
||
Line 2,102: | Line 2,102: | ||
(-1)^hammingweight(gray)*factorback(innerSums) |
(-1)^hammingweight(gray)*factorback(innerSums) |
||
)*(-1)^n; |
)*(-1)^n; |
||
}</ |
}</syntaxhighlight> |
||
{{works with|PARI/GP|2.10.0+}} |
{{works with|PARI/GP|2.10.0+}} |
||
As of version 2.10, the matrix permanent is built in: |
As of version 2.10, the matrix permanent is built in: |
||
<lang |
<syntaxhighlight lang="parigp">matpermanent(M)</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
use warnings; |
use warnings; |
||
Line 2,136: | Line 2,136: | ||
print "det(M) = " . $M->determinant . ".\n"; |
print "det(M) = " . $M->determinant . ".\n"; |
||
print "det(M) = " . $M->det . ".\n"; |
print "det(M) = " . $M->det . ".\n"; |
||
print "perm(M) = " . permanent($M) . ".\n";</ |
print "perm(M) = " . permanent($M) . ".\n";</syntaxhighlight> |
||
<code>determinant</code> and <code>det</code> are already defined in PDL, see[http://pdl.perl.org/?docs=MatrixOps&title=the%20PDL::MatrixOps%20manpage#det]. <code>permanent</code> has to be defined manually. |
<code>determinant</code> and <code>det</code> are already defined in PDL, see[http://pdl.perl.org/?docs=MatrixOps&title=the%20PDL::MatrixOps%20manpage#det]. <code>permanent</code> has to be defined manually. |
||
Line 2,156: | Line 2,156: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #008080;">function</span> <span style="color: #000000;">minor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span> |
<span style="color: #008080;">function</span> <span style="color: #000000;">minor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">)</span> |
||
Line 2,246: | Line 2,246: | ||
<span style="color: #0000FF;">?{</span><span style="color: #000000;">det</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)}</span> |
<span style="color: #0000FF;">?{</span><span style="color: #000000;">det</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">),</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ti</span><span style="color: #0000FF;">)}</span> |
||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,264: | Line 2,264: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function det-perm ($array) { |
function det-perm ($array) { |
||
if($array) { |
if($array) { |
||
Line 2,309: | Line 2,309: | ||
det-perm @(@(2,5),@(4,3)) |
det-perm @(@(2,5),@(4,3)) |
||
det-perm @(@(4,4),@(2,2)) |
det-perm @(@(4,4),@(2,2)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
<b>Output:</b> |
<b>Output:</b> |
||
<pre> |
<pre> |
||
Line 2,325: | Line 2,325: | ||
Using the module file spermutations.py from [[Permutations by swapping#Python|Permutations by swapping]]. The algorithm for the determinant is a more literal translation of the expression in the task description and the Wikipedia reference. |
Using the module file spermutations.py from [[Permutations by swapping#Python|Permutations by swapping]]. The algorithm for the determinant is a more literal translation of the expression in the task description and the Wikipedia reference. |
||
< |
<syntaxhighlight lang="python">from itertools import permutations |
||
from operator import mul |
from operator import mul |
||
from math import fsum |
from math import fsum |
||
Line 2,369: | Line 2,369: | ||
print('') |
print('') |
||
pp(a) |
pp(a) |
||
print('Perm: %s Det: %s' % (perm(a), det(a)))</ |
print('Perm: %s Det: %s' % (perm(a), det(a)))</syntaxhighlight> |
||
;Sample output: |
;Sample output: |
||
Line 2,389: | Line 2,389: | ||
=={{header|R}}== |
=={{header|R}}== |
||
R has matrix algebra built in, so we do not need to import anything when calculating the determinant. However, we will use a library to generate the permutations of 1:n. |
R has matrix algebra built in, so we do not need to import anything when calculating the determinant. However, we will use a library to generate the permutations of 1:n. |
||
< |
<syntaxhighlight lang="rsplus">library(combinat) |
||
perm <- function(A) |
perm <- function(A) |
||
{ |
{ |
||
Line 2,404: | Line 2,404: | ||
"Test 3" = rbind(c(0, 1, 2, 3, 4), c(5, 6, 7, 8, 9), c(10, 11, 12, 13, 14), |
"Test 3" = rbind(c(0, 1, 2, 3, 4), c(5, 6, 7, 8, 9), c(10, 11, 12, 13, 14), |
||
c(15, 16, 17, 18, 19), c(20, 21, 22, 23, 24))) |
c(15, 16, 17, 18, 19), c(20, 21, 22, 23, 24))) |
||
print(sapply(testData, function(x) list(Determinant = det(x), Permanent = perm(x))))</ |
print(sapply(testData, function(x) list(Determinant = det(x), Permanent = perm(x))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,412: | Line 2,412: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
(require math) |
(require math) |
||
Line 2,422: | Line 2,422: | ||
(for/product ([i n] [σi σ]) |
(for/product ([i n] [σi σ]) |
||
(matrix-ref M i σi)))) |
(matrix-ref M i σi)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 2,429: | Line 2,429: | ||
Uses the permutations generator from the [[Permutations by swapping#Raku|Permutations by swapping]] task. This implementation is naive and brute-force (slow) but exact. |
Uses the permutations generator from the [[Permutations by swapping#Raku|Permutations by swapping]] task. This implementation is naive and brute-force (slow) but exact. |
||
<lang |
<syntaxhighlight lang="raku" line>sub insert ($x, @xs) { ([flat @xs[0 ..^ $_], $x, @xs[$_ .. *]] for 0 .. @xs) } |
||
sub order ($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse } |
sub order ($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse } |
||
Line 2,485: | Line 2,485: | ||
say "Permanent: \t", rat-or-int @matrix.&m_arith: <perm>; |
say "Permanent: \t", rat-or-int @matrix.&m_arith: <perm>; |
||
say '-' x 40; |
say '-' x 40; |
||
}</ |
}</syntaxhighlight> |
||
'''Output''' |
'''Output''' |
||
Line 2,517: | Line 2,517: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* Test the two functions determinant and permanent |
* Test the two functions determinant and permanent |
||
* using the matrix specifications shown for other languages |
* using the matrix specifications shown for other languages |
||
Line 2,555: | Line 2,555: | ||
Say ' permanent='right(permanent(as),7) |
Say ' permanent='right(permanent(as),7) |
||
Say copies('-',50) |
Say copies('-',50) |
||
Return</ |
Return</syntaxhighlight> |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* determinant.rex |
* determinant.rex |
||
* compute the determinant of the given square matrix |
* compute the determinant of the given square matrix |
||
Line 2,616: | Line 2,616: | ||
Say 'invalid number of elements:' nn 'is not a square.' |
Say 'invalid number of elements:' nn 'is not a square.' |
||
Exit |
Exit |
||
End</ |
End</syntaxhighlight> |
||
< |
<syntaxhighlight lang="rexx">/* REXX *************************************************************** |
||
* permanent.rex |
* permanent.rex |
||
* compute the permanent of a matrix |
* compute the permanent of a matrix |
||
Line 2,722: | Line 2,722: | ||
Say 'invalid number of elements:' nn 'is not a square.' |
Say 'invalid number of elements:' nn 'is not a square.' |
||
Exit |
Exit |
||
End</ |
End</syntaxhighlight> |
||
Output: |
Output: |
||
Line 2,748: | Line 2,748: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Matrix in the standard library provides a method for the determinant, but not for the permanent. |
Matrix in the standard library provides a method for the determinant, but not for the permanent. |
||
< |
<syntaxhighlight lang="ruby">require 'matrix' |
||
class Matrix |
class Matrix |
||
Line 2,773: | Line 2,773: | ||
puts "determinant:\t #{m.determinant}", "permanent:\t #{m.permanent}" |
puts "determinant:\t #{m.determinant}", "permanent:\t #{m.permanent}" |
||
puts |
puts |
||
end</ |
end</syntaxhighlight> |
||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,787: | Line 2,787: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="rust"> |
||
fn main() { |
fn main() { |
||
let mut m1: Vec<Vec<f64>> = vec![vec![1.0,2.0],vec![3.0,4.0]]; |
let mut m1: Vec<Vec<f64>> = vec![vec![1.0,2.0],vec![3.0,4.0]]; |
||
Line 2,869: | Line 2,869: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Output}} |
{{Output}} |
||
<pre> |
<pre> |
||
Line 2,881: | Line 2,881: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala"> |
||
def permutationsSgn[T]: List[T] => List[(Int,List[T])] = { |
def permutationsSgn[T]: List[T] => List[(Int,List[T])] = { |
||
case Nil => List((1,Nil)) |
case Nil => List((1,Nil)) |
||
Line 2,907: | Line 2,907: | ||
} |
} |
||
summands.toList.foldLeft(0)({case (x,y) => x + y}) |
summands.toList.foldLeft(0)({case (x,y) => x + y}) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
The `determinant` method is provided by the Array class. |
The `determinant` method is provided by the Array class. |
||
{{trans|Ruby}} |
{{trans|Ruby}} |
||
< |
<syntaxhighlight lang="ruby">class Array { |
||
method permanent { |
method permanent { |
||
var r = @^self.len |
var r = @^self.len |
||
Line 2,942: | Line 2,942: | ||
[m1, m2, m3].each { |m| |
[m1, m2, m3].each { |m| |
||
say "determinant:\t #{m.determinant}\npermanent:\t #{m.permanent}\n" |
say "determinant:\t #{m.determinant}\npermanent:\t #{m.permanent}\n" |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>determinant: -2 |
<pre>determinant: -2 |
||
Line 2,955: | Line 2,955: | ||
=={{header|Simula}}== |
=={{header|Simula}}== |
||
< |
<syntaxhighlight lang="simula">! MATRIX ARITHMETIC ; |
||
BEGIN |
BEGIN |
||
Line 3,046: | Line 3,046: | ||
! PERMANENT: 6778800.0 ; |
! PERMANENT: 6778800.0 ; |
||
END</ |
END</syntaxhighlight> |
||
Input: |
Input: |
||
<pre> |
<pre> |
||
Line 3,077: | Line 3,077: | ||
{{works with|OpenAxiom}} |
{{works with|OpenAxiom}} |
||
{{works with|Axiom}} |
{{works with|Axiom}} |
||
< |
<syntaxhighlight lang="spad">(1) -> M:=matrix [[2, 9, 4], [7, 5, 3], [6, 1, 8]] |
||
+2 9 4+ |
+2 9 4+ |
||
Line 3,092: | Line 3,092: | ||
(3) 900 |
(3) 900 |
||
Type: PositiveInteger</ |
Type: PositiveInteger</syntaxhighlight> |
||
[http://fricas.github.io/api/Matrix.html?highlight=matrix Domain:Matrix(R)] |
[http://fricas.github.io/api/Matrix.html?highlight=matrix Domain:Matrix(R)] |
||
Line 3,102: | Line 3,102: | ||
For x=-1, the main function '''sumrec(a,x)''' computes the determinant of a by developing the determinant along the first column. For x=1, one gets the permanent. |
For x=-1, the main function '''sumrec(a,x)''' computes the determinant of a by developing the determinant along the first column. For x=1, one gets the permanent. |
||
<lang>real vector range1(real scalar n, real scalar i) { |
<syntaxhighlight lang="text">real vector range1(real scalar n, real scalar i) { |
||
if (i < 1 | i > n) { |
if (i < 1 | i > n) { |
||
return(1::n) |
return(1::n) |
||
Line 3,129: | Line 3,129: | ||
} |
} |
||
return(s) |
return(s) |
||
}</ |
}</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="stata">: a=1,1,1,0\1,1,0,1\1,0,1,1\0,1,1,1 |
||
: a |
: a |
||
[symmetric] |
[symmetric] |
||
Line 3,151: | Line 3,151: | ||
: sumrec(a,1) |
: sumrec(a,1) |
||
9</ |
9</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 3,158: | Line 3,158: | ||
{{tcllib|math::linearalgebra}} |
{{tcllib|math::linearalgebra}} |
||
{{tcllib|struct::list}} |
{{tcllib|struct::list}} |
||
< |
<syntaxhighlight lang="tcl">package require math::linearalgebra |
||
package require struct::list |
package require struct::list |
||
Line 3,172: | Line 3,172: | ||
} |
} |
||
return [::tcl::mathop::+ {*}$sum] |
return [::tcl::mathop::+ {*}$sum] |
||
}</ |
}</syntaxhighlight> |
||
Demonstrating with a sample matrix: |
Demonstrating with a sample matrix: |
||
< |
<syntaxhighlight lang="tcl">set mat { |
||
{1 2 3 4} |
{1 2 3 4} |
||
{4 5 6 7} |
{4 5 6 7} |
||
Line 3,181: | Line 3,181: | ||
} |
} |
||
puts [::math::linearalgebra::det $mat] |
puts [::math::linearalgebra::det $mat] |
||
puts [permanent $mat]</ |
puts [permanent $mat]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,190: | Line 3,190: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Function Minor(a As Double(,), x As Integer, y As Integer) As Double(,) |
Function Minor(a As Double(,), x As Integer, y As Integer) As Double(,) |
||
Line 3,267: | Line 3,267: | ||
End Sub |
End Sub |
||
End Module</ |
End Module</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[1, 2] |
<pre>[1, 2] |
||
Line 3,294: | Line 3,294: | ||
{{trans|Phix}} |
{{trans|Phix}} |
||
As an extra, the results of the built in WorksheetFuction.MDeterm are shown. The latter does not work for scalars. |
As an extra, the results of the built in WorksheetFuction.MDeterm are shown. The latter does not work for scalars. |
||
< |
<syntaxhighlight lang="vb">Option Base 1 |
||
Private Function minor(a As Variant, x As Integer, y As Integer) As Variant |
Private Function minor(a As Variant, x As Integer, y As Integer) As Variant |
||
Dim l As Integer: l = UBound(a) - 1 |
Dim l As Integer: l = UBound(a) - 1 |
||
Line 3,385: | Line 3,385: | ||
Next i |
Next i |
||
Debug.Print det(tests(13)), "error", perm(tests(13)) |
Debug.Print det(tests(13)), "error", perm(tests(13)) |
||
End Sub</ |
End Sub</syntaxhighlight>{{out}} |
||
<pre>Determinant Builtin det Permanent |
<pre>Determinant Builtin det Permanent |
||
-2 -2 10 |
-2 -2 10 |
||
Line 3,404: | Line 3,404: | ||
{{libheader|Wren-matrix}} |
{{libheader|Wren-matrix}} |
||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/matrix" for Matrix |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
Line 3,432: | Line 3,432: | ||
System.print("\nDeterminant: %(m.det)") |
System.print("\nDeterminant: %(m.det)") |
||
System.print("Permanent : %(m.perm)\n") |
System.print("Permanent : %(m.perm)\n") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,468: | Line 3,468: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">var [const] GSL=Import("zklGSL"); // libGSL (GNU Scientific Library) |
||
fcn perm(A){ // should verify A is square |
fcn perm(A){ // should verify A is square |
||
numRows:=A.rows; |
numRows:=A.rows; |
||
Line 3,478: | Line 3,478: | ||
println(A.format()); |
println(A.format()); |
||
println("Permanent: %.2f, determinant: %.2f".fmt(perm(A),A.det())); |
println("Permanent: %.2f, determinant: %.2f".fmt(perm(A),A.det())); |
||
};</ |
};</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">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); |
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, |
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); |
15,16,17,18,19, 20,21,22,23,24); |
||
T(A,B,C).apply2(test);</ |
T(A,B,C).apply2(test);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |