Ask Your Question

Alan Chang's profile - activity

2019-11-10 19:31:00 +0200 received badge  Good Question (source)
2018-03-20 13:07:39 +0200 received badge  Notable Question (source)
2017-02-12 20:19:31 +0200 received badge  Popular Question (source)
2016-06-14 11:15:08 +0200 received badge  Notable Question (source)
2015-05-20 17:28:00 +0200 received badge  Popular Question (source)
2014-06-29 11:09:58 +0200 received badge  Famous Question (source)
2014-06-29 11:09:58 +0200 received badge  Notable Question (source)
2014-06-29 11:09:58 +0200 received badge  Popular Question (source)
2013-07-01 08:26:51 +0200 received badge  Good Question (source)
2013-05-16 09:54:53 +0200 received badge  Nice Question (source)
2013-05-15 12:03:41 +0200 asked a question Importing Sage functions into Cython gives me an error (but not on sagenb.org)

If I make a file (call it a.spyx) consisting of one line:

from sage.plot.plot import list_plot

and run it in the terminal with the command sage a.spyx, I get the following error

$ sage a.spyx
Compiling a.spyx...
Traceback (most recent call last):
  File "/usr/lib/sagemath/local/bin/sage-run-cython", line 9, in <module>
    eval(compile(s, tmp_filename(), 'exec'))
  File "/home/alan/.sage/temp/alan_MacBookPro/12384/tmp_sIOI1_", line 1, in <module>

  File "_home_alan_Desktop_zeta_zero_rmt_a_spyx_0.pyx", line 7, in init _home_alan_Desktop_zeta_zero_rmt_a_spyx_0 (_home_alan_Desktop_zeta_zero_rmt_a_spyx_0.c:1400)
    from sage.plot.plot import list_plot
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/plot/__init__.py", line 1, in <module>
    import all
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/plot/all.py", line 2, in <module>
    from plot import plot, graphics_array, list_plot, parametric_plot, polar_plot
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/plot/plot.py", line 367, in <module>
    from sage.ext.fast_eval import fast_float, fast_float_constant, is_fast_float
  File "fast_eval.pyx", line 90, in init sage.ext.fast_eval (sage/ext/fast_eval.c:10738)
  File "real_mpfr.pxd", line 15, in init sage.ext.fast_callable (sage/ext/fast_callable.c:16772)
  File "integer.pxd", line 9, in init sage.rings.real_mpfr (sage/rings/real_mpfr.c:33329)
  File "integer.pyx", line 178, in init sage.rings.integer (sage/rings/integer.c:38404)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/rings/infinity.py", line 203, in <module>
    import sage.rings.rational
  File "fast_arith.pxd", line 5, in init sage.rings.rational (sage/rings/rational.c:27749)
  File "fast_arith.pyx", line 51, in init sage.rings.fast_arith (sage/rings/fast_arith.c:8051)
  File "integer_ring.pyx", line 67, in init sage.rings.integer_ring (sage/rings/integer_ring.c:12156)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/structure/factorization.py", line 188, in <module>
    from sage.misc.all import prod
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/all.py", line 85, in <module>
    from functional import (additive_order,
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/functional.py", line 36, in <module>
    from sage.rings.complex_double import CDF
  File "complex_double.pyx", line 91, in init sage.rings.complex_double (sage/rings/complex_double.c:16774)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/rings/complex_field.py", line 110, in ComplexField
    C = ComplexField_class(prec)
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/rings/complex_field.py", line 202, in __init__
    ParentWithGens.__init__(self, self._real_field(), ('I',), False, category = Fields())
  File "/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/rings/complex_field.py", line 293, in _real_field
    self.__real_field = real_mpfr.RealField(self._prec)
  File "real_mpfr.pyx", line 363, in sage.rings.real_mpfr.RealField (sage/rings/real_mpfr.c:4952)
  File "real_mpfr.pyx", line 415, in sage.rings.real_mpfr.RealField (sage/rings/real_mpfr.c:4711)
TypeError: 'NoneType' object has no attribute '__getitem__'

(The same error occurs if I try to import any other Sage function.)

However, if I create a new worksheet on sagenb.org with the ... (more)

2013-05-15 02:02:17 +0200 received badge  Nice Question (source)
2013-05-14 15:28:47 +0200 received badge  Student (source)
2013-05-14 15:08:04 +0200 marked best answer Importing Sage functions into Cython?

Hi,

To import a function you need to use a syntax like

from the_module import my_function

in your case it will be

from sage.gsl.integration import numerical_integral

To find out where is what, there is a magic function in Sage

sage: import_statements(numerical_integral)
from sage.gsl.integration import numerical_integral

(Note that the code you wrote won't run faster with cython: the function call will be in Cython but what is inside numerical_integral won't be different from a python call).

2013-05-14 15:07:58 +0200 commented answer Importing Sage functions into Cython?

Thanks! Wow, import_statements() is amazing!. Yes, I realized that this wouldn't be faster. I just wanted to give a stand-alone example of calling Sage functions from Cython. (In my code, apart from Sage functions, there are other blocks which I believe would speed up with Cython.)

2013-05-14 14:52:18 +0200 received badge  Editor (source)
2013-05-14 14:46:04 +0200 asked a question Importing Sage functions into Cython?

I am playing around with Cython but I can't get it to work very well with Sage. As a very simple example of the problems I am having, the following code doesn't work. I know that the reason is that I need to import numerical_integral() from Sage somehow, but I don't know exactly how to do that.

%cython

numerical_integral(x^2, 0,1)

(This is something that's been confusing me for a few days now. I'm not sure if there's something about how Cython is supposed to be used that I am not understanding properly.)

EDIT: after trying out some things, I realized that sage.gsl.integration.numerical_integral() has the same effect as numerical_integral() in Sage. So, somehow, I should import the function from sage.gsl.integration, if that even makes any sense?

2013-05-14 13:53:02 +0200 commented answer Call to sort() returns "None"

Thank you!

2013-05-14 13:52:42 +0200 received badge  Scholar (source)
2013-05-14 13:52:42 +0200 marked best answer Call to sort() returns "None"

The method .sort() sorts the list eval_args on place (i mean it modifies the list eval_args), it does not return a sorted copy of eval_args (it returns nothing, hence your behaviour):

sage: evals = Mat(CDF,10).random_element().eigenvalues()
sage: eval_args = map(arg, evals)
sage: eval_args
[-2.88140694172,
 2.66782234593,
 -0.206748063577,
 -0.663492557653,
 -1.34361452031,
 -1.67041594451,
 -2.22990648845,
 0.643653522447,
 1.5894324166,
 1.28058620418]
sage: eval_args.sort()
sage: eval_args
[-2.88140694172,
 -2.22990648845,
 -1.67041594451,
 -1.34361452031,
 -0.663492557653,
 -0.206748063577,
 0.643653522447,
 1.28058620418,
 1.5894324166,
 2.66782234593]

As you can see, after calling eval_args.sort(), the list eval_args is sorted.

If you do not want your list eval_args to be modified and get a sorted copy of it, just use the function sorted():

sage: evals = Mat(CDF,10).random_element().eigenvalues()
sage: eval_args = map(arg, evals)
sage: eval_args
[1.77315647525,
 2.73584430085,
 0.678473279316,
 1.0691046262,
 -0.229457152027,
 -2.26775012586,
 -2.35863979204,
 -1.30207117624,
 -2.75762912811,
 -0.789539732054]
sage: sorted(eval_args)
[-2.75762912811,
 -2.35863979204,
 -2.26775012586,
 -1.30207117624,
 -0.789539732054,
 -0.229457152027,
 0.678473279316,
 1.0691046262,
 1.77315647525,
 2.73584430085]
sage: eval_args        
[1.77315647525,
 2.73584430085,
 0.678473279316,
 1.0691046262,
 -0.229457152027,
 -2.26775012586,
 -2.35863979204,
 -1.30207117624,
 -2.75762912811,
 -0.789539732054]

As you can see, the list eval_args was not modified by the function sorted(). Unfortunately, there is no .sorted() method for lists.

2013-05-14 12:33:02 +0200 asked a question Call to sort() returns "None"

I am using SAGE to calculate the arguments of the eigenvalues of a randomly-generated matrix. Here is my code.

evals = Mat(CDF,10).random_element().eigenvalues()
eval_args = map(arg, evals)
print eval_args.sort()

For some reason, the call to .sort() does not work, and what is printed is "None." Why does this happen?

2013-05-07 15:40:28 +0200 received badge  Supporter (source)