Ejemplo n.º 1
0
def main():
	Maths.generateMathExpressions()
	print("Available operators:")
	for o in Maths.Expressions:
		print(Maths.Expressions[o].getAbbreviation())

	print("-------------------------")
	f = Function("cos(3*x)+6/4*(x+3)", False)
	print("RPN String", f.getRpnString())
	for x in range (2, 11):
		f.compute(x)

	print("-------------------------")

	f = Function("56*((6+2)/(8-x)*2^3", False)
	print("RPN String", f.getRpnString()) #should give 56 6 2 + 8 7 - / 2 3 ^ * *


	mainwindow = MainWindow("Function Drawer", 992, 512)
	fx = f.computeRange(0, 10)
	max_y = max(fx.values())
	min_y = min(fx.values())

	print(min_y, max_y)
	mainwindow.setCoords(-1, min_y, 11, max_y)
	for x in range(0, 11):
		print(fx[x])
		p = Point(x, fx[x])
		p.draw(mainwindow)

	input("Press a key to quit")
Ejemplo n.º 2
0
 def __init__(self):
     
     self.object_list = []
     
     self.maths = Maths()
     self.collision_manager = CollisionManager(self.maths)
     
     self.step = 0
     self.calc_time = 0
Ejemplo n.º 3
0
Archivo: key.py Proyecto: reality/PyRSA
    def generate_key(bitlength):
        e = 0
        while e == 0:
            p, q = Maths.generate_prime((int)(bitlength / 2)), \
                Maths.generate_prime((int)(bitlength / 2))
            n = p * q
            phi = ((p - 1) * (q - 1))

            # If it's not one of these might as well just restart
            if Maths.is_coprime(3, phi):
                e = 3
            elif Maths.is_coprime(17, phi):
                e = 17
            elif Maths.is_coprime(65537, phi):
                e = 65537

        gcd = Maths.extended_gcd(e, phi)
        d = (gcd[0] + phi) % phi

        private = [n, d]
        public = [n, e]

        return Key(bitlength, private, public)
Ejemplo n.º 4
0
class Universe(object):
    '''
    Universe class has objects in it and it requires maths-object
    '''
    
    def __init__(self):
        
        self.object_list = []
        
        self.maths = Maths()
        self.collision_manager = CollisionManager(self.maths)
        
        self.step = 0
        self.calc_time = 0
    
    def add_object(self, uni_object):
        '''
        Appends Object to the object list
        '''
        
        self.object_list.append(uni_object)

    def get_object_list(self):
        '''
        Returns Object list
        '''
        
        return self.object_list

    def calculate_gravity(self):
        '''
        Calculates new values to Object's force vectors
        By the law of gravity
        
        It goes through each object pair.
        '''
        
        amount = len(self.object_list)
        
        for i in range(amount):
            
            j = i + 1
            
            for k in range(j, amount):

                #print "\n" + self.object_list[i].name +" + "+self.object_list[k].name
                collision = self.maths.gravity(self.object_list[i], self.object_list[k])
                '''
                if del_object is not None:
                    self.object_list.remove(del_object)
                    self.calculate_gravity()
                    return
                '''
                if collision:
                    del_object = self.collision_manager.record(self.object_list[i], self.object_list[k], self.calc_time)
                    self.object_list.remove(del_object)
                    #self.calculate_gravity()
                    return
                
    def move_objects(self):
        '''
        Moves each object based on it's force vector(s)
        '''
        for uni_object in self.object_list:
            uni_object.clear_force()
        
        self.step += 1
        self.calc_time += self.maths.time
        self.calculate_gravity()
        
        for uni_object in self.object_list:
            self.maths.move(uni_object, self.calc_time)
Ejemplo n.º 5
0
 def setUp(self):
     self.calc = Maths()
Ejemplo n.º 6
0
class MathTest(unittest.TestCase):
    def setUp(self):
        self.calc = Maths()

    def test_is_even(self):
        self.assertEqual(self.calc.is_even(4), True,
                         "tests if a positive number is even")
        self.assertEqual(self.calc.is_even(-6), True,
                         "tests if a negative number is even")
        self.assertEqual(self.calc.is_even(0), True, "tests if 0 is even")
        self.assertEqual(self.calc.is_even(5), False,
                         "tests if an odd number is even")
        self.assertEqual(self.calc.is_even(-9), False,
                         "tests if a negative odd number is even")
        self.assertEqual(self.calc.is_even(4.2), True, "tests even decimal")
        self.assertEqual(self.calc.is_even(5.1), False, "tests odd decimal")

    def test_factorial(self):
        self.assertEqual(self.calc.factorial(8), 40320,
                         "tests positive number")
        self.assertEqual(self.calc.factorial(-4), -24, "tests negative number")
        self.assertEqual(self.calc.factorial(0), 1, "tests 0")

    def test_power(self):
        self.assertEqual(self.calc.power(2, 5), 32,
                         "tests two positive numbers")
        self.assertEqual(self.calc.power(-2, 8), 256, "tests a negative base")
        self.assertEqual(self.calc.power(2, -5), 32, "tests a negative power")
        self.assertEqual(self.calc.power(3, 0), 1, "tests to the zero power")
        self.assertEqual(self.calc.power(0, 5), 0, "tests zero as a base")