chrislo-sourceclassifier 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +325 -0
- data/README.textile +51 -0
- data/Rakefile +64 -0
- data/examples/example.rb +22 -0
- data/lib/sourceclassifier.rb +22 -0
- data/lib/trainer.rb +51 -0
- data/sourceclassifier.gemspec +35 -0
- data/sources/gcc/ackermann.gcc-2.gcc +93 -0
- data/sources/gcc/ackermann.gcc-3.gcc +20 -0
- data/sources/gcc/ary.gcc +40 -0
- data/sources/gcc/binarytrees.gcc +136 -0
- data/sources/gcc/binarytrees.gcc-2.gcc +140 -0
- data/sources/gcc/binarytrees.gcc-3.gcc +134 -0
- data/sources/gcc/chameneos.gcc +117 -0
- data/sources/gcc/chameneos.gcc-2.gcc +134 -0
- data/sources/gcc/chameneos.gcc-3.gcc +120 -0
- data/sources/gcc/chameneosredux.gcc +306 -0
- data/sources/gcc/echo.gcc +144 -0
- data/sources/gcc/except.gcc +52 -0
- data/sources/gcc/fannkuch.gcc +105 -0
- data/sources/gcc/fannkuch.gcc-2.gcc +94 -0
- data/sources/gcc/fasta.gcc +158 -0
- data/sources/gcc/fasta.gcc-2.gcc +132 -0
- data/sources/gcc/fasta.gcc-3.gcc +145 -0
- data/sources/gcc/fasta.gcc-4.gcc +141 -0
- data/sources/gcc/fibo.gcc +20 -0
- data/sources/gcc/harmonic.gcc +22 -0
- data/sources/gcc/hash.gcc +31 -0
- data/sources/gcc/hash2.gcc +38 -0
- data/sources/gcc/heapsort.gcc +72 -0
- data/sources/gcc/hello.gcc-2.gcc +12 -0
- data/sources/gcc/hello.gcc-3.gcc +20 -0
- data/sources/gcc/knucleotide.gcc +173 -0
- data/sources/gcc/knucleotide.gcc-2.gcc +168 -0
- data/sources/gcc/lists.gcc +226 -0
- data/sources/gcc/magicsquares.gcc +378 -0
- data/sources/gcc/mandelbrot.gcc +84 -0
- data/sources/gcc/mandelbrot.gcc-2.gcc +63 -0
- data/sources/gcc/mandelbrot.gcc-3.gcc +91 -0
- data/sources/gcc/mandelbrot.gcc-4.gcc +78 -0
- data/sources/gcc/matrix.gcc +66 -0
- data/sources/gcc/message.gcc-2.gcc +72 -0
- data/sources/gcc/message.gcc-3.gcc +64 -0
- data/sources/gcc/methcall.gcc +89 -0
- data/sources/gcc/moments.gcc +120 -0
- data/sources/gcc/nbody.gcc +142 -0
- data/sources/gcc/nbody.gcc-2.gcc +143 -0
- data/sources/gcc/nbody.gcc-3.gcc +142 -0
- data/sources/gcc/nestedloop.gcc +25 -0
- data/sources/gcc/nsieve.gcc +35 -0
- data/sources/gcc/nsievebits.gcc-2.gcc +37 -0
- data/sources/gcc/nsievebits.gcc-3.gcc +53 -0
- data/sources/gcc/objinst.gcc +95 -0
- data/sources/gcc/partialsums.gcc +86 -0
- data/sources/gcc/partialsums.gcc-3.gcc +81 -0
- data/sources/gcc/partialsums.gcc-4.gcc +68 -0
- data/sources/gcc/pidigits.gcc +105 -0
- data/sources/gcc/primes.gcc +75 -0
- data/sources/gcc/prodcons.gcc +86 -0
- data/sources/gcc/random.gcc +29 -0
- data/sources/gcc/raytracer.gcc +125 -0
- data/sources/gcc/raytracer.gcc-2.gcc +181 -0
- data/sources/gcc/recursive.gcc +68 -0
- data/sources/gcc/recursive.gcc-2.gcc +55 -0
- data/sources/gcc/regexdna.gcc-2.gcc +126 -0
- data/sources/gcc/regexmatch.gcc +136 -0
- data/sources/gcc/revcomp.gcc +85 -0
- data/sources/gcc/revcomp.gcc-2.gcc +88 -0
- data/sources/gcc/revcomp.gcc-4.gcc +71 -0
- data/sources/gcc/reversefile.gcc +103 -0
- data/sources/gcc/reversefile.gcc-2.gcc +56 -0
- data/sources/gcc/sieve.gcc +34 -0
- data/sources/gcc/spectralnorm.gcc +54 -0
- data/sources/gcc/spellcheck.gcc +72 -0
- data/sources/gcc/spellcheck.gcc-2.gcc +61 -0
- data/sources/gcc/strcat.gcc +38 -0
- data/sources/gcc/sumcol.gcc-2.gcc +98 -0
- data/sources/gcc/sumcol.gcc-3.gcc +22 -0
- data/sources/gcc/sumcol.gcc-4.gcc +18 -0
- data/sources/gcc/sumcol.gcc-5.gcc +32 -0
- data/sources/gcc/takfp.gcc +23 -0
- data/sources/gcc/tcp-stream.gcc +122 -0
- data/sources/gcc/tcpecho.gcc +122 -0
- data/sources/gcc/tcpecho.gcc-2.gcc +136 -0
- data/sources/gcc/tcprequest.gcc +122 -0
- data/sources/gcc/threadring.gcc +73 -0
- data/sources/gcc/wc.gcc +52 -0
- data/sources/gcc/wc.gcc-2.gcc +40 -0
- data/sources/gcc/wc.gcc-3.gcc +46 -0
- data/sources/gcc/wordfreq.gcc-2.gcc +85 -0
- data/sources/perl/ackermann.perl +28 -0
- data/sources/perl/ackermann.perl-2.perl +25 -0
- data/sources/perl/ackermann.perl-3.perl +20 -0
- data/sources/perl/ackermann.perl-4.perl +18 -0
- data/sources/perl/ary.perl +25 -0
- data/sources/perl/ary.perl-2.perl +23 -0
- data/sources/perl/binarytrees.perl +66 -0
- data/sources/perl/binarytrees.perl-2.perl +71 -0
- data/sources/perl/chameneos.perl +67 -0
- data/sources/perl/echo.perl +81 -0
- data/sources/perl/except.perl +73 -0
- data/sources/perl/fannkuch.perl +44 -0
- data/sources/perl/fannkuch.perl-2.perl +38 -0
- data/sources/perl/fasta.perl +112 -0
- data/sources/perl/fasta.perl-2.perl +135 -0
- data/sources/perl/fasta.perl-4.perl +122 -0
- data/sources/perl/fibo.perl +15 -0
- data/sources/perl/fibo.perl-2.perl +0 -0
- data/sources/perl/fibo.perl-3.perl +0 -0
- data/sources/perl/harmonic.perl +7 -0
- data/sources/perl/hash.perl +23 -0
- data/sources/perl/hash.perl-2.perl +17 -0
- data/sources/perl/hash.perl-3.perl +24 -0
- data/sources/perl/hash2.perl +16 -0
- data/sources/perl/heapsort.perl +65 -0
- data/sources/perl/heapsort.perl-2.perl +0 -0
- data/sources/perl/hello.perl +5 -0
- data/sources/perl/knucleotide.perl-2.perl +30 -0
- data/sources/perl/lists.perl +48 -0
- data/sources/perl/mandelbrot.perl-2.perl +32 -0
- data/sources/perl/matrix.perl +59 -0
- data/sources/perl/matrix.perl-2.perl +0 -0
- data/sources/perl/matrix.perl-3.perl +0 -0
- data/sources/perl/message.perl +27 -0
- data/sources/perl/methcall.perl +66 -0
- data/sources/perl/moments.perl +44 -0
- data/sources/perl/nbody.perl +108 -0
- data/sources/perl/nestedloop.perl +28 -0
- data/sources/perl/nsieve.perl-2.perl +41 -0
- data/sources/perl/nsieve.perl-4.perl +43 -0
- data/sources/perl/nsievebits.perl +37 -0
- data/sources/perl/objinst.perl +73 -0
- data/sources/perl/partialsums.perl-3.perl +31 -0
- data/sources/perl/pidigits.perl +52 -0
- data/sources/perl/pidigits.perl-2.perl +47 -0
- data/sources/perl/process.perl +50 -0
- data/sources/perl/prodcons.perl +47 -0
- data/sources/perl/random.perl-4.perl +17 -0
- data/sources/perl/recursive.perl-2.perl +57 -0
- data/sources/perl/regexdna.perl +48 -0
- data/sources/perl/regexdna.perl-2.perl +43 -0
- data/sources/perl/regexdna.perl-3.perl +50 -0
- data/sources/perl/regexdna.perl-4.perl +49 -0
- data/sources/perl/regexdna.perl-5.perl +42 -0
- data/sources/perl/regexdna.perl-6.perl +43 -0
- data/sources/perl/regexmatch.perl +35 -0
- data/sources/perl/revcomp.perl-2.perl +34 -0
- data/sources/perl/reversefile.perl +8 -0
- data/sources/perl/reversefile.perl-2.perl +0 -0
- data/sources/perl/reversefile.perl-3.perl +0 -0
- data/sources/perl/sieve.perl +23 -0
- data/sources/perl/spectralnorm.perl-2.perl +54 -0
- data/sources/perl/spellcheck.perl +24 -0
- data/sources/perl/strcat.perl +13 -0
- data/sources/perl/strcat.perl-2.perl +0 -0
- data/sources/perl/sumcol.perl +8 -0
- data/sources/perl/takfp.perl +23 -0
- data/sources/perl/takfp.perl-3.perl +20 -0
- data/sources/perl/tcpecho.perl +61 -0
- data/sources/perl/tcprequest.perl +61 -0
- data/sources/perl/tcpstream.perl +61 -0
- data/sources/perl/threadring.perl +55 -0
- data/sources/perl/threadring.perl-2.perl +43 -0
- data/sources/perl/wc.perl +20 -0
- data/sources/perl/wc.perl-2.perl +14 -0
- data/sources/perl/wordfreq.perl +22 -0
- data/sources/perl/wordfreq.perl-3.perl +0 -0
- data/sources/perl/wordfreq.perl3.perl +0 -0
- data/sources/python/ackermann.python +21 -0
- data/sources/python/ary.python +19 -0
- data/sources/python/binarytrees.python +39 -0
- data/sources/python/binarytrees.python-3.python +44 -0
- data/sources/python/chameneos.python-6.python +73 -0
- data/sources/python/chameneosredux.python +126 -0
- data/sources/python/chameneosredux.python-2.python +122 -0
- data/sources/python/dispatch.python +176 -0
- data/sources/python/dispatch.python-2.python +136 -0
- data/sources/python/echo.python +64 -0
- data/sources/python/except.python +62 -0
- data/sources/python/fannkuch.python +50 -0
- data/sources/python/fannkuch.python-2.python +54 -0
- data/sources/python/fasta.python-2.python +79 -0
- data/sources/python/fibo.python +17 -0
- data/sources/python/fibo.python-2.python +0 -0
- data/sources/python/fibo.python-3.python +0 -0
- data/sources/python/harmonic.python-2.python +9 -0
- data/sources/python/hash.python +21 -0
- data/sources/python/hash.python-2.python +0 -0
- data/sources/python/hash2.python +30 -0
- data/sources/python/heapsort.python-3.python +66 -0
- data/sources/python/hello.python +5 -0
- data/sources/python/implicitode.python +231 -0
- data/sources/python/knucleotide.python +55 -0
- data/sources/python/lists.python +44 -0
- data/sources/python/magicsquares.python +145 -0
- data/sources/python/mandelbrot.python +44 -0
- data/sources/python/mandelbrot.python-2.python +35 -0
- data/sources/python/mandelbrot.python-3.python +46 -0
- data/sources/python/matrix.python +34 -0
- data/sources/python/matrix.python-2.python +23 -0
- data/sources/python/message.python +24 -0
- data/sources/python/message.python-2.python +20 -0
- data/sources/python/message.python-3.python +19 -0
- data/sources/python/meteor.python +210 -0
- data/sources/python/meteor.python-2.python +192 -0
- data/sources/python/methcall.python +51 -0
- data/sources/python/moments.python +65 -0
- data/sources/python/nbody.python +123 -0
- data/sources/python/nbody.python-2.python +120 -0
- data/sources/python/nestedloop.python +24 -0
- data/sources/python/nsieve.python +27 -0
- data/sources/python/nsieve.python-2.python +23 -0
- data/sources/python/nsieve.python-4.python +25 -0
- data/sources/python/nsievebits.python +27 -0
- data/sources/python/nsievebits.python-2.python +43 -0
- data/sources/python/objinst.python +53 -0
- data/sources/python/partialsums.python +37 -0
- data/sources/python/partialsums.python-2.python +35 -0
- data/sources/python/partialsums.python-3.python +48 -0
- data/sources/python/pidigits.python +38 -0
- data/sources/python/pidigits.python-3.python +63 -0
- data/sources/python/pidigits.python-4.python +24 -0
- data/sources/python/process.python +51 -0
- data/sources/python/process.python-2.python +133 -0
- data/sources/python/prodcons.python +51 -0
- data/sources/python/prodcons.python-2.python +0 -0
- data/sources/python/random.python +27 -0
- data/sources/python/raytracer.python +203 -0
- data/sources/python/recursive.python +35 -0
- data/sources/python/regexdna.python +39 -0
- data/sources/python/regexdna.python-2.python +34 -0
- data/sources/python/regexmatch.python +36 -0
- data/sources/python/revcomp.python-3.python +31 -0
- data/sources/python/reversefile.python +13 -0
- data/sources/python/reversefile.python-2.python +0 -0
- data/sources/python/reversefile.python-3.python +0 -0
- data/sources/python/sieve.python +50 -0
- data/sources/python/spectralnorm.python-2.python +36 -0
- data/sources/python/spellcheck.python +17 -0
- data/sources/python/strcat.python +35 -0
- data/sources/python/strcat.python-2.python +0 -0
- data/sources/python/sumcol.python-2.python +0 -0
- data/sources/python/sumcol.python-3.python +0 -0
- data/sources/python/takfp.python +19 -0
- data/sources/python/tcpecho.python +67 -0
- data/sources/python/tcprequest.python +67 -0
- data/sources/python/tcpstream.python +67 -0
- data/sources/python/threadring.python +47 -0
- data/sources/python/threadring.python-2.python +40 -0
- data/sources/python/threadring.python-3.python +34 -0
- data/sources/python/wc.python-2.python +19 -0
- data/sources/python/wordfreq.python +43 -0
- data/sources/python/wordfreq.python-2.python +0 -0
- data/sources/python/wordfreq.python-3.python +28 -0
- data/sources/python/wordfreq.python-4.python +38 -0
- data/sources/python/wordfreq.python-5.python +39 -0
- data/sources/ruby/ackermann.ruby +17 -0
- data/sources/ruby/ackermann.ruby-5.ruby +153 -0
- data/sources/ruby/ary.ruby +22 -0
- data/sources/ruby/binarytrees.ruby-2.ruby +55 -0
- data/sources/ruby/chameneos.ruby-2.ruby +71 -0
- data/sources/ruby/dispatch.ruby +114 -0
- data/sources/ruby/echo.ruby +41 -0
- data/sources/ruby/except.ruby +61 -0
- data/sources/ruby/except.ruby-2.ruby +61 -0
- data/sources/ruby/fannkuch.ruby +42 -0
- data/sources/ruby/fasta.ruby +81 -0
- data/sources/ruby/fibo.ruby +15 -0
- data/sources/ruby/harmonic.ruby-2.ruby +15 -0
- data/sources/ruby/hash.ruby +19 -0
- data/sources/ruby/hash2.ruby +23 -0
- data/sources/ruby/heapsort.ruby +55 -0
- data/sources/ruby/hello.ruby +6 -0
- data/sources/ruby/knucleotide.ruby-2.ruby +44 -0
- data/sources/ruby/lists.ruby +46 -0
- data/sources/ruby/mandelbrot.ruby-3.ruby +63 -0
- data/sources/ruby/matrix.ruby +40 -0
- data/sources/ruby/matrix.ruby-2.ruby +30 -0
- data/sources/ruby/message.ruby +29 -0
- data/sources/ruby/message.ruby-2.ruby +24 -0
- data/sources/ruby/meteor.ruby +386 -0
- data/sources/ruby/meteor.ruby-2.ruby +561 -0
- data/sources/ruby/methcall.ruby +58 -0
- data/sources/ruby/methcall.ruby-2.ruby +54 -0
- data/sources/ruby/moments.ruby +64 -0
- data/sources/ruby/nbody.ruby-2.ruby +145 -0
- data/sources/ruby/nestedloop.ruby +22 -0
- data/sources/ruby/nsieve.ruby +36 -0
- data/sources/ruby/nsieve.ruby-2.ruby +25 -0
- data/sources/ruby/nsievebits.ruby-2.ruby +42 -0
- data/sources/ruby/objinst.ruby +58 -0
- data/sources/ruby/partialsums.ruby +39 -0
- data/sources/ruby/pidigits.ruby +92 -0
- data/sources/ruby/pidigits.ruby-2.ruby +109 -0
- data/sources/ruby/prodcons.ruby +41 -0
- data/sources/ruby/random.ruby +17 -0
- data/sources/ruby/recursive.ruby-2.ruby +53 -0
- data/sources/ruby/regexdna.ruby +32 -0
- data/sources/ruby/regexdna.ruby-2.ruby +38 -0
- data/sources/ruby/regexmatch.ruby +33 -0
- data/sources/ruby/revcomp.ruby +28 -0
- data/sources/ruby/reversefile.ruby +7 -0
- data/sources/ruby/sieve.ruby +30 -0
- data/sources/ruby/spectralnorm.ruby +48 -0
- data/sources/ruby/spellcheck.ruby +18 -0
- data/sources/ruby/spellcheck.ruby-2.ruby +0 -0
- data/sources/ruby/strcat.ruby +12 -0
- data/sources/ruby/strcat.ruby-2.ruby +12 -0
- data/sources/ruby/sumcol.ruby +12 -0
- data/sources/ruby/sumcol.ruby-2.ruby +5 -0
- data/sources/ruby/takfp.ruby +15 -0
- data/sources/ruby/tcpecho.ruby +45 -0
- data/sources/ruby/tcprequest.ruby +45 -0
- data/sources/ruby/tcpstream.ruby +45 -0
- data/sources/ruby/threadring.ruby +61 -0
- data/sources/ruby/threadring.ruby-2.ruby +33 -0
- data/sources/ruby/wc.ruby +15 -0
- data/sources/ruby/wordfreq.ruby +17 -0
- data/sources/ruby/wordfreq.ruby2.ruby +0 -0
- data/test/fixtures/sources/gcc/ackermann.gcc-2.gcc +93 -0
- data/test/fixtures/sources/python/ackermann.python +21 -0
- data/test/fixtures/sources/ruby/ackermann.ruby +17 -0
- data/test/test_source_classifier.rb +40 -0
- data/test/test_trainer.rb +34 -0
- data/trainer.bin +1193 -0
- metadata +393 -0
@@ -0,0 +1,120 @@
|
|
1
|
+
/* -*- mode: c -*-
|
2
|
+
* $Id: moments.gcc,v 1.1.1.1 2004-05-19 18:10:47 bfulgham Exp $
|
3
|
+
* http://www.bagley.org/~doug/shootout/
|
4
|
+
* from Waldek Hebisch
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <stdio.h>
|
8
|
+
#include <stdlib.h>
|
9
|
+
#include <math.h>
|
10
|
+
|
11
|
+
#define MAXLINELEN 128
|
12
|
+
|
13
|
+
/* kmedian permutes elements of a to get
|
14
|
+
a[i]<=a[k] for i<k
|
15
|
+
a[i]>=a[k] for i>k
|
16
|
+
See. N. Wirth, Algorithms+data structures = Programs
|
17
|
+
*/
|
18
|
+
|
19
|
+
void kmedian(double *a, int n, int k)
|
20
|
+
{
|
21
|
+
while (1){
|
22
|
+
int j=random()%n;
|
23
|
+
double b = a[j];
|
24
|
+
int i=0;
|
25
|
+
j = n-1;
|
26
|
+
while(1) {
|
27
|
+
while( a[i]<b ) i++;
|
28
|
+
while( a[j]>b ) j--;
|
29
|
+
if(i<j) {
|
30
|
+
double tmp=a[i];
|
31
|
+
a[i]=a[j];
|
32
|
+
a[j]=tmp;
|
33
|
+
i++;
|
34
|
+
j--;
|
35
|
+
} else {
|
36
|
+
if(a[j]<b) j++;
|
37
|
+
if(a[i]>b) i--;
|
38
|
+
break;
|
39
|
+
}
|
40
|
+
}
|
41
|
+
if(i<k) {
|
42
|
+
k-=i+1;
|
43
|
+
n-=i+1;
|
44
|
+
a+=i+1;
|
45
|
+
} else if (j>k) {
|
46
|
+
n=j;
|
47
|
+
} else return;
|
48
|
+
}
|
49
|
+
}
|
50
|
+
|
51
|
+
double max(double *a, int n)
|
52
|
+
{
|
53
|
+
int j;
|
54
|
+
double temp=a[0];
|
55
|
+
for(j=1;j<n;j++) {
|
56
|
+
if(a[j]>temp) {
|
57
|
+
temp=a[j];
|
58
|
+
}
|
59
|
+
}
|
60
|
+
return temp;
|
61
|
+
}
|
62
|
+
|
63
|
+
|
64
|
+
int
|
65
|
+
main() {
|
66
|
+
char line[MAXLINELEN];
|
67
|
+
int i, n = 0, mid = 0;
|
68
|
+
double sum = 0.0;
|
69
|
+
double mean = 0.0;
|
70
|
+
double average_deviation = 0.0;
|
71
|
+
double standard_deviation = 0.0;
|
72
|
+
double variance = 0.0;
|
73
|
+
double skew = 0.0;
|
74
|
+
double kurtosis = 0.0;
|
75
|
+
double median = 0.0;
|
76
|
+
double deviation = 0.0;
|
77
|
+
int array_size = 4096;
|
78
|
+
|
79
|
+
double *nums = (double *)malloc(array_size * sizeof(double));
|
80
|
+
|
81
|
+
while (fgets(line, MAXLINELEN, stdin)) {
|
82
|
+
sum += (nums[n++] = atof(line));
|
83
|
+
if (n == array_size) {
|
84
|
+
array_size *= 2;
|
85
|
+
nums = (double *)realloc(nums, array_size * sizeof(double));
|
86
|
+
}
|
87
|
+
}
|
88
|
+
mean = sum/n;
|
89
|
+
for (i=0; i<n; i++) {
|
90
|
+
double dev = nums[i] - mean;
|
91
|
+
double dev2=dev*dev;
|
92
|
+
double dev3=dev2*dev;
|
93
|
+
double dev4=dev3*dev;
|
94
|
+
average_deviation += fabs(dev);
|
95
|
+
variance += dev2 /*pow(deviation,2)*/;
|
96
|
+
skew += dev3 /* pow(deviation,3) */;
|
97
|
+
kurtosis += dev4 /* pow(deviation,4) */;
|
98
|
+
}
|
99
|
+
average_deviation /= n;
|
100
|
+
variance /= (n - 1);
|
101
|
+
standard_deviation = sqrt(variance);
|
102
|
+
if (variance) {
|
103
|
+
skew /= (n * variance * standard_deviation);
|
104
|
+
kurtosis = (kurtosis/(n * variance * variance)) - 3.0;
|
105
|
+
}
|
106
|
+
mid = (n/2);
|
107
|
+
kmedian(nums, n, mid);
|
108
|
+
median = n % 2 ? nums[mid] : (nums[mid] + max(nums,mid))/2;
|
109
|
+
free(nums);
|
110
|
+
printf("n: %d\n", n);
|
111
|
+
printf("median: %f\n", median);
|
112
|
+
printf("mean: %f\n", mean);
|
113
|
+
printf("average_deviation: %f\n", average_deviation);
|
114
|
+
printf("standard_deviation: %f\n", standard_deviation);
|
115
|
+
printf("variance: %f\n", variance);
|
116
|
+
printf("skew: %f\n", skew);
|
117
|
+
printf("kurtosis: %f\n", kurtosis);
|
118
|
+
|
119
|
+
return(0);
|
120
|
+
}
|
@@ -0,0 +1,142 @@
|
|
1
|
+
/*
|
2
|
+
* The Great Computer Language Shootout
|
3
|
+
* http://shootout.alioth.debian.org/
|
4
|
+
*
|
5
|
+
* contributed by Christoph Bauer
|
6
|
+
*
|
7
|
+
*/
|
8
|
+
|
9
|
+
#include <math.h>
|
10
|
+
#include <stdio.h>
|
11
|
+
#include <stdlib.h>
|
12
|
+
|
13
|
+
#define pi 3.141592653589793
|
14
|
+
#define solar_mass (4 * pi * pi)
|
15
|
+
#define days_per_year 365.24
|
16
|
+
|
17
|
+
struct planet {
|
18
|
+
double x, y, z;
|
19
|
+
double vx, vy, vz;
|
20
|
+
double mass;
|
21
|
+
};
|
22
|
+
|
23
|
+
void advance(int nbodies, struct planet * bodies, double dt)
|
24
|
+
{
|
25
|
+
int i, j;
|
26
|
+
|
27
|
+
for (i = 0; i < nbodies; i++) {
|
28
|
+
struct planet * b = &(bodies[i]);
|
29
|
+
for (j = i + 1; j < nbodies; j++) {
|
30
|
+
struct planet * b2 = &(bodies[j]);
|
31
|
+
double dx = b->x - b2->x;
|
32
|
+
double dy = b->y - b2->y;
|
33
|
+
double dz = b->z - b2->z;
|
34
|
+
double distance = sqrt(dx * dx + dy * dy + dz * dz);
|
35
|
+
double mag = dt / (distance * distance * distance);
|
36
|
+
b->vx -= dx * b2->mass * mag;
|
37
|
+
b->vy -= dy * b2->mass * mag;
|
38
|
+
b->vz -= dz * b2->mass * mag;
|
39
|
+
b2->vx += dx * b->mass * mag;
|
40
|
+
b2->vy += dy * b->mass * mag;
|
41
|
+
b2->vz += dz * b->mass * mag;
|
42
|
+
}
|
43
|
+
}
|
44
|
+
for (i = 0; i < nbodies; i++) {
|
45
|
+
struct planet * b = &(bodies[i]);
|
46
|
+
b->x += dt * b->vx;
|
47
|
+
b->y += dt * b->vy;
|
48
|
+
b->z += dt * b->vz;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
|
52
|
+
double energy(int nbodies, struct planet * bodies)
|
53
|
+
{
|
54
|
+
double e;
|
55
|
+
int i, j;
|
56
|
+
|
57
|
+
e = 0.0;
|
58
|
+
for (i = 0; i < nbodies; i++) {
|
59
|
+
struct planet * b = &(bodies[i]);
|
60
|
+
e += 0.5 * b->mass * (b->vx * b->vx + b->vy * b->vy + b->vz * b->vz);
|
61
|
+
for (j = i + 1; j < nbodies; j++) {
|
62
|
+
struct planet * b2 = &(bodies[j]);
|
63
|
+
double dx = b->x - b2->x;
|
64
|
+
double dy = b->y - b2->y;
|
65
|
+
double dz = b->z - b2->z;
|
66
|
+
double distance = sqrt(dx * dx + dy * dy + dz * dz);
|
67
|
+
e -= (b->mass * b2->mass) / distance;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
return e;
|
71
|
+
}
|
72
|
+
|
73
|
+
void offset_momentum(int nbodies, struct planet * bodies)
|
74
|
+
{
|
75
|
+
double px = 0.0, py = 0.0, pz = 0.0;
|
76
|
+
int i;
|
77
|
+
for (i = 0; i < nbodies; i++) {
|
78
|
+
px += bodies[i].vx * bodies[i].mass;
|
79
|
+
py += bodies[i].vy * bodies[i].mass;
|
80
|
+
pz += bodies[i].vz * bodies[i].mass;
|
81
|
+
}
|
82
|
+
bodies[0].vx = - px / solar_mass;
|
83
|
+
bodies[0].vy = - py / solar_mass;
|
84
|
+
bodies[0].vz = - pz / solar_mass;
|
85
|
+
}
|
86
|
+
|
87
|
+
#define NBODIES 5
|
88
|
+
struct planet bodies[NBODIES] = {
|
89
|
+
{ /* sun */
|
90
|
+
0, 0, 0, 0, 0, 0, solar_mass
|
91
|
+
},
|
92
|
+
{ /* jupiter */
|
93
|
+
4.84143144246472090e+00,
|
94
|
+
-1.16032004402742839e+00,
|
95
|
+
-1.03622044471123109e-01,
|
96
|
+
1.66007664274403694e-03 * days_per_year,
|
97
|
+
7.69901118419740425e-03 * days_per_year,
|
98
|
+
-6.90460016972063023e-05 * days_per_year,
|
99
|
+
9.54791938424326609e-04 * solar_mass
|
100
|
+
},
|
101
|
+
{ /* saturn */
|
102
|
+
8.34336671824457987e+00,
|
103
|
+
4.12479856412430479e+00,
|
104
|
+
-4.03523417114321381e-01,
|
105
|
+
-2.76742510726862411e-03 * days_per_year,
|
106
|
+
4.99852801234917238e-03 * days_per_year,
|
107
|
+
2.30417297573763929e-05 * days_per_year,
|
108
|
+
2.85885980666130812e-04 * solar_mass
|
109
|
+
},
|
110
|
+
{ /* uranus */
|
111
|
+
1.28943695621391310e+01,
|
112
|
+
-1.51111514016986312e+01,
|
113
|
+
-2.23307578892655734e-01,
|
114
|
+
2.96460137564761618e-03 * days_per_year,
|
115
|
+
2.37847173959480950e-03 * days_per_year,
|
116
|
+
-2.96589568540237556e-05 * days_per_year,
|
117
|
+
4.36624404335156298e-05 * solar_mass
|
118
|
+
},
|
119
|
+
{ /* neptune */
|
120
|
+
1.53796971148509165e+01,
|
121
|
+
-2.59193146099879641e+01,
|
122
|
+
1.79258772950371181e-01,
|
123
|
+
2.68067772490389322e-03 * days_per_year,
|
124
|
+
1.62824170038242295e-03 * days_per_year,
|
125
|
+
-9.51592254519715870e-05 * days_per_year,
|
126
|
+
5.15138902046611451e-05 * solar_mass
|
127
|
+
}
|
128
|
+
};
|
129
|
+
|
130
|
+
int main(int argc, char ** argv)
|
131
|
+
{
|
132
|
+
int n = atoi(argv[1]);
|
133
|
+
int i;
|
134
|
+
|
135
|
+
offset_momentum(NBODIES, bodies);
|
136
|
+
printf ("%.9f\n", energy(NBODIES, bodies));
|
137
|
+
for (i = 1; i <= n; i++)
|
138
|
+
advance(NBODIES, bodies, 0.01);
|
139
|
+
printf ("%.9f\n", energy(NBODIES, bodies));
|
140
|
+
return 0;
|
141
|
+
}
|
142
|
+
|
@@ -0,0 +1,143 @@
|
|
1
|
+
/*
|
2
|
+
* The Computer Language Benchmarks Game
|
3
|
+
* http://shootout.alioth.debian.org/
|
4
|
+
*
|
5
|
+
* contributed by Christoph Bauer
|
6
|
+
* modified by bearophile
|
7
|
+
*/
|
8
|
+
|
9
|
+
|
10
|
+
#include <math.h>
|
11
|
+
#include <stdio.h>
|
12
|
+
#include <stdlib.h>
|
13
|
+
|
14
|
+
#define pi 3.141592653589793
|
15
|
+
#define solar_mass (4 * pi * pi)
|
16
|
+
#define days_per_year 365.24
|
17
|
+
|
18
|
+
struct planet {
|
19
|
+
double x, y, z;
|
20
|
+
double vx, vy, vz;
|
21
|
+
double mass;
|
22
|
+
};
|
23
|
+
|
24
|
+
void advance(int nbodies, struct planet * bodies, double dt)
|
25
|
+
{
|
26
|
+
int i, j;
|
27
|
+
|
28
|
+
for (i = 0; i < nbodies; i++) {
|
29
|
+
struct planet * b = &(bodies[i]);
|
30
|
+
for (j = i + 1; j < nbodies; j++) {
|
31
|
+
struct planet * b2 = &(bodies[j]);
|
32
|
+
double dx = b->x - b2->x;
|
33
|
+
double dy = b->y - b2->y;
|
34
|
+
double dz = b->z - b2->z;
|
35
|
+
double distance_squared = dx * dx + dy * dy + dz * dz;
|
36
|
+
double distance = sqrt(distance_squared);
|
37
|
+
double mag = dt / (distance * distance_squared);
|
38
|
+
b->vx -= dx * b2->mass * mag;
|
39
|
+
b->vy -= dy * b2->mass * mag;
|
40
|
+
b->vz -= dz * b2->mass * mag;
|
41
|
+
b2->vx += dx * b->mass * mag;
|
42
|
+
b2->vy += dy * b->mass * mag;
|
43
|
+
b2->vz += dz * b->mass * mag;
|
44
|
+
}
|
45
|
+
}
|
46
|
+
for (i = 0; i < nbodies; i++) {
|
47
|
+
struct planet * b = &(bodies[i]);
|
48
|
+
b->x += dt * b->vx;
|
49
|
+
b->y += dt * b->vy;
|
50
|
+
b->z += dt * b->vz;
|
51
|
+
}
|
52
|
+
}
|
53
|
+
|
54
|
+
double energy(int nbodies, struct planet * bodies)
|
55
|
+
{
|
56
|
+
double e;
|
57
|
+
int i, j;
|
58
|
+
|
59
|
+
e = 0.0;
|
60
|
+
for (i = 0; i < nbodies; i++) {
|
61
|
+
struct planet * b = &(bodies[i]);
|
62
|
+
e += 0.5 * b->mass * (b->vx * b->vx + b->vy * b->vy + b->vz * b->vz);
|
63
|
+
for (j = i + 1; j < nbodies; j++) {
|
64
|
+
struct planet * b2 = &(bodies[j]);
|
65
|
+
double dx = b->x - b2->x;
|
66
|
+
double dy = b->y - b2->y;
|
67
|
+
double dz = b->z - b2->z;
|
68
|
+
double distance = sqrt(dx * dx + dy * dy + dz * dz);
|
69
|
+
e -= (b->mass * b2->mass) / distance;
|
70
|
+
}
|
71
|
+
}
|
72
|
+
return e;
|
73
|
+
}
|
74
|
+
|
75
|
+
void offset_momentum(int nbodies, struct planet * bodies)
|
76
|
+
{
|
77
|
+
double px = 0.0, py = 0.0, pz = 0.0;
|
78
|
+
int i;
|
79
|
+
for (i = 0; i < nbodies; i++) {
|
80
|
+
px += bodies[i].vx * bodies[i].mass;
|
81
|
+
py += bodies[i].vy * bodies[i].mass;
|
82
|
+
pz += bodies[i].vz * bodies[i].mass;
|
83
|
+
}
|
84
|
+
bodies[0].vx = - px / solar_mass;
|
85
|
+
bodies[0].vy = - py / solar_mass;
|
86
|
+
bodies[0].vz = - pz / solar_mass;
|
87
|
+
}
|
88
|
+
|
89
|
+
#define NBODIES 5
|
90
|
+
struct planet bodies[NBODIES] = {
|
91
|
+
{ /* sun */
|
92
|
+
0, 0, 0, 0, 0, 0, solar_mass
|
93
|
+
},
|
94
|
+
{ /* jupiter */
|
95
|
+
4.84143144246472090e+00,
|
96
|
+
-1.16032004402742839e+00,
|
97
|
+
-1.03622044471123109e-01,
|
98
|
+
1.66007664274403694e-03 * days_per_year,
|
99
|
+
7.69901118419740425e-03 * days_per_year,
|
100
|
+
-6.90460016972063023e-05 * days_per_year,
|
101
|
+
9.54791938424326609e-04 * solar_mass
|
102
|
+
},
|
103
|
+
{ /* saturn */
|
104
|
+
8.34336671824457987e+00,
|
105
|
+
4.12479856412430479e+00,
|
106
|
+
-4.03523417114321381e-01,
|
107
|
+
-2.76742510726862411e-03 * days_per_year,
|
108
|
+
4.99852801234917238e-03 * days_per_year,
|
109
|
+
2.30417297573763929e-05 * days_per_year,
|
110
|
+
2.85885980666130812e-04 * solar_mass
|
111
|
+
},
|
112
|
+
{ /* uranus */
|
113
|
+
1.28943695621391310e+01,
|
114
|
+
-1.51111514016986312e+01,
|
115
|
+
-2.23307578892655734e-01,
|
116
|
+
2.96460137564761618e-03 * days_per_year,
|
117
|
+
2.37847173959480950e-03 * days_per_year,
|
118
|
+
-2.96589568540237556e-05 * days_per_year,
|
119
|
+
4.36624404335156298e-05 * solar_mass
|
120
|
+
},
|
121
|
+
{ /* neptune */
|
122
|
+
1.53796971148509165e+01,
|
123
|
+
-2.59193146099879641e+01,
|
124
|
+
1.79258772950371181e-01,
|
125
|
+
2.68067772490389322e-03 * days_per_year,
|
126
|
+
1.62824170038242295e-03 * days_per_year,
|
127
|
+
-9.51592254519715870e-05 * days_per_year,
|
128
|
+
5.15138902046611451e-05 * solar_mass
|
129
|
+
}
|
130
|
+
};
|
131
|
+
|
132
|
+
int main(int argc, char ** argv)
|
133
|
+
{
|
134
|
+
int n = atoi(argv[1]);
|
135
|
+
int i;
|
136
|
+
|
137
|
+
offset_momentum(NBODIES, bodies);
|
138
|
+
printf ("%.9f\n", energy(NBODIES, bodies));
|
139
|
+
for (i = 1; i <= n; i++)
|
140
|
+
advance(NBODIES, bodies, 0.01);
|
141
|
+
printf ("%.9f\n", energy(NBODIES, bodies));
|
142
|
+
return 0;
|
143
|
+
}
|
@@ -0,0 +1,142 @@
|
|
1
|
+
/*
|
2
|
+
* The Computer Language Benchmarks Game
|
3
|
+
* http://shootout.alioth.debian.org/
|
4
|
+
*
|
5
|
+
* Contributed by Christoph Bauer
|
6
|
+
* modified by Jorge Peixoto de Morais Neto
|
7
|
+
*/
|
8
|
+
|
9
|
+
#include <stdio.h>
|
10
|
+
#include <stdlib.h>
|
11
|
+
#include <math.h>
|
12
|
+
#include <err.h>
|
13
|
+
|
14
|
+
#define SOLAR_MASS (4 * M_PI * M_PI)
|
15
|
+
|
16
|
+
/*power-of-two alignment helps when indexing arrays */
|
17
|
+
struct __attribute__ ((aligned(64))) planet_t {
|
18
|
+
double x, y, z;
|
19
|
+
double vx, vy, vz;
|
20
|
+
double mass;
|
21
|
+
};
|
22
|
+
typedef struct planet_t planet_t;
|
23
|
+
|
24
|
+
static void advance (planet_t *bodies, int nb, double dt) {
|
25
|
+
int i, j;
|
26
|
+
for (i = nb - 1; i >= 0; i--)
|
27
|
+
for (j = 0; j < i; j++) {
|
28
|
+
double dx = bodies[i].x - bodies[j].x;
|
29
|
+
double dy = bodies[i].y - bodies[j].y;
|
30
|
+
double dz = bodies[i].z - bodies[j].z;
|
31
|
+
double distance = sqrt (dx * dx + dy * dy + dz * dz);
|
32
|
+
double mag = dt / (distance * distance * distance);
|
33
|
+
bodies[i].vx -= bodies[j].mass * mag * dx;
|
34
|
+
bodies[i].vy -= bodies[j].mass * mag * dy;
|
35
|
+
bodies[i].vz -= bodies[j].mass * mag * dz;
|
36
|
+
bodies[j].vy += bodies[i].mass * mag * dy;
|
37
|
+
bodies[j].vz += bodies[i].mass * mag * dz;
|
38
|
+
bodies[j].vx += bodies[i].mass * mag * dx;
|
39
|
+
}
|
40
|
+
|
41
|
+
for (i = nb - 1; i >= 0; i--) {
|
42
|
+
bodies[i].x += dt * bodies[i].vx;
|
43
|
+
bodies[i].y += dt * bodies[i].vy;
|
44
|
+
bodies[i].z += dt * bodies[i].vz;
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
static inline double energy (planet_t const *bodies, int nb) {
|
49
|
+
double e = 0.0;
|
50
|
+
int i, j;
|
51
|
+
for (i = nb - 1; i >= 0; i--)
|
52
|
+
e += bodies[i].mass *
|
53
|
+
(bodies[i].vx * bodies[i].vx + bodies[i].vy * bodies[i].vy + bodies[i].vz * bodies[i].vz);
|
54
|
+
|
55
|
+
e /= 2;
|
56
|
+
for (i = nb - 1; i >= 0; i--)
|
57
|
+
for (j = 0; j < i; j++) {
|
58
|
+
double dx = bodies[i].x - bodies[j].x;
|
59
|
+
double dy = bodies[i].y - bodies[j].y;
|
60
|
+
double dz = bodies[i].z - bodies[j].z;
|
61
|
+
double distance = sqrt (dx * dx + dy * dy + dz * dz);
|
62
|
+
e -= bodies[i].mass * bodies[j].mass / distance;
|
63
|
+
}
|
64
|
+
return e;
|
65
|
+
}
|
66
|
+
|
67
|
+
static inline void offset_momentum (planet_t *bodies, int nb) {
|
68
|
+
double py = 0.0, pz = 0.0, px = 0.0;
|
69
|
+
int i;
|
70
|
+
for (i = nb - 1; i >= 0; i--) {
|
71
|
+
py += bodies[i].vy * bodies[i].mass;
|
72
|
+
pz += bodies[i].vz * bodies[i].mass;
|
73
|
+
px += bodies[i].vx * bodies[i].mass;
|
74
|
+
}
|
75
|
+
/*bodies[0] is the Sun*/
|
76
|
+
bodies[0].vx = -px / SOLAR_MASS;
|
77
|
+
bodies[0].vy = -py / SOLAR_MASS;
|
78
|
+
bodies[0].vz = -pz / SOLAR_MASS;
|
79
|
+
}
|
80
|
+
|
81
|
+
int main (int argc, char **argv) {
|
82
|
+
#define NBODIES 5
|
83
|
+
#define DAYS_PER_YEAR 365.24
|
84
|
+
static planet_t bodies[NBODIES] = {
|
85
|
+
{ /* sun */
|
86
|
+
0, 0, 0, 0, 0, 0, SOLAR_MASS
|
87
|
+
},
|
88
|
+
{ /* jupiter */
|
89
|
+
4.84143144246472090e+00,
|
90
|
+
-1.16032004402742839e+00,
|
91
|
+
-1.03622044471123109e-01,
|
92
|
+
1.66007664274403694e-03 * DAYS_PER_YEAR,
|
93
|
+
7.69901118419740425e-03 * DAYS_PER_YEAR,
|
94
|
+
-6.90460016972063023e-05 * DAYS_PER_YEAR,
|
95
|
+
9.54791938424326609e-04 * SOLAR_MASS
|
96
|
+
},
|
97
|
+
{ /* saturn */
|
98
|
+
8.34336671824457987e+00,
|
99
|
+
4.12479856412430479e+00,
|
100
|
+
-4.03523417114321381e-01,
|
101
|
+
-2.76742510726862411e-03 * DAYS_PER_YEAR,
|
102
|
+
4.99852801234917238e-03 * DAYS_PER_YEAR,
|
103
|
+
2.30417297573763929e-05 * DAYS_PER_YEAR,
|
104
|
+
2.85885980666130812e-04 * SOLAR_MASS
|
105
|
+
},
|
106
|
+
{ /* uranus */
|
107
|
+
1.28943695621391310e+01,
|
108
|
+
-1.51111514016986312e+01,
|
109
|
+
-2.23307578892655734e-01,
|
110
|
+
2.96460137564761618e-03 * DAYS_PER_YEAR,
|
111
|
+
2.37847173959480950e-03 * DAYS_PER_YEAR,
|
112
|
+
-2.96589568540237556e-05 * DAYS_PER_YEAR,
|
113
|
+
4.36624404335156298e-05 * SOLAR_MASS
|
114
|
+
},
|
115
|
+
{ /* neptune */
|
116
|
+
1.53796971148509165e+01,
|
117
|
+
-2.59193146099879641e+01,
|
118
|
+
1.79258772950371181e-01,
|
119
|
+
2.68067772490389322e-03 * DAYS_PER_YEAR,
|
120
|
+
1.62824170038242295e-03 * DAYS_PER_YEAR,
|
121
|
+
-9.51592254519715870e-05 * DAYS_PER_YEAR,
|
122
|
+
5.15138902046611451e-05 * SOLAR_MASS
|
123
|
+
}
|
124
|
+
};
|
125
|
+
long n = 1000;
|
126
|
+
if (argc >= 2) {
|
127
|
+
char *tail;
|
128
|
+
char const *arg = argv[1];
|
129
|
+
n = strtol (arg, &tail, 0);
|
130
|
+
if (tail == arg)
|
131
|
+
errx (1, "Could no convert \"%s\" to a long integer", arg);
|
132
|
+
}
|
133
|
+
|
134
|
+
offset_momentum (bodies, NBODIES);
|
135
|
+
printf ("%.9f\n", energy (bodies, NBODIES));
|
136
|
+
long i;
|
137
|
+
for (i = n; i > 0; i--)
|
138
|
+
advance (bodies, NBODIES, 0.01);
|
139
|
+
|
140
|
+
printf ("%.9f\n", energy (bodies, NBODIES));
|
141
|
+
return 0;
|
142
|
+
}
|