Skip to content

Don't understand how to pass numpy arrays to Julia function #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arbenede opened this issue Jul 14, 2021 · 2 comments
Closed

Don't understand how to pass numpy arrays to Julia function #28

arbenede opened this issue Jul 14, 2021 · 2 comments

Comments

@arbenede
Copy link

Let's consider the code fragment

import numpy as np

X = np.ones((10,10))
Y = np.ones((10,10))

x, y = jl.MyModule.compute_something(X, Y, -1.0, 1.0, -1.0, 1.0)

where I call a Julia function defined in the MyModule module. I am getting the error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Julia: MethodError: no method matching compute_something(::PythonCall.PyArray{Float64, 2, Float64, true, false}, ::PythonCall.PyArray{Float64, 2, Float64, true, false}, ::Float64, ::Float64, ::Float64, ::Float64)
Closest candidates are:
  compute_something(!Matched::Matrix{T}, !Matched::Matrix{T}, ::T, ::T, ::T, ::T) where T<:Real at /opt/julia/compute_something:362

Does this mean that I need to define a new method for compute_something() with ::PythonCall.PyArray arguments? Can I avoid doing that? From the documentation it is not clear what to do in this situation.

@arbenede
Copy link
Author

I think I figured it out:

x, y = jl.MyModule.compute_something(jl.Matrix[jl.Float64](target), jl.Matrix[jl.Float64](reference), -5.0, 5.0, -5.0, 5.0)

@cjdoris
Copy link
Collaborator

cjdoris commented Jul 15, 2021

That's one way. Probably a better way is to change the signature of your function to

compute_something(::AbstractMatrix{T}, ::AbstractMatrix{T}, ::T, ::T, ::T, ::T) where {T<:Real}

so that it can take any matrix type. There are lots of matrix types in Julia (see subtypes(AbstractMatrix)) of which Matrix is only one, and your function probably doesn't care what kind of matrix it got.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants