ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 12 Dec 2020 01:29:20 +0100Import specific functions in order to reduce the startup timehttps://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/# 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?
Thu, 10 Dec 2020 20:35:03 +0100https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/Answer by John Palmieri for <h1>The context</h1>
<p>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</p>
<pre><code>from sage.all import *
print(factor(100))
</code></pre>
<h1>The problem</h1>
<p>Executing that simple script takes a lot of time because there are plenty of modules that are expoted because of the <code>from ... import</code> statemnt.</p>
<p>I tried finding the file in which the function <code>factor</code> is defined and importing that specific module.</p>
<pre><code>from sage.arith.misc import factor
print(factor(100))
</code></pre>
<p>However, this yields the following error.</p>
<pre><code>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
</code></pre>
<p>I can fix this problem by adding the following <code>import</code> rule.</p>
<pre><code>from sage.misc.all import *
from sage.arith.misc import factor
print(factor(100))
</code></pre>
<p>However, this also loads a lot of modules.</p>
<h1>The question</h1>
<p>Is it possible to import less modules than the last code block shown?</p>
https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?answer=54634#post-id-54634This works for me:
from sage.rings.complex_double import CDF
from sage.arith.misc import factor
print(factor(100))
Fri, 11 Dec 2020 04:43:49 +0100https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?answer=54634#post-id-54634Comment by slelievre for <p>This works for me:</p>
<pre><code>from sage.rings.complex_double import CDF
from sage.arith.misc import factor
print(factor(100))
</code></pre>
https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?comment=54635#post-id-54635One 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?Fri, 11 Dec 2020 11:04:38 +0100https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?comment=54635#post-id-54635Comment by doltes for <p>This works for me:</p>
<pre><code>from sage.rings.complex_double import CDF
from sage.arith.misc import factor
print(factor(100))
</code></pre>
https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?comment=54639#post-id-54639I 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.Fri, 11 Dec 2020 17:46:54 +0100https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?comment=54639#post-id-54639Comment by John Palmieri for <p>This works for me:</p>
<pre><code>from sage.rings.complex_double import CDF
from sage.arith.misc import factor
print(factor(100))
</code></pre>
https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?comment=54651#post-id-54651@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.Fri, 11 Dec 2020 19:26:00 +0100https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?comment=54651#post-id-54651Answer by slelievre for <h1>The context</h1>
<p>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</p>
<pre><code>from sage.all import *
print(factor(100))
</code></pre>
<h1>The problem</h1>
<p>Executing that simple script takes a lot of time because there are plenty of modules that are expoted because of the <code>from ... import</code> statemnt.</p>
<p>I tried finding the file in which the function <code>factor</code> is defined and importing that specific module.</p>
<pre><code>from sage.arith.misc import factor
print(factor(100))
</code></pre>
<p>However, this yields the following error.</p>
<pre><code>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
</code></pre>
<p>I can fix this problem by adding the following <code>import</code> rule.</p>
<pre><code>from sage.misc.all import *
from sage.arith.misc import factor
print(factor(100))
</code></pre>
<p>However, this also loads a lot of modules.</p>
<h1>The question</h1>
<p>Is it possible to import less modules than the last code block shown?</p>
https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?answer=54654#post-id-54654If 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}Sat, 12 Dec 2020 01:29:20 +0100https://ask.sagemath.org/question/54632/import-specific-functions-in-order-to-reduce-the-startup-time/?answer=54654#post-id-54654