Determinant and permanent: Difference between revisions
Add C# implementation
Alextretyak (talk | contribs) (Added 11l) |
(Add C# implementation) |
||
(7 intermediate revisions by 7 users not shown) | |||
Line 18:
{{trans|Nim}}
<
V items = [[Int]()]
L(j) seq
Line 66:
print(‘perm: ’perm(a)‘ det: ’det(a))
print(‘perm: ’perm(b)‘ det: ’det(b))
print(‘perm: ’perm(c)‘ det: ’det(c))</
{{out}}
Line 79:
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.
<
MATARI START
STM R14,R12,12(R13) save caller's registers
Line 230:
Q DS F sub matrix q((n-1)*(n-1)+1)
YREGS
END MATARI</
{{out}}
<pre>
Line 239:
=={{header|Arturo}}==
<
loop m 'row -> print map row 'val [pad to :string .format:".2f" val 6]
print "--------------------------------"
Line 314:
print ["A: perm ->" perm A "det ->" det A]
print ["B: perm ->" perm B "det ->" det B]
print ["C: perm ->" perm C "det ->" det C]</
{{out}}
Line 324:
=={{header|C}}==
C99 code. By no means efficient or reliable. If you need it for serious work, go find a serious library.
<
#include <stdlib.h>
#include <string.h>
Line 368:
return 0;
}</
A method to calculate determinant that might actually be usable:
<
#include <stdlib.h>
#include <tgmath.h>
Line 445:
printf("det: %19f\n", det(x, N));
return 0;
}</
=={{header|C#}}==
{{trans|Go}}
<syntaxhighlight lang="C#">
using System;
using System.Collections.Generic;
using System.Linq; // This is required for LINQ extension methods
class Program
{
static IEnumerable<IEnumerable<int>> GetPermutations(IEnumerable<int> list, int length)
{
if (length == 1) return list.Select(t => new int[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(e => !t.Contains(e)),
(t1, t2) => t1.Concat(new int[] { t2 }));
}
static double Determinant(double[][] m)
{
double d = 0;
var p = new List<int>();
for (int i = 0; i < m.Length; i++)
{
p.Add(i);
}
var permutations = GetPermutations(p, p.Count);
foreach (var perm in permutations)
{
double pr = 1;
int sign = Math.Sign(GetPermutationSign(perm.ToList()));
for (int i = 0; i < perm.Count(); i++)
{
pr *= m[i][perm.ElementAt(i)];
}
d += sign * pr;
}
return d;
}
static int GetPermutationSign(IList<int> perm)
{
int inversions = 0;
for (int i = 0; i < perm.Count; i++)
for (int j = i + 1; j < perm.Count; j++)
if (perm[i] > perm[j])
inversions++;
return inversions % 2 == 0 ? 1 : -1;
}
static double Permanent(double[][] m)
{
double d = 0;
var p = new List<int>();
for (int i = 0; i < m.Length; i++)
{
p.Add(i);
}
var permutations = GetPermutations(p, p.Count);
foreach (var perm in permutations)
{
double pr = 1;
for (int i = 0; i < perm.Count(); i++)
{
pr *= m[i][perm.ElementAt(i)];
}
d += pr;
}
return d;
}
static void Main(string[] args)
{
double[][] m2 = new double[][] {
new double[] { 1, 2 },
new double[] { 3, 4 }
};
double[][] m3 = new double[][] {
new double[] { 2, 9, 4 },
new double[] { 7, 5, 3 },
new double[] { 6, 1, 8 }
};
Console.WriteLine($"{Determinant(m2)}, {Permanent(m2)}");
Console.WriteLine($"{Determinant(m3)}, {Permanent(m3)}");
}
}
</syntaxhighlight>
{{out}}
<pre>
-2, 10
-360, 900
</pre>
=={{header|C++}}==
{{trans|Java}}
<
#include <vector>
Line 542 ⟶ 642:
return 0;
}</
{{out}}
<pre>[[1, 2], [3, 4]]
Line 556 ⟶ 656:
A recursive version, no libraries required, it doesn't use much consing, only for the list of columns to skip
<
(defun determinant (rows &optional (skip-cols nil))
(let* ((result 0) (sgn -1))
Line 600 ⟶ 700:
(dolist (m (list m2 m3 m4 m5))
(format t "~a determinant: ~a, permanent: ~a~%" m (determinant m) (permanent m)) )
</syntaxhighlight>
{{out}}
Line 612 ⟶ 712:
This requires the modules from the [[Permutations#D|Permutations]] and [[Permutations_by_swapping#D|Permutations by swapping]] tasks.
{{trans|Python}}
<
permutations_by_swapping1;
Line 666 ⟶ 766:
a.permanent, a.determinant);
}
}</
{{out}}
<pre>[[ 1, 2],
Line 687 ⟶ 787:
{{libheader| System.SysUtils}}
{{Trans|Java}}
<syntaxhighlight lang="delphi">
program Determinant_and_permanent;
Line 816 ⟶ 916:
writeln(#10'Permanent: ', perm(a): 3: 2);
readln;
end.</
{{out}}
<pre>Enter with matrix size:
Line 837 ⟶ 937:
=={{header|EchoLisp}}==
This requires the 'list' library for '''(in-permutations n)''' and the 'matrix' library for the built-in '''(determinant M)'''.
<
(lib 'list)
(lib 'matrix)
Line 866 ⟶ 966:
(permanent M) → 6778800
</syntaxhighlight>
=={{header|Factor}}==
<
: permanent ( matrix -- x )
dup square-matrix? [ "Matrix must be square." throw ] unless
[ dim first <iota> ] keep
'[ [ _ nth nth ] map-index product ] map-permutations sum ;</
Example output:
<pre>
Line 891 ⟶ 991:
{{works with|gforth|0.7.9_20170427}}
Requiring a permute.fs file from the [[Permutations_by_swapping#Forth|Permutations by swapping]] task.
<
S" fsl/dynmem.seq" REQUIRED
[UNDEFINED] defines [IF] SYNONYM defines IS [THEN]
Line 928 ⟶ 1,028:
m3{{ lmat lufact
lmat det F. 3 m3{{ permanent F. CR
lmat lu-free</
=={{header|Fortran}}==
Line 934 ⟶ 1,034:
Please find the compilation and example run at the start of the comments in the f90 source. Thank you.
<syntaxhighlight lang="fortran">
!-*- mode: compilation; default-directory: "/tmp/" -*-
!Compilation started at Sat May 18 23:25:42
Line 1,006 ⟶ 1,106:
end program f
</syntaxhighlight>
=={{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()
dim as uinteger ii, jj, size=ubound(M), ix, jx
for ii = 1 to size-1
if ii<i then ix = ii else ix = ii + 1
for jj = 1 to size-1
if jj<j then jx = jj else jx = jj + 1
S(ii, jj) = M(ix, jx)
next jj
next ii
end sub
function deperminant( M() as double, det as boolean ) as double
'calculates the determinant or the permanent of a square matrix M
'det = true for determinant, false for permanent
'assumes a square matrix
dim as uinteger size = ubound(M,1), i
dim as integer sign
dim as double S(1 to size-1, 1 to size-1)
dim as double ret = 0.0, inc
if size = 1 then return M(1,1) 'matrices of size < 3 are easy to calculate
if size = 2 and det then return M(1,1)*M(2,2) - M(1,2)*M(2,1)
if size = 2 then return M(1,1)*M(2,2) + M(1,2)*M(2,1)
for i = 1 to size
if det then sign = (-1)^(i+1) else sign = 1 'this bit is what distinguishes a determinant from a permanent
make_S( M(), S(), i, 1 )
inc = sign*M(i,1)*deperminant( S(), det ) 'recursively call on submatrices
ret += inc
next i
return ret
end function
dim as double A(1 to 2, 1 to 2) = {{1,2},{3,4}}
dim as double B(1 to 4, 1 to 4) = {_
{1,2,3,4}, {4,5,6,7}, {7,8,9,10}, {10,11,12,13} }
dim as double C(1 to 5, 1 to 5) = {_
{ 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 } }
print deperminant( A(), true ), deperminant( A(), false )
print deperminant( B(), true ), deperminant( B(), false )
print deperminant( C(), true ), deperminant( C(), false )</syntaxhighlight>
{{out}}<pre>
-2 10
0 29556
0 6778800
</pre>
=={{header|FunL}}==
From the task description:
<
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() )</
Laplace expansion (recursive):
<
| 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() )
Line 1,023 ⟶ 1,177:
def det( m )
| 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() )</
Test using the first set of definitions (from task description):
<
( (1, 2),
(3, 4)),
Line 1,043 ⟶ 1,197:
for m <- matrices
println( m, 'perm: ' + perm(m), 'det: ' + det(m) )</
{{out}}
Line 1,055 ⟶ 1,209:
=={{header|GLSL}}==
<
mat4 m1 = mat3(1, 2, 3, 4,
5, 6, 7, 8
Line 1,062 ⟶ 1,216:
float d = det(m1);
</syntaxhighlight>
=={{header|Go}}==
===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.
<
import (
Line 1,118 ⟶ 1,272:
fmt.Println(determinant(m2), permanent(m2))
fmt.Println(determinant(m3), permanent(m3))
}</
{{out}}
<pre>
Line 1,125 ⟶ 1,279:
</pre>
===Ryser permanent===
<
import "fmt"
Line 1,168 ⟶ 1,322:
}
return
}</
{{out}}
<pre>
Line 1,176 ⟶ 1,330:
===Library determinant===
'''go.matrix:'''
<
import (
Line 1,192 ⟶ 1,346:
{7, 5, 3},
{6, 1, 8}}).Det())
}</
{{out}}
<pre>
Line 1,199 ⟶ 1,353:
</pre>
'''gonum/mat:'''
<
import (
Line 1,215 ⟶ 1,369:
7, 5, 3,
6, 1, 8})))
}</
{{out}}
<pre>
Line 1,223 ⟶ 1,377:
=={{header|Haskell}}==
<
sPermutations = flip zip (cycle [1, -1]) . foldl aux [[]]
where
Line 1,281 ⟶ 1,435:
, [[2, 5], [4, 3]]
, [[4, 4], [2, 2]]
]</
{{Out}}
<pre>Matrix:
Line 1,341 ⟶ 1,495:
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">
outer :: (a->b->c) -> [a] -> [b] -> [[c]]
outer f [] _ = []
Line 1,393 ⟶ 1,547:
perm m = (mul m (padj m)) !! 0 !! 0
</syntaxhighlight>
=={{header|J}}==
Line 1,401 ⟶ 1,555:
For example, given the matrix:
<
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</
Its determinant is 0. When we use IEEE floating point, we only get an approximation of this result:
<
_1.30277e_44</
If we use exact (rational) arithmetic, we get a precise result:
<
0</
Meanwhile, the permanent does not have this problem in this example (the matrix contains no negative values and permanent does not use subtraction):
<
6778800</
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,427 ⟶ 1,581:
=={{header|Java}}==
<
public class MatrixArithmetic {
Line 1,481 ⟶ 1,635:
System.out.println("Permanent: "+perm(a));
}
}</
Note that the first input is the size of the matrix.
Line 1,487 ⟶ 1,641:
For example:
<syntaxhighlight lang="text">2
1 2
3 4
Line 1,502 ⟶ 1,656:
Determinant: 0.0
Permanent: 6778800.0
</syntaxhighlight>
=={{header|JavaScript}}==
<
arr.length === 1 ? (
arr[0][0]
Line 1,533 ⟶ 1,687:
];
console.log(determinant(M));
console.log(permanent(M));</
{{Out}}
<pre>0
Line 1,541 ⟶ 1,695:
{{Works with|jq|1.4}}
====Recursive definitions====
<
def except(i;j):
reduce del(.[i])[] as $row ([]; . + [$row | del(.[j]) ] );
Line 1,558 ⟶ 1,712:
| reduce range(0; length) as $i
(0; . + $m[0][$i] * ( $m | except(0;$i) | perm) )
end ;</
'''Examples'''
<
[ [1, 2],
[3, 4]],
Line 1,581 ⟶ 1,735:
"Determinants: ", (matrices | det),
"Permanents: ", (matrices | perm)</
{{Out}}
<
Determinants:
-2
Line 1,593 ⟶ 1,747:
10
29556
6778800</
====Determinant via LU Decomposition====
The following uses the jq infrastructure at [[LU decomposition]] to achieve an efficient implementation of det/0:
<
def det:
def product_diagonal:
Line 1,604 ⟶ 1,758:
| (.[0]|product_diagonal) as $l
| if $l == 0 then 0 else $l * (.[1]|product_diagonal) | tidy end ;
</syntaxhighlight>
'''Examples'''
Using matrices/0 as defined above:
<syntaxhighlight lang
{{Output}}
$ /usr/local/bin/jq -M -n -f LU.rc
Line 1,617 ⟶ 1,771:
=={{header|Julia}}==
<syntaxhighlight lang
The determinant of a matrix <code>A</code> can be computed by the built-in function
<syntaxhighlight lang
{{trans|Python}}
The following function computes the permanent of a matrix A from the definition:
<
m, n = size(A)
if m != n; throw(ArgumentError("permanent is for square matrices only")); end
sum(σ -> prod(i -> A[i,σ[i]], 1:n), permutations(1:n))
end</
Example output:
<
julia> det(A), perm(A)
(-360.0,900)</
=={{header|Kotlin}}==
<
typealias Matrix = Array<DoubleArray>
Line 1,721 ⟶ 1,875:
println(" permanent = ${permanent(m)}\n")
}
}</
{{out}}
Line 1,743 ⟶ 1,897:
=={{header|Lambdatalk}}==
<
{require lib_matrix}
Line 1,767 ⟶ 1,921:
[7,8,-9]]}}
-> 216
</syntaxhighlight>
=={{header|Lua}}==
<
_JT={}
function JT(dim)
Line 1,856 ⟶ 2,010:
matrix2:dump();
print("det:",matrix2:det(), "permanent:",matrix2:perm())
</syntaxhighlight>
{{out}}
<pre>7 2 -2 4
Line 1,870 ⟶ 2,024:
=={{header|Maple}}==
<
with(LinearAlgebra):
Determinant(M);
Permanent(M);</
Output:
<pre> -360
Line 1,882 ⟶ 2,036:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Determinant is a built in function Det
[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]]},
Coefficient[Times @@ (m.v), Times @@ v]
]</
=={{header|Maxima}}==
<
determinant(a);
Line 1,894 ⟶ 2,050:
permanent(a);
900</
=={{header|МК-61/52}}==
Line 1,915 ⟶ 2,071:
{{trans|Python}}
Using the permutationsswap module from [[Permutations by swapping#Nim|Permutations by swapping]]:
<
type Matrix[M,N: static[int]] = array[M, array[N, float]]
Line 1,951 ⟶ 2,107:
echo "perm: ", a.perm, " det: ", a.det
echo "perm: ", b.perm, " det: ", b.det
echo "perm: ", c.perm, " det: ", c.det</
Output:
<pre>perm: 10.0 det: -2.0
Line 1,958 ⟶ 2,114:
=={{header|Ol}}==
<
; helper function that returns rest of matrix by col/row
(define (rest matrix i j)
Line 2,025 ⟶ 2,181:
(20 21 22 23 24))))
; ==> 6778800
</syntaxhighlight>
=={{header|PARI/GP}}==
The determinant is built in:
<syntaxhighlight lang
and the permanent can be defined as
<
For better performance, here's a version using Ryser's formula:
<
{
my(n=matsize(M)[1],innerSums=vectorv(n));
Line 2,046 ⟶ 2,202:
(-1)^hammingweight(gray)*factorback(innerSums)
)*(-1)^n;
}</
{{works with|PARI/GP|2.10.0+}}
As of version 2.10, the matrix permanent is built in:
<syntaxhighlight lang
=={{header|Perl}}==
{{trans|C}}
<
use strict;
use warnings;
Line 2,080 ⟶ 2,236:
print "det(M) = " . $M->determinant . ".\n";
print "det(M) = " . $M->det . ".\n";
print "perm(M) = " . permanent($M) . ".\n";</
<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,100 ⟶ 2,256:
=={{header|Phix}}==
{{trans|Java}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<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: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">),</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">result</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)][</span><span style="color: #000000;">j</span><span style="color: #0000FF;">+(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">y</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>
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">det</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: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
<span style="color: #000000;">sgn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">sgn</span><span style="color: #0000FF;">*</span><span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">det</span><span style="color: #0000FF;">(</span><span style="color: #000000;">minor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
<span style="color: #000000;">sgn</span> <span style="color: #0000FF;">*=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">perm</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: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">a</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">perm</span><span style="color: #0000FF;">(</span><span style="color: #000000;">minor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">a</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: -2, permanent: 10</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: -360, permanent: 900</span>
<span style="color: #0000FF;">{{</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">13</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: 0, permanent: 29556</span>
<span style="color: #0000FF;">{{</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">6</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">13</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">15</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">16</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">17</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">18</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">19</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">21</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">22</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">23</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">24</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant:
<span style="color: #0000FF;">{{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: 5, permanent: 5 </span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: 1, permanent: 1</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: -1, Permanent: 1</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: 14, Permanent: 26</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: -14, Permanent: 26</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--Determinant: 0, Permanent: 16</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">7</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">9</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">12</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">13</span><span style="color: #0000FF;">}},</span>
<span style="color: #000080;font-style:italic;">--det: -4319 permanent: 10723</span>
<span style="color: #0000FF;">{{-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">3</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">2</span> <span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}</span>
<span style="color: #000080;font-style:italic;">--det: 18 permanent: 10</span>
<span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ti</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,205 ⟶ 2,364:
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
function det-perm ($array) {
if($array) {
Line 2,250 ⟶ 2,409:
det-perm @(@(2,5),@(4,3))
det-perm @(@(4,4),@(2,2))
</syntaxhighlight>
<b>Output:</b>
<pre>
Line 2,266 ⟶ 2,425:
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.
<
from operator import mul
from math import fsum
Line 2,310 ⟶ 2,469:
print('')
pp(a)
print('Perm: %s Det: %s' % (perm(a), det(a)))</
;Sample output:
Line 2,330 ⟶ 2,489:
=={{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.
<
perm <- function(A)
{
Line 2,345 ⟶ 2,504:
"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)))
print(sapply(testData, function(x) list(Determinant = det(x), Permanent = perm(x))))</
{{out}}
Line 2,353 ⟶ 2,512:
=={{header|Racket}}==
<
#lang racket
(require math)
Line 2,363 ⟶ 2,522:
(for/product ([i n] [σi σ])
(matrix-ref M i σi))))
</syntaxhighlight>
=={{header|Raku}}==
Line 2,370 ⟶ 2,529:
Uses the permutations generator from the [[Permutations by swapping#Raku|Permutations by swapping]] task. This implementation is naive and brute-force (slow) but exact.
<syntaxhighlight lang="raku"
sub order ($sg, @xs) { $sg > 0 ?? @xs !! @xs.reverse }
Line 2,426 ⟶ 2,585:
say "Permanent: \t", rat-or-int @matrix.&m_arith: <perm>;
say '-' x 40;
}</
'''Output'''
Line 2,458 ⟶ 2,617:
=={{header|REXX}}==
<
* Test the two functions determinant and permanent
* using the matrix specifications shown for other languages
Line 2,496 ⟶ 2,655:
Say ' permanent='right(permanent(as),7)
Say copies('-',50)
Return</
<
* determinant.rex
* compute the determinant of the given square matrix
Line 2,557 ⟶ 2,716:
Say 'invalid number of elements:' nn 'is not a square.'
Exit
End</
<
* permanent.rex
* compute the permanent of a matrix
Line 2,663 ⟶ 2,822:
Say 'invalid number of elements:' nn 'is not a square.'
Exit
End</
Output:
Line 2,686 ⟶ 2,845:
permanent=6778800
--------------------------------------------------</pre>
=={{header|RPL}}==
{{trans|Phix}}
{{works with|HP|48G}}
« → a x y
« a SIZE {-1 -1} ADD 0 CON
1 OVER SIZE 1 GET '''FOR''' k
1 OVER SIZE 1 GET '''FOR''' j
k j 2 →LIST
a k DUP x ≥ + j DUP y ≥ + 2 →LIST GET
PUT
'''NEXT NEXT'''
» » '<span style="color:blue">MINOR</span>' STO <span style="color:grey">@ ''( matrix x y → matrix )''</span>
« DUP SIZE 1 GET
'''IF''' DUP 1 == '''THEN''' GET
'''ELSE'''
0
1 ROT '''FOR''' k
OVER { 1 } k + GET
3 PICK 1 k <span style="color:blue">MINOR PRM</span> * +
'''NEXT'''
SWAP DROP
END
» '<span style="color:blue">PRM</span>' STO <span style="color:grey">@ ''( matrix → permanent )''</span>
[[ 1 2 ]
[ 3 4 ]] DET LASTARG <span style="color:blue">PRM</span>
[[2 9 4]
[7 5 3]
[6 1 8]] DET LASTARG <span style="color:blue">PRM</span>
{{out}}
<pre>
4: -2
3: 10
2: -360
1: 900
</pre>
=={{header|Ruby}}==
Matrix in the standard library provides a method for the determinant, but not for the permanent.
<
class Matrix
Line 2,714 ⟶ 2,911:
puts "determinant:\t #{m.determinant}", "permanent:\t #{m.permanent}"
puts
end</
{{Output}}
<pre>
Line 2,726 ⟶ 2,923:
permanent: 6778800
</pre>
=={{header|Rust}}==
{{trans|Java}}
<
fn main() {
let mut m1: Vec<Vec<f64>> = vec![vec![1.0,2.0],vec![3.0,4.0]];
Line 2,810 ⟶ 3,008:
}
</syntaxhighlight>
{{Output}}
<pre>
Line 2,822 ⟶ 3,020:
=={{header|Scala}}==
<
def permutationsSgn[T]: List[T] => List[(Int,List[T])] = {
case Nil => List((1,Nil))
Line 2,848 ⟶ 3,046:
}
summands.toList.foldLeft(0)({case (x,y) => x + y})
</syntaxhighlight>
=={{header|Sidef}}==
The `determinant` method is provided by the Array class.
{{trans|Ruby}}
<
method permanent {
var r = @^self.len
Line 2,883 ⟶ 3,081:
[m1, m2, m3].each { |m|
say "determinant:\t #{m.determinant}\npermanent:\t #{m.permanent}\n"
}</
{{out}}
<pre>determinant: -2
Line 2,896 ⟶ 3,094:
=={{header|Simula}}==
<
BEGIN
Line 2,987 ⟶ 3,185:
! PERMANENT: 6778800.0 ;
END</
Input:
<pre>
Line 3,018 ⟶ 3,216:
{{works with|OpenAxiom}}
{{works with|Axiom}}
<
+2 9 4+
Line 3,033 ⟶ 3,231:
(3) 900
Type: PositiveInteger</
[http://fricas.github.io/api/Matrix.html?highlight=matrix Domain:Matrix(R)]
Line 3,043 ⟶ 3,241:
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.
<syntaxhighlight lang="text">real vector range1(real scalar n, real scalar i) {
if (i < 1 | i > n) {
return(1::n)
Line 3,070 ⟶ 3,268:
}
return(s)
}</
Example:
<
: a
[symmetric]
Line 3,092 ⟶ 3,290:
: sumrec(a,1)
9</
=={{header|Tcl}}==
Line 3,099 ⟶ 3,297:
{{tcllib|math::linearalgebra}}
{{tcllib|struct::list}}
<
package require struct::list
Line 3,113 ⟶ 3,311:
}
return [::tcl::mathop::+ {*}$sum]
}</
Demonstrating with a sample matrix:
<
{1 2 3 4}
{4 5 6 7}
Line 3,122 ⟶ 3,320:
}
puts [::math::linearalgebra::det $mat]
puts [permanent $mat]</
{{out}}
<pre>
Line 3,131 ⟶ 3,329:
=={{header|Visual Basic .NET}}==
{{trans|Java}}
<
Function Minor(a As Double(,), x As Integer, y As Integer) As Double(,)
Line 3,208 ⟶ 3,406:
End Sub
End Module</
{{out}}
<pre>[1, 2]
Line 3,235 ⟶ 3,433:
{{trans|Phix}}
As an extra, the results of the built in WorksheetFuction.MDeterm are shown. The latter does not work for scalars.
<
Private Function minor(a As Variant, x As Integer, y As Integer) As Variant
Dim l As Integer: l = UBound(a) - 1
Line 3,326 ⟶ 3,524:
Next i
Debug.Print det(tests(13)), "error", perm(tests(13))
End Sub</
<pre>Determinant Builtin det Permanent
-2 -2 10
Line 3,345 ⟶ 3,543:
{{libheader|Wren-matrix}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var arrays = [
Line 3,373 ⟶ 3,571:
System.print("\nDeterminant: %(m.det)")
System.print("Permanent : %(m.perm)\n")
}</
{{out}}
Line 3,409 ⟶ 3,607:
=={{header|zkl}}==
<
fcn perm(A){ // should verify A is square
numRows:=A.rows;
Line 3,419 ⟶ 3,617:
println(A.format());
println("Permanent: %.2f, determinant: %.2f".fmt(perm(A),A.det()));
};</
<
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);</
{{out}}
<pre>
|