Python High-level General-purpose Dynamic Readable Multi-paradigm Portable
Pre-Defined Functions Python x + y x * y x - y x / y x**y abs(x) sqrt(x) exp(x) pow(x, y) log(x) sin(x) atan(x) atan2(y, x) floor(x) Matematics x + y x y x y x/y x y x x e x x y log x sin x atan x atan y x x
From Mathematics to Python 1 2 + 3 + sin 5 2 2
From Mathematics to Python 1 2 + 3 + sin 5 2 2 1/2 + sqrt(3) + sin(2)**(5/2)
From Mathematics to Python 1 2 + 3 + sin 5 2 2 1/2 + sqrt(3) + sin(2)**(5/2) cos4 2 5 atan 3
From Mathematics to Python 1 2 + 3 + sin 5 2 2 1/2 + sqrt(3) + sin(2)**(5/2) cos4 2 5 atan 3 (cos(2/sqrt(5))**4)/atan(3)
From Mathematics to Python 1 2 + 3 + sin 5 2 2 1/2 + sqrt(3) + sin(2)**(5/2) cos4 2 5 atan 3 (cos(2/sqrt(5))**4)/atan(3) 1 log 2 (3 9 log 25)
From Mathematics to Python 1 2 + 3 + sin 5 2 2 1/2 + sqrt(3) + sin(2)**(5/2) cos4 2 5 atan 3 (cos(2/sqrt(5))**4)/atan(3) 1 log 2 (3 9 log 25) sqrt(1/log(2**abs(3-9*log(25))))
From Python to Mathematics log(sin(2 + floor(atan(pi))/sqrt(5)))
From Python to Mathematics log(sin(2 + floor(atan(pi))/sqrt(5))) log sin(2 + atan π 5 )
From Python to Mathematics log(sin(2 + floor(atan(pi))/sqrt(5))) log sin(2 + atan π 5 ) cos(cos(cos(0.5)))**5
From Python to Mathematics log(sin(2 + floor(atan(pi))/sqrt(5))) log sin(2 + atan π 5 ) cos(cos(cos(0.5)))**5 cos 5 cos cos 0.5
From Python to Mathematics log(sin(2 + floor(atan(pi))/sqrt(5))) log sin(2 + atan π 5 ) cos(cos(cos(0.5)))**5 cos 5 cos cos 0.5 sin(cos(sin(pi/3)/3)/3)
From Python to Mathematics log(sin(2 + floor(atan(pi))/sqrt(5))) log sin(2 + atan π 5 ) cos(cos(cos(0.5)))**5 cos 5 cos cos 0.5 sin(cos(sin(pi/3)/3)/3) sin cos sin π 3 3 3
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results Example: f(x) = x x
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results Example: f(x) = x x Python:
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results Example: f(x) = x x Python: def f(x): return x*x
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results Example: f(x) = x x Python: def f(x): return x*x > f(3)
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results Example: f(x) = x x Python: def f(x): return x*x > f(3) 9
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results Example: f(x) = x x Python: def f(x): return x*x > f(3) 9 > f(5)
From Mathematics to Python Function A mapping between arguments and results The mapping is implicit, as it is computed when the function is applied to a set of arguments and returns the result of the computation A function accepts arguments and returns results Example: f(x) = x x Python: def f(x): return x*x > f(3) 9 > f(5) 25
Arithmetic in Python A number can be: Integral Rational Real Complex Number
Arithmetic in Python A number can be: Integral Rational Real Complex Number A number can also be: Exact Inexact Inexactness is contagious: whenever an inexact number is used in an operation, the result is inexact
Arithmetic in Python A number can be: Integral Rational Real Complex Number A number can also be: Exact Inexact Inexactness is contagious: whenever an inexact number is used in an operation, the result is inexact All inexact numbers above a pre-defined limit are infinite and cause an error
Arithmetic in Python A number can be: Integral Rational Real Complex Number A number can also be: Exact Inexact Inexactness is contagious: whenever an inexact number is used in an operation, the result is inexact All inexact numbers above a pre-defined limit are infinite and cause an error Inexactness is contagious: whenever an inexact number is used in an operation, the result is inexact
Arithmetic in Python A number can be: Integral Rational Real Complex Number A number can also be: Exact Inexact Inexactness is contagious: whenever an inexact number is used in an operation, the result is inexact All inexact numbers above a pre-defined limit are infinite and cause an error Inexactness is contagious: whenever an inexact number is used in an operation, the result is inexact Python uses the word float for inexact numbers
Arithmetic in Python Exact Numbers > 10*10
Arithmetic in Python Exact Numbers > 10*10 10000000000
Arithmetic in Python Exact Numbers > 10*10 10000000000 > 10*100
Arithmetic in Python Exact Numbers > 10*10 10000000000 > 10*100 100000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000
Arithmetic in Python Exact Numbers > 10*10 10000000000 > 10*100 100000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000 > 10**1000
Arithmetic in Python Exact Numbers > 10*10 10000000000 > 10*100 100000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000 > 10**1000 100000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000
Arithmetic in Python Inexact Numbers > 10.0**10
Arithmetic in Python Inexact Numbers > 10.0**10 10000000000.0
Arithmetic in Python Inexact Numbers > 10.0**10 10000000000.0 > 10.0**100
Arithmetic in Python Inexact Numbers > 10.0**10 10000000000.0 > 10.0**100 1e+100
Arithmetic in Python Inexact Numbers > 10.0**10 10000000000.0 > 10.0**100 1e+100 > 10.0**1000
Arithmetic in Python Inexact Numbers > 10.0**10 10000000000.0 > 10.0**100 1e+100 > 10.0**1000 OverflowError: 'Result too large'
Arithmetic in Python Inexact Numbers > 10.0**10 10000000000.0 > 10.0**100 1e+100 > 10.0**1000 OverflowError: 'Result too large' Inexact numbers have limited size and limited precision But they are much more efficient to compute There are no solutions, only trade-offs
Arithmetic in Python Rounding Errors > (4/3-1)*3-1
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10)
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10)
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7)
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7) 0.9999999701976776
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7) 0.9999999701976776 > f(1209983553611.9)
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7) 0.9999999701976776 > f(1209983553611.9) 0.999755859375
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7) 0.9999999701976776 > f(1209983553611.9) 0.999755859375 > f(19843566622234755.9)
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7) 0.9999999701976776 > f(1209983553611.9) 0.999755859375 > f(19843566622234755.9) 0.0
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7) 0.9999999701976776 > f(1209983553611.9) 0.999755859375 > f(19843566622234755.9) 0.0 > f(553774558711019983333.9)
Arithmetic in Python Rounding Errors > (4/3-1)*3-1 -2.220446049250313e-16 Dramatic Example: f(x) = x 0.1 (10 x 10) > def f(x): return x - 0.1*(10*x - 10) > f(5.1) 0.9999999999999991 > f(176498634.7) 0.9999999701976776 > f(1209983553611.9) 0.999755859375 > f(19843566622234755.9) 0.0 > f(553774558711019983333.9) -65536.0
Conditional Expressions x = { x, if x < 0 x, otherwise.
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression An expression whose value depends on one or more conditions if α, return β, else return γ
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression An expression whose value depends on one or more conditions if α, return β, else return γ return β if α, else γ
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression An expression whose value depends on one or more conditions if α, return β, else return γ return β if α, else γ α is the condition, β is the consequent, γ is the alternative
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression An expression whose value depends on one or more conditions if α, return β, else return γ return β if α, else γ α is the condition, β is the consequent, γ is the alternative Logical Expression An expression whose value is either true or false α is a logical expression
Conditional Expressions Logical Value x = { x, if x < 0 x, otherwise. True or False In Python: True, False False, None, 0, (and others) are considered false, anything else is considered true (including True) Conditional expressions use the logical value of the condition to evaluate the correct choice
Conditional Expressions x = { x, if x < 0 x, otherwise. Predicate A function (or operator) that always returns true or false < is a predicate Relational Operator A predicate that compares entities <, >, =,,, and are relational operator In Python: <, >, ==, <=, >= and!=
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression return β if α else γ
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression return β if α else γ if α, return β, else return γ
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression return β if α else γ if α, return β, else return γ Python: def abs(x): return -x if x < 0 else x
Conditional Expressions x = { x, if x < 0 x, otherwise. Conditional Expression return β if α else γ if α, return β, else return γ Python: def abs(x): return -x if x < 0 else x def abs(x): if x < 0: return -x else: return x
Conditional Expressions Python: max(x, y) = def max(x, y): return x if x > y else y { x, if x > y y, otherwise.
Conditional Expressions Python: max(x, y) = def max(x, y): return x if x > y else y def max(x, y): if x > y: return x else: return y { x, if x > y y, otherwise.
Conditional Expressions 1 if x < 0 sgn x = 0 if x = 0 1 otherwise
Conditional Expressions 1 if x < 0 sgn x = 0 if x = 0 1 otherwise 1 { if x < 0 sgn x = 0 if x = 0 otherwise 1 otherwise
Conditional Expressions Python: 1 if x < 0 sgn x = 0 if x = 0 1 otherwise 1 { if x < 0 sgn x = 0 if x = 0 otherwise 1 otherwise
Conditional Expressions Python: 1 if x < 0 sgn x = 0 if x = 0 1 otherwise 1 { if x < 0 sgn x = 0 if x = 0 otherwise 1 otherwise def signum(x): return -1 if x < 0 else 0 if x == 0 else 1
Conditional Expressions Python: 1 if x < 0 sgn x = 0 if x = 0 1 otherwise 1 { if x < 0 sgn x = 0 if x = 0 otherwise 1 otherwise def signum(x): return -1 if x < 0 else (0 if x == 0 else 1)
Conditional Expressions Python: def signum(x): if x < 0: return -1 else: if x == 0: return 0 else: return 1 1 if x < 0 sgn x = 0 if x = 0 1 otherwise 1 { if x < 0 sgn x = 0 if x = 0 otherwise 1 otherwise
Conditional Expressions Python: def signum(x): if x < 0: return -1 elif x == 0: return 0 else: return 1 1 if x < 0 sgn x = 0 if x = 0 1 otherwise 1 { if x < 0 sgn x = 0 if x = 0 otherwise 1 otherwise
Local Variables c b. a
Local Variables c b. a Heron s formula A = s (s a) (s b) (s c) where s = a + b + c 2
Local Variables Heron s formula A = s (s a) (s b) (s c) where s = a + b + c 2 Local Variable s is a local variable A local variable is an additional name That abstracts concepts That simplifies expressions That avoids repeated computations
Local Variables Heron s formula A = s (s a) (s b) (s c) where s = a + b + c 2 def triangle_area(a, b, c): s = (a + b + c)/2 return sqrt(s*(s - a)*(s - b)*(s - c))
Local Variables Heron s formula A = s (s a) (s b) (s c) where s = a + b + c 2 def triangle_area(a, b, c): s = (a + b + c)/2 return sqrt(s*(s - a)*(s - b)*(s - c)) Local Variable A variable is defined the first time it is given a value The scope of the variable is the function that contains its definition
Global Variables pi = 3.14159 def area_circle(r): return pi*r*r
Global Variables pi = 3.14159 def area_circle(r): return pi*r*r Global Variable A local variable is visible only in the function where it is defined A global variable is visible in the entire program The definition of global variables is identical to the definition of local variables, but they must be defined outside any function definition In most cases, global variables should be constants
Modules Every time Python is restarted, it starts in a clean state All definitions made before are lost In order to save them, put them in a file (called a module) We import the module to regain the definitions Most of Python is stored in modules
Modules Every time Python is restarted, it starts in a clean state All definitions made before are lost In order to save them, put them in a file (called a module) We import the module to regain the definitions Most of Python is stored in modules import math
Modules Every time Python is restarted, it starts in a clean state All definitions made before are lost In order to save them, put them in a file (called a module) We import the module to regain the definitions Most of Python is stored in modules import math math.sqrt(2) + math.sin(math.pi)
Modules Every time Python is restarted, it starts in a clean state All definitions made before are lost In order to save them, put them in a file (called a module) We import the module to regain the definitions Most of Python is stored in modules import math as m m.sqrt(2) + m.sin(m.pi)
Modules Every time Python is restarted, it starts in a clean state All definitions made before are lost In order to save them, put them in a file (called a module) We import the module to regain the definitions Most of Python is stored in modules from math import sqrt, sin sqrt(2) + sin(math.pi)
Modules Every time Python is restarted, it starts in a clean state All definitions made before are lost In order to save them, put them in a file (called a module) We import the module to regain the definitions Most of Python is stored in modules from math import * sqrt(2) + sin(pi)
Modules import khepri khepri.box(khepri.xyz(2,1,1), khepri.xyz(3,4,5)) khepri.cone(khepri.xyz(6,0,0), 1, khepri.xyz(8,1,5)) khepri.cone_frustum(khepri.xyz(11,1,0), 2, khepri.xyz(10,0,5), 1) khepri.sphere(khepri.xyz(8,4,5), 2) khepri.cylinder(khepri.xyz(8,7,0), 1, khepri.xyz(6,8,7)) khepri.regular_pyramid(5, khepri.xyz(-2,1,0), 1, 0, khepri.xyz(2,7,7)) khepri.torus(khepri.xyz(14,6,5), 2, 1)
Modules import khepri as kh kh.box(kh.xyz(2,1,1), kh.xyz(3,4,5)) kh.cone(kh.xyz(6,0,0), 1, kh.xyz(8,1,5)) kh.cone_frustum(kh.xyz(11,1,0), 2, kh.xyz(10,0,5), 1) kh.sphere(kh.xyz(8,4,5), 2) kh.cylinder(kh.xyz(8,7,0), 1, kh.xyz(6,8,7)) kh.regular_pyramid(5, kh.xyz(-2,1,0), 1, 0, kh.xyz(2,7,7)) kh.torus(kh.xyz(14,6,5), 2, 1)
Modules from khepri import * box(xyz(2,1,1), xyz(3,4,5)) cone(xyz(6,0,0), 1, xyz(8,1,5)) cone_frustum(xyz(11,1,0), 2, xyz(10,0,5), 1) sphere(xyz(8,4,5), 2) cylinder(xyz(8,7,0), 1, xyz(6,8,7)) regular_pyramid(5, xyz(-2,1,0), 1, 0, xyz(2,7,7)) torus(xyz(14,6,5), 2, 1)
Cartesian Coordinates Z Y P. z x y X
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2 def distance(px, py, pz, qx, qy, qz): return sqrt((qx-px)**2 + (qy-py)**2 + (qz-pz)**2)
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2 def distance(px, py, pz, qx, qy, qz): return sqrt((qx-px)**2 + (qy-py)**2 + (qz-pz)**2) The distance between P = (2, 1, 3) and Q = (5, 6, 4) is > distance(2, 1, 3, 5, 6, 4) 5.916079783099616
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2 def distance(px, py, pz, qx, qy, qz): return sqrt((qx-px)**2 + (qy-py)**2 + (qz-pz)**2) The distance between P = (2, 1, 3) and Q = (5, 6, 4) is > distance(2, 1, 3, 5, 6, 4) 5.916079783099616 It is not as clear as in mathematics
Cartesian Coordinates Intermediate Point The Intermediate point between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is ( p x + q x 2, p y + q y 2, p z + q z ) 2
Cartesian Coordinates Intermediate Point The Intermediate point between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is ( p x + q x 2, p y + q y 2, p z + q z ) 2 def intermediate_point(px, py, pz, qx, qy, qz): return...(px+qx)/2, (py+qy)/2, (pz+qz)/2...
Cartesian Coordinates Intermediate Point The Intermediate point between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is ( p x + q x 2, p y + q y 2, p z + q z ) 2 def intermediate_point(px, py, pz, qx, qy, qz): return...(px+qx)/2, (py+qy)/2, (pz+qz)/2... We need to handle coordinates as an entity, and not as triplets of numbers
Cartesian Coordinates from khepri import *
Cartesian Coordinates from khepri import * > xyz(1, 2, 3)
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3)
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3) > cx(xyz(1, 2, 3))
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3) > cx(xyz(1, 2, 3)) 1
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3) > cx(xyz(1, 2, 3)) 1 > cy(xyz(1, 2, 3))
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3) > cx(xyz(1, 2, 3)) 1 > cy(xyz(1, 2, 3)) 2
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3) > cx(xyz(1, 2, 3)) 1 > cy(xyz(1, 2, 3)) 2 > cz(xyz(1, 2, 3))
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3) > cx(xyz(1, 2, 3)) 1 > cy(xyz(1, 2, 3)) 2 > cz(xyz(1, 2, 3)) 3 cx(xyz(x, y, z)) = x cy(xyz(x, y, z)) = y cz(xyz(x, y, z)) = z
Cartesian Coordinates from khepri import * > xyz(1, 2, 3) xyz(1, 2, 3) > cx(xyz(1, 2, 3)) 1 > cy(xyz(1, 2, 3)) 2 > cz(xyz(1, 2, 3)) 3 cx(xyz(x, y, z)) = x cy(xyz(x, y, z)) = y cz(xyz(x, y, z)) = z Operations for Cartesian Coordinates xyz is the constructor cx, cy, and cz, are the selectors
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2 def distance(px, py, pz, qx, qy, qz): return sqrt((qx-px)**2 + (qy-py)**2 + (qz-pz)**2) The distance between P = (2, 1, 3) and Q = (5, 6, 4) is > distance(2, 1, 3, 5, 6, 4) 5.916079783099616
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2 def distance(p, q): return sqrt((qx?-px?)**2 + (qy?-py?)**2 + (qz?-pz?)**2) The distance between P = (2, 1, 3) and Q = (5, 6, 4) is > distance(xyz(2, 1, 3), xyz(5, 6, 4)) 5.916079783099616
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2 def distance(p, q): return sqrt((cx(q)-cx(p))**2+(cy(q)-cy(p))**2+(cz(q)-cz(p))**2) The distance between P = (2, 1, 3) and Q = (5, 6, 4) is > distance(xyz(2, 1, 3), xyz(5, 6, 4)) 5.916079783099616
Cartesian Coordinates Distance The distance d between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is d = (q x p x ) 2 + (q y p y ) 2 + (q z p z ) 2 def distance(p, q): return sqrt((q.x-p.x)**2 + (q.y-p.y)**2 + (q.z-p.z)**2) The distance between P = (2, 1, 3) and Q = (5, 6, 4) is > distance(xyz(2, 1, 3), xyz(5, 6, 4)) 5.916079783099616
Cartesian Coordinates Intermediate Point The Intermediate point P m between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is P m = ( p x + q x 2, p y + q y 2, p z + q z ) 2 def intermediate_point(px, py, pz, qx, qy, qz): return...(px+qx)/2, (py+qy)/2, (pz+qz)/2...
Cartesian Coordinates Intermediate Point The Intermediate point P m between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is P m = ( p x + q x 2, p y + q y 2, p z + q z ) 2 def intermediate_point(p, q): return xyz((cx(p)+cx(q))/2, (cy(p)+cy(q))/2, (cz(p)+cz(q))/2)
Cartesian Coordinates Intermediate Point The Intermediate point P m between P = (p x, p y, p z ) and Q = (q x, q y, q z ) is P m = ( p x + q x 2, p y + q y 2, p z + q z ) 2 def intermediate_point(p, q): return xyz((p.x+q.x)/2, (p.y+q.y)/2, (p.z+q.z)/2)
Cartesian Coordinates > xy(1, 2)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1) xyz(1, 0, 0)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1) xyz(1, 0, 0) > y(2)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1) xyz(1, 0, 0) > y(2) xyz(0, 2, 0)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1) xyz(1, 0, 0) > y(2) xyz(0, 2, 0) > z(3)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1) xyz(1, 0, 0) > y(2) xyz(0, 2, 0) > z(3) xyz(0, 0, 3)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1) xyz(1, 0, 0) > y(2) xyz(0, 2, 0) > z(3) xyz(0, 0, 3) def xy(x, y): return xyz(x, y, 0) def xz(x, z): return xyz(x, 0, z) def yz(y, z): return xyz(0, y, z)
Cartesian Coordinates > xy(1, 2) xyz(1, 2, 0) > yz(2, 3) xyz(0, 2, 3) > xz(1, 3) xyz(1, 0, 3) > x(1) xyz(1, 0, 0) > y(2) xyz(0, 2, 0) > z(3) xyz(0, 0, 3) def xy(x, y): return xyz(x, y, 0) def xz(x, z): return xyz(x, 0, z) def yz(y, z): return xyz(0, y, z) def x(x): return xyz(x, 0, 0) def y(y): return xyz(0, y, 0) def z(z): return xyz(0, 0, z)
Cartesian Coordinates Example: Min. number of risers for max. riser of 0.18? Y P 1 X.... P 0
Cartesian Coordinates Example: Min. number of risers for max. riser of 0.18? Y P 1 X.... P 0 def minimum_number_of_steps(p0, p1): return ceil((cy(p1) - cy(p0))/0.18)
Cartesian Coordinates Example: Min. number of risers for max. riser of 0.18? Y P 1 X.... P 0 def minimum_number_of_steps(p0, p1): return ceil((p1.y - p0.y)/0.18)
Cartesian Coordinates Translation Z P Y. X
Cartesian Coordinates Translation Z P Y z. x y X
Cartesian Coordinates Translation Z P P Y z. x y X
Cartesian Coordinates Translation Z P P Y z z z. x x y y y X
Cartesian Coordinates Translation Z P V Y P z z z. x x y y y X
Cartesian Coordinates Translation Z P V Y P z V z z. x x y y y X
Cartesian Coordinates Translation Z P V Y P z V z. x x z y y z y X x y
Cartesian Coordinates Translation Z P V Y P z V z. x x z y y z y X x y P = P + V
Cartesian Coordinates Translation Z P V Y P z V z. x x z y y z y X x y V = P P
Cartesian Coordinates Vectors A vector represents a displacement A vector does not represent a location A vector does not have an origin or a destination A vector is created by the constructor vxyz
Cartesian Coordinates Vectors A vector represents a displacement A vector does not represent a location A vector does not have an origin or a destination A vector is created by the constructor vxyz def vxy(dx, dy): return vxyz(dx, dy, 0) def vxz(dx, dz): return vxyz(dx, 0, dz) def vyz(dy, dz): return vxyz(0, dy, dz)
Cartesian Coordinates Vectors A vector represents a displacement A vector does not represent a location A vector does not have an origin or a destination A vector is created by the constructor vxyz def vx(dx): return vxyz(dx, 0, 0) def vy(dy): return vxyz(0, dy, 0) def vz(dz): return vxyz(0, 0, dz)
Cartesian Coordinates Vectors A vector represents a displacement A vector does not represent a location A vector does not have an origin or a destination A vector is created by the constructor vxyz xyz(x, y, z)+vxyz( x, y, z ) xyz(x + x, y + y, z + z )
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2) vxyz(0,2,-2)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2) vxyz(0,2,-2) > xyz(1,2,3) + (xyz(4,5,6) - xyz(3,2,1))
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2) vxyz(0,2,-2) > xyz(1,2,3) + (xyz(4,5,6) - xyz(3,2,1)) xyz(2,5,8)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2) vxyz(0,2,-2) > xyz(1,2,3) + (xyz(4,5,6) - xyz(3,2,1)) xyz(2,5,8) > xyz(1,2,3) + xyz(4,5,6) - xyz(3,2,1)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2) vxyz(0,2,-2) > xyz(1,2,3) + (xyz(4,5,6) - xyz(3,2,1)) xyz(2,5,8) > xyz(1,2,3) + xyz(4,5,6) - xyz(3,2,1) RuntimeError: Positions cannot be added
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2) vxyz(0,2,-2) > xyz(1,2,3) + (xyz(4,5,6) - xyz(3,2,1)) xyz(2,5,8) > xyz(1,2,3) + xyz(4,5,6) - xyz(3,2,1) RuntimeError: Positions cannot be added > xyz(1,2,3) - xyz(3,2,1) + xyz(4,5,6)
Cartesian Coordinates Examples > xyz(1,2,3) + vxyz(3,2,1) xyz(4,4,4) > xyz(1,2,3) + vx(4) xyz(5,2,3) > xyz(1,2,3) + vyz(4,5) xyz(1,6,8) > xyz(1,2,3) - yz(1,1) vxyz(1,1,2) > vxy(1,2) + vxz(1,2) vxyz(2,2,2) > vxy(1,2) - vxz(1,2) vxyz(0,2,-2) > xyz(1,2,3) + (xyz(4,5,6) - xyz(3,2,1)) xyz(2,5,8) > xyz(1,2,3) + xyz(4,5,6) - xyz(3,2,1) RuntimeError: Positions cannot be added > xyz(1,2,3) - xyz(3,2,1) + xyz(4,5,6) xyz(2,5,8)
Polar Coordinates Y 2π n d. X
Polar Coordinates. ϕ ρ x y
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi))
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0)
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0) xyz(1, 0, 0)
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0) xyz(1, 0, 0) > pol(sqrt(2), pi/4)
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0) xyz(1, 0, 0) > pol(sqrt(2), pi/4) xyz(1.0000000000000002, 1.0, 0)
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0) xyz(1, 0, 0) > pol(sqrt(2), pi/4) xyz(1.0000000000000002, 1.0, 0) > pol(1, pi/2)
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0) xyz(1, 0, 0) > pol(sqrt(2), pi/4) xyz(1.0000000000000002, 1.0, 0) > pol(1, pi/2) xyz(6.123233995736766e-017, 1.0, 0)
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0) xyz(1, 0, 0) > pol(sqrt(2), pi/4) xyz(1.0000000000000002, 1.0, 0) > pol(1, pi/2) xyz(6.123233995736766e-017, 1.0, 0) > pol(1, pi)
Polar Coordinates. ϕ ρ x y def pol(rho, phi): return xy(rho*cos(phi), rho*sin(phi)) > pol(1, 0) xyz(1, 0, 0) > pol(sqrt(2), pi/4) xyz(1.0000000000000002, 1.0, 0) > pol(1, pi/2) xyz(6.123233995736766e-017, 1.0, 0) > pol(1, pi) xyz(-1.0, 1.2246467991473532e-016, 0)
Polar Coordinates. ϕ ρ x y def vpol(rho, phi): return vxy(rho*cos(phi), rho*sin(phi)) > vpol(1, 0) vxyz(1, 0, 0) > vpol(sqrt(2), pi/4) vxyz(1.0000000000000002, 1.0, 0) > vpol(1, pi/2) vxyz(6.123233995736766e-017, 1.0, 0) > vpol(1, pi) vxyz(-1.0, 1.2246467991473532e-016, 0)
Polar Coordinates ρ P P ϕ.
Polar Coordinates ρ P P ϕ. > xy(1, 2) + vpol(sqrt(2), pi/4)
Polar Coordinates ρ P P ϕ. > xy(1, 2) + vpol(sqrt(2), pi/4) xyz(2.0, 3.0, 0.0)
Polar Coordinates ρ P P ϕ. > xy(1, 2) + vpol(sqrt(2), pi/4) xyz(2.0, 3.0, 0.0) > xy(1, 2) + vpol(1, 0)
Polar Coordinates ρ P P ϕ. > xy(1, 2) + vpol(sqrt(2), pi/4) xyz(2.0, 3.0, 0.0) > xy(1, 2) + vpol(1, 0) xyz(2.0, 2.0, 0.0)
Polar Coordinates ρ P P ϕ. > xy(1, 2) + vpol(sqrt(2), pi/4) xyz(2.0, 3.0, 0.0) > xy(1, 2) + vpol(1, 0) xyz(2.0, 2.0, 0.0) > xy(1, 2) + vpol(1, pi/2)
Polar Coordinates ρ P P ϕ. > xy(1, 2) + vpol(sqrt(2), pi/4) xyz(2.0, 3.0, 0.0) > xy(1, 2) + vpol(1, 0) xyz(2.0, 2.0, 0.0) > xy(1, 2) + vpol(1, pi/2) xyz(1.0, 3.0, 0.0)
Geometric Modeling
Geometric Modeling For AutoCAD from khepri.autocad import *
Geometric Modeling For AutoCAD from khepri.autocad import * For Rhino from khepri.rhino import *
Geometric Modeling.
Geometric Modeling For AutoCAD from khepri.autocad import *. circle(pol(0, 0), 4) circle(pol(4, pi/4), 2) circle(pol(6, pi/4), 1)
Geometric Modeling For AutoCAD from khepri.autocad import *. circle(pol(0, 0), 4) circle(pol(4, pi/4), 2) circle(pol(6, pi/4), 1) For Rhino from khepri.rhino import * circle(pol(0, 0), 4) circle(pol(4, pi/4), 2) circle(pol(6, pi/4), 1)
Geometric Modeling polygon(pol(1, 2*pi*0/5), pol(1, 2*pi*1/5), pol(1, 2*pi*2/5), pol(1, 2*pi*3/5), pol(1, 2*pi*4/5))
Geometric Modeling polygon(pol(1, 2*pi*0/5), pol(1, 2*pi*1/5), pol(1, 2*pi*2/5), pol(1, 2*pi*3/5), pol(1, 2*pi*4/5)).
Geometric Modeling regular_polygon(5).
Geometric Modeling regular_polygon(3, xy(0, 0), 1, 0, True) regular_polygon(3, xy(0, 0), 1, pi/3, True) regular_polygon(4, xy(3, 0), 1, 0, True) regular_polygon(4, xy(3, 0), 1, pi/4, True) regular_polygon(5, xy(6, 0), 1, 0, True) regular_polygon(5, xy(6, 0), 1, pi/5, True)
Geometric Modeling regular_polygon(3, xy(0, 0), 1, 0, True) regular_polygon(3, xy(0, 0), 1, pi/3, True) regular_polygon(4, xy(3, 0), 1, 0, True) regular_polygon(4, xy(3, 0), 1, pi/4, True) regular_polygon(5, xy(6, 0), 1, 0, True) regular_polygon(5, xy(6, 0), 1, pi/5, True).
Geometric Modeling rectangle(xy(0, 1), xy(3, 2)) rectangle(xy(3, 2), 1, 2)
Geometric Modeling rectangle(xy(0, 1), xy(3, 2)) rectangle(xy(3, 2), 1, 2).
Side Effects rectangle(xy(0, 1), xy(3, 2)) rectangle(xy(3, 2), 1, 2).
Side Effects rectangle(xy(0, 1), xy(3, 2)) rectangle(xy(3, 2), 1, 2) Side Effects. Every Python expression has a value Some Python expressions also have a side effect All shape-producing expressions cause a side effect on the CAD tool Side effects can be composed by sequencing them A function that only does side effects returns nothing
Side Effects. P r P r
Side Effects. P r P r def circle_square(p, r, inscribed): if inscribed: creates a circle and a square inscribed in the circle else: creates a circle and a square circumscribed in the circle
Side Effects. P r P r def circle_square(p, r, inscribed): if inscribed: creates a circle creates a square inscribed in the circle else: creates a circle creates a square circumscribed in the circle
Side Effects. P r P r def circle_square(p, r, inscribed): if inscribed: circle(p, r) rectangle(p + vpol(r, 5/4*pi), p + vpol(r, 1/4*pi)) else: circle(p, r) rectangle(p + vxy(-r, -r), p + vxy(r, r))
Side Effects. P r P r def circle_square(p, r, inscribed): circle(p, r) if inscribed: rectangle(p + vpol(r, 5/4*pi), p + vpol(r, 1/4*pi)) else: rectangle(p + vxy(-r, -r), p + vxy(r, r))