1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use num_traits::{Num, NumCast};
use polars_error::{polars_bail, PolarsResult};

use super::{Column, ScalarColumn, Series};
use crate::utils::Container;

fn output_length(a: &Column, b: &Column) -> PolarsResult<usize> {
    match (a.len(), b.len()) {
        // broadcasting
        (1, o) | (o, 1) => Ok(o),
        // equal
        (a, b) if a == b => Ok(a),
        // unequal
        (a, b) => {
            polars_bail!(InvalidOperation: "cannot do arithmetic operation on series of different lengths: got {} and {}", a, b)
        },
    }
}

fn unit_series_op<F: Fn(&Series, &Series) -> PolarsResult<Series>>(
    l: &Series,
    r: &Series,
    op: F,
    length: usize,
) -> PolarsResult<Column> {
    debug_assert!(l.len() <= 1);
    debug_assert!(r.len() <= 1);

    op(l, r)
        .and_then(|s| ScalarColumn::from_single_value_series(s, length))
        .map(Column::from)
}

fn op_with_broadcast<F: Fn(&Series, &Series) -> PolarsResult<Series>>(
    l: &Column,
    r: &Column,
    op: F,
) -> PolarsResult<Column> {
    // Here we rely on the underlying broadcast operations.

    let length = output_length(l, r)?;
    match (l, r) {
        (Column::Series(l), Column::Series(r)) => op(l, r).map(Column::from),
        (Column::Series(l), Column::Scalar(r)) => {
            let r = r.as_single_value_series();
            if l.len() == 1 {
                unit_series_op(l, &r, op, length)
            } else {
                op(l, &r).map(Column::from)
            }
        },
        (Column::Scalar(l), Column::Series(r)) => {
            let l = l.as_single_value_series();
            if r.len() == 1 {
                unit_series_op(&l, r, op, length)
            } else {
                op(&l, r).map(Column::from)
            }
        },
        (Column::Scalar(l), Column::Scalar(r)) => unit_series_op(
            &l.as_single_value_series(),
            &r.as_single_value_series(),
            op,
            length,
        ),
    }
}

fn num_op_with_broadcast<T: Num + NumCast, F: Fn(&Series, T) -> Series>(
    c: &'_ Column,
    n: T,
    op: F,
) -> PolarsResult<Column> {
    match c {
        Column::Series(s) => Ok(op(s, n).into()),
        Column::Scalar(s) => {
            ScalarColumn::from_single_value_series(op(&s.as_single_value_series(), n), s.length)
                .map(Column::from)
        },
    }
}

macro_rules! broadcastable_ops {
    ($(($trait:ident, $op:ident))+) => {
        $(
        impl std::ops::$trait for Column {
            type Output = PolarsResult<Column>;

            #[inline]
            fn $op(self, rhs: Self) -> Self::Output {
                op_with_broadcast(&self, &rhs, |l, r| l.$op(r))
            }
        }

        impl std::ops::$trait for &Column {
            type Output = PolarsResult<Column>;

            #[inline]
            fn $op(self, rhs: Self) -> Self::Output {
                op_with_broadcast(self, rhs, |l, r| l.$op(r))
            }
        }
        )+
    }
}

macro_rules! broadcastable_num_ops {
    ($(($trait:ident, $op:ident))+) => {
        $(
        impl<T> std::ops::$trait::<T> for Column
        where
            T: Num + NumCast,
        {
            type Output = PolarsResult<Self>;

            #[inline]
            fn $op(self, rhs: T) -> Self::Output {
                num_op_with_broadcast(&self, rhs, |l, r| l.$op(r))
            }
        }

        impl<T> std::ops::$trait::<T> for &Column
        where
            T: Num + NumCast,
        {
            type Output = PolarsResult<Column>;

            #[inline]
            fn $op(self, rhs: T) -> Self::Output {
                num_op_with_broadcast(self, rhs, |l, r| l.$op(r))
            }
        }
        )+
    };
}

broadcastable_ops! {
    (Add, add)
    (Sub, sub)
    (Mul, mul)
    (Div, div)
    (Rem, rem)
    (BitAnd, bitand)
    (BitOr, bitor)
    (BitXor, bitxor)
}

broadcastable_num_ops! {
    (Add, add)
    (Sub, sub)
    (Mul, mul)
    (Div, div)
    (Rem, rem)
}