2. 3D-vektor- und matrix- klasen und funktionen

2.1. Module mrmath.vecmat3d

Klassen für dreidimensionale Vektoren und 3*3-Matrizen.

Module author: Michael Rippstein <info@comlab.ch>

Idea and parts of the source from [Montenbruck2004a].

For the classes Vec3D and Mat3D the following operation are definde.

Funktion

Arg_1 (a)

Arg_2 (b)

Wert (c)

Notation

Bedeutung

-

Vec3D

Vec3D

\[c = -a\]

Unäres Minus

Mat3D

Mat3D

\[C = -A\]

-

Vec3D

Vec3D

Vec3D

\[c = a - b\]

Vektor-Subtraktion

Mat3D

Mat3D

Mat3D

\[C = A - B\]

Matrix-Subtraktion

+

Vec3D

Vec3D

Vec3D

\[c = a + b\]

Vektor-Addition

Mat3D

Mat3D

Mat3D

\[C = A + B\]

Matrix-Addition

*

int

Vec3D

Vec3D

\[c = ab\]

Skalar-Multiplikation

float

Vec3D

Vec3D

\[c = ab\]

Vec3D

int

Vec3D

\[c = ab\]

Vec3D

float

Vec3D

\[c = ab\]

int

Mat3D

Mat3D

\[C = aB\]

float

Mat3D

Mat3D

\[C = aB\]

Mat3D

int

Mat3D

\[C = Ab\]

Mat3D

float

Mat3D

\[C = Ab\]

*

Mat3D

Vec3D

Vec3D

\[c = Ab\]

Matrix/Vektor-Multiplikation

/

Vec3D

int

Vec3D

\[c = a/b\]

Skalar-Division

Vec3D

float

Vec3D

\[c = a/b\]

Mat3D

int

Mat3D

\[C = A/b\]

Mat3D

float

Mat3D

\[C = A/b\]

abs

Vec3D

float

\[c = |a|\]

dot

Vec3D

Vec3D

float

\[c = ab\]

Skalarprodukt

cross @

Vec3D

Vec3D

Vec3D

\[c = a × b\]

Vektorprodukt, Kreuzprodukt

tranp

Mat3D

Mat3D

\[C = A^T\]

Transponierte

einmat

Mat3D

Einheitsmatrix

rotmatx

float

Mat3D

Elementare Drehmatrix

rotmaty

float

Mat3D

rotmatz

float

Mat3D

==

Vec3D

Vec3D

bool

2.1.1. Bedeutung der Winkel bei den Kugelkoordinaten

Todo

Korekte winkel bezeichungen im den bilder!

_images/sphere.png

References

Montenbruck2004a

Montenbruck, Oliver; Pfleger, Thomas: “Astronomie mit dem Personal Computer”; 4. Auflage; Springer-Verlag; Berlin, Heidelberg 2004

2.1.2. Class Vec3D

class Vec3D(arg=None, **kwargs)[source]

Bases: object

Dreidimensionaler Vector.

Der Aufruf ohne die angabe von Parameter initialisiert einen Null-Vector.

null_vector = Vec3D()
Parameters
  • **kwargs – siehe unten

  • arg (Union[None, Tuple, List]) –

    siehe unten

    Vec3D([x, y, z])
    Vec3D((x, y, z))
    

  • kwargs (Optional[float]) –

Keyword Arguments
  • r (az, elev,) –

    Polarkoordinaten

    Vec3D(theta=, phi=, r=)
    

  • elev (az,) –

    Polarkoordinaten

    Vec3D(az=, elev=)
    

  • r

    Polarkoordinaten

    Vec3D(az=, elev=, r=)
    

Raises

TypeError

Return type

None

Examples

>>> nullvector = Vec3D()
>>> print(nullvector)
Vec3D([0, 0, 0])

>>> print(nullvector.xyz)
[0, 0, 0]

>>> repr(nullvector.array)
'array([[0],\n       [0],\n       [0]])'

>>> print("x: {}, y: {}, z: {}".format(nullvector.x, nullvector.y, nullvector.z))
x: 0, y: 0, z: 0

>>> print("phi: {}, theta: {}, r: {}".format(nullvector.phi,
...                                          nullvector.theta,
...                                          nullvector.r))
phi: 0, theta: 0, r: 0

>>> print(Vec3D([1, 2, 3]))
Vec3D([1, 2, 3])

