Opened 3 years ago

Last modified 3 years ago

#25282 needs_info defect

Add .is_prime() function for symbolic expression

Reported by: vklein Owned by: vklein
Priority: major Milestone: sage-8.4
Component: symbolics Keywords:
Cc: vdelecroix, rws Merged in:
Authors: Vincent Klein Reviewers: Vincent Delecroix
Report Upstream: N/A Work issues:
Branch: u/vklein/fix__is_prime___for_symbolic_expression (Commits, GitHub, GitLab) Commit: b46350d2c4b68d836c3b3d9e4ec8b236ca7c3807
Dependencies: Stopgaps:

Status badges

Description (last modified by vklein)

Fix the following behaviour :

sage: f(n)=n^2
sage: f(3).is_prime()
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
<ipython-input-2-9727cba91e39> in <module>()
----> 1 f(Integer(3)).is_prime()

/home/vklein/odk/sage/src/sage/structure/element.pyx in sage.structure.element.RingElement.is_prime (build/cythonized/sage/structure/element.c:19230)()
   2741         if not self:  # We exclude the 0 element
   2742             return False
-> 2743         return self._parent.ideal(self).is_prime()
   2744 
   2745 

/home/vklein/odk/sage/local/lib/python2.7/site-packages/sage/rings/ideal.pyc in is_prime(self)
    822             ass = self.associated_primes()
    823         except (NotImplementedError, ValueError):
--> 824             raise NotImplementedError
    825         if len(ass) != 1:
    826             return False

NotImplementedError:

Change History (14)

comment:1 Changed 3 years ago by vklein

  • Description modified (diff)

comment:2 Changed 3 years ago by vklein

  • Owner changed from (none) to vklein

comment:3 Changed 3 years ago by vklein

  • Summary changed from Fix .is_prime() for symbolic expression to Add .is_prime() function for symbolic expression

comment:4 Changed 3 years ago by vklein

  • Branch set to u/vklein/fix__is_prime___for_symbolic_expression

comment:5 Changed 3 years ago by vklein

  • Commit set to f46abe6c0e167d03c89c6d2f347347c3d8f0f1a2
  • Status changed from new to needs_review

comment:6 Changed 3 years ago by vklein

  • Description modified (diff)

comment:7 Changed 3 years ago by git

  • Commit changed from f46abe6c0e167d03c89c6d2f347347c3d8f0f1a2 to b46350d2c4b68d836c3b3d9e4ec8b236ca7c3807

Branch pushed to git repo; I updated commit sha1. New commits:

b46350dTrac #25282: Add is_prime() function to synmbolic ...

comment:8 Changed 3 years ago by vklein

  • Authors set to Vincent Klein

comment:9 Changed 3 years ago by vdelecroix

  • Cc vdelecroix rws added; videlec removed

comment:10 follow-up: Changed 3 years ago by vdelecroix

  • Reviewers set to Vincent Delecroix
  • Status changed from needs_review to needs_work
  1. Instead of prime number I would rather use prime integer.
  1. Because of the changes in this branch, the following
    sage: is_prime(SR(2/3))
    Traceback (most recent call last):
    ...
    TypeError: no conversion of this rational to integer
    
    becomes
    sage: is_prime(SR(2/3))
    False
    
    you need to check whether self.pyobject() is actually an integer.
  1. (possibly for a later ticket) I think that a cleaner way to proceed is to introduce a symbolic is_prime function that would also handle variables like
    sage: factorial(x)
    factorial(x)
    sage: factorial(x).subs(x=3)
    6
    
    In other words the following should ideally work
    sage: n = SR.var('n')
    sage: assume(n, 'integer')
    sage: is_prime(n)
    is_prime(n)
    sage: is_prime(n).subs(n=3)
    True
    
    You can have a look at sage.functions.other.

comment:11 in reply to: ↑ 10 ; follow-up: Changed 3 years ago by vklein

Replying to vdelecroix:

  1. Because of the changes in this branch, the following
    sage: is_prime(SR(2/3))
    Traceback (most recent call last):
    ...
    TypeError: no conversion of this rational to integer
    
    becomes
    sage: is_prime(SR(2/3))
    False
    
    you need to check whether self.pyobject() is actually an integer.

On the other hand even without this branch we have this :

sage: is_prime(2/3)
False

Shouldn't is_prime(SR(2/3)) and is_prime(2/3) behave the same way ?

The next question is if we want "This number is not an integer and therefore it is not prime" or "This number is not an integer so calling is_prime on it is an error"

comment:12 in reply to: ↑ 11 Changed 3 years ago by vdelecroix

Replying to vklein:

Replying to vdelecroix:

  1. Because of the changes in this branch, the following
    sage: is_prime(SR(2/3))
    Traceback (most recent call last):
    ...
    TypeError: no conversion of this rational to integer
    
    becomes
    sage: is_prime(SR(2/3))
    False
    
    you need to check whether self.pyobject() is actually an integer.

On the other hand even without this branch we have this :

sage: is_prime(2/3)
False

Shouldn't is_prime(SR(2/3)) and is_prime(2/3) behave the same way ?

Perhaps.

The next question is if we want "This number is not an integer and therefore it is not prime" or "This number is not an integer so calling is_prime on it is an error"

I think so, see https://groups.google.com/forum/#!topic/sage-devel/NN0VP-OVZLw (but definitely a distinct ticket)

comment:13 Changed 3 years ago by vklein

  • Status changed from needs_work to needs_info

This ticket is on hold. A discussion about what is_prime should do is in progress : How much do we support the casual user.

Last edited 3 years ago by vklein (previous) (diff)

comment:14 Changed 3 years ago by vdelecroix

  • Milestone changed from sage-8.3 to sage-8.4

milestone update 8.3 -> 8.4

Note: See TracTickets for help on using tickets.