| [ Index ] |
PHP Cross Reference of MantisBT |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * File containing the abstract ezcGraphPolynom class 4 * 5 * @package Graph 6 * @version 1.5 7 * @copyright Copyright (C) 2005-2009 eZ Systems AS. All rights reserved. 8 * @license http://ez.no/licenses/new_bsd New BSD License 9 */ 10 11 /** 12 * Provides a class for generic operations on polynoms 13 * 14 * This class is mainly used for internal representation of polynoms in the 15 * average dataset ezcGraphDataSetAveragePolynom. 16 * 17 * It provides only very basic mechanisms to work with polynoms, like adding of 18 * polynomes and evaluating the polynom with a given number, to calculate a 19 * point in the chart for a given value on the x axis. 20 * 21 * Beside this the __toString implementation may be used to echo the polynoms 22 * calculated by the least squares mechanism in the above mentioned average 23 * datasets. The class does not provide any options to customize the output. 24 * 25 * The class can be used like: 26 * 27 * <code> 28 * // Equivalent to: x^2 + .5 29 * $polynom = new ezcGraphPolynom( array( 2 => 1, 0 => .5 ) ); 30 * 31 * // Calculate result for x = 1, echos: 1.5 32 * echo $polynom->evaluate( 1 ), PHP_EOL; 33 * 34 * // Build the sum with another polynom 35 * $polynom->add( new ezcGraphPolynom( array( 1 => 1 ) ) ); 36 * 37 * // Print polynom, echos: 38 * // x^2 + x + 5.00e-1 39 * echo $polynom, PHP_EOL; 40 * </code> 41 * 42 * @version 1.5 43 * @package Graph 44 */ 45 class ezcGraphPolynom 46 { 47 /** 48 * Factors of the polynom 49 * 50 * An example: 51 * Polynom: 52 * 2 * x^3 + .5 * x - 3 53 * Array: 54 * array ( 55 * (int) 3 => (float) 2, 56 * (int) 1 => (float) .5, 57 * (int) 0 => (float) -3, 58 * ) 59 * 60 * @var array 61 */ 62 protected $values; 63 64 // @TODO: Introduce precision option for string output? 65 66 /** 67 * Constructor 68 * 69 * Constructs a polynom object from given array, where the key is the 70 * exponent and the value the factor. 71 * An example: 72 * Polynom: 73 * 2 * x^3 + .5 * x - 3 74 * Array: 75 * array ( 76 * (int) 3 => (float) 2, 77 * (int) 1 => (float) .5, 78 * (int) 0 => (float) -3, 79 * ) 80 * 81 * @param array $values Array with values 82 * @return ezcGraphPolynom 83 */ 84 public function __construct( array $values = array() ) 85 { 86 foreach ( $values as $exponent => $factor ) 87 { 88 $this->values[(int) $exponent] = (float) $factor; 89 } 90 } 91 92 /** 93 * Initialise a polygon 94 * 95 * Initialise a polygon of the given order. Sets all factors to 0. 96 * 97 * @param int $order Order of polygon 98 * @return ezcGraphPolynom Created polynom 99 */ 100 public function init( $order ) 101 { 102 for ( $i = 0; $i <= $order; ++$i ) 103 { 104 $this->values[$i] = 0; 105 } 106 107 return $this; 108 } 109 110 /** 111 * Return factor for one exponent 112 * 113 * @param int $exponent Exponent 114 * @return float Factor 115 */ 116 public function get( $exponent ) 117 { 118 if ( !isset( $this->values[$exponent] ) ) 119 { 120 return 0; 121 } 122 else 123 { 124 return $this->values[$exponent]; 125 } 126 } 127 128 /** 129 * Set the factor for one exponent 130 * 131 * @param int $exponent Exponent 132 * @param float $factor Factor 133 * @return ezcGraphPolynom Modified polynom 134 */ 135 public function set( $exponent, $factor ) 136 { 137 $this->values[(int) $exponent] = (float) $factor; 138 139 return $this; 140 } 141 142 /** 143 * Returns the order of the polynom 144 * 145 * @return int Polynom order 146 */ 147 public function getOrder() 148 { 149 return max( array_keys( $this->values ) ); 150 } 151 152 /** 153 * Adds polynom to current polynom 154 * 155 * @param ezcGraphPolynom $polynom Polynom to add 156 * @return ezcGraphPolynom Modified polynom 157 */ 158 public function add( ezcGraphPolynom $polynom ) 159 { 160 $order = max( 161 $this->getOrder(), 162 $polynom->getOrder() 163 ); 164 165 for ( $i = 0; $i <= $order; ++$i ) 166 { 167 $this->set( $i, $this->get( $i ) + $polynom->get( $i ) ); 168 } 169 170 return $this; 171 } 172 173 /** 174 * Evaluate Polynom with a given value 175 * 176 * @param float $x Value 177 * @return float Result 178 */ 179 public function evaluate( $x ) 180 { 181 $value = 0; 182 foreach ( $this->values as $exponent => $factor ) 183 { 184 $value += $factor * pow( $x, $exponent ); 185 } 186 187 return $value; 188 } 189 190 /** 191 * Returns a string represenation of the polynom 192 * 193 * @return string String representation of polynom 194 */ 195 public function __toString() 196 { 197 krsort( $this->values ); 198 $string = ''; 199 200 foreach ( $this->values as $exponent => $factor ) 201 { 202 if ( $factor == 0 ) 203 { 204 continue; 205 } 206 207 $string .= ( $factor < 0 ? ' - ' : ' + ' ); 208 209 $factor = abs( $factor ); 210 switch ( true ) 211 { 212 case abs( 1 - $factor ) < .0001: 213 // No not append, if factor is ~1 214 break; 215 case $factor < 1: 216 case $factor >= 1000: 217 $string .= sprintf( '%.2e ', $factor ); 218 break; 219 case $factor >= 100: 220 $string .= sprintf( '%.0f ', $factor ); 221 break; 222 case $factor >= 10: 223 $string .= sprintf( '%.1f ', $factor ); 224 break; 225 default: 226 $string .= sprintf( '%.2f ', $factor ); 227 break; 228 } 229 230 switch ( true ) 231 { 232 case $exponent > 1: 233 $string .= sprintf( 'x^%d', $exponent ); 234 break; 235 case $exponent === 1: 236 $string .= 'x'; 237 break; 238 case $exponent === 0: 239 if ( abs( 1 - $factor ) < .0001 ) 240 { 241 $string .= '1'; 242 } 243 break; 244 } 245 } 246 247 if ( substr( $string, 0, 3 ) === ' + ' ) 248 { 249 $string = substr( $string, 3 ); 250 } 251 else 252 { 253 $string = '-' . substr( $string, 3 ); 254 } 255 256 return trim( $string ); 257 } 258 } 259 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Jul 28 15:48:31 2011 | Cross-referenced by PHPXref 0.7 |