Robert Kern avatar Robert Kern committed 443a78c

Ad some unit tests from Konrad. Fix some typechecks. Fix factorial of 0 to return an integer 1 instead of 1.0.

Comments (0)

Files changed (2)

    
 Acknowledgements
 ----------------
-Konrad Hinsen fixed a few bugs in the conversion to numpy.
+Konrad Hinsen fixed a few bugs in the conversion to numpy and adding some unit
+tests.
 
 
 Changes 0.6-0.7
 """
 
 # Standard library imports.
-import types
 import math
 
 # Major library imports.
         _checkOther(other, coerce=1) --> newOther, isMultiVector
         """
 
-        if type(other) in (types.IntType, types.FloatType, types.LongType):
+        if isinstance(other, (int, float, long)):
             if coerce:
                 # numeric scalar
                 newOther = self._newMV()
         __pow__(other) --> MultiVector
         """
         
-        if type(other) not in (types.IntType, types.FloatType):
-            raise ValueError, "exponent must be a Python Int or Float"
+        if not isinstance(other, (int, float)):
+            raise ValueError, "exponent must be a Python int or float"
         
         if abs(round(other) - other) > _eps:
             raise ValueError, "exponent must have no fractional part"
         if grade not in self.layout.gradeList:
             raise ValueError, "algebra does not have grade %s" % grade
         
-        if isinstance(grade, not types.IntType):
+        if not isinstance(grade, int):
             raise ValueError, "grade must be an integer"
 
         mask = np.equal(grade, self.layout.gradeList)
     """
 
     def fact(n):
+        if n == 0:
+            return 1
         return np.multiply.reduce(range(1, n+1))
 
     return fact(n) / (fact(k) * fact(n-k))
+from clifford import MultiVector, Cl, randomMV
+from numpy.random import uniform
+import unittest
+
+# Put the names of the blades into the module namespace.
+# This is a bit of a hack, but it's quite convenient.
+#import __main__
+#for name, value in blades.items():
+#    setattr(__main__, name, value)
+
+class CliffordTests(unittest.TestCase):
+
+    def setUp(self):
+        self.algebras = [Cl(i) for i in [3, 4, 5, 6]]
+
+    def test_inverse(self):
+        for layout, blades in self.algebras:
+            a = 1. + blades['e0']
+            self.assertRaises(ValueError, lambda x: 1/x, a)
+            for i in range(10):
+                a = randomMV(layout, grades=[0, 1])
+                denominator = float(a(1)**2-a(0)**2)
+                if abs(denominator) > 1.e-5:
+                    a_inv = (-a(0)/denominator) + ((1./denominator) & a(1))
+                    self.assert_(abs((a & a_inv)-1.) < 1.e-11)
+                    self.assert_(abs((a_inv & a)-1.) < 1.e-11)
+                    self.assert_(abs(a_inv - 1./a) < 1.e-11)
+
+if __name__ == '__main__':
+    unittest.main()
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.