def screwMotion(self): l = self.vector.length() if l == 0.: return Geometry.Vector(0.,0.,0.), \ Geometry.Vector(0.,0.,1.), 0., 0. else: return Geometry.Vector(0., 0., 0.), self.vector / l, 0., l
def __rmul__(self, other): from Scientific import Geometry if Geometry.isTensor(other): product = other.dot(Geometry.Tensor(self.array)) if product.rank == 1: return Vector(product.array) else: return product else: return Vector(Numeric.multiply(self.array, other))
def __init__(self, *args): if len(args) == 1: if Geometry.isTensor(args[0]): self.tensor = args[0] else: self.tensor = Geometry.Tensor(args[0]) assert self.tensor.rank == 2 elif len(args) == 3 and Geometry.isVector(args[0]) \ and Geometry.isVector(args[1]) and Geometry.isVector(args[2]): self.tensor = Geometry.Tensor([args[0].array, args[1].array, args[2].array]).transpose()
def __mul__(self, other): from Scientific import Geometry if isVector(other): return Numeric.add.reduce(self.array * other.array) elif Geometry.isTensor(other): product = Geometry.Tensor(self.array).dot(other) if product.rank == 1: return Vector(product.array) else: return product elif hasattr(other, "_product_with_vector"): return other._product_with_vector(self) else: return Vector(Numeric.multiply(self.array, other))
def asTensor(self): """ @returns: an equivalent rank-1 tensor object @rtype: L{Scientific.Geometry.Tensor} """ from Scientific import Geometry return Geometry.Tensor(self.array, 1)
def dyadicProduct(self, other): """ @param other: a vector or a tensor @type other: L{Vector} or L{Scientific.Geometry.Tensor} @returns: the dyadic product with other @rtype: L{Scientific.Geometry.Tensor} @raises TypeError: if other is not a vector or a tensor """ from Scientific import Geometry if isVector(other): return Geometry.Tensor( self.array[:, N.NewAxis] * other.array[N.NewAxis, :], 1) elif Geometry.isTensor(other): return Geometry.Tensor(self.array, 1) * other else: raise TypeError("Dyadic product with non-vector")
def __mul__(self, other): from Scientific import Geometry if isTensor(other): a = self.array[self.rank * (slice(None), ) + (N.NewAxis, )] b = other.array[other.rank * (slice(None), ) + (N.NewAxis, )] return Tensor(N.innerproduct(a, b), 1) elif Geometry.isVector(other): return other.__rmul__(self) else: return Tensor(self.array * other, 1)
def __mul__(self, other): from Scientific import Geometry if isTensor(other): a = self.array[self.rank*(slice(None),)+(N.NewAxis,)] b = other.array[other.rank*(slice(None),)+(N.NewAxis,)] return Tensor(N.innerproduct(a, b), 1) elif Geometry.isVector(other): return other.__rmul__(self) else: return Tensor(self.array*other, 1)
def screwMotion(self): axis, angle = self.rotation().axisAndAngle() d = self.vector*axis x = d*axis-self.vector if abs(angle) < 1.e-9: r0 = Geometry.Vector(0., 0., 0.) angle = 0. else: r0 = -0.5*((N.cos(0.5*angle)/N.sin(0.5*angle))*axis.cross(x)+x) return r0, axis, angle, d
def __rmul__(self, other): from Scientific import Geometry if Geometry.isTensor(other): product = other.dot(Geometry.Tensor(self.array)) if product.rank == 1: return Vector(product.array) else: return product else: return Vector(Numeric.multiply(self.array, other))
def asVector(self): """ @returns: an equivalent vector object @rtype: L{Scientific.Geometry.Vector} @raises ValueError: if rank > 1 """ from Scientific import Geometry if self.rank == 1: return Geometry.Vector(self.array) else: raise ValueError('rank > 1')
def __mul__(self, other): from Scientific import Geometry if isVector(other): return Numeric.add.reduce(self.array*other.array) elif Geometry.isTensor(other): product = Geometry.Tensor(self.array).dot(other) if product.rank == 1: return Vector(product.array) else: return product elif hasattr(other, "_product_with_vector"): return other._product_with_vector(self) else: return Vector(Numeric.multiply(self.array, other))
def __init__(self, *args): """ There are two calling patterns: - Rotation(tensor), where tensor is a L{Scientific.Geometry.Tensor} of rank 2 containing the rotation matrix. - Rotation(axis, angle), where axis is a L{Scientific.Geometry.Vector} and angle a number (the angle in radians). """ if len(args) == 1: self.tensor = args[0] if not Geometry.isTensor(self.tensor): self.tensor = Geometry.Tensor(self.tensor) elif len(args) == 2: axis, angle = args axis = axis.normal() projector = axis.dyadicProduct(axis) self.tensor = projector - \ N.sin(angle)*Geometry.epsilon*axis + \ N.cos(angle)*(Geometry.delta-projector) else: raise TypeError('one or two arguments required')
def screwMotion(self): axis, angle = self.rotation().axisAndAngle() d = self.vector * axis if d < 0.: d = -d axis = -axis angle = -angle if abs(angle) < 1.e-9: r0 = Geometry.Vector(0., 0., 0.) angle = 0. else: x = d * axis - self.vector r0 = -0.5 * ( (N.cos(0.5 * angle) / N.sin(0.5 * angle)) * axis.cross(x) + x) return r0, axis, angle % (2. * N.pi), d
def dyadicProduct(self, other): """ @param other: a vector or a tensor @type other: L{Vector} or L{Scientific.Geometry.Tensor} @returns: the dyadic product with other @rtype: L{Scientific.Geometry.Tensor} @raises TypeError: if other is not a vector or a tensor """ from Scientific import Geometry if isVector(other): return Geometry.Tensor(self.array, 1) * \ Geometry.Tensor(other.array, 1) elif Geometry.isTensor(other): return Geometry.Tensor(self.array, 1)*other else: raise TypeError("Dyadic product with non-vector")
def __init__(self, *args): if len(args) == 1: if Geometry.isTensor(args[0]): self.tensor = args[0] else: self.tensor = Geometry.Tensor(args[0]) assert self.tensor.rank == 2 elif len(args) == 3 and Geometry.isVector(args[0]) \ and Geometry.isVector(args[1]) and Geometry.isVector(args[2]): self.tensor = Geometry.Tensor( [args[0].array, args[1].array, args[2].array]).transpose()
def axisAndAngle(self): """ @returns: the axis (a normalized vector) and angle (in radians). The angle is in the interval (-pi, pi] @rtype: (L{Scientific.Geometry.Vector}, C{float}) """ asym = -self.tensor.asymmetricalPart() axis = Geometry.Vector(asym[1, 2], asym[2, 0], asym[0, 1]) sine = axis.length() if abs(sine) > 1.e-10: axis = axis / sine projector = axis.dyadicProduct(axis) cosine = (self.tensor - projector).trace() / (3. - axis * axis) angle = angleFromSineAndCosine(sine, cosine) else: t = 0.5 * (self.tensor + Geometry.delta) i = N.argmax(t.diagonal().array) axis = (t[i] / N.sqrt(t[i, i])).asVector() angle = 0. if t.trace() < 2.: angle = N.pi return axis, angle
def __init__(self, *args): """ There are two calling patterns: - Rotation(tensor), where tensor is a L{Scientific.Geometry.Tensor} of rank 2 containing the rotation matrix. - Rotation(axis, angle), where axis is a L{Scientific.Geometry.Vector} and angle a number (the angle in radians). """ if len(args) == 1: self.tensor = args[0] if not Geometry.isTensor(self.tensor): self.tensor = Geometry.Tensor(self.tensor) elif len(args) == 2: axis, angle = args axis = axis.normal() projector = axis.dyadicProduct(axis) self.tensor = projector - \ Numeric.sin(angle)*Geometry.epsilon*axis + \ Numeric.cos(angle)*(Geometry.delta-projector) else: raise TypeError('one or two arguments required')
def inverse(self): return LinearTransformation(self.tensor.inverse(), -self.vector) # Utility functions def angleFromSineAndCosine(y, x): return atan2(y, x) def mod_angle(angle, mod): return (angle + mod / 2.) % mod - mod / 2 # Test code if __name__ == '__main__': t = Translation(Geometry.Vector(1., -2., 0)) r = Rotation(Geometry.Vector(0.1, -2., 0.5), 1.e-10) q = r.asQuaternion() angles = r.threeAngles(Geometry.Vector(1., 0., 0.), Geometry.Vector(0., 1., 0.), Geometry.Vector(0., 0., 1.)) c = t * r print c.screwMotion() s = Scaling(2.) all = s * t * r print all(Geometry.ex)
def translation(self): return Translation(Geometry.Vector(0., 0., 0.))
def screwMotion(self): axis, angle = self.axisAndAngle() return Geometry.Vector(0., 0., 0.), axis, angle, 0.
junk = co_file.readline() testline = co_file.readline() while testline != "": # Eliminate white space and split line in_str = testline.strip() in_str = in_str.split() #Convert coordinates based on atom type if (in_str[0] == "H" and len(H1) == len(H2)): #Convert coordinates to vectors for the H1's H1.append( Geo.Vector(float(in_str[1]), float(in_str[2]), float(in_str[3]))) print "H1" elif (in_str[0] == "H" and len(H2) == (len(H1) - 1)): #Convert coordinates to vectors for the H2's H2.append( Geo.Vector(float(in_str[1]), float(in_str[2]), float(in_str[3]))) print "H2" elif (in_str[0] == "O"): #Convert coordinates to vectors for the O's Oxy.append( Geo.Vector(float(in_str[1]), float(in_str[2]), float(in_str[3]))) print "O"
junk = xyz_file.readline() junk = xyz_file.readline() while 1: line = xyz_file.readline() if not line: break x, y, z = map(float, string.split(line)[1:]) #ignore atom label cage_type = string.split(line)[0] #get the atom type #Convert coordinates based on atom type if (cage_type == "Ar(Small)"): #Convert coordinates to vectors for the Small Cages small_cages.append(Geo.Vector(x,y,z)) elif (cage_type == "Kr(Large)"): #Convert coordinates to vectors for the Large Cages large_cages.append(Geo.Vector(x,y,z)) else: raise CorrelationError("Can't parse file. Encountered strange cage centre type: " + cage_type + " Only 'Ar(Small)' and 'Kr(Large)' accepted.") xyz_file.close() print "Read in Cage Centre locations. There are %d small and %d large cages." % (len(small_cages), len(large_cages))