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

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

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> 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.

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).

1 Like

Probably provide a custom py type (if you don’t already) Julia to Python · PythonCall & JuliaCall that implements __mul__ and __rmul__ to call julia. Something like

  class YourGMTGridWrapper:
    def __mul__(self,rhs):
      return jl.seval(f"{self} * {rhs}") 
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?

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.

Thanks. Looks more complicated that it worth.