>>> print(Vec3D([1.0, 2.0, 3.0]))
Vec3D([1.0, 2.0, 3.0])

>>> vector = Vec3D()
>>> vector.x = 9.9
>>> vector.y = 5
>>> vector.z = 1.1
>>> print(vector)
Vec3D([9.9, 5, 1.1])

>>> polar = Vec3D(theta=math.pi/2, phi=math.pi/2, r=1)
>>> '{:.3f}, {:.3f}, {:.3f}'.format(polar.x, polar.y, polar.z)
'0.000, 0.000, 1.000'

>>> vector.x = 'text'
Traceback (most recent call last):
...
TypeError: unsupported type for setter: '<class 'str'>'
__abs__()[source]

Betrag (Radius).

Returns

Betrag des Vektors

Return type

float

Examples

>>> vector = Vec3D([1, 0, 0])
>>> print('{:.4f}'.format(abs(vector)))
1.0000

>>> vector = Vec3D([1, 1, 0])
>>> print('{:.4f}'.format(abs(vector)))
1.4142

>>> vector = Vec3D([1, 0, 1])
>>> print('{:.4f}'.format(abs(vector)))
1.4142
__add__(other)[source]

Addition: self + other.

Parameters

other (Union[Vec3D, float]) – Summand

Returns

Summe

Return type

Vec3D

Examples

>>> summand = Vec3D([2, 6, 20])
>>> summe = summand + -9
>>> print(summe)
Vec3D([-7, -3, 11])

>>> summand1 = Vec3D([2, 6, 20])
>>> summand2 = Vec3D([-9.0, -9.0, -9.0])
>>> summe = summand1 + summand2
>>> print(summe)
Vec3D([-7.0, -3.0, 11.0])
__eq__(other)[source]

Comparisons self == other.

Parameters

other (object) –

Return type

bool

__getitem__(key)[source]

Implement the self[key] call.

Returns

for key = 0: x for key = 1: y for key = 2: z

Return type

float

Raises

IndexError – When key outside of 0…2

Parameters

key (int) –

Examples

>>> vector = Vec3D([3, 7, 13])
>>> print(vector[0])
3

>>> print(vector[1])
7

>>> print(vector[2])
13

>>> print(vector[3])
Traceback (most recent call last):
...
IndexError: list index out of range

>>> for m in vector:
...     print(m)
3
7
13
__iadd__(other)[source]

Addition: self += other.

Parameters

other (Union[Vec3D, float]) – Summand

Returns

Summe

Return type

Vec3D

Examples

>>> summe = Vec3D([2, 6, 20])
>>> summe += -9
>>> print(summe)
Vec3D([-7, -3, 11])

>>> summe = Vec3D([2, 6, 20])
>>> summand = Vec3D([-9.0, -9.0, -9.0])
>>> summe += summand
>>> print(summe)
Vec3D([-7.0, -3.0, 11.0])
__imul__(other)[source]

Multiplikation: self = self * other.

Parameters

other (Union[Mat3D, float]) – Multiplikand

Returns

Produkt

Return type

Vex3D

Examples

>>> faktor1 = Vec3D((2, 4, 6))
>>> produkt = faktor1 * 3.75
>>> print(produkt)
Vec3D([7.5, 15.0, 22.5])

>>> faktor1 = Vec3D((2, 4, 6))
>>> faktor2 = rotmatx(0.5 * math.pi)
>>> produkt = faktor1 * faktor2
>>> print(produkt)
Vec3D([2, -4, 6])

>>> produkt = faktor1 * 'text'
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for '*': '<class '__main__.Vec3D'>' and '<class 'str'>'
__isub__(other)[source]

Subtraktion: self -= other.

Parameters

other (Union[Vec3D, float]) – Subtrahend

Returns

Differenz

Return type

Vec3D

Examples

>>> differenz = Vec3D([4, 8, 16])
>>> differenz -= 2
>>> print(differenz)
Vec3D([2, 6, 14])
__itruediv__(other)[source]

Division self /= other.

Parameters

other (Union[int, float]) – Divisor

Returns

Quotient

Return type

Vec3D

Examples

>>> quotient = Vec3D([1, 2, 3])
>>> quotient /= 2
>>> print(quotient)
Vec3D([0.5, 1.0, 1.5])

