def log(x): """Return the natural logarithm (base e) of x.""" if isinstance(x, (int, long, float)): return Quat(math.log(x)) elif isinstance(x, complex): return Quat(1) * cmath.log(x) else: # isinstance(x, Quat) pass
def test_cosh(self): self.assertAlmostEqual(qmath.cosh(self.f1), math.cosh(self.f1)) self.assertAlmostEqual(qmath.cosh(self.c1), cmath.cosh(self.c1)) self.assertAlmostEqual(qmath.cosh(self.c2), cmath.cosh(self.c2)) self.assertAlmostEqual(qmath.cosh(Quat(self.f1)), math.cosh(self.f1)) self.assertAlmostEqual(qmath.cosh(Quat(0, 3)), cmath.cosh(3J)) self.assertAlmostEqual(qmath.cosh(Quat(4, 5)), cmath.cosh(4 + 5J)) self.assertAlmostEqual(qmath.cosh(Quat(0, self.f1)), Quat(math.cos(self.f1)))
def test_sin(self): self.assertAlmostEqual(qmath.sin(self.f1), math.sin(self.f1)) self.assertAlmostEqual(qmath.sin(self.c1), cmath.sin(self.c1)) self.assertAlmostEqual(qmath.sin(self.c2), cmath.sin(self.c2)) self.assertAlmostEqual(qmath.sin(Quat(self.f1)), math.sin(self.f1)) self.assertAlmostEqual(qmath.sin(Quat(0, 3)), cmath.sin(3J)) self.assertAlmostEqual(qmath.sin(Quat(4, 5)), cmath.sin(4 + 5J)) self.assertAlmostEqual(qmath.sin(Quat(0, self.f1)), Quat(0, math.sinh(self.f1)))
def cosh(x): """Return the hyperbolic cosine of x.""" if isinstance(x, (int, long, float)): return Quat(math.cosh(x)) elif isinstance(x, complex): #return Quat(1) * cmath.cosh(x) return Quat( math.cosh(x.real) * math.cos(x.imag), math.sinh(x.real) * math.sin(x.imag)) else: # isinstance(x, Quat) # cos(q)=(exp(q)+exp(-q))/2 result = exp(x) + exp(-x) result = result * Quat(0.5) return result
def test_exp(self): self.assertAlmostEqual(qmath.exp(self.f1), math.exp(self.f1)) self.assertAlmostEqual(qmath.exp(self.c1), cmath.exp(self.c1)) self.assertAlmostEqual(qmath.exp(self.c2), cmath.exp(self.c2)) self.assertAlmostEqual(qmath.exp(Quat(self.f1)), math.exp(self.f1)) self.assertAlmostEqual(qmath.exp(Quat(0, 3)), cmath.exp(3J)) self.assertAlmostEqual(qmath.exp(Quat(4, 5)), cmath.exp(4 + 5J)) self.assertAlmostEqual(qmath.exp(Quat(0, math.pi)), -1.0) self.assertAlmostEqual(qmath.exp(Quat(0, 0, math.pi)), -1.0) self.assertAlmostEqual(qmath.exp(Quat(0, 0, 0, math.pi)), -1.0) self.assertAlmostEqual(qmath.exp(Quat(self.f1, 0, self.f2)), math.exp(self.f1) * Quat(math.cos(self.f2), 0, math.sin(self.f2))) self.assertAlmostEqual(qmath.exp(Quat(self.f1, 0, 0, self.f2)), math.exp(self.f1) * Quat(math.cos(self.f2), 0, 0, math.sin(self.f2)))
def cos(x): """Return the cosine of x.""" if isinstance(x, (int, long, float)): return Quat(math.cos(x)) elif isinstance(x, complex): #return Quat(1) * cmath.cos(x) return Quat( math.cos(x.real) * math.cosh(x.imag), -math.sin(x.real) * math.sinh(x.imag)) else: # isinstance(x, Quat) # cos(q)=(exp(qJ)+exp(-qJ))/2 quat = x * Quat(0, 1) result = exp(quat) + exp(-quat) result = result * Quat(0.5) return result
def exp(x): """Return the exponential value e**x.""" if isinstance(x, (int, long, float)): return Quat(math.exp(x)) elif isinstance(x, complex): # exp(a+bJ)=exp(a)*(cos(b)+sin(b)J) exp_real = math.exp(x.real) return Quat(exp_real * math.cos(x.imag), exp_real * math.sin(x.imag)) #return Quat(1) * cmath.exp(x) else: # isinstance(x, Quat) quat_imag = Quat(0, x.q[1], x.q[2], x.q[3]) v = abs(quat_imag) #if v < 1e4: # result = quat_imag * (1.0 - v*v/6.0) if v == 0.0: result = quat_imag else: result = quat_imag * (math.sin(v) / v) result = result + Quat(math.cos(v)) result = result * math.exp(x.q[0]) return result
from symmetries import hcp_syms, fcc_syms import numpy as np from time import time # Generate random cubochoric coordinates N = 10000000 symmetry_system = 'hcp' rand_cubos = np.random.uniform(low = -1*np.pi/2, high = np.pi/2, size = (N, 3)) t1 = time() # Convert to quaternions and assign as Quats conv_quats = Quat(cubo2quat(rand_cubos, scalar_first=False)) # Preallocate ndarrays for symmetry and perform outer product quat_syms = 0 symcount = 0 if symmetry_system is 'hcp': quat_syms = conv_quats.outer_prod(hcp_syms) symcount = 12 else: quat_syms = conv_quats.outer_prod(fcc_syms) symcount = 24 ##################### # Loss calcualtiona with expanded quaternion symmetry would be calculated here using quat_syms ####################
import math # Generate random cubochoric coordinates torchpi = torch.tensor(math.pi) N = 10 symmetry_system = 'hcp' rand_cubos = (torchpi / 2 - (-1 * torchpi / 2)) * torch.rand(N, 3) + (-1 * torchpi / 2) t1 = time() # Convert to quaternions and assign as Quats conv_quats = Quat( quat_conversion_torch.cubo2quat(rand_cubos, scalar_first=False)).to_torch() # Preallocate ndarrays for symmetry and perform outer product quat_syms = torch.tensor(0) symcount = torch.tensor(0) torch_hcp_syms, torch_fcc_syms = hcp_syms.to_torch(), fcc_syms.to_torch() if symmetry_system is 'hcp': quat_syms = conv_quats.outer_prod(torch_hcp_syms) symcount = 12 else: quat_syms = conv_quats.outer_prod(torch_fcc_syms) symcount = 24 #####################