-
Notifications
You must be signed in to change notification settings - Fork 0
/
math.class.php
104 lines (93 loc) · 2.64 KB
/
math.class.php
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
namespace biophp;
class math {
/**
* @desc: Machine Learning
* @usage: \biophp\math\ml::...()
* @import: ml.class.php
*/
static public function ml () {}
/**
* @desc: Statistics
* @usage: \biophp\math\stat::...()
* @import: stat.class.php
*/
static public function stat () {}
/**
* @desc: Gene Set Enrichment Analysis
* @usage: \biophp\math\gsea::...()
* @import: gsea.class.php
*/
static public function gsea () {}
/**
* Draw the distribution for a serial of data
* The generated data could be put into excel to draw the curve
* @param array $data The data
* @param int $bins Number of bins
* @param float $start The start point
* @param float $end The end point
* @param string $file If given, ouput will be written to the file,
* otherwise to STDOUT
* @return
* START bin1 bin2 END
* |----x1----|----x2----|----...----|----xn----|
* |----y1----|----y2----|----...----|----yn----|
* y(n) is the number of data points in bin(n)
*/
static public function distribution($data, $bins=10, $start=0, $end=1, $file="") {
$f = (!empty($file)) ? fopen($file, 'w') : STDOUT;
$xs = array();
$cell = ($end-$start)/$bins;
for ($i=0; $i<$bins; $i++)
$xs[] = ($start+$cell/2) + $i*$cell;
fwrite($f, implode("\t", $xs) . PHP_EOL);
$ys = array_fill(0, $bins, 0);
foreach ($data as $d)
$ys[floor($d/$cell)] ++;
fwrite($f, implode("\t", $ys));
if (!empty($file)) fclose($file);
}
/**
* Matrix
* @param mixed $forc The matrix file or multi-dimension array
* @return biophp\math\matrix object
* @import: matrix.class.php
*/
static public function &matrix($forc) {
$m = new math\matrix($forc);
return $m;
}
/**
* Sparse Matrix
* @param mixed $forc The matrix file or multi-dimension array
* The array should be in format $array[$i][$j] = $value
* @return biophp\math\sparse object
* @import: sparse.class.php
*/
static public function &sparse($forc) {
$m = new math\sparse($forc);
return $m;
}
/**
* Calculate log(x!)
*/
static public function logfact ($x) {
$ser = ( 1.000000000190015
+ 76.18009172947146 / ($x + 2)
- 86.50532032941677 / ($x + 3)
+ 24.01409824083091 / ($x + 4)
- 1.231739572450155 / ($x + 5)
+ 0.12086509738661e-2 / ($x + 6)
- 0.5395239384953e-5 / ($x + 7) );
$tmp = $x + 6.5;
return ($x + 1.5) * log($tmp) - $tmp + log(2.5066282746310005 * $ser / ($x+1));
}
/**
* Calculate x!
*/
static public function fact($x) {
$ret = 1;
while ($x) $ret*=$x--;
return $ret;
}
}