>>> quotient /= 'string'
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for '/=': '<class '__main__.Vec3D'>' and '<class 'str'>'
__mul__(other)[source]

Multiplikation self * other.

Parameters

other (Union[Mat3D, float]) – Multiplikatand

Returns

Produkt

Return type

Vec3D

Examples

>>> faktor1 = Vec3D((2,4,6))
>>> produkt = faktor1 * 3.75
>>> print(produkt)
Vec3D([7.5, 15.0, 22.5])

>>> produkt = faktor1 * 'text'
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for '*': '<class '__main__.Vec3D'>' and '<class 'str'>'
__neg__()[source]

Negation: -self.

Returns

inverted vector

Return type

Vec3D

Examples

>>> vector = Vec3D([1, 2, 3])
>>> print(-vector)
Vec3D([-1, -2, -3])

>>> vector = Vec3D([-1, -2, -3])
>>> print(-vector)
Vec3D([1, 2, 3])
__radd__(other)[source]

Addition: other + self.

Parameters

other (Union[Vec3D, float]) – Summand

Returns

Summe

Return type

Vec3D

Examples

>>> summand = Vec3D([2, 6, 20])
>>> summe = -9 + summand
>>> print(summe)
Vec3D([-7, -3, 11])
__repr__()[source]

Reprentation des Vectors.

Returns

Reprepresantation

Return type

str

__rmul__(other)[source]

Multiplikation other * self.

Parameters

other (Union[Mat3D, float]) – Multiplikator

Returns

Produkt

Return type

Vec3D

Examples

>>> faktor2 = Vec3D((2,4,6))
>>> produkt = 3.75 * faktor2
>>> print(produkt)
Vec3D([7.5, 15.0, 22.5])

>>> produkt = faktor2 * 'text'
Traceback (most recent call last):
...
TypeError: ...
__rsub__(other)[source]

Subraktion: other - self.

Parameters

other (Union[Vec3D, float]) – Minuend

Returns

Differenz

Return type

Vec3D

Examples

>>> minuend = Vec3D([4, 8, 16])
>>> subtrahend = Vec3D([3, 9, 27])
>>> differenz = 2 - subtrahend
>>> print(differenz)
Vec3D([-1, -7, -25])

>>> differenz = minuend - subtrahend
>>> print(differenz)
Vec3D([1, -1, -11])
__setitem__(key, value)[source]

Implement the assignment to self[key].

Parameters
  • key (int) – Schlüssel

  • value (float) – Wert

Raises

IndexError: – When key outside of 0…2

Return type

None

Examples

>>> vector = Vec3D()
>>> vector[0] = 3
>>> vector[1] = 7
>>> vector[2] = 13
>>> print(vector)
Vec3D([3, 7, 13])

>>> vector[3] = 17
Traceback (most recent call last):
...
IndexError: list index out of range

>>> for m in vector:
...     print(m)
3
7
13
__sub__(other)[source]

Subtraktion: self - other.

Parameters

other (Union[Vec3D, float]) – Subtrahend

Returns

Differenz

Return type

Vec3D

Examples

>>> minuend = Vec3D([4, 8, 16])
>>> subtrahend = Vec3D([3, 9, 27])
>>> differenz = minuend - 2
>>> print(differenz)
Vec3D([2, 6, 14])

>>> differenz = minuend - subtrahend
>>> print(differenz)
Vec3D([1, -1, -11])
__truediv__(other)[source]

Division self / other.

Parameters

other (float) – Divisor

Returns

Quotient

Return type

Vec3D

Examples

>>> dividend = Vec3D([1, 2, 3])
>>> quotient = dividend / 2
>>> print(quotient)
Vec3D([0.5, 1.0, 1.5])
_calcpolar()[source]

Berechne polare Komponenten.

Berechnet die polaren Komponenten des Vektors mit den aktuellen kartesischen Daten. Setzt __polarvalid auf True.

Examples

>>> vector1 = Vec3D([1, 0, 0])
>>> print("phi: {}, theta: {}, r: {}".format(vector1.phi,
...                                          vector1.theta,
...                                          vector1.r))
phi: 0.0, theta: 0.0, r: 1.0

>>> vector2 = Vec3D([0, 1, 0])
>>> print("phi: {:.4f}, theta: {}, r: {}".format(vector2.phi,
...                                              vector2.theta,
...                                              vector2.r))
phi: 1.5708, theta: 0.0, r: 1.0

