c4dynamics.states.state.state.P#
- state.P(state2=None)[source]#
Euclidean distance.
Calculates the Euclidean distance between the self state object and a second object state2. If state2 is not provided, then the self Euclidean distance is calculated.
When a second state object is provided:
\[P = \sum_{k=x,y,z} (self.k - state2.k)^2\]Otherwise:
\[P = \sum_{k=x,y,z} self.k^2\]- Raises:
TypeError – If the states don’t include any position coordinate (x, y, z).
Note
The provided states must have at least oneposition coordinate (x, y, z).
In the context of
P()
, x, y, z, (case sensitive) are considered position coordinates.
- Parameters:
state2 (
state
) – A second state object for which the relative distance is calculated.- Returns:
out (float) – Euclidean norm of the distance vector. The return type specifically is a numpy.float64.
Examples
>>> import c4dynamics as c4d
>>> s = c4d.state(theta = 3.14, x = 1, y = 1) >>> s.P() 1.414...
>>> s = c4d.state(theta = 3.14, x = 1, y = 1) >>> s2 = c4d.state(x = 1) >>> s.P(s2) 1.0
>>> s = c4d.state(theta = 3.14, x = 1, y = 1) >>> s2 = c4d.state(z = 1) >>> s.P(s2) 1.73...
For final example, import required packages:
>>> import numpy as np >>> from matplotlib import pyplot as plt
Settings and initial conditions:
>>> camera = c4d.state(x = 0, y = 0) >>> car = c4d.datapoint(x = -100, vx = 40, vy = -7) >>> dist = [] >>> time = np.linspace(0, 10, 1000)
Main loop:
>>> for t in time: ... car.inteqm(np.zeros(3), time[1] - time[0]) ... dist.append(camera.P(car))
Show results:
>>> plt.plot(time, dist, 'm') >>> c4d.plotdefaults(plt.gca(), 'Distance', 'Time (s)', '(m)') >>> plt.show()