# Missing operators implementations ###### tags: `archive` ## Bitwise operations Negation through the use of `tilde` (~) ```python= def bitwise_negation(cond: boolean, inp0: Field, inp1: Field): result = inp0 if ~cond else inp1 return result ``` Implementationg of XOR with use of `apostrophe` (^) ```python= def bitwise_xor(cond1: boolean, cond2: boolean, inp0: Field, inp1: Field): result = inp0 if cond1^cond2 else inp1 return result ``` HV: For field of booleans only, right? Makes sense. NF: Correct, booleans or fields of booleans ## Basic trigonometric operations - all already implemented (it seems) Most importantly: sin, cosin, tan ```python= def sin_imp(inp1: Field): return sin(inp1) ``` HV: here is the list we want to support https://github.com/GridTools/gt4py/blob/adec0c3bf36fd2660ee766ec38114733672150ad/src/gt4py/gtscript.py#L39 NF: Good point! I didn't think about including this list in the doc ## Implement tests for all math builtins A lot of math builtins such as log and exp are not tested anywhere ## Augmented assignments This should work for primary BinaryOperators: +=, -=, *=, /= ```python= def aug_add(inp1: Field): inp1 += 1 return inp1 ``` HV: In general, we don't want to support them because it obfuscates the functional language. Augmented assigns look like you are updating the left hand side, but all we can do is return a new object with the result of `inp1 + 1`. NF: makes sense ## Logarithmic operators This should include log2 and log10 ```python= def log2_imp(inp1: Field): return log2(inp1) ``` ```python= def log10_imp(inp1: Field): return log10(inp1) ``` HV: That means adding them to the frontend as syntax sugar, right? NF: Yes, it does ## Matrix multiplication operator Basic Python (from 3.5 onwards) uses the `at` (@) for matrix multiplication: A = np.asarray([[1, 2], [3, 4]]) B = np.asarray([[11, 12], [13, 14]]) A @ B = array([[1 * 11 + 2 * 13, 1 * 12 + 2 * 14], [3 * 11 + 4 * 13, 3 * 12 + 4 * 14]]) A @ B = array([[37, 40], [85, 92]]) Likewise in gt4py: ```python= def matrix_mult(inp0: Field, inp1: Field): return inp0@inp1 ``` Here fields need to have the same dimensions HV: I don't think we want this in the way you describe, but instead we want to add matrix/vector operations acting on (matrix/vector-valued) elements of the field. NF: Ok, we can discuss this further once it's time ## Neighbor reductions As extensions of neighbor_sum, this would include neighbor reductions functions for primary BinaryOperators: ```python= @field_operator def sum_cells(inp0: Field[[CellDim], float]) -> Field[[EdgeDim], float]: return neighbor_sum(inp0(E2C), axis=E2CDim) ``` Likewise, we may want: neighbor_sub, neighbor_mult, neighbor_div HV: Yes, we want a general reduce and then we can implement all of this as library functions. There are open issues we want to resolve before we start that https://github.com/GridTools/gt4py/issues/827. NF: Good point, I should have checked the already existing issues. Thanks for pointing it out