Skip to content

Compute the row-wise dot product with another Array expression

Source code

Description

Both inputs must contain arrays of the same width. Their inner data types are cast to a common supertype, which must be Float32 or Float64. An input with one row is broadcast against the other input. If either input array is null for a row, the result for that row is null.

Usage

<Expr>$arr$dot(other)

Arguments

other Array expression or query vector to compute the dot product with. A one-row input is broadcast against the other input.

Details

Elements are paired by position. Pairs where either element is null do not contribute to the sum. If a non-null row has no pairs where both elements are valid, the result is 0.

Value

A polars expression

Examples

library("polars")

df <- pl$DataFrame(
  a = list(c(1, 2), c(3, 4)),
  b = list(c(5, 6), c(7, 8))
)$cast(pl$Array(pl$Float64, 2))

df$with_columns(dot = pl$col("a")$arr$dot("b"))
#> shape: (2, 3)
#> ┌───────────────┬───────────────┬──────┐
#> │ a             ┆ b             ┆ dot  │
#> │ ---           ┆ ---           ┆ ---  │
#> │ array[f64, 2] ┆ array[f64, 2] ┆ f64  │
#> ╞═══════════════╪═══════════════╪══════╡
#> │ [1.0, 2.0]    ┆ [5.0, 6.0]    ┆ 17.0 │
#> │ [3.0, 4.0]    ┆ [7.0, 8.0]    ┆ 53.0 │
#> └───────────────┴───────────────┴──────┘