httk.core.vectors package¶
Submodules¶
httk.core.vectors.fracmath module¶

httk.core.vectors.fracmath.
any_to_fraction
(arg, min_accuracy=Fraction(1, 10000))[source]¶ min_accuracy: we always assume the accuracy is at least this good. i.e., with min_accuracy=1/10000, we take 0.33 to really mean 0.3300, because we assume people meaning 1/3 at least makes the effort to write 0.3333

httk.core.vectors.fracmath.
frac_acos
(x, degrees=False, prec=Fraction(1, 10000000000), limit=True)[source]¶ Return the arc cosine (measured in radians) of Decimal x.

httk.core.vectors.fracmath.
frac_asin
(x, degrees=False, prec=Fraction(1, 10000000000), limit=True)[source]¶ Return the arc sine (measured in radians) of Decimal x.

httk.core.vectors.fracmath.
frac_atan
(x, degrees=False, prec=Fraction(1, 10000000000), limit=True)[source]¶ Return the arctangent of x in radians.

httk.core.vectors.fracmath.
frac_atan2
(y, x, degrees=False, prec=Fraction(1, 10000000000), limit=True)[source]¶ Return the arctangent of y/x in radians.
Unlike atan(y/x), the signs of both x and y are considered.

httk.core.vectors.fracmath.
frac_cos
(x, prec=Fraction(1, 10000000000), limit=True, degrees=False)[source]¶

httk.core.vectors.fracmath.
frac_exp
(x, prec=Fraction(1, 10000000000), limit=True)[source]¶ Return e raised to the power of x.

httk.core.vectors.fracmath.
frac_log
(x, base=None, prec=Fraction(1, 10000000000), limit=True)[source]¶ Return the logarithm of x to the given base.
If the base not specified, return the natural logarithm (base e) of x.

httk.core.vectors.fracmath.
frac_log10
(x, prec=Fraction(1, 10000000000), limit=True)[source]¶ Return the base 10 logarithm of x.

httk.core.vectors.fracmath.
frac_pi
(prec=Fraction(1, 10000000000), limit=True)[source]¶ Compute Pi to the precision prec.

httk.core.vectors.fracmath.
frac_sin
(x, prec=Fraction(1, 10000000000), limit=True, degrees=False)[source]¶
httk.core.vectors.fracvector module¶

class
httk.core.vectors.fracvector.
FracScalar
(nom, denom)[source]¶ Bases:
httk.core.vectors.fracvector.FracVector
Represents the fractional number nom/denom. This is a subclass of FracVector with the purpose of making it clear when a scalar fracvector is needed/used.

class
httk.core.vectors.fracvector.
FracVector
(noms, denom=1)[source]¶ Bases:
httk.core.vectors.vector.Vector
FracVector is a general immutable Ndimensional vector (tensor) class for performing linear algebra with fractional numbers.
A FracVector consists of a multidimensional tuple of integer nominators, and a single shared integer denominator.
Since FracVectors are immutable, every operation on a FracVector returns a new FracVector with the result of the operation. A created FracVector never changes. Hence, they are safe to use as keys in dictionaries, to use in sets, etc.
Note: most methods returns FracVector results that are not simplified (i.e., the FracVector returned does not have the smallest possible integer denominator). To return a FracVector with the smallest possible denominator, just call FracVector.simplify() at the last step.

acos
(prec=None, degrees=False, limit=False)[source]¶ Return a FracVector where every element is the arccos of the element in the source FracVector.
prec = precision (should be set as a fraction) limit = True requires the denominator to be smaller or equal to precision

asin
(prec=None, degrees=False, limit=False)[source]¶ Return a FracVector where every element is the arcsin of the element in the source FracVector.
prec = precision (should be set as a fraction) limit = True requires the denominator to be smaller or equal to precision

ceil
()[source]¶ Returns the integer that is equal to or just below the value stored in a scalar FracVector.

