Vector3D¶
turtlend.vector3D
¶
This module is part of turtlend and provides utility functions for calculating geometric properties such as angles and dihedral angles between vectors in 3D space. The vectors are represented using the Vector3D class, which supports various vector operations.
Functions: calc_angle(v1, v2, v3): Calculate the angle between three vectors representing three connected points.
calc_dihedral(v1, v2, v3, v4):
Calculate the dihedral angle between four vectors representing four connected points.
Part of turtlend, https://github.com/Flux-Frontiers/turtlend
Vector3D(x, y=None, z=None)
¶
A class to represent a three-dimensional vector.
Attributes:¶
x : float The x-coordinate of the vector. y : float The y-coordinate of the vector. z : float The z-coordinate of the vector.
Methods:¶
init(x, y, z): Initializes the vector with x, y, and z coordinates. add(other): Adds another Vector3D to this vector and returns the result. sub(other): Subtracts another Vector3D from this vector and returns the result. truediv(scalar): Divides the vector by a scalar and returns the result. dot(other): Computes the dot product of this vector and another Vector3D. magnitude(): Computes the magnitude (length) of the vector. normalize(): Normalizes the vector (makes it unit length). normalized(): Returns a normalized copy of the vector. angle_with(other): Computes the angle between this vector and another Vector3D in degrees. cross(other): Computes the cross product of this vector and another Vector3D. repr(): Returns a string representation of the vector.
Examples:¶
v1 = Vector3D(1, 2, 3) v2 = Vector3D(4, 5, 6) v3 = v1 + v2 v3
v4 = v1 - v2 v4
v5 = v1 / 2 v5
dot_product = v1 * v2 float(dot_product) 32.0
magnitude = v1.magnitude() float(round(magnitude, 2)) 3.74
angle = v1.angle_with(v2) round(angle, 2) 12.93
v6 = v1 ** v2 v6
Initialize the vector with either a list/tuple/array of 3 elements or three separate values.
Source code in src/turtlend/vector3D.py
98 99 100 101 102 103 104 105 106 107 108 | |
__add__(other)
¶
Add another vector or a scalar to this vector.
Source code in src/turtlend/vector3D.py
119 120 121 122 123 124 | |
__contains__(value)
¶
Check if a value is in the vector.
Source code in src/turtlend/vector3D.py
156 157 158 | |
__getitem__(index)
¶
Get the coordinate at the specified index.
Source code in src/turtlend/vector3D.py
148 149 150 | |
__mul__(other)
¶
Compute the dot product with another vector.
Source code in src/turtlend/vector3D.py
133 134 135 | |
__neg__()
¶
Return the negation of the vector.
Source code in src/turtlend/vector3D.py
115 116 117 | |
__pow__(other)
¶
Compute the cross product with another vector or multiply by a scalar.
Source code in src/turtlend/vector3D.py
141 142 143 144 145 146 | |
__repr__()
¶
Return a string representation of the vector.
Source code in src/turtlend/vector3D.py
110 111 112 113 | |
__setitem__(index, value)
¶
Set the coordinate at the specified index.
Source code in src/turtlend/vector3D.py
152 153 154 | |
__sub__(other)
¶
Subtract another vector or a scalar from this vector.
Source code in src/turtlend/vector3D.py
126 127 128 129 130 131 | |
__truediv__(scalar)
¶
Divide the vector by a scalar.
Source code in src/turtlend/vector3D.py
137 138 139 | |
angle_with(other)
¶
Calculate the angle between this vector and another vector in degrees.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Vector3D
|
The other vector to calculate the angle with. |
required |
Returns:
| Type | Description |
|---|---|
float
Example:
>>> v1 = Vector3D(1, 0, 0)
>>> v2 = Vector3D(0, 1, 0)
>>> v1.angle_with(v2)
90.0
|
The angle between the two vectors in degrees. |
Source code in src/turtlend/vector3D.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
copy()
¶
Return a copy of this vector.
Source code in src/turtlend/vector3D.py
224 225 226 | |
get_array()
¶
Return a copy of the vector's coordinates as a numpy array.
Source code in src/turtlend/vector3D.py
212 213 214 | |
left_multiply(matrix)
¶
Multiply this vector by a matrix from the left.
Source code in src/turtlend/vector3D.py
216 217 218 | |
magnitude()
¶
Return the magnitude (norm) of the vector.
Source code in src/turtlend/vector3D.py
160 161 162 | |
magnitude_squared()
¶
Return the squared magnitude of the vector.
Source code in src/turtlend/vector3D.py
164 165 166 | |
normalize()
¶
Normalize the vector in place.
Source code in src/turtlend/vector3D.py
177 178 179 180 181 182 183 | |
normalized()
¶
Return a normalized copy of the Vector.
To avoid allocating new objects use the normalize method.
Source code in src/turtlend/vector3D.py
168 169 170 171 172 173 174 175 | |
right_multiply(matrix)
¶
Multiply this vector by a matrix from the right.
Source code in src/turtlend/vector3D.py
220 221 222 | |
calc_angle(v1, v2, v3)
¶
Calculate the angle between three vectors representing three connected points.
This function calculates the angle formed by the vectors (v1-v2) and (v3-v2).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v1
|
Vector3D
|
The first point that defines the angle. |
required |
v2
|
Vector3D
|
The second point that defines the angle. |
required |
v3
|
Vector3D
|
The third point that defines the angle. |
required |
Returns:
| Type | Description |
|---|---|
float
Example:
>>> v1 = Vector3D(1.0, 0.0, 0.0)
>>> v2 = Vector3D(0.0, 1.0, 0.0)
>>> v3 = Vector3D(0.0, 0.0, 1.0)
>>> angle = calc_angle(v1, v2, v3)
>>> print(float(round(angle, 2)))
60.0
|
The angle between the three vectors in degrees. |
Source code in src/turtlend/vector3D.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
calc_dihedral(v1, v2, v3, v4)
¶
Return the dihedral angle between four 3D points, (-180-180 degrees).
:example:
v1 = Vector3D(0.0, 0.0, 0.0) v2 = Vector3D(1.0, 0.0, 0.0) v3 = Vector3D(1.0, 1.0, 1.0) v4 = Vector3D(1.0, 1.0, 2.0) float(round(calc_dihedral(v1, v2, v3, v4), 2)) 90.0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v1
|
Vector3D
|
The first point. |
required |
v2
|
Vector3D
|
The second point. |
required |
v3
|
Vector3D
|
The third point. |
required |
v4
|
Vector3D
|
The fourth point. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The dihedral angle in degrees. |
Source code in src/turtlend/vector3D.py
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
calculate_bond_angle(atom1, atom2, atom3)
¶
Calculate the bond angle between three atoms given their x, y, z coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
atom1
|
x, y, z coordinates of the first atom. |
required | |
atom2
|
x, y, z coordinates of the second atom. |
required | |
atom3
|
x, y, z coordinates of the third atom. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Bond angle in degrees. |
Source code in src/turtlend/vector3D.py
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
distance3d(p1, p2)
¶
Calculate the 3D Euclidean distance for 2 Vector3D objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p1
|
Vector3D
|
Point 1. |
required |
p2
|
Vector3D
|
Point 2. |
required |
Returns:
| Type | Description |
|---|---|
float
Example:
>>> p1 = Vector3D(1, 0, 0)
>>> p2 = Vector3D(0, 1, 0)
>>> distance3d(p1, p2)
1.4142135623730951
|
Distance between two points. |
Source code in src/turtlend/vector3D.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
rms_difference(calculated_angles, idealized_angles)
¶
Calculate the Root Mean Square (RMS) difference between two angle arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calculated_angles
|
ndarray
|
An array of calculated angles. |
required |
idealized_angles
|
ndarray
|
An array of idealized angles. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The RMS difference between the calculated and idealized angles. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input arrays do not have the same shape or are empty. |
Source code in src/turtlend/vector3D.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | |