@@ -184,6 +184,7 @@ discard block |
||
184 | 184 | * @access public |
185 | 185 | * @param array $arr the data set |
186 | 186 | * @param optional int $opt data format: STATS_DATA_CUMMULATIVE or STATS_DATA_SIMPLE (default) |
187 | + * @param integer $opt |
|
187 | 188 | * @return mixed true on success, a PEAR_Error object otherwise |
188 | 189 | */ |
189 | 190 | function setData($arr, $opt=STATS_DATA_SIMPLE) {/*{{{*/ |
@@ -512,7 +513,7 @@ discard block |
||
512 | 513 | * Handles cummulative data sets correctly |
513 | 514 | * |
514 | 515 | * @access public |
515 | - * @param numeric $n the exponent |
|
516 | + * @param integer $n the exponent |
|
516 | 517 | * @return mixed the sum on success, a PEAR_Error object otherwise |
517 | 518 | * @see calc() |
518 | 519 | * @see sum() |
@@ -560,7 +561,7 @@ discard block |
||
560 | 561 | * Handles cummulative data sets correctly |
561 | 562 | * |
562 | 563 | * @access public |
563 | - * @param numeric $n the exponent |
|
564 | + * @param integer $n the exponent |
|
564 | 565 | * @return mixed the product on success, a PEAR_Error object otherwise |
565 | 566 | * @see product() |
566 | 567 | */ |
@@ -707,7 +708,7 @@ discard block |
||
707 | 708 | * |
708 | 709 | * @access public |
709 | 710 | * @param numeric $mean the fixed mean value |
710 | - * @return mixed the variance on success, a PEAR_Error object otherwise |
|
711 | + * @return double the variance on success, a PEAR_Error object otherwise |
|
711 | 712 | * @see __sumdiff() |
712 | 713 | * @see count() |
713 | 714 | * @see variance() |
@@ -1381,7 +1382,7 @@ discard block |
||
1381 | 1382 | * @todo need to double check generality of the algorithm |
1382 | 1383 | * |
1383 | 1384 | * @access public |
1384 | - * @param numeric $p the percentile to estimate, e.g. 25 for 25th percentile |
|
1385 | + * @param integer $p the percentile to estimate, e.g. 25 for 25th percentile |
|
1385 | 1386 | * @return mixed a numeric value on success, a PEAR_Error otherwise |
1386 | 1387 | * @see quartiles() |
1387 | 1388 | * @see median() |
@@ -1416,7 +1417,7 @@ discard block |
||
1416 | 1417 | * Utility function to calculate: SUM { (xi - mean)^n } |
1417 | 1418 | * |
1418 | 1419 | * @access private |
1419 | - * @param numeric $power the exponent |
|
1420 | + * @param integer $power the exponent |
|
1420 | 1421 | * @param optional double $mean the data set mean value |
1421 | 1422 | * @return mixed the sum on success, a PEAR_Error object otherwise |
1422 | 1423 | * |
@@ -1536,7 +1537,7 @@ discard block |
||
1536 | 1537 | * |
1537 | 1538 | * @access private |
1538 | 1539 | * @param mixed $v value to be formatted |
1539 | - * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
1540 | + * @param boolean $useErrorObject whether the raw PEAR_Error (when true, default), |
|
1540 | 1541 | * or only the error message will be returned (when false) |
1541 | 1542 | * @return mixed if the value is a PEAR_Error object, and $useErrorObject |
1542 | 1543 | * is false, then a string with the error message will be returned, |
@@ -117,1485 +117,1485 @@ |
||
117 | 117 | * @package Math_Stats |
118 | 118 | */ |
119 | 119 | class Base {/*{{{*/ |
120 | - // properties /*{{{*/ |
|
121 | - |
|
122 | - /** |
|
123 | - * The simple or cummulative data set. |
|
124 | - * Null by default. |
|
125 | - * |
|
126 | - * @access private |
|
127 | - * @var array |
|
128 | - */ |
|
129 | - public $_data = null; |
|
130 | - |
|
131 | - /** |
|
132 | - * Expanded data set. Only set when cummulative data |
|
133 | - * is being used. Null by default. |
|
134 | - * |
|
135 | - * @access private |
|
136 | - * @var array |
|
137 | - */ |
|
138 | - public $_dataExpanded = null; |
|
139 | - |
|
140 | - /** |
|
141 | - * Flag for data type, one of STATS_DATA_SIMPLE or |
|
142 | - * STATS_DATA_CUMMULATIVE. Null by default. |
|
143 | - * |
|
144 | - * @access private |
|
145 | - * @var int |
|
146 | - */ |
|
147 | - public $_dataOption = null; |
|
148 | - |
|
149 | - /** |
|
150 | - * Flag for null handling options. One of STATS_REJECT_NULL, |
|
151 | - * STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO |
|
152 | - * |
|
153 | - * @access private |
|
154 | - * @var int |
|
155 | - */ |
|
156 | - public $_nullOption; |
|
157 | - |
|
158 | - /** |
|
159 | - * Array for caching result values, should be reset |
|
160 | - * when using setData() |
|
161 | - * |
|
162 | - * @access private |
|
163 | - * @var array |
|
164 | - */ |
|
165 | - public $_calculatedValues = array(); |
|
166 | - |
|
167 | - /*}}}*/ |
|
168 | - |
|
169 | - /** |
|
170 | - * Constructor for the class |
|
171 | - * |
|
172 | - * @access public |
|
173 | - * @param optional int $nullOption how to handle null values |
|
174 | - * @return object Math_Stats |
|
175 | - */ |
|
176 | - function Math_Stats($nullOption=STATS_REJECT_NULL) {/*{{{*/ |
|
177 | - $this->_nullOption = $nullOption; |
|
178 | - }/*}}}*/ |
|
179 | - |
|
180 | - /** |
|
181 | - * Sets and verifies the data, checking for nulls and using |
|
182 | - * the current null handling option |
|
183 | - * |
|
184 | - * @access public |
|
185 | - * @param array $arr the data set |
|
186 | - * @param optional int $opt data format: STATS_DATA_CUMMULATIVE or STATS_DATA_SIMPLE (default) |
|
187 | - * @return mixed true on success, a PEAR_Error object otherwise |
|
188 | - */ |
|
189 | - function setData($arr, $opt=STATS_DATA_SIMPLE) {/*{{{*/ |
|
190 | - if (!is_array($arr)) { |
|
191 | - return PEAR::raiseError('invalid data, an array of numeric data was expected'); |
|
192 | - } |
|
193 | - $this->_data = null; |
|
194 | - $this->_dataExpanded = null; |
|
195 | - $this->_dataOption = null; |
|
196 | - $this->_calculatedValues = array(); |
|
197 | - if ($opt == STATS_DATA_SIMPLE) { |
|
198 | - $this->_dataOption = $opt; |
|
199 | - $this->_data = array_values($arr); |
|
200 | - } else if ($opt == STATS_DATA_CUMMULATIVE) { |
|
201 | - $this->_dataOption = $opt; |
|
202 | - $this->_data = $arr; |
|
203 | - $this->_dataExpanded = array(); |
|
204 | - } |
|
205 | - return $this->_validate(); |
|
206 | - }/*}}}*/ |
|
207 | - |
|
208 | - /** |
|
209 | - * Returns the data which might have been modified |
|
210 | - * according to the current null handling options. |
|
211 | - * |
|
212 | - * @access public |
|
213 | - * @param boolean $expanded whether to return a expanded list, default is false |
|
214 | - * @return mixed array of data on success, a PEAR_Error object otherwise |
|
215 | - * @see _validate() |
|
216 | - */ |
|
217 | - function getData($expanded=false) {/*{{{*/ |
|
218 | - if ($this->_data == null) { |
|
219 | - return PEAR::raiseError('data has not been set'); |
|
220 | - } |
|
221 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE && $expanded) { |
|
222 | - return $this->_dataExpanded; |
|
223 | - } else { |
|
224 | - return $this->_data; |
|
225 | - } |
|
226 | - }/*}}}*/ |
|
227 | - |
|
228 | - /** |
|
229 | - * Sets the null handling option. |
|
230 | - * Must be called before assigning a new data set containing null values |
|
231 | - * |
|
232 | - * @access public |
|
233 | - * @return mixed true on success, a PEAR_Error object otherwise |
|
234 | - * @see _validate() |
|
235 | - */ |
|
236 | - function setNullOption($nullOption) {/*{{{*/ |
|
237 | - if ($nullOption == STATS_REJECT_NULL |
|
238 | - || $nullOption == STATS_IGNORE_NULL |
|
239 | - || $nullOption == STATS_USE_NULL_AS_ZERO) { |
|
240 | - $this->_nullOption = $nullOption; |
|
241 | - return true; |
|
242 | - } else { |
|
243 | - return PEAR::raiseError('invalid null handling option expecting: '. |
|
244 | - 'STATS_REJECT_NULL, STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO'); |
|
245 | - } |
|
246 | - }/*}}}*/ |
|
247 | - |
|
248 | - /** |
|
249 | - * Transforms the data by substracting each entry from the mean and |
|
250 | - * dividing by its standard deviation. This will reset all pre-calculated |
|
251 | - * values to their original (unset) defaults. |
|
252 | - * |
|
253 | - * @access public |
|
254 | - * @return mixed true on success, a PEAR_Error object otherwise |
|
255 | - * @see mean() |
|
256 | - * @see stDev() |
|
257 | - * @see setData() |
|
258 | - */ |
|
259 | - function studentize() {/*{{{*/ |
|
260 | - $mean = $this->mean(); |
|
261 | - if (PEAR::isError($mean)) { |
|
262 | - return $mean; |
|
263 | - } |
|
264 | - $std = $this->stDev(); |
|
265 | - if (PEAR::isError($std)) { |
|
266 | - return $std; |
|
267 | - } |
|
268 | - if ($std == 0) { |
|
269 | - return PEAR::raiseError('cannot studentize data, standard deviation is zero.'); |
|
270 | - } |
|
271 | - $arr = array(); |
|
272 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
273 | - foreach ($this->_data as $val=>$freq) { |
|
274 | - $newval = ($val - $mean) / $std; |
|
275 | - $arr["$newval"] = $freq; |
|
276 | - } |
|
277 | - } else { |
|
278 | - foreach ($this->_data as $val) { |
|
279 | - $newval = ($val - $mean) / $std; |
|
280 | - $arr[] = $newval; |
|
281 | - } |
|
282 | - } |
|
283 | - return $this->setData($arr, $this->_dataOption); |
|
284 | - }/*}}}*/ |
|
285 | - |
|
286 | - /** |
|
287 | - * Transforms the data by substracting each entry from the mean. |
|
288 | - * This will reset all pre-calculated values to their original (unset) defaults. |
|
289 | - * |
|
290 | - * @access public |
|
291 | - * @return mixed true on success, a PEAR_Error object otherwise |
|
292 | - * @see mean() |
|
293 | - * @see setData() |
|
294 | - */ |
|
295 | - function center() {/*{{{*/ |
|
296 | - $mean = $this->mean(); |
|
297 | - if (PEAR::isError($mean)) { |
|
298 | - return $mean; |
|
299 | - } |
|
300 | - $arr = array(); |
|
301 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
302 | - foreach ($this->_data as $val=>$freq) { |
|
303 | - $newval = $val - $mean; |
|
304 | - $arr["$newval"] = $freq; |
|
305 | - } |
|
306 | - } else { |
|
307 | - foreach ($this->_data as $val) { |
|
308 | - $newval = $val - $mean; |
|
309 | - $arr[] = $newval; |
|
310 | - } |
|
311 | - } |
|
312 | - return $this->setData($arr, $this->_dataOption); |
|
313 | - }/*}}}*/ |
|
314 | - |
|
315 | - /** |
|
316 | - * Calculates the basic or full statistics for the data set |
|
317 | - * |
|
318 | - * @access public |
|
319 | - * @param int $mode one of STATS_BASIC or STATS_FULL |
|
320 | - * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
321 | - * or only the error message will be returned (when false), if an error happens. |
|
322 | - * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise |
|
323 | - * @see calcBasic() |
|
324 | - * @see calcFull() |
|
325 | - */ |
|
326 | - function calc($mode, $returnErrorObject=true) {/*{{{*/ |
|
327 | - if ($this->_data == null) { |
|
328 | - return PEAR::raiseError('data has not been set'); |
|
329 | - } |
|
330 | - if ($mode == STATS_BASIC) { |
|
331 | - return $this->calcBasic($returnErrorObject); |
|
332 | - } elseif ($mode == STATS_FULL) { |
|
333 | - return $this->calcFull($returnErrorObject); |
|
334 | - } else { |
|
335 | - return PEAR::raiseError('incorrect mode, expected STATS_BASIC or STATS_FULL'); |
|
336 | - } |
|
337 | - }/*}}}*/ |
|
338 | - |
|
339 | - /** |
|
340 | - * Calculates a basic set of statistics |
|
341 | - * |
|
342 | - * @access public |
|
343 | - * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
344 | - * or only the error message will be returned (when false), if an error happens. |
|
345 | - * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise |
|
346 | - * @see calc() |
|
347 | - * @see calcFull() |
|
348 | - */ |
|
349 | - function calcBasic($returnErrorObject=true) {/*{{{*/ |
|
350 | - return array ( |
|
351 | - 'min' => $this->__format($this->min(), $returnErrorObject), |
|
352 | - 'max' => $this->__format($this->max(), $returnErrorObject), |
|
353 | - 'sum' => $this->__format($this->sum(), $returnErrorObject), |
|
354 | - 'sum2' => $this->__format($this->sum2(), $returnErrorObject), |
|
355 | - 'count' => $this->__format($this->count(), $returnErrorObject), |
|
356 | - 'mean' => $this->__format($this->mean(), $returnErrorObject), |
|
357 | - 'stdev' => $this->__format($this->stDev(), $returnErrorObject), |
|
358 | - 'variance' => $this->__format($this->variance(), $returnErrorObject), |
|
359 | - 'range' => $this->__format($this->range(), $returnErrorObject) |
|
360 | - ); |
|
361 | - }/*}}}*/ |
|
362 | - |
|
363 | - /** |
|
364 | - * Calculates a full set of statistics |
|
365 | - * |
|
366 | - * @access public |
|
367 | - * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
368 | - * or only the error message will be returned (when false), if an error happens. |
|
369 | - * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise |
|
370 | - * @see calc() |
|
371 | - * @see calcBasic() |
|
372 | - */ |
|
373 | - function calcFull($returnErrorObject=true) {/*{{{*/ |
|
374 | - return array ( |
|
375 | - 'min' => $this->__format($this->min(), $returnErrorObject), |
|
376 | - 'max' => $this->__format($this->max(), $returnErrorObject), |
|
377 | - 'sum' => $this->__format($this->sum(), $returnErrorObject), |
|
378 | - 'sum2' => $this->__format($this->sum2(), $returnErrorObject), |
|
379 | - 'count' => $this->__format($this->count(), $returnErrorObject), |
|
380 | - 'mean' => $this->__format($this->mean(), $returnErrorObject), |
|
381 | - 'median' => $this->__format($this->median(), $returnErrorObject), |
|
382 | - 'mode' => $this->__format($this->mode(), $returnErrorObject), |
|
383 | - 'midrange' => $this->__format($this->midrange(), $returnErrorObject), |
|
384 | - 'geometric_mean' => $this->__format($this->geometricMean(), $returnErrorObject), |
|
385 | - 'harmonic_mean' => $this->__format($this->harmonicMean(), $returnErrorObject), |
|
386 | - 'stdev' => $this->__format($this->stDev(), $returnErrorObject), |
|
387 | - 'absdev' => $this->__format($this->absDev(), $returnErrorObject), |
|
388 | - 'variance' => $this->__format($this->variance(), $returnErrorObject), |
|
389 | - 'range' => $this->__format($this->range(), $returnErrorObject), |
|
390 | - 'std_error_of_mean' => $this->__format($this->stdErrorOfMean(), $returnErrorObject), |
|
391 | - 'skewness' => $this->__format($this->skewness(), $returnErrorObject), |
|
392 | - 'kurtosis' => $this->__format($this->kurtosis(), $returnErrorObject), |
|
393 | - 'coeff_of_variation' => $this->__format($this->coeffOfVariation(), $returnErrorObject), |
|
394 | - 'sample_central_moments' => array ( |
|
395 | - 1 => $this->__format($this->sampleCentralMoment(1), $returnErrorObject), |
|
396 | - 2 => $this->__format($this->sampleCentralMoment(2), $returnErrorObject), |
|
397 | - 3 => $this->__format($this->sampleCentralMoment(3), $returnErrorObject), |
|
398 | - 4 => $this->__format($this->sampleCentralMoment(4), $returnErrorObject), |
|
399 | - 5 => $this->__format($this->sampleCentralMoment(5), $returnErrorObject) |
|
400 | - ), |
|
401 | - 'sample_raw_moments' => array ( |
|
402 | - 1 => $this->__format($this->sampleRawMoment(1), $returnErrorObject), |
|
403 | - 2 => $this->__format($this->sampleRawMoment(2), $returnErrorObject), |
|
404 | - 3 => $this->__format($this->sampleRawMoment(3), $returnErrorObject), |
|
405 | - 4 => $this->__format($this->sampleRawMoment(4), $returnErrorObject), |
|
406 | - 5 => $this->__format($this->sampleRawMoment(5), $returnErrorObject) |
|
407 | - ), |
|
408 | - 'frequency' => $this->__format($this->frequency(), $returnErrorObject), |
|
409 | - 'quartiles' => $this->__format($this->quartiles(), $returnErrorObject), |
|
410 | - 'interquartile_range' => $this->__format($this->interquartileRange(), $returnErrorObject), |
|
411 | - 'interquartile_mean' => $this->__format($this->interquartileMean(), $returnErrorObject), |
|
412 | - 'quartile_deviation' => $this->__format($this->quartileDeviation(), $returnErrorObject), |
|
413 | - 'quartile_variation_coefficient' => $this->__format($this->quartileVariationCoefficient(), $returnErrorObject), |
|
414 | - 'quartile_skewness_coefficient' => $this->__format($this->quartileSkewnessCoefficient(), $returnErrorObject) |
|
415 | - ); |
|
416 | - }/*}}}*/ |
|
417 | - |
|
418 | - /** |
|
419 | - * Calculates the minimum of a data set. |
|
420 | - * Handles cummulative data sets correctly |
|
421 | - * |
|
422 | - * @access public |
|
423 | - * @return mixed the minimum value on success, a PEAR_Error object otherwise |
|
424 | - * @see calc() |
|
425 | - * @see max() |
|
426 | - */ |
|
427 | - function min() {/*{{{*/ |
|
428 | - if ($this->_data == null) { |
|
429 | - return PEAR::raiseError('data has not been set'); |
|
430 | - } |
|
431 | - if (!array_key_exists('min', $this->_calculatedValues)) { |
|
432 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
433 | - $min = min(array_keys($this->_data)); |
|
434 | - } else { |
|
435 | - $min = min($this->_data); |
|
436 | - } |
|
437 | - $this->_calculatedValues['min'] = $min; |
|
438 | - } |
|
439 | - return $this->_calculatedValues['min']; |
|
440 | - }/*}}}*/ |
|
441 | - |
|
442 | - /** |
|
443 | - * Calculates the maximum of a data set. |
|
444 | - * Handles cummulative data sets correctly |
|
445 | - * |
|
446 | - * @access public |
|
447 | - * @return mixed the maximum value on success, a PEAR_Error object otherwise |
|
448 | - * @see calc() |
|
449 | - * @see min() |
|
450 | - */ |
|
451 | - function max() {/*{{{*/ |
|
452 | - if ($this->_data == null) { |
|
453 | - return PEAR::raiseError('data has not been set'); |
|
454 | - } |
|
455 | - if (!array_key_exists('max', $this->_calculatedValues)) { |
|
456 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
457 | - $max = max(array_keys($this->_data)); |
|
458 | - } else { |
|
459 | - $max = max($this->_data); |
|
460 | - } |
|
461 | - $this->_calculatedValues['max'] = $max; |
|
462 | - } |
|
463 | - return $this->_calculatedValues['max']; |
|
464 | - }/*}}}*/ |
|
465 | - |
|
466 | - /** |
|
467 | - * Calculates SUM { xi } |
|
468 | - * Handles cummulative data sets correctly |
|
469 | - * |
|
470 | - * @access public |
|
471 | - * @return mixed the sum on success, a PEAR_Error object otherwise |
|
472 | - * @see calc() |
|
473 | - * @see sum2() |
|
474 | - * @see sumN() |
|
475 | - */ |
|
476 | - function sum() {/*{{{*/ |
|
477 | - if (!array_key_exists('sum', $this->_calculatedValues)) { |
|
478 | - $sum = $this->sumN(1); |
|
479 | - if (PEAR::isError($sum)) { |
|
480 | - return $sum; |
|
481 | - } else { |
|
482 | - $this->_calculatedValues['sum'] = $sum; |
|
483 | - } |
|
484 | - } |
|
485 | - return $this->_calculatedValues['sum']; |
|
486 | - }/*}}}*/ |
|
487 | - |
|
488 | - /** |
|
489 | - * Calculates SUM { (xi)^2 } |
|
490 | - * Handles cummulative data sets correctly |
|
491 | - * |
|
492 | - * @access public |
|
493 | - * @return mixed the sum on success, a PEAR_Error object otherwise |
|
494 | - * @see calc() |
|
495 | - * @see sum() |
|
496 | - * @see sumN() |
|
497 | - */ |
|
498 | - function sum2() {/*{{{*/ |
|
499 | - if (!array_key_exists('sum2', $this->_calculatedValues)) { |
|
500 | - $sum2 = $this->sumN(2); |
|
501 | - if (PEAR::isError($sum2)) { |
|
502 | - return $sum2; |
|
503 | - } else { |
|
504 | - $this->_calculatedValues['sum2'] = $sum2; |
|
505 | - } |
|
506 | - } |
|
507 | - return $this->_calculatedValues['sum2']; |
|
508 | - }/*}}}*/ |
|
509 | - |
|
510 | - /** |
|
511 | - * Calculates SUM { (xi)^n } |
|
512 | - * Handles cummulative data sets correctly |
|
513 | - * |
|
514 | - * @access public |
|
515 | - * @param numeric $n the exponent |
|
516 | - * @return mixed the sum on success, a PEAR_Error object otherwise |
|
517 | - * @see calc() |
|
518 | - * @see sum() |
|
519 | - * @see sum2() |
|
520 | - */ |
|
521 | - function sumN($n) {/*{{{*/ |
|
522 | - if ($this->_data == null) { |
|
523 | - return PEAR::raiseError('data has not been set'); |
|
524 | - } |
|
525 | - $sumN = 0; |
|
526 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
527 | - foreach($this->_data as $val=>$freq) { |
|
528 | - $sumN += $freq * pow((double)$val, (double)$n); |
|
529 | - } |
|
530 | - } else { |
|
531 | - foreach($this->_data as $val) { |
|
532 | - $sumN += pow((double)$val, (double)$n); |
|
533 | - } |
|
534 | - } |
|
535 | - return $sumN; |
|
536 | - }/*}}}*/ |
|
537 | - |
|
538 | - /** |
|
539 | - * Calculates PROD { (xi) }, (the product of all observations) |
|
540 | - * Handles cummulative data sets correctly |
|
541 | - * |
|
542 | - * @access public |
|
543 | - * @return mixed the product on success, a PEAR_Error object otherwise |
|
544 | - * @see productN() |
|
545 | - */ |
|
546 | - function product() {/*{{{*/ |
|
547 | - if (!array_key_exists('product', $this->_calculatedValues)) { |
|
548 | - $product = $this->productN(1); |
|
549 | - if (PEAR::isError($product)) { |
|
550 | - return $product; |
|
551 | - } else { |
|
552 | - $this->_calculatedValues['product'] = $product; |
|
553 | - } |
|
554 | - } |
|
555 | - return $this->_calculatedValues['product']; |
|
556 | - }/*}}}*/ |
|
557 | - |
|
558 | - /** |
|
559 | - * Calculates PROD { (xi)^n }, which is the product of all observations |
|
560 | - * Handles cummulative data sets correctly |
|
561 | - * |
|
562 | - * @access public |
|
563 | - * @param numeric $n the exponent |
|
564 | - * @return mixed the product on success, a PEAR_Error object otherwise |
|
565 | - * @see product() |
|
566 | - */ |
|
567 | - function productN($n) {/*{{{*/ |
|
568 | - if ($this->_data == null) { |
|
569 | - return PEAR::raiseError('data has not been set'); |
|
570 | - } |
|
571 | - $prodN = 1.0; |
|
572 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
573 | - foreach($this->_data as $val=>$freq) { |
|
574 | - if ($val == 0) { |
|
575 | - return 0.0; |
|
576 | - } |
|
577 | - $prodN *= $freq * pow((double)$val, (double)$n); |
|
578 | - } |
|
579 | - } else { |
|
580 | - foreach($this->_data as $val) { |
|
581 | - if ($val == 0) { |
|
582 | - return 0.0; |
|
583 | - } |
|
584 | - $prodN *= pow((double)$val, (double)$n); |
|
585 | - } |
|
586 | - } |
|
587 | - return $prodN; |
|
588 | - |
|
589 | - }/*}}}*/ |
|
590 | - |
|
591 | - /** |
|
592 | - * Calculates the number of data points in the set |
|
593 | - * Handles cummulative data sets correctly |
|
594 | - * |
|
595 | - * @access public |
|
596 | - * @return mixed the count on success, a PEAR_Error object otherwise |
|
597 | - * @see calc() |
|
598 | - */ |
|
599 | - function count() {/*{{{*/ |
|
600 | - if ($this->_data == null) { |
|
601 | - return PEAR::raiseError('data has not been set'); |
|
602 | - } |
|
603 | - if (!array_key_exists('count', $this->_calculatedValues)) { |
|
604 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
605 | - $count = count($this->_dataExpanded); |
|
606 | - } else { |
|
607 | - $count = count($this->_data); |
|
608 | - } |
|
609 | - $this->_calculatedValues['count'] = $count; |
|
610 | - } |
|
611 | - return $this->_calculatedValues['count']; |
|
612 | - }/*}}}*/ |
|
613 | - |
|
614 | - /** |
|
615 | - * Calculates the mean (average) of the data points in the set |
|
616 | - * Handles cummulative data sets correctly |
|
617 | - * |
|
618 | - * @access public |
|
619 | - * @return mixed the mean value on success, a PEAR_Error object otherwise |
|
620 | - * @see calc() |
|
621 | - * @see sum() |
|
622 | - * @see count() |
|
623 | - */ |
|
624 | - function mean() {/*{{{*/ |
|
625 | - if (!array_key_exists('mean', $this->_calculatedValues)) { |
|
626 | - $sum = $this->sum(); |
|
627 | - if (PEAR::isError($sum)) { |
|
628 | - return $sum; |
|
629 | - } |
|
630 | - $count = $this->count(); |
|
631 | - if (PEAR::isError($count)) { |
|
632 | - return $count; |
|
633 | - } |
|
634 | - $this->_calculatedValues['mean'] = $sum / $count; |
|
635 | - } |
|
636 | - return $this->_calculatedValues['mean']; |
|
637 | - }/*}}}*/ |
|
638 | - |
|
639 | - /** |
|
640 | - * Calculates the range of the data set = max - min |
|
641 | - * |
|
642 | - * @access public |
|
643 | - * @return mixed the value of the range on success, a PEAR_Error object otherwise. |
|
644 | - */ |
|
645 | - function range() {/*{{{*/ |
|
646 | - if (!array_key_exists('range', $this->_calculatedValues)) { |
|
647 | - $min = $this->min(); |
|
648 | - if (PEAR::isError($min)) { |
|
649 | - return $min; |
|
650 | - } |
|
651 | - $max = $this->max(); |
|
652 | - if (PEAR::isError($max)) { |
|
653 | - return $max; |
|
654 | - } |
|
655 | - $this->_calculatedValues['range'] = $max - $min; |
|
656 | - } |
|
657 | - return $this->_calculatedValues['range']; |
|
658 | - |
|
659 | - }/*}}}*/ |
|
660 | - |
|
661 | - /** |
|
662 | - * Calculates the variance (unbiased) of the data points in the set |
|
663 | - * Handles cummulative data sets correctly |
|
664 | - * |
|
665 | - * @access public |
|
666 | - * @return mixed the variance value on success, a PEAR_Error object otherwise |
|
667 | - * @see calc() |
|
668 | - * @see __sumdiff() |
|
669 | - * @see count() |
|
670 | - */ |
|
671 | - function variance() {/*{{{*/ |
|
672 | - if (!array_key_exists('variance', $this->_calculatedValues)) { |
|
673 | - $variance = $this->__calcVariance(); |
|
674 | - if (PEAR::isError($variance)) { |
|
675 | - return $variance; |
|
676 | - } |
|
677 | - $this->_calculatedValues['variance'] = $variance; |
|
678 | - } |
|
679 | - return $this->_calculatedValues['variance']; |
|
680 | - }/*}}}*/ |
|
681 | - |
|
682 | - /** |
|
683 | - * Calculates the standard deviation (unbiased) of the data points in the set |
|
684 | - * Handles cummulative data sets correctly |
|
685 | - * |
|
686 | - * @access public |
|
687 | - * @return mixed the standard deviation on success, a PEAR_Error object otherwise |
|
688 | - * @see calc() |
|
689 | - * @see variance() |
|
690 | - */ |
|
691 | - function stDev() {/*{{{*/ |
|
692 | - if (!array_key_exists('stDev', $this->_calculatedValues)) { |
|
693 | - $variance = $this->variance(); |
|
694 | - if (PEAR::isError($variance)) { |
|
695 | - return $variance; |
|
696 | - } |
|
697 | - $this->_calculatedValues['stDev'] = sqrt($variance); |
|
698 | - } |
|
699 | - return $this->_calculatedValues['stDev']; |
|
700 | - }/*}}}*/ |
|
701 | - |
|
702 | - /** |
|
703 | - * Calculates the variance (unbiased) of the data points in the set |
|
704 | - * given a fixed mean (average) value. Not used in calcBasic(), calcFull() |
|
705 | - * or calc(). |
|
706 | - * Handles cummulative data sets correctly |
|
707 | - * |
|
708 | - * @access public |
|
709 | - * @param numeric $mean the fixed mean value |
|
710 | - * @return mixed the variance on success, a PEAR_Error object otherwise |
|
711 | - * @see __sumdiff() |
|
712 | - * @see count() |
|
713 | - * @see variance() |
|
714 | - */ |
|
715 | - function varianceWithMean($mean) {/*{{{*/ |
|
716 | - return $this->__calcVariance($mean); |
|
717 | - }/*}}}*/ |
|
718 | - |
|
719 | - /** |
|
720 | - * Calculates the standard deviation (unbiased) of the data points in the set |
|
721 | - * given a fixed mean (average) value. Not used in calcBasic(), calcFull() |
|
722 | - * or calc(). |
|
723 | - * Handles cummulative data sets correctly |
|
724 | - * |
|
725 | - * @access public |
|
726 | - * @param numeric $mean the fixed mean value |
|
727 | - * @return mixed the standard deviation on success, a PEAR_Error object otherwise |
|
728 | - * @see varianceWithMean() |
|
729 | - * @see stDev() |
|
730 | - */ |
|
731 | - function stDevWithMean($mean) {/*{{{*/ |
|
732 | - $varianceWM = $this->varianceWithMean($mean); |
|
733 | - if (PEAR::isError($varianceWM)) { |
|
734 | - return $varianceWM; |
|
735 | - } |
|
736 | - return sqrt($varianceWM); |
|
737 | - }/*}}}*/ |
|
738 | - |
|
739 | - /** |
|
740 | - * Calculates the absolute deviation of the data points in the set |
|
741 | - * Handles cummulative data sets correctly |
|
742 | - * |
|
743 | - * @access public |
|
744 | - * @return mixed the absolute deviation on success, a PEAR_Error object otherwise |
|
745 | - * @see calc() |
|
746 | - * @see __sumabsdev() |
|
747 | - * @see count() |
|
748 | - * @see absDevWithMean() |
|
749 | - */ |
|
750 | - function absDev() {/*{{{*/ |
|
751 | - if (!array_key_exists('absDev', $this->_calculatedValues)) { |
|
752 | - $absDev = $this->__calcAbsoluteDeviation(); |
|
753 | - if (PEAR::isError($absdev)) { |
|
754 | - return $absdev; |
|
755 | - } |
|
756 | - $this->_calculatedValues['absDev'] = $absDev; |
|
757 | - } |
|
758 | - return $this->_calculatedValues['absDev']; |
|
759 | - }/*}}}*/ |
|
760 | - |
|
761 | - /** |
|
762 | - * Calculates the absolute deviation of the data points in the set |
|
763 | - * given a fixed mean (average) value. Not used in calcBasic(), calcFull() |
|
764 | - * or calc(). |
|
765 | - * Handles cummulative data sets correctly |
|
766 | - * |
|
767 | - * @access public |
|
768 | - * @param numeric $mean the fixed mean value |
|
769 | - * @return mixed the absolute deviation on success, a PEAR_Error object otherwise |
|
770 | - * @see __sumabsdev() |
|
771 | - * @see absDev() |
|
772 | - */ |
|
773 | - function absDevWithMean($mean) {/*{{{*/ |
|
774 | - return $this->__calcAbsoluteDeviation($mean); |
|
775 | - }/*}}}*/ |
|
776 | - |
|
777 | - /** |
|
778 | - * Calculates the skewness of the data distribution in the set |
|
779 | - * The skewness measures the degree of asymmetry of a distribution, |
|
780 | - * and is related to the third central moment of a distribution. |
|
781 | - * A normal distribution has a skewness = 0 |
|
782 | - * A distribution with a tail off towards the high end of the scale |
|
783 | - * (positive skew) has a skewness > 0 |
|
784 | - * A distribution with a tail off towards the low end of the scale |
|
785 | - * (negative skew) has a skewness < 0 |
|
786 | - * Handles cummulative data sets correctly |
|
787 | - * |
|
788 | - * @access public |
|
789 | - * @return mixed the skewness value on success, a PEAR_Error object otherwise |
|
790 | - * @see __sumdiff() |
|
791 | - * @see count() |
|
792 | - * @see stDev() |
|
793 | - * @see calc() |
|
794 | - */ |
|
795 | - function skewness() {/*{{{*/ |
|
796 | - if (!array_key_exists('skewness', $this->_calculatedValues)) { |
|
797 | - $count = $this->count(); |
|
798 | - if (PEAR::isError($count)) { |
|
799 | - return $count; |
|
800 | - } |
|
801 | - $stDev = $this->stDev(); |
|
802 | - if (PEAR::isError($stDev)) { |
|
803 | - return $stDev; |
|
804 | - } |
|
805 | - $sumdiff3 = $this->__sumdiff(3); |
|
806 | - if (PEAR::isError($sumdiff3)) { |
|
807 | - return $sumdiff3; |
|
808 | - } |
|
809 | - $this->_calculatedValues['skewness'] = ($sumdiff3 / ($count * pow($stDev, 3))); |
|
810 | - } |
|
811 | - return $this->_calculatedValues['skewness']; |
|
812 | - }/*}}}*/ |
|
813 | - |
|
814 | - /** |
|
815 | - * Calculates the kurtosis of the data distribution in the set |
|
816 | - * The kurtosis measures the degrees of peakedness of a distribution. |
|
817 | - * It is also called the "excess" or "excess coefficient", and is |
|
818 | - * a normalized form of the fourth central moment of a distribution. |
|
819 | - * A normal distributions has kurtosis = 0 |
|
820 | - * A narrow and peaked (leptokurtic) distribution has a |
|
821 | - * kurtosis > 0 |
|
822 | - * A flat and wide (platykurtic) distribution has a kurtosis < 0 |
|
823 | - * Handles cummulative data sets correctly |
|
824 | - * |
|
825 | - * @access public |
|
826 | - * @return mixed the kurtosis value on success, a PEAR_Error object otherwise |
|
827 | - * @see __sumdiff() |
|
828 | - * @see count() |
|
829 | - * @see stDev() |
|
830 | - * @see calc() |
|
831 | - */ |
|
832 | - function kurtosis() {/*{{{*/ |
|
833 | - if (!array_key_exists('kurtosis', $this->_calculatedValues)) { |
|
834 | - $count = $this->count(); |
|
835 | - if (PEAR::isError($count)) { |
|
836 | - return $count; |
|
837 | - } |
|
838 | - $stDev = $this->stDev(); |
|
839 | - if (PEAR::isError($stDev)) { |
|
840 | - return $stDev; |
|
841 | - } |
|
842 | - $sumdiff4 = $this->__sumdiff(4); |
|
843 | - if (PEAR::isError($sumdiff4)) { |
|
844 | - return $sumdiff4; |
|
845 | - } |
|
846 | - $this->_calculatedValues['kurtosis'] = ($sumdiff4 / ($count * pow($stDev, 4))) - 3; |
|
847 | - } |
|
848 | - return $this->_calculatedValues['kurtosis']; |
|
849 | - }/*}}}*/ |
|
850 | - |
|
851 | - /** |
|
852 | - * Calculates the median of a data set. |
|
853 | - * The median is the value such that half of the points are below it |
|
854 | - * in a sorted data set. |
|
855 | - * If the number of values is odd, it is the middle item. |
|
856 | - * If the number of values is even, is the average of the two middle items. |
|
857 | - * Handles cummulative data sets correctly |
|
858 | - * |
|
859 | - * @access public |
|
860 | - * @return mixed the median value on success, a PEAR_Error object otherwise |
|
861 | - * @see count() |
|
862 | - * @see calc() |
|
863 | - */ |
|
864 | - function median() {/*{{{*/ |
|
865 | - if ($this->_data == null) { |
|
866 | - return PEAR::raiseError('data has not been set'); |
|
867 | - } |
|
868 | - if (!array_key_exists('median', $this->_calculatedValues)) { |
|
869 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
870 | - $arr =& $this->_dataExpanded; |
|
871 | - } else { |
|
872 | - $arr =& $this->_data; |
|
873 | - } |
|
874 | - $n = $this->count(); |
|
875 | - if (PEAR::isError($n)) { |
|
876 | - return $n; |
|
877 | - } |
|
878 | - $h = intval($n / 2); |
|
879 | - if ($n % 2 == 0) { |
|
880 | - $median = ($arr[$h] + $arr[$h - 1]) / 2; |
|
881 | - } else { |
|
882 | - $median = $arr[$h + 1]; |
|
883 | - } |
|
884 | - $this->_calculatedValues['median'] = $median; |
|
885 | - } |
|
886 | - return $this->_calculatedValues['median']; |
|
887 | - }/*}}}*/ |
|
888 | - |
|
889 | - /** |
|
890 | - * Calculates the mode of a data set. |
|
891 | - * The mode is the value with the highest frequency in the data set. |
|
892 | - * There can be more than one mode. |
|
893 | - * Handles cummulative data sets correctly |
|
894 | - * |
|
895 | - * @access public |
|
896 | - * @return mixed an array of mode value on success, a PEAR_Error object otherwise |
|
897 | - * @see frequency() |
|
898 | - * @see calc() |
|
899 | - */ |
|
900 | - function mode() {/*{{{*/ |
|
901 | - if ($this->_data == null) { |
|
902 | - return PEAR::raiseError('data has not been set'); |
|
903 | - } |
|
904 | - if (!array_key_exists('mode', $this->_calculatedValues)) { |
|
905 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
906 | - $arr = $this->_data; |
|
907 | - } else { |
|
908 | - $arr = $this->frequency(); |
|
909 | - } |
|
910 | - arsort($arr); |
|
911 | - $mcount = 1; |
|
912 | - foreach ($arr as $val=>$freq) { |
|
913 | - if ($mcount == 1) { |
|
914 | - $mode = array($val); |
|
915 | - $mfreq = $freq; |
|
916 | - ++$mcount; |
|
917 | - continue; |
|
918 | - } |
|
919 | - if ($mfreq == $freq) |
|
920 | - $mode[] = $val; |
|
921 | - if ($mfreq > $freq) |
|
922 | - break; |
|
923 | - } |
|
924 | - $this->_calculatedValues['mode'] = $mode; |
|
925 | - } |
|
926 | - return $this->_calculatedValues['mode']; |
|
927 | - }/*}}}*/ |
|
928 | - |
|
929 | - /** |
|
930 | - * Calculates the midrange of a data set. |
|
931 | - * The midrange is the average of the minimum and maximum of the data set. |
|
932 | - * Handles cummulative data sets correctly |
|
933 | - * |
|
934 | - * @access public |
|
935 | - * @return mixed the midrange value on success, a PEAR_Error object otherwise |
|
936 | - * @see min() |
|
937 | - * @see max() |
|
938 | - * @see calc() |
|
939 | - */ |
|
940 | - function midrange() {/*{{{*/ |
|
941 | - if (!array_key_exists('midrange', $this->_calculatedValues)) { |
|
942 | - $min = $this->min(); |
|
943 | - if (PEAR::isError($min)) { |
|
944 | - return $min; |
|
945 | - } |
|
946 | - $max = $this->max(); |
|
947 | - if (PEAR::isError($max)) { |
|
948 | - return $max; |
|
949 | - } |
|
950 | - $this->_calculatedValues['midrange'] = (($max + $min) / 2); |
|
951 | - } |
|
952 | - return $this->_calculatedValues['midrange']; |
|
953 | - }/*}}}*/ |
|
954 | - |
|
955 | - /** |
|
956 | - * Calculates the geometrical mean of the data points in the set |
|
957 | - * Handles cummulative data sets correctly |
|
958 | - * |
|
959 | - * @access public |
|
960 | - * @return mixed the geometrical mean value on success, a PEAR_Error object otherwise |
|
961 | - * @see calc() |
|
962 | - * @see product() |
|
963 | - * @see count() |
|
964 | - */ |
|
965 | - function geometricMean() {/*{{{*/ |
|
966 | - if (!array_key_exists('geometricMean', $this->_calculatedValues)) { |
|
967 | - $count = $this->count(); |
|
968 | - if (PEAR::isError($count)) { |
|
969 | - return $count; |
|
970 | - } |
|
971 | - $prod = $this->product(); |
|
972 | - if (PEAR::isError($prod)) { |
|
973 | - return $prod; |
|
974 | - } |
|
975 | - if ($prod == 0.0) { |
|
976 | - return 0.0; |
|
977 | - } |
|
978 | - if ($prod < 0) { |
|
979 | - return PEAR::raiseError('The product of the data set is negative, geometric mean undefined.'); |
|
980 | - } |
|
981 | - $this->_calculatedValues['geometricMean'] = pow($prod , 1 / $count); |
|
982 | - } |
|
983 | - return $this->_calculatedValues['geometricMean']; |
|
984 | - }/*}}}*/ |
|
985 | - |
|
986 | - /** |
|
987 | - * Calculates the harmonic mean of the data points in the set |
|
988 | - * Handles cummulative data sets correctly |
|
989 | - * |
|
990 | - * @access public |
|
991 | - * @return mixed the harmonic mean value on success, a PEAR_Error object otherwise |
|
992 | - * @see calc() |
|
993 | - * @see count() |
|
994 | - */ |
|
995 | - function harmonicMean() {/*{{{*/ |
|
996 | - if ($this->_data == null) { |
|
997 | - return PEAR::raiseError('data has not been set'); |
|
998 | - } |
|
999 | - if (!array_key_exists('harmonicMean', $this->_calculatedValues)) { |
|
1000 | - $count = $this->count(); |
|
1001 | - if (PEAR::isError($count)) { |
|
1002 | - return $count; |
|
1003 | - } |
|
1004 | - $invsum = 0.0; |
|
1005 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1006 | - foreach($this->_data as $val=>$freq) { |
|
1007 | - if ($val == 0) { |
|
1008 | - return PEAR::raiseError('cannot calculate a '. |
|
1009 | - 'harmonic mean with data values of zero.'); |
|
1010 | - } |
|
1011 | - $invsum += $freq / $val; |
|
1012 | - } |
|
1013 | - } else { |
|
1014 | - foreach($this->_data as $val) { |
|
1015 | - if ($val == 0) { |
|
1016 | - return PEAR::raiseError('cannot calculate a '. |
|
1017 | - 'harmonic mean with data values of zero.'); |
|
1018 | - } |
|
1019 | - $invsum += 1 / $val; |
|
1020 | - } |
|
1021 | - } |
|
1022 | - $this->_calculatedValues['harmonicMean'] = $count / $invsum; |
|
1023 | - } |
|
1024 | - return $this->_calculatedValues['harmonicMean']; |
|
1025 | - }/*}}}*/ |
|
1026 | - |
|
1027 | - /** |
|
1028 | - * Calculates the nth central moment (m{n}) of a data set. |
|
1029 | - * |
|
1030 | - * The definition of a sample central moment is: |
|
1031 | - * |
|
1032 | - * m{n} = 1/N * SUM { (xi - avg)^n } |
|
1033 | - * |
|
1034 | - * where: N = sample size, avg = sample mean. |
|
1035 | - * |
|
1036 | - * @access public |
|
1037 | - * @param integer $n moment to calculate |
|
1038 | - * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
|
1039 | - */ |
|
1040 | - function sampleCentralMoment($n) {/*{{{*/ |
|
1041 | - if (!is_int($n) || $n < 1) { |
|
1042 | - return PEAR::isError('moment must be a positive integer >= 1.'); |
|
1043 | - } |
|
1044 | - |
|
1045 | - if ($n == 1) { |
|
1046 | - return 0; |
|
1047 | - } |
|
1048 | - $count = $this->count(); |
|
1049 | - if (PEAR::isError($count)) { |
|
1050 | - return $count; |
|
1051 | - } |
|
1052 | - if ($count == 0) { |
|
1053 | - return PEAR::raiseError("Cannot calculate {$n}th sample moment, ". |
|
1054 | - 'there are zero data entries'); |
|
1055 | - } |
|
1056 | - $sum = $this->__sumdiff($n); |
|
1057 | - if (PEAR::isError($sum)) { |
|
1058 | - return $sum; |
|
1059 | - } |
|
1060 | - return ($sum / $count); |
|
1061 | - }/*}}}*/ |
|
1062 | - |
|
1063 | - /** |
|
1064 | - * Calculates the nth raw moment (m{n}) of a data set. |
|
1065 | - * |
|
1066 | - * The definition of a sample central moment is: |
|
1067 | - * |
|
1068 | - * m{n} = 1/N * SUM { xi^n } |
|
1069 | - * |
|
1070 | - * where: N = sample size, avg = sample mean. |
|
1071 | - * |
|
1072 | - * @access public |
|
1073 | - * @param integer $n moment to calculate |
|
1074 | - * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
|
1075 | - */ |
|
1076 | - function sampleRawMoment($n) {/*{{{*/ |
|
1077 | - if (!is_int($n) || $n < 1) { |
|
1078 | - return PEAR::isError('moment must be a positive integer >= 1.'); |
|
1079 | - } |
|
1080 | - |
|
1081 | - $count = $this->count(); |
|
1082 | - if (PEAR::isError($count)) { |
|
1083 | - return $count; |
|
1084 | - } |
|
1085 | - if ($count == 0) { |
|
1086 | - return PEAR::raiseError("Cannot calculate {$n}th raw moment, ". |
|
1087 | - 'there are zero data entries.'); |
|
1088 | - } |
|
1089 | - $sum = $this->sumN($n); |
|
1090 | - if (PEAR::isError($sum)) { |
|
1091 | - return $sum; |
|
1092 | - } |
|
1093 | - return ($sum / $count); |
|
1094 | - }/*}}}*/ |
|
1095 | - |
|
1096 | - |
|
1097 | - /** |
|
1098 | - * Calculates the coefficient of variation of a data set. |
|
1099 | - * The coefficient of variation measures the spread of a set of data |
|
1100 | - * as a proportion of its mean. It is often expressed as a percentage. |
|
1101 | - * Handles cummulative data sets correctly |
|
1102 | - * |
|
1103 | - * @access public |
|
1104 | - * @return mixed the coefficient of variation on success, a PEAR_Error object otherwise |
|
1105 | - * @see stDev() |
|
1106 | - * @see mean() |
|
1107 | - * @see calc() |
|
1108 | - */ |
|
1109 | - function coeffOfVariation() {/*{{{*/ |
|
1110 | - if (!array_key_exists('coeffOfVariation', $this->_calculatedValues)) { |
|
1111 | - $mean = $this->mean(); |
|
1112 | - if (PEAR::isError($mean)) { |
|
1113 | - return $mean; |
|
1114 | - } |
|
1115 | - if ($mean == 0.0) { |
|
1116 | - return PEAR::raiseError('cannot calculate the coefficient '. |
|
1117 | - 'of variation, mean of sample is zero'); |
|
1118 | - } |
|
1119 | - $stDev = $this->stDev(); |
|
1120 | - if (PEAR::isError($stDev)) { |
|
1121 | - return $stDev; |
|
1122 | - } |
|
1123 | - |
|
1124 | - $this->_calculatedValues['coeffOfVariation'] = $stDev / $mean; |
|
1125 | - } |
|
1126 | - return $this->_calculatedValues['coeffOfVariation']; |
|
1127 | - }/*}}}*/ |
|
1128 | - |
|
1129 | - /** |
|
1130 | - * Calculates the standard error of the mean. |
|
1131 | - * It is the standard deviation of the sampling distribution of |
|
1132 | - * the mean. The formula is: |
|
1133 | - * |
|
1134 | - * S.E. Mean = SD / (N)^(1/2) |
|
1135 | - * |
|
1136 | - * This formula does not assume a normal distribution, and shows |
|
1137 | - * that the size of the standard error of the mean is inversely |
|
1138 | - * proportional to the square root of the sample size. |
|
1139 | - * |
|
1140 | - * @access public |
|
1141 | - * @return mixed the standard error of the mean on success, a PEAR_Error object otherwise |
|
1142 | - * @see stDev() |
|
1143 | - * @see count() |
|
1144 | - * @see calc() |
|
1145 | - */ |
|
1146 | - function stdErrorOfMean() {/*{{{*/ |
|
1147 | - if (!array_key_exists('stdErrorOfMean', $this->_calculatedValues)) { |
|
1148 | - $count = $this->count(); |
|
1149 | - if (PEAR::isError($count)) { |
|
1150 | - return $count; |
|
1151 | - } |
|
1152 | - $stDev = $this->stDev(); |
|
1153 | - if (PEAR::isError($stDev)) { |
|
1154 | - return $stDev; |
|
1155 | - } |
|
1156 | - $this->_calculatedValues['stdErrorOfMean'] = $stDev / sqrt($count); |
|
1157 | - } |
|
1158 | - return $this->_calculatedValues['stdErrorOfMean']; |
|
1159 | - }/*}}}*/ |
|
1160 | - |
|
1161 | - /** |
|
1162 | - * Calculates the value frequency table of a data set. |
|
1163 | - * Handles cummulative data sets correctly |
|
1164 | - * |
|
1165 | - * @access public |
|
1166 | - * @return mixed an associative array of value=>frequency items on success, a PEAR_Error object otherwise |
|
1167 | - * @see min() |
|
1168 | - * @see max() |
|
1169 | - * @see calc() |
|
1170 | - */ |
|
1171 | - function frequency() {/*{{{*/ |
|
1172 | - if ($this->_data == null) { |
|
1173 | - return PEAR::raiseError('data has not been set'); |
|
1174 | - } |
|
1175 | - if (!array_key_exists('frequency', $this->_calculatedValues)) { |
|
1176 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1177 | - $freq = $this->_data; |
|
1178 | - } else { |
|
1179 | - $freq = array(); |
|
1180 | - foreach ($this->_data as $val) { |
|
1181 | - $freq["$val"]++; |
|
1182 | - } |
|
1183 | - ksort($freq); |
|
1184 | - } |
|
1185 | - $this->_calculatedValues['frequency'] = $freq; |
|
1186 | - } |
|
1187 | - return $this->_calculatedValues['frequency']; |
|
1188 | - }/*}}}*/ |
|
1189 | - |
|
1190 | - /** |
|
1191 | - * The quartiles are defined as the values that divide a sorted |
|
1192 | - * data set into four equal-sized subsets, and correspond to the |
|
1193 | - * 25th, 50th, and 75th percentiles. |
|
1194 | - * |
|
1195 | - * @access public |
|
1196 | - * @return mixed an associative array of quartiles on success, a PEAR_Error otherwise |
|
1197 | - * @see percentile() |
|
1198 | - */ |
|
1199 | - function quartiles() {/*{{{*/ |
|
1200 | - if (!array_key_exists('quartiles', $this->_calculatedValues)) { |
|
1201 | - $q1 = $this->percentile(25); |
|
1202 | - if (PEAR::isError($q1)) { |
|
1203 | - return $q1; |
|
1204 | - } |
|
1205 | - $q2 = $this->percentile(50); |
|
1206 | - if (PEAR::isError($q2)) { |
|
1207 | - return $q2; |
|
1208 | - } |
|
1209 | - $q3 = $this->percentile(75); |
|
1210 | - if (PEAR::isError($q3)) { |
|
1211 | - return $q3; |
|
1212 | - } |
|
1213 | - $this->_calculatedValues['quartiles'] = array ( |
|
1214 | - '25' => $q1, |
|
1215 | - '50' => $q2, |
|
1216 | - '75' => $q3 |
|
1217 | - ); |
|
1218 | - } |
|
1219 | - return $this->_calculatedValues['quartiles']; |
|
1220 | - }/*}}}*/ |
|
1221 | - |
|
1222 | - /** |
|
1223 | - * The interquartile mean is defined as the mean of the values left |
|
1224 | - * after discarding the lower 25% and top 25% ranked values, i.e.: |
|
1225 | - * |
|
1226 | - * interquart mean = mean(<P(25),P(75)>) |
|
1227 | - * |
|
1228 | - * where: P = percentile |
|
1229 | - * |
|
1230 | - * @todo need to double check the equation |
|
1231 | - * @access public |
|
1232 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1233 | - * @see quartiles() |
|
1234 | - */ |
|
1235 | - function interquartileMean() {/*{{{*/ |
|
1236 | - if (!array_key_exists('interquartileMean', $this->_calculatedValues)) { |
|
1237 | - $quart = $this->quartiles(); |
|
1238 | - if (PEAR::isError($quart)) { |
|
1239 | - return $quart; |
|
1240 | - } |
|
1241 | - $q3 = $quart['75']; |
|
1242 | - $q1 = $quart['25']; |
|
1243 | - $sum = 0; |
|
1244 | - $n = 0; |
|
1245 | - foreach ($this->getData(true) as $val) { |
|
1246 | - if ($val >= $q1 && $val <= $q3) { |
|
1247 | - $sum += $val; |
|
1248 | - ++$n; |
|
1249 | - } |
|
1250 | - } |
|
1251 | - if ($n == 0) { |
|
1252 | - return PEAR::raiseError('error calculating interquartile mean, '. |
|
1253 | - 'empty interquartile range of values.'); |
|
1254 | - } |
|
1255 | - $this->_calculatedValues['interquartileMean'] = $sum / $n; |
|
1256 | - } |
|
1257 | - return $this->_calculatedValues['interquartileMean']; |
|
1258 | - }/*}}}*/ |
|
1259 | - |
|
1260 | - /** |
|
1261 | - * The interquartile range is the distance between the 75th and 25th |
|
1262 | - * percentiles. Basically the range of the middle 50% of the data set, |
|
1263 | - * and thus is not affected by outliers or extreme values. |
|
1264 | - * |
|
1265 | - * interquart range = P(75) - P(25) |
|
1266 | - * |
|
1267 | - * where: P = percentile |
|
1268 | - * |
|
1269 | - * @access public |
|
1270 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1271 | - * @see quartiles() |
|
1272 | - */ |
|
1273 | - function interquartileRange() {/*{{{*/ |
|
1274 | - if (!array_key_exists('interquartileRange', $this->_calculatedValues)) { |
|
1275 | - $quart = $this->quartiles(); |
|
1276 | - if (PEAR::isError($quart)) { |
|
1277 | - return $quart; |
|
1278 | - } |
|
1279 | - $q3 = $quart['75']; |
|
1280 | - $q1 = $quart['25']; |
|
1281 | - $this->_calculatedValues['interquartileRange'] = $q3 - $q1; |
|
1282 | - } |
|
1283 | - return $this->_calculatedValues['interquartileRange']; |
|
1284 | - }/*}}}*/ |
|
1285 | - |
|
1286 | - /** |
|
1287 | - * The quartile deviation is half of the interquartile range value |
|
1288 | - * |
|
1289 | - * quart dev = (P(75) - P(25)) / 2 |
|
1290 | - * |
|
1291 | - * where: P = percentile |
|
1292 | - * |
|
1293 | - * @access public |
|
1294 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1295 | - * @see quartiles() |
|
1296 | - * @see interquartileRange() |
|
1297 | - */ |
|
1298 | - function quartileDeviation() {/*{{{*/ |
|
1299 | - if (!array_key_exists('quartileDeviation', $this->_calculatedValues)) { |
|
1300 | - $iqr = $this->interquartileRange(); |
|
1301 | - if (PEAR::isError($iqr)) { |
|
1302 | - return $iqr; |
|
1303 | - } |
|
1304 | - $this->_calculatedValues['quartileDeviation'] = $iqr / 2; |
|
1305 | - } |
|
1306 | - return $this->_calculatedValues['quartileDeviation']; |
|
1307 | - }/*}}}*/ |
|
1308 | - |
|
1309 | - /** |
|
1310 | - * The quartile variation coefficient is defines as follows: |
|
1311 | - * |
|
1312 | - * quart var coeff = 100 * (P(75) - P(25)) / (P(75) + P(25)) |
|
1313 | - * |
|
1314 | - * where: P = percentile |
|
1315 | - * |
|
1316 | - * @todo need to double check the equation |
|
1317 | - * @access public |
|
1318 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1319 | - * @see quartiles() |
|
1320 | - */ |
|
1321 | - function quartileVariationCoefficient() {/*{{{*/ |
|
1322 | - if (!array_key_exists('quartileVariationCoefficient', $this->_calculatedValues)) { |
|
1323 | - $quart = $this->quartiles(); |
|
1324 | - if (PEAR::isError($quart)) { |
|
1325 | - return $quart; |
|
1326 | - } |
|
1327 | - $q3 = $quart['75']; |
|
1328 | - $q1 = $quart['25']; |
|
1329 | - $d = $q3 - $q1; |
|
1330 | - $s = $q3 + $q1; |
|
1331 | - $this->_calculatedValues['quartileVariationCoefficient'] = 100 * $d / $s; |
|
1332 | - } |
|
1333 | - return $this->_calculatedValues['quartileVariationCoefficient']; |
|
1334 | - }/*}}}*/ |
|
1335 | - |
|
1336 | - /** |
|
1337 | - * The quartile skewness coefficient (also known as Bowley Skewness), |
|
1338 | - * is defined as follows: |
|
1339 | - * |
|
1340 | - * quart skewness coeff = (P(25) - 2*P(50) + P(75)) / (P(75) - P(25)) |
|
1341 | - * |
|
1342 | - * where: P = percentile |
|
1343 | - * |
|
1344 | - * @todo need to double check the equation |
|
1345 | - * @access public |
|
1346 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1347 | - * @see quartiles() |
|
1348 | - */ |
|
1349 | - function quartileSkewnessCoefficient() {/*{{{*/ |
|
1350 | - if (!array_key_exists('quartileSkewnessCoefficient', $this->_calculatedValues)) { |
|
1351 | - $quart = $this->quartiles(); |
|
1352 | - if (PEAR::isError($quart)) { |
|
1353 | - return $quart; |
|
1354 | - } |
|
1355 | - $q3 = $quart['75']; |
|
1356 | - $q2 = $quart['50']; |
|
1357 | - $q1 = $quart['25']; |
|
1358 | - $d = $q3 - 2*$q2 + $q1; |
|
1359 | - $s = $q3 - $q1; |
|
1360 | - $this->_calculatedValues['quartileSkewnessCoefficient'] = $d / $s; |
|
1361 | - } |
|
1362 | - return $this->_calculatedValues['quartileSkewnessCoefficient']; |
|
1363 | - }/*}}}*/ |
|
1364 | - |
|
1365 | - /** |
|
1366 | - * The pth percentile is the value such that p% of the a sorted data set |
|
1367 | - * is smaller than it, and (100 - p)% of the data is larger. |
|
1368 | - * |
|
1369 | - * A quick algorithm to pick the appropriate value from a sorted data |
|
1370 | - * set is as follows: |
|
1371 | - * |
|
1372 | - * - Count the number of values: n |
|
1373 | - * - Calculate the position of the value in the data list: i = p * (n + 1) |
|
1374 | - * - if i is an integer, return the data at that position |
|
1375 | - * - if i < 1, return the minimum of the data set |
|
1376 | - * - if i > n, return the maximum of the data set |
|
1377 | - * - otherwise, average the entries at adjacent positions to i |
|
1378 | - * |
|
1379 | - * The median is the 50th percentile value. |
|
1380 | - * |
|
1381 | - * @todo need to double check generality of the algorithm |
|
1382 | - * |
|
1383 | - * @access public |
|
1384 | - * @param numeric $p the percentile to estimate, e.g. 25 for 25th percentile |
|
1385 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1386 | - * @see quartiles() |
|
1387 | - * @see median() |
|
1388 | - */ |
|
1389 | - function percentile($p) {/*{{{*/ |
|
1390 | - $count = $this->count(); |
|
1391 | - if (PEAR::isError($count)) { |
|
1392 | - return $count; |
|
1393 | - } |
|
1394 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1395 | - $data =& $this->_dataExpanded; |
|
1396 | - } else { |
|
1397 | - $data =& $this->_data; |
|
1398 | - } |
|
1399 | - $obsidx = $p * ($count + 1) / 100; |
|
1400 | - if (intval($obsidx) == $obsidx) { |
|
1401 | - return $data[($obsidx - 1)]; |
|
1402 | - } elseif ($obsidx < 1) { |
|
1403 | - return $data[0]; |
|
1404 | - } elseif ($obsidx > $count) { |
|
1405 | - return $data[($count - 1)]; |
|
1406 | - } else { |
|
1407 | - $left = floor($obsidx - 1); |
|
1408 | - $right = ceil($obsidx - 1); |
|
1409 | - return ($data[$left] + $data[$right]) / 2; |
|
1410 | - } |
|
1411 | - }/*}}}*/ |
|
1412 | - |
|
1413 | - // private methods |
|
1414 | - |
|
1415 | - /** |
|
1416 | - * Utility function to calculate: SUM { (xi - mean)^n } |
|
1417 | - * |
|
1418 | - * @access private |
|
1419 | - * @param numeric $power the exponent |
|
1420 | - * @param optional double $mean the data set mean value |
|
1421 | - * @return mixed the sum on success, a PEAR_Error object otherwise |
|
1422 | - * |
|
1423 | - * @see stDev() |
|
1424 | - * @see variaceWithMean(); |
|
1425 | - * @see skewness(); |
|
1426 | - * @see kurtosis(); |
|
1427 | - */ |
|
1428 | - function __sumdiff($power, $mean=null) {/*{{{*/ |
|
1429 | - if ($this->_data == null) { |
|
1430 | - return PEAR::raiseError('data has not been set'); |
|
1431 | - } |
|
1432 | - if (is_null($mean)) { |
|
1433 | - $mean = $this->mean(); |
|
1434 | - if (PEAR::isError($mean)) { |
|
1435 | - return $mean; |
|
1436 | - } |
|
1437 | - } |
|
1438 | - $sdiff = 0; |
|
1439 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1440 | - foreach ($this->_data as $val=>$freq) { |
|
1441 | - $sdiff += $freq * pow((double)($val - $mean), (double)$power); |
|
1442 | - } |
|
1443 | - } else { |
|
1444 | - foreach ($this->_data as $val) |
|
1445 | - $sdiff += pow((double)($val - $mean), (double)$power); |
|
1446 | - } |
|
1447 | - return $sdiff; |
|
1448 | - }/*}}}*/ |
|
1449 | - |
|
1450 | - /** |
|
1451 | - * Utility function to calculate the variance with or without |
|
1452 | - * a fixed mean |
|
1453 | - * |
|
1454 | - * @access private |
|
1455 | - * @param $mean the fixed mean to use, null as default |
|
1456 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1457 | - * @see variance() |
|
1458 | - * @see varianceWithMean() |
|
1459 | - */ |
|
1460 | - function __calcVariance($mean = null) {/*{{{*/ |
|
1461 | - if ($this->_data == null) { |
|
1462 | - return PEAR::raiseError('data has not been set'); |
|
1463 | - } |
|
1464 | - $sumdiff2 = $this->__sumdiff(2, $mean); |
|
1465 | - if (PEAR::isError($sumdiff2)) { |
|
1466 | - return $sumdiff2; |
|
1467 | - } |
|
1468 | - $count = $this->count(); |
|
1469 | - if (PEAR::isError($count)) { |
|
1470 | - return $count; |
|
1471 | - } |
|
1472 | - if ($count == 1) { |
|
1473 | - return PEAR::raiseError('cannot calculate variance of a singe data point'); |
|
1474 | - } |
|
1475 | - return ($sumdiff2 / ($count - 1)); |
|
1476 | - }/*}}}*/ |
|
1477 | - |
|
1478 | - /** |
|
1479 | - * Utility function to calculate the absolute deviation with or without |
|
1480 | - * a fixed mean |
|
1481 | - * |
|
1482 | - * @access private |
|
1483 | - * @param $mean the fixed mean to use, null as default |
|
1484 | - * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1485 | - * @see absDev() |
|
1486 | - * @see absDevWithMean() |
|
1487 | - */ |
|
1488 | - function __calcAbsoluteDeviation($mean = null) {/*{{{*/ |
|
1489 | - if ($this->_data == null) { |
|
1490 | - return PEAR::raiseError('data has not been set'); |
|
1491 | - } |
|
1492 | - $count = $this->count(); |
|
1493 | - if (PEAR::isError($count)) { |
|
1494 | - return $count; |
|
1495 | - } |
|
1496 | - $sumabsdev = $this->__sumabsdev($mean); |
|
1497 | - if (PEAR::isError($sumabsdev)) { |
|
1498 | - return $sumabsdev; |
|
1499 | - } |
|
1500 | - return $sumabsdev / $count; |
|
1501 | - }/*}}}*/ |
|
1502 | - |
|
1503 | - /** |
|
1504 | - * Utility function to calculate: SUM { | xi - mean | } |
|
1505 | - * |
|
1506 | - * @access private |
|
1507 | - * @param optional double $mean the mean value for the set or population |
|
1508 | - * @return mixed the sum on success, a PEAR_Error object otherwise |
|
1509 | - * |
|
1510 | - * @see absDev() |
|
1511 | - * @see absDevWithMean() |
|
1512 | - */ |
|
1513 | - function __sumabsdev($mean=null) {/*{{{*/ |
|
1514 | - if ($this->_data == null) { |
|
1515 | - return PEAR::raiseError('data has not been set'); |
|
1516 | - } |
|
1517 | - if (is_null($mean)) { |
|
1518 | - $mean = $this->mean(); |
|
1519 | - } |
|
1520 | - $sdev = 0; |
|
1521 | - if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1522 | - foreach ($this->_data as $val=>$freq) { |
|
1523 | - $sdev += $freq * abs($val - $mean); |
|
1524 | - } |
|
1525 | - } else { |
|
1526 | - foreach ($this->_data as $val) { |
|
1527 | - $sdev += abs($val - $mean); |
|
1528 | - } |
|
1529 | - } |
|
1530 | - return $sdev; |
|
1531 | - }/*}}}*/ |
|
1532 | - |
|
1533 | - /** |
|
1534 | - * Utility function to format a PEAR_Error to be used by calc(), |
|
1535 | - * calcBasic() and calcFull() |
|
1536 | - * |
|
1537 | - * @access private |
|
1538 | - * @param mixed $v value to be formatted |
|
1539 | - * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
1540 | - * or only the error message will be returned (when false) |
|
1541 | - * @return mixed if the value is a PEAR_Error object, and $useErrorObject |
|
1542 | - * is false, then a string with the error message will be returned, |
|
1543 | - * otherwise the value will not be modified and returned as passed. |
|
1544 | - */ |
|
1545 | - function __format($v, $useErrorObject=true) {/*{{{*/ |
|
1546 | - if (PEAR::isError($v) && $useErrorObject == false) { |
|
1547 | - return $v->getMessage(); |
|
1548 | - } else { |
|
1549 | - return $v; |
|
1550 | - } |
|
1551 | - }/*}}}*/ |
|
1552 | - |
|
1553 | - /** |
|
1554 | - * Utility function to validate the data and modify it |
|
1555 | - * according to the current null handling option |
|
1556 | - * |
|
1557 | - * @access private |
|
1558 | - * @return mixed true on success, a PEAR_Error object otherwise |
|
1559 | - * |
|
1560 | - * @see setData() |
|
1561 | - */ |
|
1562 | - function _validate() {/*{{{*/ |
|
1563 | - $flag = ($this->_dataOption == STATS_DATA_CUMMULATIVE); |
|
1564 | - foreach ($this->_data as $key=>$value) { |
|
1565 | - $d = ($flag) ? $key : $value; |
|
1566 | - $v = ($flag) ? $value : $key; |
|
1567 | - if (!is_numeric($d)) { |
|
1568 | - switch ($this->_nullOption) { |
|
1569 | - case STATS_IGNORE_NULL : |
|
1570 | - unset($this->_data["$key"]); |
|
1571 | - break; |
|
1572 | - case STATS_USE_NULL_AS_ZERO: |
|
1573 | - if ($flag) { |
|
1574 | - unset($this->_data["$key"]); |
|
1575 | - $this->_data[0] += $v; |
|
1576 | - } else { |
|
1577 | - $this->_data[$key] = 0; |
|
1578 | - } |
|
1579 | - break; |
|
1580 | - case STATS_REJECT_NULL : |
|
1581 | - default: |
|
1582 | - return PEAR::raiseError('data rejected, contains NULL values'); |
|
1583 | - break; |
|
1584 | - } |
|
1585 | - } |
|
1586 | - } |
|
1587 | - if ($flag) { |
|
1588 | - ksort($this->_data); |
|
1589 | - $this->_dataExpanded = array(); |
|
1590 | - foreach ($this->_data as $val=>$freq) { |
|
1591 | - $this->_dataExpanded = array_pad($this->_dataExpanded, count($this->_dataExpanded) + $freq, $val); |
|
1592 | - } |
|
1593 | - sort($this->_dataExpanded); |
|
1594 | - } else { |
|
1595 | - sort($this->_data); |
|
1596 | - } |
|
1597 | - return true; |
|
1598 | - }/*}}}*/ |
|
120 | + // properties /*{{{*/ |
|
121 | + |
|
122 | + /** |
|
123 | + * The simple or cummulative data set. |
|
124 | + * Null by default. |
|
125 | + * |
|
126 | + * @access private |
|
127 | + * @var array |
|
128 | + */ |
|
129 | + public $_data = null; |
|
130 | + |
|
131 | + /** |
|
132 | + * Expanded data set. Only set when cummulative data |
|
133 | + * is being used. Null by default. |
|
134 | + * |
|
135 | + * @access private |
|
136 | + * @var array |
|
137 | + */ |
|
138 | + public $_dataExpanded = null; |
|
139 | + |
|
140 | + /** |
|
141 | + * Flag for data type, one of STATS_DATA_SIMPLE or |
|
142 | + * STATS_DATA_CUMMULATIVE. Null by default. |
|
143 | + * |
|
144 | + * @access private |
|
145 | + * @var int |
|
146 | + */ |
|
147 | + public $_dataOption = null; |
|
148 | + |
|
149 | + /** |
|
150 | + * Flag for null handling options. One of STATS_REJECT_NULL, |
|
151 | + * STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO |
|
152 | + * |
|
153 | + * @access private |
|
154 | + * @var int |
|
155 | + */ |
|
156 | + public $_nullOption; |
|
157 | + |
|
158 | + /** |
|
159 | + * Array for caching result values, should be reset |
|
160 | + * when using setData() |
|
161 | + * |
|
162 | + * @access private |
|
163 | + * @var array |
|
164 | + */ |
|
165 | + public $_calculatedValues = array(); |
|
166 | + |
|
167 | + /*}}}*/ |
|
168 | + |
|
169 | + /** |
|
170 | + * Constructor for the class |
|
171 | + * |
|
172 | + * @access public |
|
173 | + * @param optional int $nullOption how to handle null values |
|
174 | + * @return object Math_Stats |
|
175 | + */ |
|
176 | + function Math_Stats($nullOption=STATS_REJECT_NULL) {/*{{{*/ |
|
177 | + $this->_nullOption = $nullOption; |
|
178 | + }/*}}}*/ |
|
179 | + |
|
180 | + /** |
|
181 | + * Sets and verifies the data, checking for nulls and using |
|
182 | + * the current null handling option |
|
183 | + * |
|
184 | + * @access public |
|
185 | + * @param array $arr the data set |
|
186 | + * @param optional int $opt data format: STATS_DATA_CUMMULATIVE or STATS_DATA_SIMPLE (default) |
|
187 | + * @return mixed true on success, a PEAR_Error object otherwise |
|
188 | + */ |
|
189 | + function setData($arr, $opt=STATS_DATA_SIMPLE) {/*{{{*/ |
|
190 | + if (!is_array($arr)) { |
|
191 | + return PEAR::raiseError('invalid data, an array of numeric data was expected'); |
|
192 | + } |
|
193 | + $this->_data = null; |
|
194 | + $this->_dataExpanded = null; |
|
195 | + $this->_dataOption = null; |
|
196 | + $this->_calculatedValues = array(); |
|
197 | + if ($opt == STATS_DATA_SIMPLE) { |
|
198 | + $this->_dataOption = $opt; |
|
199 | + $this->_data = array_values($arr); |
|
200 | + } else if ($opt == STATS_DATA_CUMMULATIVE) { |
|
201 | + $this->_dataOption = $opt; |
|
202 | + $this->_data = $arr; |
|
203 | + $this->_dataExpanded = array(); |
|
204 | + } |
|
205 | + return $this->_validate(); |
|
206 | + }/*}}}*/ |
|
207 | + |
|
208 | + /** |
|
209 | + * Returns the data which might have been modified |
|
210 | + * according to the current null handling options. |
|
211 | + * |
|
212 | + * @access public |
|
213 | + * @param boolean $expanded whether to return a expanded list, default is false |
|
214 | + * @return mixed array of data on success, a PEAR_Error object otherwise |
|
215 | + * @see _validate() |
|
216 | + */ |
|
217 | + function getData($expanded=false) {/*{{{*/ |
|
218 | + if ($this->_data == null) { |
|
219 | + return PEAR::raiseError('data has not been set'); |
|
220 | + } |
|
221 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE && $expanded) { |
|
222 | + return $this->_dataExpanded; |
|
223 | + } else { |
|
224 | + return $this->_data; |
|
225 | + } |
|
226 | + }/*}}}*/ |
|
227 | + |
|
228 | + /** |
|
229 | + * Sets the null handling option. |
|
230 | + * Must be called before assigning a new data set containing null values |
|
231 | + * |
|
232 | + * @access public |
|
233 | + * @return mixed true on success, a PEAR_Error object otherwise |
|
234 | + * @see _validate() |
|
235 | + */ |
|
236 | + function setNullOption($nullOption) {/*{{{*/ |
|
237 | + if ($nullOption == STATS_REJECT_NULL |
|
238 | + || $nullOption == STATS_IGNORE_NULL |
|
239 | + || $nullOption == STATS_USE_NULL_AS_ZERO) { |
|
240 | + $this->_nullOption = $nullOption; |
|
241 | + return true; |
|
242 | + } else { |
|
243 | + return PEAR::raiseError('invalid null handling option expecting: '. |
|
244 | + 'STATS_REJECT_NULL, STATS_IGNORE_NULL or STATS_USE_NULL_AS_ZERO'); |
|
245 | + } |
|
246 | + }/*}}}*/ |
|
247 | + |
|
248 | + /** |
|
249 | + * Transforms the data by substracting each entry from the mean and |
|
250 | + * dividing by its standard deviation. This will reset all pre-calculated |
|
251 | + * values to their original (unset) defaults. |
|
252 | + * |
|
253 | + * @access public |
|
254 | + * @return mixed true on success, a PEAR_Error object otherwise |
|
255 | + * @see mean() |
|
256 | + * @see stDev() |
|
257 | + * @see setData() |
|
258 | + */ |
|
259 | + function studentize() {/*{{{*/ |
|
260 | + $mean = $this->mean(); |
|
261 | + if (PEAR::isError($mean)) { |
|
262 | + return $mean; |
|
263 | + } |
|
264 | + $std = $this->stDev(); |
|
265 | + if (PEAR::isError($std)) { |
|
266 | + return $std; |
|
267 | + } |
|
268 | + if ($std == 0) { |
|
269 | + return PEAR::raiseError('cannot studentize data, standard deviation is zero.'); |
|
270 | + } |
|
271 | + $arr = array(); |
|
272 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
273 | + foreach ($this->_data as $val=>$freq) { |
|
274 | + $newval = ($val - $mean) / $std; |
|
275 | + $arr["$newval"] = $freq; |
|
276 | + } |
|
277 | + } else { |
|
278 | + foreach ($this->_data as $val) { |
|
279 | + $newval = ($val - $mean) / $std; |
|
280 | + $arr[] = $newval; |
|
281 | + } |
|
282 | + } |
|
283 | + return $this->setData($arr, $this->_dataOption); |
|
284 | + }/*}}}*/ |
|
285 | + |
|
286 | + /** |
|
287 | + * Transforms the data by substracting each entry from the mean. |
|
288 | + * This will reset all pre-calculated values to their original (unset) defaults. |
|
289 | + * |
|
290 | + * @access public |
|
291 | + * @return mixed true on success, a PEAR_Error object otherwise |
|
292 | + * @see mean() |
|
293 | + * @see setData() |
|
294 | + */ |
|
295 | + function center() {/*{{{*/ |
|
296 | + $mean = $this->mean(); |
|
297 | + if (PEAR::isError($mean)) { |
|
298 | + return $mean; |
|
299 | + } |
|
300 | + $arr = array(); |
|
301 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
302 | + foreach ($this->_data as $val=>$freq) { |
|
303 | + $newval = $val - $mean; |
|
304 | + $arr["$newval"] = $freq; |
|
305 | + } |
|
306 | + } else { |
|
307 | + foreach ($this->_data as $val) { |
|
308 | + $newval = $val - $mean; |
|
309 | + $arr[] = $newval; |
|
310 | + } |
|
311 | + } |
|
312 | + return $this->setData($arr, $this->_dataOption); |
|
313 | + }/*}}}*/ |
|
314 | + |
|
315 | + /** |
|
316 | + * Calculates the basic or full statistics for the data set |
|
317 | + * |
|
318 | + * @access public |
|
319 | + * @param int $mode one of STATS_BASIC or STATS_FULL |
|
320 | + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
321 | + * or only the error message will be returned (when false), if an error happens. |
|
322 | + * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise |
|
323 | + * @see calcBasic() |
|
324 | + * @see calcFull() |
|
325 | + */ |
|
326 | + function calc($mode, $returnErrorObject=true) {/*{{{*/ |
|
327 | + if ($this->_data == null) { |
|
328 | + return PEAR::raiseError('data has not been set'); |
|
329 | + } |
|
330 | + if ($mode == STATS_BASIC) { |
|
331 | + return $this->calcBasic($returnErrorObject); |
|
332 | + } elseif ($mode == STATS_FULL) { |
|
333 | + return $this->calcFull($returnErrorObject); |
|
334 | + } else { |
|
335 | + return PEAR::raiseError('incorrect mode, expected STATS_BASIC or STATS_FULL'); |
|
336 | + } |
|
337 | + }/*}}}*/ |
|
338 | + |
|
339 | + /** |
|
340 | + * Calculates a basic set of statistics |
|
341 | + * |
|
342 | + * @access public |
|
343 | + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
344 | + * or only the error message will be returned (when false), if an error happens. |
|
345 | + * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise |
|
346 | + * @see calc() |
|
347 | + * @see calcFull() |
|
348 | + */ |
|
349 | + function calcBasic($returnErrorObject=true) {/*{{{*/ |
|
350 | + return array ( |
|
351 | + 'min' => $this->__format($this->min(), $returnErrorObject), |
|
352 | + 'max' => $this->__format($this->max(), $returnErrorObject), |
|
353 | + 'sum' => $this->__format($this->sum(), $returnErrorObject), |
|
354 | + 'sum2' => $this->__format($this->sum2(), $returnErrorObject), |
|
355 | + 'count' => $this->__format($this->count(), $returnErrorObject), |
|
356 | + 'mean' => $this->__format($this->mean(), $returnErrorObject), |
|
357 | + 'stdev' => $this->__format($this->stDev(), $returnErrorObject), |
|
358 | + 'variance' => $this->__format($this->variance(), $returnErrorObject), |
|
359 | + 'range' => $this->__format($this->range(), $returnErrorObject) |
|
360 | + ); |
|
361 | + }/*}}}*/ |
|
362 | + |
|
363 | + /** |
|
364 | + * Calculates a full set of statistics |
|
365 | + * |
|
366 | + * @access public |
|
367 | + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
368 | + * or only the error message will be returned (when false), if an error happens. |
|
369 | + * @return mixed an associative array of statistics on success, a PEAR_Error object otherwise |
|
370 | + * @see calc() |
|
371 | + * @see calcBasic() |
|
372 | + */ |
|
373 | + function calcFull($returnErrorObject=true) {/*{{{*/ |
|
374 | + return array ( |
|
375 | + 'min' => $this->__format($this->min(), $returnErrorObject), |
|
376 | + 'max' => $this->__format($this->max(), $returnErrorObject), |
|
377 | + 'sum' => $this->__format($this->sum(), $returnErrorObject), |
|
378 | + 'sum2' => $this->__format($this->sum2(), $returnErrorObject), |
|
379 | + 'count' => $this->__format($this->count(), $returnErrorObject), |
|
380 | + 'mean' => $this->__format($this->mean(), $returnErrorObject), |
|
381 | + 'median' => $this->__format($this->median(), $returnErrorObject), |
|
382 | + 'mode' => $this->__format($this->mode(), $returnErrorObject), |
|
383 | + 'midrange' => $this->__format($this->midrange(), $returnErrorObject), |
|
384 | + 'geometric_mean' => $this->__format($this->geometricMean(), $returnErrorObject), |
|
385 | + 'harmonic_mean' => $this->__format($this->harmonicMean(), $returnErrorObject), |
|
386 | + 'stdev' => $this->__format($this->stDev(), $returnErrorObject), |
|
387 | + 'absdev' => $this->__format($this->absDev(), $returnErrorObject), |
|
388 | + 'variance' => $this->__format($this->variance(), $returnErrorObject), |
|
389 | + 'range' => $this->__format($this->range(), $returnErrorObject), |
|
390 | + 'std_error_of_mean' => $this->__format($this->stdErrorOfMean(), $returnErrorObject), |
|
391 | + 'skewness' => $this->__format($this->skewness(), $returnErrorObject), |
|
392 | + 'kurtosis' => $this->__format($this->kurtosis(), $returnErrorObject), |
|
393 | + 'coeff_of_variation' => $this->__format($this->coeffOfVariation(), $returnErrorObject), |
|
394 | + 'sample_central_moments' => array ( |
|
395 | + 1 => $this->__format($this->sampleCentralMoment(1), $returnErrorObject), |
|
396 | + 2 => $this->__format($this->sampleCentralMoment(2), $returnErrorObject), |
|
397 | + 3 => $this->__format($this->sampleCentralMoment(3), $returnErrorObject), |
|
398 | + 4 => $this->__format($this->sampleCentralMoment(4), $returnErrorObject), |
|
399 | + 5 => $this->__format($this->sampleCentralMoment(5), $returnErrorObject) |
|
400 | + ), |
|
401 | + 'sample_raw_moments' => array ( |
|
402 | + 1 => $this->__format($this->sampleRawMoment(1), $returnErrorObject), |
|
403 | + 2 => $this->__format($this->sampleRawMoment(2), $returnErrorObject), |
|
404 | + 3 => $this->__format($this->sampleRawMoment(3), $returnErrorObject), |
|
405 | + 4 => $this->__format($this->sampleRawMoment(4), $returnErrorObject), |
|
406 | + 5 => $this->__format($this->sampleRawMoment(5), $returnErrorObject) |
|
407 | + ), |
|
408 | + 'frequency' => $this->__format($this->frequency(), $returnErrorObject), |
|
409 | + 'quartiles' => $this->__format($this->quartiles(), $returnErrorObject), |
|
410 | + 'interquartile_range' => $this->__format($this->interquartileRange(), $returnErrorObject), |
|
411 | + 'interquartile_mean' => $this->__format($this->interquartileMean(), $returnErrorObject), |
|
412 | + 'quartile_deviation' => $this->__format($this->quartileDeviation(), $returnErrorObject), |
|
413 | + 'quartile_variation_coefficient' => $this->__format($this->quartileVariationCoefficient(), $returnErrorObject), |
|
414 | + 'quartile_skewness_coefficient' => $this->__format($this->quartileSkewnessCoefficient(), $returnErrorObject) |
|
415 | + ); |
|
416 | + }/*}}}*/ |
|
417 | + |
|
418 | + /** |
|
419 | + * Calculates the minimum of a data set. |
|
420 | + * Handles cummulative data sets correctly |
|
421 | + * |
|
422 | + * @access public |
|
423 | + * @return mixed the minimum value on success, a PEAR_Error object otherwise |
|
424 | + * @see calc() |
|
425 | + * @see max() |
|
426 | + */ |
|
427 | + function min() {/*{{{*/ |
|
428 | + if ($this->_data == null) { |
|
429 | + return PEAR::raiseError('data has not been set'); |
|
430 | + } |
|
431 | + if (!array_key_exists('min', $this->_calculatedValues)) { |
|
432 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
433 | + $min = min(array_keys($this->_data)); |
|
434 | + } else { |
|
435 | + $min = min($this->_data); |
|
436 | + } |
|
437 | + $this->_calculatedValues['min'] = $min; |
|
438 | + } |
|
439 | + return $this->_calculatedValues['min']; |
|
440 | + }/*}}}*/ |
|
441 | + |
|
442 | + /** |
|
443 | + * Calculates the maximum of a data set. |
|
444 | + * Handles cummulative data sets correctly |
|
445 | + * |
|
446 | + * @access public |
|
447 | + * @return mixed the maximum value on success, a PEAR_Error object otherwise |
|
448 | + * @see calc() |
|
449 | + * @see min() |
|
450 | + */ |
|
451 | + function max() {/*{{{*/ |
|
452 | + if ($this->_data == null) { |
|
453 | + return PEAR::raiseError('data has not been set'); |
|
454 | + } |
|
455 | + if (!array_key_exists('max', $this->_calculatedValues)) { |
|
456 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
457 | + $max = max(array_keys($this->_data)); |
|
458 | + } else { |
|
459 | + $max = max($this->_data); |
|
460 | + } |
|
461 | + $this->_calculatedValues['max'] = $max; |
|
462 | + } |
|
463 | + return $this->_calculatedValues['max']; |
|
464 | + }/*}}}*/ |
|
465 | + |
|
466 | + /** |
|
467 | + * Calculates SUM { xi } |
|
468 | + * Handles cummulative data sets correctly |
|
469 | + * |
|
470 | + * @access public |
|
471 | + * @return mixed the sum on success, a PEAR_Error object otherwise |
|
472 | + * @see calc() |
|
473 | + * @see sum2() |
|
474 | + * @see sumN() |
|
475 | + */ |
|
476 | + function sum() {/*{{{*/ |
|
477 | + if (!array_key_exists('sum', $this->_calculatedValues)) { |
|
478 | + $sum = $this->sumN(1); |
|
479 | + if (PEAR::isError($sum)) { |
|
480 | + return $sum; |
|
481 | + } else { |
|
482 | + $this->_calculatedValues['sum'] = $sum; |
|
483 | + } |
|
484 | + } |
|
485 | + return $this->_calculatedValues['sum']; |
|
486 | + }/*}}}*/ |
|
487 | + |
|
488 | + /** |
|
489 | + * Calculates SUM { (xi)^2 } |
|
490 | + * Handles cummulative data sets correctly |
|
491 | + * |
|
492 | + * @access public |
|
493 | + * @return mixed the sum on success, a PEAR_Error object otherwise |
|
494 | + * @see calc() |
|
495 | + * @see sum() |
|
496 | + * @see sumN() |
|
497 | + */ |
|
498 | + function sum2() {/*{{{*/ |
|
499 | + if (!array_key_exists('sum2', $this->_calculatedValues)) { |
|
500 | + $sum2 = $this->sumN(2); |
|
501 | + if (PEAR::isError($sum2)) { |
|
502 | + return $sum2; |
|
503 | + } else { |
|
504 | + $this->_calculatedValues['sum2'] = $sum2; |
|
505 | + } |
|
506 | + } |
|
507 | + return $this->_calculatedValues['sum2']; |
|
508 | + }/*}}}*/ |
|
509 | + |
|
510 | + /** |
|
511 | + * Calculates SUM { (xi)^n } |
|
512 | + * Handles cummulative data sets correctly |
|
513 | + * |
|
514 | + * @access public |
|
515 | + * @param numeric $n the exponent |
|
516 | + * @return mixed the sum on success, a PEAR_Error object otherwise |
|
517 | + * @see calc() |
|
518 | + * @see sum() |
|
519 | + * @see sum2() |
|
520 | + */ |
|
521 | + function sumN($n) {/*{{{*/ |
|
522 | + if ($this->_data == null) { |
|
523 | + return PEAR::raiseError('data has not been set'); |
|
524 | + } |
|
525 | + $sumN = 0; |
|
526 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
527 | + foreach($this->_data as $val=>$freq) { |
|
528 | + $sumN += $freq * pow((double)$val, (double)$n); |
|
529 | + } |
|
530 | + } else { |
|
531 | + foreach($this->_data as $val) { |
|
532 | + $sumN += pow((double)$val, (double)$n); |
|
533 | + } |
|
534 | + } |
|
535 | + return $sumN; |
|
536 | + }/*}}}*/ |
|
537 | + |
|
538 | + /** |
|
539 | + * Calculates PROD { (xi) }, (the product of all observations) |
|
540 | + * Handles cummulative data sets correctly |
|
541 | + * |
|
542 | + * @access public |
|
543 | + * @return mixed the product on success, a PEAR_Error object otherwise |
|
544 | + * @see productN() |
|
545 | + */ |
|
546 | + function product() {/*{{{*/ |
|
547 | + if (!array_key_exists('product', $this->_calculatedValues)) { |
|
548 | + $product = $this->productN(1); |
|
549 | + if (PEAR::isError($product)) { |
|
550 | + return $product; |
|
551 | + } else { |
|
552 | + $this->_calculatedValues['product'] = $product; |
|
553 | + } |
|
554 | + } |
|
555 | + return $this->_calculatedValues['product']; |
|
556 | + }/*}}}*/ |
|
557 | + |
|
558 | + /** |
|
559 | + * Calculates PROD { (xi)^n }, which is the product of all observations |
|
560 | + * Handles cummulative data sets correctly |
|
561 | + * |
|
562 | + * @access public |
|
563 | + * @param numeric $n the exponent |
|
564 | + * @return mixed the product on success, a PEAR_Error object otherwise |
|
565 | + * @see product() |
|
566 | + */ |
|
567 | + function productN($n) {/*{{{*/ |
|
568 | + if ($this->_data == null) { |
|
569 | + return PEAR::raiseError('data has not been set'); |
|
570 | + } |
|
571 | + $prodN = 1.0; |
|
572 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
573 | + foreach($this->_data as $val=>$freq) { |
|
574 | + if ($val == 0) { |
|
575 | + return 0.0; |
|
576 | + } |
|
577 | + $prodN *= $freq * pow((double)$val, (double)$n); |
|
578 | + } |
|
579 | + } else { |
|
580 | + foreach($this->_data as $val) { |
|
581 | + if ($val == 0) { |
|
582 | + return 0.0; |
|
583 | + } |
|
584 | + $prodN *= pow((double)$val, (double)$n); |
|
585 | + } |
|
586 | + } |
|
587 | + return $prodN; |
|
588 | + |
|
589 | + }/*}}}*/ |
|
590 | + |
|
591 | + /** |
|
592 | + * Calculates the number of data points in the set |
|
593 | + * Handles cummulative data sets correctly |
|
594 | + * |
|
595 | + * @access public |
|
596 | + * @return mixed the count on success, a PEAR_Error object otherwise |
|
597 | + * @see calc() |
|
598 | + */ |
|
599 | + function count() {/*{{{*/ |
|
600 | + if ($this->_data == null) { |
|
601 | + return PEAR::raiseError('data has not been set'); |
|
602 | + } |
|
603 | + if (!array_key_exists('count', $this->_calculatedValues)) { |
|
604 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
605 | + $count = count($this->_dataExpanded); |
|
606 | + } else { |
|
607 | + $count = count($this->_data); |
|
608 | + } |
|
609 | + $this->_calculatedValues['count'] = $count; |
|
610 | + } |
|
611 | + return $this->_calculatedValues['count']; |
|
612 | + }/*}}}*/ |
|
613 | + |
|
614 | + /** |
|
615 | + * Calculates the mean (average) of the data points in the set |
|
616 | + * Handles cummulative data sets correctly |
|
617 | + * |
|
618 | + * @access public |
|
619 | + * @return mixed the mean value on success, a PEAR_Error object otherwise |
|
620 | + * @see calc() |
|
621 | + * @see sum() |
|
622 | + * @see count() |
|
623 | + */ |
|
624 | + function mean() {/*{{{*/ |
|
625 | + if (!array_key_exists('mean', $this->_calculatedValues)) { |
|
626 | + $sum = $this->sum(); |
|
627 | + if (PEAR::isError($sum)) { |
|
628 | + return $sum; |
|
629 | + } |
|
630 | + $count = $this->count(); |
|
631 | + if (PEAR::isError($count)) { |
|
632 | + return $count; |
|
633 | + } |
|
634 | + $this->_calculatedValues['mean'] = $sum / $count; |
|
635 | + } |
|
636 | + return $this->_calculatedValues['mean']; |
|
637 | + }/*}}}*/ |
|
638 | + |
|
639 | + /** |
|
640 | + * Calculates the range of the data set = max - min |
|
641 | + * |
|
642 | + * @access public |
|
643 | + * @return mixed the value of the range on success, a PEAR_Error object otherwise. |
|
644 | + */ |
|
645 | + function range() {/*{{{*/ |
|
646 | + if (!array_key_exists('range', $this->_calculatedValues)) { |
|
647 | + $min = $this->min(); |
|
648 | + if (PEAR::isError($min)) { |
|
649 | + return $min; |
|
650 | + } |
|
651 | + $max = $this->max(); |
|
652 | + if (PEAR::isError($max)) { |
|
653 | + return $max; |
|
654 | + } |
|
655 | + $this->_calculatedValues['range'] = $max - $min; |
|
656 | + } |
|
657 | + return $this->_calculatedValues['range']; |
|
658 | + |
|
659 | + }/*}}}*/ |
|
660 | + |
|
661 | + /** |
|
662 | + * Calculates the variance (unbiased) of the data points in the set |
|
663 | + * Handles cummulative data sets correctly |
|
664 | + * |
|
665 | + * @access public |
|
666 | + * @return mixed the variance value on success, a PEAR_Error object otherwise |
|
667 | + * @see calc() |
|
668 | + * @see __sumdiff() |
|
669 | + * @see count() |
|
670 | + */ |
|
671 | + function variance() {/*{{{*/ |
|
672 | + if (!array_key_exists('variance', $this->_calculatedValues)) { |
|
673 | + $variance = $this->__calcVariance(); |
|
674 | + if (PEAR::isError($variance)) { |
|
675 | + return $variance; |
|
676 | + } |
|
677 | + $this->_calculatedValues['variance'] = $variance; |
|
678 | + } |
|
679 | + return $this->_calculatedValues['variance']; |
|
680 | + }/*}}}*/ |
|
681 | + |
|
682 | + /** |
|
683 | + * Calculates the standard deviation (unbiased) of the data points in the set |
|
684 | + * Handles cummulative data sets correctly |
|
685 | + * |
|
686 | + * @access public |
|
687 | + * @return mixed the standard deviation on success, a PEAR_Error object otherwise |
|
688 | + * @see calc() |
|
689 | + * @see variance() |
|
690 | + */ |
|
691 | + function stDev() {/*{{{*/ |
|
692 | + if (!array_key_exists('stDev', $this->_calculatedValues)) { |
|
693 | + $variance = $this->variance(); |
|
694 | + if (PEAR::isError($variance)) { |
|
695 | + return $variance; |
|
696 | + } |
|
697 | + $this->_calculatedValues['stDev'] = sqrt($variance); |
|
698 | + } |
|
699 | + return $this->_calculatedValues['stDev']; |
|
700 | + }/*}}}*/ |
|
701 | + |
|
702 | + /** |
|
703 | + * Calculates the variance (unbiased) of the data points in the set |
|
704 | + * given a fixed mean (average) value. Not used in calcBasic(), calcFull() |
|
705 | + * or calc(). |
|
706 | + * Handles cummulative data sets correctly |
|
707 | + * |
|
708 | + * @access public |
|
709 | + * @param numeric $mean the fixed mean value |
|
710 | + * @return mixed the variance on success, a PEAR_Error object otherwise |
|
711 | + * @see __sumdiff() |
|
712 | + * @see count() |
|
713 | + * @see variance() |
|
714 | + */ |
|
715 | + function varianceWithMean($mean) {/*{{{*/ |
|
716 | + return $this->__calcVariance($mean); |
|
717 | + }/*}}}*/ |
|
718 | + |
|
719 | + /** |
|
720 | + * Calculates the standard deviation (unbiased) of the data points in the set |
|
721 | + * given a fixed mean (average) value. Not used in calcBasic(), calcFull() |
|
722 | + * or calc(). |
|
723 | + * Handles cummulative data sets correctly |
|
724 | + * |
|
725 | + * @access public |
|
726 | + * @param numeric $mean the fixed mean value |
|
727 | + * @return mixed the standard deviation on success, a PEAR_Error object otherwise |
|
728 | + * @see varianceWithMean() |
|
729 | + * @see stDev() |
|
730 | + */ |
|
731 | + function stDevWithMean($mean) {/*{{{*/ |
|
732 | + $varianceWM = $this->varianceWithMean($mean); |
|
733 | + if (PEAR::isError($varianceWM)) { |
|
734 | + return $varianceWM; |
|
735 | + } |
|
736 | + return sqrt($varianceWM); |
|
737 | + }/*}}}*/ |
|
738 | + |
|
739 | + /** |
|
740 | + * Calculates the absolute deviation of the data points in the set |
|
741 | + * Handles cummulative data sets correctly |
|
742 | + * |
|
743 | + * @access public |
|
744 | + * @return mixed the absolute deviation on success, a PEAR_Error object otherwise |
|
745 | + * @see calc() |
|
746 | + * @see __sumabsdev() |
|
747 | + * @see count() |
|
748 | + * @see absDevWithMean() |
|
749 | + */ |
|
750 | + function absDev() {/*{{{*/ |
|
751 | + if (!array_key_exists('absDev', $this->_calculatedValues)) { |
|
752 | + $absDev = $this->__calcAbsoluteDeviation(); |
|
753 | + if (PEAR::isError($absdev)) { |
|
754 | + return $absdev; |
|
755 | + } |
|
756 | + $this->_calculatedValues['absDev'] = $absDev; |
|
757 | + } |
|
758 | + return $this->_calculatedValues['absDev']; |
|
759 | + }/*}}}*/ |
|
760 | + |
|
761 | + /** |
|
762 | + * Calculates the absolute deviation of the data points in the set |
|
763 | + * given a fixed mean (average) value. Not used in calcBasic(), calcFull() |
|
764 | + * or calc(). |
|
765 | + * Handles cummulative data sets correctly |
|
766 | + * |
|
767 | + * @access public |
|
768 | + * @param numeric $mean the fixed mean value |
|
769 | + * @return mixed the absolute deviation on success, a PEAR_Error object otherwise |
|
770 | + * @see __sumabsdev() |
|
771 | + * @see absDev() |
|
772 | + */ |
|
773 | + function absDevWithMean($mean) {/*{{{*/ |
|
774 | + return $this->__calcAbsoluteDeviation($mean); |
|
775 | + }/*}}}*/ |
|
776 | + |
|
777 | + /** |
|
778 | + * Calculates the skewness of the data distribution in the set |
|
779 | + * The skewness measures the degree of asymmetry of a distribution, |
|
780 | + * and is related to the third central moment of a distribution. |
|
781 | + * A normal distribution has a skewness = 0 |
|
782 | + * A distribution with a tail off towards the high end of the scale |
|
783 | + * (positive skew) has a skewness > 0 |
|
784 | + * A distribution with a tail off towards the low end of the scale |
|
785 | + * (negative skew) has a skewness < 0 |
|
786 | + * Handles cummulative data sets correctly |
|
787 | + * |
|
788 | + * @access public |
|
789 | + * @return mixed the skewness value on success, a PEAR_Error object otherwise |
|
790 | + * @see __sumdiff() |
|
791 | + * @see count() |
|
792 | + * @see stDev() |
|
793 | + * @see calc() |
|
794 | + */ |
|
795 | + function skewness() {/*{{{*/ |
|
796 | + if (!array_key_exists('skewness', $this->_calculatedValues)) { |
|
797 | + $count = $this->count(); |
|
798 | + if (PEAR::isError($count)) { |
|
799 | + return $count; |
|
800 | + } |
|
801 | + $stDev = $this->stDev(); |
|
802 | + if (PEAR::isError($stDev)) { |
|
803 | + return $stDev; |
|
804 | + } |
|
805 | + $sumdiff3 = $this->__sumdiff(3); |
|
806 | + if (PEAR::isError($sumdiff3)) { |
|
807 | + return $sumdiff3; |
|
808 | + } |
|
809 | + $this->_calculatedValues['skewness'] = ($sumdiff3 / ($count * pow($stDev, 3))); |
|
810 | + } |
|
811 | + return $this->_calculatedValues['skewness']; |
|
812 | + }/*}}}*/ |
|
813 | + |
|
814 | + /** |
|
815 | + * Calculates the kurtosis of the data distribution in the set |
|
816 | + * The kurtosis measures the degrees of peakedness of a distribution. |
|
817 | + * It is also called the "excess" or "excess coefficient", and is |
|
818 | + * a normalized form of the fourth central moment of a distribution. |
|
819 | + * A normal distributions has kurtosis = 0 |
|
820 | + * A narrow and peaked (leptokurtic) distribution has a |
|
821 | + * kurtosis > 0 |
|
822 | + * A flat and wide (platykurtic) distribution has a kurtosis < 0 |
|
823 | + * Handles cummulative data sets correctly |
|
824 | + * |
|
825 | + * @access public |
|
826 | + * @return mixed the kurtosis value on success, a PEAR_Error object otherwise |
|
827 | + * @see __sumdiff() |
|
828 | + * @see count() |
|
829 | + * @see stDev() |
|
830 | + * @see calc() |
|
831 | + */ |
|
832 | + function kurtosis() {/*{{{*/ |
|
833 | + if (!array_key_exists('kurtosis', $this->_calculatedValues)) { |
|
834 | + $count = $this->count(); |
|
835 | + if (PEAR::isError($count)) { |
|
836 | + return $count; |
|
837 | + } |
|
838 | + $stDev = $this->stDev(); |
|
839 | + if (PEAR::isError($stDev)) { |
|
840 | + return $stDev; |
|
841 | + } |
|
842 | + $sumdiff4 = $this->__sumdiff(4); |
|
843 | + if (PEAR::isError($sumdiff4)) { |
|
844 | + return $sumdiff4; |
|
845 | + } |
|
846 | + $this->_calculatedValues['kurtosis'] = ($sumdiff4 / ($count * pow($stDev, 4))) - 3; |
|
847 | + } |
|
848 | + return $this->_calculatedValues['kurtosis']; |
|
849 | + }/*}}}*/ |
|
850 | + |
|
851 | + /** |
|
852 | + * Calculates the median of a data set. |
|
853 | + * The median is the value such that half of the points are below it |
|
854 | + * in a sorted data set. |
|
855 | + * If the number of values is odd, it is the middle item. |
|
856 | + * If the number of values is even, is the average of the two middle items. |
|
857 | + * Handles cummulative data sets correctly |
|
858 | + * |
|
859 | + * @access public |
|
860 | + * @return mixed the median value on success, a PEAR_Error object otherwise |
|
861 | + * @see count() |
|
862 | + * @see calc() |
|
863 | + */ |
|
864 | + function median() {/*{{{*/ |
|
865 | + if ($this->_data == null) { |
|
866 | + return PEAR::raiseError('data has not been set'); |
|
867 | + } |
|
868 | + if (!array_key_exists('median', $this->_calculatedValues)) { |
|
869 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
870 | + $arr =& $this->_dataExpanded; |
|
871 | + } else { |
|
872 | + $arr =& $this->_data; |
|
873 | + } |
|
874 | + $n = $this->count(); |
|
875 | + if (PEAR::isError($n)) { |
|
876 | + return $n; |
|
877 | + } |
|
878 | + $h = intval($n / 2); |
|
879 | + if ($n % 2 == 0) { |
|
880 | + $median = ($arr[$h] + $arr[$h - 1]) / 2; |
|
881 | + } else { |
|
882 | + $median = $arr[$h + 1]; |
|
883 | + } |
|
884 | + $this->_calculatedValues['median'] = $median; |
|
885 | + } |
|
886 | + return $this->_calculatedValues['median']; |
|
887 | + }/*}}}*/ |
|
888 | + |
|
889 | + /** |
|
890 | + * Calculates the mode of a data set. |
|
891 | + * The mode is the value with the highest frequency in the data set. |
|
892 | + * There can be more than one mode. |
|
893 | + * Handles cummulative data sets correctly |
|
894 | + * |
|
895 | + * @access public |
|
896 | + * @return mixed an array of mode value on success, a PEAR_Error object otherwise |
|
897 | + * @see frequency() |
|
898 | + * @see calc() |
|
899 | + */ |
|
900 | + function mode() {/*{{{*/ |
|
901 | + if ($this->_data == null) { |
|
902 | + return PEAR::raiseError('data has not been set'); |
|
903 | + } |
|
904 | + if (!array_key_exists('mode', $this->_calculatedValues)) { |
|
905 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
906 | + $arr = $this->_data; |
|
907 | + } else { |
|
908 | + $arr = $this->frequency(); |
|
909 | + } |
|
910 | + arsort($arr); |
|
911 | + $mcount = 1; |
|
912 | + foreach ($arr as $val=>$freq) { |
|
913 | + if ($mcount == 1) { |
|
914 | + $mode = array($val); |
|
915 | + $mfreq = $freq; |
|
916 | + ++$mcount; |
|
917 | + continue; |
|
918 | + } |
|
919 | + if ($mfreq == $freq) |
|
920 | + $mode[] = $val; |
|
921 | + if ($mfreq > $freq) |
|
922 | + break; |
|
923 | + } |
|
924 | + $this->_calculatedValues['mode'] = $mode; |
|
925 | + } |
|
926 | + return $this->_calculatedValues['mode']; |
|
927 | + }/*}}}*/ |
|
928 | + |
|
929 | + /** |
|
930 | + * Calculates the midrange of a data set. |
|
931 | + * The midrange is the average of the minimum and maximum of the data set. |
|
932 | + * Handles cummulative data sets correctly |
|
933 | + * |
|
934 | + * @access public |
|
935 | + * @return mixed the midrange value on success, a PEAR_Error object otherwise |
|
936 | + * @see min() |
|
937 | + * @see max() |
|
938 | + * @see calc() |
|
939 | + */ |
|
940 | + function midrange() {/*{{{*/ |
|
941 | + if (!array_key_exists('midrange', $this->_calculatedValues)) { |
|
942 | + $min = $this->min(); |
|
943 | + if (PEAR::isError($min)) { |
|
944 | + return $min; |
|
945 | + } |
|
946 | + $max = $this->max(); |
|
947 | + if (PEAR::isError($max)) { |
|
948 | + return $max; |
|
949 | + } |
|
950 | + $this->_calculatedValues['midrange'] = (($max + $min) / 2); |
|
951 | + } |
|
952 | + return $this->_calculatedValues['midrange']; |
|
953 | + }/*}}}*/ |
|
954 | + |
|
955 | + /** |
|
956 | + * Calculates the geometrical mean of the data points in the set |
|
957 | + * Handles cummulative data sets correctly |
|
958 | + * |
|
959 | + * @access public |
|
960 | + * @return mixed the geometrical mean value on success, a PEAR_Error object otherwise |
|
961 | + * @see calc() |
|
962 | + * @see product() |
|
963 | + * @see count() |
|
964 | + */ |
|
965 | + function geometricMean() {/*{{{*/ |
|
966 | + if (!array_key_exists('geometricMean', $this->_calculatedValues)) { |
|
967 | + $count = $this->count(); |
|
968 | + if (PEAR::isError($count)) { |
|
969 | + return $count; |
|
970 | + } |
|
971 | + $prod = $this->product(); |
|
972 | + if (PEAR::isError($prod)) { |
|
973 | + return $prod; |
|
974 | + } |
|
975 | + if ($prod == 0.0) { |
|
976 | + return 0.0; |
|
977 | + } |
|
978 | + if ($prod < 0) { |
|
979 | + return PEAR::raiseError('The product of the data set is negative, geometric mean undefined.'); |
|
980 | + } |
|
981 | + $this->_calculatedValues['geometricMean'] = pow($prod , 1 / $count); |
|
982 | + } |
|
983 | + return $this->_calculatedValues['geometricMean']; |
|
984 | + }/*}}}*/ |
|
985 | + |
|
986 | + /** |
|
987 | + * Calculates the harmonic mean of the data points in the set |
|
988 | + * Handles cummulative data sets correctly |
|
989 | + * |
|
990 | + * @access public |
|
991 | + * @return mixed the harmonic mean value on success, a PEAR_Error object otherwise |
|
992 | + * @see calc() |
|
993 | + * @see count() |
|
994 | + */ |
|
995 | + function harmonicMean() {/*{{{*/ |
|
996 | + if ($this->_data == null) { |
|
997 | + return PEAR::raiseError('data has not been set'); |
|
998 | + } |
|
999 | + if (!array_key_exists('harmonicMean', $this->_calculatedValues)) { |
|
1000 | + $count = $this->count(); |
|
1001 | + if (PEAR::isError($count)) { |
|
1002 | + return $count; |
|
1003 | + } |
|
1004 | + $invsum = 0.0; |
|
1005 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1006 | + foreach($this->_data as $val=>$freq) { |
|
1007 | + if ($val == 0) { |
|
1008 | + return PEAR::raiseError('cannot calculate a '. |
|
1009 | + 'harmonic mean with data values of zero.'); |
|
1010 | + } |
|
1011 | + $invsum += $freq / $val; |
|
1012 | + } |
|
1013 | + } else { |
|
1014 | + foreach($this->_data as $val) { |
|
1015 | + if ($val == 0) { |
|
1016 | + return PEAR::raiseError('cannot calculate a '. |
|
1017 | + 'harmonic mean with data values of zero.'); |
|
1018 | + } |
|
1019 | + $invsum += 1 / $val; |
|
1020 | + } |
|
1021 | + } |
|
1022 | + $this->_calculatedValues['harmonicMean'] = $count / $invsum; |
|
1023 | + } |
|
1024 | + return $this->_calculatedValues['harmonicMean']; |
|
1025 | + }/*}}}*/ |
|
1026 | + |
|
1027 | + /** |
|
1028 | + * Calculates the nth central moment (m{n}) of a data set. |
|
1029 | + * |
|
1030 | + * The definition of a sample central moment is: |
|
1031 | + * |
|
1032 | + * m{n} = 1/N * SUM { (xi - avg)^n } |
|
1033 | + * |
|
1034 | + * where: N = sample size, avg = sample mean. |
|
1035 | + * |
|
1036 | + * @access public |
|
1037 | + * @param integer $n moment to calculate |
|
1038 | + * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
|
1039 | + */ |
|
1040 | + function sampleCentralMoment($n) {/*{{{*/ |
|
1041 | + if (!is_int($n) || $n < 1) { |
|
1042 | + return PEAR::isError('moment must be a positive integer >= 1.'); |
|
1043 | + } |
|
1044 | + |
|
1045 | + if ($n == 1) { |
|
1046 | + return 0; |
|
1047 | + } |
|
1048 | + $count = $this->count(); |
|
1049 | + if (PEAR::isError($count)) { |
|
1050 | + return $count; |
|
1051 | + } |
|
1052 | + if ($count == 0) { |
|
1053 | + return PEAR::raiseError("Cannot calculate {$n}th sample moment, ". |
|
1054 | + 'there are zero data entries'); |
|
1055 | + } |
|
1056 | + $sum = $this->__sumdiff($n); |
|
1057 | + if (PEAR::isError($sum)) { |
|
1058 | + return $sum; |
|
1059 | + } |
|
1060 | + return ($sum / $count); |
|
1061 | + }/*}}}*/ |
|
1062 | + |
|
1063 | + /** |
|
1064 | + * Calculates the nth raw moment (m{n}) of a data set. |
|
1065 | + * |
|
1066 | + * The definition of a sample central moment is: |
|
1067 | + * |
|
1068 | + * m{n} = 1/N * SUM { xi^n } |
|
1069 | + * |
|
1070 | + * where: N = sample size, avg = sample mean. |
|
1071 | + * |
|
1072 | + * @access public |
|
1073 | + * @param integer $n moment to calculate |
|
1074 | + * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
|
1075 | + */ |
|
1076 | + function sampleRawMoment($n) {/*{{{*/ |
|
1077 | + if (!is_int($n) || $n < 1) { |
|
1078 | + return PEAR::isError('moment must be a positive integer >= 1.'); |
|
1079 | + } |
|
1080 | + |
|
1081 | + $count = $this->count(); |
|
1082 | + if (PEAR::isError($count)) { |
|
1083 | + return $count; |
|
1084 | + } |
|
1085 | + if ($count == 0) { |
|
1086 | + return PEAR::raiseError("Cannot calculate {$n}th raw moment, ". |
|
1087 | + 'there are zero data entries.'); |
|
1088 | + } |
|
1089 | + $sum = $this->sumN($n); |
|
1090 | + if (PEAR::isError($sum)) { |
|
1091 | + return $sum; |
|
1092 | + } |
|
1093 | + return ($sum / $count); |
|
1094 | + }/*}}}*/ |
|
1095 | + |
|
1096 | + |
|
1097 | + /** |
|
1098 | + * Calculates the coefficient of variation of a data set. |
|
1099 | + * The coefficient of variation measures the spread of a set of data |
|
1100 | + * as a proportion of its mean. It is often expressed as a percentage. |
|
1101 | + * Handles cummulative data sets correctly |
|
1102 | + * |
|
1103 | + * @access public |
|
1104 | + * @return mixed the coefficient of variation on success, a PEAR_Error object otherwise |
|
1105 | + * @see stDev() |
|
1106 | + * @see mean() |
|
1107 | + * @see calc() |
|
1108 | + */ |
|
1109 | + function coeffOfVariation() {/*{{{*/ |
|
1110 | + if (!array_key_exists('coeffOfVariation', $this->_calculatedValues)) { |
|
1111 | + $mean = $this->mean(); |
|
1112 | + if (PEAR::isError($mean)) { |
|
1113 | + return $mean; |
|
1114 | + } |
|
1115 | + if ($mean == 0.0) { |
|
1116 | + return PEAR::raiseError('cannot calculate the coefficient '. |
|
1117 | + 'of variation, mean of sample is zero'); |
|
1118 | + } |
|
1119 | + $stDev = $this->stDev(); |
|
1120 | + if (PEAR::isError($stDev)) { |
|
1121 | + return $stDev; |
|
1122 | + } |
|
1123 | + |
|
1124 | + $this->_calculatedValues['coeffOfVariation'] = $stDev / $mean; |
|
1125 | + } |
|
1126 | + return $this->_calculatedValues['coeffOfVariation']; |
|
1127 | + }/*}}}*/ |
|
1128 | + |
|
1129 | + /** |
|
1130 | + * Calculates the standard error of the mean. |
|
1131 | + * It is the standard deviation of the sampling distribution of |
|
1132 | + * the mean. The formula is: |
|
1133 | + * |
|
1134 | + * S.E. Mean = SD / (N)^(1/2) |
|
1135 | + * |
|
1136 | + * This formula does not assume a normal distribution, and shows |
|
1137 | + * that the size of the standard error of the mean is inversely |
|
1138 | + * proportional to the square root of the sample size. |
|
1139 | + * |
|
1140 | + * @access public |
|
1141 | + * @return mixed the standard error of the mean on success, a PEAR_Error object otherwise |
|
1142 | + * @see stDev() |
|
1143 | + * @see count() |
|
1144 | + * @see calc() |
|
1145 | + */ |
|
1146 | + function stdErrorOfMean() {/*{{{*/ |
|
1147 | + if (!array_key_exists('stdErrorOfMean', $this->_calculatedValues)) { |
|
1148 | + $count = $this->count(); |
|
1149 | + if (PEAR::isError($count)) { |
|
1150 | + return $count; |
|
1151 | + } |
|
1152 | + $stDev = $this->stDev(); |
|
1153 | + if (PEAR::isError($stDev)) { |
|
1154 | + return $stDev; |
|
1155 | + } |
|
1156 | + $this->_calculatedValues['stdErrorOfMean'] = $stDev / sqrt($count); |
|
1157 | + } |
|
1158 | + return $this->_calculatedValues['stdErrorOfMean']; |
|
1159 | + }/*}}}*/ |
|
1160 | + |
|
1161 | + /** |
|
1162 | + * Calculates the value frequency table of a data set. |
|
1163 | + * Handles cummulative data sets correctly |
|
1164 | + * |
|
1165 | + * @access public |
|
1166 | + * @return mixed an associative array of value=>frequency items on success, a PEAR_Error object otherwise |
|
1167 | + * @see min() |
|
1168 | + * @see max() |
|
1169 | + * @see calc() |
|
1170 | + */ |
|
1171 | + function frequency() {/*{{{*/ |
|
1172 | + if ($this->_data == null) { |
|
1173 | + return PEAR::raiseError('data has not been set'); |
|
1174 | + } |
|
1175 | + if (!array_key_exists('frequency', $this->_calculatedValues)) { |
|
1176 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1177 | + $freq = $this->_data; |
|
1178 | + } else { |
|
1179 | + $freq = array(); |
|
1180 | + foreach ($this->_data as $val) { |
|
1181 | + $freq["$val"]++; |
|
1182 | + } |
|
1183 | + ksort($freq); |
|
1184 | + } |
|
1185 | + $this->_calculatedValues['frequency'] = $freq; |
|
1186 | + } |
|
1187 | + return $this->_calculatedValues['frequency']; |
|
1188 | + }/*}}}*/ |
|
1189 | + |
|
1190 | + /** |
|
1191 | + * The quartiles are defined as the values that divide a sorted |
|
1192 | + * data set into four equal-sized subsets, and correspond to the |
|
1193 | + * 25th, 50th, and 75th percentiles. |
|
1194 | + * |
|
1195 | + * @access public |
|
1196 | + * @return mixed an associative array of quartiles on success, a PEAR_Error otherwise |
|
1197 | + * @see percentile() |
|
1198 | + */ |
|
1199 | + function quartiles() {/*{{{*/ |
|
1200 | + if (!array_key_exists('quartiles', $this->_calculatedValues)) { |
|
1201 | + $q1 = $this->percentile(25); |
|
1202 | + if (PEAR::isError($q1)) { |
|
1203 | + return $q1; |
|
1204 | + } |
|
1205 | + $q2 = $this->percentile(50); |
|
1206 | + if (PEAR::isError($q2)) { |
|
1207 | + return $q2; |
|
1208 | + } |
|
1209 | + $q3 = $this->percentile(75); |
|
1210 | + if (PEAR::isError($q3)) { |
|
1211 | + return $q3; |
|
1212 | + } |
|
1213 | + $this->_calculatedValues['quartiles'] = array ( |
|
1214 | + '25' => $q1, |
|
1215 | + '50' => $q2, |
|
1216 | + '75' => $q3 |
|
1217 | + ); |
|
1218 | + } |
|
1219 | + return $this->_calculatedValues['quartiles']; |
|
1220 | + }/*}}}*/ |
|
1221 | + |
|
1222 | + /** |
|
1223 | + * The interquartile mean is defined as the mean of the values left |
|
1224 | + * after discarding the lower 25% and top 25% ranked values, i.e.: |
|
1225 | + * |
|
1226 | + * interquart mean = mean(<P(25),P(75)>) |
|
1227 | + * |
|
1228 | + * where: P = percentile |
|
1229 | + * |
|
1230 | + * @todo need to double check the equation |
|
1231 | + * @access public |
|
1232 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1233 | + * @see quartiles() |
|
1234 | + */ |
|
1235 | + function interquartileMean() {/*{{{*/ |
|
1236 | + if (!array_key_exists('interquartileMean', $this->_calculatedValues)) { |
|
1237 | + $quart = $this->quartiles(); |
|
1238 | + if (PEAR::isError($quart)) { |
|
1239 | + return $quart; |
|
1240 | + } |
|
1241 | + $q3 = $quart['75']; |
|
1242 | + $q1 = $quart['25']; |
|
1243 | + $sum = 0; |
|
1244 | + $n = 0; |
|
1245 | + foreach ($this->getData(true) as $val) { |
|
1246 | + if ($val >= $q1 && $val <= $q3) { |
|
1247 | + $sum += $val; |
|
1248 | + ++$n; |
|
1249 | + } |
|
1250 | + } |
|
1251 | + if ($n == 0) { |
|
1252 | + return PEAR::raiseError('error calculating interquartile mean, '. |
|
1253 | + 'empty interquartile range of values.'); |
|
1254 | + } |
|
1255 | + $this->_calculatedValues['interquartileMean'] = $sum / $n; |
|
1256 | + } |
|
1257 | + return $this->_calculatedValues['interquartileMean']; |
|
1258 | + }/*}}}*/ |
|
1259 | + |
|
1260 | + /** |
|
1261 | + * The interquartile range is the distance between the 75th and 25th |
|
1262 | + * percentiles. Basically the range of the middle 50% of the data set, |
|
1263 | + * and thus is not affected by outliers or extreme values. |
|
1264 | + * |
|
1265 | + * interquart range = P(75) - P(25) |
|
1266 | + * |
|
1267 | + * where: P = percentile |
|
1268 | + * |
|
1269 | + * @access public |
|
1270 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1271 | + * @see quartiles() |
|
1272 | + */ |
|
1273 | + function interquartileRange() {/*{{{*/ |
|
1274 | + if (!array_key_exists('interquartileRange', $this->_calculatedValues)) { |
|
1275 | + $quart = $this->quartiles(); |
|
1276 | + if (PEAR::isError($quart)) { |
|
1277 | + return $quart; |
|
1278 | + } |
|
1279 | + $q3 = $quart['75']; |
|
1280 | + $q1 = $quart['25']; |
|
1281 | + $this->_calculatedValues['interquartileRange'] = $q3 - $q1; |
|
1282 | + } |
|
1283 | + return $this->_calculatedValues['interquartileRange']; |
|
1284 | + }/*}}}*/ |
|
1285 | + |
|
1286 | + /** |
|
1287 | + * The quartile deviation is half of the interquartile range value |
|
1288 | + * |
|
1289 | + * quart dev = (P(75) - P(25)) / 2 |
|
1290 | + * |
|
1291 | + * where: P = percentile |
|
1292 | + * |
|
1293 | + * @access public |
|
1294 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1295 | + * @see quartiles() |
|
1296 | + * @see interquartileRange() |
|
1297 | + */ |
|
1298 | + function quartileDeviation() {/*{{{*/ |
|
1299 | + if (!array_key_exists('quartileDeviation', $this->_calculatedValues)) { |
|
1300 | + $iqr = $this->interquartileRange(); |
|
1301 | + if (PEAR::isError($iqr)) { |
|
1302 | + return $iqr; |
|
1303 | + } |
|
1304 | + $this->_calculatedValues['quartileDeviation'] = $iqr / 2; |
|
1305 | + } |
|
1306 | + return $this->_calculatedValues['quartileDeviation']; |
|
1307 | + }/*}}}*/ |
|
1308 | + |
|
1309 | + /** |
|
1310 | + * The quartile variation coefficient is defines as follows: |
|
1311 | + * |
|
1312 | + * quart var coeff = 100 * (P(75) - P(25)) / (P(75) + P(25)) |
|
1313 | + * |
|
1314 | + * where: P = percentile |
|
1315 | + * |
|
1316 | + * @todo need to double check the equation |
|
1317 | + * @access public |
|
1318 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1319 | + * @see quartiles() |
|
1320 | + */ |
|
1321 | + function quartileVariationCoefficient() {/*{{{*/ |
|
1322 | + if (!array_key_exists('quartileVariationCoefficient', $this->_calculatedValues)) { |
|
1323 | + $quart = $this->quartiles(); |
|
1324 | + if (PEAR::isError($quart)) { |
|
1325 | + return $quart; |
|
1326 | + } |
|
1327 | + $q3 = $quart['75']; |
|
1328 | + $q1 = $quart['25']; |
|
1329 | + $d = $q3 - $q1; |
|
1330 | + $s = $q3 + $q1; |
|
1331 | + $this->_calculatedValues['quartileVariationCoefficient'] = 100 * $d / $s; |
|
1332 | + } |
|
1333 | + return $this->_calculatedValues['quartileVariationCoefficient']; |
|
1334 | + }/*}}}*/ |
|
1335 | + |
|
1336 | + /** |
|
1337 | + * The quartile skewness coefficient (also known as Bowley Skewness), |
|
1338 | + * is defined as follows: |
|
1339 | + * |
|
1340 | + * quart skewness coeff = (P(25) - 2*P(50) + P(75)) / (P(75) - P(25)) |
|
1341 | + * |
|
1342 | + * where: P = percentile |
|
1343 | + * |
|
1344 | + * @todo need to double check the equation |
|
1345 | + * @access public |
|
1346 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1347 | + * @see quartiles() |
|
1348 | + */ |
|
1349 | + function quartileSkewnessCoefficient() {/*{{{*/ |
|
1350 | + if (!array_key_exists('quartileSkewnessCoefficient', $this->_calculatedValues)) { |
|
1351 | + $quart = $this->quartiles(); |
|
1352 | + if (PEAR::isError($quart)) { |
|
1353 | + return $quart; |
|
1354 | + } |
|
1355 | + $q3 = $quart['75']; |
|
1356 | + $q2 = $quart['50']; |
|
1357 | + $q1 = $quart['25']; |
|
1358 | + $d = $q3 - 2*$q2 + $q1; |
|
1359 | + $s = $q3 - $q1; |
|
1360 | + $this->_calculatedValues['quartileSkewnessCoefficient'] = $d / $s; |
|
1361 | + } |
|
1362 | + return $this->_calculatedValues['quartileSkewnessCoefficient']; |
|
1363 | + }/*}}}*/ |
|
1364 | + |
|
1365 | + /** |
|
1366 | + * The pth percentile is the value such that p% of the a sorted data set |
|
1367 | + * is smaller than it, and (100 - p)% of the data is larger. |
|
1368 | + * |
|
1369 | + * A quick algorithm to pick the appropriate value from a sorted data |
|
1370 | + * set is as follows: |
|
1371 | + * |
|
1372 | + * - Count the number of values: n |
|
1373 | + * - Calculate the position of the value in the data list: i = p * (n + 1) |
|
1374 | + * - if i is an integer, return the data at that position |
|
1375 | + * - if i < 1, return the minimum of the data set |
|
1376 | + * - if i > n, return the maximum of the data set |
|
1377 | + * - otherwise, average the entries at adjacent positions to i |
|
1378 | + * |
|
1379 | + * The median is the 50th percentile value. |
|
1380 | + * |
|
1381 | + * @todo need to double check generality of the algorithm |
|
1382 | + * |
|
1383 | + * @access public |
|
1384 | + * @param numeric $p the percentile to estimate, e.g. 25 for 25th percentile |
|
1385 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1386 | + * @see quartiles() |
|
1387 | + * @see median() |
|
1388 | + */ |
|
1389 | + function percentile($p) {/*{{{*/ |
|
1390 | + $count = $this->count(); |
|
1391 | + if (PEAR::isError($count)) { |
|
1392 | + return $count; |
|
1393 | + } |
|
1394 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1395 | + $data =& $this->_dataExpanded; |
|
1396 | + } else { |
|
1397 | + $data =& $this->_data; |
|
1398 | + } |
|
1399 | + $obsidx = $p * ($count + 1) / 100; |
|
1400 | + if (intval($obsidx) == $obsidx) { |
|
1401 | + return $data[($obsidx - 1)]; |
|
1402 | + } elseif ($obsidx < 1) { |
|
1403 | + return $data[0]; |
|
1404 | + } elseif ($obsidx > $count) { |
|
1405 | + return $data[($count - 1)]; |
|
1406 | + } else { |
|
1407 | + $left = floor($obsidx - 1); |
|
1408 | + $right = ceil($obsidx - 1); |
|
1409 | + return ($data[$left] + $data[$right]) / 2; |
|
1410 | + } |
|
1411 | + }/*}}}*/ |
|
1412 | + |
|
1413 | + // private methods |
|
1414 | + |
|
1415 | + /** |
|
1416 | + * Utility function to calculate: SUM { (xi - mean)^n } |
|
1417 | + * |
|
1418 | + * @access private |
|
1419 | + * @param numeric $power the exponent |
|
1420 | + * @param optional double $mean the data set mean value |
|
1421 | + * @return mixed the sum on success, a PEAR_Error object otherwise |
|
1422 | + * |
|
1423 | + * @see stDev() |
|
1424 | + * @see variaceWithMean(); |
|
1425 | + * @see skewness(); |
|
1426 | + * @see kurtosis(); |
|
1427 | + */ |
|
1428 | + function __sumdiff($power, $mean=null) {/*{{{*/ |
|
1429 | + if ($this->_data == null) { |
|
1430 | + return PEAR::raiseError('data has not been set'); |
|
1431 | + } |
|
1432 | + if (is_null($mean)) { |
|
1433 | + $mean = $this->mean(); |
|
1434 | + if (PEAR::isError($mean)) { |
|
1435 | + return $mean; |
|
1436 | + } |
|
1437 | + } |
|
1438 | + $sdiff = 0; |
|
1439 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1440 | + foreach ($this->_data as $val=>$freq) { |
|
1441 | + $sdiff += $freq * pow((double)($val - $mean), (double)$power); |
|
1442 | + } |
|
1443 | + } else { |
|
1444 | + foreach ($this->_data as $val) |
|
1445 | + $sdiff += pow((double)($val - $mean), (double)$power); |
|
1446 | + } |
|
1447 | + return $sdiff; |
|
1448 | + }/*}}}*/ |
|
1449 | + |
|
1450 | + /** |
|
1451 | + * Utility function to calculate the variance with or without |
|
1452 | + * a fixed mean |
|
1453 | + * |
|
1454 | + * @access private |
|
1455 | + * @param $mean the fixed mean to use, null as default |
|
1456 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1457 | + * @see variance() |
|
1458 | + * @see varianceWithMean() |
|
1459 | + */ |
|
1460 | + function __calcVariance($mean = null) {/*{{{*/ |
|
1461 | + if ($this->_data == null) { |
|
1462 | + return PEAR::raiseError('data has not been set'); |
|
1463 | + } |
|
1464 | + $sumdiff2 = $this->__sumdiff(2, $mean); |
|
1465 | + if (PEAR::isError($sumdiff2)) { |
|
1466 | + return $sumdiff2; |
|
1467 | + } |
|
1468 | + $count = $this->count(); |
|
1469 | + if (PEAR::isError($count)) { |
|
1470 | + return $count; |
|
1471 | + } |
|
1472 | + if ($count == 1) { |
|
1473 | + return PEAR::raiseError('cannot calculate variance of a singe data point'); |
|
1474 | + } |
|
1475 | + return ($sumdiff2 / ($count - 1)); |
|
1476 | + }/*}}}*/ |
|
1477 | + |
|
1478 | + /** |
|
1479 | + * Utility function to calculate the absolute deviation with or without |
|
1480 | + * a fixed mean |
|
1481 | + * |
|
1482 | + * @access private |
|
1483 | + * @param $mean the fixed mean to use, null as default |
|
1484 | + * @return mixed a numeric value on success, a PEAR_Error otherwise |
|
1485 | + * @see absDev() |
|
1486 | + * @see absDevWithMean() |
|
1487 | + */ |
|
1488 | + function __calcAbsoluteDeviation($mean = null) {/*{{{*/ |
|
1489 | + if ($this->_data == null) { |
|
1490 | + return PEAR::raiseError('data has not been set'); |
|
1491 | + } |
|
1492 | + $count = $this->count(); |
|
1493 | + if (PEAR::isError($count)) { |
|
1494 | + return $count; |
|
1495 | + } |
|
1496 | + $sumabsdev = $this->__sumabsdev($mean); |
|
1497 | + if (PEAR::isError($sumabsdev)) { |
|
1498 | + return $sumabsdev; |
|
1499 | + } |
|
1500 | + return $sumabsdev / $count; |
|
1501 | + }/*}}}*/ |
|
1502 | + |
|
1503 | + /** |
|
1504 | + * Utility function to calculate: SUM { | xi - mean | } |
|
1505 | + * |
|
1506 | + * @access private |
|
1507 | + * @param optional double $mean the mean value for the set or population |
|
1508 | + * @return mixed the sum on success, a PEAR_Error object otherwise |
|
1509 | + * |
|
1510 | + * @see absDev() |
|
1511 | + * @see absDevWithMean() |
|
1512 | + */ |
|
1513 | + function __sumabsdev($mean=null) {/*{{{*/ |
|
1514 | + if ($this->_data == null) { |
|
1515 | + return PEAR::raiseError('data has not been set'); |
|
1516 | + } |
|
1517 | + if (is_null($mean)) { |
|
1518 | + $mean = $this->mean(); |
|
1519 | + } |
|
1520 | + $sdev = 0; |
|
1521 | + if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
|
1522 | + foreach ($this->_data as $val=>$freq) { |
|
1523 | + $sdev += $freq * abs($val - $mean); |
|
1524 | + } |
|
1525 | + } else { |
|
1526 | + foreach ($this->_data as $val) { |
|
1527 | + $sdev += abs($val - $mean); |
|
1528 | + } |
|
1529 | + } |
|
1530 | + return $sdev; |
|
1531 | + }/*}}}*/ |
|
1532 | + |
|
1533 | + /** |
|
1534 | + * Utility function to format a PEAR_Error to be used by calc(), |
|
1535 | + * calcBasic() and calcFull() |
|
1536 | + * |
|
1537 | + * @access private |
|
1538 | + * @param mixed $v value to be formatted |
|
1539 | + * @param boolean $returnErrorObject whether the raw PEAR_Error (when true, default), |
|
1540 | + * or only the error message will be returned (when false) |
|
1541 | + * @return mixed if the value is a PEAR_Error object, and $useErrorObject |
|
1542 | + * is false, then a string with the error message will be returned, |
|
1543 | + * otherwise the value will not be modified and returned as passed. |
|
1544 | + */ |
|
1545 | + function __format($v, $useErrorObject=true) {/*{{{*/ |
|
1546 | + if (PEAR::isError($v) && $useErrorObject == false) { |
|
1547 | + return $v->getMessage(); |
|
1548 | + } else { |
|
1549 | + return $v; |
|
1550 | + } |
|
1551 | + }/*}}}*/ |
|
1552 | + |
|
1553 | + /** |
|
1554 | + * Utility function to validate the data and modify it |
|
1555 | + * according to the current null handling option |
|
1556 | + * |
|
1557 | + * @access private |
|
1558 | + * @return mixed true on success, a PEAR_Error object otherwise |
|
1559 | + * |
|
1560 | + * @see setData() |
|
1561 | + */ |
|
1562 | + function _validate() {/*{{{*/ |
|
1563 | + $flag = ($this->_dataOption == STATS_DATA_CUMMULATIVE); |
|
1564 | + foreach ($this->_data as $key=>$value) { |
|
1565 | + $d = ($flag) ? $key : $value; |
|
1566 | + $v = ($flag) ? $value : $key; |
|
1567 | + if (!is_numeric($d)) { |
|
1568 | + switch ($this->_nullOption) { |
|
1569 | + case STATS_IGNORE_NULL : |
|
1570 | + unset($this->_data["$key"]); |
|
1571 | + break; |
|
1572 | + case STATS_USE_NULL_AS_ZERO: |
|
1573 | + if ($flag) { |
|
1574 | + unset($this->_data["$key"]); |
|
1575 | + $this->_data[0] += $v; |
|
1576 | + } else { |
|
1577 | + $this->_data[$key] = 0; |
|
1578 | + } |
|
1579 | + break; |
|
1580 | + case STATS_REJECT_NULL : |
|
1581 | + default: |
|
1582 | + return PEAR::raiseError('data rejected, contains NULL values'); |
|
1583 | + break; |
|
1584 | + } |
|
1585 | + } |
|
1586 | + } |
|
1587 | + if ($flag) { |
|
1588 | + ksort($this->_data); |
|
1589 | + $this->_dataExpanded = array(); |
|
1590 | + foreach ($this->_data as $val=>$freq) { |
|
1591 | + $this->_dataExpanded = array_pad($this->_dataExpanded, count($this->_dataExpanded) + $freq, $val); |
|
1592 | + } |
|
1593 | + sort($this->_dataExpanded); |
|
1594 | + } else { |
|
1595 | + sort($this->_data); |
|
1596 | + } |
|
1597 | + return true; |
|
1598 | + }/*}}}*/ |
|
1599 | 1599 | |
1600 | 1600 | }/*}}}*/ |
1601 | 1601 |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * @param optional int $nullOption how to handle null values |
174 | 174 | * @return object Math_Stats |
175 | 175 | */ |
176 | - function Math_Stats($nullOption=STATS_REJECT_NULL) {/*{{{*/ |
|
176 | + function Math_Stats($nullOption = STATS_REJECT_NULL) {/*{{{*/ |
|
177 | 177 | $this->_nullOption = $nullOption; |
178 | 178 | }/*}}}*/ |
179 | 179 | |
@@ -186,8 +186,8 @@ discard block |
||
186 | 186 | * @param optional int $opt data format: STATS_DATA_CUMMULATIVE or STATS_DATA_SIMPLE (default) |
187 | 187 | * @return mixed true on success, a PEAR_Error object otherwise |
188 | 188 | */ |
189 | - function setData($arr, $opt=STATS_DATA_SIMPLE) {/*{{{*/ |
|
190 | - if (!is_array($arr)) { |
|
189 | + function setData($arr, $opt = STATS_DATA_SIMPLE) {/*{{{*/ |
|
190 | + if ( ! is_array($arr)) { |
|
191 | 191 | return PEAR::raiseError('invalid data, an array of numeric data was expected'); |
192 | 192 | } |
193 | 193 | $this->_data = null; |
@@ -214,7 +214,7 @@ discard block |
||
214 | 214 | * @return mixed array of data on success, a PEAR_Error object otherwise |
215 | 215 | * @see _validate() |
216 | 216 | */ |
217 | - function getData($expanded=false) {/*{{{*/ |
|
217 | + function getData($expanded = false) {/*{{{*/ |
|
218 | 218 | if ($this->_data == null) { |
219 | 219 | return PEAR::raiseError('data has not been set'); |
220 | 220 | } |
@@ -268,7 +268,7 @@ discard block |
||
268 | 268 | if ($std == 0) { |
269 | 269 | return PEAR::raiseError('cannot studentize data, standard deviation is zero.'); |
270 | 270 | } |
271 | - $arr = array(); |
|
271 | + $arr = array(); |
|
272 | 272 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
273 | 273 | foreach ($this->_data as $val=>$freq) { |
274 | 274 | $newval = ($val - $mean) / $std; |
@@ -297,7 +297,7 @@ discard block |
||
297 | 297 | if (PEAR::isError($mean)) { |
298 | 298 | return $mean; |
299 | 299 | } |
300 | - $arr = array(); |
|
300 | + $arr = array(); |
|
301 | 301 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
302 | 302 | foreach ($this->_data as $val=>$freq) { |
303 | 303 | $newval = $val - $mean; |
@@ -323,7 +323,7 @@ discard block |
||
323 | 323 | * @see calcBasic() |
324 | 324 | * @see calcFull() |
325 | 325 | */ |
326 | - function calc($mode, $returnErrorObject=true) {/*{{{*/ |
|
326 | + function calc($mode, $returnErrorObject = true) {/*{{{*/ |
|
327 | 327 | if ($this->_data == null) { |
328 | 328 | return PEAR::raiseError('data has not been set'); |
329 | 329 | } |
@@ -346,8 +346,8 @@ discard block |
||
346 | 346 | * @see calc() |
347 | 347 | * @see calcFull() |
348 | 348 | */ |
349 | - function calcBasic($returnErrorObject=true) {/*{{{*/ |
|
350 | - return array ( |
|
349 | + function calcBasic($returnErrorObject = true) {/*{{{*/ |
|
350 | + return array( |
|
351 | 351 | 'min' => $this->__format($this->min(), $returnErrorObject), |
352 | 352 | 'max' => $this->__format($this->max(), $returnErrorObject), |
353 | 353 | 'sum' => $this->__format($this->sum(), $returnErrorObject), |
@@ -370,8 +370,8 @@ discard block |
||
370 | 370 | * @see calc() |
371 | 371 | * @see calcBasic() |
372 | 372 | */ |
373 | - function calcFull($returnErrorObject=true) {/*{{{*/ |
|
374 | - return array ( |
|
373 | + function calcFull($returnErrorObject = true) {/*{{{*/ |
|
374 | + return array( |
|
375 | 375 | 'min' => $this->__format($this->min(), $returnErrorObject), |
376 | 376 | 'max' => $this->__format($this->max(), $returnErrorObject), |
377 | 377 | 'sum' => $this->__format($this->sum(), $returnErrorObject), |
@@ -391,14 +391,14 @@ discard block |
||
391 | 391 | 'skewness' => $this->__format($this->skewness(), $returnErrorObject), |
392 | 392 | 'kurtosis' => $this->__format($this->kurtosis(), $returnErrorObject), |
393 | 393 | 'coeff_of_variation' => $this->__format($this->coeffOfVariation(), $returnErrorObject), |
394 | - 'sample_central_moments' => array ( |
|
394 | + 'sample_central_moments' => array( |
|
395 | 395 | 1 => $this->__format($this->sampleCentralMoment(1), $returnErrorObject), |
396 | 396 | 2 => $this->__format($this->sampleCentralMoment(2), $returnErrorObject), |
397 | 397 | 3 => $this->__format($this->sampleCentralMoment(3), $returnErrorObject), |
398 | 398 | 4 => $this->__format($this->sampleCentralMoment(4), $returnErrorObject), |
399 | 399 | 5 => $this->__format($this->sampleCentralMoment(5), $returnErrorObject) |
400 | 400 | ), |
401 | - 'sample_raw_moments' => array ( |
|
401 | + 'sample_raw_moments' => array( |
|
402 | 402 | 1 => $this->__format($this->sampleRawMoment(1), $returnErrorObject), |
403 | 403 | 2 => $this->__format($this->sampleRawMoment(2), $returnErrorObject), |
404 | 404 | 3 => $this->__format($this->sampleRawMoment(3), $returnErrorObject), |
@@ -428,7 +428,7 @@ discard block |
||
428 | 428 | if ($this->_data == null) { |
429 | 429 | return PEAR::raiseError('data has not been set'); |
430 | 430 | } |
431 | - if (!array_key_exists('min', $this->_calculatedValues)) { |
|
431 | + if ( ! array_key_exists('min', $this->_calculatedValues)) { |
|
432 | 432 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
433 | 433 | $min = min(array_keys($this->_data)); |
434 | 434 | } else { |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | if ($this->_data == null) { |
453 | 453 | return PEAR::raiseError('data has not been set'); |
454 | 454 | } |
455 | - if (!array_key_exists('max', $this->_calculatedValues)) { |
|
455 | + if ( ! array_key_exists('max', $this->_calculatedValues)) { |
|
456 | 456 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
457 | 457 | $max = max(array_keys($this->_data)); |
458 | 458 | } else { |
@@ -474,7 +474,7 @@ discard block |
||
474 | 474 | * @see sumN() |
475 | 475 | */ |
476 | 476 | function sum() {/*{{{*/ |
477 | - if (!array_key_exists('sum', $this->_calculatedValues)) { |
|
477 | + if ( ! array_key_exists('sum', $this->_calculatedValues)) { |
|
478 | 478 | $sum = $this->sumN(1); |
479 | 479 | if (PEAR::isError($sum)) { |
480 | 480 | return $sum; |
@@ -496,7 +496,7 @@ discard block |
||
496 | 496 | * @see sumN() |
497 | 497 | */ |
498 | 498 | function sum2() {/*{{{*/ |
499 | - if (!array_key_exists('sum2', $this->_calculatedValues)) { |
|
499 | + if ( ! array_key_exists('sum2', $this->_calculatedValues)) { |
|
500 | 500 | $sum2 = $this->sumN(2); |
501 | 501 | if (PEAR::isError($sum2)) { |
502 | 502 | return $sum2; |
@@ -524,12 +524,12 @@ discard block |
||
524 | 524 | } |
525 | 525 | $sumN = 0; |
526 | 526 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
527 | - foreach($this->_data as $val=>$freq) { |
|
528 | - $sumN += $freq * pow((double)$val, (double)$n); |
|
527 | + foreach ($this->_data as $val=>$freq) { |
|
528 | + $sumN += $freq * pow((double) $val, (double) $n); |
|
529 | 529 | } |
530 | 530 | } else { |
531 | - foreach($this->_data as $val) { |
|
532 | - $sumN += pow((double)$val, (double)$n); |
|
531 | + foreach ($this->_data as $val) { |
|
532 | + $sumN += pow((double) $val, (double) $n); |
|
533 | 533 | } |
534 | 534 | } |
535 | 535 | return $sumN; |
@@ -544,7 +544,7 @@ discard block |
||
544 | 544 | * @see productN() |
545 | 545 | */ |
546 | 546 | function product() {/*{{{*/ |
547 | - if (!array_key_exists('product', $this->_calculatedValues)) { |
|
547 | + if ( ! array_key_exists('product', $this->_calculatedValues)) { |
|
548 | 548 | $product = $this->productN(1); |
549 | 549 | if (PEAR::isError($product)) { |
550 | 550 | return $product; |
@@ -570,18 +570,18 @@ discard block |
||
570 | 570 | } |
571 | 571 | $prodN = 1.0; |
572 | 572 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
573 | - foreach($this->_data as $val=>$freq) { |
|
573 | + foreach ($this->_data as $val=>$freq) { |
|
574 | 574 | if ($val == 0) { |
575 | 575 | return 0.0; |
576 | 576 | } |
577 | - $prodN *= $freq * pow((double)$val, (double)$n); |
|
577 | + $prodN *= $freq * pow((double) $val, (double) $n); |
|
578 | 578 | } |
579 | 579 | } else { |
580 | - foreach($this->_data as $val) { |
|
580 | + foreach ($this->_data as $val) { |
|
581 | 581 | if ($val == 0) { |
582 | 582 | return 0.0; |
583 | 583 | } |
584 | - $prodN *= pow((double)$val, (double)$n); |
|
584 | + $prodN *= pow((double) $val, (double) $n); |
|
585 | 585 | } |
586 | 586 | } |
587 | 587 | return $prodN; |
@@ -600,7 +600,7 @@ discard block |
||
600 | 600 | if ($this->_data == null) { |
601 | 601 | return PEAR::raiseError('data has not been set'); |
602 | 602 | } |
603 | - if (!array_key_exists('count', $this->_calculatedValues)) { |
|
603 | + if ( ! array_key_exists('count', $this->_calculatedValues)) { |
|
604 | 604 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
605 | 605 | $count = count($this->_dataExpanded); |
606 | 606 | } else { |
@@ -622,7 +622,7 @@ discard block |
||
622 | 622 | * @see count() |
623 | 623 | */ |
624 | 624 | function mean() {/*{{{*/ |
625 | - if (!array_key_exists('mean', $this->_calculatedValues)) { |
|
625 | + if ( ! array_key_exists('mean', $this->_calculatedValues)) { |
|
626 | 626 | $sum = $this->sum(); |
627 | 627 | if (PEAR::isError($sum)) { |
628 | 628 | return $sum; |
@@ -643,7 +643,7 @@ discard block |
||
643 | 643 | * @return mixed the value of the range on success, a PEAR_Error object otherwise. |
644 | 644 | */ |
645 | 645 | function range() {/*{{{*/ |
646 | - if (!array_key_exists('range', $this->_calculatedValues)) { |
|
646 | + if ( ! array_key_exists('range', $this->_calculatedValues)) { |
|
647 | 647 | $min = $this->min(); |
648 | 648 | if (PEAR::isError($min)) { |
649 | 649 | return $min; |
@@ -669,7 +669,7 @@ discard block |
||
669 | 669 | * @see count() |
670 | 670 | */ |
671 | 671 | function variance() {/*{{{*/ |
672 | - if (!array_key_exists('variance', $this->_calculatedValues)) { |
|
672 | + if ( ! array_key_exists('variance', $this->_calculatedValues)) { |
|
673 | 673 | $variance = $this->__calcVariance(); |
674 | 674 | if (PEAR::isError($variance)) { |
675 | 675 | return $variance; |
@@ -689,7 +689,7 @@ discard block |
||
689 | 689 | * @see variance() |
690 | 690 | */ |
691 | 691 | function stDev() {/*{{{*/ |
692 | - if (!array_key_exists('stDev', $this->_calculatedValues)) { |
|
692 | + if ( ! array_key_exists('stDev', $this->_calculatedValues)) { |
|
693 | 693 | $variance = $this->variance(); |
694 | 694 | if (PEAR::isError($variance)) { |
695 | 695 | return $variance; |
@@ -748,7 +748,7 @@ discard block |
||
748 | 748 | * @see absDevWithMean() |
749 | 749 | */ |
750 | 750 | function absDev() {/*{{{*/ |
751 | - if (!array_key_exists('absDev', $this->_calculatedValues)) { |
|
751 | + if ( ! array_key_exists('absDev', $this->_calculatedValues)) { |
|
752 | 752 | $absDev = $this->__calcAbsoluteDeviation(); |
753 | 753 | if (PEAR::isError($absdev)) { |
754 | 754 | return $absdev; |
@@ -793,7 +793,7 @@ discard block |
||
793 | 793 | * @see calc() |
794 | 794 | */ |
795 | 795 | function skewness() {/*{{{*/ |
796 | - if (!array_key_exists('skewness', $this->_calculatedValues)) { |
|
796 | + if ( ! array_key_exists('skewness', $this->_calculatedValues)) { |
|
797 | 797 | $count = $this->count(); |
798 | 798 | if (PEAR::isError($count)) { |
799 | 799 | return $count; |
@@ -830,7 +830,7 @@ discard block |
||
830 | 830 | * @see calc() |
831 | 831 | */ |
832 | 832 | function kurtosis() {/*{{{*/ |
833 | - if (!array_key_exists('kurtosis', $this->_calculatedValues)) { |
|
833 | + if ( ! array_key_exists('kurtosis', $this->_calculatedValues)) { |
|
834 | 834 | $count = $this->count(); |
835 | 835 | if (PEAR::isError($count)) { |
836 | 836 | return $count; |
@@ -865,11 +865,11 @@ discard block |
||
865 | 865 | if ($this->_data == null) { |
866 | 866 | return PEAR::raiseError('data has not been set'); |
867 | 867 | } |
868 | - if (!array_key_exists('median', $this->_calculatedValues)) { |
|
868 | + if ( ! array_key_exists('median', $this->_calculatedValues)) { |
|
869 | 869 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
870 | - $arr =& $this->_dataExpanded; |
|
870 | + $arr = & $this->_dataExpanded; |
|
871 | 871 | } else { |
872 | - $arr =& $this->_data; |
|
872 | + $arr = & $this->_data; |
|
873 | 873 | } |
874 | 874 | $n = $this->count(); |
875 | 875 | if (PEAR::isError($n)) { |
@@ -901,7 +901,7 @@ discard block |
||
901 | 901 | if ($this->_data == null) { |
902 | 902 | return PEAR::raiseError('data has not been set'); |
903 | 903 | } |
904 | - if (!array_key_exists('mode', $this->_calculatedValues)) { |
|
904 | + if ( ! array_key_exists('mode', $this->_calculatedValues)) { |
|
905 | 905 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
906 | 906 | $arr = $this->_data; |
907 | 907 | } else { |
@@ -938,7 +938,7 @@ discard block |
||
938 | 938 | * @see calc() |
939 | 939 | */ |
940 | 940 | function midrange() {/*{{{*/ |
941 | - if (!array_key_exists('midrange', $this->_calculatedValues)) { |
|
941 | + if ( ! array_key_exists('midrange', $this->_calculatedValues)) { |
|
942 | 942 | $min = $this->min(); |
943 | 943 | if (PEAR::isError($min)) { |
944 | 944 | return $min; |
@@ -963,7 +963,7 @@ discard block |
||
963 | 963 | * @see count() |
964 | 964 | */ |
965 | 965 | function geometricMean() {/*{{{*/ |
966 | - if (!array_key_exists('geometricMean', $this->_calculatedValues)) { |
|
966 | + if ( ! array_key_exists('geometricMean', $this->_calculatedValues)) { |
|
967 | 967 | $count = $this->count(); |
968 | 968 | if (PEAR::isError($count)) { |
969 | 969 | return $count; |
@@ -978,7 +978,7 @@ discard block |
||
978 | 978 | if ($prod < 0) { |
979 | 979 | return PEAR::raiseError('The product of the data set is negative, geometric mean undefined.'); |
980 | 980 | } |
981 | - $this->_calculatedValues['geometricMean'] = pow($prod , 1 / $count); |
|
981 | + $this->_calculatedValues['geometricMean'] = pow($prod, 1 / $count); |
|
982 | 982 | } |
983 | 983 | return $this->_calculatedValues['geometricMean']; |
984 | 984 | }/*}}}*/ |
@@ -996,14 +996,14 @@ discard block |
||
996 | 996 | if ($this->_data == null) { |
997 | 997 | return PEAR::raiseError('data has not been set'); |
998 | 998 | } |
999 | - if (!array_key_exists('harmonicMean', $this->_calculatedValues)) { |
|
999 | + if ( ! array_key_exists('harmonicMean', $this->_calculatedValues)) { |
|
1000 | 1000 | $count = $this->count(); |
1001 | 1001 | if (PEAR::isError($count)) { |
1002 | 1002 | return $count; |
1003 | 1003 | } |
1004 | 1004 | $invsum = 0.0; |
1005 | 1005 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
1006 | - foreach($this->_data as $val=>$freq) { |
|
1006 | + foreach ($this->_data as $val=>$freq) { |
|
1007 | 1007 | if ($val == 0) { |
1008 | 1008 | return PEAR::raiseError('cannot calculate a '. |
1009 | 1009 | 'harmonic mean with data values of zero.'); |
@@ -1011,7 +1011,7 @@ discard block |
||
1011 | 1011 | $invsum += $freq / $val; |
1012 | 1012 | } |
1013 | 1013 | } else { |
1014 | - foreach($this->_data as $val) { |
|
1014 | + foreach ($this->_data as $val) { |
|
1015 | 1015 | if ($val == 0) { |
1016 | 1016 | return PEAR::raiseError('cannot calculate a '. |
1017 | 1017 | 'harmonic mean with data values of zero.'); |
@@ -1038,7 +1038,7 @@ discard block |
||
1038 | 1038 | * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
1039 | 1039 | */ |
1040 | 1040 | function sampleCentralMoment($n) {/*{{{*/ |
1041 | - if (!is_int($n) || $n < 1) { |
|
1041 | + if ( ! is_int($n) || $n < 1) { |
|
1042 | 1042 | return PEAR::isError('moment must be a positive integer >= 1.'); |
1043 | 1043 | } |
1044 | 1044 | |
@@ -1074,7 +1074,7 @@ discard block |
||
1074 | 1074 | * @return mixed the numeric value of the moment on success, PEAR_Error otherwise |
1075 | 1075 | */ |
1076 | 1076 | function sampleRawMoment($n) {/*{{{*/ |
1077 | - if (!is_int($n) || $n < 1) { |
|
1077 | + if ( ! is_int($n) || $n < 1) { |
|
1078 | 1078 | return PEAR::isError('moment must be a positive integer >= 1.'); |
1079 | 1079 | } |
1080 | 1080 | |
@@ -1107,7 +1107,7 @@ discard block |
||
1107 | 1107 | * @see calc() |
1108 | 1108 | */ |
1109 | 1109 | function coeffOfVariation() {/*{{{*/ |
1110 | - if (!array_key_exists('coeffOfVariation', $this->_calculatedValues)) { |
|
1110 | + if ( ! array_key_exists('coeffOfVariation', $this->_calculatedValues)) { |
|
1111 | 1111 | $mean = $this->mean(); |
1112 | 1112 | if (PEAR::isError($mean)) { |
1113 | 1113 | return $mean; |
@@ -1144,7 +1144,7 @@ discard block |
||
1144 | 1144 | * @see calc() |
1145 | 1145 | */ |
1146 | 1146 | function stdErrorOfMean() {/*{{{*/ |
1147 | - if (!array_key_exists('stdErrorOfMean', $this->_calculatedValues)) { |
|
1147 | + if ( ! array_key_exists('stdErrorOfMean', $this->_calculatedValues)) { |
|
1148 | 1148 | $count = $this->count(); |
1149 | 1149 | if (PEAR::isError($count)) { |
1150 | 1150 | return $count; |
@@ -1172,7 +1172,7 @@ discard block |
||
1172 | 1172 | if ($this->_data == null) { |
1173 | 1173 | return PEAR::raiseError('data has not been set'); |
1174 | 1174 | } |
1175 | - if (!array_key_exists('frequency', $this->_calculatedValues)) { |
|
1175 | + if ( ! array_key_exists('frequency', $this->_calculatedValues)) { |
|
1176 | 1176 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
1177 | 1177 | $freq = $this->_data; |
1178 | 1178 | } else { |
@@ -1197,7 +1197,7 @@ discard block |
||
1197 | 1197 | * @see percentile() |
1198 | 1198 | */ |
1199 | 1199 | function quartiles() {/*{{{*/ |
1200 | - if (!array_key_exists('quartiles', $this->_calculatedValues)) { |
|
1200 | + if ( ! array_key_exists('quartiles', $this->_calculatedValues)) { |
|
1201 | 1201 | $q1 = $this->percentile(25); |
1202 | 1202 | if (PEAR::isError($q1)) { |
1203 | 1203 | return $q1; |
@@ -1210,7 +1210,7 @@ discard block |
||
1210 | 1210 | if (PEAR::isError($q3)) { |
1211 | 1211 | return $q3; |
1212 | 1212 | } |
1213 | - $this->_calculatedValues['quartiles'] = array ( |
|
1213 | + $this->_calculatedValues['quartiles'] = array( |
|
1214 | 1214 | '25' => $q1, |
1215 | 1215 | '50' => $q2, |
1216 | 1216 | '75' => $q3 |
@@ -1233,7 +1233,7 @@ discard block |
||
1233 | 1233 | * @see quartiles() |
1234 | 1234 | */ |
1235 | 1235 | function interquartileMean() {/*{{{*/ |
1236 | - if (!array_key_exists('interquartileMean', $this->_calculatedValues)) { |
|
1236 | + if ( ! array_key_exists('interquartileMean', $this->_calculatedValues)) { |
|
1237 | 1237 | $quart = $this->quartiles(); |
1238 | 1238 | if (PEAR::isError($quart)) { |
1239 | 1239 | return $quart; |
@@ -1271,7 +1271,7 @@ discard block |
||
1271 | 1271 | * @see quartiles() |
1272 | 1272 | */ |
1273 | 1273 | function interquartileRange() {/*{{{*/ |
1274 | - if (!array_key_exists('interquartileRange', $this->_calculatedValues)) { |
|
1274 | + if ( ! array_key_exists('interquartileRange', $this->_calculatedValues)) { |
|
1275 | 1275 | $quart = $this->quartiles(); |
1276 | 1276 | if (PEAR::isError($quart)) { |
1277 | 1277 | return $quart; |
@@ -1296,7 +1296,7 @@ discard block |
||
1296 | 1296 | * @see interquartileRange() |
1297 | 1297 | */ |
1298 | 1298 | function quartileDeviation() {/*{{{*/ |
1299 | - if (!array_key_exists('quartileDeviation', $this->_calculatedValues)) { |
|
1299 | + if ( ! array_key_exists('quartileDeviation', $this->_calculatedValues)) { |
|
1300 | 1300 | $iqr = $this->interquartileRange(); |
1301 | 1301 | if (PEAR::isError($iqr)) { |
1302 | 1302 | return $iqr; |
@@ -1319,7 +1319,7 @@ discard block |
||
1319 | 1319 | * @see quartiles() |
1320 | 1320 | */ |
1321 | 1321 | function quartileVariationCoefficient() {/*{{{*/ |
1322 | - if (!array_key_exists('quartileVariationCoefficient', $this->_calculatedValues)) { |
|
1322 | + if ( ! array_key_exists('quartileVariationCoefficient', $this->_calculatedValues)) { |
|
1323 | 1323 | $quart = $this->quartiles(); |
1324 | 1324 | if (PEAR::isError($quart)) { |
1325 | 1325 | return $quart; |
@@ -1347,7 +1347,7 @@ discard block |
||
1347 | 1347 | * @see quartiles() |
1348 | 1348 | */ |
1349 | 1349 | function quartileSkewnessCoefficient() {/*{{{*/ |
1350 | - if (!array_key_exists('quartileSkewnessCoefficient', $this->_calculatedValues)) { |
|
1350 | + if ( ! array_key_exists('quartileSkewnessCoefficient', $this->_calculatedValues)) { |
|
1351 | 1351 | $quart = $this->quartiles(); |
1352 | 1352 | if (PEAR::isError($quart)) { |
1353 | 1353 | return $quart; |
@@ -1355,7 +1355,7 @@ discard block |
||
1355 | 1355 | $q3 = $quart['75']; |
1356 | 1356 | $q2 = $quart['50']; |
1357 | 1357 | $q1 = $quart['25']; |
1358 | - $d = $q3 - 2*$q2 + $q1; |
|
1358 | + $d = $q3 - 2 * $q2 + $q1; |
|
1359 | 1359 | $s = $q3 - $q1; |
1360 | 1360 | $this->_calculatedValues['quartileSkewnessCoefficient'] = $d / $s; |
1361 | 1361 | } |
@@ -1392,9 +1392,9 @@ discard block |
||
1392 | 1392 | return $count; |
1393 | 1393 | } |
1394 | 1394 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
1395 | - $data =& $this->_dataExpanded; |
|
1395 | + $data = & $this->_dataExpanded; |
|
1396 | 1396 | } else { |
1397 | - $data =& $this->_data; |
|
1397 | + $data = & $this->_data; |
|
1398 | 1398 | } |
1399 | 1399 | $obsidx = $p * ($count + 1) / 100; |
1400 | 1400 | if (intval($obsidx) == $obsidx) { |
@@ -1425,7 +1425,7 @@ discard block |
||
1425 | 1425 | * @see skewness(); |
1426 | 1426 | * @see kurtosis(); |
1427 | 1427 | */ |
1428 | - function __sumdiff($power, $mean=null) {/*{{{*/ |
|
1428 | + function __sumdiff($power, $mean = null) {/*{{{*/ |
|
1429 | 1429 | if ($this->_data == null) { |
1430 | 1430 | return PEAR::raiseError('data has not been set'); |
1431 | 1431 | } |
@@ -1438,11 +1438,11 @@ discard block |
||
1438 | 1438 | $sdiff = 0; |
1439 | 1439 | if ($this->_dataOption == STATS_DATA_CUMMULATIVE) { |
1440 | 1440 | foreach ($this->_data as $val=>$freq) { |
1441 | - $sdiff += $freq * pow((double)($val - $mean), (double)$power); |
|
1441 | + $sdiff += $freq * pow((double) ($val - $mean), (double) $power); |
|
1442 | 1442 | } |
1443 | 1443 | } else { |
1444 | 1444 | foreach ($this->_data as $val) |
1445 | - $sdiff += pow((double)($val - $mean), (double)$power); |
|
1445 | + $sdiff += pow((double) ($val - $mean), (double) $power); |
|
1446 | 1446 | } |
1447 | 1447 | return $sdiff; |
1448 | 1448 | }/*}}}*/ |
@@ -1510,7 +1510,7 @@ discard block |
||
1510 | 1510 | * @see absDev() |
1511 | 1511 | * @see absDevWithMean() |
1512 | 1512 | */ |
1513 | - function __sumabsdev($mean=null) {/*{{{*/ |
|
1513 | + function __sumabsdev($mean = null) {/*{{{*/ |
|
1514 | 1514 | if ($this->_data == null) { |
1515 | 1515 | return PEAR::raiseError('data has not been set'); |
1516 | 1516 | } |
@@ -1542,7 +1542,7 @@ discard block |
||
1542 | 1542 | * is false, then a string with the error message will be returned, |
1543 | 1543 | * otherwise the value will not be modified and returned as passed. |
1544 | 1544 | */ |
1545 | - function __format($v, $useErrorObject=true) {/*{{{*/ |
|
1545 | + function __format($v, $useErrorObject = true) {/*{{{*/ |
|
1546 | 1546 | if (PEAR::isError($v) && $useErrorObject == false) { |
1547 | 1547 | return $v->getMessage(); |
1548 | 1548 | } else { |
@@ -1564,7 +1564,7 @@ discard block |
||
1564 | 1564 | foreach ($this->_data as $key=>$value) { |
1565 | 1565 | $d = ($flag) ? $key : $value; |
1566 | 1566 | $v = ($flag) ? $value : $key; |
1567 | - if (!is_numeric($d)) { |
|
1567 | + if ( ! is_numeric($d)) { |
|
1568 | 1568 | switch ($this->_nullOption) { |
1569 | 1569 | case STATS_IGNORE_NULL : |
1570 | 1570 | unset($this->_data["$key"]); |
@@ -916,10 +916,12 @@ discard block |
||
916 | 916 | ++$mcount; |
917 | 917 | continue; |
918 | 918 | } |
919 | - if ($mfreq == $freq) |
|
920 | - $mode[] = $val; |
|
921 | - if ($mfreq > $freq) |
|
922 | - break; |
|
919 | + if ($mfreq == $freq) { |
|
920 | + $mode[] = $val; |
|
921 | + } |
|
922 | + if ($mfreq > $freq) { |
|
923 | + break; |
|
924 | + } |
|
923 | 925 | } |
924 | 926 | $this->_calculatedValues['mode'] = $mode; |
925 | 927 | } |
@@ -1441,8 +1443,9 @@ discard block |
||
1441 | 1443 | $sdiff += $freq * pow((double)($val - $mean), (double)$power); |
1442 | 1444 | } |
1443 | 1445 | } else { |
1444 | - foreach ($this->_data as $val) |
|
1445 | - $sdiff += pow((double)($val - $mean), (double)$power); |
|
1446 | + foreach ($this->_data as $val) { |
|
1447 | + $sdiff += pow((double)($val - $mean), (double)$power); |
|
1448 | + } |
|
1446 | 1449 | } |
1447 | 1450 | return $sdiff; |
1448 | 1451 | }/*}}}*/ |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | /** |
58 | 58 | * LU Decomposition constructor. |
59 | 59 | * |
60 | - * @param $A Rectangular matrix |
|
60 | + * @param PHPExcel_Shared_JAMA_Matrix $A Rectangular matrix |
|
61 | 61 | * @return Structure to access L, U and piv. |
62 | 62 | */ |
63 | 63 | public function __construct($A) { |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | /** |
124 | 124 | * Get lower triangular factor. |
125 | 125 | * |
126 | - * @return array Lower triangular factor |
|
126 | + * @return PHPExcel_Shared_JAMA_Matrix Lower triangular factor |
|
127 | 127 | */ |
128 | 128 | public function getL() { |
129 | 129 | for ($i = 0; $i < $this->m; ++$i) { |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | /** |
145 | 145 | * Get upper triangular factor. |
146 | 146 | * |
147 | - * @return array Upper triangular factor |
|
147 | + * @return PHPExcel_Shared_JAMA_Matrix Upper triangular factor |
|
148 | 148 | */ |
149 | 149 | public function getU() { |
150 | 150 | for ($i = 0; $i < $this->n; ++$i) { |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | /** |
184 | 184 | * Is the matrix nonsingular? |
185 | 185 | * |
186 | - * @return true if U, and hence A, is nonsingular. |
|
186 | + * @return boolean if U, and hence A, is nonsingular. |
|
187 | 187 | */ |
188 | 188 | public function isNonsingular() { |
189 | 189 | for ($j = 0; $j < $this->n; ++$j) { |
@@ -20,8 +20,8 @@ discard block |
||
20 | 20 | */ |
21 | 21 | class PHPExcel_Shared_JAMA_LUDecomposition { |
22 | 22 | |
23 | - const MatrixSingularException = "Can only perform operation on singular matrix."; |
|
24 | - const MatrixSquareException = "Mismatched Row dimension"; |
|
23 | + const MatrixSingularException = "Can only perform operation on singular matrix."; |
|
24 | + const MatrixSquareException = "Mismatched Row dimension"; |
|
25 | 25 | |
26 | 26 | /** |
27 | 27 | * Decomposition storage |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | for ($i = 0; $i < $this->m; ++$i) { |
83 | 83 | $LUrowi = $this->LU[$i]; |
84 | 84 | // Most of the time is spent in the following dot product. |
85 | - $kmax = min($i,$j); |
|
85 | + $kmax = min($i, $j); |
|
86 | 86 | $s = 0.0; |
87 | 87 | for ($k = 0; $k < $kmax; ++$k) { |
88 | 88 | $s += $LUrowi[$k] * $LUcolj[$k]; |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | } |
92 | 92 | // Find pivot and exchange if necessary. |
93 | 93 | $p = $j; |
94 | - for ($i = $j+1; $i < $this->m; ++$i) { |
|
94 | + for ($i = $j + 1; $i < $this->m; ++$i) { |
|
95 | 95 | if (abs($LUcolj[$i]) > abs($LUcolj[$p])) { |
96 | 96 | $p = $i; |
97 | 97 | } |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | } |
110 | 110 | // Compute multipliers. |
111 | 111 | if (($j < $this->m) && ($this->LU[$j][$j] != 0.0)) { |
112 | - for ($i = $j+1; $i < $this->m; ++$i) { |
|
112 | + for ($i = $j + 1; $i < $this->m; ++$i) { |
|
113 | 113 | $this->LU[$i][$j] /= $this->LU[$j][$j]; |
114 | 114 | } |
115 | 115 | } |
@@ -226,17 +226,17 @@ discard block |
||
226 | 226 | if ($this->isNonsingular()) { |
227 | 227 | // Copy right hand side with pivoting |
228 | 228 | $nx = $B->getColumnDimension(); |
229 | - $X = $B->getMatrix($this->piv, 0, $nx-1); |
|
229 | + $X = $B->getMatrix($this->piv, 0, $nx - 1); |
|
230 | 230 | // Solve L*Y = B(piv,:) |
231 | 231 | for ($k = 0; $k < $this->n; ++$k) { |
232 | - for ($i = $k+1; $i < $this->n; ++$i) { |
|
232 | + for ($i = $k + 1; $i < $this->n; ++$i) { |
|
233 | 233 | for ($j = 0; $j < $nx; ++$j) { |
234 | 234 | $X->A[$i][$j] -= $X->A[$k][$j] * $this->LU[$i][$k]; |
235 | 235 | } |
236 | 236 | } |
237 | 237 | } |
238 | 238 | // Solve U*X = Y; |
239 | - for ($k = $this->n-1; $k >= 0; --$k) { |
|
239 | + for ($k = $this->n - 1; $k >= 0; --$k) { |
|
240 | 240 | for ($j = 0; $j < $nx; ++$j) { |
241 | 241 | $X->A[$k][$j] /= $this->LU[$k][$k]; |
242 | 242 | } |
@@ -162,11 +162,7 @@ discard block |
||
162 | 162 | * getMatrix |
163 | 163 | * |
164 | 164 | * Get a submatrix |
165 | - * @param int $i0 Initial row index |
|
166 | - * @param int $iF Final row index |
|
167 | - * @param int $j0 Initial column index |
|
168 | - * @param int $jF Final column index |
|
169 | - * @return Matrix Submatrix |
|
165 | + * @return PHPExcel_Shared_JAMA_Matrix|null |
|
170 | 166 | */ |
171 | 167 | public function getMatrix() { |
172 | 168 | if (func_num_args() > 0) { |
@@ -304,7 +300,7 @@ discard block |
||
304 | 300 | * Generate an identity matrix. |
305 | 301 | * @param int $m Row dimension |
306 | 302 | * @param int $n Column dimension |
307 | - * @return Matrix Identity matrix |
|
303 | + * @return PHPExcel_Shared_JAMA_Matrix Identity matrix |
|
308 | 304 | */ |
309 | 305 | public function identity($m = null, $n = null) { |
310 | 306 | return $this->diagonal($m, $n, 1); |
@@ -317,8 +313,8 @@ discard block |
||
317 | 313 | * Generate a diagonal matrix |
318 | 314 | * @param int $m Row dimension |
319 | 315 | * @param int $n Column dimension |
320 | - * @param mixed $c Diagonal value |
|
321 | - * @return Matrix Diagonal matrix |
|
316 | + * @param integer $c Diagonal value |
|
317 | + * @return PHPExcel_Shared_JAMA_Matrix Diagonal matrix |
|
322 | 318 | */ |
323 | 319 | public function diagonal($m = null, $n = null, $c = 1) { |
324 | 320 | $R = new PHPExcel_Shared_JAMA_Matrix($m, $n); |
@@ -335,7 +331,7 @@ discard block |
||
335 | 331 | * Get a submatrix by row index/range |
336 | 332 | * @param int $i0 Initial row index |
337 | 333 | * @param int $iF Final row index |
338 | - * @return Matrix Submatrix |
|
334 | + * @return PHPExcel_Shared_JAMA_Matrix|null Submatrix |
|
339 | 335 | */ |
340 | 336 | public function getMatrixByRow($i0 = null, $iF = null) { |
341 | 337 | if (is_int($i0)) { |
@@ -354,9 +350,7 @@ discard block |
||
354 | 350 | * getMatrixByCol |
355 | 351 | * |
356 | 352 | * Get a submatrix by column index/range |
357 | - * @param int $i0 Initial column index |
|
358 | - * @param int $iF Final column index |
|
359 | - * @return Matrix Submatrix |
|
353 | + * @return PHPExcel_Shared_JAMA_Matrix|null |
|
360 | 354 | */ |
361 | 355 | public function getMatrixByCol($j0 = null, $jF = null) { |
362 | 356 | if (is_int($j0)) { |
@@ -375,7 +369,7 @@ discard block |
||
375 | 369 | * transpose |
376 | 370 | * |
377 | 371 | * Tranpose matrix |
378 | - * @return Matrix Transposed matrix |
|
372 | + * @return PHPExcel_Shared_JAMA_Matrix Transposed matrix |
|
379 | 373 | */ |
380 | 374 | public function transpose() { |
381 | 375 | $R = new PHPExcel_Shared_JAMA_Matrix($this->n, $this->m); |
@@ -392,7 +386,7 @@ discard block |
||
392 | 386 | * trace |
393 | 387 | * |
394 | 388 | * Sum of diagonal elements |
395 | - * @return float Sum of diagonal elements |
|
389 | + * @return integer Sum of diagonal elements |
|
396 | 390 | */ |
397 | 391 | public function trace() { |
398 | 392 | $s = 0; |
@@ -418,8 +412,7 @@ discard block |
||
418 | 412 | * plus |
419 | 413 | * |
420 | 414 | * A + B |
421 | - * @param mixed $B Matrix/Array |
|
422 | - * @return Matrix Sum |
|
415 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
423 | 416 | */ |
424 | 417 | public function plus() { |
425 | 418 | if (func_num_args() > 0) { |
@@ -454,8 +447,7 @@ discard block |
||
454 | 447 | * plusEquals |
455 | 448 | * |
456 | 449 | * A = A + B |
457 | - * @param mixed $B Matrix/Array |
|
458 | - * @return Matrix Sum |
|
450 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
459 | 451 | */ |
460 | 452 | public function plusEquals() { |
461 | 453 | if (func_num_args() > 0) { |
@@ -504,8 +496,7 @@ discard block |
||
504 | 496 | * minus |
505 | 497 | * |
506 | 498 | * A - B |
507 | - * @param mixed $B Matrix/Array |
|
508 | - * @return Matrix Sum |
|
499 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
509 | 500 | */ |
510 | 501 | public function minus() { |
511 | 502 | if (func_num_args() > 0) { |
@@ -540,8 +531,7 @@ discard block |
||
540 | 531 | * minusEquals |
541 | 532 | * |
542 | 533 | * A = A - B |
543 | - * @param mixed $B Matrix/Array |
|
544 | - * @return Matrix Sum |
|
534 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
545 | 535 | */ |
546 | 536 | public function minusEquals() { |
547 | 537 | if (func_num_args() > 0) { |
@@ -591,8 +581,7 @@ discard block |
||
591 | 581 | * |
592 | 582 | * Element-by-element multiplication |
593 | 583 | * Cij = Aij * Bij |
594 | - * @param mixed $B Matrix/Array |
|
595 | - * @return Matrix Matrix Cij |
|
584 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
596 | 585 | */ |
597 | 586 | public function arrayTimes() { |
598 | 587 | if (func_num_args() > 0) { |
@@ -628,8 +617,7 @@ discard block |
||
628 | 617 | * |
629 | 618 | * Element-by-element multiplication |
630 | 619 | * Aij = Aij * Bij |
631 | - * @param mixed $B Matrix/Array |
|
632 | - * @return Matrix Matrix Aij |
|
620 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
633 | 621 | */ |
634 | 622 | public function arrayTimesEquals() { |
635 | 623 | if (func_num_args() > 0) { |
@@ -679,8 +667,7 @@ discard block |
||
679 | 667 | * |
680 | 668 | * Element-by-element right division |
681 | 669 | * A / B |
682 | - * @param Matrix $B Matrix B |
|
683 | - * @return Matrix Division result |
|
670 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
684 | 671 | */ |
685 | 672 | public function arrayRightDivide() { |
686 | 673 | if (func_num_args() > 0) { |
@@ -735,8 +722,7 @@ discard block |
||
735 | 722 | * |
736 | 723 | * Element-by-element right division |
737 | 724 | * Aij = Aij / Bij |
738 | - * @param mixed $B Matrix/Array |
|
739 | - * @return Matrix Matrix Aij |
|
725 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
740 | 726 | */ |
741 | 727 | public function arrayRightDivideEquals() { |
742 | 728 | if (func_num_args() > 0) { |
@@ -772,8 +758,7 @@ discard block |
||
772 | 758 | * |
773 | 759 | * Element-by-element Left division |
774 | 760 | * A / B |
775 | - * @param Matrix $B Matrix B |
|
776 | - * @return Matrix Division result |
|
761 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
777 | 762 | */ |
778 | 763 | public function arrayLeftDivide() { |
779 | 764 | if (func_num_args() > 0) { |
@@ -809,8 +794,7 @@ discard block |
||
809 | 794 | * |
810 | 795 | * Element-by-element Left division |
811 | 796 | * Aij = Aij / Bij |
812 | - * @param mixed $B Matrix/Array |
|
813 | - * @return Matrix Matrix Aij |
|
797 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
814 | 798 | */ |
815 | 799 | public function arrayLeftDivideEquals() { |
816 | 800 | if (func_num_args() > 0) { |
@@ -845,8 +829,6 @@ discard block |
||
845 | 829 | * times |
846 | 830 | * |
847 | 831 | * Matrix multiplication |
848 | - * @param mixed $n Matrix/Array/Scalar |
|
849 | - * @return Matrix Product |
|
850 | 832 | */ |
851 | 833 | public function times() { |
852 | 834 | if (func_num_args() > 0) { |
@@ -936,8 +918,7 @@ discard block |
||
936 | 918 | * power |
937 | 919 | * |
938 | 920 | * A = A ^ B |
939 | - * @param mixed $B Matrix/Array |
|
940 | - * @return Matrix Sum |
|
921 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
941 | 922 | */ |
942 | 923 | public function power() { |
943 | 924 | if (func_num_args() > 0) { |
@@ -986,8 +967,7 @@ discard block |
||
986 | 967 | * concat |
987 | 968 | * |
988 | 969 | * A = A & B |
989 | - * @param mixed $B Matrix/Array |
|
990 | - * @return Matrix Sum |
|
970 | + * @return PHPExcel_Shared_JAMA_Matrix |
|
991 | 971 | */ |
992 | 972 | public function concat() { |
993 | 973 | if (func_num_args() > 0) { |
@@ -1020,7 +1000,7 @@ discard block |
||
1020 | 1000 | /** |
1021 | 1001 | * Solve A*X = B. |
1022 | 1002 | * |
1023 | - * @param Matrix $B Right hand side |
|
1003 | + * @param PHPExcel_Shared_JAMA_Matrix $B Right hand side |
|
1024 | 1004 | * @return Matrix ... Solution if A is square, least squares solution otherwise |
1025 | 1005 | */ |
1026 | 1006 | public function solve($B) { |
@@ -4,12 +4,12 @@ discard block |
||
4 | 4 | */ |
5 | 5 | |
6 | 6 | /** PHPExcel root directory */ |
7 | -if (!defined('PHPEXCEL_ROOT')) { |
|
7 | +if ( ! defined('PHPEXCEL_ROOT')) { |
|
8 | 8 | /** |
9 | 9 | * @ignore |
10 | 10 | */ |
11 | - define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../../../'); |
|
12 | - require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php'); |
|
11 | + define('PHPEXCEL_ROOT', dirname(__FILE__).'/../../../'); |
|
12 | + require(PHPEXCEL_ROOT.'PHPExcel/Autoloader.php'); |
|
13 | 13 | } |
14 | 14 | |
15 | 15 | |
@@ -27,11 +27,11 @@ discard block |
||
27 | 27 | class PHPExcel_Shared_JAMA_Matrix { |
28 | 28 | |
29 | 29 | |
30 | - const PolymorphicArgumentException = "Invalid argument pattern for polymorphic function."; |
|
31 | - const ArgumentTypeException = "Invalid argument type."; |
|
32 | - const ArgumentBoundsException = "Invalid argument range."; |
|
33 | - const MatrixDimensionException = "Matrix dimensions are not equal."; |
|
34 | - const ArrayLengthException = "Array length must be a multiple of m."; |
|
30 | + const PolymorphicArgumentException = "Invalid argument pattern for polymorphic function."; |
|
31 | + const ArgumentTypeException = "Invalid argument type."; |
|
32 | + const ArgumentBoundsException = "Invalid argument range."; |
|
33 | + const MatrixDimensionException = "Matrix dimensions are not equal."; |
|
34 | + const ArrayLengthException = "Array length must be a multiple of m."; |
|
35 | 35 | |
36 | 36 | /** |
37 | 37 | * Matrix storage |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | $args = func_get_args(); |
69 | 69 | $match = implode(",", array_map('gettype', $args)); |
70 | 70 | |
71 | - switch($match) { |
|
71 | + switch ($match) { |
|
72 | 72 | //Rectangular matrix - m x n initialized from 2D array |
73 | 73 | case 'array': |
74 | 74 | $this->m = count($args[0]); |
@@ -96,8 +96,8 @@ discard block |
||
96 | 96 | $this->n = 0; |
97 | 97 | } |
98 | 98 | if (($this->m * $this->n) == count($args[0])) { |
99 | - for($i = 0; $i < $this->m; ++$i) { |
|
100 | - for($j = 0; $j < $this->n; ++$j) { |
|
99 | + for ($i = 0; $i < $this->m; ++$i) { |
|
100 | + for ($j = 0; $j < $this->n; ++$j) { |
|
101 | 101 | $this->A[$i][$j] = $args[0][$i + $j * $this->m]; |
102 | 102 | } |
103 | 103 | } |
@@ -173,15 +173,15 @@ discard block |
||
173 | 173 | $args = func_get_args(); |
174 | 174 | $match = implode(",", array_map('gettype', $args)); |
175 | 175 | |
176 | - switch($match) { |
|
176 | + switch ($match) { |
|
177 | 177 | //A($i0...; $j0...) |
178 | 178 | case 'integer,integer': |
179 | 179 | list($i0, $j0) = $args; |
180 | 180 | if ($i0 >= 0) { $m = $this->m - $i0; } else { throw new Exception(self::ArgumentBoundsException); } |
181 | 181 | if ($j0 >= 0) { $n = $this->n - $j0; } else { throw new Exception(self::ArgumentBoundsException); } |
182 | 182 | $R = new PHPExcel_Shared_JAMA_Matrix($m, $n); |
183 | - for($i = $i0; $i < $this->m; ++$i) { |
|
184 | - for($j = $j0; $j < $this->n; ++$j) { |
|
183 | + for ($i = $i0; $i < $this->m; ++$i) { |
|
184 | + for ($j = $j0; $j < $this->n; ++$j) { |
|
185 | 185 | $R->set($i, $j, $this->A[$i][$j]); |
186 | 186 | } |
187 | 187 | } |
@@ -192,9 +192,9 @@ discard block |
||
192 | 192 | list($i0, $iF, $j0, $jF) = $args; |
193 | 193 | if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new Exception(self::ArgumentBoundsException); } |
194 | 194 | if (($jF > $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(self::ArgumentBoundsException); } |
195 | - $R = new PHPExcel_Shared_JAMA_Matrix($m+1, $n+1); |
|
196 | - for($i = $i0; $i <= $iF; ++$i) { |
|
197 | - for($j = $j0; $j <= $jF; ++$j) { |
|
195 | + $R = new PHPExcel_Shared_JAMA_Matrix($m + 1, $n + 1); |
|
196 | + for ($i = $i0; $i <= $iF; ++$i) { |
|
197 | + for ($j = $j0; $j <= $jF; ++$j) { |
|
198 | 198 | $R->set($i - $i0, $j - $j0, $this->A[$i][$j]); |
199 | 199 | } |
200 | 200 | } |
@@ -206,8 +206,8 @@ discard block |
||
206 | 206 | if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(self::ArgumentBoundsException); } |
207 | 207 | if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(self::ArgumentBoundsException); } |
208 | 208 | $R = new PHPExcel_Shared_JAMA_Matrix($m, $n); |
209 | - for($i = 0; $i < $m; ++$i) { |
|
210 | - for($j = 0; $j < $n; ++$j) { |
|
209 | + for ($i = 0; $i < $m; ++$i) { |
|
210 | + for ($j = 0; $j < $n; ++$j) { |
|
211 | 211 | $R->set($i - $i0, $j - $j0, $this->A[$RL[$i]][$CL[$j]]); |
212 | 212 | } |
213 | 213 | } |
@@ -219,8 +219,8 @@ discard block |
||
219 | 219 | if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(self::ArgumentBoundsException); } |
220 | 220 | if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(self::ArgumentBoundsException); } |
221 | 221 | $R = new PHPExcel_Shared_JAMA_Matrix($m, $n); |
222 | - for($i = 0; $i < $m; ++$i) { |
|
223 | - for($j = 0; $j < $n; ++$j) { |
|
222 | + for ($i = 0; $i < $m; ++$i) { |
|
223 | + for ($j = 0; $j < $n; ++$j) { |
|
224 | 224 | $R->set($i, $j, $this->A[$RL[$i]][$CL[$j]]); |
225 | 225 | } |
226 | 226 | } |
@@ -232,8 +232,8 @@ discard block |
||
232 | 232 | if (($iF > $i0) && ($this->m >= $iF) && ($i0 >= 0)) { $m = $iF - $i0; } else { throw new Exception(self::ArgumentBoundsException); } |
233 | 233 | if (count($CL) > 0) { $n = count($CL); } else { throw new Exception(self::ArgumentBoundsException); } |
234 | 234 | $R = new PHPExcel_Shared_JAMA_Matrix($m, $n); |
235 | - for($i = $i0; $i < $iF; ++$i) { |
|
236 | - for($j = 0; $j < $n; ++$j) { |
|
235 | + for ($i = $i0; $i < $iF; ++$i) { |
|
236 | + for ($j = 0; $j < $n; ++$j) { |
|
237 | 237 | $R->set($i - $i0, $j, $this->A[$RL[$i]][$j]); |
238 | 238 | } |
239 | 239 | } |
@@ -244,9 +244,9 @@ discard block |
||
244 | 244 | list($RL, $j0, $jF) = $args; |
245 | 245 | if (count($RL) > 0) { $m = count($RL); } else { throw new Exception(self::ArgumentBoundsException); } |
246 | 246 | if (($jF >= $j0) && ($this->n >= $jF) && ($j0 >= 0)) { $n = $jF - $j0; } else { throw new Exception(self::ArgumentBoundsException); } |
247 | - $R = new PHPExcel_Shared_JAMA_Matrix($m, $n+1); |
|
248 | - for($i = 0; $i < $m; ++$i) { |
|
249 | - for($j = $j0; $j <= $jF; ++$j) { |
|
247 | + $R = new PHPExcel_Shared_JAMA_Matrix($m, $n + 1); |
|
248 | + for ($i = 0; $i < $m; ++$i) { |
|
249 | + for ($j = $j0; $j <= $jF; ++$j) { |
|
250 | 250 | $R->set($i, $j - $j0, $this->A[$RL[$i]][$j]); |
251 | 251 | } |
252 | 252 | } |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | */ |
323 | 323 | public function diagonal($m = null, $n = null, $c = 1) { |
324 | 324 | $R = new PHPExcel_Shared_JAMA_Matrix($m, $n); |
325 | - for($i = 0; $i < $m; ++$i) { |
|
325 | + for ($i = 0; $i < $m; ++$i) { |
|
326 | 326 | $R->set($i, $i, $c); |
327 | 327 | } |
328 | 328 | return $R; |
@@ -379,8 +379,8 @@ discard block |
||
379 | 379 | */ |
380 | 380 | public function transpose() { |
381 | 381 | $R = new PHPExcel_Shared_JAMA_Matrix($this->n, $this->m); |
382 | - for($i = 0; $i < $this->m; ++$i) { |
|
383 | - for($j = 0; $j < $this->n; ++$j) { |
|
382 | + for ($i = 0; $i < $this->m; ++$i) { |
|
383 | + for ($j = 0; $j < $this->n; ++$j) { |
|
384 | 384 | $R->set($j, $i, $this->A[$i][$j]); |
385 | 385 | } |
386 | 386 | } |
@@ -397,7 +397,7 @@ discard block |
||
397 | 397 | public function trace() { |
398 | 398 | $s = 0; |
399 | 399 | $n = min($this->m, $this->n); |
400 | - for($i = 0; $i < $n; ++$i) { |
|
400 | + for ($i = 0; $i < $n; ++$i) { |
|
401 | 401 | $s += $this->A[$i][$i]; |
402 | 402 | } |
403 | 403 | return $s; |
@@ -426,7 +426,7 @@ discard block |
||
426 | 426 | $args = func_get_args(); |
427 | 427 | $match = implode(",", array_map('gettype', $args)); |
428 | 428 | |
429 | - switch($match) { |
|
429 | + switch ($match) { |
|
430 | 430 | case 'object': |
431 | 431 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
432 | 432 | break; |
@@ -438,8 +438,8 @@ discard block |
||
438 | 438 | break; |
439 | 439 | } |
440 | 440 | $this->checkMatrixDimensions($M); |
441 | - for($i = 0; $i < $this->m; ++$i) { |
|
442 | - for($j = 0; $j < $this->n; ++$j) { |
|
441 | + for ($i = 0; $i < $this->m; ++$i) { |
|
442 | + for ($j = 0; $j < $this->n; ++$j) { |
|
443 | 443 | $M->set($i, $j, $M->get($i, $j) + $this->A[$i][$j]); |
444 | 444 | } |
445 | 445 | } |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | $args = func_get_args(); |
463 | 463 | $match = implode(",", array_map('gettype', $args)); |
464 | 464 | |
465 | - switch($match) { |
|
465 | + switch ($match) { |
|
466 | 466 | case 'object': |
467 | 467 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
468 | 468 | break; |
@@ -474,16 +474,16 @@ discard block |
||
474 | 474 | break; |
475 | 475 | } |
476 | 476 | $this->checkMatrixDimensions($M); |
477 | - for($i = 0; $i < $this->m; ++$i) { |
|
478 | - for($j = 0; $j < $this->n; ++$j) { |
|
477 | + for ($i = 0; $i < $this->m; ++$i) { |
|
478 | + for ($j = 0; $j < $this->n; ++$j) { |
|
479 | 479 | $validValues = True; |
480 | 480 | $value = $M->get($i, $j); |
481 | - if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { |
|
482 | - $this->A[$i][$j] = trim($this->A[$i][$j],'"'); |
|
481 | + if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && ( ! is_numeric($this->A[$i][$j]))) { |
|
482 | + $this->A[$i][$j] = trim($this->A[$i][$j], '"'); |
|
483 | 483 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]); |
484 | 484 | } |
485 | - if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { |
|
486 | - $value = trim($value,'"'); |
|
485 | + if ((is_string($value)) && (strlen($value) > 0) && ( ! is_numeric($value))) { |
|
486 | + $value = trim($value, '"'); |
|
487 | 487 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value); |
488 | 488 | } |
489 | 489 | if ($validValues) { |
@@ -512,7 +512,7 @@ discard block |
||
512 | 512 | $args = func_get_args(); |
513 | 513 | $match = implode(",", array_map('gettype', $args)); |
514 | 514 | |
515 | - switch($match) { |
|
515 | + switch ($match) { |
|
516 | 516 | case 'object': |
517 | 517 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
518 | 518 | break; |
@@ -524,8 +524,8 @@ discard block |
||
524 | 524 | break; |
525 | 525 | } |
526 | 526 | $this->checkMatrixDimensions($M); |
527 | - for($i = 0; $i < $this->m; ++$i) { |
|
528 | - for($j = 0; $j < $this->n; ++$j) { |
|
527 | + for ($i = 0; $i < $this->m; ++$i) { |
|
528 | + for ($j = 0; $j < $this->n; ++$j) { |
|
529 | 529 | $M->set($i, $j, $M->get($i, $j) - $this->A[$i][$j]); |
530 | 530 | } |
531 | 531 | } |
@@ -548,7 +548,7 @@ discard block |
||
548 | 548 | $args = func_get_args(); |
549 | 549 | $match = implode(",", array_map('gettype', $args)); |
550 | 550 | |
551 | - switch($match) { |
|
551 | + switch ($match) { |
|
552 | 552 | case 'object': |
553 | 553 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
554 | 554 | break; |
@@ -560,16 +560,16 @@ discard block |
||
560 | 560 | break; |
561 | 561 | } |
562 | 562 | $this->checkMatrixDimensions($M); |
563 | - for($i = 0; $i < $this->m; ++$i) { |
|
564 | - for($j = 0; $j < $this->n; ++$j) { |
|
563 | + for ($i = 0; $i < $this->m; ++$i) { |
|
564 | + for ($j = 0; $j < $this->n; ++$j) { |
|
565 | 565 | $validValues = True; |
566 | 566 | $value = $M->get($i, $j); |
567 | - if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { |
|
568 | - $this->A[$i][$j] = trim($this->A[$i][$j],'"'); |
|
567 | + if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && ( ! is_numeric($this->A[$i][$j]))) { |
|
568 | + $this->A[$i][$j] = trim($this->A[$i][$j], '"'); |
|
569 | 569 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]); |
570 | 570 | } |
571 | - if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { |
|
572 | - $value = trim($value,'"'); |
|
571 | + if ((is_string($value)) && (strlen($value) > 0) && ( ! is_numeric($value))) { |
|
572 | + $value = trim($value, '"'); |
|
573 | 573 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value); |
574 | 574 | } |
575 | 575 | if ($validValues) { |
@@ -599,7 +599,7 @@ discard block |
||
599 | 599 | $args = func_get_args(); |
600 | 600 | $match = implode(",", array_map('gettype', $args)); |
601 | 601 | |
602 | - switch($match) { |
|
602 | + switch ($match) { |
|
603 | 603 | case 'object': |
604 | 604 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
605 | 605 | break; |
@@ -611,8 +611,8 @@ discard block |
||
611 | 611 | break; |
612 | 612 | } |
613 | 613 | $this->checkMatrixDimensions($M); |
614 | - for($i = 0; $i < $this->m; ++$i) { |
|
615 | - for($j = 0; $j < $this->n; ++$j) { |
|
614 | + for ($i = 0; $i < $this->m; ++$i) { |
|
615 | + for ($j = 0; $j < $this->n; ++$j) { |
|
616 | 616 | $M->set($i, $j, $M->get($i, $j) * $this->A[$i][$j]); |
617 | 617 | } |
618 | 618 | } |
@@ -636,7 +636,7 @@ discard block |
||
636 | 636 | $args = func_get_args(); |
637 | 637 | $match = implode(",", array_map('gettype', $args)); |
638 | 638 | |
639 | - switch($match) { |
|
639 | + switch ($match) { |
|
640 | 640 | case 'object': |
641 | 641 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
642 | 642 | break; |
@@ -648,16 +648,16 @@ discard block |
||
648 | 648 | break; |
649 | 649 | } |
650 | 650 | $this->checkMatrixDimensions($M); |
651 | - for($i = 0; $i < $this->m; ++$i) { |
|
652 | - for($j = 0; $j < $this->n; ++$j) { |
|
651 | + for ($i = 0; $i < $this->m; ++$i) { |
|
652 | + for ($j = 0; $j < $this->n; ++$j) { |
|
653 | 653 | $validValues = True; |
654 | 654 | $value = $M->get($i, $j); |
655 | - if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { |
|
656 | - $this->A[$i][$j] = trim($this->A[$i][$j],'"'); |
|
655 | + if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && ( ! is_numeric($this->A[$i][$j]))) { |
|
656 | + $this->A[$i][$j] = trim($this->A[$i][$j], '"'); |
|
657 | 657 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]); |
658 | 658 | } |
659 | - if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { |
|
660 | - $value = trim($value,'"'); |
|
659 | + if ((is_string($value)) && (strlen($value) > 0) && ( ! is_numeric($value))) { |
|
660 | + $value = trim($value, '"'); |
|
661 | 661 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value); |
662 | 662 | } |
663 | 663 | if ($validValues) { |
@@ -687,7 +687,7 @@ discard block |
||
687 | 687 | $args = func_get_args(); |
688 | 688 | $match = implode(",", array_map('gettype', $args)); |
689 | 689 | |
690 | - switch($match) { |
|
690 | + switch ($match) { |
|
691 | 691 | case 'object': |
692 | 692 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
693 | 693 | break; |
@@ -699,16 +699,16 @@ discard block |
||
699 | 699 | break; |
700 | 700 | } |
701 | 701 | $this->checkMatrixDimensions($M); |
702 | - for($i = 0; $i < $this->m; ++$i) { |
|
703 | - for($j = 0; $j < $this->n; ++$j) { |
|
702 | + for ($i = 0; $i < $this->m; ++$i) { |
|
703 | + for ($j = 0; $j < $this->n; ++$j) { |
|
704 | 704 | $validValues = True; |
705 | 705 | $value = $M->get($i, $j); |
706 | - if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { |
|
707 | - $this->A[$i][$j] = trim($this->A[$i][$j],'"'); |
|
706 | + if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && ( ! is_numeric($this->A[$i][$j]))) { |
|
707 | + $this->A[$i][$j] = trim($this->A[$i][$j], '"'); |
|
708 | 708 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]); |
709 | 709 | } |
710 | - if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { |
|
711 | - $value = trim($value,'"'); |
|
710 | + if ((is_string($value)) && (strlen($value) > 0) && ( ! is_numeric($value))) { |
|
711 | + $value = trim($value, '"'); |
|
712 | 712 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value); |
713 | 713 | } |
714 | 714 | if ($validValues) { |
@@ -743,7 +743,7 @@ discard block |
||
743 | 743 | $args = func_get_args(); |
744 | 744 | $match = implode(",", array_map('gettype', $args)); |
745 | 745 | |
746 | - switch($match) { |
|
746 | + switch ($match) { |
|
747 | 747 | case 'object': |
748 | 748 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
749 | 749 | break; |
@@ -755,8 +755,8 @@ discard block |
||
755 | 755 | break; |
756 | 756 | } |
757 | 757 | $this->checkMatrixDimensions($M); |
758 | - for($i = 0; $i < $this->m; ++$i) { |
|
759 | - for($j = 0; $j < $this->n; ++$j) { |
|
758 | + for ($i = 0; $i < $this->m; ++$i) { |
|
759 | + for ($j = 0; $j < $this->n; ++$j) { |
|
760 | 760 | $this->A[$i][$j] = $this->A[$i][$j] / $M->get($i, $j); |
761 | 761 | } |
762 | 762 | } |
@@ -780,7 +780,7 @@ discard block |
||
780 | 780 | $args = func_get_args(); |
781 | 781 | $match = implode(",", array_map('gettype', $args)); |
782 | 782 | |
783 | - switch($match) { |
|
783 | + switch ($match) { |
|
784 | 784 | case 'object': |
785 | 785 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
786 | 786 | break; |
@@ -792,8 +792,8 @@ discard block |
||
792 | 792 | break; |
793 | 793 | } |
794 | 794 | $this->checkMatrixDimensions($M); |
795 | - for($i = 0; $i < $this->m; ++$i) { |
|
796 | - for($j = 0; $j < $this->n; ++$j) { |
|
795 | + for ($i = 0; $i < $this->m; ++$i) { |
|
796 | + for ($j = 0; $j < $this->n; ++$j) { |
|
797 | 797 | $M->set($i, $j, $M->get($i, $j) / $this->A[$i][$j]); |
798 | 798 | } |
799 | 799 | } |
@@ -817,7 +817,7 @@ discard block |
||
817 | 817 | $args = func_get_args(); |
818 | 818 | $match = implode(",", array_map('gettype', $args)); |
819 | 819 | |
820 | - switch($match) { |
|
820 | + switch ($match) { |
|
821 | 821 | case 'object': |
822 | 822 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
823 | 823 | break; |
@@ -829,8 +829,8 @@ discard block |
||
829 | 829 | break; |
830 | 830 | } |
831 | 831 | $this->checkMatrixDimensions($M); |
832 | - for($i = 0; $i < $this->m; ++$i) { |
|
833 | - for($j = 0; $j < $this->n; ++$j) { |
|
832 | + for ($i = 0; $i < $this->m; ++$i) { |
|
833 | + for ($j = 0; $j < $this->n; ++$j) { |
|
834 | 834 | $this->A[$i][$j] = $M->get($i, $j) / $this->A[$i][$j]; |
835 | 835 | } |
836 | 836 | } |
@@ -853,19 +853,19 @@ discard block |
||
853 | 853 | $args = func_get_args(); |
854 | 854 | $match = implode(",", array_map('gettype', $args)); |
855 | 855 | |
856 | - switch($match) { |
|
856 | + switch ($match) { |
|
857 | 857 | case 'object': |
858 | 858 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $B = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
859 | 859 | if ($this->n == $B->m) { |
860 | 860 | $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n); |
861 | - for($j = 0; $j < $B->n; ++$j) { |
|
861 | + for ($j = 0; $j < $B->n; ++$j) { |
|
862 | 862 | for ($k = 0; $k < $this->n; ++$k) { |
863 | 863 | $Bcolj[$k] = $B->A[$k][$j]; |
864 | 864 | } |
865 | - for($i = 0; $i < $this->m; ++$i) { |
|
865 | + for ($i = 0; $i < $this->m; ++$i) { |
|
866 | 866 | $Arowi = $this->A[$i]; |
867 | 867 | $s = 0; |
868 | - for($k = 0; $k < $this->n; ++$k) { |
|
868 | + for ($k = 0; $k < $this->n; ++$k) { |
|
869 | 869 | $s += $Arowi[$k] * $Bcolj[$k]; |
870 | 870 | } |
871 | 871 | $C->A[$i][$j] = $s; |
@@ -880,10 +880,10 @@ discard block |
||
880 | 880 | $B = new PHPExcel_Shared_JAMA_Matrix($args[0]); |
881 | 881 | if ($this->n == $B->m) { |
882 | 882 | $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $B->n); |
883 | - for($i = 0; $i < $C->m; ++$i) { |
|
884 | - for($j = 0; $j < $C->n; ++$j) { |
|
883 | + for ($i = 0; $i < $C->m; ++$i) { |
|
884 | + for ($j = 0; $j < $C->n; ++$j) { |
|
885 | 885 | $s = "0"; |
886 | - for($k = 0; $k < $C->n; ++$k) { |
|
886 | + for ($k = 0; $k < $C->n; ++$k) { |
|
887 | 887 | $s += $this->A[$i][$k] * $B->A[$k][$j]; |
888 | 888 | } |
889 | 889 | $C->A[$i][$j] = $s; |
@@ -897,8 +897,8 @@ discard block |
||
897 | 897 | break; |
898 | 898 | case 'integer': |
899 | 899 | $C = new PHPExcel_Shared_JAMA_Matrix($this->A); |
900 | - for($i = 0; $i < $C->m; ++$i) { |
|
901 | - for($j = 0; $j < $C->n; ++$j) { |
|
900 | + for ($i = 0; $i < $C->m; ++$i) { |
|
901 | + for ($j = 0; $j < $C->n; ++$j) { |
|
902 | 902 | $C->A[$i][$j] *= $args[0]; |
903 | 903 | } |
904 | 904 | } |
@@ -906,8 +906,8 @@ discard block |
||
906 | 906 | break; |
907 | 907 | case 'double': |
908 | 908 | $C = new PHPExcel_Shared_JAMA_Matrix($this->m, $this->n); |
909 | - for($i = 0; $i < $C->m; ++$i) { |
|
910 | - for($j = 0; $j < $C->n; ++$j) { |
|
909 | + for ($i = 0; $i < $C->m; ++$i) { |
|
910 | + for ($j = 0; $j < $C->n; ++$j) { |
|
911 | 911 | $C->A[$i][$j] = $args[0] * $this->A[$i][$j]; |
912 | 912 | } |
913 | 913 | } |
@@ -915,8 +915,8 @@ discard block |
||
915 | 915 | break; |
916 | 916 | case 'float': |
917 | 917 | $C = new PHPExcel_Shared_JAMA_Matrix($this->A); |
918 | - for($i = 0; $i < $C->m; ++$i) { |
|
919 | - for($j = 0; $j < $C->n; ++$j) { |
|
918 | + for ($i = 0; $i < $C->m; ++$i) { |
|
919 | + for ($j = 0; $j < $C->n; ++$j) { |
|
920 | 920 | $C->A[$i][$j] *= $args[0]; |
921 | 921 | } |
922 | 922 | } |
@@ -944,7 +944,7 @@ discard block |
||
944 | 944 | $args = func_get_args(); |
945 | 945 | $match = implode(",", array_map('gettype', $args)); |
946 | 946 | |
947 | - switch($match) { |
|
947 | + switch ($match) { |
|
948 | 948 | case 'object': |
949 | 949 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
950 | 950 | break; |
@@ -956,20 +956,20 @@ discard block |
||
956 | 956 | break; |
957 | 957 | } |
958 | 958 | $this->checkMatrixDimensions($M); |
959 | - for($i = 0; $i < $this->m; ++$i) { |
|
960 | - for($j = 0; $j < $this->n; ++$j) { |
|
959 | + for ($i = 0; $i < $this->m; ++$i) { |
|
960 | + for ($j = 0; $j < $this->n; ++$j) { |
|
961 | 961 | $validValues = True; |
962 | 962 | $value = $M->get($i, $j); |
963 | - if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && (!is_numeric($this->A[$i][$j]))) { |
|
964 | - $this->A[$i][$j] = trim($this->A[$i][$j],'"'); |
|
963 | + if ((is_string($this->A[$i][$j])) && (strlen($this->A[$i][$j]) > 0) && ( ! is_numeric($this->A[$i][$j]))) { |
|
964 | + $this->A[$i][$j] = trim($this->A[$i][$j], '"'); |
|
965 | 965 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($this->A[$i][$j]); |
966 | 966 | } |
967 | - if ((is_string($value)) && (strlen($value) > 0) && (!is_numeric($value))) { |
|
968 | - $value = trim($value,'"'); |
|
967 | + if ((is_string($value)) && (strlen($value) > 0) && ( ! is_numeric($value))) { |
|
968 | + $value = trim($value, '"'); |
|
969 | 969 | $validValues &= PHPExcel_Shared_String::convertToNumberIfFraction($value); |
970 | 970 | } |
971 | 971 | if ($validValues) { |
972 | - $this->A[$i][$j] = pow($this->A[$i][$j],$value); |
|
972 | + $this->A[$i][$j] = pow($this->A[$i][$j], $value); |
|
973 | 973 | } else { |
974 | 974 | $this->A[$i][$j] = PHPExcel_Calculation_Functions::NaN(); |
975 | 975 | } |
@@ -994,7 +994,7 @@ discard block |
||
994 | 994 | $args = func_get_args(); |
995 | 995 | $match = implode(",", array_map('gettype', $args)); |
996 | 996 | |
997 | - switch($match) { |
|
997 | + switch ($match) { |
|
998 | 998 | case 'object': |
999 | 999 | if ($args[0] instanceof PHPExcel_Shared_JAMA_Matrix) { $M = $args[0]; } else { throw new Exception(self::ArgumentTypeException); } |
1000 | 1000 | case 'array': |
@@ -1005,9 +1005,9 @@ discard block |
||
1005 | 1005 | break; |
1006 | 1006 | } |
1007 | 1007 | $this->checkMatrixDimensions($M); |
1008 | - for($i = 0; $i < $this->m; ++$i) { |
|
1009 | - for($j = 0; $j < $this->n; ++$j) { |
|
1010 | - $this->A[$i][$j] = trim($this->A[$i][$j],'"').trim($M->get($i, $j),'"'); |
|
1008 | + for ($i = 0; $i < $this->m; ++$i) { |
|
1009 | + for ($j = 0; $j < $this->n; ++$j) { |
|
1010 | + $this->A[$i][$j] = trim($this->A[$i][$j], '"').trim($M->get($i, $j), '"'); |
|
1011 | 1011 | } |
1012 | 1012 | } |
1013 | 1013 | return $this; |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | /** |
112 | 112 | * Return the Householder vectors |
113 | 113 | * |
114 | - * @return Matrix Lower trapezoidal matrix whose columns define the reflections |
|
114 | + * @return PHPExcel_Shared_JAMA_Matrix Lower trapezoidal matrix whose columns define the reflections |
|
115 | 115 | */ |
116 | 116 | public function getH() { |
117 | 117 | for ($i = 0; $i < $this->m; ++$i) { |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | /** |
131 | 131 | * Return the upper triangular factor |
132 | 132 | * |
133 | - * @return Matrix upper triangular factor |
|
133 | + * @return PHPExcel_Shared_JAMA_Matrix upper triangular factor |
|
134 | 134 | */ |
135 | 135 | public function getR() { |
136 | 136 | for ($i = 0; $i < $this->n; ++$i) { |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | /** |
152 | 152 | * Generate and return the (economy-sized) orthogonal factor |
153 | 153 | * |
154 | - * @return Matrix orthogonal factor |
|
154 | + * @return PHPExcel_Shared_JAMA_Matrix orthogonal factor |
|
155 | 155 | */ |
156 | 156 | public function getQ() { |
157 | 157 | for ($k = $this->n-1; $k >= 0; --$k) { |
@@ -189,7 +189,7 @@ discard block |
||
189 | 189 | * Least squares solution of A*X = B |
190 | 190 | * |
191 | 191 | * @param Matrix $B A Matrix with as many rows as A and any number of columns. |
192 | - * @return Matrix Matrix that minimizes the two norm of Q*R*X-B. |
|
192 | + * @return PHPExcel_Shared_JAMA_Matrix|null Matrix that minimizes the two norm of Q*R*X-B. |
|
193 | 193 | */ |
194 | 194 | public function solve($B) { |
195 | 195 | if ($B->getRowDimension() == $this->m) { |
@@ -33,9 +33,9 @@ |
||
33 | 33 | private $m; |
34 | 34 | |
35 | 35 | /** |
36 | - * Column dimension. |
|
37 | - * @var integer |
|
38 | - */ |
|
36 | + * Column dimension. |
|
37 | + * @var integer |
|
38 | + */ |
|
39 | 39 | private $n; |
40 | 40 | |
41 | 41 | /** |
@@ -18,7 +18,7 @@ discard block |
||
18 | 18 | */ |
19 | 19 | class PHPExcel_Shared_JAMA_QRDecomposition { |
20 | 20 | |
21 | - const MatrixRankException = "Can only perform operation on full-rank matrix."; |
|
21 | + const MatrixRankException = "Can only perform operation on full-rank matrix."; |
|
22 | 22 | |
23 | 23 | /** |
24 | 24 | * Array for internal storage of decomposition. |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | * @return Structure to access R and the Householder vectors and compute Q. |
53 | 53 | */ |
54 | 54 | public function __construct($A) { |
55 | - if($A instanceof PHPExcel_Shared_JAMA_Matrix) { |
|
55 | + if ($A instanceof PHPExcel_Shared_JAMA_Matrix) { |
|
56 | 56 | // Initialize. |
57 | 57 | $this->QR = $A->getArrayCopy(); |
58 | 58 | $this->m = $A->getRowDimension(); |
@@ -74,12 +74,12 @@ discard block |
||
74 | 74 | } |
75 | 75 | $this->QR[$k][$k] += 1.0; |
76 | 76 | // Apply transformation to remaining columns. |
77 | - for ($j = $k+1; $j < $this->n; ++$j) { |
|
77 | + for ($j = $k + 1; $j < $this->n; ++$j) { |
|
78 | 78 | $s = 0.0; |
79 | 79 | for ($i = $k; $i < $this->m; ++$i) { |
80 | 80 | $s += $this->QR[$i][$k] * $this->QR[$i][$j]; |
81 | 81 | } |
82 | - $s = -$s/$this->QR[$k][$k]; |
|
82 | + $s = -$s / $this->QR[$k][$k]; |
|
83 | 83 | for ($i = $k; $i < $this->m; ++$i) { |
84 | 84 | $this->QR[$i][$j] += $s * $this->QR[$i][$k]; |
85 | 85 | } |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | * @return Matrix orthogonal factor |
155 | 155 | */ |
156 | 156 | public function getQ() { |
157 | - for ($k = $this->n-1; $k >= 0; --$k) { |
|
157 | + for ($k = $this->n - 1; $k >= 0; --$k) { |
|
158 | 158 | for ($i = 0; $i < $this->m; ++$i) { |
159 | 159 | $Q[$i][$k] = 0.0; |
160 | 160 | } |
@@ -165,7 +165,7 @@ discard block |
||
165 | 165 | for ($i = $k; $i < $this->m; ++$i) { |
166 | 166 | $s += $this->QR[$i][$k] * $Q[$i][$j]; |
167 | 167 | } |
168 | - $s = -$s/$this->QR[$k][$k]; |
|
168 | + $s = -$s / $this->QR[$k][$k]; |
|
169 | 169 | for ($i = $k; $i < $this->m; ++$i) { |
170 | 170 | $Q[$i][$j] += $s * $this->QR[$i][$k]; |
171 | 171 | } |
@@ -204,25 +204,25 @@ discard block |
||
204 | 204 | for ($i = $k; $i < $this->m; ++$i) { |
205 | 205 | $s += $this->QR[$i][$k] * $X[$i][$j]; |
206 | 206 | } |
207 | - $s = -$s/$this->QR[$k][$k]; |
|
207 | + $s = -$s / $this->QR[$k][$k]; |
|
208 | 208 | for ($i = $k; $i < $this->m; ++$i) { |
209 | 209 | $X[$i][$j] += $s * $this->QR[$i][$k]; |
210 | 210 | } |
211 | 211 | } |
212 | 212 | } |
213 | 213 | // Solve R*X = Y; |
214 | - for ($k = $this->n-1; $k >= 0; --$k) { |
|
214 | + for ($k = $this->n - 1; $k >= 0; --$k) { |
|
215 | 215 | for ($j = 0; $j < $nx; ++$j) { |
216 | 216 | $X[$k][$j] /= $this->Rdiag[$k]; |
217 | 217 | } |
218 | 218 | for ($i = 0; $i < $k; ++$i) { |
219 | 219 | for ($j = 0; $j < $nx; ++$j) { |
220 | - $X[$i][$j] -= $X[$k][$j]* $this->QR[$i][$k]; |
|
220 | + $X[$i][$j] -= $X[$k][$j] * $this->QR[$i][$k]; |
|
221 | 221 | } |
222 | 222 | } |
223 | 223 | } |
224 | 224 | $X = new PHPExcel_Shared_JAMA_Matrix($X); |
225 | - return ($X->getMatrix(0, $this->n-1, 0, $nx)); |
|
225 | + return ($X->getMatrix(0, $this->n - 1, 0, $nx)); |
|
226 | 226 | } else { |
227 | 227 | throw new Exception(self::MatrixRankException); |
228 | 228 | } |
@@ -509,7 +509,7 @@ |
||
509 | 509 | * Effective numerical matrix rank |
510 | 510 | * |
511 | 511 | * @access public |
512 | - * @return Number of nonnegligible singular values. |
|
512 | + * @return integer of nonnegligible singular values. |
|
513 | 513 | */ |
514 | 514 | public function rank() { |
515 | 515 | $eps = pow(2.0, -52.0); |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * @license PHP v3.0 |
18 | 18 | * @version 1.1 |
19 | 19 | */ |
20 | -class SingularValueDecomposition { |
|
20 | +class SingularValueDecomposition { |
|
21 | 21 | |
22 | 22 | /** |
23 | 23 | * Internal storage of U. |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | |
75 | 75 | // Reduce A to bidiagonal form, storing the diagonal elements |
76 | 76 | // in s and the super-diagonal elements in e. |
77 | - for ($k = 0; $k < max($nct,$nrt); ++$k) { |
|
77 | + for ($k = 0; $k < max($nct, $nrt); ++$k) { |
|
78 | 78 | |
79 | 79 | if ($k < $nct) { |
80 | 80 | // Compute the transformation for the k-th column and |
@@ -130,27 +130,27 @@ discard block |
||
130 | 130 | $e[$k] = hypo($e[$k], $e[$i]); |
131 | 131 | } |
132 | 132 | if ($e[$k] != 0.0) { |
133 | - if ($e[$k+1] < 0.0) { |
|
133 | + if ($e[$k + 1] < 0.0) { |
|
134 | 134 | $e[$k] = -$e[$k]; |
135 | 135 | } |
136 | 136 | for ($i = $k + 1; $i < $this->n; ++$i) { |
137 | 137 | $e[$i] /= $e[$k]; |
138 | 138 | } |
139 | - $e[$k+1] += 1.0; |
|
139 | + $e[$k + 1] += 1.0; |
|
140 | 140 | } |
141 | 141 | $e[$k] = -$e[$k]; |
142 | - if (($k+1 < $this->m) AND ($e[$k] != 0.0)) { |
|
142 | + if (($k + 1 < $this->m) AND ($e[$k] != 0.0)) { |
|
143 | 143 | // Apply the transformation. |
144 | - for ($i = $k+1; $i < $this->m; ++$i) { |
|
144 | + for ($i = $k + 1; $i < $this->m; ++$i) { |
|
145 | 145 | $work[$i] = 0.0; |
146 | 146 | } |
147 | - for ($j = $k+1; $j < $this->n; ++$j) { |
|
148 | - for ($i = $k+1; $i < $this->m; ++$i) { |
|
147 | + for ($j = $k + 1; $j < $this->n; ++$j) { |
|
148 | + for ($i = $k + 1; $i < $this->m; ++$i) { |
|
149 | 149 | $work[$i] += $e[$j] * $A[$i][$j]; |
150 | 150 | } |
151 | 151 | } |
152 | 152 | for ($j = $k + 1; $j < $this->n; ++$j) { |
153 | - $t = -$e[$j] / $e[$k+1]; |
|
153 | + $t = -$e[$j] / $e[$k + 1]; |
|
154 | 154 | for ($i = $k + 1; $i < $this->m; ++$i) { |
155 | 155 | $A[$i][$j] += $t * $work[$i]; |
156 | 156 | } |
@@ -172,12 +172,12 @@ discard block |
||
172 | 172 | $this->s[$nct] = $A[$nct][$nct]; |
173 | 173 | } |
174 | 174 | if ($this->m < $p) { |
175 | - $this->s[$p-1] = 0.0; |
|
175 | + $this->s[$p - 1] = 0.0; |
|
176 | 176 | } |
177 | 177 | if ($nrt + 1 < $p) { |
178 | - $e[$nrt] = $A[$nrt][$p-1]; |
|
178 | + $e[$nrt] = $A[$nrt][$p - 1]; |
|
179 | 179 | } |
180 | - $e[$p-1] = 0.0; |
|
180 | + $e[$p - 1] = 0.0; |
|
181 | 181 | // If required, generate U. |
182 | 182 | if ($wantu) { |
183 | 183 | for ($j = $nct; $j < $nu; ++$j) { |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | $this->U[$i][$j] += $t * $this->U[$i][$k]; |
199 | 199 | } |
200 | 200 | } |
201 | - for ($i = $k; $i < $this->m; ++$i ) { |
|
201 | + for ($i = $k; $i < $this->m; ++$i) { |
|
202 | 202 | $this->U[$i][$k] = -$this->U[$i][$k]; |
203 | 203 | } |
204 | 204 | $this->U[$k][$k] = 1.0 + $this->U[$k][$k]; |
@@ -221,9 +221,9 @@ discard block |
||
221 | 221 | for ($j = $k + 1; $j < $nu; ++$j) { |
222 | 222 | $t = 0; |
223 | 223 | for ($i = $k + 1; $i < $this->n; ++$i) { |
224 | - $t += $this->V[$i][$k]* $this->V[$i][$j]; |
|
224 | + $t += $this->V[$i][$k] * $this->V[$i][$j]; |
|
225 | 225 | } |
226 | - $t = -$t / $this->V[$k+1][$k]; |
|
226 | + $t = -$t / $this->V[$k + 1][$k]; |
|
227 | 227 | for ($i = $k + 1; $i < $this->n; ++$i) { |
228 | 228 | $this->V[$i][$j] += $t * $this->V[$i][$k]; |
229 | 229 | } |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | if ($k == -1) { |
256 | 256 | break; |
257 | 257 | } |
258 | - if (abs($e[$k]) <= $eps * (abs($this->s[$k]) + abs($this->s[$k+1]))) { |
|
258 | + if (abs($e[$k]) <= $eps * (abs($this->s[$k]) + abs($this->s[$k + 1]))) { |
|
259 | 259 | $e[$k] = 0.0; |
260 | 260 | break; |
261 | 261 | } |
@@ -267,15 +267,15 @@ discard block |
||
267 | 267 | if ($ks == $k) { |
268 | 268 | break; |
269 | 269 | } |
270 | - $t = ($ks != $p ? abs($e[$ks]) : 0.) + ($ks != $k + 1 ? abs($e[$ks-1]) : 0.); |
|
271 | - if (abs($this->s[$ks]) <= $eps * $t) { |
|
270 | + $t = ($ks != $p ? abs($e[$ks]) : 0.) + ($ks != $k + 1 ? abs($e[$ks - 1]) : 0.); |
|
271 | + if (abs($this->s[$ks]) <= $eps * $t) { |
|
272 | 272 | $this->s[$ks] = 0.0; |
273 | 273 | break; |
274 | 274 | } |
275 | 275 | } |
276 | 276 | if ($ks == $k) { |
277 | 277 | $kase = 3; |
278 | - } else if ($ks == $p-1) { |
|
278 | + } else if ($ks == $p - 1) { |
|
279 | 279 | $kase = 1; |
280 | 280 | } else { |
281 | 281 | $kase = 2; |
@@ -288,21 +288,21 @@ discard block |
||
288 | 288 | switch ($kase) { |
289 | 289 | // Deflate negligible s(p). |
290 | 290 | case 1: |
291 | - $f = $e[$p-2]; |
|
292 | - $e[$p-2] = 0.0; |
|
291 | + $f = $e[$p - 2]; |
|
292 | + $e[$p - 2] = 0.0; |
|
293 | 293 | for ($j = $p - 2; $j >= $k; --$j) { |
294 | - $t = hypo($this->s[$j],$f); |
|
294 | + $t = hypo($this->s[$j], $f); |
|
295 | 295 | $cs = $this->s[$j] / $t; |
296 | 296 | $sn = $f / $t; |
297 | 297 | $this->s[$j] = $t; |
298 | 298 | if ($j != $k) { |
299 | - $f = -$sn * $e[$j-1]; |
|
300 | - $e[$j-1] = $cs * $e[$j-1]; |
|
299 | + $f = -$sn * $e[$j - 1]; |
|
300 | + $e[$j - 1] = $cs * $e[$j - 1]; |
|
301 | 301 | } |
302 | 302 | if ($wantv) { |
303 | 303 | for ($i = 0; $i < $this->n; ++$i) { |
304 | - $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$p-1]; |
|
305 | - $this->V[$i][$p-1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$p-1]; |
|
304 | + $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$p - 1]; |
|
305 | + $this->V[$i][$p - 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$p - 1]; |
|
306 | 306 | $this->V[$i][$j] = $t; |
307 | 307 | } |
308 | 308 | } |
@@ -310,8 +310,8 @@ discard block |
||
310 | 310 | break; |
311 | 311 | // Split at negligible s(k). |
312 | 312 | case 2: |
313 | - $f = $e[$k-1]; |
|
314 | - $e[$k-1] = 0.0; |
|
313 | + $f = $e[$k - 1]; |
|
314 | + $e[$k - 1] = 0.0; |
|
315 | 315 | for ($j = $k; $j < $p; ++$j) { |
316 | 316 | $t = hypo($this->s[$j], $f); |
317 | 317 | $cs = $this->s[$j] / $t; |
@@ -321,8 +321,8 @@ discard block |
||
321 | 321 | $e[$j] = $cs * $e[$j]; |
322 | 322 | if ($wantu) { |
323 | 323 | for ($i = 0; $i < $this->m; ++$i) { |
324 | - $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$k-1]; |
|
325 | - $this->U[$i][$k-1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$k-1]; |
|
324 | + $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$k - 1]; |
|
325 | + $this->U[$i][$k - 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$k - 1]; |
|
326 | 326 | $this->U[$i][$j] = $t; |
327 | 327 | } |
328 | 328 | } |
@@ -332,11 +332,11 @@ discard block |
||
332 | 332 | case 3: |
333 | 333 | // Calculate the shift. |
334 | 334 | $scale = max(max(max(max( |
335 | - abs($this->s[$p-1]),abs($this->s[$p-2])),abs($e[$p-2])), |
|
335 | + abs($this->s[$p - 1]), abs($this->s[$p - 2])), abs($e[$p - 2])), |
|
336 | 336 | abs($this->s[$k])), abs($e[$k])); |
337 | - $sp = $this->s[$p-1] / $scale; |
|
338 | - $spm1 = $this->s[$p-2] / $scale; |
|
339 | - $epm1 = $e[$p-2] / $scale; |
|
337 | + $sp = $this->s[$p - 1] / $scale; |
|
338 | + $spm1 = $this->s[$p - 2] / $scale; |
|
339 | + $epm1 = $e[$p - 2] / $scale; |
|
340 | 340 | $sk = $this->s[$k] / $scale; |
341 | 341 | $ek = $e[$k] / $scale; |
342 | 342 | $b = (($spm1 + $sp) * ($spm1 - $sp) + $epm1 * $epm1) / 2.0; |
@@ -352,41 +352,41 @@ discard block |
||
352 | 352 | $f = ($sk + $sp) * ($sk - $sp) + $shift; |
353 | 353 | $g = $sk * $ek; |
354 | 354 | // Chase zeros. |
355 | - for ($j = $k; $j < $p-1; ++$j) { |
|
356 | - $t = hypo($f,$g); |
|
357 | - $cs = $f/$t; |
|
358 | - $sn = $g/$t; |
|
355 | + for ($j = $k; $j < $p - 1; ++$j) { |
|
356 | + $t = hypo($f, $g); |
|
357 | + $cs = $f / $t; |
|
358 | + $sn = $g / $t; |
|
359 | 359 | if ($j != $k) { |
360 | - $e[$j-1] = $t; |
|
360 | + $e[$j - 1] = $t; |
|
361 | 361 | } |
362 | 362 | $f = $cs * $this->s[$j] + $sn * $e[$j]; |
363 | 363 | $e[$j] = $cs * $e[$j] - $sn * $this->s[$j]; |
364 | - $g = $sn * $this->s[$j+1]; |
|
365 | - $this->s[$j+1] = $cs * $this->s[$j+1]; |
|
364 | + $g = $sn * $this->s[$j + 1]; |
|
365 | + $this->s[$j + 1] = $cs * $this->s[$j + 1]; |
|
366 | 366 | if ($wantv) { |
367 | 367 | for ($i = 0; $i < $this->n; ++$i) { |
368 | - $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$j+1]; |
|
369 | - $this->V[$i][$j+1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$j+1]; |
|
368 | + $t = $cs * $this->V[$i][$j] + $sn * $this->V[$i][$j + 1]; |
|
369 | + $this->V[$i][$j + 1] = -$sn * $this->V[$i][$j] + $cs * $this->V[$i][$j + 1]; |
|
370 | 370 | $this->V[$i][$j] = $t; |
371 | 371 | } |
372 | 372 | } |
373 | - $t = hypo($f,$g); |
|
374 | - $cs = $f/$t; |
|
375 | - $sn = $g/$t; |
|
373 | + $t = hypo($f, $g); |
|
374 | + $cs = $f / $t; |
|
375 | + $sn = $g / $t; |
|
376 | 376 | $this->s[$j] = $t; |
377 | - $f = $cs * $e[$j] + $sn * $this->s[$j+1]; |
|
378 | - $this->s[$j+1] = -$sn * $e[$j] + $cs * $this->s[$j+1]; |
|
379 | - $g = $sn * $e[$j+1]; |
|
380 | - $e[$j+1] = $cs * $e[$j+1]; |
|
377 | + $f = $cs * $e[$j] + $sn * $this->s[$j + 1]; |
|
378 | + $this->s[$j + 1] = -$sn * $e[$j] + $cs * $this->s[$j + 1]; |
|
379 | + $g = $sn * $e[$j + 1]; |
|
380 | + $e[$j + 1] = $cs * $e[$j + 1]; |
|
381 | 381 | if ($wantu && ($j < $this->m - 1)) { |
382 | 382 | for ($i = 0; $i < $this->m; ++$i) { |
383 | - $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$j+1]; |
|
384 | - $this->U[$i][$j+1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$j+1]; |
|
383 | + $t = $cs * $this->U[$i][$j] + $sn * $this->U[$i][$j + 1]; |
|
384 | + $this->U[$i][$j + 1] = -$sn * $this->U[$i][$j] + $cs * $this->U[$i][$j + 1]; |
|
385 | 385 | $this->U[$i][$j] = $t; |
386 | 386 | } |
387 | 387 | } |
388 | 388 | } |
389 | - $e[$p-2] = $f; |
|
389 | + $e[$p - 2] = $f; |
|
390 | 390 | $iter = $iter + 1; |
391 | 391 | break; |
392 | 392 | // Convergence. |
@@ -402,23 +402,23 @@ discard block |
||
402 | 402 | } |
403 | 403 | // Order the singular values. |
404 | 404 | while ($k < $pp) { |
405 | - if ($this->s[$k] >= $this->s[$k+1]) { |
|
405 | + if ($this->s[$k] >= $this->s[$k + 1]) { |
|
406 | 406 | break; |
407 | 407 | } |
408 | 408 | $t = $this->s[$k]; |
409 | - $this->s[$k] = $this->s[$k+1]; |
|
410 | - $this->s[$k+1] = $t; |
|
409 | + $this->s[$k] = $this->s[$k + 1]; |
|
410 | + $this->s[$k + 1] = $t; |
|
411 | 411 | if ($wantv AND ($k < $this->n - 1)) { |
412 | 412 | for ($i = 0; $i < $this->n; ++$i) { |
413 | - $t = $this->V[$i][$k+1]; |
|
414 | - $this->V[$i][$k+1] = $this->V[$i][$k]; |
|
413 | + $t = $this->V[$i][$k + 1]; |
|
414 | + $this->V[$i][$k + 1] = $this->V[$i][$k]; |
|
415 | 415 | $this->V[$i][$k] = $t; |
416 | 416 | } |
417 | 417 | } |
418 | - if ($wantu AND ($k < $this->m-1)) { |
|
418 | + if ($wantu AND ($k < $this->m - 1)) { |
|
419 | 419 | for ($i = 0; $i < $this->m; ++$i) { |
420 | - $t = $this->U[$i][$k+1]; |
|
421 | - $this->U[$i][$k+1] = $this->U[$i][$k]; |
|
420 | + $t = $this->U[$i][$k + 1]; |
|
421 | + $this->U[$i][$k + 1] = $this->U[$i][$k]; |
|
422 | 422 | $this->U[$i][$k] = $t; |
423 | 423 | } |
424 | 424 | } |
@@ -364,8 +364,6 @@ discard block |
||
364 | 364 | |
365 | 365 | /** |
366 | 366 | * Check norm of difference of "vectors". |
367 | - * @param float $x[] |
|
368 | - * @param float $y[] |
|
369 | 367 | */ |
370 | 368 | function checkVectors($x, $y) { |
371 | 369 | $nx = count($x); |
@@ -379,8 +377,6 @@ discard block |
||
379 | 377 | |
380 | 378 | /** |
381 | 379 | * Check norm of difference of "arrays". |
382 | - * @param float $x[][] |
|
383 | - * @param float $y[][] |
|
384 | 380 | */ |
385 | 381 | function checkArrays($x, $y) { |
386 | 382 | $A = new Matrix($x); |
@@ -6,115 +6,115 @@ discard block |
||
6 | 6 | |
7 | 7 | function TestMatrix() { |
8 | 8 | |
9 | - // define test variables |
|
10 | - |
|
11 | - $errorCount = 0; |
|
12 | - $warningCount = 0; |
|
13 | - $columnwise = array(1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.); |
|
14 | - $rowwise = array(1.,4.,7.,10.,2.,5.,8.,11.,3.,6.,9.,12.); |
|
15 | - $avals = array(array(1.,4.,7.,10.),array(2.,5.,8.,11.),array(3.,6.,9.,12.)); |
|
16 | - $rankdef = $avals; |
|
17 | - $tvals = array(array(1.,2.,3.),array(4.,5.,6.),array(7.,8.,9.),array(10.,11.,12.)); |
|
18 | - $subavals = array(array(5.,8.,11.),array(6.,9.,12.)); |
|
19 | - $rvals = array(array(1.,4.,7.),array(2.,5.,8.,11.),array(3.,6.,9.,12.)); |
|
20 | - $pvals = array(array(1.,1.,1.),array(1.,2.,3.),array(1.,3.,6.)); |
|
21 | - $ivals = array(array(1.,0.,0.,0.),array(0.,1.,0.,0.),array(0.,0.,1.,0.)); |
|
22 | - $evals = array(array(0.,1.,0.,0.),array(1.,0.,2.e-7,0.),array(0.,-2.e-7,0.,1.),array(0.,0.,1.,0.)); |
|
23 | - $square = array(array(166.,188.,210.),array(188.,214.,240.),array(210.,240.,270.)); |
|
24 | - $sqSolution = array(array(13.),array(15.)); |
|
25 | - $condmat = array(array(1.,3.),array(7.,9.)); |
|
26 | - $rows = 3; |
|
27 | - $cols = 4; |
|
28 | - $invalidID = 5; /* should trigger bad shape for construction with val */ |
|
29 | - $raggedr = 0; /* (raggedr,raggedc) should be out of bounds in ragged array */ |
|
30 | - $raggedc = 4; |
|
31 | - $validID = 3; /* leading dimension of intended test Matrices */ |
|
32 | - $nonconformld = 4; /* leading dimension which is valid, but nonconforming */ |
|
33 | - $ib = 1; /* index ranges for sub Matrix */ |
|
34 | - $ie = 2; |
|
35 | - $jb = 1; |
|
36 | - $je = 3; |
|
37 | - $rowindexset = array(1,2); |
|
38 | - $badrowindexset = array(1,3); |
|
39 | - $columnindexset = array(1,2,3); |
|
40 | - $badcolumnindexset = array(1,2,4); |
|
41 | - $columnsummax = 33.; |
|
42 | - $rowsummax = 30.; |
|
43 | - $sumofdiagonals = 15; |
|
44 | - $sumofsquares = 650; |
|
45 | - |
|
46 | - /** |
|
47 | - * Test matrix methods |
|
48 | - */ |
|
49 | - |
|
50 | - /** |
|
51 | - * Constructors and constructor-like methods: |
|
52 | - * |
|
53 | - * Matrix(double[], int) |
|
54 | - * Matrix(double[][]) |
|
55 | - * Matrix(int, int) |
|
56 | - * Matrix(int, int, double) |
|
57 | - * Matrix(int, int, double[][]) |
|
58 | - * constructWithCopy(double[][]) |
|
59 | - * random(int,int) |
|
60 | - * identity(int) |
|
61 | - */ |
|
62 | - echo "<p>Testing constructors and constructor-like methods...</p>"; |
|
63 | - |
|
64 | - $A = new Matrix($columnwise, 3); |
|
65 | - if($A instanceof Matrix) { |
|
66 | - $this->try_success("Column-packed constructor..."); |
|
67 | - } else |
|
68 | - $errorCount = $this->try_failure($errorCount, "Column-packed constructor...", "Unable to construct Matrix"); |
|
69 | - |
|
70 | - $T = new Matrix($tvals); |
|
71 | - if($T instanceof Matrix) |
|
72 | - $this->try_success("2D array constructor..."); |
|
73 | - else |
|
74 | - $errorCount = $this->try_failure($errorCount, "2D array constructor...", "Unable to construct Matrix"); |
|
75 | - |
|
76 | - $A = new Matrix($columnwise, $validID); |
|
77 | - $B = new Matrix($avals); |
|
78 | - $tmp = $B->get(0,0); |
|
79 | - $avals[0][0] = 0.0; |
|
80 | - $C = $B->minus($A); |
|
81 | - $avals[0][0] = $tmp; |
|
82 | - $B = Matrix::constructWithCopy($avals); |
|
83 | - $tmp = $B->get(0,0); |
|
84 | - $avals[0][0] = 0.0; |
|
85 | - /** check that constructWithCopy behaves properly **/ |
|
86 | - if ( ( $tmp - $B->get(0,0) ) != 0.0 ) |
|
87 | - $errorCount = $this->try_failure($errorCount,"constructWithCopy... ","copy not effected... data visible outside"); |
|
88 | - else |
|
89 | - $this->try_success("constructWithCopy... ",""); |
|
90 | - |
|
91 | - $I = new Matrix($ivals); |
|
92 | - if ( $this->checkMatrices($I,Matrix::identity(3,4)) ) |
|
93 | - $this->try_success("identity... ",""); |
|
94 | - else |
|
95 | - $errorCount = $this->try_failure($errorCount,"identity... ","identity Matrix not successfully created"); |
|
96 | - |
|
97 | - /** |
|
98 | - * Access Methods: |
|
99 | - * |
|
100 | - * getColumnDimension() |
|
101 | - * getRowDimension() |
|
102 | - * getArray() |
|
103 | - * getArrayCopy() |
|
104 | - * getColumnPackedCopy() |
|
105 | - * getRowPackedCopy() |
|
106 | - * get(int,int) |
|
107 | - * getMatrix(int,int,int,int) |
|
108 | - * getMatrix(int,int,int[]) |
|
109 | - * getMatrix(int[],int,int) |
|
110 | - * getMatrix(int[],int[]) |
|
111 | - * set(int,int,double) |
|
112 | - * setMatrix(int,int,int,int,Matrix) |
|
113 | - * setMatrix(int,int,int[],Matrix) |
|
114 | - * setMatrix(int[],int,int,Matrix) |
|
115 | - * setMatrix(int[],int[],Matrix) |
|
116 | - */ |
|
117 | - print "<p>Testing access methods...</p>"; |
|
9 | + // define test variables |
|
10 | + |
|
11 | + $errorCount = 0; |
|
12 | + $warningCount = 0; |
|
13 | + $columnwise = array(1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.); |
|
14 | + $rowwise = array(1.,4.,7.,10.,2.,5.,8.,11.,3.,6.,9.,12.); |
|
15 | + $avals = array(array(1.,4.,7.,10.),array(2.,5.,8.,11.),array(3.,6.,9.,12.)); |
|
16 | + $rankdef = $avals; |
|
17 | + $tvals = array(array(1.,2.,3.),array(4.,5.,6.),array(7.,8.,9.),array(10.,11.,12.)); |
|
18 | + $subavals = array(array(5.,8.,11.),array(6.,9.,12.)); |
|
19 | + $rvals = array(array(1.,4.,7.),array(2.,5.,8.,11.),array(3.,6.,9.,12.)); |
|
20 | + $pvals = array(array(1.,1.,1.),array(1.,2.,3.),array(1.,3.,6.)); |
|
21 | + $ivals = array(array(1.,0.,0.,0.),array(0.,1.,0.,0.),array(0.,0.,1.,0.)); |
|
22 | + $evals = array(array(0.,1.,0.,0.),array(1.,0.,2.e-7,0.),array(0.,-2.e-7,0.,1.),array(0.,0.,1.,0.)); |
|
23 | + $square = array(array(166.,188.,210.),array(188.,214.,240.),array(210.,240.,270.)); |
|
24 | + $sqSolution = array(array(13.),array(15.)); |
|
25 | + $condmat = array(array(1.,3.),array(7.,9.)); |
|
26 | + $rows = 3; |
|
27 | + $cols = 4; |
|
28 | + $invalidID = 5; /* should trigger bad shape for construction with val */ |
|
29 | + $raggedr = 0; /* (raggedr,raggedc) should be out of bounds in ragged array */ |
|
30 | + $raggedc = 4; |
|
31 | + $validID = 3; /* leading dimension of intended test Matrices */ |
|
32 | + $nonconformld = 4; /* leading dimension which is valid, but nonconforming */ |
|
33 | + $ib = 1; /* index ranges for sub Matrix */ |
|
34 | + $ie = 2; |
|
35 | + $jb = 1; |
|
36 | + $je = 3; |
|
37 | + $rowindexset = array(1,2); |
|
38 | + $badrowindexset = array(1,3); |
|
39 | + $columnindexset = array(1,2,3); |
|
40 | + $badcolumnindexset = array(1,2,4); |
|
41 | + $columnsummax = 33.; |
|
42 | + $rowsummax = 30.; |
|
43 | + $sumofdiagonals = 15; |
|
44 | + $sumofsquares = 650; |
|
45 | + |
|
46 | + /** |
|
47 | + * Test matrix methods |
|
48 | + */ |
|
49 | + |
|
50 | + /** |
|
51 | + * Constructors and constructor-like methods: |
|
52 | + * |
|
53 | + * Matrix(double[], int) |
|
54 | + * Matrix(double[][]) |
|
55 | + * Matrix(int, int) |
|
56 | + * Matrix(int, int, double) |
|
57 | + * Matrix(int, int, double[][]) |
|
58 | + * constructWithCopy(double[][]) |
|
59 | + * random(int,int) |
|
60 | + * identity(int) |
|
61 | + */ |
|
62 | + echo "<p>Testing constructors and constructor-like methods...</p>"; |
|
63 | + |
|
64 | + $A = new Matrix($columnwise, 3); |
|
65 | + if($A instanceof Matrix) { |
|
66 | + $this->try_success("Column-packed constructor..."); |
|
67 | + } else |
|
68 | + $errorCount = $this->try_failure($errorCount, "Column-packed constructor...", "Unable to construct Matrix"); |
|
69 | + |
|
70 | + $T = new Matrix($tvals); |
|
71 | + if($T instanceof Matrix) |
|
72 | + $this->try_success("2D array constructor..."); |
|
73 | + else |
|
74 | + $errorCount = $this->try_failure($errorCount, "2D array constructor...", "Unable to construct Matrix"); |
|
75 | + |
|
76 | + $A = new Matrix($columnwise, $validID); |
|
77 | + $B = new Matrix($avals); |
|
78 | + $tmp = $B->get(0,0); |
|
79 | + $avals[0][0] = 0.0; |
|
80 | + $C = $B->minus($A); |
|
81 | + $avals[0][0] = $tmp; |
|
82 | + $B = Matrix::constructWithCopy($avals); |
|
83 | + $tmp = $B->get(0,0); |
|
84 | + $avals[0][0] = 0.0; |
|
85 | + /** check that constructWithCopy behaves properly **/ |
|
86 | + if ( ( $tmp - $B->get(0,0) ) != 0.0 ) |
|
87 | + $errorCount = $this->try_failure($errorCount,"constructWithCopy... ","copy not effected... data visible outside"); |
|
88 | + else |
|
89 | + $this->try_success("constructWithCopy... ",""); |
|
90 | + |
|
91 | + $I = new Matrix($ivals); |
|
92 | + if ( $this->checkMatrices($I,Matrix::identity(3,4)) ) |
|
93 | + $this->try_success("identity... ",""); |
|
94 | + else |
|
95 | + $errorCount = $this->try_failure($errorCount,"identity... ","identity Matrix not successfully created"); |
|
96 | + |
|
97 | + /** |
|
98 | + * Access Methods: |
|
99 | + * |
|
100 | + * getColumnDimension() |
|
101 | + * getRowDimension() |
|
102 | + * getArray() |
|
103 | + * getArrayCopy() |
|
104 | + * getColumnPackedCopy() |
|
105 | + * getRowPackedCopy() |
|
106 | + * get(int,int) |
|
107 | + * getMatrix(int,int,int,int) |
|
108 | + * getMatrix(int,int,int[]) |
|
109 | + * getMatrix(int[],int,int) |
|
110 | + * getMatrix(int[],int[]) |
|
111 | + * set(int,int,double) |
|
112 | + * setMatrix(int,int,int,int,Matrix) |
|
113 | + * setMatrix(int,int,int[],Matrix) |
|
114 | + * setMatrix(int[],int,int,Matrix) |
|
115 | + * setMatrix(int[],int[],Matrix) |
|
116 | + */ |
|
117 | + print "<p>Testing access methods...</p>"; |
|
118 | 118 | |
119 | 119 | $B = new Matrix($avals); |
120 | 120 | if($B->getRowDimension() == $rows) |
@@ -145,268 +145,268 @@ discard block |
||
145 | 145 | else |
146 | 146 | $errorCount = $this->try_failure($errorCount, "getRowPackedCopy..."); |
147 | 147 | |
148 | - /** |
|
149 | - * Array-like methods: |
|
150 | - * minus |
|
151 | - * minusEquals |
|
152 | - * plus |
|
153 | - * plusEquals |
|
154 | - * arrayLeftDivide |
|
155 | - * arrayLeftDivideEquals |
|
156 | - * arrayRightDivide |
|
157 | - * arrayRightDivideEquals |
|
158 | - * arrayTimes |
|
159 | - * arrayTimesEquals |
|
160 | - * uminus |
|
161 | - */ |
|
162 | - print "<p>Testing array-like methods...</p>"; |
|
163 | - |
|
164 | - /** |
|
165 | - * I/O methods: |
|
166 | - * read |
|
167 | ||
168 | - * serializable: |
|
169 | - * writeObject |
|
170 | - * readObject |
|
171 | - */ |
|
172 | - print "<p>Testing I/O methods...</p>"; |
|
173 | - |
|
174 | - /** |
|
175 | - * Test linear algebra methods |
|
176 | - */ |
|
177 | - echo "<p>Testing linear algebra methods...<p>"; |
|
178 | - |
|
179 | - $A = new Matrix($columnwise, 3); |
|
180 | - if( $this->checkMatrices($A->transpose(), $T) ) |
|
181 | - $this->try_success("Transpose check..."); |
|
182 | - else |
|
183 | - $errorCount = $this->try_failure($errorCount, "Transpose check...", "Matrices are not equal"); |
|
184 | - |
|
185 | - if($this->checkScalars($A->norm1(), $columnsummax)) |
|
186 | - $this->try_success("Maximum column sum..."); |
|
187 | - else |
|
188 | - $errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: " . $A->norm1() . " != " . $columnsummax); |
|
189 | - |
|
190 | - if($this->checkScalars($A->normInf(), $rowsummax)) |
|
191 | - $this->try_success("Maximum row sum..."); |
|
192 | - else |
|
193 | - $errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: " . $A->normInf() . " != " . $rowsummax ); |
|
194 | - |
|
195 | - if($this->checkScalars($A->normF(), sqrt($sumofsquares))) |
|
196 | - $this->try_success("Frobenius norm..."); |
|
197 | - else |
|
198 | - $errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:" . $A->normF() . " != " . sqrt($sumofsquares)); |
|
199 | - |
|
200 | - if($this->checkScalars($A->trace(), $sumofdiagonals)) |
|
201 | - $this->try_success("Matrix trace..."); |
|
202 | - else |
|
203 | - $errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: " . $A->trace() . " != " . $sumofdiagonals); |
|
204 | - |
|
205 | - $B = $A->getMatrix(0, $A->getRowDimension(), 0, $A->getRowDimension()); |
|
206 | - if( $B->det() == 0 ) |
|
207 | - $this->try_success("Matrix determinant..."); |
|
208 | - else |
|
209 | - $errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: " . $B->det() . " != " . 0); |
|
210 | - |
|
211 | - $A = new Matrix($columnwise,3); |
|
212 | - $SQ = new Matrix($square); |
|
213 | - if ($this->checkMatrices($SQ, $A->times($A->transpose()))) |
|
214 | - $this->try_success("times(Matrix)..."); |
|
215 | - else { |
|
216 | - $errorCount = $this->try_failure($errorCount, "times(Matrix)...", "Unable to multiply matrices"); |
|
217 | - $SQ->toHTML(); |
|
218 | - $AT->toHTML(); |
|
219 | - } |
|
220 | - |
|
221 | - $A = new Matrix($columnwise, 4); |
|
222 | - |
|
223 | - $QR = $A->qr(); |
|
224 | - $R = $QR->getR(); |
|
225 | - $Q = $QR->getQ(); |
|
226 | - if($this->checkMatrices($A, $Q->times($R))) |
|
227 | - $this->try_success("QRDecomposition...",""); |
|
228 | - else |
|
229 | - $errorCount = $this->try_failure($errorCount,"QRDecomposition...","incorrect qr decomposition calculation"); |
|
230 | - |
|
231 | - $A = new Matrix($columnwise, 4); |
|
232 | - $SVD = $A->svd(); |
|
233 | - $U = $SVD->getU(); |
|
234 | - $S = $SVD->getS(); |
|
235 | - $V = $SVD->getV(); |
|
236 | - if ($this->checkMatrices($A, $U->times($S->times($V->transpose())))) |
|
237 | - $this->try_success("SingularValueDecomposition...",""); |
|
238 | - else |
|
239 | - $errorCount = $this->try_failure($errorCount,"SingularValueDecomposition...","incorrect singular value decomposition calculation"); |
|
240 | - |
|
241 | - $n = $A->getColumnDimension(); |
|
242 | - $A = $A->getMatrix(0,$n-1,0,$n-1); |
|
243 | - $A->set(0,0,0.); |
|
244 | - |
|
245 | - $LU = $A->lu(); |
|
246 | - $L = $LU->getL(); |
|
247 | - if ( $this->checkMatrices($A->getMatrix($LU->getPivot(),0,$n-1), $L->times($LU->getU())) ) |
|
248 | - $this->try_success("LUDecomposition...",""); |
|
249 | - else |
|
250 | - $errorCount = $this->try_failure($errorCount,"LUDecomposition...","incorrect LU decomposition calculation"); |
|
251 | - |
|
252 | - $X = $A->inverse(); |
|
253 | - if ( $this->checkMatrices($A->times($X),Matrix::identity(3,3)) ) |
|
254 | - $this->try_success("inverse()...",""); |
|
255 | - else |
|
256 | - $errorCount = $this->try_failure($errorCount, "inverse()...","incorrect inverse calculation"); |
|
257 | - |
|
258 | - $DEF = new Matrix($rankdef); |
|
259 | - if($this->checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension())-1)) |
|
260 | - $this->try_success("Rank..."); |
|
261 | - else |
|
262 | - $this->try_failure("Rank...", "incorrect rank calculation"); |
|
263 | - |
|
264 | - $B = new Matrix($condmat); |
|
265 | - $SVD = $B->svd(); |
|
266 | - $singularvalues = $SVD->getSingularValues(); |
|
267 | - if($this->checkScalars($B->cond(), $singularvalues[0]/$singularvalues[min($B->getRowDimension(), $B->getColumnDimension())-1])) |
|
268 | - $this->try_success("Condition number..."); |
|
269 | - else |
|
270 | - $this->try_failure("Condition number...", "incorrect condition number calculation"); |
|
271 | - |
|
272 | - $SUB = new Matrix($subavals); |
|
273 | - $O = new Matrix($SUB->getRowDimension(),1,1.0); |
|
274 | - $SOL = new Matrix($sqSolution); |
|
275 | - $SQ = $SUB->getMatrix(0,$SUB->getRowDimension()-1,0,$SUB->getRowDimension()-1); |
|
276 | - if ( $this->checkMatrices($SQ->solve($SOL),$O) ) |
|
277 | - $this->try_success("solve()...",""); |
|
278 | - else |
|
279 | - $errorCount = $this->try_failure($errorCount,"solve()...","incorrect lu solve calculation"); |
|
280 | - |
|
281 | - $A = new Matrix($pvals); |
|
282 | - $Chol = $A->chol(); |
|
283 | - $L = $Chol->getL(); |
|
284 | - if ( $this->checkMatrices($A, $L->times($L->transpose())) ) |
|
285 | - $this->try_success("CholeskyDecomposition...",""); |
|
286 | - else |
|
287 | - $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition...","incorrect Cholesky decomposition calculation"); |
|
288 | - |
|
289 | - $X = $Chol->solve(Matrix::identity(3,3)); |
|
290 | - if ( $this->checkMatrices($A->times($X), Matrix::identity(3,3)) ) |
|
291 | - $this->try_success("CholeskyDecomposition solve()...",""); |
|
292 | - else |
|
293 | - $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition solve()...","incorrect Choleskydecomposition solve calculation"); |
|
294 | - |
|
295 | - $Eig = $A->eig(); |
|
296 | - $D = $Eig->getD(); |
|
297 | - $V = $Eig->getV(); |
|
298 | - if( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
299 | - $this->try_success("EigenvalueDecomposition (symmetric)...",""); |
|
300 | - else |
|
301 | - $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (symmetric)...","incorrect symmetric Eigenvalue decomposition calculation"); |
|
302 | - |
|
303 | - $A = new Matrix($evals); |
|
304 | - $Eig = $A->eig(); |
|
305 | - $D = $Eig->getD(); |
|
306 | - $V = $Eig->getV(); |
|
307 | - if ( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
308 | - $this->try_success("EigenvalueDecomposition (nonsymmetric)...",""); |
|
309 | - else |
|
310 | - $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (nonsymmetric)...","incorrect nonsymmetric Eigenvalue decomposition calculation"); |
|
148 | + /** |
|
149 | + * Array-like methods: |
|
150 | + * minus |
|
151 | + * minusEquals |
|
152 | + * plus |
|
153 | + * plusEquals |
|
154 | + * arrayLeftDivide |
|
155 | + * arrayLeftDivideEquals |
|
156 | + * arrayRightDivide |
|
157 | + * arrayRightDivideEquals |
|
158 | + * arrayTimes |
|
159 | + * arrayTimesEquals |
|
160 | + * uminus |
|
161 | + */ |
|
162 | + print "<p>Testing array-like methods...</p>"; |
|
163 | + |
|
164 | + /** |
|
165 | + * I/O methods: |
|
166 | + * read |
|
167 | ||
168 | + * serializable: |
|
169 | + * writeObject |
|
170 | + * readObject |
|
171 | + */ |
|
172 | + print "<p>Testing I/O methods...</p>"; |
|
173 | + |
|
174 | + /** |
|
175 | + * Test linear algebra methods |
|
176 | + */ |
|
177 | + echo "<p>Testing linear algebra methods...<p>"; |
|
178 | + |
|
179 | + $A = new Matrix($columnwise, 3); |
|
180 | + if( $this->checkMatrices($A->transpose(), $T) ) |
|
181 | + $this->try_success("Transpose check..."); |
|
182 | + else |
|
183 | + $errorCount = $this->try_failure($errorCount, "Transpose check...", "Matrices are not equal"); |
|
184 | + |
|
185 | + if($this->checkScalars($A->norm1(), $columnsummax)) |
|
186 | + $this->try_success("Maximum column sum..."); |
|
187 | + else |
|
188 | + $errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: " . $A->norm1() . " != " . $columnsummax); |
|
189 | + |
|
190 | + if($this->checkScalars($A->normInf(), $rowsummax)) |
|
191 | + $this->try_success("Maximum row sum..."); |
|
192 | + else |
|
193 | + $errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: " . $A->normInf() . " != " . $rowsummax ); |
|
194 | + |
|
195 | + if($this->checkScalars($A->normF(), sqrt($sumofsquares))) |
|
196 | + $this->try_success("Frobenius norm..."); |
|
197 | + else |
|
198 | + $errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:" . $A->normF() . " != " . sqrt($sumofsquares)); |
|
199 | + |
|
200 | + if($this->checkScalars($A->trace(), $sumofdiagonals)) |
|
201 | + $this->try_success("Matrix trace..."); |
|
202 | + else |
|
203 | + $errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: " . $A->trace() . " != " . $sumofdiagonals); |
|
204 | + |
|
205 | + $B = $A->getMatrix(0, $A->getRowDimension(), 0, $A->getRowDimension()); |
|
206 | + if( $B->det() == 0 ) |
|
207 | + $this->try_success("Matrix determinant..."); |
|
208 | + else |
|
209 | + $errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: " . $B->det() . " != " . 0); |
|
210 | + |
|
211 | + $A = new Matrix($columnwise,3); |
|
212 | + $SQ = new Matrix($square); |
|
213 | + if ($this->checkMatrices($SQ, $A->times($A->transpose()))) |
|
214 | + $this->try_success("times(Matrix)..."); |
|
215 | + else { |
|
216 | + $errorCount = $this->try_failure($errorCount, "times(Matrix)...", "Unable to multiply matrices"); |
|
217 | + $SQ->toHTML(); |
|
218 | + $AT->toHTML(); |
|
219 | + } |
|
220 | + |
|
221 | + $A = new Matrix($columnwise, 4); |
|
222 | + |
|
223 | + $QR = $A->qr(); |
|
224 | + $R = $QR->getR(); |
|
225 | + $Q = $QR->getQ(); |
|
226 | + if($this->checkMatrices($A, $Q->times($R))) |
|
227 | + $this->try_success("QRDecomposition...",""); |
|
228 | + else |
|
229 | + $errorCount = $this->try_failure($errorCount,"QRDecomposition...","incorrect qr decomposition calculation"); |
|
230 | + |
|
231 | + $A = new Matrix($columnwise, 4); |
|
232 | + $SVD = $A->svd(); |
|
233 | + $U = $SVD->getU(); |
|
234 | + $S = $SVD->getS(); |
|
235 | + $V = $SVD->getV(); |
|
236 | + if ($this->checkMatrices($A, $U->times($S->times($V->transpose())))) |
|
237 | + $this->try_success("SingularValueDecomposition...",""); |
|
238 | + else |
|
239 | + $errorCount = $this->try_failure($errorCount,"SingularValueDecomposition...","incorrect singular value decomposition calculation"); |
|
240 | + |
|
241 | + $n = $A->getColumnDimension(); |
|
242 | + $A = $A->getMatrix(0,$n-1,0,$n-1); |
|
243 | + $A->set(0,0,0.); |
|
244 | + |
|
245 | + $LU = $A->lu(); |
|
246 | + $L = $LU->getL(); |
|
247 | + if ( $this->checkMatrices($A->getMatrix($LU->getPivot(),0,$n-1), $L->times($LU->getU())) ) |
|
248 | + $this->try_success("LUDecomposition...",""); |
|
249 | + else |
|
250 | + $errorCount = $this->try_failure($errorCount,"LUDecomposition...","incorrect LU decomposition calculation"); |
|
251 | + |
|
252 | + $X = $A->inverse(); |
|
253 | + if ( $this->checkMatrices($A->times($X),Matrix::identity(3,3)) ) |
|
254 | + $this->try_success("inverse()...",""); |
|
255 | + else |
|
256 | + $errorCount = $this->try_failure($errorCount, "inverse()...","incorrect inverse calculation"); |
|
257 | + |
|
258 | + $DEF = new Matrix($rankdef); |
|
259 | + if($this->checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension())-1)) |
|
260 | + $this->try_success("Rank..."); |
|
261 | + else |
|
262 | + $this->try_failure("Rank...", "incorrect rank calculation"); |
|
263 | + |
|
264 | + $B = new Matrix($condmat); |
|
265 | + $SVD = $B->svd(); |
|
266 | + $singularvalues = $SVD->getSingularValues(); |
|
267 | + if($this->checkScalars($B->cond(), $singularvalues[0]/$singularvalues[min($B->getRowDimension(), $B->getColumnDimension())-1])) |
|
268 | + $this->try_success("Condition number..."); |
|
269 | + else |
|
270 | + $this->try_failure("Condition number...", "incorrect condition number calculation"); |
|
271 | + |
|
272 | + $SUB = new Matrix($subavals); |
|
273 | + $O = new Matrix($SUB->getRowDimension(),1,1.0); |
|
274 | + $SOL = new Matrix($sqSolution); |
|
275 | + $SQ = $SUB->getMatrix(0,$SUB->getRowDimension()-1,0,$SUB->getRowDimension()-1); |
|
276 | + if ( $this->checkMatrices($SQ->solve($SOL),$O) ) |
|
277 | + $this->try_success("solve()...",""); |
|
278 | + else |
|
279 | + $errorCount = $this->try_failure($errorCount,"solve()...","incorrect lu solve calculation"); |
|
280 | + |
|
281 | + $A = new Matrix($pvals); |
|
282 | + $Chol = $A->chol(); |
|
283 | + $L = $Chol->getL(); |
|
284 | + if ( $this->checkMatrices($A, $L->times($L->transpose())) ) |
|
285 | + $this->try_success("CholeskyDecomposition...",""); |
|
286 | + else |
|
287 | + $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition...","incorrect Cholesky decomposition calculation"); |
|
288 | + |
|
289 | + $X = $Chol->solve(Matrix::identity(3,3)); |
|
290 | + if ( $this->checkMatrices($A->times($X), Matrix::identity(3,3)) ) |
|
291 | + $this->try_success("CholeskyDecomposition solve()...",""); |
|
292 | + else |
|
293 | + $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition solve()...","incorrect Choleskydecomposition solve calculation"); |
|
294 | + |
|
295 | + $Eig = $A->eig(); |
|
296 | + $D = $Eig->getD(); |
|
297 | + $V = $Eig->getV(); |
|
298 | + if( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
299 | + $this->try_success("EigenvalueDecomposition (symmetric)...",""); |
|
300 | + else |
|
301 | + $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (symmetric)...","incorrect symmetric Eigenvalue decomposition calculation"); |
|
302 | + |
|
303 | + $A = new Matrix($evals); |
|
304 | + $Eig = $A->eig(); |
|
305 | + $D = $Eig->getD(); |
|
306 | + $V = $Eig->getV(); |
|
307 | + if ( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
308 | + $this->try_success("EigenvalueDecomposition (nonsymmetric)...",""); |
|
309 | + else |
|
310 | + $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (nonsymmetric)...","incorrect nonsymmetric Eigenvalue decomposition calculation"); |
|
311 | 311 | |
312 | 312 | print("<b>{$errorCount} total errors</b>."); |
313 | 313 | } |
314 | 314 | |
315 | 315 | /** |
316 | - * Print appropriate messages for successful outcome try |
|
317 | - * @param string $s |
|
318 | - * @param string $e |
|
319 | - */ |
|
316 | + * Print appropriate messages for successful outcome try |
|
317 | + * @param string $s |
|
318 | + * @param string $e |
|
319 | + */ |
|
320 | 320 | function try_success($s, $e = "") { |
321 | - print "> ". $s ."success<br />"; |
|
322 | - if ($e != "") |
|
323 | - print "> Message: ". $e ."<br />"; |
|
321 | + print "> ". $s ."success<br />"; |
|
322 | + if ($e != "") |
|
323 | + print "> Message: ". $e ."<br />"; |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | /** |
327 | - * Print appropriate messages for unsuccessful outcome try |
|
328 | - * @param int $count |
|
329 | - * @param string $s |
|
330 | - * @param string $e |
|
331 | - * @return int incremented counter |
|
332 | - */ |
|
327 | + * Print appropriate messages for unsuccessful outcome try |
|
328 | + * @param int $count |
|
329 | + * @param string $s |
|
330 | + * @param string $e |
|
331 | + * @return int incremented counter |
|
332 | + */ |
|
333 | 333 | function try_failure($count, $s, $e="") { |
334 | - print "> ". $s ."*** failure ***<br />> Message: ". $e ."<br />"; |
|
335 | - return ++$count; |
|
334 | + print "> ". $s ."*** failure ***<br />> Message: ". $e ."<br />"; |
|
335 | + return ++$count; |
|
336 | 336 | } |
337 | 337 | |
338 | 338 | /** |
339 | - * Print appropriate messages for unsuccessful outcome try |
|
340 | - * @param int $count |
|
341 | - * @param string $s |
|
342 | - * @param string $e |
|
343 | - * @return int incremented counter |
|
344 | - */ |
|
339 | + * Print appropriate messages for unsuccessful outcome try |
|
340 | + * @param int $count |
|
341 | + * @param string $s |
|
342 | + * @param string $e |
|
343 | + * @return int incremented counter |
|
344 | + */ |
|
345 | 345 | function try_warning($count, $s, $e="") { |
346 | - print "> ". $s ."*** warning ***<br />> Message: ". $e ."<br />"; |
|
347 | - return ++$count; |
|
346 | + print "> ". $s ."*** warning ***<br />> Message: ". $e ."<br />"; |
|
347 | + return ++$count; |
|
348 | 348 | } |
349 | 349 | |
350 | 350 | /** |
351 | - * Check magnitude of difference of "scalars". |
|
352 | - * @param float $x |
|
353 | - * @param float $y |
|
354 | - */ |
|
351 | + * Check magnitude of difference of "scalars". |
|
352 | + * @param float $x |
|
353 | + * @param float $y |
|
354 | + */ |
|
355 | 355 | function checkScalars($x, $y) { |
356 | - $eps = pow(2.0,-52.0); |
|
357 | - if ($x == 0 & abs($y) < 10*$eps) return; |
|
358 | - if ($y == 0 & abs($x) < 10*$eps) return; |
|
359 | - if (abs($x-$y) > 10 * $eps * max(abs($x),abs($y))) |
|
360 | - return false; |
|
361 | - else |
|
362 | - return true; |
|
356 | + $eps = pow(2.0,-52.0); |
|
357 | + if ($x == 0 & abs($y) < 10*$eps) return; |
|
358 | + if ($y == 0 & abs($x) < 10*$eps) return; |
|
359 | + if (abs($x-$y) > 10 * $eps * max(abs($x),abs($y))) |
|
360 | + return false; |
|
361 | + else |
|
362 | + return true; |
|
363 | 363 | } |
364 | 364 | |
365 | 365 | /** |
366 | - * Check norm of difference of "vectors". |
|
367 | - * @param float $x[] |
|
368 | - * @param float $y[] |
|
369 | - */ |
|
366 | + * Check norm of difference of "vectors". |
|
367 | + * @param float $x[] |
|
368 | + * @param float $y[] |
|
369 | + */ |
|
370 | 370 | function checkVectors($x, $y) { |
371 | - $nx = count($x); |
|
372 | - $ny = count($y); |
|
373 | - if ($nx == $ny) |
|
374 | - for($i=0; $i < $nx; ++$i) |
|
375 | - $this->checkScalars($x[$i],$y[$i]); |
|
376 | - else |
|
377 | - die("Attempt to compare vectors of different lengths"); |
|
371 | + $nx = count($x); |
|
372 | + $ny = count($y); |
|
373 | + if ($nx == $ny) |
|
374 | + for($i=0; $i < $nx; ++$i) |
|
375 | + $this->checkScalars($x[$i],$y[$i]); |
|
376 | + else |
|
377 | + die("Attempt to compare vectors of different lengths"); |
|
378 | 378 | } |
379 | 379 | |
380 | 380 | /** |
381 | - * Check norm of difference of "arrays". |
|
382 | - * @param float $x[][] |
|
383 | - * @param float $y[][] |
|
384 | - */ |
|
381 | + * Check norm of difference of "arrays". |
|
382 | + * @param float $x[][] |
|
383 | + * @param float $y[][] |
|
384 | + */ |
|
385 | 385 | function checkArrays($x, $y) { |
386 | - $A = new Matrix($x); |
|
387 | - $B = new Matrix($y); |
|
388 | - return $this->checkMatrices($A,$B); |
|
386 | + $A = new Matrix($x); |
|
387 | + $B = new Matrix($y); |
|
388 | + return $this->checkMatrices($A,$B); |
|
389 | 389 | } |
390 | 390 | |
391 | 391 | /** |
392 | - * Check norm of difference of "matrices". |
|
393 | - * @param matrix $X |
|
394 | - * @param matrix $Y |
|
395 | - */ |
|
392 | + * Check norm of difference of "matrices". |
|
393 | + * @param matrix $X |
|
394 | + * @param matrix $Y |
|
395 | + */ |
|
396 | 396 | function checkMatrices($X = null, $Y = null) { |
397 | - if( $X == null || $Y == null ) |
|
398 | - return false; |
|
397 | + if( $X == null || $Y == null ) |
|
398 | + return false; |
|
399 | 399 | |
400 | - $eps = pow(2.0,-52.0); |
|
401 | - if ($X->norm1() == 0. & $Y->norm1() < 10*$eps) return true; |
|
402 | - if ($Y->norm1() == 0. & $X->norm1() < 10*$eps) return true; |
|
400 | + $eps = pow(2.0,-52.0); |
|
401 | + if ($X->norm1() == 0. & $Y->norm1() < 10*$eps) return true; |
|
402 | + if ($Y->norm1() == 0. & $X->norm1() < 10*$eps) return true; |
|
403 | 403 | |
404 | - $A = $X->minus($Y); |
|
404 | + $A = $X->minus($Y); |
|
405 | 405 | |
406 | - if ($A->norm1() > 1000 * $eps * max($X->norm1(),$Y->norm1())) |
|
407 | - die("The norm of (X-Y) is too large: ".$A->norm1()); |
|
408 | - else |
|
409 | - return true; |
|
406 | + if ($A->norm1() > 1000 * $eps * max($X->norm1(),$Y->norm1())) |
|
407 | + die("The norm of (X-Y) is too large: ".$A->norm1()); |
|
408 | + else |
|
409 | + return true; |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | } |
@@ -10,19 +10,19 @@ discard block |
||
10 | 10 | |
11 | 11 | $errorCount = 0; |
12 | 12 | $warningCount = 0; |
13 | - $columnwise = array(1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.); |
|
14 | - $rowwise = array(1.,4.,7.,10.,2.,5.,8.,11.,3.,6.,9.,12.); |
|
15 | - $avals = array(array(1.,4.,7.,10.),array(2.,5.,8.,11.),array(3.,6.,9.,12.)); |
|
13 | + $columnwise = array(1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.); |
|
14 | + $rowwise = array(1., 4., 7., 10., 2., 5., 8., 11., 3., 6., 9., 12.); |
|
15 | + $avals = array(array(1., 4., 7., 10.), array(2., 5., 8., 11.), array(3., 6., 9., 12.)); |
|
16 | 16 | $rankdef = $avals; |
17 | - $tvals = array(array(1.,2.,3.),array(4.,5.,6.),array(7.,8.,9.),array(10.,11.,12.)); |
|
18 | - $subavals = array(array(5.,8.,11.),array(6.,9.,12.)); |
|
19 | - $rvals = array(array(1.,4.,7.),array(2.,5.,8.,11.),array(3.,6.,9.,12.)); |
|
20 | - $pvals = array(array(1.,1.,1.),array(1.,2.,3.),array(1.,3.,6.)); |
|
21 | - $ivals = array(array(1.,0.,0.,0.),array(0.,1.,0.,0.),array(0.,0.,1.,0.)); |
|
22 | - $evals = array(array(0.,1.,0.,0.),array(1.,0.,2.e-7,0.),array(0.,-2.e-7,0.,1.),array(0.,0.,1.,0.)); |
|
23 | - $square = array(array(166.,188.,210.),array(188.,214.,240.),array(210.,240.,270.)); |
|
24 | - $sqSolution = array(array(13.),array(15.)); |
|
25 | - $condmat = array(array(1.,3.),array(7.,9.)); |
|
17 | + $tvals = array(array(1., 2., 3.), array(4., 5., 6.), array(7., 8., 9.), array(10., 11., 12.)); |
|
18 | + $subavals = array(array(5., 8., 11.), array(6., 9., 12.)); |
|
19 | + $rvals = array(array(1., 4., 7.), array(2., 5., 8., 11.), array(3., 6., 9., 12.)); |
|
20 | + $pvals = array(array(1., 1., 1.), array(1., 2., 3.), array(1., 3., 6.)); |
|
21 | + $ivals = array(array(1., 0., 0., 0.), array(0., 1., 0., 0.), array(0., 0., 1., 0.)); |
|
22 | + $evals = array(array(0., 1., 0., 0.), array(1., 0., 2.e-7, 0.), array(0., -2.e-7, 0., 1.), array(0., 0., 1., 0.)); |
|
23 | + $square = array(array(166., 188., 210.), array(188., 214., 240.), array(210., 240., 270.)); |
|
24 | + $sqSolution = array(array(13.), array(15.)); |
|
25 | + $condmat = array(array(1., 3.), array(7., 9.)); |
|
26 | 26 | $rows = 3; |
27 | 27 | $cols = 4; |
28 | 28 | $invalidID = 5; /* should trigger bad shape for construction with val */ |
@@ -34,10 +34,10 @@ discard block |
||
34 | 34 | $ie = 2; |
35 | 35 | $jb = 1; |
36 | 36 | $je = 3; |
37 | - $rowindexset = array(1,2); |
|
38 | - $badrowindexset = array(1,3); |
|
39 | - $columnindexset = array(1,2,3); |
|
40 | - $badcolumnindexset = array(1,2,4); |
|
37 | + $rowindexset = array(1, 2); |
|
38 | + $badrowindexset = array(1, 3); |
|
39 | + $columnindexset = array(1, 2, 3); |
|
40 | + $badcolumnindexset = array(1, 2, 4); |
|
41 | 41 | $columnsummax = 33.; |
42 | 42 | $rowsummax = 30.; |
43 | 43 | $sumofdiagonals = 15; |
@@ -62,37 +62,37 @@ discard block |
||
62 | 62 | echo "<p>Testing constructors and constructor-like methods...</p>"; |
63 | 63 | |
64 | 64 | $A = new Matrix($columnwise, 3); |
65 | - if($A instanceof Matrix) { |
|
65 | + if ($A instanceof Matrix) { |
|
66 | 66 | $this->try_success("Column-packed constructor..."); |
67 | 67 | } else |
68 | 68 | $errorCount = $this->try_failure($errorCount, "Column-packed constructor...", "Unable to construct Matrix"); |
69 | 69 | |
70 | 70 | $T = new Matrix($tvals); |
71 | - if($T instanceof Matrix) |
|
71 | + if ($T instanceof Matrix) |
|
72 | 72 | $this->try_success("2D array constructor..."); |
73 | 73 | else |
74 | 74 | $errorCount = $this->try_failure($errorCount, "2D array constructor...", "Unable to construct Matrix"); |
75 | 75 | |
76 | 76 | $A = new Matrix($columnwise, $validID); |
77 | 77 | $B = new Matrix($avals); |
78 | - $tmp = $B->get(0,0); |
|
78 | + $tmp = $B->get(0, 0); |
|
79 | 79 | $avals[0][0] = 0.0; |
80 | 80 | $C = $B->minus($A); |
81 | 81 | $avals[0][0] = $tmp; |
82 | 82 | $B = Matrix::constructWithCopy($avals); |
83 | - $tmp = $B->get(0,0); |
|
83 | + $tmp = $B->get(0, 0); |
|
84 | 84 | $avals[0][0] = 0.0; |
85 | 85 | /** check that constructWithCopy behaves properly **/ |
86 | - if ( ( $tmp - $B->get(0,0) ) != 0.0 ) |
|
87 | - $errorCount = $this->try_failure($errorCount,"constructWithCopy... ","copy not effected... data visible outside"); |
|
86 | + if (($tmp - $B->get(0, 0)) != 0.0) |
|
87 | + $errorCount = $this->try_failure($errorCount, "constructWithCopy... ", "copy not effected... data visible outside"); |
|
88 | 88 | else |
89 | - $this->try_success("constructWithCopy... ",""); |
|
89 | + $this->try_success("constructWithCopy... ", ""); |
|
90 | 90 | |
91 | 91 | $I = new Matrix($ivals); |
92 | - if ( $this->checkMatrices($I,Matrix::identity(3,4)) ) |
|
93 | - $this->try_success("identity... ",""); |
|
92 | + if ($this->checkMatrices($I, Matrix::identity(3, 4))) |
|
93 | + $this->try_success("identity... ", ""); |
|
94 | 94 | else |
95 | - $errorCount = $this->try_failure($errorCount,"identity... ","identity Matrix not successfully created"); |
|
95 | + $errorCount = $this->try_failure($errorCount, "identity... ", "identity Matrix not successfully created"); |
|
96 | 96 | |
97 | 97 | /** |
98 | 98 | * Access Methods: |
@@ -117,30 +117,30 @@ discard block |
||
117 | 117 | print "<p>Testing access methods...</p>"; |
118 | 118 | |
119 | 119 | $B = new Matrix($avals); |
120 | - if($B->getRowDimension() == $rows) |
|
120 | + if ($B->getRowDimension() == $rows) |
|
121 | 121 | $this->try_success("getRowDimension..."); |
122 | 122 | else |
123 | 123 | $errorCount = $this->try_failure($errorCount, "getRowDimension..."); |
124 | 124 | |
125 | - if($B->getColumnDimension() == $cols) |
|
125 | + if ($B->getColumnDimension() == $cols) |
|
126 | 126 | $this->try_success("getColumnDimension..."); |
127 | 127 | else |
128 | 128 | $errorCount = $this->try_failure($errorCount, "getColumnDimension..."); |
129 | 129 | |
130 | 130 | $barray = $B->getArray(); |
131 | - if($this->checkArrays($barray, $avals)) |
|
131 | + if ($this->checkArrays($barray, $avals)) |
|
132 | 132 | $this->try_success("getArray..."); |
133 | 133 | else |
134 | 134 | $errorCount = $this->try_failure($errorCount, "getArray..."); |
135 | 135 | |
136 | 136 | $bpacked = $B->getColumnPackedCopy(); |
137 | - if($this->checkArrays($bpacked, $columnwise)) |
|
137 | + if ($this->checkArrays($bpacked, $columnwise)) |
|
138 | 138 | $this->try_success("getColumnPackedCopy..."); |
139 | 139 | else |
140 | 140 | $errorCount = $this->try_failure($errorCount, "getColumnPackedCopy..."); |
141 | 141 | |
142 | 142 | $bpacked = $B->getRowPackedCopy(); |
143 | - if($this->checkArrays($bpacked, $rowwise)) |
|
143 | + if ($this->checkArrays($bpacked, $rowwise)) |
|
144 | 144 | $this->try_success("getRowPackedCopy..."); |
145 | 145 | else |
146 | 146 | $errorCount = $this->try_failure($errorCount, "getRowPackedCopy..."); |
@@ -177,38 +177,38 @@ discard block |
||
177 | 177 | echo "<p>Testing linear algebra methods...<p>"; |
178 | 178 | |
179 | 179 | $A = new Matrix($columnwise, 3); |
180 | - if( $this->checkMatrices($A->transpose(), $T) ) |
|
180 | + if ($this->checkMatrices($A->transpose(), $T)) |
|
181 | 181 | $this->try_success("Transpose check..."); |
182 | 182 | else |
183 | 183 | $errorCount = $this->try_failure($errorCount, "Transpose check...", "Matrices are not equal"); |
184 | 184 | |
185 | - if($this->checkScalars($A->norm1(), $columnsummax)) |
|
185 | + if ($this->checkScalars($A->norm1(), $columnsummax)) |
|
186 | 186 | $this->try_success("Maximum column sum..."); |
187 | 187 | else |
188 | - $errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: " . $A->norm1() . " != " . $columnsummax); |
|
188 | + $errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: ".$A->norm1()." != ".$columnsummax); |
|
189 | 189 | |
190 | - if($this->checkScalars($A->normInf(), $rowsummax)) |
|
190 | + if ($this->checkScalars($A->normInf(), $rowsummax)) |
|
191 | 191 | $this->try_success("Maximum row sum..."); |
192 | 192 | else |
193 | - $errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: " . $A->normInf() . " != " . $rowsummax ); |
|
193 | + $errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: ".$A->normInf()." != ".$rowsummax); |
|
194 | 194 | |
195 | - if($this->checkScalars($A->normF(), sqrt($sumofsquares))) |
|
195 | + if ($this->checkScalars($A->normF(), sqrt($sumofsquares))) |
|
196 | 196 | $this->try_success("Frobenius norm..."); |
197 | 197 | else |
198 | - $errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:" . $A->normF() . " != " . sqrt($sumofsquares)); |
|
198 | + $errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:".$A->normF()." != ".sqrt($sumofsquares)); |
|
199 | 199 | |
200 | - if($this->checkScalars($A->trace(), $sumofdiagonals)) |
|
200 | + if ($this->checkScalars($A->trace(), $sumofdiagonals)) |
|
201 | 201 | $this->try_success("Matrix trace..."); |
202 | 202 | else |
203 | - $errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: " . $A->trace() . " != " . $sumofdiagonals); |
|
203 | + $errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: ".$A->trace()." != ".$sumofdiagonals); |
|
204 | 204 | |
205 | 205 | $B = $A->getMatrix(0, $A->getRowDimension(), 0, $A->getRowDimension()); |
206 | - if( $B->det() == 0 ) |
|
206 | + if ($B->det() == 0) |
|
207 | 207 | $this->try_success("Matrix determinant..."); |
208 | 208 | else |
209 | - $errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: " . $B->det() . " != " . 0); |
|
209 | + $errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: ".$B->det()." != ".0); |
|
210 | 210 | |
211 | - $A = new Matrix($columnwise,3); |
|
211 | + $A = new Matrix($columnwise, 3); |
|
212 | 212 | $SQ = new Matrix($square); |
213 | 213 | if ($this->checkMatrices($SQ, $A->times($A->transpose()))) |
214 | 214 | $this->try_success("times(Matrix)..."); |
@@ -223,10 +223,10 @@ discard block |
||
223 | 223 | $QR = $A->qr(); |
224 | 224 | $R = $QR->getR(); |
225 | 225 | $Q = $QR->getQ(); |
226 | - if($this->checkMatrices($A, $Q->times($R))) |
|
227 | - $this->try_success("QRDecomposition...",""); |
|
226 | + if ($this->checkMatrices($A, $Q->times($R))) |
|
227 | + $this->try_success("QRDecomposition...", ""); |
|
228 | 228 | else |
229 | - $errorCount = $this->try_failure($errorCount,"QRDecomposition...","incorrect qr decomposition calculation"); |
|
229 | + $errorCount = $this->try_failure($errorCount, "QRDecomposition...", "incorrect qr decomposition calculation"); |
|
230 | 230 | |
231 | 231 | $A = new Matrix($columnwise, 4); |
232 | 232 | $SVD = $A->svd(); |
@@ -234,29 +234,29 @@ discard block |
||
234 | 234 | $S = $SVD->getS(); |
235 | 235 | $V = $SVD->getV(); |
236 | 236 | if ($this->checkMatrices($A, $U->times($S->times($V->transpose())))) |
237 | - $this->try_success("SingularValueDecomposition...",""); |
|
237 | + $this->try_success("SingularValueDecomposition...", ""); |
|
238 | 238 | else |
239 | - $errorCount = $this->try_failure($errorCount,"SingularValueDecomposition...","incorrect singular value decomposition calculation"); |
|
239 | + $errorCount = $this->try_failure($errorCount, "SingularValueDecomposition...", "incorrect singular value decomposition calculation"); |
|
240 | 240 | |
241 | 241 | $n = $A->getColumnDimension(); |
242 | - $A = $A->getMatrix(0,$n-1,0,$n-1); |
|
243 | - $A->set(0,0,0.); |
|
242 | + $A = $A->getMatrix(0, $n - 1, 0, $n - 1); |
|
243 | + $A->set(0, 0, 0.); |
|
244 | 244 | |
245 | 245 | $LU = $A->lu(); |
246 | 246 | $L = $LU->getL(); |
247 | - if ( $this->checkMatrices($A->getMatrix($LU->getPivot(),0,$n-1), $L->times($LU->getU())) ) |
|
248 | - $this->try_success("LUDecomposition...",""); |
|
247 | + if ($this->checkMatrices($A->getMatrix($LU->getPivot(), 0, $n - 1), $L->times($LU->getU()))) |
|
248 | + $this->try_success("LUDecomposition...", ""); |
|
249 | 249 | else |
250 | - $errorCount = $this->try_failure($errorCount,"LUDecomposition...","incorrect LU decomposition calculation"); |
|
250 | + $errorCount = $this->try_failure($errorCount, "LUDecomposition...", "incorrect LU decomposition calculation"); |
|
251 | 251 | |
252 | 252 | $X = $A->inverse(); |
253 | - if ( $this->checkMatrices($A->times($X),Matrix::identity(3,3)) ) |
|
254 | - $this->try_success("inverse()...",""); |
|
253 | + if ($this->checkMatrices($A->times($X), Matrix::identity(3, 3))) |
|
254 | + $this->try_success("inverse()...", ""); |
|
255 | 255 | else |
256 | - $errorCount = $this->try_failure($errorCount, "inverse()...","incorrect inverse calculation"); |
|
256 | + $errorCount = $this->try_failure($errorCount, "inverse()...", "incorrect inverse calculation"); |
|
257 | 257 | |
258 | 258 | $DEF = new Matrix($rankdef); |
259 | - if($this->checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension())-1)) |
|
259 | + if ($this->checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension()) - 1)) |
|
260 | 260 | $this->try_success("Rank..."); |
261 | 261 | else |
262 | 262 | $this->try_failure("Rank...", "incorrect rank calculation"); |
@@ -264,50 +264,50 @@ discard block |
||
264 | 264 | $B = new Matrix($condmat); |
265 | 265 | $SVD = $B->svd(); |
266 | 266 | $singularvalues = $SVD->getSingularValues(); |
267 | - if($this->checkScalars($B->cond(), $singularvalues[0]/$singularvalues[min($B->getRowDimension(), $B->getColumnDimension())-1])) |
|
267 | + if ($this->checkScalars($B->cond(), $singularvalues[0] / $singularvalues[min($B->getRowDimension(), $B->getColumnDimension()) - 1])) |
|
268 | 268 | $this->try_success("Condition number..."); |
269 | 269 | else |
270 | 270 | $this->try_failure("Condition number...", "incorrect condition number calculation"); |
271 | 271 | |
272 | 272 | $SUB = new Matrix($subavals); |
273 | - $O = new Matrix($SUB->getRowDimension(),1,1.0); |
|
273 | + $O = new Matrix($SUB->getRowDimension(), 1, 1.0); |
|
274 | 274 | $SOL = new Matrix($sqSolution); |
275 | - $SQ = $SUB->getMatrix(0,$SUB->getRowDimension()-1,0,$SUB->getRowDimension()-1); |
|
276 | - if ( $this->checkMatrices($SQ->solve($SOL),$O) ) |
|
277 | - $this->try_success("solve()...",""); |
|
275 | + $SQ = $SUB->getMatrix(0, $SUB->getRowDimension() - 1, 0, $SUB->getRowDimension() - 1); |
|
276 | + if ($this->checkMatrices($SQ->solve($SOL), $O)) |
|
277 | + $this->try_success("solve()...", ""); |
|
278 | 278 | else |
279 | - $errorCount = $this->try_failure($errorCount,"solve()...","incorrect lu solve calculation"); |
|
279 | + $errorCount = $this->try_failure($errorCount, "solve()...", "incorrect lu solve calculation"); |
|
280 | 280 | |
281 | 281 | $A = new Matrix($pvals); |
282 | 282 | $Chol = $A->chol(); |
283 | 283 | $L = $Chol->getL(); |
284 | - if ( $this->checkMatrices($A, $L->times($L->transpose())) ) |
|
285 | - $this->try_success("CholeskyDecomposition...",""); |
|
284 | + if ($this->checkMatrices($A, $L->times($L->transpose()))) |
|
285 | + $this->try_success("CholeskyDecomposition...", ""); |
|
286 | 286 | else |
287 | - $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition...","incorrect Cholesky decomposition calculation"); |
|
287 | + $errorCount = $this->try_failure($errorCount, "CholeskyDecomposition...", "incorrect Cholesky decomposition calculation"); |
|
288 | 288 | |
289 | - $X = $Chol->solve(Matrix::identity(3,3)); |
|
290 | - if ( $this->checkMatrices($A->times($X), Matrix::identity(3,3)) ) |
|
291 | - $this->try_success("CholeskyDecomposition solve()...",""); |
|
289 | + $X = $Chol->solve(Matrix::identity(3, 3)); |
|
290 | + if ($this->checkMatrices($A->times($X), Matrix::identity(3, 3))) |
|
291 | + $this->try_success("CholeskyDecomposition solve()...", ""); |
|
292 | 292 | else |
293 | - $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition solve()...","incorrect Choleskydecomposition solve calculation"); |
|
293 | + $errorCount = $this->try_failure($errorCount, "CholeskyDecomposition solve()...", "incorrect Choleskydecomposition solve calculation"); |
|
294 | 294 | |
295 | 295 | $Eig = $A->eig(); |
296 | 296 | $D = $Eig->getD(); |
297 | 297 | $V = $Eig->getV(); |
298 | - if( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
299 | - $this->try_success("EigenvalueDecomposition (symmetric)...",""); |
|
298 | + if ($this->checkMatrices($A->times($V), $V->times($D))) |
|
299 | + $this->try_success("EigenvalueDecomposition (symmetric)...", ""); |
|
300 | 300 | else |
301 | - $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (symmetric)...","incorrect symmetric Eigenvalue decomposition calculation"); |
|
301 | + $errorCount = $this->try_failure($errorCount, "EigenvalueDecomposition (symmetric)...", "incorrect symmetric Eigenvalue decomposition calculation"); |
|
302 | 302 | |
303 | 303 | $A = new Matrix($evals); |
304 | 304 | $Eig = $A->eig(); |
305 | 305 | $D = $Eig->getD(); |
306 | 306 | $V = $Eig->getV(); |
307 | - if ( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
308 | - $this->try_success("EigenvalueDecomposition (nonsymmetric)...",""); |
|
307 | + if ($this->checkMatrices($A->times($V), $V->times($D))) |
|
308 | + $this->try_success("EigenvalueDecomposition (nonsymmetric)...", ""); |
|
309 | 309 | else |
310 | - $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (nonsymmetric)...","incorrect nonsymmetric Eigenvalue decomposition calculation"); |
|
310 | + $errorCount = $this->try_failure($errorCount, "EigenvalueDecomposition (nonsymmetric)...", "incorrect nonsymmetric Eigenvalue decomposition calculation"); |
|
311 | 311 | |
312 | 312 | print("<b>{$errorCount} total errors</b>."); |
313 | 313 | } |
@@ -318,9 +318,9 @@ discard block |
||
318 | 318 | * @param string $e |
319 | 319 | */ |
320 | 320 | function try_success($s, $e = "") { |
321 | - print "> ". $s ."success<br />"; |
|
321 | + print "> ".$s."success<br />"; |
|
322 | 322 | if ($e != "") |
323 | - print "> Message: ". $e ."<br />"; |
|
323 | + print "> Message: ".$e."<br />"; |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | /** |
@@ -330,8 +330,8 @@ discard block |
||
330 | 330 | * @param string $e |
331 | 331 | * @return int incremented counter |
332 | 332 | */ |
333 | - function try_failure($count, $s, $e="") { |
|
334 | - print "> ". $s ."*** failure ***<br />> Message: ". $e ."<br />"; |
|
333 | + function try_failure($count, $s, $e = "") { |
|
334 | + print "> ".$s."*** failure ***<br />> Message: ".$e."<br />"; |
|
335 | 335 | return ++$count; |
336 | 336 | } |
337 | 337 | |
@@ -342,8 +342,8 @@ discard block |
||
342 | 342 | * @param string $e |
343 | 343 | * @return int incremented counter |
344 | 344 | */ |
345 | - function try_warning($count, $s, $e="") { |
|
346 | - print "> ". $s ."*** warning ***<br />> Message: ". $e ."<br />"; |
|
345 | + function try_warning($count, $s, $e = "") { |
|
346 | + print "> ".$s."*** warning ***<br />> Message: ".$e."<br />"; |
|
347 | 347 | return ++$count; |
348 | 348 | } |
349 | 349 | |
@@ -353,10 +353,10 @@ discard block |
||
353 | 353 | * @param float $y |
354 | 354 | */ |
355 | 355 | function checkScalars($x, $y) { |
356 | - $eps = pow(2.0,-52.0); |
|
357 | - if ($x == 0 & abs($y) < 10*$eps) return; |
|
358 | - if ($y == 0 & abs($x) < 10*$eps) return; |
|
359 | - if (abs($x-$y) > 10 * $eps * max(abs($x),abs($y))) |
|
356 | + $eps = pow(2.0, -52.0); |
|
357 | + if ($x == 0 & abs($y) < 10 * $eps) return; |
|
358 | + if ($y == 0 & abs($x) < 10 * $eps) return; |
|
359 | + if (abs($x - $y) > 10 * $eps * max(abs($x), abs($y))) |
|
360 | 360 | return false; |
361 | 361 | else |
362 | 362 | return true; |
@@ -371,8 +371,8 @@ discard block |
||
371 | 371 | $nx = count($x); |
372 | 372 | $ny = count($y); |
373 | 373 | if ($nx == $ny) |
374 | - for($i=0; $i < $nx; ++$i) |
|
375 | - $this->checkScalars($x[$i],$y[$i]); |
|
374 | + for ($i = 0; $i < $nx; ++$i) |
|
375 | + $this->checkScalars($x[$i], $y[$i]); |
|
376 | 376 | else |
377 | 377 | die("Attempt to compare vectors of different lengths"); |
378 | 378 | } |
@@ -385,7 +385,7 @@ discard block |
||
385 | 385 | function checkArrays($x, $y) { |
386 | 386 | $A = new Matrix($x); |
387 | 387 | $B = new Matrix($y); |
388 | - return $this->checkMatrices($A,$B); |
|
388 | + return $this->checkMatrices($A, $B); |
|
389 | 389 | } |
390 | 390 | |
391 | 391 | /** |
@@ -394,16 +394,16 @@ discard block |
||
394 | 394 | * @param matrix $Y |
395 | 395 | */ |
396 | 396 | function checkMatrices($X = null, $Y = null) { |
397 | - if( $X == null || $Y == null ) |
|
397 | + if ($X == null || $Y == null) |
|
398 | 398 | return false; |
399 | 399 | |
400 | - $eps = pow(2.0,-52.0); |
|
401 | - if ($X->norm1() == 0. & $Y->norm1() < 10*$eps) return true; |
|
402 | - if ($Y->norm1() == 0. & $X->norm1() < 10*$eps) return true; |
|
400 | + $eps = pow(2.0, -52.0); |
|
401 | + if ($X->norm1() == 0. & $Y->norm1() < 10 * $eps) return true; |
|
402 | + if ($Y->norm1() == 0. & $X->norm1() < 10 * $eps) return true; |
|
403 | 403 | |
404 | 404 | $A = $X->minus($Y); |
405 | 405 | |
406 | - if ($A->norm1() > 1000 * $eps * max($X->norm1(),$Y->norm1())) |
|
406 | + if ($A->norm1() > 1000 * $eps * max($X->norm1(), $Y->norm1())) |
|
407 | 407 | die("The norm of (X-Y) is too large: ".$A->norm1()); |
408 | 408 | else |
409 | 409 | return true; |
@@ -64,14 +64,16 @@ discard block |
||
64 | 64 | $A = new Matrix($columnwise, 3); |
65 | 65 | if($A instanceof Matrix) { |
66 | 66 | $this->try_success("Column-packed constructor..."); |
67 | - } else |
|
68 | - $errorCount = $this->try_failure($errorCount, "Column-packed constructor...", "Unable to construct Matrix"); |
|
67 | + } else { |
|
68 | + $errorCount = $this->try_failure($errorCount, "Column-packed constructor...", "Unable to construct Matrix"); |
|
69 | + } |
|
69 | 70 | |
70 | 71 | $T = new Matrix($tvals); |
71 | - if($T instanceof Matrix) |
|
72 | - $this->try_success("2D array constructor..."); |
|
73 | - else |
|
74 | - $errorCount = $this->try_failure($errorCount, "2D array constructor...", "Unable to construct Matrix"); |
|
72 | + if($T instanceof Matrix) { |
|
73 | + $this->try_success("2D array constructor..."); |
|
74 | + } else { |
|
75 | + $errorCount = $this->try_failure($errorCount, "2D array constructor...", "Unable to construct Matrix"); |
|
76 | + } |
|
75 | 77 | |
76 | 78 | $A = new Matrix($columnwise, $validID); |
77 | 79 | $B = new Matrix($avals); |
@@ -83,16 +85,18 @@ discard block |
||
83 | 85 | $tmp = $B->get(0,0); |
84 | 86 | $avals[0][0] = 0.0; |
85 | 87 | /** check that constructWithCopy behaves properly **/ |
86 | - if ( ( $tmp - $B->get(0,0) ) != 0.0 ) |
|
87 | - $errorCount = $this->try_failure($errorCount,"constructWithCopy... ","copy not effected... data visible outside"); |
|
88 | - else |
|
89 | - $this->try_success("constructWithCopy... ",""); |
|
88 | + if ( ( $tmp - $B->get(0,0) ) != 0.0 ) { |
|
89 | + $errorCount = $this->try_failure($errorCount,"constructWithCopy... ","copy not effected... data visible outside"); |
|
90 | + } else { |
|
91 | + $this->try_success("constructWithCopy... ",""); |
|
92 | + } |
|
90 | 93 | |
91 | 94 | $I = new Matrix($ivals); |
92 | - if ( $this->checkMatrices($I,Matrix::identity(3,4)) ) |
|
93 | - $this->try_success("identity... ",""); |
|
94 | - else |
|
95 | - $errorCount = $this->try_failure($errorCount,"identity... ","identity Matrix not successfully created"); |
|
95 | + if ( $this->checkMatrices($I,Matrix::identity(3,4)) ) { |
|
96 | + $this->try_success("identity... ",""); |
|
97 | + } else { |
|
98 | + $errorCount = $this->try_failure($errorCount,"identity... ","identity Matrix not successfully created"); |
|
99 | + } |
|
96 | 100 | |
97 | 101 | /** |
98 | 102 | * Access Methods: |
@@ -117,33 +121,38 @@ discard block |
||
117 | 121 | print "<p>Testing access methods...</p>"; |
118 | 122 | |
119 | 123 | $B = new Matrix($avals); |
120 | - if($B->getRowDimension() == $rows) |
|
121 | - $this->try_success("getRowDimension..."); |
|
122 | - else |
|
123 | - $errorCount = $this->try_failure($errorCount, "getRowDimension..."); |
|
124 | - |
|
125 | - if($B->getColumnDimension() == $cols) |
|
126 | - $this->try_success("getColumnDimension..."); |
|
127 | - else |
|
128 | - $errorCount = $this->try_failure($errorCount, "getColumnDimension..."); |
|
124 | + if($B->getRowDimension() == $rows) { |
|
125 | + $this->try_success("getRowDimension..."); |
|
126 | + } else { |
|
127 | + $errorCount = $this->try_failure($errorCount, "getRowDimension..."); |
|
128 | + } |
|
129 | + |
|
130 | + if($B->getColumnDimension() == $cols) { |
|
131 | + $this->try_success("getColumnDimension..."); |
|
132 | + } else { |
|
133 | + $errorCount = $this->try_failure($errorCount, "getColumnDimension..."); |
|
134 | + } |
|
129 | 135 | |
130 | 136 | $barray = $B->getArray(); |
131 | - if($this->checkArrays($barray, $avals)) |
|
132 | - $this->try_success("getArray..."); |
|
133 | - else |
|
134 | - $errorCount = $this->try_failure($errorCount, "getArray..."); |
|
137 | + if($this->checkArrays($barray, $avals)) { |
|
138 | + $this->try_success("getArray..."); |
|
139 | + } else { |
|
140 | + $errorCount = $this->try_failure($errorCount, "getArray..."); |
|
141 | + } |
|
135 | 142 | |
136 | 143 | $bpacked = $B->getColumnPackedCopy(); |
137 | - if($this->checkArrays($bpacked, $columnwise)) |
|
138 | - $this->try_success("getColumnPackedCopy..."); |
|
139 | - else |
|
140 | - $errorCount = $this->try_failure($errorCount, "getColumnPackedCopy..."); |
|
144 | + if($this->checkArrays($bpacked, $columnwise)) { |
|
145 | + $this->try_success("getColumnPackedCopy..."); |
|
146 | + } else { |
|
147 | + $errorCount = $this->try_failure($errorCount, "getColumnPackedCopy..."); |
|
148 | + } |
|
141 | 149 | |
142 | 150 | $bpacked = $B->getRowPackedCopy(); |
143 | - if($this->checkArrays($bpacked, $rowwise)) |
|
144 | - $this->try_success("getRowPackedCopy..."); |
|
145 | - else |
|
146 | - $errorCount = $this->try_failure($errorCount, "getRowPackedCopy..."); |
|
151 | + if($this->checkArrays($bpacked, $rowwise)) { |
|
152 | + $this->try_success("getRowPackedCopy..."); |
|
153 | + } else { |
|
154 | + $errorCount = $this->try_failure($errorCount, "getRowPackedCopy..."); |
|
155 | + } |
|
147 | 156 | |
148 | 157 | /** |
149 | 158 | * Array-like methods: |
@@ -177,42 +186,48 @@ discard block |
||
177 | 186 | echo "<p>Testing linear algebra methods...<p>"; |
178 | 187 | |
179 | 188 | $A = new Matrix($columnwise, 3); |
180 | - if( $this->checkMatrices($A->transpose(), $T) ) |
|
181 | - $this->try_success("Transpose check..."); |
|
182 | - else |
|
183 | - $errorCount = $this->try_failure($errorCount, "Transpose check...", "Matrices are not equal"); |
|
184 | - |
|
185 | - if($this->checkScalars($A->norm1(), $columnsummax)) |
|
186 | - $this->try_success("Maximum column sum..."); |
|
187 | - else |
|
188 | - $errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: " . $A->norm1() . " != " . $columnsummax); |
|
189 | - |
|
190 | - if($this->checkScalars($A->normInf(), $rowsummax)) |
|
191 | - $this->try_success("Maximum row sum..."); |
|
192 | - else |
|
193 | - $errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: " . $A->normInf() . " != " . $rowsummax ); |
|
194 | - |
|
195 | - if($this->checkScalars($A->normF(), sqrt($sumofsquares))) |
|
196 | - $this->try_success("Frobenius norm..."); |
|
197 | - else |
|
198 | - $errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:" . $A->normF() . " != " . sqrt($sumofsquares)); |
|
199 | - |
|
200 | - if($this->checkScalars($A->trace(), $sumofdiagonals)) |
|
201 | - $this->try_success("Matrix trace..."); |
|
202 | - else |
|
203 | - $errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: " . $A->trace() . " != " . $sumofdiagonals); |
|
189 | + if( $this->checkMatrices($A->transpose(), $T) ) { |
|
190 | + $this->try_success("Transpose check..."); |
|
191 | + } else { |
|
192 | + $errorCount = $this->try_failure($errorCount, "Transpose check...", "Matrices are not equal"); |
|
193 | + } |
|
194 | + |
|
195 | + if($this->checkScalars($A->norm1(), $columnsummax)) { |
|
196 | + $this->try_success("Maximum column sum..."); |
|
197 | + } else { |
|
198 | + $errorCount = $this->try_failure($errorCount, "Maximum column sum...", "Incorrect: " . $A->norm1() . " != " . $columnsummax); |
|
199 | + } |
|
200 | + |
|
201 | + if($this->checkScalars($A->normInf(), $rowsummax)) { |
|
202 | + $this->try_success("Maximum row sum..."); |
|
203 | + } else { |
|
204 | + $errorCount = $this->try_failure($errorCount, "Maximum row sum...", "Incorrect: " . $A->normInf() . " != " . $rowsummax ); |
|
205 | + } |
|
206 | + |
|
207 | + if($this->checkScalars($A->normF(), sqrt($sumofsquares))) { |
|
208 | + $this->try_success("Frobenius norm..."); |
|
209 | + } else { |
|
210 | + $errorCount = $this->try_failure($errorCount, "Frobenius norm...", "Incorrect:" . $A->normF() . " != " . sqrt($sumofsquares)); |
|
211 | + } |
|
212 | + |
|
213 | + if($this->checkScalars($A->trace(), $sumofdiagonals)) { |
|
214 | + $this->try_success("Matrix trace..."); |
|
215 | + } else { |
|
216 | + $errorCount = $this->try_failure($errorCount, "Matrix trace...", "Incorrect: " . $A->trace() . " != " . $sumofdiagonals); |
|
217 | + } |
|
204 | 218 | |
205 | 219 | $B = $A->getMatrix(0, $A->getRowDimension(), 0, $A->getRowDimension()); |
206 | - if( $B->det() == 0 ) |
|
207 | - $this->try_success("Matrix determinant..."); |
|
208 | - else |
|
209 | - $errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: " . $B->det() . " != " . 0); |
|
220 | + if( $B->det() == 0 ) { |
|
221 | + $this->try_success("Matrix determinant..."); |
|
222 | + } else { |
|
223 | + $errorCount = $this->try_failure($errorCount, "Matrix determinant...", "Incorrect: " . $B->det() . " != " . 0); |
|
224 | + } |
|
210 | 225 | |
211 | 226 | $A = new Matrix($columnwise,3); |
212 | 227 | $SQ = new Matrix($square); |
213 | - if ($this->checkMatrices($SQ, $A->times($A->transpose()))) |
|
214 | - $this->try_success("times(Matrix)..."); |
|
215 | - else { |
|
228 | + if ($this->checkMatrices($SQ, $A->times($A->transpose()))) { |
|
229 | + $this->try_success("times(Matrix)..."); |
|
230 | + } else { |
|
216 | 231 | $errorCount = $this->try_failure($errorCount, "times(Matrix)...", "Unable to multiply matrices"); |
217 | 232 | $SQ->toHTML(); |
218 | 233 | $AT->toHTML(); |
@@ -223,20 +238,22 @@ discard block |
||
223 | 238 | $QR = $A->qr(); |
224 | 239 | $R = $QR->getR(); |
225 | 240 | $Q = $QR->getQ(); |
226 | - if($this->checkMatrices($A, $Q->times($R))) |
|
227 | - $this->try_success("QRDecomposition...",""); |
|
228 | - else |
|
229 | - $errorCount = $this->try_failure($errorCount,"QRDecomposition...","incorrect qr decomposition calculation"); |
|
241 | + if($this->checkMatrices($A, $Q->times($R))) { |
|
242 | + $this->try_success("QRDecomposition...",""); |
|
243 | + } else { |
|
244 | + $errorCount = $this->try_failure($errorCount,"QRDecomposition...","incorrect qr decomposition calculation"); |
|
245 | + } |
|
230 | 246 | |
231 | 247 | $A = new Matrix($columnwise, 4); |
232 | 248 | $SVD = $A->svd(); |
233 | 249 | $U = $SVD->getU(); |
234 | 250 | $S = $SVD->getS(); |
235 | 251 | $V = $SVD->getV(); |
236 | - if ($this->checkMatrices($A, $U->times($S->times($V->transpose())))) |
|
237 | - $this->try_success("SingularValueDecomposition...",""); |
|
238 | - else |
|
239 | - $errorCount = $this->try_failure($errorCount,"SingularValueDecomposition...","incorrect singular value decomposition calculation"); |
|
252 | + if ($this->checkMatrices($A, $U->times($S->times($V->transpose())))) { |
|
253 | + $this->try_success("SingularValueDecomposition...",""); |
|
254 | + } else { |
|
255 | + $errorCount = $this->try_failure($errorCount,"SingularValueDecomposition...","incorrect singular value decomposition calculation"); |
|
256 | + } |
|
240 | 257 | |
241 | 258 | $n = $A->getColumnDimension(); |
242 | 259 | $A = $A->getMatrix(0,$n-1,0,$n-1); |
@@ -244,70 +261,79 @@ discard block |
||
244 | 261 | |
245 | 262 | $LU = $A->lu(); |
246 | 263 | $L = $LU->getL(); |
247 | - if ( $this->checkMatrices($A->getMatrix($LU->getPivot(),0,$n-1), $L->times($LU->getU())) ) |
|
248 | - $this->try_success("LUDecomposition...",""); |
|
249 | - else |
|
250 | - $errorCount = $this->try_failure($errorCount,"LUDecomposition...","incorrect LU decomposition calculation"); |
|
264 | + if ( $this->checkMatrices($A->getMatrix($LU->getPivot(),0,$n-1), $L->times($LU->getU())) ) { |
|
265 | + $this->try_success("LUDecomposition...",""); |
|
266 | + } else { |
|
267 | + $errorCount = $this->try_failure($errorCount,"LUDecomposition...","incorrect LU decomposition calculation"); |
|
268 | + } |
|
251 | 269 | |
252 | 270 | $X = $A->inverse(); |
253 | - if ( $this->checkMatrices($A->times($X),Matrix::identity(3,3)) ) |
|
254 | - $this->try_success("inverse()...",""); |
|
255 | - else |
|
256 | - $errorCount = $this->try_failure($errorCount, "inverse()...","incorrect inverse calculation"); |
|
271 | + if ( $this->checkMatrices($A->times($X),Matrix::identity(3,3)) ) { |
|
272 | + $this->try_success("inverse()...",""); |
|
273 | + } else { |
|
274 | + $errorCount = $this->try_failure($errorCount, "inverse()...","incorrect inverse calculation"); |
|
275 | + } |
|
257 | 276 | |
258 | 277 | $DEF = new Matrix($rankdef); |
259 | - if($this->checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension())-1)) |
|
260 | - $this->try_success("Rank..."); |
|
261 | - else |
|
262 | - $this->try_failure("Rank...", "incorrect rank calculation"); |
|
278 | + if($this->checkScalars($DEF->rank(), min($DEF->getRowDimension(), $DEF->getColumnDimension())-1)) { |
|
279 | + $this->try_success("Rank..."); |
|
280 | + } else { |
|
281 | + $this->try_failure("Rank...", "incorrect rank calculation"); |
|
282 | + } |
|
263 | 283 | |
264 | 284 | $B = new Matrix($condmat); |
265 | 285 | $SVD = $B->svd(); |
266 | 286 | $singularvalues = $SVD->getSingularValues(); |
267 | - if($this->checkScalars($B->cond(), $singularvalues[0]/$singularvalues[min($B->getRowDimension(), $B->getColumnDimension())-1])) |
|
268 | - $this->try_success("Condition number..."); |
|
269 | - else |
|
270 | - $this->try_failure("Condition number...", "incorrect condition number calculation"); |
|
287 | + if($this->checkScalars($B->cond(), $singularvalues[0]/$singularvalues[min($B->getRowDimension(), $B->getColumnDimension())-1])) { |
|
288 | + $this->try_success("Condition number..."); |
|
289 | + } else { |
|
290 | + $this->try_failure("Condition number...", "incorrect condition number calculation"); |
|
291 | + } |
|
271 | 292 | |
272 | 293 | $SUB = new Matrix($subavals); |
273 | 294 | $O = new Matrix($SUB->getRowDimension(),1,1.0); |
274 | 295 | $SOL = new Matrix($sqSolution); |
275 | 296 | $SQ = $SUB->getMatrix(0,$SUB->getRowDimension()-1,0,$SUB->getRowDimension()-1); |
276 | - if ( $this->checkMatrices($SQ->solve($SOL),$O) ) |
|
277 | - $this->try_success("solve()...",""); |
|
278 | - else |
|
279 | - $errorCount = $this->try_failure($errorCount,"solve()...","incorrect lu solve calculation"); |
|
297 | + if ( $this->checkMatrices($SQ->solve($SOL),$O) ) { |
|
298 | + $this->try_success("solve()...",""); |
|
299 | + } else { |
|
300 | + $errorCount = $this->try_failure($errorCount,"solve()...","incorrect lu solve calculation"); |
|
301 | + } |
|
280 | 302 | |
281 | 303 | $A = new Matrix($pvals); |
282 | 304 | $Chol = $A->chol(); |
283 | 305 | $L = $Chol->getL(); |
284 | - if ( $this->checkMatrices($A, $L->times($L->transpose())) ) |
|
285 | - $this->try_success("CholeskyDecomposition...",""); |
|
286 | - else |
|
287 | - $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition...","incorrect Cholesky decomposition calculation"); |
|
306 | + if ( $this->checkMatrices($A, $L->times($L->transpose())) ) { |
|
307 | + $this->try_success("CholeskyDecomposition...",""); |
|
308 | + } else { |
|
309 | + $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition...","incorrect Cholesky decomposition calculation"); |
|
310 | + } |
|
288 | 311 | |
289 | 312 | $X = $Chol->solve(Matrix::identity(3,3)); |
290 | - if ( $this->checkMatrices($A->times($X), Matrix::identity(3,3)) ) |
|
291 | - $this->try_success("CholeskyDecomposition solve()...",""); |
|
292 | - else |
|
293 | - $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition solve()...","incorrect Choleskydecomposition solve calculation"); |
|
313 | + if ( $this->checkMatrices($A->times($X), Matrix::identity(3,3)) ) { |
|
314 | + $this->try_success("CholeskyDecomposition solve()...",""); |
|
315 | + } else { |
|
316 | + $errorCount = $this->try_failure($errorCount,"CholeskyDecomposition solve()...","incorrect Choleskydecomposition solve calculation"); |
|
317 | + } |
|
294 | 318 | |
295 | 319 | $Eig = $A->eig(); |
296 | 320 | $D = $Eig->getD(); |
297 | 321 | $V = $Eig->getV(); |
298 | - if( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
299 | - $this->try_success("EigenvalueDecomposition (symmetric)...",""); |
|
300 | - else |
|
301 | - $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (symmetric)...","incorrect symmetric Eigenvalue decomposition calculation"); |
|
322 | + if( $this->checkMatrices($A->times($V),$V->times($D)) ) { |
|
323 | + $this->try_success("EigenvalueDecomposition (symmetric)...",""); |
|
324 | + } else { |
|
325 | + $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (symmetric)...","incorrect symmetric Eigenvalue decomposition calculation"); |
|
326 | + } |
|
302 | 327 | |
303 | 328 | $A = new Matrix($evals); |
304 | 329 | $Eig = $A->eig(); |
305 | 330 | $D = $Eig->getD(); |
306 | 331 | $V = $Eig->getV(); |
307 | - if ( $this->checkMatrices($A->times($V),$V->times($D)) ) |
|
308 | - $this->try_success("EigenvalueDecomposition (nonsymmetric)...",""); |
|
309 | - else |
|
310 | - $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (nonsymmetric)...","incorrect nonsymmetric Eigenvalue decomposition calculation"); |
|
332 | + if ( $this->checkMatrices($A->times($V),$V->times($D)) ) { |
|
333 | + $this->try_success("EigenvalueDecomposition (nonsymmetric)...",""); |
|
334 | + } else { |
|
335 | + $errorCount = $this->try_failure($errorCount,"EigenvalueDecomposition (nonsymmetric)...","incorrect nonsymmetric Eigenvalue decomposition calculation"); |
|
336 | + } |
|
311 | 337 | |
312 | 338 | print("<b>{$errorCount} total errors</b>."); |
313 | 339 | } |
@@ -319,8 +345,9 @@ discard block |
||
319 | 345 | */ |
320 | 346 | function try_success($s, $e = "") { |
321 | 347 | print "> ". $s ."success<br />"; |
322 | - if ($e != "") |
|
323 | - print "> Message: ". $e ."<br />"; |
|
348 | + if ($e != "") { |
|
349 | + print "> Message: ". $e ."<br />"; |
|
350 | + } |
|
324 | 351 | } |
325 | 352 | |
326 | 353 | /** |
@@ -354,12 +381,17 @@ discard block |
||
354 | 381 | */ |
355 | 382 | function checkScalars($x, $y) { |
356 | 383 | $eps = pow(2.0,-52.0); |
357 | - if ($x == 0 & abs($y) < 10*$eps) return; |
|
358 | - if ($y == 0 & abs($x) < 10*$eps) return; |
|
359 | - if (abs($x-$y) > 10 * $eps * max(abs($x),abs($y))) |
|
360 | - return false; |
|
361 | - else |
|
362 | - return true; |
|
384 | + if ($x == 0 & abs($y) < 10*$eps) { |
|
385 | + return; |
|
386 | + } |
|
387 | + if ($y == 0 & abs($x) < 10*$eps) { |
|
388 | + return; |
|
389 | + } |
|
390 | + if (abs($x-$y) > 10 * $eps * max(abs($x),abs($y))) { |
|
391 | + return false; |
|
392 | + } else { |
|
393 | + return true; |
|
394 | + } |
|
363 | 395 | } |
364 | 396 | |
365 | 397 | /** |
@@ -370,11 +402,15 @@ discard block |
||
370 | 402 | function checkVectors($x, $y) { |
371 | 403 | $nx = count($x); |
372 | 404 | $ny = count($y); |
373 | - if ($nx == $ny) |
|
374 | - for($i=0; $i < $nx; ++$i) |
|
375 | - $this->checkScalars($x[$i],$y[$i]); |
|
376 | - else |
|
377 | - die("Attempt to compare vectors of different lengths"); |
|
405 | + if ($nx == $ny) { |
|
406 | + for($i=0; |
|
407 | + } |
|
408 | + $i < $nx; ++$i) { |
|
409 | + $this->checkScalars($x[$i],$y[$i]); |
|
410 | + } |
|
411 | + else { |
|
412 | + die("Attempt to compare vectors of different lengths"); |
|
413 | + } |
|
378 | 414 | } |
379 | 415 | |
380 | 416 | /** |
@@ -394,19 +430,25 @@ discard block |
||
394 | 430 | * @param matrix $Y |
395 | 431 | */ |
396 | 432 | function checkMatrices($X = null, $Y = null) { |
397 | - if( $X == null || $Y == null ) |
|
398 | - return false; |
|
433 | + if( $X == null || $Y == null ) { |
|
434 | + return false; |
|
435 | + } |
|
399 | 436 | |
400 | 437 | $eps = pow(2.0,-52.0); |
401 | - if ($X->norm1() == 0. & $Y->norm1() < 10*$eps) return true; |
|
402 | - if ($Y->norm1() == 0. & $X->norm1() < 10*$eps) return true; |
|
438 | + if ($X->norm1() == 0. & $Y->norm1() < 10*$eps) { |
|
439 | + return true; |
|
440 | + } |
|
441 | + if ($Y->norm1() == 0. & $X->norm1() < 10*$eps) { |
|
442 | + return true; |
|
443 | + } |
|
403 | 444 | |
404 | 445 | $A = $X->minus($Y); |
405 | 446 | |
406 | - if ($A->norm1() > 1000 * $eps * max($X->norm1(),$Y->norm1())) |
|
407 | - die("The norm of (X-Y) is too large: ".$A->norm1()); |
|
408 | - else |
|
409 | - return true; |
|
447 | + if ($A->norm1() > 1000 * $eps * max($X->norm1(),$Y->norm1())) { |
|
448 | + die("The norm of (X-Y) is too large: ".$A->norm1()); |
|
449 | + } else { |
|
450 | + return true; |
|
451 | + } |
|
410 | 452 | } |
411 | 453 | |
412 | 454 | } |
@@ -65,7 +65,7 @@ |
||
65 | 65 | |
66 | 66 | /** |
67 | 67 | * Custom error handler |
68 | - * @param int $num Error number |
|
68 | + * @return string|null |
|
69 | 69 | */ |
70 | 70 | function JAMAError($errorNumber = null) { |
71 | 71 | global $error; |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | * |
92 | 92 | * @acces public |
93 | 93 | * @param string $file |
94 | - * @return mixed true on success, PEAR_Error on failure |
|
94 | + * @return boolean true on success, PEAR_Error on failure |
|
95 | 95 | */ |
96 | 96 | public function read($file) |
97 | 97 | { |
@@ -222,6 +222,7 @@ discard block |
||
222 | 222 | /** |
223 | 223 | * Reads a signed char. |
224 | 224 | * @param resource file handle |
225 | + * @param resource $fh |
|
225 | 226 | * @return int |
226 | 227 | * @access public |
227 | 228 | */ |
@@ -234,6 +235,7 @@ discard block |
||
234 | 235 | /** |
235 | 236 | * Reads an unsigned short (2 octets). |
236 | 237 | * @param resource file handle |
238 | + * @param resource $fh |
|
237 | 239 | * @return int |
238 | 240 | * @access public |
239 | 241 | */ |
@@ -246,6 +248,7 @@ discard block |
||
246 | 248 | /** |
247 | 249 | * Reads an unsigned long (4 octets). |
248 | 250 | * @param resource file handle |
251 | + * @param resource $fh |
|
249 | 252 | * @return int |
250 | 253 | * @access public |
251 | 254 | */ |
@@ -261,7 +264,8 @@ discard block |
||
261 | 264 | * |
262 | 265 | * @access public |
263 | 266 | * @param integer the block id of the first block |
264 | - * @return mixed true on success, PEAR_Error on failure |
|
267 | + * @param integer $blockId |
|
268 | + * @return boolean true on success, PEAR_Error on failure |
|
265 | 269 | */ |
266 | 270 | public function _readPpsWks($blockId) |
267 | 271 | { |
@@ -47,43 +47,43 @@ discard block |
||
47 | 47 | /** |
48 | 48 | * The file handle for reading an OLE container |
49 | 49 | * @var resource |
50 | - */ |
|
50 | + */ |
|
51 | 51 | public $_file_handle; |
52 | 52 | |
53 | 53 | /** |
54 | - * Array of PPS's found on the OLE container |
|
55 | - * @var array |
|
56 | - */ |
|
54 | + * Array of PPS's found on the OLE container |
|
55 | + * @var array |
|
56 | + */ |
|
57 | 57 | public $_list = array(); |
58 | 58 | |
59 | 59 | /** |
60 | 60 | * Root directory of OLE container |
61 | 61 | * @var OLE_PPS_Root |
62 | - */ |
|
62 | + */ |
|
63 | 63 | public $root; |
64 | 64 | |
65 | 65 | /** |
66 | 66 | * Big Block Allocation Table |
67 | 67 | * @var array (blockId => nextBlockId) |
68 | - */ |
|
68 | + */ |
|
69 | 69 | public $bbat; |
70 | 70 | |
71 | 71 | /** |
72 | 72 | * Short Block Allocation Table |
73 | 73 | * @var array (blockId => nextBlockId) |
74 | - */ |
|
74 | + */ |
|
75 | 75 | public $sbat; |
76 | 76 | |
77 | 77 | /** |
78 | 78 | * Size of big blocks. This is usually 512. |
79 | 79 | * @var int number of octets per block. |
80 | - */ |
|
80 | + */ |
|
81 | 81 | public $bigBlockSize; |
82 | 82 | |
83 | 83 | /** |
84 | 84 | * Size of small blocks. This is usually 64. |
85 | 85 | * @var int number of octets per block |
86 | - */ |
|
86 | + */ |
|
87 | 87 | public $smallBlockSize; |
88 | 88 | |
89 | 89 | /** |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | * @acces public |
93 | 93 | * @param string $file |
94 | 94 | * @return mixed true on success, PEAR_Error on failure |
95 | - */ |
|
95 | + */ |
|
96 | 96 | public function read($file) |
97 | 97 | { |
98 | 98 | $fh = fopen($file, "r"); |
@@ -189,11 +189,11 @@ discard block |
||
189 | 189 | } |
190 | 190 | |
191 | 191 | /** |
192 | - * Returns a stream for use with fread() etc. External callers should |
|
193 | - * use PHPExcel_Shared_OLE_PPS_File::getStream(). |
|
194 | - * @param int|PPS block id or PPS |
|
195 | - * @return resource read-only stream |
|
196 | - */ |
|
192 | + * Returns a stream for use with fread() etc. External callers should |
|
193 | + * use PHPExcel_Shared_OLE_PPS_File::getStream(). |
|
194 | + * @param int|PPS block id or PPS |
|
195 | + * @return resource read-only stream |
|
196 | + */ |
|
197 | 197 | public function getStream($blockIdOrPps) |
198 | 198 | { |
199 | 199 | static $isRegistered = false; |
@@ -256,13 +256,13 @@ discard block |
||
256 | 256 | } |
257 | 257 | |
258 | 258 | /** |
259 | - * Gets information about all PPS's on the OLE container from the PPS WK's |
|
260 | - * creates an OLE_PPS object for each one. |
|
261 | - * |
|
262 | - * @access public |
|
263 | - * @param integer the block id of the first block |
|
264 | - * @return mixed true on success, PEAR_Error on failure |
|
265 | - */ |
|
259 | + * Gets information about all PPS's on the OLE container from the PPS WK's |
|
260 | + * creates an OLE_PPS object for each one. |
|
261 | + * |
|
262 | + * @access public |
|
263 | + * @param integer the block id of the first block |
|
264 | + * @return mixed true on success, PEAR_Error on failure |
|
265 | + */ |
|
266 | 266 | public function _readPpsWks($blockId) |
267 | 267 | { |
268 | 268 | $fh = $this->getStream($blockId); |
@@ -333,13 +333,13 @@ discard block |
||
333 | 333 | } |
334 | 334 | |
335 | 335 | /** |
336 | - * It checks whether the PPS tree is complete (all PPS's read) |
|
337 | - * starting with the given PPS (not necessarily root) |
|
338 | - * |
|
339 | - * @access public |
|
340 | - * @param integer $index The index of the PPS from which we are checking |
|
341 | - * @return boolean Whether the PPS tree for the given PPS is complete |
|
342 | - */ |
|
336 | + * It checks whether the PPS tree is complete (all PPS's read) |
|
337 | + * starting with the given PPS (not necessarily root) |
|
338 | + * |
|
339 | + * @access public |
|
340 | + * @param integer $index The index of the PPS from which we are checking |
|
341 | + * @return boolean Whether the PPS tree for the given PPS is complete |
|
342 | + */ |
|
343 | 343 | public function _ppsTreeComplete($index) |
344 | 344 | { |
345 | 345 | return isset($this->_list[$index]) && |
@@ -353,13 +353,13 @@ discard block |
||
353 | 353 | } |
354 | 354 | |
355 | 355 | /** |
356 | - * Checks whether a PPS is a File PPS or not. |
|
357 | - * If there is no PPS for the index given, it will return false. |
|
358 | - * |
|
359 | - * @access public |
|
360 | - * @param integer $index The index for the PPS |
|
361 | - * @return bool true if it's a File PPS, false otherwise |
|
362 | - */ |
|
356 | + * Checks whether a PPS is a File PPS or not. |
|
357 | + * If there is no PPS for the index given, it will return false. |
|
358 | + * |
|
359 | + * @access public |
|
360 | + * @param integer $index The index for the PPS |
|
361 | + * @return bool true if it's a File PPS, false otherwise |
|
362 | + */ |
|
363 | 363 | public function isFile($index) |
364 | 364 | { |
365 | 365 | if (isset($this->_list[$index])) { |
@@ -369,13 +369,13 @@ discard block |
||
369 | 369 | } |
370 | 370 | |
371 | 371 | /** |
372 | - * Checks whether a PPS is a Root PPS or not. |
|
373 | - * If there is no PPS for the index given, it will return false. |
|
374 | - * |
|
375 | - * @access public |
|
376 | - * @param integer $index The index for the PPS. |
|
377 | - * @return bool true if it's a Root PPS, false otherwise |
|
378 | - */ |
|
372 | + * Checks whether a PPS is a Root PPS or not. |
|
373 | + * If there is no PPS for the index given, it will return false. |
|
374 | + * |
|
375 | + * @access public |
|
376 | + * @param integer $index The index for the PPS. |
|
377 | + * @return bool true if it's a Root PPS, false otherwise |
|
378 | + */ |
|
379 | 379 | public function isRoot($index) |
380 | 380 | { |
381 | 381 | if (isset($this->_list[$index])) { |
@@ -385,28 +385,28 @@ discard block |
||
385 | 385 | } |
386 | 386 | |
387 | 387 | /** |
388 | - * Gives the total number of PPS's found in the OLE container. |
|
389 | - * |
|
390 | - * @access public |
|
391 | - * @return integer The total number of PPS's found in the OLE container |
|
392 | - */ |
|
388 | + * Gives the total number of PPS's found in the OLE container. |
|
389 | + * |
|
390 | + * @access public |
|
391 | + * @return integer The total number of PPS's found in the OLE container |
|
392 | + */ |
|
393 | 393 | public function ppsTotal() |
394 | 394 | { |
395 | 395 | return count($this->_list); |
396 | 396 | } |
397 | 397 | |
398 | 398 | /** |
399 | - * Gets data from a PPS |
|
400 | - * If there is no PPS for the index given, it will return an empty string. |
|
401 | - * |
|
402 | - * @access public |
|
403 | - * @param integer $index The index for the PPS |
|
404 | - * @param integer $position The position from which to start reading |
|
405 | - * (relative to the PPS) |
|
406 | - * @param integer $length The amount of bytes to read (at most) |
|
407 | - * @return string The binary string containing the data requested |
|
408 | - * @see OLE_PPS_File::getStream() |
|
409 | - */ |
|
399 | + * Gets data from a PPS |
|
400 | + * If there is no PPS for the index given, it will return an empty string. |
|
401 | + * |
|
402 | + * @access public |
|
403 | + * @param integer $index The index for the PPS |
|
404 | + * @param integer $position The position from which to start reading |
|
405 | + * (relative to the PPS) |
|
406 | + * @param integer $length The amount of bytes to read (at most) |
|
407 | + * @return string The binary string containing the data requested |
|
408 | + * @see OLE_PPS_File::getStream() |
|
409 | + */ |
|
410 | 410 | public function getData($index, $position, $length) |
411 | 411 | { |
412 | 412 | // if position is not valid return empty string |
@@ -420,13 +420,13 @@ discard block |
||
420 | 420 | } |
421 | 421 | |
422 | 422 | /** |
423 | - * Gets the data length from a PPS |
|
424 | - * If there is no PPS for the index given, it will return 0. |
|
425 | - * |
|
426 | - * @access public |
|
427 | - * @param integer $index The index for the PPS |
|
428 | - * @return integer The amount of bytes in data the PPS has |
|
429 | - */ |
|
423 | + * Gets the data length from a PPS |
|
424 | + * If there is no PPS for the index given, it will return 0. |
|
425 | + * |
|
426 | + * @access public |
|
427 | + * @param integer $index The index for the PPS |
|
428 | + * @return integer The amount of bytes in data the PPS has |
|
429 | + */ |
|
430 | 430 | public function getDataLength($index) |
431 | 431 | { |
432 | 432 | if (isset($this->_list[$index])) { |
@@ -436,13 +436,13 @@ discard block |
||
436 | 436 | } |
437 | 437 | |
438 | 438 | /** |
439 | - * Utility function to transform ASCII text to Unicode |
|
440 | - * |
|
441 | - * @access public |
|
442 | - * @static |
|
443 | - * @param string $ascii The ASCII string to transform |
|
444 | - * @return string The string in Unicode |
|
445 | - */ |
|
439 | + * Utility function to transform ASCII text to Unicode |
|
440 | + * |
|
441 | + * @access public |
|
442 | + * @static |
|
443 | + * @param string $ascii The ASCII string to transform |
|
444 | + * @return string The string in Unicode |
|
445 | + */ |
|
446 | 446 | public static function Asc2Ucs($ascii) |
447 | 447 | { |
448 | 448 | $rawname = ''; |
@@ -453,14 +453,14 @@ discard block |
||
453 | 453 | } |
454 | 454 | |
455 | 455 | /** |
456 | - * Utility function |
|
457 | - * Returns a string for the OLE container with the date given |
|
458 | - * |
|
459 | - * @access public |
|
460 | - * @static |
|
461 | - * @param integer $date A timestamp |
|
462 | - * @return string The string for the OLE container |
|
463 | - */ |
|
456 | + * Utility function |
|
457 | + * Returns a string for the OLE container with the date given |
|
458 | + * |
|
459 | + * @access public |
|
460 | + * @static |
|
461 | + * @param integer $date A timestamp |
|
462 | + * @return string The string for the OLE container |
|
463 | + */ |
|
464 | 464 | public static function LocalDate2OLE($date = null) |
465 | 465 | { |
466 | 466 | if (!isset($date)) { |
@@ -499,13 +499,13 @@ discard block |
||
499 | 499 | } |
500 | 500 | |
501 | 501 | /** |
502 | - * Returns a timestamp from an OLE container's date |
|
503 | - * |
|
504 | - * @access public |
|
505 | - * @static |
|
506 | - * @param integer $string A binary string with the encoded date |
|
507 | - * @return string The timestamp corresponding to the string |
|
508 | - */ |
|
502 | + * Returns a timestamp from an OLE container's date |
|
503 | + * |
|
504 | + * @access public |
|
505 | + * @static |
|
506 | + * @param integer $string A binary string with the encoded date |
|
507 | + * @return string The timestamp corresponding to the string |
|
508 | + */ |
|
509 | 509 | public static function OLE2LocalDate($string) |
510 | 510 | { |
511 | 511 | if (strlen($string) != 8) { |
@@ -275,19 +275,19 @@ |
||
275 | 275 | $name = str_replace("\x00", "", $nameUtf16); |
276 | 276 | $type = self::_readInt1($fh); |
277 | 277 | switch ($type) { |
278 | - case self::OLE_PPS_TYPE_ROOT: |
|
279 | - $pps = new PHPExcel_Shared_OLE_PPS_Root(null, null, array()); |
|
280 | - $this->root = $pps; |
|
281 | - break; |
|
282 | - case self::OLE_PPS_TYPE_DIR: |
|
283 | - $pps = new PHPExcel_Shared_OLE_PPS(null, null, null, null, null, |
|
284 | - null, null, null, null, array()); |
|
285 | - break; |
|
286 | - case self::OLE_PPS_TYPE_FILE: |
|
287 | - $pps = new PHPExcel_Shared_OLE_PPS_File($name); |
|
288 | - break; |
|
289 | - default: |
|
290 | - continue; |
|
278 | + case self::OLE_PPS_TYPE_ROOT: |
|
279 | + $pps = new PHPExcel_Shared_OLE_PPS_Root(null, null, array()); |
|
280 | + $this->root = $pps; |
|
281 | + break; |
|
282 | + case self::OLE_PPS_TYPE_DIR: |
|
283 | + $pps = new PHPExcel_Shared_OLE_PPS(null, null, null, null, null, |
|
284 | + null, null, null, null, array()); |
|
285 | + break; |
|
286 | + case self::OLE_PPS_TYPE_FILE: |
|
287 | + $pps = new PHPExcel_Shared_OLE_PPS_File($name); |
|
288 | + break; |
|
289 | + default: |
|
290 | + continue; |
|
291 | 291 | } |
292 | 292 | fseek($fh, 1, SEEK_CUR); |
293 | 293 | $pps->Type = $type; |
@@ -37,12 +37,12 @@ discard block |
||
37 | 37 | */ |
38 | 38 | class PHPExcel_Shared_OLE |
39 | 39 | { |
40 | - const OLE_PPS_TYPE_ROOT = 5; |
|
41 | - const OLE_PPS_TYPE_DIR = 1; |
|
42 | - const OLE_PPS_TYPE_FILE = 2; |
|
40 | + const OLE_PPS_TYPE_ROOT = 5; |
|
41 | + const OLE_PPS_TYPE_DIR = 1; |
|
42 | + const OLE_PPS_TYPE_FILE = 2; |
|
43 | 43 | const OLE_DATA_SIZE_SMALL = 0x1000; |
44 | - const OLE_LONG_INT_SIZE = 4; |
|
45 | - const OLE_PPS_SIZE = 0x80; |
|
44 | + const OLE_LONG_INT_SIZE = 4; |
|
45 | + const OLE_PPS_SIZE = 0x80; |
|
46 | 46 | |
47 | 47 | /** |
48 | 48 | * The file handle for reading an OLE container |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | public function read($file) |
97 | 97 | { |
98 | 98 | $fh = fopen($file, "r"); |
99 | - if (!$fh) { |
|
99 | + if ( ! $fh) { |
|
100 | 100 | throw new Exception("Can't open file $file"); |
101 | 101 | } |
102 | 102 | $this->_file_handle = $fh; |
@@ -112,7 +112,7 @@ discard block |
||
112 | 112 | } |
113 | 113 | // Size of blocks and short blocks in bytes |
114 | 114 | $this->bigBlockSize = pow(2, self::_readInt2($fh)); |
115 | - $this->smallBlockSize = pow(2, self::_readInt2($fh)); |
|
115 | + $this->smallBlockSize = pow(2, self::_readInt2($fh)); |
|
116 | 116 | |
117 | 117 | // Skip UID, revision number and version number |
118 | 118 | fseek($fh, 44); |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | for ($i = 0; $i < $bbatBlockCount; ++$i) { |
160 | 160 | $pos = $this->_getBlockOffset($mbatBlocks[$i]); |
161 | 161 | fseek($fh, $pos); |
162 | - for ($j = 0 ; $j < $this->bigBlockSize / 4; ++$j) { |
|
162 | + for ($j = 0; $j < $this->bigBlockSize / 4; ++$j) { |
|
163 | 163 | $this->bbat[] = self::_readInt4($fh); |
164 | 164 | } |
165 | 165 | } |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | public function getStream($blockIdOrPps) |
198 | 198 | { |
199 | 199 | static $isRegistered = false; |
200 | - if (!$isRegistered) { |
|
200 | + if ( ! $isRegistered) { |
|
201 | 201 | stream_wrapper_register('ole-chainedblockstream', |
202 | 202 | 'PHPExcel_Shared_OLE_ChainedBlockStream'); |
203 | 203 | $isRegistered = true; |
@@ -209,12 +209,12 @@ discard block |
||
209 | 209 | $GLOBALS['_OLE_INSTANCES'][] = $this; |
210 | 210 | $instanceId = end(array_keys($GLOBALS['_OLE_INSTANCES'])); |
211 | 211 | |
212 | - $path = 'ole-chainedblockstream://oleInstanceId=' . $instanceId; |
|
212 | + $path = 'ole-chainedblockstream://oleInstanceId='.$instanceId; |
|
213 | 213 | if ($blockIdOrPps instanceof PHPExcel_Shared_OLE_PPS) { |
214 | - $path .= '&blockId=' . $blockIdOrPps->_StartBlock; |
|
215 | - $path .= '&size=' . $blockIdOrPps->Size; |
|
214 | + $path .= '&blockId='.$blockIdOrPps->_StartBlock; |
|
215 | + $path .= '&size='.$blockIdOrPps->Size; |
|
216 | 216 | } else { |
217 | - $path .= '&blockId=' . $blockIdOrPps; |
|
217 | + $path .= '&blockId='.$blockIdOrPps; |
|
218 | 218 | } |
219 | 219 | return fopen($path, 'r'); |
220 | 220 | } |
@@ -410,7 +410,7 @@ discard block |
||
410 | 410 | public function getData($index, $position, $length) |
411 | 411 | { |
412 | 412 | // if position is not valid return empty string |
413 | - if (!isset($this->_list[$index]) || ($position >= $this->_list[$index]->Size) || ($position < 0)) { |
|
413 | + if ( ! isset($this->_list[$index]) || ($position >= $this->_list[$index]->Size) || ($position < 0)) { |
|
414 | 414 | return ''; |
415 | 415 | } |
416 | 416 | $fh = $this->getStream($this->_list[$index]); |
@@ -447,7 +447,7 @@ discard block |
||
447 | 447 | { |
448 | 448 | $rawname = ''; |
449 | 449 | for ($i = 0; $i < strlen($ascii); ++$i) { |
450 | - $rawname .= $ascii{$i} . "\x00"; |
|
450 | + $rawname .= $ascii{$i}."\x00"; |
|
451 | 451 | } |
452 | 452 | return $rawname; |
453 | 453 | } |
@@ -463,7 +463,7 @@ discard block |
||
463 | 463 | */ |
464 | 464 | public static function LocalDate2OLE($date = null) |
465 | 465 | { |
466 | - if (!isset($date)) { |
|
466 | + if ( ! isset($date)) { |
|
467 | 467 | return "\x00\x00\x00\x00\x00\x00\x00\x00"; |
468 | 468 | } |
469 | 469 | |
@@ -473,8 +473,8 @@ discard block |
||
473 | 473 | // days from 1-1-1601 until the beggining of UNIX era |
474 | 474 | $days = 134774; |
475 | 475 | // calculate seconds |
476 | - $big_date = $days*24*3600 + gmmktime(date("H",$date),date("i",$date),date("s",$date), |
|
477 | - date("m",$date),date("d",$date),date("Y",$date)); |
|
476 | + $big_date = $days * 24 * 3600 + gmmktime(date("H", $date), date("i", $date), date("s", $date), |
|
477 | + date("m", $date), date("d", $date), date("Y", $date)); |
|
478 | 478 | // multiply just to make MS happy |
479 | 479 | $big_date *= 10000000; |
480 | 480 | |
@@ -513,7 +513,7 @@ discard block |
||
513 | 513 | } |
514 | 514 | |
515 | 515 | // factor used for separating numbers into 4 bytes parts |
516 | - $factor = pow(2,32); |
|
516 | + $factor = pow(2, 32); |
|
517 | 517 | list(, $high_part) = unpack('V', substr($string, 4, 4)); |
518 | 518 | list(, $low_part) = unpack('V', substr($string, 0, 4)); |
519 | 519 |
@@ -64,9 +64,9 @@ discard block |
||
64 | 64 | * If a resource pointer to a stream created by fopen() is passed |
65 | 65 | * it will be used, but you have to close such stream by yourself. |
66 | 66 | * |
67 | - * @param string|resource $filename The name of the file or stream where to save the OLE container. |
|
67 | + * @param string|null $filename The name of the file or stream where to save the OLE container. |
|
68 | 68 | * @access public |
69 | - * @return mixed true on success |
|
69 | + * @return boolean true on success |
|
70 | 70 | */ |
71 | 71 | public function save($filename) |
72 | 72 | { |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | * |
123 | 123 | * @access public |
124 | 124 | * @param array $raList Reference to an array of PPS's |
125 | - * @return array The array of numbers |
|
125 | + * @return double[] The array of numbers |
|
126 | 126 | */ |
127 | 127 | public function _calcSize(&$raList) |
128 | 128 | { |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | * @access public |
162 | 162 | * @param integer $i2 The argument |
163 | 163 | * @see save() |
164 | - * @return integer |
|
164 | + * @return double |
|
165 | 165 | */ |
166 | 166 | private static function _adjust2($i2) |
167 | 167 | { |
@@ -58,16 +58,16 @@ discard block |
||
58 | 58 | } |
59 | 59 | |
60 | 60 | /** |
61 | - * Method for saving the whole OLE container (including files). |
|
62 | - * In fact, if called with an empty argument (or '-'), it saves to a |
|
63 | - * temporary file and then outputs it's contents to stdout. |
|
64 | - * If a resource pointer to a stream created by fopen() is passed |
|
65 | - * it will be used, but you have to close such stream by yourself. |
|
66 | - * |
|
67 | - * @param string|resource $filename The name of the file or stream where to save the OLE container. |
|
68 | - * @access public |
|
69 | - * @return mixed true on success |
|
70 | - */ |
|
61 | + * Method for saving the whole OLE container (including files). |
|
62 | + * In fact, if called with an empty argument (or '-'), it saves to a |
|
63 | + * temporary file and then outputs it's contents to stdout. |
|
64 | + * If a resource pointer to a stream created by fopen() is passed |
|
65 | + * it will be used, but you have to close such stream by yourself. |
|
66 | + * |
|
67 | + * @param string|resource $filename The name of the file or stream where to save the OLE container. |
|
68 | + * @access public |
|
69 | + * @return mixed true on success |
|
70 | + */ |
|
71 | 71 | public function save($filename) |
72 | 72 | { |
73 | 73 | // Initial Setting for saving |
@@ -77,7 +77,7 @@ discard block |
||
77 | 77 | ((isset($this->_SMALL_BLOCK_SIZE))? self::_adjust2($this->_SMALL_BLOCK_SIZE): 6)); |
78 | 78 | |
79 | 79 | if (is_resource($filename)) { |
80 | - $this->_FILEH_ = $filename; |
|
80 | + $this->_FILEH_ = $filename; |
|
81 | 81 | } else if ($filename == '-' || $filename == '') { |
82 | 82 | if ($this->_tmp_dir === NULL) |
83 | 83 | $this->_tmp_dir = PHPExcel_Shared_File::sys_get_temp_dir(); |
@@ -118,12 +118,12 @@ discard block |
||
118 | 118 | } |
119 | 119 | |
120 | 120 | /** |
121 | - * Calculate some numbers |
|
122 | - * |
|
123 | - * @access public |
|
124 | - * @param array $raList Reference to an array of PPS's |
|
125 | - * @return array The array of numbers |
|
126 | - */ |
|
121 | + * Calculate some numbers |
|
122 | + * |
|
123 | + * @access public |
|
124 | + * @param array $raList Reference to an array of PPS's |
|
125 | + * @return array The array of numbers |
|
126 | + */ |
|
127 | 127 | public function _calcSize(&$raList) |
128 | 128 | { |
129 | 129 | // Calculate Basic Setting |
@@ -156,13 +156,13 @@ discard block |
||
156 | 156 | } |
157 | 157 | |
158 | 158 | /** |
159 | - * Helper function for caculating a magic value for block sizes |
|
160 | - * |
|
161 | - * @access public |
|
162 | - * @param integer $i2 The argument |
|
163 | - * @see save() |
|
164 | - * @return integer |
|
165 | - */ |
|
159 | + * Helper function for caculating a magic value for block sizes |
|
160 | + * |
|
161 | + * @access public |
|
162 | + * @param integer $i2 The argument |
|
163 | + * @see save() |
|
164 | + * @return integer |
|
165 | + */ |
|
166 | 166 | private static function _adjust2($i2) |
167 | 167 | { |
168 | 168 | $iWk = log($i2)/log(2); |
@@ -170,13 +170,13 @@ discard block |
||
170 | 170 | } |
171 | 171 | |
172 | 172 | /** |
173 | - * Save OLE header |
|
174 | - * |
|
175 | - * @access public |
|
176 | - * @param integer $iSBDcnt |
|
177 | - * @param integer $iBBcnt |
|
178 | - * @param integer $iPPScnt |
|
179 | - */ |
|
173 | + * Save OLE header |
|
174 | + * |
|
175 | + * @access public |
|
176 | + * @param integer $iSBDcnt |
|
177 | + * @param integer $iBBcnt |
|
178 | + * @param integer $iPPScnt |
|
179 | + */ |
|
180 | 180 | public function _saveHeader($iSBDcnt, $iBBcnt, $iPPScnt) |
181 | 181 | { |
182 | 182 | $FILE = $this->_FILEH_; |
@@ -249,12 +249,12 @@ discard block |
||
249 | 249 | } |
250 | 250 | |
251 | 251 | /** |
252 | - * Saving big data (PPS's with data bigger than PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) |
|
253 | - * |
|
254 | - * @access public |
|
255 | - * @param integer $iStBlk |
|
256 | - * @param array &$raList Reference to array of PPS's |
|
257 | - */ |
|
252 | + * Saving big data (PPS's with data bigger than PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) |
|
253 | + * |
|
254 | + * @access public |
|
255 | + * @param integer $iStBlk |
|
256 | + * @param array &$raList Reference to array of PPS's |
|
257 | + */ |
|
258 | 258 | public function _saveBigData($iStBlk, &$raList) |
259 | 259 | { |
260 | 260 | $FILE = $this->_FILEH_; |
@@ -299,11 +299,11 @@ discard block |
||
299 | 299 | } |
300 | 300 | |
301 | 301 | /** |
302 | - * get small data (PPS's with data smaller than PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) |
|
303 | - * |
|
304 | - * @access public |
|
305 | - * @param array &$raList Reference to array of PPS's |
|
306 | - */ |
|
302 | + * get small data (PPS's with data smaller than PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) |
|
303 | + * |
|
304 | + * @access public |
|
305 | + * @param array &$raList Reference to array of PPS's |
|
306 | + */ |
|
307 | 307 | public function _makeSmallData(&$raList) |
308 | 308 | { |
309 | 309 | $sRes = ''; |
@@ -356,11 +356,11 @@ discard block |
||
356 | 356 | } |
357 | 357 | |
358 | 358 | /** |
359 | - * Saves all the PPS's WKs |
|
360 | - * |
|
361 | - * @access public |
|
362 | - * @param array $raList Reference to an array with all PPS's |
|
363 | - */ |
|
359 | + * Saves all the PPS's WKs |
|
360 | + * |
|
361 | + * @access public |
|
362 | + * @param array $raList Reference to an array with all PPS's |
|
363 | + */ |
|
364 | 364 | public function _savePps(&$raList) |
365 | 365 | { |
366 | 366 | // Save each PPS WK |
@@ -377,13 +377,13 @@ discard block |
||
377 | 377 | } |
378 | 378 | |
379 | 379 | /** |
380 | - * Saving Big Block Depot |
|
381 | - * |
|
382 | - * @access public |
|
383 | - * @param integer $iSbdSize |
|
384 | - * @param integer $iBsize |
|
385 | - * @param integer $iPpsCnt |
|
386 | - */ |
|
380 | + * Saving Big Block Depot |
|
381 | + * |
|
382 | + * @access public |
|
383 | + * @param integer $iSbdSize |
|
384 | + * @param integer $iBsize |
|
385 | + * @param integer $iPpsCnt |
|
386 | + */ |
|
387 | 387 | public function _saveBbd($iSbdSize, $iBsize, $iPpsCnt) |
388 | 388 | { |
389 | 389 | $FILE = $this->_FILEH_; |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * Directory for temporary files |
35 | 35 | * @var string |
36 | 36 | */ |
37 | - protected $_tmp_dir = NULL; |
|
37 | + protected $_tmp_dir = NULL; |
|
38 | 38 | |
39 | 39 | /** |
40 | 40 | * @param integer $time_1st A timestamp |
@@ -72,9 +72,9 @@ discard block |
||
72 | 72 | { |
73 | 73 | // Initial Setting for saving |
74 | 74 | $this->_BIG_BLOCK_SIZE = pow(2, |
75 | - ((isset($this->_BIG_BLOCK_SIZE))? self::_adjust2($this->_BIG_BLOCK_SIZE) : 9)); |
|
76 | - $this->_SMALL_BLOCK_SIZE= pow(2, |
|
77 | - ((isset($this->_SMALL_BLOCK_SIZE))? self::_adjust2($this->_SMALL_BLOCK_SIZE): 6)); |
|
75 | + ((isset($this->_BIG_BLOCK_SIZE)) ? self::_adjust2($this->_BIG_BLOCK_SIZE) : 9)); |
|
76 | + $this->_SMALL_BLOCK_SIZE = pow(2, |
|
77 | + ((isset($this->_SMALL_BLOCK_SIZE)) ? self::_adjust2($this->_SMALL_BLOCK_SIZE) : 6)); |
|
78 | 78 | |
79 | 79 | if (is_resource($filename)) { |
80 | 80 | $this->_FILEH_ = $filename; |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | if ($this->_tmp_dir === NULL) |
83 | 83 | $this->_tmp_dir = PHPExcel_Shared_File::sys_get_temp_dir(); |
84 | 84 | $this->_tmp_filename = tempnam($this->_tmp_dir, "OLE_PPS_Root"); |
85 | - $this->_FILEH_ = fopen($this->_tmp_filename,"w+b"); |
|
85 | + $this->_FILEH_ = fopen($this->_tmp_filename, "w+b"); |
|
86 | 86 | if ($this->_FILEH_ == false) { |
87 | 87 | throw new Exception("Can't create temporary file."); |
88 | 88 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | // Write Big Block Depot and BDList and Adding Header informations |
111 | 111 | $this->_saveBbd($iSBDcnt, $iBBcnt, $iPPScnt); |
112 | 112 | |
113 | - if (!is_resource($filename)) { |
|
113 | + if ( ! is_resource($filename)) { |
|
114 | 114 | fclose($this->_FILEH_); |
115 | 115 | } |
116 | 116 | |
@@ -127,7 +127,7 @@ discard block |
||
127 | 127 | public function _calcSize(&$raList) |
128 | 128 | { |
129 | 129 | // Calculate Basic Setting |
130 | - list($iSBDcnt, $iBBcnt, $iPPScnt) = array(0,0,0); |
|
130 | + list($iSBDcnt, $iBBcnt, $iPPScnt) = array(0, 0, 0); |
|
131 | 131 | $iSmallLen = 0; |
132 | 132 | $iSBcnt = 0; |
133 | 133 | $iCount = count($raList); |
@@ -136,21 +136,21 @@ discard block |
||
136 | 136 | $raList[$i]->Size = $raList[$i]->_DataLen(); |
137 | 137 | if ($raList[$i]->Size < PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) { |
138 | 138 | $iSBcnt += floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE) |
139 | - + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0); |
|
139 | + + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) ? 1 : 0); |
|
140 | 140 | } else { |
141 | 141 | $iBBcnt += (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) + |
142 | - (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0)); |
|
142 | + (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) ? 1 : 0)); |
|
143 | 143 | } |
144 | 144 | } |
145 | 145 | } |
146 | 146 | $iSmallLen = $iSBcnt * $this->_SMALL_BLOCK_SIZE; |
147 | 147 | $iSlCnt = floor($this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_LONG_INT_SIZE); |
148 | - $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt)? 1:0); |
|
149 | - $iBBcnt += (floor($iSmallLen / $this->_BIG_BLOCK_SIZE) + |
|
150 | - (( $iSmallLen % $this->_BIG_BLOCK_SIZE)? 1: 0)); |
|
148 | + $iSBDcnt = floor($iSBcnt / $iSlCnt) + (($iSBcnt % $iSlCnt) ? 1 : 0); |
|
149 | + $iBBcnt += (floor($iSmallLen / $this->_BIG_BLOCK_SIZE) + |
|
150 | + (($iSmallLen % $this->_BIG_BLOCK_SIZE) ? 1 : 0)); |
|
151 | 151 | $iCnt = count($raList); |
152 | 152 | $iBdCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_PPS_SIZE; |
153 | - $iPPScnt = (floor($iCnt/$iBdCnt) + (($iCnt % $iBdCnt)? 1: 0)); |
|
153 | + $iPPScnt = (floor($iCnt / $iBdCnt) + (($iCnt % $iBdCnt) ? 1 : 0)); |
|
154 | 154 | |
155 | 155 | return array($iSBDcnt, $iBBcnt, $iPPScnt); |
156 | 156 | } |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | */ |
166 | 166 | private static function _adjust2($i2) |
167 | 167 | { |
168 | - $iWk = log($i2)/log(2); |
|
169 | - return ($iWk > floor($iWk))? floor($iWk)+1:$iWk; |
|
168 | + $iWk = log($i2) / log(2); |
|
169 | + return ($iWk > floor($iWk)) ? floor($iWk) + 1 : $iWk; |
|
170 | 170 | } |
171 | 171 | |
172 | 172 | /** |
@@ -188,17 +188,17 @@ discard block |
||
188 | 188 | $iBdExL = 0; |
189 | 189 | $iAll = $iBBcnt + $iPPScnt + $iSBDcnt; |
190 | 190 | $iAllW = $iAll; |
191 | - $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0); |
|
192 | - $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0); |
|
191 | + $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0); |
|
192 | + $iBdCnt = floor(($iAll + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0); |
|
193 | 193 | |
194 | 194 | // Calculate BD count |
195 | 195 | if ($iBdCnt > $i1stBdL) { |
196 | 196 | while (1) { |
197 | 197 | ++$iBdExL; |
198 | 198 | ++$iAllW; |
199 | - $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt)? 1: 0); |
|
200 | - $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW+$iBdCntW) % $iBlCnt)? 1: 0); |
|
201 | - if ($iBdCnt <= ($iBdExL*$iBlCnt+ $i1stBdL)) { |
|
199 | + $iBdCntW = floor($iAllW / $iBlCnt) + (($iAllW % $iBlCnt) ? 1 : 0); |
|
200 | + $iBdCnt = floor(($iAllW + $iBdCntW) / $iBlCnt) + ((($iAllW + $iBdCntW) % $iBlCnt) ? 1 : 0); |
|
201 | + if ($iBdCnt <= ($iBdExL * $iBlCnt + $i1stBdL)) { |
|
202 | 202 | break; |
203 | 203 | } |
204 | 204 | } |
@@ -220,7 +220,7 @@ discard block |
||
220 | 220 | . "\x00\x00\x00\x00" |
221 | 221 | . "\x00\x00\x00\x00" |
222 | 222 | . pack("V", $iBdCnt) |
223 | - . pack("V", $iBBcnt+$iSBDcnt) //ROOT START |
|
223 | + . pack("V", $iBBcnt + $iSBDcnt) //ROOT START |
|
224 | 224 | . pack("V", 0) |
225 | 225 | . pack("V", 0x1000) |
226 | 226 | . pack("V", $iSBDcnt ? 0 : -2) //Small Block Depot |
@@ -233,12 +233,12 @@ discard block |
||
233 | 233 | . pack("V", 0) // Extra BDList Count |
234 | 234 | ); |
235 | 235 | } else { |
236 | - fwrite($FILE, pack("V", $iAll+$iBdCnt) . pack("V", $iBdExL)); |
|
236 | + fwrite($FILE, pack("V", $iAll + $iBdCnt).pack("V", $iBdExL)); |
|
237 | 237 | } |
238 | 238 | |
239 | 239 | // BDList |
240 | 240 | for ($i = 0; $i < $i1stBdL && $i < $iBdCnt; ++$i) { |
241 | - fwrite($FILE, pack("V", $iAll+$i)); |
|
241 | + fwrite($FILE, pack("V", $iAll + $i)); |
|
242 | 242 | } |
243 | 243 | if ($i < $i1stBdL) { |
244 | 244 | $jB = $i1stBdL - $i; |
@@ -286,7 +286,7 @@ discard block |
||
286 | 286 | $raList[$i]->_StartBlock = $iStBlk; |
287 | 287 | $iStBlk += |
288 | 288 | (floor($raList[$i]->Size / $this->_BIG_BLOCK_SIZE) + |
289 | - (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE)? 1: 0)); |
|
289 | + (($raList[$i]->Size % $this->_BIG_BLOCK_SIZE) ? 1 : 0)); |
|
290 | 290 | } |
291 | 291 | // Close file for each PPS, and unlink it |
292 | 292 | //if (isset($raList[$i]->_PPS_FILE)) { |
@@ -319,11 +319,11 @@ discard block |
||
319 | 319 | } |
320 | 320 | if ($raList[$i]->Size < PHPExcel_Shared_OLE::OLE_DATA_SIZE_SMALL) { |
321 | 321 | $iSmbCnt = floor($raList[$i]->Size / $this->_SMALL_BLOCK_SIZE) |
322 | - + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)? 1: 0); |
|
322 | + + (($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) ? 1 : 0); |
|
323 | 323 | // Add to SBD |
324 | 324 | $jB = $iSmbCnt - 1; |
325 | 325 | for ($j = 0; $j < $jB; ++$j) { |
326 | - fwrite($FILE, pack("V", $j+$iSmBlk+1)); |
|
326 | + fwrite($FILE, pack("V", $j + $iSmBlk + 1)); |
|
327 | 327 | } |
328 | 328 | fwrite($FILE, pack("V", -2)); |
329 | 329 | |
@@ -337,7 +337,7 @@ discard block |
||
337 | 337 | $sRes .= $raList[$i]->_data; |
338 | 338 | //} |
339 | 339 | if ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE) { |
340 | - $sRes .= str_repeat("\x00",$this->_SMALL_BLOCK_SIZE - ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)); |
|
340 | + $sRes .= str_repeat("\x00", $this->_SMALL_BLOCK_SIZE - ($raList[$i]->Size % $this->_SMALL_BLOCK_SIZE)); |
|
341 | 341 | } |
342 | 342 | // Set for PPS |
343 | 343 | $raList[$i]->_StartBlock = $iSmBlk; |
@@ -372,7 +372,7 @@ discard block |
||
372 | 372 | $iCnt = count($raList); |
373 | 373 | $iBCnt = $this->_BIG_BLOCK_SIZE / PHPExcel_Shared_OLE::OLE_PPS_SIZE; |
374 | 374 | if ($iCnt % $iBCnt) { |
375 | - fwrite($this->_FILEH_, str_repeat("\x00",($iBCnt - ($iCnt % $iBCnt)) * PHPExcel_Shared_OLE::OLE_PPS_SIZE)); |
|
375 | + fwrite($this->_FILEH_, str_repeat("\x00", ($iBCnt - ($iCnt % $iBCnt)) * PHPExcel_Shared_OLE::OLE_PPS_SIZE)); |
|
376 | 376 | } |
377 | 377 | } |
378 | 378 | |
@@ -394,16 +394,16 @@ discard block |
||
394 | 394 | $iBdExL = 0; |
395 | 395 | $iAll = $iBsize + $iPpsCnt + $iSbdSize; |
396 | 396 | $iAllW = $iAll; |
397 | - $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0); |
|
398 | - $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0); |
|
397 | + $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0); |
|
398 | + $iBdCnt = floor(($iAll + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0); |
|
399 | 399 | // Calculate BD count |
400 | - if ($iBdCnt >$i1stBdL) { |
|
400 | + if ($iBdCnt > $i1stBdL) { |
|
401 | 401 | while (1) { |
402 | 402 | ++$iBdExL; |
403 | 403 | ++$iAllW; |
404 | - $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt)? 1: 0); |
|
405 | - $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW+$iBdCntW) % $iBbCnt)? 1: 0); |
|
406 | - if ($iBdCnt <= ($iBdExL*$iBbCnt+ $i1stBdL)) { |
|
404 | + $iBdCntW = floor($iAllW / $iBbCnt) + (($iAllW % $iBbCnt) ? 1 : 0); |
|
405 | + $iBdCnt = floor(($iAllW + $iBdCntW) / $iBbCnt) + ((($iAllW + $iBdCntW) % $iBbCnt) ? 1 : 0); |
|
406 | + if ($iBdCnt <= ($iBdExL * $iBbCnt + $i1stBdL)) { |
|
407 | 407 | break; |
408 | 408 | } |
409 | 409 | } |
@@ -413,19 +413,19 @@ discard block |
||
413 | 413 | // Set for SBD |
414 | 414 | if ($iSbdSize > 0) { |
415 | 415 | for ($i = 0; $i < ($iSbdSize - 1); ++$i) { |
416 | - fwrite($FILE, pack("V", $i+1)); |
|
416 | + fwrite($FILE, pack("V", $i + 1)); |
|
417 | 417 | } |
418 | 418 | fwrite($FILE, pack("V", -2)); |
419 | 419 | } |
420 | 420 | // Set for B |
421 | 421 | for ($i = 0; $i < ($iBsize - 1); ++$i) { |
422 | - fwrite($FILE, pack("V", $i+$iSbdSize+1)); |
|
422 | + fwrite($FILE, pack("V", $i + $iSbdSize + 1)); |
|
423 | 423 | } |
424 | 424 | fwrite($FILE, pack("V", -2)); |
425 | 425 | |
426 | 426 | // Set for PPS |
427 | 427 | for ($i = 0; $i < ($iPpsCnt - 1); ++$i) { |
428 | - fwrite($FILE, pack("V", $i+$iSbdSize+$iBsize+1)); |
|
428 | + fwrite($FILE, pack("V", $i + $iSbdSize + $iBsize + 1)); |
|
429 | 429 | } |
430 | 430 | fwrite($FILE, pack("V", -2)); |
431 | 431 | // Set for BBD itself ( 0xFFFFFFFD : BBD) |
@@ -445,17 +445,17 @@ discard block |
||
445 | 445 | } |
446 | 446 | // Extra BDList |
447 | 447 | if ($iBdCnt > $i1stBdL) { |
448 | - $iN=0; |
|
449 | - $iNb=0; |
|
450 | - for ($i = $i1stBdL;$i < $iBdCnt; $i++, ++$iN) { |
|
448 | + $iN = 0; |
|
449 | + $iNb = 0; |
|
450 | + for ($i = $i1stBdL; $i < $iBdCnt; $i++, ++$iN) { |
|
451 | 451 | if ($iN >= ($iBbCnt - 1)) { |
452 | 452 | $iN = 0; |
453 | 453 | ++$iNb; |
454 | - fwrite($FILE, pack("V", $iAll+$iBdCnt+$iNb)); |
|
454 | + fwrite($FILE, pack("V", $iAll + $iBdCnt + $iNb)); |
|
455 | 455 | } |
456 | - fwrite($FILE, pack("V", $iBsize+$iSbdSize+$iPpsCnt+$i)); |
|
456 | + fwrite($FILE, pack("V", $iBsize + $iSbdSize + $iPpsCnt + $i)); |
|
457 | 457 | } |
458 | - if (($iBdCnt-$i1stBdL) % ($iBbCnt-1)) { |
|
458 | + if (($iBdCnt - $i1stBdL) % ($iBbCnt - 1)) { |
|
459 | 459 | $iB = ($iBbCnt - 1) - (($iBdCnt - $i1stBdL) % ($iBbCnt - 1)); |
460 | 460 | for ($i = 0; $i < $iB; ++$i) { |
461 | 461 | fwrite($FILE, pack("V", -1)); |
@@ -79,8 +79,9 @@ |
||
79 | 79 | if (is_resource($filename)) { |
80 | 80 | $this->_FILEH_ = $filename; |
81 | 81 | } else if ($filename == '-' || $filename == '') { |
82 | - if ($this->_tmp_dir === NULL) |
|
83 | - $this->_tmp_dir = PHPExcel_Shared_File::sys_get_temp_dir(); |
|
82 | + if ($this->_tmp_dir === NULL) { |
|
83 | + $this->_tmp_dir = PHPExcel_Shared_File::sys_get_temp_dir(); |
|
84 | + } |
|
84 | 85 | $this->_tmp_filename = tempnam($this->_tmp_dir, "OLE_PPS_Root"); |
85 | 86 | $this->_FILEH_ = fopen($this->_tmp_filename,"w+b"); |
86 | 87 | if ($this->_FILEH_ == false) { |