classmethod
chain_vecs
(vecs)[source]¶ Optimized chaining of FracVectors.
vecs: a list (or tuple) of fracvectors.
 Returns the same thing as
 FracVector.create(vecs,chain=True)
 i.e., removes outermost dimension and chain the subsequences. If input=[[1 2 3],[4,5,6]], then
 FracVector.chain(input) > [1,2,3,4,5,6]
but this method assumes all vectors share the same denominator (it raises an exception if this is not true)

cos
(prec=None, degrees=False, limit=False)[source]¶ Return a FracVector where every element is the cosine of the element in the source FracVector.
prec = precision (should be set as a fraction) limit = True requires the denominator to be smaller or equal to precision

classmethod
create
(noms, denom=None, simplify=True, chain=False, min_accuracy=Fraction(1, 10000))[source]¶ Create a FracVector from various types of sequences.
Simplest use:
FracVector.create(some_kind_of_sequence)
where ‘some_kind_of_sequence’ can be any nested list or tuple of objects that can be used in the constructor of the Python Fraction class (also works with strings!). If any object found while traveling the items has a .to_fractions() method, it will be called and is expected to return a fraction or list or tuple of fractions.
Optional parameters:
 Invocation with denominator: FracVector.create(nominators,denominator) nominators is any sequence, and denominator a common denominator to divide all nominators with
 simplify: boolean, return a FracVector with the smallest possible denominator.
 chain: boolean, remove outermost dimension and chain the subsequences. I.e., if input=[[1 2 3],[4,5,6]], then FracVector.create(input) > [1,2,3,4,5,6]
Relevant: FracVector itself implements .to_fractions(), and hence, the same constructor allows stacking several FracVector objects like this:
vertical_fracvector = FracVector.create([[fracvector1],[fracvector2]]) horizontal_fracvector = FracVector.create([fracvector1,fracvector2],chain=True)
 min_accuracy: set to a boolean to adjust the minimum accuracy assumed in string input. The default is 1/10000, i.e. 0.33 = 0.3300 = 33/100, whereas 0.3333 = 1/3. Set it to None to assume infinite accuracy, i.e., convert exactly whatever string is given (unless a standard deviation is given as a parenthesis after the string.)

classmethod
create_cos
(data, degrees=False, limit=False, find_best_rational=True, prec=Fraction(1, 1000000))[source]¶ Creating a FracVector as the cosine of the argument data. If data are composed by strings, the standard deviation of the numbers are taken into account, and the best possible fractional approximation to the cosines of the data are returned within the standard deviation.
This is not the same as FracVector.create(data).cos(), which creates the best possible fractional approximations of data and then takes cos on that.

classmethod
create_exp
(data, prec=Fraction(1, 1000000), limit=False)[source]¶ Creating a FracVector as the exponent of the argument data. If data are composed by strings, the standard deviation of the numbers are taken into account, and the best possible fractional approximation to the cosines of the data are returned within the standard deviation.
This is not the same as FracVector.create(data).exp(), which creates the best possible fractional approximations of data and then takes exp on that.

classmethod
create_sin
(data, degrees=False, limit=False, prec=Fraction(1, 1000000))[source]¶ Creating a FracVector as the sine of the argument data. If data are composed by strings, the standard deviation of the numbers are taken into account, and the best possible fractional approximation to the cosines of the data are returned within the standard deviation.
This is not the same as FracVector.create(data).sin(), which creates the best possible fractional approximations of data and then takes cos on that.

cross
(other)[source]¶ Returns the vector cross product of the 3element 1D vector with the 3element 1D vector ‘other’, i.e., A x B.

dim
¶ This property returns a tuple with the dimensionality of each dimension of the FracVector (the noms are assumed to be a nested list of rectangular shape).

dot
(other)[source]¶ Returns the vector dot product of the 1D vector with the 1D vector ‘other’, i.e., A . B or A cdot B. The same as A * B.T().

