Changeset 2314


Ignore:
Timestamp:
Feb 1, 2006, 3:27:53 PM (19 years ago)
Author:
sexton
Message:

Investigating 0/0 and 1/0 in apply_expression_to_dictionary

Location:
inundation/pyvolution
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/test_util.py

    r1932 r2314  
    33
    44import unittest
    5 from Numeric import zeros, array, allclose
     5from Numeric import zeros, array, allclose, Float
    66from math import sqrt, pi
    77
     
    974974    def test_apply_expression_to_dictionary(self):
    975975
     976        #FIXME: Division is not expected to work for integers.
     977        #This must be caught.
    976978        foo = array([[1,2,3],
    977                      [4,5,6]])
     979                     [4,5,6]], Float)
    978980
    979981        bar = array([[-1,0,5],
    980                      [6,1,1]])                 
     982                     [6,1,1]], Float)                 
    981983
    982984        D = {'X': foo, 'Y': bar}
     
    990992        Z = apply_expression_to_dictionary('4*X+Y', D)       
    991993        assert allclose(Z, 4*foo+bar)       
     994
     995        # test zero division is OK
     996        Z = apply_expression_to_dictionary('X/Y', D)
     997        assert allclose(1/Z, 1/(foo/bar)) # can't compare inf to inf
     998
     999        # make an error for zero on zero
     1000        # this is really an error in Numeric, SciPy core can handle it
     1001        # Z = apply_expression_to_dictionary('0/Y', D)
    9921002
    9931003        #Check exceptions
     
    10461056if __name__ == "__main__":
    10471057    suite = unittest.makeSuite(Test_Util,'test')
    1048     #suite = unittest.makeSuite(Test_Util,'test_file_function_time')
     1058    #suite = unittest.makeSuite(Test_Util,'test_apply')
    10491059    runner = unittest.TextTestRunner()
    10501060    runner.run(suite)
  • inundation/pyvolution/util.py

    r2074 r2314  
    509509                will be evaluated by expression.
    510510                Values in dictionary must support operators given in
    511                 expression e.g. by overloading               
     511                expression e.g. by overloading
     512
     513    due to a limitation with Numeric, this can not evaluate 0/0
     514    In general, the user can fix by adding 1e-30 to the numerator.
     515    SciPy core can handle this situation.
    512516    """
    513517
     
    532536        msg = 'Expression "%s" could not be evaluated: %s' %(expression, e)
    533537        raise NameError, msg
     538    except ValueError, e:
     539        msg = 'Expression "%s" could not be evaluated: %s' %(expression, e)
     540        raise ValueError, msg
    534541   
    535542
Note: See TracChangeset for help on using the changeset viewer.