php for文でstr 0000~9999をつくる


for($i=0; $i<10; $i++){
	for($j=0; $j<10; $j++){
		echo $i.$j."<br>";

000~999 入れ子を増やす

$time_start = microtime(true);
for($i=0; $i<10; $i++){
	for($j=0; $j<10; $j++){
		for($k=0; $k<10; $k++){
			echo $i.$j.$k."<br>";
$time = microtime(true) - $time_start;
echo "{$time} 秒";

0.0011639595031738 秒

0~9999 4重にする

$time_start = microtime(true);
for($i=0; $i<10; $i++){
	for($j=0; $j<10; $j++){
		for($k=0; $k<10; $k++){
			for($l=0; $l<10; $l++){
				echo $i.$j.$k.$l."<br>";
$time = microtime(true) - $time_start;
echo "{$time} 秒";

0.017668008804321 秒


Functions for Lines

from decimal import Decimal, getcontext

from vector import vector
getcontext().prec = 30

class Line(object):
	NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero element found'

	def __init__(self, normal_vector=None, constant_term=None):
		self.dimension = 2

		if not normal_vector:
			all_zeros = ['0']*self.dimension
			normal_vector = Vector(all_zeros)
		self.normal_vector = normal_vector

Ax + By + Cz = k1
Dx + Ey + Fz = k2
[A B C], [D E F] not parallel
dx = vector from x0 to x
Since x0, x in plane 1, dx is orth to n1

-One equation in two variables defines a line
-One equation in three variables define a plane

Caution: Count variables with coefficient 0
x + y = 2 defines a line in 2D
x + y = 2 defines a plane in 3D

systematically manipulate the system to make it easier to find the solution
– Manipulations should preserve the solution
– Manipulations should be reversible


Geometric object = a set of points satisfying a given relationship(e.g. equations)

“Flat” = defined by linear equations
Linear equations:
-Can add and subtract variables and constants
-Can multiply a variable by a constant

Linear: x + 2y = 1, y/2 – 2z =x
Nonlinear: x^2 -1 = y, y/x = 3

Equations come from observed or modeled relationships between real-world quantities.

ML for trading
stock:A, B
Wa = proportion of portfolio invested in A
Wb = proportion of portfolio invested in B
0 < Wa, Wb < 1 β value = measure of correlation of a stock's price movements with market movement β of portfolio = weighted average of individual components' β values Wa + Wb = 1 B portfolio = 2Wb - Wa = 0 Variables: weights of the stocks Constraints: equations representing physical realities or objectives Lines in Two dimensions Two pieces of info define a line -basepoint x -direction vector v x(t) = x + tv Ax + By = 0 (A and B not both 0)

Inner Products

Inner Products(Dot Products)
v*w = ||v||*||w||*cosΘ

def dot(self, v):
	return sum([x*y for x,y in zip(self.coordinates, v.coordinates)])

	def angle_with(self, v, in_degrees=False)
			u1 = self.normalized()
			u2 = v.normalized()
			angle_in_raddians = acos(

			if in_degrees:
				degrees_per_radian = 180./ pi
				return angle_in_radians * degrees_per_radian
				return angle_in_radians

			except Exception as e:
					raise Exception('Cannot compute an angle with the zero vector')
					raise e

Magnitude and Direction

P -> Q
magnitude of v = distance between P and Q
h^2 = (△x)^2 + (△y)^2
v = √(Vx^2 + Vy^2)

-A unit vector is a vector whose magnitude is 1.
-A vector’s direction can be represented by a unit vector.

Normalization: process of finding a unit vector in the same direction as a given vector.
[0 0 0] = 0
||0|| = 0
1 / ||0|| = ?

def magnitude(self):
	coordinates_squared = [x**2 for x in self.coordinates]
	return sqrt(sum(coordinates_squared))

def normalized(self):
		magnitude = self.magnitude()
		return self.times_scalar(1./magnitude)

	except ZeroDivisionError:
		raise Exception('Cannot normalize the zero vector')

def plus(self, v):
	new_coordinates = [x+y for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

Operating on Vectors

– Addition
– Subtraction
[1 3] – [5 1] = [-4 2]
– Scalar Multiplication
2, 1, 1/2, -3/2

[8.218 -9.341]+[-1.129 2.111]=[7.089 -7.23]
[7.119 8.215] – [-8.223 0.878]=[15.342 7.337]
7.41[1.671 -1.012 -0.318]= [12.382 -7.499 -2.356]

def plus(self, v):
	new_coordinates = [x+y for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

def minus(self, v):
	new_coordinates = [x-y for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

def times_scalar(self, c):
	new_coordinates = [c*x for x,y in zip(self.coordinates, v.coordinates)]
	return Vector(new_coordinates)

def __str__(self):
	return 'Vector: {}'.format(self.coordinates)

def __eg__(self, v):
	return self.coordinates == v.coordinates

Point and Vectors

-represent location
-written as (x,y,z…)
e.g. (2,-1)

-represent change in location
-magnitude and direction
-written as [x,y,z…]

Common practice to interchange points and vectors
Technically different but often a useful shortcut
V=[-2, -3] P=(-2,-3)
P=(-4,-4), Q=(4,-2)

class Vector(object):
	def __init__(self, coordinates):
			if not coordinates:
				raise ValueError
			self.coordinate = tuple(cordinates)
			self.dimension = len(coordinates)

		except ValueError:
			raise ValueError('The coordinates must be nonempty')

		except TypeError:
			raise TypeError('The coordinate must be an iterable')

	def __str__(self):
		return 'Vector: {}'.format(self.coordinates)