Vector

vector is arrows
a-> = (4 3)
b-> = (2 1)
c-> = (1 -3)
b + c = sum of vector

||a|| = √4^2 + 3^2 = 5

Newton’s low of gravity
F = G m1*m2 / d^2
Gravity = 6.67*10^-11 Nm^2/kg^2

Mass of the moon
m2 = 1.62 m/s^2
R = 1.737*10^6m

A2 = 4A, A1
xM, dES, dMS

import numpy

earth_mass = 5.97e24
moon_mass = 7.35e22
gravitational_constant = 6.67e-11

def acceleration(moon_position, spaceship_position):
	vector_to_moon = moon_position - spaceship_position
	vector_to_earth = - spaceship_position
	nonsense = numpy.linalg.norm(moon_position) * spaceship_position
	return nonsense

Gravity Acceleration

F = m * a(9.81m/s^2)

x(t) = v(t)
v(t) = a(t) = F/m

small time steps of size h
x(h) = x(0) + hv(0)
v(h) = v(0) + h F/m

from xxxplots import

def forward_euler():
	h = 0.1
	g = 9.81

	num_steps = 50

	t = numpy.zeros(num_steps + 1)
	x = numpy.zeros(num_steps + 1)
	v = numpy.zeros(num_steps + 1)

	for step in range(num_steps):
		t[step + 1] = h * (step + 1)
		x[step + 1] = x[step] + h * v[step]
		v[step + 1] = v[step] - h * g
	return t, x, v

t, x, v = forward_euler()

@show_plot
def plot_me():
	axes_height = matplotlib.pyplot.subplot(211)
    matplotlib.pyplot.plot(t, x)
    axes_velocity = matplotlib.pyplot.subplot(212)
    matplotlib.pyplot.plot(t, v)
    axes_height.set_ylabel('Height in m')
    axes_velocity.set_ylabel('Velocity in m/s')
    axes_velocity.set_xlabel('Time in s')
    # Uncomment the line below when running locally.
    # matplotlib.pyplot.show() 

plot_me()
import numpy
import matplotlib.pyplot

def forward_euler():
	h = 0.1
	g = 9.81

	num_steps = 50

	t = numpy.zeros(num_steps + 1)
	x = numpy.zeros(num_steps + 1)
	v = numpy.zeros(num_steps + 1)

	for step in range(num_steps):
		t[step + 1] = h * (step + 1)
		x[step + 1] = x[step] + h * v[step]
		v[step + 1] = v[step] - h * g
	return t, x, v

t, x, v = froward_euler()

if so

k = 130
m = 7655

if k > 42:
    print 'Hi'
elif m > 43:
    print 'Hello'
else:
    print 'Bye'
======================== RESTART: C:/Python27/test.py ========================
Hi
>>> 
import numpy

n = numpy.zeros(5)
n[0] = 20.
n[4] = 99.

print n
print n[-1]
import numpy

p = numpy.zeros([2, 3])

p[0, 1] = 7.
p[1, 2] = 8.

print p
p = 1. + 2. * p

print p
def three_times(u):
    r = 2 * u
    return r + u

print three_times(1), three_times(7)
======================== RESTART: C:/Python27/test.py ========================
3 21
>>> 
import math
from xxx import *

def sin_cos():
    num_points = 50

    x = numpy.zeros(num_points)
    sin_x = numpy.zeros(num_points)
    cos_x = numpy.zeros(num_points)

    for i in range(num_points):
        x[i] = 2. * math.pi * i / (num_points - 1.)
        sin_x[i] = math.sin(x[i])
        cos_x[i] = math.cos(x[i])

    return x, sin_x, cos_x

x, sin_x, cos_x = sin_cos()

@show_plot
def plot_me():
    matplotlib_pyplot.plot(x, sin_x)
    matplotlib_pyplot.plot(x, cos_x)
plot_me()

Python Playground

import math

f = math.sqrt(4)
g = radius_of_earth * math.sin(math.pi / 180.0 * 23.4)
g = radius_of_earth * math.sin(math.pi / 180.0 * 23.4)

print f, g
======================== RESTART: C:/Python27/test.py ========================
2.0 2530229.21123
>>> 
for h in range(7):
    i = h**2
    print i
======================== RESTART: C:/Python27/test.py ========================
0
1
4
9
16
25
36

k = 42
m = 43

while k < 130 or m == 140: k += 1 m += k print k, m [/python]

======================== RESTART: C:/Python27/test.py ========================
130 7655
>>> 

Houston We Have a Problem

Apollo 13
TLI, MCC-2, DPS-1

what is important?
mass of the spacecraft, size of the moon, motion of the moon, size of the earth, motion of the earth, 3D

