# Is it possible to do basic Julia algebraic operations via juliacall?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-do-basic-julia-algebraic-operations-via-juliacall/113973>\
**Category:** General Usage\
**Tags:** juliacall\
**Created:** [May 7, 2024, 9:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-do-basic-julia-algebraic-operations-via-juliacall/113973 "2024-05-07T21:46:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 7, 2024, 9:46pm UTC](https://discourse.julialang.org/t/is-it-possible-to-do-basic-julia-algebraic-operations-via-juliacall/113973/1 "2024-05-07T21:46:50Z")

</div>

In Julia I’ve extend the basic operations (+,-,\*,/,…) to operate on GMT types. That allow for example to do:

```julia
julia> G = peaks()
A GMTgrid object with 1 layers of type Float32
Gridline node registration used
x_min: -3.0 x_max :3.0 x_inc :0.125 n_columns :49
y_min: -3.0 y_max :3.0 y_inc :0.125 n_rows :49
z_min: -6.541661739349365 z_max :8.083918571472168
Mem layout: BCB

```

```julia
julia> G * 100
A GMTgrid object with 1 layers of type Float32
Gridline node registration used
x_min: -3.0 x_max :3.0 x_inc :0.125 n_columns :49
y_min: -3.0 y_max :3.0 y_inc :0.125 n_rows :49
z_min: -654.1661739349365 z_max :808.3918571472168
Mem layout: BCB

```

But the same fails on the Python side because it is the Py multiplication that is being called instead of the Julia one.

```julia
In [1]: from juliacall import Main as jl

In [2]: jl.seval("using GMT")

In [3]: G = jl.peaks()

In [4]: G * 100
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[4], line 1
----> 1 G * 100

TypeError: unsupported operand type(s) for *: 'ArrayValue' and 'int'

```

Is it possible to tell py to let Julia do the mat? (tried a couple of things but they all failed).

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 7, 2024, 10:52pm UTC](https://discourse.julialang.org/t/is-it-possible-to-do-basic-julia-algebraic-operations-via-juliacall/113973/2 "2024-05-07T22:52:50Z")

</div>

Probably provide a custom py type (if you don’t already) [Julia to Python · PythonCall & JuliaCall](https://juliapy.github.io/PythonCall.jl/stable/conversion-to-python/#jl2py-conversion-custom) that implements ` __mul__ ` and ` __rmul__ ` to call julia. Something like

```python
  class YourGMTGridWrapper:
    def __mul__ (self,rhs):
      return jl.seval(f"{self} * {rhs}") 

```

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [May 7, 2024, 11:29pm UTC](https://discourse.julialang.org/t/is-it-possible-to-do-basic-julia-algebraic-operations-via-juliacall/113973/3 "2024-05-07T23:29:49Z")

</div>

```julia
f"{self} * {rhs}"

```

I’m not super familiar with `JuliaCall`, but won’t this call `str` on your objects to interpolate them in (in Python-land), which is probably not the right thing to do?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [May 8, 2024, 12:13am UTC](https://discourse.julialang.org/t/is-it-possible-to-do-basic-julia-algebraic-operations-via-juliacall/113973/4 "2024-05-08T00:13:56Z")

</div>

Yeah, probably not. There’s a lot of arm waving here. I wanted to point to where to override the wrapper so the operators could be implemented.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [May 8, 2024, 11:30am UTC](https://discourse.julialang.org/t/is-it-possible-to-do-basic-julia-algebraic-operations-via-juliacall/113973/5 "2024-05-08T11:30:26Z")

</div>

Thanks. Looks more complicated that it worth.
