vector module

Vector algebra/calculus methods for numpy ndarrays.

Module author: Michael S. Chapman <chapmanms@missouri.edu>

Authors:

Michael S. Chapman <chapmanms@missouri.edu>

Oregon Health & Science University & University of Missouri

Version:

1, Nov 26, 2024

Changed in version 0.5.6: (04/02/18) Initial coding

Changed in version 1.0.0: 11/17/20 Python 2.7 –> 3.6

vector.assertOneD(a)
vector.assertSameSize(a, b)
vector.crossDeriv(a, b)

Derivative of the vector product v = a x b with respect to components of a & b

Parameters:

b (ndarray a,) – one-dimensional, same length

Returns:

partial derivatives for the vector product v, for each component of a, b

Return type:

(ndarray, shape=(a.shape, a.shape),)*2

For: \vec{v} = \vec{a} \times \vec{b}, where: \vec{a} = a_1\vec{i} + a_2\vec{j} + a_3\vec{k}; \vec{b} = b_1\vec{i} + b_2\vec{j} + b_3\vec{k} and \left(\vec{i}, \vec{j}, \vec{k}\right) are the basis vectors:

(1)v_1 = (a_2b_3 - a_3b_2);

(2)\qquad \frac{\partial v_1}{\partial \vec{a}} = (0, b_3, -b_2);

(3)\qquad \frac{\partial v_1}{\partial \vec{b}} = (0, -a_3, a_2)

(4)v_2 = (a_3b_1 - a_1b_3);

(5)\qquad \frac{\partial v_2}{\partial \vec{a}} = (-b_3, 0, b_1);

(6)\qquad \frac{\partial v_2}{\partial \vec{b}} = (a_3, 0, -a_1)

(7)v_3 = (a_1b_2 - a_2b_1);

(8)\qquad \frac{\partial v_3}{\partial \vec{a}} = (b_2, -b_1, 0);

(9)\qquad \frac{\partial v_3}{\partial \vec{b}} = (-a_2, a_1, 0)

Returns \frac{\partial \vec{v}}{\partial \vec{a}}, \frac{\partial \vec{v}}{\partial \vec{b}}, where:

(10)\frac{\partial \vec{v}}{\vec{a}} = \left[
\left[\frac{\partial v_1}{\vec{a}}\right]\;
\left[\frac{\partial v_2}{\vec{a}}\right]\;
\left[\frac{\partial v_3}{\vec{a}}\right]\right]

and where:

(11)\frac{\partial v}{\vec{a}} = \left[
\frac{\partial v}{\overrightarrow{a_1}},\;
\frac{\partial v}{\overrightarrow{a_2}},\;
\frac{\partial v}{\overrightarrow{a_3}}\right]

vector.crossNorm(a, b)

Length of the vector product v = a x b.

Parameters:

b (ndarray a,) – one-dimensional, same length

Returns:

norm-2 length of the vector product v

Return type:

float

vector.crossNormDeriv(a, b)

Derivative of the length of the vector product a x b with respect to components of a & b

Parameters:

b (ndarray a,) – one-dimensional, same length

Returns:

partial derivatives for the vector product v, for each component of a, b

Return type:

(ndarray, shape=(a.shape, a.shape),)*2

For: \vec{v} = \vec{a} \times \vec{b},

(12)\frac{\partial \|\vec{v}\|}{\partial \vec{a}} 
 = \frac{\partial \|\vec{v}\|}{\partial \vec{v}}
 \cdot\frac{\partial \left(\vec{a}\times\vec{b}\right)}{\partial \vec{a}}

(By the chain rule, where the 1st term comes from normDeriv() and the 2nd from crossDeriv().)

vector.crossUnit(a, b)

Unit vector in direction of vector product, v = a x b.

Parameters:

b (ndarray a,) – one-dimensional, same length

Returns:

unit vector along vector product v

Return type:

ndarray, shape=a.size

vector.crossUnitDeriv(a, b)

Derivative of the unit vector normal to a & b with respect to components of a & b

Parameters:

b (ndarray a,) – one-dimensional, same length

Returns:

partial derivatives for the vector product v, for each component of a, b

Return type:

(ndarray, shape=(a.shape, a.shape),)*2

For: \vec{v} = \vec{a} \times \vec{b},

(13)\frac{\partial \|\vec{v}\|}{\partial \vec{a}} 
 = \frac{\partial \|\vec{v}\|}{\partial \vec{v}}
 \cdot\frac{\partial \left(\vec{a}\times\vec{b}\right)}{\partial \vec{a}}

(By the chain rule, where the 1st term comes from normDeriv() and the 2nd from crossDeriv().)

vector.dotDeriv(a, b)

Derivative of the scalar product with respect to a, b.

Note that the return is the trivial (b, a) if want to hard-wire.

Parameters:

b (ndarray a,) – one-dimensional, same length

Returns:

scalar product derivatives for each component of a, b

Return type:

(ndarray, ndarray), shaped like a=b

For: \vec{v} = \vec{a} \cdot \vec{b},

(14)\frac{\partial s}{\partial \vec{a}} 
= \vec{a} \cdot \frac{\partial \vec{b}}{\partial \vec{a}}
+ \vec{b} \cdot \frac{\partial \vec{a}}{\partial \vec{a}}
= \vec{b}

Similarly,

(15)\frac{\partial s}{\partial \vec{b}} = \vec{a}

vector.normDeriv(a)

Derivative of the 2-norm length of a vector with respect to its components.

Parameters:

a (ndarray) – one-dimensional

Returns:

derivative with respect to each component

Return type:

ndarray, shaped like a