sine & friends
b/c = sinβ
a/c = cosβ

60°=1rad、0.1rad=6°、sin40°=6.4
sin(40)=0.6427876096865393

Motion of the moon around the earth
t = time
4.10^8 2πt*27days

Using match

def get_db(db_name):
	from pymongo import MongoClient
	client = MongoClient('localhost:27017')
	db = client[db_name]
	return db

def make_pipeline():
	pipeline = [ ]
	return pipeline

def aggregate(db, pipeline):
	return [doc for doc in db.tweets.aggregate(pipeline)]

if __name__ == '__main__':
	db = get_db('twitter')
	pipeline = make_pipeline()
	result = aggregate(db, pipeline)
	import pprint
	assert len(result) == 1
	assert result[0]["followers"] == 17209

Insert into the DB

import json

def insert_data(data, db)

	passs

if __name__ == "__main__":

	from pymongo import MongoClient
	client = MongoClient("mongodb://localhost:27017")
	db = client.examples

	with open('arachnid.json') as f:
		data = json.loads(f.read())
		insert_data(data, db)
		print db.arachnid.find_one()

Preparing data

import codecs
import csv
import json
import pprint
import re

DATAFILE = 'arachnid.csv'
FIELDS ={'rdf-schema#label': 'label',
         'URI': 'uri',
         'rdf-schema#comment': 'description',
         'synonym': 'synonym',
         'name': 'name',
         'family_label': 'family',
         'class_label': 'class',
         'phylum_label': 'phylum',
         'order_label': 'order',
         'kingdom_label': 'kingdom',
         'genus_label': 'genus'}

def process_file(filename, fields):
	process_fields = fields.keys()
	data = []
	with open(filename, "r") as f:
		reader = csv.DictReader(f)
		for i in range(3):
			l = reader.next()

		for line in reader:
			pass
	return data

def parse_array(v):
	if(v[0] == "{") and (v[-1] == "}"):
		v = v.lstrip("{")
		v = v.rstrip("}")
		v_array = v.split("|")
		v_array = [i.strip() for i in v_array]
		return v_array
	return [v]

def test():
	data = process_file(DATAFILE, FIELDS)
	print "your first entry:"
	pprint.pprint(data[0])
	first_entry = {
		"synonym": None,
		"name": "Argiope",
		"classification" : {
			"kingdom":"Animal",
			"family":"Orb-weaver spider",
			"order": "Spider",
			"phylum": "Arthropod",
			"genus": None,
			"class": "Arachnid"
		},
		"uri": "http://dbpedia.org/resource/Argiope_(spider)",
		"label":"Argiope",
		"description": "The genus Argiope includes rather large and spectacular spiders that often have a strikingly coloured abdomen. These spiders are distributed throughout the world. Most countries in tropical or temperate climates host one or more species that are similar in appearance. The etymology of the name is from a Greek name meaning silver-faced."
	}
	assert len(data) = 76
	assert data[0] == first_entry
	assert data[17]["name"] == "Ogdenia"
	assert data[48]["label"] == "Hydrachnidiae"
	assert data[14]["synonym"] == ["Cyrene Peckham & Peckham"]

if __name__ == "__main__"
	test

Range Queries

#!/usr/bin/env python
import pprint

client = MongoClient("mongodb://localhost:27017")

db = client.examples

def find():
	query = {"population" : {"$gt" : 250000}}
	cities = db.cities.find(query)

	num_cities = 0
	for c in cities:
		pprint.pprint(c)
		num_cities += 1

	print "\nNumber of cities matching: %d\n" % num_cities
from datetime import datetime

def range_query():
	query = {}
	return query

def get_db():
	from pymongo import MongoClient
	client = MongoClient('localhost:27017')
	db = client.examples
	return db

if __name__ == "__main__":
	db = get_db()
	query = range_query()
	cities = db.cities.find(query)

	print "Found cities:", cities.count()
	import pprint
	pprint.pprint(cities[0])

Multiple Field

from pymongo import MongoClient
import pprint

client = MongoClient("mongodb://localhost:27017")

db = client.examples

def find():
	autos = db.autos.find(
		{
			"manufacturer" : "Toyota" , "class": "mid-size car"
		})
		for a in autos:
			pprint.pprint(a)

if __name__ == '__main__':
	find()
#!/usr/bin/env python
from autos import process_file

def insert_autos(infile, db):
	data = process_file(infile)

if __name__ == "__main__":
	from pymongo import MongoClient
	client = MongoClient("mongodb://localhost:27017")
	db = client.examples

	insert_autos('autos-small.csv', db)
	print db.autos.find_one()