update from Atlas with major reorg

This commit is contained in:
Luciano Ramalho
2015-04-17 21:29:30 -03:00
parent 57902d31b5
commit a786180239
134 changed files with 369 additions and 520 deletions

View File

@@ -0,0 +1,48 @@
# Test data for spherical coordinates computed by Vector.angles()
#
# π π/2 π/3 π/4
# 3.141592653590 1.570796326795 1.047197551197 0.785398163397
#
# azimuth
# x y θ
# x1 x2 r Φ1
1 1 1.414213562373 0.785398163397
1 0 1.000000000000 0.000000000000
0 1 1.000000000000 1.570796326795
0 0 0.000000000000 0.000000000000
1 -1 1.414213562373 5.497787143782
-1 1 1.414213562373 2.356194490192
0 -1 1.000000000000 4.712388980385
-1 -1 1.414213562373 3.926990816987
#
# x y z θ Φ
# x1 x2 x3 r Φ1 Φ2
1 1 1 1.732050807569 0.955316618125 0.785398163397
2 2 2 3.464101615138 0.955316618125 0.785398163397
0 0 0 0.000000000000 0.000000000000 0.000000000000
1 0 0 1.000000000000 0.000000000000 0.000000000000
0 1 0 1.000000000000 1.570796326795 0.000000000000
0 0 1 1.000000000000 1.570796326795 1.570796326795
1 1 0 1.414213562373 0.785398163397 0.000000000000
1 0 1 1.414213562373 0.785398163397 1.570796326795
0 1 1 1.414213562373 1.570796326795 0.785398163397
1 1 -1 1.732050807569 0.955316618125 5.497787143782
#
# x y z t θ Φ
# x1 x2 x3 x4 r Φ1 Φ2 Φ3
1 1 1 0 1.732050807569 0.955316618125 0.785398163397 0.000000000000
2 2 2 0 3.464101615138 0.955316618125 0.785398163397 0.000000000000
1 1 1 1 2.000000000000 1.047197551197 0.955316618125 0.785398163397
2 2 2 2 4.000000000000 1.047197551197 0.955316618125 0.785398163397
1 0 0 0 1.000000000000 0.000000000000 0.000000000000 0.000000000000
0 1 0 0 1.000000000000 1.570796326795 0.000000000000 0.000000000000
0 0 1 0 1.000000000000 1.570796326795 1.570796326795 0.000000000000
0 0 0 1 1.000000000000 1.570796326795 1.570796326795 1.570796326795
1 1 0 0 1.414213562373 0.785398163397 0.000000000000 0.000000000000
0 1 1 0 1.414213562373 1.570796326795 0.785398163397 0.000000000000
0 0 1 1 1.414213562373 1.570796326795 1.570796326795 0.785398163397
1 0 0 1 1.414213562373 0.785398163397 1.570796326795 1.570796326795
1 0 1 0 1.414213562373 0.785398163397 1.570796326795 0.000000000000
0 1 0 1 1.414213562373 1.570796326795 0.785398163397 1.570796326795
1 1 1 -1 2.000000000000 1.047197551197 0.955316618125 5.497787143782
-1 -1 -1 -1 2.000000000000 2.094395102393 2.186276035465 3.926990816987

View File

@@ -0,0 +1,85 @@
======================================
Pythonic way to sum n-th list element?
======================================
Examples inspired by Guy Middleton's question on Python-list, Fri Apr 18 22:21:08 CEST 2003. Message: https://mail.python.org/pipermail/python-list/2003-April/218568.html
Guy Middleton::
>>> my_list = [[1, 2, 3], [40, 50, 60], [9, 8, 7]]
>>> import functools as ft
>>> ft.reduce(lambda a, b: a+b, [sub[1] for sub in my_list])
60
LR::
>>> ft.reduce(lambda a, b: a + b[1], my_list, 0)
60
Fernando Perez::
>>> import numpy as np
>>> my_array = np.array(my_list)
>>> np.sum(my_array[:, 1])
60
Skip Montanaro::
>>> import operator
>>> ft.reduce(operator.add, [sub[1] for sub in my_list], 0)
60
>>> ft.reduce(operator.add, [sub[1] for sub in []])
Traceback (most recent call last):
...
TypeError: reduce() of empty sequence with no initial value
>>> ft.reduce(operator.add, [sub[1] for sub in []], 0)
0
Evan Simpson::
>>> total = 0
>>> for sub in my_list:
... total += sub[1]
>>> total
60
Alex Martelli (``sum`` was added in Python 2.3, released July 9, 2003)::
>>> sum([sub[1] for sub in my_list])
60
After generator expressions (added in Python 2.4, November 30, 2004)::
>>> sum(sub[1] for sub in my_list)
60
If you want the sum of a list of items, you should write it in a way
that looks like "the sum of a list of items", not in a way that looks
like "loop over these items, maintain another variable t, perform a
sequence of additions". Why do we have high level languages if not to
express our intentions at a higher level and let the language worry
about what low-level operations are needed to implement it?
David Eppstein
Alex Martelli
https://mail.python.org/pipermail/python-list/2003-April/186311.html
"The sum" is so frequently needed that I wouldn't mind at all if
Python singled it out as a built-in. But "reduce(operator.add, ..."
just isn't a great way to express it, in my opinion (and yet as an
old APL'er, and FP-liker, I _should_ like it -- but I don't).
https://mail.python.org/pipermail/python-list/2003-April/225323.html
Four years later, having coded a lot of Python, taught it widely,
written a lot about it, and so on, I've changed my mind: I now
think that reduce is more trouble than it's worth and Python
would be better off without it, if it was being designed from
scratch today -- it would not substantially reduce (:-) Python's
power and WOULD substantially ease the teaching/&c task. That's
not a strong-enough argument to REMOVE a builtin, of course, and
thus that's definitely NOT what I'm arguing for. But I do suggest
avoiding reduce in most cases -- that's all.

View File

@@ -0,0 +1,37 @@
"""
Test spherical coordinates in ``Vector`` class
"""
import sys
from vector_v5 import Vector
FIXTURE = 'spherical-coordinates.txt'
EPSILON = 10**-8
def parse_float_cells(cells):
floats = []
for cell in cells:
try:
floats.append(float(cell))
except ValueError:
continue
return floats
def load_fixture(verbose=False):
with open(FIXTURE, encoding='utf8') as text:
for line in text:
if line.startswith('#'): # comment line
continue
cells = line.split('\t')
cartesian = parse_float_cells(cells[:5])
spherical = parse_float_cells(cells[5:])
v = Vector(cartesian)
if verbose:
print(repr(v), '\t->', spherical)
diff = abs(abs(v) - spherical[0])
assert diff < EPSILON, 'expected {}, got {}'.format(spherical[0], abs(v))
assert all(abs(av - af) < EPSILON for av, af in zip(v.angles(), spherical[1:])), (
'expected {}, got {}'.format(spherical[1:], list(v.angles())))
if __name__=='__main__':
load_fixture('-v' in sys.argv)