import torch


def rotmat(a,b):
	"""
	Adapted from http://www.netlib.org/templates/matlab/rotmat.m

	Compute the Givens rotation matrix parameters for a and b.
	"""
	c = torch.zeros_like(a)
	s = torch.zeros_like(a)
	temp = torch.zeros_like(a)

	mask = (b.abs()>a.abs())
	temp[mask] = a[mask] / b[mask]
	s[mask] = 1.0 / torch.sqrt(1.0+temp[mask]**2)
	c[mask] = temp[mask] * s[mask]

	mask = (b.abs()<=a.abs())
	temp[mask] = b[mask] / a[mask]
	c[mask] = 1.0 / torch.sqrt(1.0+temp[mask]**2)
	s[mask] = temp[mask] * c[mask]

	mask = (b==0)
	c[mask] = 1.0
	s[mask] = 0.0

	# if b==0.0:
	# 	c = 1.0
	# 	s = 0.0
	# elif b.abs()>a.abs():
	# 	temp = a / b
	# 	s = 1.0 / torch.sqrt(1.0+temp**2)
	# 	c = temp * s
	# else:
	# 	temp = b / a
	# 	c = 1.0 / torch.sqrt(1.0+temp**2)
	# 	s = temp * c
	return c, s