From 80e1738a8474b9761036b73a4cc0180e6cee080c Mon Sep 17 00:00:00 2001 From: Kore Nordmann Date: Wed, 24 Jan 2007 12:22:57 +0000 Subject: - Fixed issue #9950: Improved ezcGraphPolynom::__toString method for more exact output # Based on a patch by Matthew Carroll --- ChangeLog | 2 ++ src/math/polynom.php | 54 +++++++++++++++++++++++++++++++++++++----- tests/dataset_average_test.php | 14 +++++------ tests/matrix_test.php | 2 +- tests/polynom_test.php | 54 ++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 110 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index e301f10..4d93426 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,8 @@ - Fixed issue #9948: Make ezcGraphPolynom documentation public - Fixed issue #10074: Use iconv instead of mbstring - Fixed issue #10056: Fixed drawing order for boxes with background and border +- Fixed issue #9950: Improved ezcGraphPolynom::__toString method for more + exact output 1.0 - Monday 18 December 2006 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/src/math/polynom.php b/src/math/polynom.php index bf9ca49..a824910 100644 --- a/src/math/polynom.php +++ b/src/math/polynom.php @@ -16,6 +16,8 @@ class ezcGraphPolynom { protected $values; + // @TODO: Introduce precision option for string output? + /** * Constructor * @@ -156,17 +158,57 @@ class ezcGraphPolynom { continue; } - elseif ( $factor != 1 ) + + $string .= ( $factor < 0 ? ' - ' : ' + ' ); + + $factor = abs( $factor ); + switch ( true ) { - $string .= sprintf( '%.2f * ', $factor ); + case abs( 1 - $factor ) < .0001: + // No not append, if factor is ~1 + break; + case $factor < 1: + case $factor >= 1000: + $string .= sprintf( '%.3e ', $factor ); + break; + case $factor >= 100: + $string .= sprintf( '%.0f ', $factor ); + break; + case $factor >= 10: + $string .= sprintf( '%.1f ', $factor ); + break; + default: + $string .= sprintf( '%.2f ', $factor ); + break; } - $string .= ( $exponent > 1 ? sprintf( 'x^%d + ', $exponent ) : - ( $exponent === 1 ? 'x + ' : '' ) - ); + switch ( true ) + { + case $exponent > 1: + $string .= sprintf( 'x^%d', $exponent ); + break; + case $exponent === 1: + $string .= 'x'; + break; + case $exponent === 0: + if ( abs( 1 - $factor ) < .0001 ) + { + $string .= '1'; + } + break; + } + } + + if ( substr( $string, 0, 3 ) === ' + ' ) + { + $string = substr( $string, 3 ); + } + else + { + $string = '-' . substr( $string, 3 ); } - return substr( $string, 0, -3 ); + return trim( $string ); } } ?> diff --git a/tests/dataset_average_test.php b/tests/dataset_average_test.php index 5cea07f..6de8821 100644 --- a/tests/dataset_average_test.php +++ b/tests/dataset_average_test.php @@ -68,7 +68,7 @@ class ezcGraphDataSetAverageTest extends ezcGraphTestCase $polynom = $averageDataSet->getPolynom(); $this->assertEquals( - 'x^2 + 1.00', + 'x^2 + 1', $polynom->__toString() ); } @@ -90,7 +90,7 @@ class ezcGraphDataSetAverageTest extends ezcGraphTestCase $polynom = $averageDataSet->getPolynom(); $this->assertEquals( - '0.00 * x^2 + -1.85 * x + 1044430783.35', + '8.21e-10 x^2 - 1.85 x + 1.04e+9', $polynom->__toString() ); } @@ -109,7 +109,7 @@ class ezcGraphDataSetAverageTest extends ezcGraphTestCase $polynom = $averageDataSet->getPolynom(); $this->assertEquals( - '1.00 * x^3 + -6.21 * x^2 + 13.04 * x + -11.69', + 'x^3 - 6.21 x^2 + 13.0 x - 11.7', $polynom->__toString() ); } @@ -128,7 +128,7 @@ class ezcGraphDataSetAverageTest extends ezcGraphTestCase $polynom = $averageDataSet->getPolynom(); $this->assertEquals( - 'x^3 + -0.21 * x^2 + 0.20 * x + -2.45', + 'x^3 - 2.10e-1 x^2 + 2.00e-1 x - 2.45', $polynom->__toString() ); } @@ -143,7 +143,7 @@ class ezcGraphDataSetAverageTest extends ezcGraphTestCase $polynom = $averageDataSet->getPolynom(); $this->assertEquals( - '2.00 * x + 2.67', + '2.00 x + 2.67', $polynom->__toString() ); } @@ -158,7 +158,7 @@ class ezcGraphDataSetAverageTest extends ezcGraphTestCase $polynom = $averageDataSet->getPolynom(); $this->assertEquals( - 'x^2 + 1.00', + 'x^2 + 1', $polynom->__toString() ); } @@ -171,7 +171,7 @@ class ezcGraphDataSetAverageTest extends ezcGraphTestCase $polynom = $averageDataSet->getPolynom(); $this->assertEquals( - 'x^2 + 1.00', + 'x^2 + 1', $polynom->__toString() ); } diff --git a/tests/matrix_test.php b/tests/matrix_test.php index 7c94209..ec54772 100644 --- a/tests/matrix_test.php +++ b/tests/matrix_test.php @@ -315,7 +315,7 @@ class ezcGraphMatrixTest extends ezcTestCase $polynom = $a->solveNonlinearEquatation( $b ); $this->assertEquals( - '-0.12 * x^2 + 0.02 * x + 0.35', + '-1.15e-1 x^2 + 1.92e-2 x + 3.46e-1', $polynom->__toString() ); } diff --git a/tests/polynom_test.php b/tests/polynom_test.php index 0af6f92..a492a31 100644 --- a/tests/polynom_test.php +++ b/tests/polynom_test.php @@ -49,7 +49,7 @@ class ezcGraphPolynomTest extends ezcTestCase $polynom = new ezcGraphPolynom( array( 2 => .5, 1 => 3, 0 => -4.5 ) ); $this->assertEquals( - '0.50 * x^2 + 3.00 * x + -4.50', + '5.00e-1 x^2 + 3.00 x - 4.50', $polynom->__toString() ); } @@ -70,7 +70,7 @@ class ezcGraphPolynomTest extends ezcTestCase $polynom->add( new ezcGraphPolynom( array( 2 => 1 ) ) ); $this->assertEquals( - '1.50 * x^2 + 3.00 * x + -4.50', + '1.50 x^2 + 3.00 x - 4.50', $polynom->__toString() ); } @@ -110,6 +110,56 @@ class ezcGraphPolynomTest extends ezcTestCase .1 ); } + + public function testPolynomToString1() + { + $polynom = new ezcGraphPolynom( array( + -109384, + -19322, + -9032, + -984.2, + -32.65, + -5.613, + -1, + -.9345, + -.0, + -.03245, + -.002346, + -.0001326, + -.00008327, + -.000008437, + ) ); + + $this->assertEquals( + '-8.44e-6 x^13 - 8.33e-5 x^12 - 1.33e-4 x^11 - 2.35e-3 x^10 - 3.25e-2 x^9 - 9.35e-1 x^7 - x^6 - 5.61 x^5 - 32.6 x^4 - 984 x^3 - 9.03e+3 x^2 - 1.93e+4 x - 1.09e+5', + $polynom->__toString() + ); + } + + public function testPolynomToString2() + { + $polynom = new ezcGraphPolynom( array( + 109384, + 19322, + 9032, + 984.2, + 32.65, + 5.613, + 1, + .9345, + .0, + .03245, + .002346, + .0001326, + .00008327, + .000008437, + ) ); + + $this->assertEquals( + '8.44e-6 x^13 + 8.33e-5 x^12 + 1.33e-4 x^11 + 2.35e-3 x^10 + 3.25e-2 x^9 + 9.35e-1 x^7 + x^6 + 5.61 x^5 + 32.6 x^4 + 984 x^3 + 9.03e+3 x^2 + 1.93e+4 x + 1.09e+5', + $polynom->__toString() + ); + } } ?> -- cgit v1.1