>>> vector3 = Vec3D([0, 0, 1])
>>> print("phi: {}, theta: {:.4f}, r: {}".format(vector3.phi,
...                                              vector3.theta,
...                                              vector3.r))
phi: 0.0, theta: 1.5708, r: 1.0

>>> vector4 = Vec3D([-1, -1, -1])
>>> print("phi: {:.4f}, theta: {:.4f}, r: {:.4f}".format(vector4.phi,
...                                                      vector4.theta,
...                                                      vector4.r))
phi: 3.9270, theta: -0.6155, r: 1.7321
Return type

None

property array: numpy.ndarray

Gibt die kartesischen Koordinaten als Spaltenvector in einem numpy.array zurück.

Returns

Spaltenvektor [[x][y][z]]

Return type

numpy.ndarray

property phi: float

Azimut in den Polarkoordinaten.

property polar: List[float]

Gibt die polar Koordinaten zurück.

Returns

Polarkoordinaten: [phi, theta, r]

Return type

List[float]

property r: float

Radius in den Polarkoordinaten.

property theta: float

Elevation in den Polarkoordinaten.

property x: float

x-axis in the cartesian coordinate system.

Returns

x-axis

Return type

float

property xyz: List[float]

Gibt die kartesischen Koordinaten zurück.

Returns

Kartesischekoordinaten [x, y, z]

Return type

List[float]

property y: float

y-axis in the cartesian coordinate system.

property z: float

z-axis in the cartesian coordinate system.

2.1.3. Class Mat3D

class Mat3D(args)[source]

Bases: object

3x3 Matrix.

Parameters

args (List) –

Return type

None

__getitem__(key)[source]

FIXME! briefly describe function.

Parameters

key (int) –

Return type

float

__mul__(other)[source]

Multiplikation: ‘self * other’.

Parameters

other (Union[Mat3D, int, float]) – Multiplikator

Returns

Produkt

Return type

Mat3D

__repr__()[source]

FIXME! briefly describe function.

Return type

str

2.1.4. Operationen

cross(left, right)[source]

Vektorprodukt (Kreuzprodukt).

Parameters
  • left (Vec3D) – Linker Vektor

  • right (Vec3D) – Rechter Vektor

Returns

Kreuzprodukt

Return type

Vec3D

Raises

TypeError – If left or right is not from type Vec3D

dot(left, right)[source]

Skalarprodukt zweier Vektoren.

Parameters
  • left (Vec3D) – Linker Vektor

  • right (Vec3D) – Rechter Vektor

Returns

Skalarprodukt

Return type

float

Raises

TypeError – If left or right is not from type Vec3D

transp(matrix)[source]

Transponierte einer Matrix.

Parameters

matrix (Mat3D) – Originalmatrix

Returns

Transponierte Matrix

Return type

Mat3D

einmat()[source]

Einheitsmatrix.

Returns

Einheitsmatrix

Return type

Mat3D

rotmatx(alpha)[source]

Matrix für die rotation um die x-achse.

_images/rotmatx.png
Parameters

alpha (float) – Drehwinkel

Returns

Rotationsmatrix

Return type

Mat3D

Examples

>>> a = rotmatx(0.25 * math.pi)
>>> a._mat
[[1, 0, 0], [0, 0.7071067811865476, 0.7071067811865475], [0, -0.7071067811865475, 0.7071067811865476]]

>>> a = rotmatx(0.5 * math.pi)
>>> b = Vec3D((0.0, 1.0, 0.0))
>>> a * b
Vec3D([0.0, 6.123233995736766e-17, -1.0])
rotmaty(alpha)[source]

Matrix für die rotation um die y-achse.

_images/rotmaty.png
Parameters

alpha (float) – Drehwinkel

Returns

Rotationsmatrix

Return type

Mat3D

rotmatz(alpha)[source]

Matrix für die Rotation um die Z-Achse.

_images/rotmatz.png
Parameters

alpha (float) – Drehwinkel

Returns

Rotationsmatrix

Return type

Mat3D

polar2kart(phi, theta, r)[source]

Polarkoordinaten zu Kartesischenkoordinaten.

_images/sphere.png
Parameters
  • phi (float) – winkel zwischen z-achse und vektor

  • theta (float) – winkel zwischen x-achse und vektor

  • r (float) – länge des vector

Return type

(x, y, z) kart.-koord.