Exemplo n.º 1
0
 def _shoot(self):
     self.ticks_to_fire -= 1
     if self.ticks_to_fire <= 0:
         direction = vector.from_angle(self.angle + 180)
         velocity = vector.scalar_multiply(direction, 3)
         location = vector.add(
             self.maprect.center,
             vector.scalar_multiply(direction, self.maprect.width / 2))
         self.level.add(TickShot(velocity=list(velocity)), location)
         self.ticks_to_fire = self.fps
Exemplo n.º 2
0
 def update(self):
     super(TickFactory, self).update()
     self.ticks_to_spawn -= 1
     if self.ticks_to_spawn <= 0:
         direction = vector.from_angle(self.angle + 180)
         velocity = vector.scalar_multiply(direction, 3)
         location = vector.add(
             self.maprect.center,
             vector.scalar_multiply(direction, self.maprect.width / 2))
         self.level.add(TickShip(velocity=list(velocity)), location)
         self.ticks_to_spawn = self.fps * 2.1
Exemplo n.º 3
0
def minimize_stochastic(target_fn, gradient_fn, x, y, theta_0, alpha_0=0.01):
    """
    Do Stochastic Gradient Descent via minimization.
    """
    data = zip(x, y)
    theta = theta_0
    alpha = alpha_0
    min_theta, min_value = None, float("inf")
    iterations_with_no_improvement = 0

    while iterations_with_no_imporovement < 100:
        value = sum(target_fn(x_i, y_i, theta) for x_i, y_i in data)

        if value < min_value:
            min_theta, min_value = theta, value
            iterations_with_no_improvement = 0
            alpha = alpha_0
        else:
            iterations_with_no_improvement += 1
            alpha *= 0.9

        for x_i, y_i in in_random_order(data):
            gradient_i = gradient_fn(x_i, y_i, theta)
            theta = v.vector_subtract(
                theta,
                v.scalar_multiply(alpha, gradient_i)
            )

    return min_theta
Exemplo n.º 4
0
 def draw_angle(self):
     """Draw a line representing the angle """
     direction = vector.from_angle(self.angle)
     vect = vector.scalar_multiply(direction, 20)
     endpos = vector.add(self.rect.center, vect)
     return pygame.draw.line(self.level.screen, (255, 255, 0),
                             self.rect.center, endpos)
Exemplo n.º 5
0
 def move(self, offset):
     self.moves += 1
     toff = vector.scalar_multiply(self.velocity, self.moves)
     self.rect = self.original_rect.move(
         vector.add(toff, self.scroll_offset))
     self.maprect = self.original_maprect.move(toff)
     self.ttl -= 1
     if self.ttl <= 0:
         self.kill()
Exemplo n.º 6
0
def find_eigenvector(A, tolerance=0.00001):
    guess = [1 for __ in A]

    while True:
        result = matrix_operate(A, guess)
        length = magnitude(result)
        next_guess = scalar_multiply(1 / length, result)

        if distance(guess, next_guess) < tolerance:
            return next_guess, length  # eigenvector, eigenvalue

        guess = next_guess
Exemplo n.º 7
0
 def _fire(self):
     """Fire if the mouse button is held down."""
     buttons = pygame.mouse.get_pressed()
     if buttons[0] and self.ammo and self.fire_wait_tick <= 0:
         pos = pygame.mouse.get_pos()
         velocity = vector.subtract(pos, self.rect.center)
         velocity = vector.normalize(velocity)
         velocity = vector.scalar_multiply(velocity, 10)
         velocity = vector.add(velocity, vector.intvector(self.velocity))
         self.level.add(PlayerShot(velocity=list(velocity)),
                        self.maprect.center)
         self.fire_wait_tick = 10
         self.ammo -= 1
     else:
         self.fire_wait_tick -= 1
Exemplo n.º 8
0
 def draw_velocity(self):
     """Draw a line representing the velocity."""
     vect = vector.scalar_multiply(self.velocity, 10)
     endpos = vector.add(self.rect.center, vect)
     return pygame.draw.line(self.level.screen, (0, 255, 255),
                             self.rect.center, endpos)
Exemplo n.º 9
0
		[1, 2]]

складывает векторы 
result_vec_add = vector.vector_add(height_weight_age, grades)
print(result_vec_add)

# вычетает векторы 
result_vec_sub = vector.vector_subtract(height_weight_age, grades)
print(result_vec_sub)

# покомпонентная сума списка векторов
result_vec_sum = vector.vector_sum(A)
print(result_vec_sum)

# умножение вектора на скаляр
result_vec_dist = vector.scalar_multiply(scalar, grades)
print(result_vec_dist)

# покомпонентное среднее значение списка векторов
result_vec_mean = vector.vector_mean(A)
print(result_vec_mean)

# скалярное произведение
result_dot = vector.dot(height_weight_age, grades)
print(result_dot)

# сумма квадратов вектора
result_sum_of_squares = vector.sum_of_squares(grades)
print(result_sum_of_squares)

# длина вектора
Exemplo n.º 10
0
# assertions.py

from vector import (add, subtract, vector_sum, scalar_multiply, vector_mean,
                    dot, sum_of_squares, magnitude, distance)

from matrices import shape, identity_matrix, get_column, get_row, make_matrix

# Vector
assert add([1, 2, 3], [4, 5, 6]) == [5, 7, 9]
assert subtract([5, 7, 9], [4, 5, 6]) == [1, 2, 3]
assert vector_sum([[1, 2], [3, 4], [5, 6], [7, 8]]) == [16, 20]
assert scalar_multiply(2, [1, 2, 3]) == [2, 4, 6]
assert vector_mean([[1, 2], [3, 4], [5, 6]]) == [3, 4]
assert dot([1, 2, 3], [4, 5, 6]) == 32  # 1 * 4 + 2 * 5 + 3 * 6
assert sum_of_squares([1, 2, 3]) == 14  # 1 * 1 + 2 * 2 + 3 * 3
assert magnitude([3, 4]) == 5

# Matrices
assert shape([[1, 2, 3], [4, 5, 6]]) == (2, 3)  # 2 rows, 3 columns
assert identity_matrix(5) == [[1, 0, 0, 0, 0], [0, 1, 0, 0,
                                                0], [0, 0, 1, 0, 0],
                              [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]]