本文整理汇总了Python中numpy.arccos方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.arccos方法的具体用法?Python numpy.arccos怎么用?Python numpy.arccos使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.arccos方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: vector_angle
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def vector_angle(u, v, direction=None):
'''
vector_angle(u, v) yields the angle between the two vectors u and v. The optional argument
direction is by default None, which specifies that the smallest possible angle between the
vectors be reported; if the vectors u and v are 2D vectors and direction parameters True and
False specify the clockwise or counter-clockwise directions, respectively; if the vectors are
3D vectors, then direction may be a 3D point that is not in the plane containing u, v, and the
origin, and it specifies around which direction (u x v or v x u) the the counter-clockwise angle
from u to v should be reported (the cross product vector that has a positive dot product with
the direction argument is used as the rotation axis).
'''
if direction is None:
return np.arccos(vector_angle_cos(u, v))
elif direction is True:
return np.arctan2(v[1], v[0]) - np.arctan2(u[1], u[0])
elif direction is False:
return np.arctan2(u[1], u[0]) - np.arctan2(v[1], v[0])
else:
axis1 = normalize(u)
axis2 = normalize(np.cross(u, v))
if np.dot(axis2, direction) < 0:
axis2 = -axis2
return np.arctan2(np.dot(axis2, v), np.dot(axis1, v))
示例2: arccosine
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def arccosine(x, null=(-np.inf, np.inf)):
'''
arccosine(x) is equivalent to acos(x) except that it also works on sparse arrays.
The optional argument null (default, (-numpy.inf, numpy.inf)) may be specified to indicate what
value(s) should be assigned when x < -1 or x > 1. If only one number is given, then it is used
for both values; otherwise the first value corresponds to <-1 and the second to >1. If null is
None, then an error is raised when invalid values are encountered.
'''
if sps.issparse(x): x = x.toarray()
else: x = np.asarray(x)
try: (nln,nlp) = null
except Exception: (nln,nlp) = (null,null)
ii = None if nln is None else np.where(x < -1)
jj = None if nlp is None else np.where(x > 1)
if ii: x[ii] = 0
if jj: x[jj] = 0
x = np.arccos(x)
if ii: x[ii] = nln
if jj: x[jj] = nlp
return x
示例3: rotate_camera_to_point_at
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def rotate_camera_to_point_at(up_from, lookat_from, up_to, lookat_to):
inputs = [up_from, lookat_from, up_to, lookat_to]
for i in range(4):
inputs[i] = normalize(np.array(inputs[i]).reshape((-1,)))
up_from, lookat_from, up_to, lookat_to = inputs
r1 = r_between(lookat_from, lookat_to)
new_x = np.dot(r1, np.array([1, 0, 0]).reshape((-1, 1))).reshape((-1))
to_x = normalize(np.cross(lookat_to, up_to))
angle = np.arccos(np.dot(new_x, to_x))
if angle > ANGLE_EPS:
if angle < np.pi - ANGLE_EPS:
ax = normalize(np.cross(new_x, to_x))
flip = np.dot(lookat_to, ax)
if flip > 0:
r2 = get_r_matrix(lookat_to, angle)
elif flip < 0:
r2 = get_r_matrix(lookat_to, -1. * angle)
else:
# Angle of rotation is too close to 180 degrees, direction of rotation
# does not matter.
r2 = get_r_matrix(lookat_to, angle)
else:
r2 = np.eye(3)
return np.dot(r2, r1)
示例4: subspace_disagreement_measure
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def subspace_disagreement_measure(self, Ps, Pt, Pst):
"""
Get the best value for the number of subspaces
For more details, read section 3.4 of the paper.
**Parameters**
Ps: Source subspace
Pt: Target subspace
Pst: Source + Target subspace
"""
def compute_angles(A, B):
_, S, _ = np.linalg.svd(np.dot(A.T, B))
S[np.where(np.isclose(S, 1, atol=self.eps) == True)[0]] = 1
return np.arccos(S)
max_d = min(Ps.shape[1], Pt.shape[1], Pst.shape[1])
alpha_d = compute_angles(Ps, Pst)
beta_d = compute_angles(Pt, Pst)
d = 0.5 * (np.sin(alpha_d) + np.sin(beta_d))
return np.argmax(d)
示例5: inverse_method
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def inverse_method(self,N,d):
t = np.linspace(1e-3,0.999,N)
f = np.log( t / (1 - t) )
f = f/f[0]
psi= np.pi*f
cosPsi = np.cos(psi)
sinTheta = ( np.abs(cosPsi) + (1-np.abs(cosPsi))*np.random.rand(len(cosPsi)))
theta = np.arcsin(sinTheta)
theta = np.pi-theta + (2*theta - np.pi)*np.round(np.random.rand(len(t)))
cosPhi = cosPsi/sinTheta
phi = np.arccos(cosPhi)*(-1)**np.round(np.random.rand(len(t)))
coords = SkyCoord(phi*u.rad,(np.pi/2-theta)*u.rad,d*np.ones(len(phi))*u.pc)
return coords
示例6: make12
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def make12(b):
theta1 = numpy.arccos(1/numpy.sqrt(5))
theta2 = (numpy.pi - theta1) * .5
r = b/2./numpy.sin(theta1/2)
rot72 = rotmatz(numpy.pi*2/5)
s1 = numpy.sin(theta1)
c1 = numpy.cos(theta1)
p1 = numpy.array(( s1*r, 0, c1*r))
p2 = numpy.array((-s1*r, 0, -c1*r))
coord = [( 0, 0, r)]
for i in range(5):
coord.append(p1)
p1 = numpy.dot(p1, rot72)
for i in range(5):
coord.append(p2)
p2 = numpy.dot(p2, rot72)
coord.append(( 0, 0, -r))
return numpy.array(coord)
示例7: get_uv
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def get_uv(self, xyz_vec):
# Extract lens parameters of interest.
fov_rad = self.lens.fov_deg * pi / 180
fov_scale = np.float32(2 * self.lens.radius_px / fov_rad)
# Normalize the input vector and rotate to match lens reference axes.
xyz_rot = get_rotation_matrix(self.lens.center_qq) * matrix_norm(xyz_vec)
# Convert to polar coordinates relative to lens boresight.
# (In lens coordinates, unit vector's X axis gives boresight angle;
# normalize Y/Z to get a planar unit vector for the bearing.)
# Note: Image +Y maps to 3D +Y, and image +X maps to 3D +Z.
theta_rad = np.arccos(xyz_rot[0,:])
proj_vec = matrix_norm(np.concatenate((xyz_rot[2,:], xyz_rot[1,:])))
# Fisheye lens maps 3D angle to focal-plane radius.
# TODO: Do we need a better model for lens distortion?
rad_px = theta_rad * fov_scale
# Convert back to focal-plane rectangular coordinates.
uv = np.multiply(rad_px, proj_vec) + self.lens.center_px
return np.asarray(uv + 0.5, dtype=int)
# Given an 2xN array of UV pixel coordinates, check if each pixel is
# within the fisheye field of view. Returns N-element boolean mask.
示例8: test_both_limit_angle_inactive
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def test_both_limit_angle_inactive(self, optimization_variables_avg):
l, Q, A = optimization_variables_avg
Q_in = 5e1 * np.eye(len(l))
max_angle = 45
m = 2e-3
m1 = 4e-3
f = .02
x = optimization_methods.optimize_focality(l, Q, f,
max_el_current=m,
max_total_current=m1,
Qin=Q_in,
max_angle=max_angle)
x_sp = optimize_focality(
l, Q, f, max_el_current=m, max_total_current=m1,
Qin=Q_in, max_angle=max_angle)
assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
assert np.all(np.abs(x) <= m + 1e-4)
assert np.isclose(np.sum(x), 0)
assert np.isclose(l.dot(x), f)
assert np.arccos(l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
assert np.allclose(x.dot(Q.dot(x)), x_sp.dot(Q.dot(x_sp)), rtol=1e-4, atol=1e-5)
示例9: test_both_limit_angle_limit
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def test_both_limit_angle_limit(self, optimization_variables_avg):
l, Q, A = optimization_variables_avg
Q_in = 5e1 * np.eye(len(l))
max_angle = 25
m = 2e-3
m1 = 4e-3
f = .02
x = optimization_methods.optimize_focality(l, Q, f,
max_el_current=m,
max_total_current=m1,
Qin=Q_in,
max_angle=max_angle)
x_sp = optimize_focality(
l, Q, f, max_el_current=m, max_total_current=m1,
Qin=Q_in, max_angle=max_angle)
assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
assert np.all(np.abs(x) <= m + 1e-4)
assert np.isclose(np.sum(x), 0)
assert np.isclose(l.dot(x), f)
assert np.arccos(l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
assert np.allclose(x.dot(Q.dot(x)), x_sp.dot(Q.dot(x_sp)), rtol=1e-4, atol=1e-5)
示例10: test_both_limit_angle_Q_iteration
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def test_both_limit_angle_Q_iteration(self, optimization_variables_avg_QCQP):
l, Q, A, Q_in = optimization_variables_avg_QCQP
# l, Q, A = optimization_variables_avg
# Q_in = 6 * np.eye(len(l)) + np.outer(l, l)
max_angle = 20
m = 2e-3
m1 = 4e-3
f = .01
x = optimization_methods.optimize_focality(l, Q, f,
max_el_current=m,
max_total_current=m1,
Qin=Q_in,
max_angle=max_angle)
x_sp = optimize_focality(
l, Q, f, max_el_current=m, max_total_current=m1,
Qin=Q_in, max_angle=max_angle)
assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
assert np.all(np.abs(x) <= m + 1e-4)
assert np.isclose(np.sum(x), 0)
assert np.isclose(l.dot(x), f)
assert np.arccos(l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
assert np.allclose(x.dot(Q.dot(x)), x_sp.dot(Q.dot(x_sp)), rtol=1e-4, atol=1e-5)
示例11: test_both_limit_angle_infeasible_field
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def test_both_limit_angle_infeasible_field(self, optimization_variables_avg_QCQP):
l, Q, A, Q_in = optimization_variables_avg_QCQP
max_angle = 15
m = 2e-3
m1 = 4e-3
f = 2
x = optimization_methods.optimize_focality(l, Q, f,
max_el_current=m,
max_total_current=m1,
Qin=Q_in,
max_angle=max_angle)
assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
assert np.all(np.abs(x) <= m + 1e-4)
assert np.isclose(np.sum(x), 0)
assert np.arccos(l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
示例12: test_limit_nr_angle
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def test_limit_nr_angle(seld, optimization_variables_avg_QCQP):
l, Q, A, Q_in = optimization_variables_avg_QCQP
max_angle = 15
m = 2e-3
m1 = 4e-3
f = 2
n = 4
x = optimization_methods.optimize_focality(l, Q, f,
max_el_current=m,
max_total_current=m1,
Qin=Q_in,
max_angle=max_angle,
max_active_electrodes=n)
assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
assert np.all(np.abs(x) <= m + 1e-4)
assert np.isclose(np.sum(x), 0)
assert np.linalg.norm(x, 0) <= n
assert np.arccos(l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
示例13: test_limit_nr_angle_change_Q
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def test_limit_nr_angle_change_Q(seld, optimization_variables_avg_QCQP):
l, Q, A, Q_in = optimization_variables_avg_QCQP
max_angle = 15
m = 2e-3
m1 = 4e-3
f = .01
n = 4
x = optimization_methods.optimize_focality(l, Q, f,
max_el_current=m,
max_total_current=m1,
Qin=Q_in,
max_angle=max_angle,
max_active_electrodes=n)
assert np.linalg.norm(x, 1) <= 2 * m1 + 1e-4
assert np.all(np.abs(x) <= m + 1e-4)
assert np.isclose(np.sum(x), 0)
assert np.linalg.norm(x, 0) <= n
assert np.arccos(l.dot(x) / np.sqrt(x.dot(Q_in).dot(x))) <= np.deg2rad(max_angle)
示例14: model_model_angles_btwn_axes
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def model_model_angles_btwn_axes(A, B, mxBasis): # Note: default 'gm' basis
""" Angle between the rotation axes of A and B (1-qubit gates)"""
decomp = _tools.decompose_gate_matrix(A)
decomp2 = _tools.decompose_gate_matrix(B)
axisOfRotn = decomp.get('axis of rotation', None)
rotnAngle = decomp.get('pi rotations', 'X')
axisOfRotn2 = decomp2.get('axis of rotation', None)
rotnAngle2 = decomp2.get('pi rotations', 'X')
if rotnAngle == 'X' or abs(rotnAngle) < 1e-4 or \
rotnAngle2 == 'X' or abs(rotnAngle2) < 1e-4:
return _np.nan
if axisOfRotn is None or axisOfRotn2 is None:
return _np.nan
real_dot = _np.clip(_np.real(_np.dot(axisOfRotn, axisOfRotn2)), -1.0, 1.0)
return _np.arccos(abs(real_dot)) / _np.pi
#Note: abs() allows axis to be off by 180 degrees -- if showing *angle* as
# well, must flip sign of angle of rotation if you allow axis to
# "reverse" by 180 degrees.
示例15: axisangle_from_rotm
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import arccos [as 别名]
def axisangle_from_rotm(R):
# logarithm of rotation matrix
# R = R.reshape(-1,3,3)
# tr = np.trace(R, axis1=1, axis2=2)
# phi = np.arccos(np.clip((tr - 1) / 2, -1, 1))
# scale = np.zeros_like(phi)
# div = 2 * np.sin(phi)
# np.divide(phi, div, out=scale, where=np.abs(div) > 1e-6)
# A = (R - R.transpose(0,2,1)) * scale.reshape(-1,1,1)
# aa = np.stack((A[:,2,1], A[:,0,2], A[:,1,0]), axis=1)
# return aa.squeeze()
R = R.reshape(-1,3,3)
omega = np.empty((R.shape[0], 3), dtype=R.dtype)
omega[:,0] = R[:,2,1] - R[:,1,2]
omega[:,1] = R[:,0,2] - R[:,2,0]
omega[:,2] = R[:,1,0] - R[:,0,1]
r = np.linalg.norm(omega, axis=1).reshape(-1,1)
t = np.trace(R, axis1=1, axis2=2).reshape(-1,1)
omega = np.arctan2(r, t-1) * omega
aa = np.zeros_like(omega)
np.divide(omega, r, out=aa, where=r != 0)
return aa.squeeze()