exp
(prec=None, limit=False)[source]¶ Return a FracVector where every element is the exponent of the element in the source FracVector.
prec = precision (should be set as a fraction) limit = True requires the denominator to be smaller or equal to precision

floor
()[source]¶ Returns the integer that is equal to or just below the value stored in a scalar FracVector.

classmethod
from_floats
(l, resolution=4294967296)[source]¶ Create a FracVector from a (nested) list or tuple of floats. You can convert a numpy array with this method if you use A.tolist()
resolution: the resolution used for interpreting the given floating point numbers. Default is 2^32.

classmethod
from_tuple
(t)[source]¶ Return a FracVector created from the tuple representation: (denom, ...noms...), returned by the to_tuple() method.

limit_denominator
(max_denom=1000000000)[source]¶ Returns a FracVector of reduced resolution.
resolution: each element in the returned FracVector is the closest numerical approximation that can is allowed by a fraction with maximally this denominator. Note: since all elements must be put on a common denominator, the result may have a larger denominator than max_denom

max
()[source]¶ Return the maximum element across all dimensions in the FracVector. max(fracvector) works for a 1D vector.

metric_product
(vecA, vecB)[source]¶  Returns the result of the metric product using the present square FracVector as the metric matrix. The same as
 vecA*self*vecB.T().

min
()[source]¶ Return the minimum element across all dimensions in the FracVector. max(fracvector) works for a 1D vector.

mul
(other)[source]¶ Returns the result of multiplying the vector with ‘other’ using matrix multiplication.
Note that for two 1D FracVectors, A.dot(B) is not the same as A.mul(B), but rather: A.mul(B.T()).

nargmax
()[source]¶ Return a list of indices of all maximum elements across all dimensions in the FracVector.

nargmin
()[source]¶ Return a list of indices for all minimum elements across all dimensions in the FracVector.

static
nested_map
(op, *ls)¶ Map an operator over a nested tuple. (i.e., the same as the builtin map(), but works recursively on a nested tuple)

static
nested_map_fractions
(op, *ls)¶ Map an operator over a nested tuple, but checks every element for a method to_fractions() and uses this to further convert objects into tuples of Fraction.

nom
¶ Returns the integer nominator of a scalar FracVector.

normalize_half
()[source]¶ Add/remove an integer +/N to each element to place it in the range [1/2,1/2)
 This is useful to find the shortest vector C between two points A, B in a space with periodic boundary conditions [0,1):
 C = (AB).normalize_half()

classmethod
pi
(prec=Fraction(1, 1000000), limit=False)[source]¶ Create a scalar FracVector with a rational approximation of pi to precision prec.

classmethod
random
(dims, minnom=100, maxnom=100, denom=100)[source]¶ Create a zero matrix with the given dimensions

classmethod
set_common_denom
(A, B)[source]¶ Used internally to combine two different FracVectors.
Returns a tuple (A2,B2,denom) where A2 is numerically equal to A, and B2 is numerically equal to B, but A2 and B2 are both set on the same shared denominator ‘denom’ which is the product of the denominator of A and B.

set_denominator
(set_denom=1000000000)[source]¶ Returns a FracVector of reduced resolution where every element is the closest numerical approximation using this denominator.

simplify
()[source]¶ Returns a reduced FracVector. I.e., each element has the same numerical value but the new FracVector represents them using the smallest possible shared denominator.

sin
(prec=None, degrees=False, limit=False)[source]¶ Return a FracVector where every element is the sine of the element in the source FracVector.
prec = precision (should be set as a fraction) limit = True requires the denominator to be smaller or equal to precision

sqrt
(prec=None, limit=False)[source]¶ Return a FracVector where every element is the sqrt of the element in the source FracVector.
prec = precision (should be set as a fraction) limit = True requires the denominator to be smaller or equal to precision


