Ask Your Question
1

Import specific functions in order to reduce the startup time

asked 2020-12-10 20:35:03 +0100

doltes gravatar image

The context

Let's suppose I want to create a simple script which only prints the prime factorization of the number 100. The script would look like

from sage.all import *
print(factor(100))

The problem

Executing that simple script takes a lot of time because there are plenty of modules that are expoted because of the from ... import statemnt.

I tried finding the file in which the function factor is defined and importing that specific module.

from sage.arith.misc import factor
print(factor(100))

However, this yields the following error.

Traceback (most recent call last):
  File "/home/myusername/Experiments/factor-100.py", line 1, in <module>
    from sage.arith.misc import factor
  File "/usr/lib/python3.9/site-packages/sage/arith/misc.py", line 23, in <module>
    from sage.libs.flint.arith import (bernoulli_number as flint_bernoulli,
  File "sage/libs/flint/arith.pyx", line 1, in init sage.libs.flint.arith (build/cythonized/sage/libs/flint/arith.c:5015)
  File "sage/rings/integer.pyx", line 1, in init sage.rings.integer (build/cythonized/sage/rings/integer.c:54202)
  File "sage/rings/rational.pyx", line 95, in init sage.rings.rational (build/cythonized/sage/rings/rational.c:40423)
  File "sage/rings/real_mpfr.pyx", line 1, in init sage.rings.real_mpfr (build/cythonized/sage/rings/real_mpfr.c:45462)
  File "sage/libs/mpmath/utils.pyx", line 1, in init sage.libs.mpmath.utils (build/cythonized/sage/libs/mpmath/utils.c:9058)
  File "sage/rings/complex_number.pyx", line 1, in init sage.rings.complex_number (build/cythonized/sage/rings/complex_number.c:26879)
  File "sage/rings/complex_double.pyx", line 98, in init sage.rings.complex_double (build/cythonized/sage/rings/complex_double.c:25209)
ImportError: cannot import name complex_number

I can fix this problem by adding the following import rule.

from sage.misc.all import *
from sage.arith.misc import factor
print(factor(100))

However, this also loads a lot of modules.

The question

Is it possible to import less modules than the last code block shown?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2020-12-11 04:43:49 +0100

This works for me:

from sage.rings.complex_double import CDF
from sage.arith.misc import factor
print(factor(100))
edit flag offensive delete link more

Comments

One could also use:

>>> from sage.rings.complex_double import CDF
>>> from sage.rings.integer_ring import ZZ
>>> ZZ(100).factor()
2^2 * 5^2

Do you know why importing CDF is required? Could that be changed?

slelievre gravatar imageslelievre ( 2020-12-11 11:04:38 +0100 )edit

I tried both of your solutions but both of them throws this error (https://pastebin.com/9QTuYWA5). I am using SageMath version 9.2. I installed it by using pacman in Arch Linux.

doltes gravatar imagedoltes ( 2020-12-11 17:46:54 +0100 )edit

@slelievre: I think that it's required because of import statements in sage.arith.misc or in sage.rings.integer, but I'm not sure.

John Palmieri gravatar imageJohn Palmieri ( 2020-12-11 19:26:00 +0100 )edit
0

answered 2020-12-12 01:29:20 +0100

slelievre gravatar image

updated 2020-12-12 01:36:50 +0100

If the goal is a script to factor integers and you need speed, look into using a specialised number theory library directly, such as FLINT, NTL or PARI/GP.

It's probably overkill to start Sage just for that.

Or as an intermediate solution, use SymPy, e.g. run this in a shell:

$ python3 -c "from sympy import factorint ; print(factorint(100))"
{2: 2, 5: 2}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-12-10 20:35:03 +0100

Seen: 466 times

Last updated: Dec 12 '20