This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,12 @@
For a given matrix, return the [[wp:Determinant|determinant]] and the [[wp:Permanent|permanent]] of the matrix.
The determinant is given by
:<math>\det(A) = \sum_\sigma\sgn(\sigma)\prod_{i=1}^n M_{i,\sigma_i}</math>
while the permanent is given by
: <math> \operatorname{perm}(A)=\sum_\sigma\prod_{i=1}^n M_{i,\sigma_i}</math>
In both cases the sum is over the permutations <math>\sigma</math> 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 [[wp:Parity of a permutation|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.
;Cf.:
* [[Permutations by swapping]]

View file

@ -0,0 +1,3 @@
---
category:
- Matrices

View file

@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
double det_in(double **in, int n, int perm)
{
if (n == 1) return in[0][0];
double sum = 0, *m[--n];
for (int i = 0; i < n; i++)
m[i] = in[i + 1] + 1;
for (int i = 0, sgn = 1; i <= n; i++) {
sum += sgn * (in[i][0] * det_in(m, n, perm));
if (i == n) break;
m[i] = in[i] + 1;
if (!perm) sgn = -sgn;
}
return sum;
}
/* wrapper function */
double det(double *in, int n, int perm)
{
double *m[n];
for (int i = 0; i < n; i++)
m[i] = in + (n * i);
return det_in(m, n, perm);
}
int main(void)
{
double x[] = { 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 };
printf("det: %14.12g\n", det(x, 5, 0));
printf("perm: %14.12g\n", det(x, 5, 1));
return 0;
}

View file

@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <tgmath.h>
void showmat(const char *s, double **m, int n)
{
printf("%s:\n", s);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
printf("%12.4f", m[i][j]);
putchar('\n');
}
}
int trianglize(double **m, int n)
{
int sign = 1;
for (int i = 0; i < n; i++) {
int max = 0;
for (int row = i; row < n; row++)
if (fabs(m[row][i]) > fabs(m[max][i]))
max = row;
if (max) {
sign = -sign;
double *tmp = m[i];
m[i] = m[max], m[max] = tmp;
}
if (!m[i][i]) return 0;
for (int row = i + 1; row < n; row++) {
double r = m[row][i] / m[i][i];
if (!r) continue;
for (int col = i; col < n; col ++)
m[row][col] -= m[i][col] * r;
}
}
return sign;
}
double det(double *in, int n)
{
double *m[n];
m[0] = in;
for (int i = 1; i < n; i++)
m[i] = m[i - 1] + n;
showmat("Matrix", m, n);
int sign = trianglize(m, n);
if (!sign)
return 0;
showmat("Upper triangle", m, n);
double p = 1;
for (int i = 0; i < n; i++)
p *= m[i][i];
return p * sign;
}
#define N 18
int main(void)
{
double x[N * N];
srand(0);
for (int i = 0; i < N * N; i++)
x[i] = rand() % N;
printf("det: %19f\n", det(x, N));
return 0;
}

View file

@ -0,0 +1,57 @@
import std.algorithm, std.range, std.traits, permutations2,
permutations_by_swapping1;
auto prod(Range)(/*in*/ Range r) /*pure nothrow*/ {
return reduce!q{a * b}(cast(ForeachType!Range)1, r);
}
T permanent(T)(in T[][] a) /*pure nothrow*/
in {
foreach (const row; a)
assert(row.length == a[0].length);
} body {
auto r = iota(cast()a.length);
T tot = 0;
foreach (sigma; permutations(r.array()))
tot += r.map!(i => a[i][sigma[i]])().prod();
return tot;
}
T determinant(T)(in T[][] a) /*pure nothrow*/
in {
foreach (const row; a)
assert(row.length == a[0].length);
} body {
immutable n = a.length;
auto r = iota(n);
T tot = 0;
//foreach (sigma, sign; spermutations(n)) {
foreach (sigma_sign; spermutations(n)) {
const sigma = sigma_sign[0];
immutable sign = sigma_sign[1];
tot += sign * r.map!(i => a[i][sigma[i]])().prod();
}
return tot;
}
void main() {
import std.stdio;
foreach (const a; [[[1, 2],
[3, 4]],
[[1, 2, 3, 4],
[4, 5, 6, 7],
[7, 8, 9, 10],
[10, 11, 12, 13]],
[[ 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]]]) {
writefln("[%([%(%2s, %)],\n %)]]", a);
writefln("Permanent: %s, determinant: %s\n",
permanent(a), determinant(a));
}
}

View file

@ -0,0 +1,6 @@
i. 5 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

View file

@ -0,0 +1,2 @@
-/ .* i. 5 5
_1.30277e_44

View file

@ -0,0 +1,2 @@
-/ .* i. 5 5x
0

View file

@ -0,0 +1,2 @@
+/ .* i. 5 5
6778800

View file

@ -0,0 +1,4 @@
Permanent[m_List] :=
With[{v = Array[x, Length[m]]},
Coefficient[Times @@ (m.v), Times @@ v]
]

View file

@ -0,0 +1,7 @@
a: matrix([2, 9, 4], [7, 5, 3], [6, 1, 8])$
determinant(a);
-360
permanent(a);
900

View file

@ -0,0 +1,45 @@
from itertools import permutations
from operator import mul
from math import fsum
from spermutations import spermutations
def prod(lst):
return reduce(mul, lst, 1)
def perm(a):
n = len(a)
r = range(n)
s = permutations(r)
return fsum(prod(a[i][sigma[i]] for i in r) for sigma in s)
def det(a):
n = len(a)
r = range(n)
s = spermutations(n)
return fsum(sign * prod(a[i][sigma[i]] for i in r)
for sigma, sign in s)
if __name__ == '__main__':
from pprint import pprint as pp
for a in (
[
[1, 2],
[3, 4]],
[
[1, 2, 3, 4],
[4, 5, 6, 7],
[7, 8, 9, 10],
[10, 11, 12, 13]],
[
[ 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('')
pp(a)
print('Perm: %s Det: %s' % (perm(a), det(a)))

View file

@ -0,0 +1,15 @@
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]
}

View file

@ -0,0 +1,8 @@
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]