httk.core.vectors.fracvector.
nested_map_fractions_list
(op, *ls)[source]¶ Map an operator over a nested list, but checks every element for a method to_fractions() and uses this to further convert objects into lists of Fraction.

httk.core.vectors.fracvector.
nested_map_fractions_tuple
(op, *ls)[source]¶ Map an operator over a nested tuple, but checks every element for a method to_fractions() and uses this to further convert objects into tuples of Fraction.

httk.core.vectors.fracvector.
nested_map_list
(op, *ls)[source]¶ Map an operator over a nested list. (i.e., the same as the builtin map(), but works recursively on a nested list)

httk.core.vectors.fracvector.
nested_map_tuple
(op, *ls)[source]¶ Map an operator over a nested tuple. (i.e., the same as the builtin map(), but works recursively on a nested tuple)

httk.core.vectors.fracvector.
nested_reduce
(op, l, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence.

httk.core.vectors.fracvector.
nested_reduce_fractions
(op, l, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence. Also checks every element for a method to_fractions() and uses this to further convert such elements to lists of fractions.

httk.core.vectors.fracvector.
nested_reduce_levels
(op, l, level=1, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence.

httk.core.vectors.fracvector.
tuple_eye
(dims, onepos=0)[source]¶ Create a matrix with the given dimensions and 1 on the diagonal

httk.core.vectors.fracvector.
tuple_index
(dims, uppidx=())[source]¶ Create a nested tuple where every element is a tuple indicating the position of that tuple

httk.core.vectors.fracvector.
tuple_random
(dims, minval, maxval)[source]¶ Create a nested tuple with the given dimensions filled with random numbers between minval and maxval
httk.core.vectors.mutablefracvector module¶

class
httk.core.vectors.mutablefracvector.
MutableFracVector
(noms, denom)[source]¶ Bases:
httk.core.vectors.fracvector.FracVector
,httk.core.vectors.vector.MutableVector
Same as FracVector, only, this version allow assignment of elements, e.g.,
mfracvec[2,7] = 5
and, e.g.,
mfracvec[:,7] = [1,2,3,4]
Other than this, the FracVector methods exist and do the same, i.e., they return copies of the fracvector, rather than modifying it.
However, methods have also been added named with set_* prefixes which performs mutating operations, e.g.,
A.set_T()
replaces A with its own transpose, whereas
A.T()
just returns a new MutableFracVector that is the transpose of A, leaving A unmodified.

invalidate
()[source]¶ Internal method to call when MutableFracVector is changed in such a way that cached properties are invalidated (e.g., _dim)

static
nested_inmap
(op, *ls)¶ Like inmap, but work for nested lists

static
nested_map
(op, *ls)¶ Map an operator over a nested list. (i.e., the same as the builtin map(), but works recursively on a nested list)

static
nested_map_fractions
(op, *ls)¶ Map an operator over a nested list, but checks every element for a method to_fractions() and uses this to further convert objects into lists of Fraction.

set_normalize_half
()[source]¶ Add/remove an integer +/N to each element to place it in the range [1/2,1/2)
 This is useful to find the shortest vector C between two points A, B in a space with periodic boundary conditions [0,1):
 C = (AB).normalize_half()

set_set_denominator
(resolution=1000000000)[source]¶ Changes MutableFracVector; reduces resolution.
resolution is the new denominator, each element becomes the closest numerical approximation using this denominator.


httk.core.vectors.mutablefracvector.
inmap
(f, x)[source]¶ Like builtin map, but work on a list and replace the elements in the list with the result of the mapping.

httk.core.vectors.mutablefracvector.
list_set_slice
(l, key, values)[source]¶  Given:
 l = list, key = python slice (i.e., what you get to __setitem__ when you write A[3:2]=[2,5]) values = a list of values,
change the elements specified by the slice in key to those given by values.
httk.core.vectors.vector module¶

class
httk.core.vectors.vector.
Scalar
[source]¶ Bases:
httk.core.vectors.vector.Vector
Baseclass for scalars

class
httk.core.vectors.vector.
Vector
[source]¶ Bases:
object
Defines the general Vector API

classmethod
chain_vecs
(vecs)[source]¶ Optimized chaining of Vectors.
vecs: a list (or tuple) of vectors.
 Returns the same thing as
 Vector.create(vecs, chain=True)
 i.e., removes outermost dimension and chain the subsequences. If input=[[1 2 3],[4,5,6]], then
 Vector.chain(input) > [1,2,3,4,5,6]
Subclasses may add requirements on the vectors to use this method over <subclass>.create

classmethod
create
(data, chain=False)[source]¶ Create a Vector from various types of sequenced data.
Will return a suitable Vector subclass for the type of data given

classmethod
random
(dims, minval=100, maxval=100)[source]¶ Create a zero matrix with the given dimensions

classmethod

httk.core.vectors.vector.
nested_map_fractions_list
(op, *ls)[source]¶ Map an operator over a nested list, but checks every element for a method to_fractions() and uses this to further convert objects into lists of Fraction.

httk.core.vectors.vector.
nested_map_list
(op, *ls)[source]¶ Map an operator over a nested list. (i.e., the same as the builtin map(), but works recursively on a nested list)

httk.core.vectors.vector.
nested_reduce
(op, l, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence.

httk.core.vectors.vector.
nested_reduce_fractions
(op, l, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence. Also checks every element for a method to_fractions() and uses this to further convert such elements to lists of fractions.

httk.core.vectors.vector.
nested_reduce_levels
(op, l, level=1, initializer=None)[source]¶ Same as builtin reduce, but operates on a nested tuple/list/sequence.

httk.core.vectors.vector.
tuple_eye
(dims, onepos=0)[source]¶ Create a matrix with the given dimensions and 1 on the diagonal

httk.core.vectors.vector.
tuple_index
(dims, uppidx=())[source]¶ Create a nested tuple where every element is a tuple indicating the position of that tuple

httk.core.vectors.vector.
tuple_random
(dims, minval, maxval)[source]¶ Create a nested tuple with the given dimensions filled with random numbers between minval and maxval
httk.core.vectors.vectormath module¶

httk.core.vectors.vectormath.
acos
(x, **args)[source]¶ Return the arc cosine of x, in radians.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
acosh
(x, **args)[source]¶ Return the inverse hyperbolic cosine of x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
asin
(x, **args)[source]¶ Return the arc sine of x, in radians.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
asinh
(x, **args)[source]¶ Return the inverse hyperbolic sine of x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
atan
(x, **args)[source]¶ Return the arc tangent of x, in radians.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
atan2
(x, y, **args)[source]¶ Return atan(y / x), in radians. The result is between pi and pi. The vector in the plane from the origin to point (x, y) makes this angle with the positive X axis. The point of atan2() is that the signs of both inputs are known to it, so it can compute the correct quadrant for the angle. For example, atan(1) and atan2(1, 1) are both pi/4, but atan2(1, 1) is 3*pi/4.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
atanh
(x, **args)[source]¶ Return the inverse hyperbolic tangent of x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
ceil
(x, **args)[source]¶ Return the ceiling of x, the smallest integer value greater than or equal to x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
copysign
(x, y, **args)[source]¶ Return x with the sign of y. If an element of y is zero, abs of the corresponding element in x is returned.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
cos
(x, **args)[source]¶ Return the cosine of x radians.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
cosh
(x, **args)[source]¶ Return the hyperbolic cosine of x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
degrees
(x, **args)[source]¶ Convert angle x from radians to degrees.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
e
(x, **args)[source]¶ Return the value of e represented using the same scalar or vector representation as x.

httk.core.vectors.vectormath.
erf
(x, **args)[source]¶ Return the error function at x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
erfc
(x, **args)[source]¶ Return the complementary error function at x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
exp
(x, **args)[source]¶ Return e**x. (For vectors applied to each element.)

httk.core.vectors.vectormath.
expm1
(x, **args)[source]¶ Return e**x  1. (For vectors applied to each element.)

httk.core.vectors.vectormath.
fabs
(x, **args)[source]¶ Return the absolute value of x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
factorial
(x, **args)[source]¶ Return x factorial. Raises ValueError if (any element of) x is negative.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
floor
(x, **args)[source]¶ Return the floor of x, the largest integer value less than or equal to x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
frexp
(x, **args)[source]¶ Return the mantissa and exponent of x as the pair (m, e). m is a float and e is an integer such that x == m * 2**e exactly. If x is zero, returns (0.0, 0), otherwise 0.5 <= abs(m) < 1.
(For vectors applied to each element and returns tuples nested in lists.)

httk.core.vectors.vectormath.
gamma
(x, **args)[source]¶ Return the Gamma function at x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
hypot
(x, y, **args)[source]¶ Return the Euclidean norm, sqrt(x*x + y*y). This is the length of the vector from the origin to point (x, y).
(For vectors applied to each element.)

httk.core.vectors.vectormath.
isanyinf
(x, **args)[source]¶ Check if the float x is positive or negative infinity.
(For vectors returns True/False if any element is inf)

httk.core.vectors.vectormath.
isanynan
(x, **args)[source]¶ Check if the float x is a NaN (not a number).
(For vectors returns True/False if any element is NaN)

httk.core.vectors.vectormath.
isinf
(x, **args)[source]¶ Check if the float x is positive or negative infinity.
(For vectors applied to each element and returns True/False as nested lists.)

httk.core.vectors.vectormath.
isnan
(x, **args)[source]¶ Check if the float x is a NaN (not a number).
(For vectors applied to each element and returns True/False as nested lists.)

httk.core.vectors.vectormath.
ldexp
(x, **args)[source]¶ Return x * (2**i). This is essentially the inverse of function frexp().
(For vectors applied to each element.)

httk.core.vectors.vectormath.
lgamma
(x, **args)[source]¶ Return the natural logarithm of the absolute value of the Gamma function at x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
log
(x, base=None, **args)[source]¶ With one argument, return the natural logarithm of x (to base e).
With two arguments, return the logarithm of x to the given base, calculated as log(x)/log(base).
(For vectors applied to each element.)

httk.core.vectors.vectormath.
log10
(x, **args)[source]¶ Return the base10 logarithm of x. This is usually more accurate than log(x, 10).
(For vectors applied to each element.)

httk.core.vectors.vectormath.
log1p
(x, **args)[source]¶ Return the natural logarithm of 1+x (base e). The result is calculated in a way which is accurate for x near zero.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
modf
(x, **args)[source]¶ Return the fractional and integer parts of x. Both results carry the sign of x.
(For vectors applied to each element and returns tuples nested in lists.)

httk.core.vectors.vectormath.
pi
(x, **args)[source]¶ Return the value of pi represented using the same scalar or vector representation as x.

httk.core.vectors.vectormath.
pow
(x, y, **args)[source]¶ Return x raised to the power y. Equivalent with x**y
(For vectors applied to each element.)

httk.core.vectors.vectormath.
radians
(x, **args)[source]¶ Convert angle x from degrees to radians.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
sign
(x, **args)[source]¶ Return the sign of x, equivalent to copysign(1,x).
(For vectors applied to each element.)

httk.core.vectors.vectormath.
sin
(x, **args)[source]¶ Return the sine of x radians.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
sinh
(x, **args)[source]¶ Return the hyperbolic sine of x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
sqrt
(x, **args)[source]¶ Return the square root of x.
(For vectors applied to each element.)

httk.core.vectors.vectormath.
tan
(x, **args)[source]¶ Return the tangent of x radians.
(For vectors applied to each element.)