Commonly used mathematical functions.
yt.utilities.math_utils.
compute_cylindrical_radius
(CoM, L, P, V)[source]¶Compute the radius for some data around an axis in cylindrical coordinates.
This is primarily for halo computations. Given some data, this computes the cylindrical radius for each point. This is accomplished by converting the reference frame of the center of mass of the halo.
Parameters: 


Returns:  cyl_r – An array N elements long that gives the radial velocity for each datum (particle). 
Return type: 
Examples
>>> CoM = np.array([0, 0, 0])
>>> L = np.array([0, 0, 1])
>>> P = np.array([[1, 0, 0], [1, 1, 1], [0, 0, 1], [1, 1, 0]])
>>> V = np.array([[0, 1, 10], [1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> cyl_r = compute_cylindrical_radius(CoM, L, P, V)
>>> cyl_r
array([ 1. , 1.41421356, 0. , 1.41421356])
yt.utilities.math_utils.
compute_parallel_velocity
(CoM, L, P, V)[source]¶Computes the parallel velocity for some data around an axis.
This is primarily for halo computations. Given some data, this computes the velocity component along the angular momentum vector. This is accomplished by converting the reference frame of the center of mass of the halo.
Parameters: 


Returns:  v – An array N elements long that gives the parallel velocity for each datum (particle). 
Return type: 
Examples
>>> CoM = np.array([0, 0, 0])
>>> L = np.array([0, 0, 1])
>>> P = np.array([[1, 0, 0], [1, 1, 1], [0, 0, 1], [1, 1, 0]])
>>> V = np.array([[0, 1, 10], [1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> paraV = compute_parallel_velocity(CoM, L, P, V)
>>> paraV
array([10, 1, 1, 1])
yt.utilities.math_utils.
compute_radial_velocity
(CoM, L, P, V)[source]¶Computes the radial velocity for some data around an axis.
This is primarily for halo computations. Given some data, this computes the radial velocity component for the data. This is accomplished by converting the reference frame of the center of mass of the halo.
Parameters: 


Returns:  v – An array N elements long that gives the radial velocity for each datum (particle). 
Return type: 
Examples
>>> CoM = np.array([0, 0, 0])
>>> L = np.array([0, 0, 1])
>>> P = np.array([[1, 0, 0], [1, 1, 1], [0, 0, 1], [1, 1, 0]])
>>> V = np.array([[0, 1, 10], [1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> radV = compute_radial_velocity(CoM, L, P, V)
>>> radV
array([ 1. , 1.41421356 , 0. , 0.])
yt.utilities.math_utils.
compute_rotational_velocity
(CoM, L, P, V)[source]¶Computes the rotational velocity for some data around an axis.
This is primarily for halo computations. Given some data, this computes the circular rotational velocity of each point (particle) in reference to the axis defined by the angular momentum vector. This is accomplished by converting the reference frame of the center of mass of the halo.
Parameters: 


Returns:  v – An array N elements long that gives the circular rotational velocity for each datum (particle). 
Return type: 
Examples
>>> CoM = np.array([0, 0, 0])
>>> L = np.array([0, 0, 1])
>>> P = np.array([[1, 0, 0], [1, 1, 1], [0, 0, 1], [1, 1, 0]])
>>> V = np.array([[0, 1, 10], [1, 1, 1], [1, 1, 1], [1, 1, 1]])
>>> circV = compute_rotational_velocity(CoM, L, P, V)
>>> circV
array([ 1. , 0. , 0. , 1.41421356])
yt.utilities.math_utils.
euclidean_dist
(a, b)[source]¶Find the Euclidean distance between two points.
Parameters: 


Examples
>>> a = [0.1, 0.1, 0.1]
>>> b = [0.9, 0,9, 0.9]
>>> period = 1.
>>> dist = euclidean_dist(a, b)
>>> dist
1.38564064606
yt.utilities.math_utils.
get_lookat_matrix
(eye, center, up)[source]¶Given the position of a camera, the point it is looking at, and an updirection. Computes the lookat matrix that moves all vectors such that the camera is at the origin of the coordinate system, looking down the zaxis.
Parameters: 


Returns:  lookat_matrix – A new 4x4 2D array in homogeneous coordinates. This matrix moves all vectors in the same way required to move the camera to the origin of the coordinate system, with it pointing down the negative zaxis. 
Return type:  ndarray 
yt.utilities.math_utils.
get_orthographic_matrix
(maxr, aspect, z_near, z_far)[source]¶Given a field of view in radians, an aspect ratio, and a near and far plane distance, this routine computes the transformation matrix corresponding to perspective projection using homogenous coordinates.
Parameters: 


Returns:  persp_matrix – A new 4x4 2D array. Represents a perspective transformation in homogeneous coordinates. Note that this matrix does not actually perform the projection. After multiplying a 4D vector of the form (x_0, y_0, z_0, 1.0), the point will be transformed to some (x_1, y_1, z_1, w). The final projection is applied by performing a divide by w, that is (x_1/w, y_1/w, z_1/w, w/w). The matrix uses a rowmajor ordering, rather than the column major ordering typically used by OpenGL. 
Return type:  ndarray 
Notes
The usage of 4D homogeneous coordinates is for OpenGL and GPU hardware that automatically performs the divide by w operation. See the following for more details about the OpenGL perspective matrices.
http://www.scratchapixel.com/lessons/3dbasicrendering/perspectiveandorthographicprojectionmatrix/orthographicprojectionmatrix http://www.tomdalling.com/blog/modernopengl/explaininghomogenouscoordinatesandprojectivegeometry/ http://www.songho.ca/opengl/gl_projectionmatrix.html
yt.utilities.math_utils.
get_perspective_matrix
(fovy, aspect, z_near, z_far)[source]¶Given a field of view in radians, an aspect ratio, and a near and far plane distance, this routine computes the transformation matrix corresponding to perspective projection using homogenous coordinates.
Parameters: 


Returns:  persp_matrix – A new 4x4 2D array. Represents a perspective transformation in homogeneous coordinates. Note that this matrix does not actually perform the projection. After multiplying a 4D vector of the form (x_0, y_0, z_0, 1.0), the point will be transformed to some (x_1, y_1, z_1, w). The final projection is applied by performing a divide by w, that is (x_1/w, y_1/w, z_1/w, w/w). The matrix uses a rowmajor ordering, rather than the column major ordering typically used by OpenGL. 
Return type:  ndarray 
Notes
The usage of 4D homogeneous coordinates is for OpenGL and GPU hardware that automatically performs the divide by w operation. See the following for more details about the OpenGL perspective matrices.
http://www.tomdalling.com/blog/modernopengl/explaininghomogenouscoordinatesandprojectivegeometry/ http://www.songho.ca/opengl/gl_projectionmatrix.html
yt.utilities.math_utils.
get_rotation_matrix
(theta, rot_vector)[source]¶Given an angle theta and a 3D vector rot_vector, this routine computes the rotation matrix corresponding to rotating theta radians about rot_vector.
Parameters: 


Returns:  rot_matrix – A new 3x3 2D array. This is the representation of a rotation of theta radians about rot_vector in the simulation box coordinate frame 
Return type:  ndarray 
See also
Examples
>>> a = [0,1,0]
>>> theta = 0.785398163 # pi/4
>>> rot = mu.get_rotation_matrix(theta,a)
>>> rot
array([[ 0.70710678, 0. , 0.70710678],
[ 0. , 1. , 0. ],
[0.70710678, 0. , 0.70710678]])
>>> np.dot(rot,a)
array([ 0., 1., 0.])
# since a is an eigenvector by construction
>>> np.dot(rot,[1,0,0])
array([ 0.70710678, 0. , 0.70710678])
yt.utilities.math_utils.
get_scale_matrix
(dx, dy, dz)[source]¶Given a scaling factor for each coordinate, returns a matrix that corresponds to the given scaling amounts.
Parameters: 


Returns:  scale_matrix – A new 4x4 2D array. Represents a scaling by dx, dy, and dz in each coordinate respectively. 
Return type:  ndarray 
yt.utilities.math_utils.
get_translate_matrix
(dx, dy, dz)[source]¶Given a movement amount for each coordinate, creates a translation matrix that moves the vector by each amount.
Parameters: 


Returns:  trans_matrix – A new 4x4 2D array. Represents a translation by dx, dy and dz in each coordinate respectively. 
Return type:  ndarray 
yt.utilities.math_utils.
modify_reference_frame
(CoM, L, P=None, V=None)[source]¶Rotates and translates data into a new reference frame to make calculations easier.
This is primarily useful for calculations of halo data. The data is translated into the center of mass frame. Next, it is rotated such that the angular momentum vector for the data is aligned with the zaxis. Put another way, if one calculates the angular momentum vector on the data that comes out of this function, it will always be along the positive zaxis. If the center of mass is recalculated, it will be at the origin.
Parameters: 


Returns: 

Examples
>>> CoM = np.array([0.5, 0.5, 0.5])
>>> L = np.array([1, 0, 0])
>>> P = np.array([[1, 0.5, 0.5], [0, 0.5, 0.5], [0.5, 0.5, 0.5], [0, 0, 0]])
>>> V = p.copy()
>>> LL, PP, VV = modify_reference_frame(CoM, L, P, V)
>>> LL
array([ 6.12323400e17, 0.00000000e+00, 1.00000000e+00])
>>> PP
array([[ 3.06161700e17, 0.00000000e+00, 5.00000000e01],
[ 3.06161700e17, 0.00000000e+00, 5.00000000e01],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 5.00000000e01, 5.00000000e01, 5.00000000e01]])
>>> VV
array([[ 5.00000000e01, 5.00000000e01, 1.00000000e+00],
[ 5.00000000e01, 5.00000000e01, 3.06161700e17],
[ 5.00000000e01, 5.00000000e01, 5.00000000e01],
[ 0.00000000e+00, 0.00000000e+00, 0.00000000e+00]])
yt.utilities.math_utils.
ortho_find
(vec1)[source]¶Find two complementary orthonormal vectors to a given vector.
For any given nonzero vector, there are infinite pairs of vectors orthonormal to it. This function gives you one arbitrary pair from that set along with the normalized version of the original vector.
Parameters:  vec1 (array_like) – An array or list to represent a 3vector. 

Returns: 

Raises:  ValueError – If input vector is the zero vector. 
Notes
Our initial vector is vec1 which consists of 3 components: x1, y1, and z1. ortho_find determines a vector, vec2, which is orthonormal to vec1 by finding a vector which has a zerovalue dotproduct with vec1.
As a starting point, we arbitrarily choose vec2 to have x2 = 1, y2 = 0:
Of course, this will fail if z1 = 0, in which case, let’s say use z2 = 1 and x2 = 0:
Similarly, if y1 = 0, this case will fail, in which case we use y2 = 1 and z2 = 0:
Since we don’t allow vec1 to be zero, all cases are accounted for.
Producing vec3, the complementary orthonormal vector to vec1 and vec2 is accomplished by simply taking the cross product of vec1 and vec2.
Examples
>>> a = [1.0, 2.0, 3.0]
>>> a, b, c = ortho_find(a)
>>> a
array([ 0.26726124, 0.53452248, 0.80178373])
>>> b
array([ 0.9486833 , 0. , 0.31622777])
>>> c
array([0.16903085, 0.84515425, 0.50709255])
yt.utilities.math_utils.
periodic_dist
(a, b, period, periodicity=(True, True, True))[source]¶Find the Euclidean periodic distance between two sets of points.
Parameters: 


Examples
>>> a = [0.1, 0.1, 0.1]
>>> b = [0.9, 0,9, 0.9]
>>> period = 1.
>>> dist = periodic_dist(a, b, 1.)
>>> dist
0.346410161514
yt.utilities.math_utils.
periodic_position
(pos, ds)[source]¶Assuming periodicity, find the periodic position within the domain.
Parameters: 

Examples
>>> a = np.array([1.1, 0.5, 0.5])
>>> data = {'Density':np.ones([32,32,32])}
>>> ds = load_uniform_grid(data, [32,32,32], 1.0)
>>> ppos = periodic_position(a, ds)
>>> ppos
array([ 0.1, 0.5, 0.5])
yt.utilities.math_utils.
quartiles
(a, axis=None, out=None, overwrite_input=False)[source]¶Compute the quartile values (25% and 75%) along the specified axis in the same way that the numpy.median calculates the median (50%) value alone a specified axis. Check numpy.median for details, as it is virtually the same algorithm.
Returns an array of the quartiles of the array elements [lower quartile, upper quartile].
Parameters: 


Returns:  quartiles – A new 2D array holding the result (unless out is specified, in which case that array is returned instead). If the input contains integers, or floats of smaller precision than 64, then the output datatype is float64. Otherwise, the output datatype is the same as that of the input. 
Return type:  ndarray 
See also
Notes
Given a vector V of length N, the quartiles of V are the 25% and 75% values
of a sorted copy of V, V_sorted
 i.e., V_sorted[(N1)/4]
and
3*V_sorted[(N1)/4]
, when N is odd. When N is even, it is the average
of the two values bounding these values of V_sorted
.
Examples
>>> a = np.arange(100).reshape(10,10)
>>> a
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, 74, 75, 76, 77, 78, 79],
[80, 81, 82, 83, 84, 85, 86, 87, 88, 89],
[90, 91, 92, 93, 94, 95, 96, 97, 98, 99]])
>>> mu.quartiles(a)
array([ 24.5, 74.5])
>>> mu.quartiles(a,axis=0)
array([[ 15., 16., 17., 18., 19., 20., 21., 22., 23., 24.],
[ 65., 66., 67., 68., 69., 70., 71., 72., 73., 74.]])
>>> mu.quartiles(a,axis=1)
array([[ 1.5, 11.5, 21.5, 31.5, 41.5, 51.5, 61.5, 71.5, 81.5,
91.5],
[ 6.5, 16.5, 26.5, 36.5, 46.5, 56.5, 66.5, 76.5, 86.5,
96.5]])
yt.utilities.math_utils.
quaternion_mult
(q1, q2)[source]¶Multiply two quaternions. The inputs are 4component numpy arrays in the order [w, x, y, z].
yt.utilities.math_utils.
quaternion_to_rotation_matrix
(quaternion)[source]¶This converts a quaternion representation of on orientation to a rotation matrix. The input is a 4component numpy array in the order [w, x, y, z], and the output is a 3x3 matrix stored as a 2D numpy array. We follow the approach in “3D Math Primer for Graphics and Game Development” by Dunn and Parberry.
yt.utilities.math_utils.
rotate_vector_3D
(a, dim, angle)[source]¶Rotates the elements of an array around an axis by some angle.
Given an array of 3D vectors a, this rotates them around a coordinate axis by a clockwise angle. An alternative way to think about it is the coordinate axes are rotated counterclockwise, which changes the directions of the vectors accordingly.
Parameters: 

Examples
>>> a = np.array([[1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 1, 1], [3, 4, 5]])
>>> b = rotate_vector_3D(a, 2, np.pi/2)
>>> print b
[[ 1.00000000e+00 1.00000000e+00 0.00000000e+00]
[ 6.12323400e17 1.00000000e+00 1.00000000e+00]
[ 1.00000000e+00 6.12323400e17 1.00000000e+00]
[ 1.00000000e+00 1.00000000e+00 1.00000000e+00]
[ 4.00000000e+00 3.00000000e+00 5.00000000e+00]]
yt.utilities.math_utils.
rotation_matrix_to_quaternion
(rot_matrix)[source]¶Convert a rotation matrixbased representation of an orientation to a quaternion. The input should be a 3x3 rotation matrix, while the output will be a 4component numpy array. We follow the approach in “3D Math Primer for Graphics and Game Development” by Dunn and Parberry.