forked from pdsteele/DES-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdet.py
More file actions
61 lines (51 loc) · 2.64 KB
/
det.py
File metadata and controls
61 lines (51 loc) · 2.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# ---------------------------------------------------------------------- *
# * A Monte Carlo simulation to estimate the probability that the *
# * determinant of a 3 by 3 matrix of random numbers is positive. *
# * *
# * Name : det.c *
# * Author : Larry Leemis *
# * Language : ANSI C
# * Latest Revision : 8-8-02 *
# * Compile with : gcc det.c rng.c *
# * if math functions needed, add #include <math.h> *
# * and compile with gcc -lm det.c rng.c
# # Translated by : Philip Steele
# # Language : Python 3.3
# # Latest Revision : 3/26/14
# Warning : Reduce number of replications for python version
# to run quickly. Full 200000000 takes an hour or two
# * ---------------------------------------------------------------------- */
from rng import putSeed, random
N = 200000000 # number of replications */
# i # replication index */
# j # row index */
# k # column index */
# temp1 # first 2 by 2 determinant */
# temp2 # second 2 by 2 determinant */
# temp3 # third 2 by 2 determinant */
# x # determinant */
a = [[0 for i in range(0,4)] for i in range(0,4)] # matrix (only 9 elements used) */
count = 0 # counts number of pos det */
putSeed(0)
for i in range(0,N):
for j in range(1,4):
for k in range(1,4):
a[j][k] = random()
if (j != k):
a[j][k] = -a[j][k]
#EndFor
#EndFor
temp1 = a[2][2] * a[3][3] - a[3][2] * a[2][3]
temp2 = a[2][1] * a[3][3] - a[3][1] * a[2][3]
temp3 = a[2][1] * a[3][2] - a[3][1] * a[2][2]
x = a[1][1] * temp1 - a[1][2] * temp2 + a[1][3] * temp3
if (x > 0):
count += 1
#EndFor
print("\nbased on {0:1d} replications ".format(N))
print("the estimated probability of a positive determinant is:")
print("{0:11.9f}".format(float(count / N)))
#C output:
# Enter a positive integer seed (9 digits or less) >> 123456789
# based on 200000000 replications the estimated probability of a positive determinant is:
# 0.050202725