Author: Kore Nordmann
Date: 2007-01-24 13:22:57 +0100 (Wed, 24 Jan 2007)
New Revision: 4556

Log:
- Fixed issue #9950: Improved ezcGraphPolynom::__toString method for more
  exact output
  # Based on a patch by Matthew Carroll

Modified:
   trunk/Graph/ChangeLog
   trunk/Graph/src/math/polynom.php
   trunk/Graph/tests/dataset_average_test.php
   trunk/Graph/tests/matrix_test.php
   trunk/Graph/tests/polynom_test.php

Modified: trunk/Graph/ChangeLog
===================================================================
--- trunk/Graph/ChangeLog       2007-01-24 12:09:48 UTC (rev 4555)
+++ trunk/Graph/ChangeLog       2007-01-24 12:22:57 UTC (rev 4556)
@@ -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
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Modified: trunk/Graph/src/math/polynom.php
===================================================================
--- trunk/Graph/src/math/polynom.php    2007-01-24 12:09:48 UTC (rev 4555)
+++ trunk/Graph/src/math/polynom.php    2007-01-24 12:22:57 UTC (rev 4556)
@@ -16,6 +16,8 @@
 {
     protected $values;
 
+    // @TODO: Introduce precision option for string output?
+
     /**
      * Constructor
      *
@@ -156,17 +158,57 @@
             {
                 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;
+            }
         }
 
-        return substr( $string, 0, -3 );
+        if ( substr( $string, 0, 3 ) === ' + ' )
+        {
+            $string = substr( $string, 3 );
+        }
+        else
+        {
+            $string = '-' . substr( $string, 3 );
+        }
+
+        return trim( $string );
     }
 }
 ?>

Modified: trunk/Graph/tests/dataset_average_test.php
===================================================================
--- trunk/Graph/tests/dataset_average_test.php  2007-01-24 12:09:48 UTC (rev 
4555)
+++ trunk/Graph/tests/dataset_average_test.php  2007-01-24 12:22:57 UTC (rev 
4556)
@@ -68,7 +68,7 @@
         $polynom = $averageDataSet->getPolynom();
 
         $this->assertEquals(
-            'x^2 + 1.00',
+            'x^2 + 1',
             $polynom->__toString()
         );
     }
@@ -90,7 +90,7 @@
         $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 @@
         $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 @@
         $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 @@
         $polynom = $averageDataSet->getPolynom();
 
         $this->assertEquals(
-            '2.00 * x + 2.67',
+            '2.00 x + 2.67',
             $polynom->__toString()
         );
     }
@@ -158,7 +158,7 @@
         $polynom = $averageDataSet->getPolynom();
 
         $this->assertEquals(
-            'x^2 + 1.00',
+            'x^2 + 1',
             $polynom->__toString()
         );
     }
@@ -171,7 +171,7 @@
         $polynom = $averageDataSet->getPolynom();
 
         $this->assertEquals(
-            'x^2 + 1.00',
+            'x^2 + 1',
             $polynom->__toString()
         );
     }

Modified: trunk/Graph/tests/matrix_test.php
===================================================================
--- trunk/Graph/tests/matrix_test.php   2007-01-24 12:09:48 UTC (rev 4555)
+++ trunk/Graph/tests/matrix_test.php   2007-01-24 12:22:57 UTC (rev 4556)
@@ -315,7 +315,7 @@
         $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()
         );
     }

Modified: trunk/Graph/tests/polynom_test.php
===================================================================
--- trunk/Graph/tests/polynom_test.php  2007-01-24 12:09:48 UTC (rev 4555)
+++ trunk/Graph/tests/polynom_test.php  2007-01-24 12:22:57 UTC (rev 4556)
@@ -49,7 +49,7 @@
         $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 @@
         $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 @@
             .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()
+        );
+    }
 }
 
 ?>

-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to