Skip to content
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

The exported signature of isprime and prime is not useful #410

Open
melonedo opened this issue Feb 5, 2021 · 1 comment
Open

The exported signature of isprime and prime is not useful #410

melonedo opened this issue Feb 5, 2021 · 1 comment

Comments

@melonedo
Copy link

melonedo commented Feb 5, 2021

The exported signature of these two functions are

isprime(ex::SymbolicObject, args...; kwargs...)
prime(ex::SymbolicObject, args...; kwargs...)

However, from its python source code, sympy.prime only accepts integers, and errors if given expressions:

julia> sympy.prime(1)
2

julia> prime(1)
ERROR: MethodError: no method matching prime(::Int64)
Closest candidates are:
  prime(::SymPy.SymbolicObject, ::Any...; kwargs...) at C:\Users\melonedo\.julia\packages\SymPy\4256i\src\importexport.jl:124

julia> @vars s
(s,)

julia> prime(s)
ERROR: PyError ($(Expr(:escape, :(ccall(#= C:\Users\melonedo\.julia\packages\PyCall\tqyST\src\pyfncall.jl:43 =# @pysym(:PyObject_Call), PyPtr, (PyPtr, PyPtr, PyPtr), o, pyargsptr, kw))))) <class 'ValueError'>
ValueError('s is not an integer',)
  File "C:\Users\melonedo\.julia\conda\3\lib\site-packages\sympy\ntheory\generate.py", line 368, in prime
    n = as_int(nth)
  File "C:\Users\melonedo\.julia\conda\3\lib\site-packages\sympy\core\compatibility.py", line 425, in as_int
    raise ValueError('%s is not an integer' % (n,))

sympy.isprime is only meaningful for integers, and returns false for expression:

julia> sympy.isprime(7)
true

julia> isprime(7)
ERROR: MethodError: no method matching isprime(::Int64)
Closest candidates are:
  isprime(::SymPy.SymbolicObject, ::Any...; kwargs...) at C:\Users\melonedo\.julia\packages\SymPy\4256i\src\importexport.jl:109

julia> isprime(s)
false
@jverzani
Copy link
Collaborator

jverzani commented Feb 5, 2021

Yup, that is the case. Some of the functions naturally expect numbers, not symbolic objects. There are then two ways to use these functions

  • As you say, qualifying, as in sympy.isprime
  • or passing in a symbolic integer, as in isprime(Sym(7)) or isprime(sympify(7)). This would be useful when the value passed is the result of a symbolic computation
    Here is a made up example where both usages might be of interest:
@vars n
mersenne = 2^n - 1
[isprime(mersenne(n=>i)) for i ∈ 2:100 if isprime(Sym(i))]

Now the particular function isprime has a history. Many moons ago, isprime was in base, so it made sense to extend it to symbolic objects. The usage isprime(11) then fell back to a julia-specific defn. Then base got cleaned up and the Primes package was created. To keep dependencies down this is not a dependency of SymPy and so loading

using Primes

now complains about a name conflict with isprime. If you were using this often, it would be kind of annoying. At that point three things could be considered (removing isprime here so it is always qualified, importing Primes -- requiring qualifying that usage, or adding Primes as a dependency.)

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