Compute the cross product of two (arrays of) vectors. The cross product of `a` and `b` in :math:`R^3` is a vector perpendicular to both `a` and `b`. The vectors in `a` and `b` are defined by the values along the dimension `dim` and can have sizes 1, 2 or 3. Where the size of ei
(
a: DataArray | Variable, b: DataArray | Variable, *, dim: Hashable
)
| 314 | |
| 315 | |
| 316 | def cross( |
| 317 | a: DataArray | Variable, b: DataArray | Variable, *, dim: Hashable |
| 318 | ) -> DataArray | Variable: |
| 319 | """ |
| 320 | Compute the cross product of two (arrays of) vectors. |
| 321 | |
| 322 | The cross product of `a` and `b` in :math:`R^3` is a vector |
| 323 | perpendicular to both `a` and `b`. The vectors in `a` and `b` are |
| 324 | defined by the values along the dimension `dim` and can have sizes |
| 325 | 1, 2 or 3. Where the size of either `a` or `b` is |
| 326 | 1 or 2, the remaining components of the input vector is assumed to |
| 327 | be zero and the cross product calculated accordingly. In cases where |
| 328 | both input vectors have dimension 2, the z-component of the cross |
| 329 | product is returned. |
| 330 | |
| 331 | Parameters |
| 332 | ---------- |
| 333 | a, b : DataArray or Variable |
| 334 | Components of the first and second vector(s). |
| 335 | dim : hashable |
| 336 | The dimension along which the cross product will be computed. |
| 337 | Must be available in both vectors. |
| 338 | |
| 339 | Examples |
| 340 | -------- |
| 341 | Vector cross-product with 3 dimensions: |
| 342 | |
| 343 | >>> a = xr.DataArray([1, 2, 3]) |
| 344 | >>> b = xr.DataArray([4, 5, 6]) |
| 345 | >>> xr.cross(a, b, dim="dim_0") |
| 346 | <xarray.DataArray (dim_0: 3)> Size: 24B |
| 347 | array([-3, 6, -3]) |
| 348 | Dimensions without coordinates: dim_0 |
| 349 | |
| 350 | Vector cross-product with 3 dimensions but zeros at the last axis |
| 351 | yields the same results as with 2 dimensions: |
| 352 | |
| 353 | >>> a = xr.DataArray([1, 2, 0]) |
| 354 | >>> b = xr.DataArray([4, 5, 0]) |
| 355 | >>> xr.cross(a, b, dim="dim_0") |
| 356 | <xarray.DataArray (dim_0: 3)> Size: 24B |
| 357 | array([ 0, 0, -3]) |
| 358 | Dimensions without coordinates: dim_0 |
| 359 | |
| 360 | Multiple vector cross-products. Note that the direction of the |
| 361 | cross product vector is defined by the right-hand rule: |
| 362 | |
| 363 | >>> a = xr.DataArray( |
| 364 | ... [[1, 2, 3], [4, 5, 6]], |
| 365 | ... dims=("time", "cartesian"), |
| 366 | ... coords=dict( |
| 367 | ... time=(["time"], [0, 1]), |
| 368 | ... cartesian=(["cartesian"], ["x", "y", "z"]), |
| 369 | ... ), |
| 370 | ... ) |
| 371 | >>> b = xr.DataArray( |
| 372 | ... [[4, 5, 6], [1, 2, 3]], |
| 373 | ... dims=("time", "cartesian"), |
nothing calls this directly
no test coverage detected
searching dependent graphs…