Merge branch '138-toggle-free-look-with-hotkey' into 'main/atys-live'
[ryzomcore.git] / web / public_php / admin / jpgraph / jpgraph_regstat.php
blobc0fe94754a7021f582e3191c98de6d308218258e
1 <?php
2 /*=======================================================================
3 // File: JPGRAPH_REGSTAT.PHP
4 // Description: Regression and statistical analysis helper classes
5 // Created: 2002-12-01
6 // Author: Johan Persson (johanp@aditus.nu)
7 // Ver: $Id: jpgraph_regstat.php,v 1.1 2006/07/07 13:37:14 powles Exp $
8 //
9 // Copyright (c) Aditus Consulting. All rights reserved.
10 //========================================================================
13 //------------------------------------------------------------------------
14 // CLASS Spline
15 // Create a new data array from an existing data array but with more points.
16 // The new points are interpolated using a cubic spline algorithm
17 //------------------------------------------------------------------------
18 class Spline {
19 // 3:rd degree polynom approximation
21 var $xdata,$ydata; // Data vectors
22 var $y2; // 2:nd derivate of ydata
23 var $n=0;
25 function Spline($xdata,$ydata) {
26 $this->y2 = array();
27 $this->xdata = $xdata;
28 $this->ydata = $ydata;
30 $n = count($ydata);
31 $this->n = $n;
32 if( $this->n !== count($xdata) ) {
33 JpGraphError::RaiseL(19001);
34 //('Spline: Number of X and Y coordinates must be the same');
37 // Natural spline 2:derivate == 0 at endpoints
38 $this->y2[0] = 0.0;
39 $this->y2[$n-1] = 0.0;
40 $delta[0] = 0.0;
42 // Calculate 2:nd derivate
43 for($i=1; $i < $n-1; ++$i) {
44 $d = ($xdata[$i+1]-$xdata[$i-1]);
45 if( $d == 0 ) {
46 JpGraphError::RaiseL(19002);
47 //('Invalid input data for spline. Two or more consecutive input X-values are equal. Each input X-value must differ since from a mathematical point of view it must be a one-to-one mapping, i.e. each X-value must correspond to exactly one Y-value.');
49 $s = ($xdata[$i]-$xdata[$i-1])/$d;
50 $p = $s*$this->y2[$i-1]+2.0;
51 $this->y2[$i] = ($s-1.0)/$p;
52 $delta[$i] = ($ydata[$i+1]-$ydata[$i])/($xdata[$i+1]-$xdata[$i]) -
53 ($ydata[$i]-$ydata[$i-1])/($xdata[$i]-$xdata[$i-1]);
54 $delta[$i] = (6.0*$delta[$i]/($xdata[$i+1]-$xdata[$i-1])-$s*$delta[$i-1])/$p;
57 // Backward substitution
58 for( $j=$n-2; $j >= 0; --$j ) {
59 $this->y2[$j] = $this->y2[$j]*$this->y2[$j+1] + $delta[$j];
63 // Return the two new data vectors
64 function Get($num=50) {
65 $n = $this->n ;
66 $step = ($this->xdata[$n-1]-$this->xdata[0]) / ($num-1);
67 $xnew=array();
68 $ynew=array();
69 $xnew[0] = $this->xdata[0];
70 $ynew[0] = $this->ydata[0];
71 for( $j=1; $j < $num; ++$j ) {
72 $xnew[$j] = $xnew[0]+$j*$step;
73 $ynew[$j] = $this->Interpolate($xnew[$j]);
75 return array($xnew,$ynew);
78 // Return a single interpolated Y-value from an x value
79 function Interpolate($xpoint) {
81 $max = $this->n-1;
82 $min = 0;
84 // Binary search to find interval
85 while( $max-$min > 1 ) {
86 $k = ($max+$min) / 2;
87 if( $this->xdata[$k] > $xpoint )
88 $max=$k;
89 else
90 $min=$k;
93 // Each interval is interpolated by a 3:degree polynom function
94 $h = $this->xdata[$max]-$this->xdata[$min];
96 if( $h == 0 ) {
97 JpGraphError::RaiseL(19002);
98 //('Invalid input data for spline. Two or more consecutive input X-values are equal. Each input X-value must differ since from a mathematical point of view it must be a one-to-one mapping, i.e. each X-value must correspond to exactly one Y-value.');
102 $a = ($this->xdata[$max]-$xpoint)/$h;
103 $b = ($xpoint-$this->xdata[$min])/$h;
104 return $a*$this->ydata[$min]+$b*$this->ydata[$max]+
105 (($a*$a*$a-$a)*$this->y2[$min]+($b*$b*$b-$b)*$this->y2[$max])*($h*$h)/6.0;
109 //------------------------------------------------------------------------
110 // CLASS Bezier
111 // Create a new data array from a number of control points
112 //------------------------------------------------------------------------
113 class Bezier {
115 * @author Thomas Despoix, openXtrem company
116 * @license released under QPL
117 * @abstract Bezier interoplated point generation,
118 * computed from control points data sets, based on Paul Bourke algorithm :
119 * http://astronomy.swin.edu.au/~pbourke/curves/bezier/
121 var $datax = array();
122 var $datay = array();
123 var $n=0;
125 function Bezier($datax, $datay, $attraction_factor = 1) {
126 // Adding control point multiple time will raise their attraction power over the curve
127 $this->n = count($datax);
128 if( $this->n !== count($datay) ) {
129 JpGraphError::RaiseL(19003);
130 //('Bezier: Number of X and Y coordinates must be the same');
132 $idx=0;
133 foreach($datax as $datumx) {
134 for ($i = 0; $i < $attraction_factor; $i++) {
135 $this->datax[$idx++] = $datumx;
138 $idx=0;
139 foreach($datay as $datumy) {
140 for ($i = 0; $i < $attraction_factor; $i++) {
141 $this->datay[$idx++] = $datumy;
146 function Get($steps) {
147 $datax = array();
148 $datay = array();
149 for ($i = 0; $i < $steps; $i++) {
150 list($datumx, $datumy) = $this->GetPoint((double) $i / (double) $steps);
151 $datax[] = $datumx;
152 $datay[] = $datumy;
155 $datax[] = end($this->datax);
156 $datay[] = end($this->datay);
158 return array($datax, $datay);
161 function GetPoint($mu) {
162 $n = $this->n - 1;
163 $k = 0;
164 $kn = 0;
165 $nn = 0;
166 $nkn = 0;
167 $blend = 0.0;
168 $newx = 0.0;
169 $newy = 0.0;
171 $muk = 1.0;
172 $munk = (double) pow(1-$mu,(double) $n);
174 for ($k = 0; $k <= $n; $k++) {
175 $nn = $n;
176 $kn = $k;
177 $nkn = $n - $k;
178 $blend = $muk * $munk;
179 $muk *= $mu;
180 $munk /= (1-$mu);
181 while ($nn >= 1) {
182 $blend *= $nn;
183 $nn--;
184 if ($kn > 1) {
185 $blend /= (double) $kn;
186 $kn--;
188 if ($nkn > 1) {
189 $blend /= (double) $nkn;
190 $nkn--;
193 $newx += $this->datax[$k] * $blend;
194 $newy += $this->datay[$k] * $blend;
197 return array($newx, $newy);
201 // EOF