ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 25 Jun 2019 12:27:38 -0500Creating a module in Sagehttp://ask.sagemath.org/question/46994/creating-a-module-in-sage/I am new to Sage, being used to Python and I am having some trouble with adapting.
In particular, I am trying to create a 'module' in the Python sense, i.e. some set of classes that I can call with 'import'.
When I did this in python in the past, I would create a directory (say /my_module/ in my home) and include a file __'__init____.sage' in that file with a line
__all__ = ['Submodule1', 'Submodule2']
Then I would have a file 'Submodule1' and 'Submodule2'.
In the file 'Submodule 1' I would define 'Class1'
and then from a file 'run.py' in home I would write
import my_module
from my_module.Submodule1 import Class1
and then I would be able to write
c=Class1()
to create an object.
I find that this is not working in Sage and the only thing I seem to be able to do is write in run.py
load("my_module/Submodule1.sage")
for each class I want to load. This has many disadvantages, for instance hiding classes from potential users so that they only exist internally.
Any suggestions on how to create and import modules in sage? What is the sage way?
Jesus Martinez GarciaTue, 25 Jun 2019 12:27:38 -0500http://ask.sagemath.org/question/46994/Is it possible to use Arb-library commands directly in Sagemath?http://ask.sagemath.org/question/41811/is-it-possible-to-use-arb-library-commands-directly-in-sagemath/ I would like to use some commands/functions from the high performance/precision [Arb-library](http://arblib.org) that in turn builds on the Flint-library. Searching the web, I did find references that some commands from both libraries already underpin directly or indirectly the Sagemath environment, however basic commands like for instance:
arb.pi()
don't seem to be supported (or better, I couldn't get them to work yet).
What I am after is to use Flint and Arb functions within Python like in [this example](http://fredrikj.net/blog/2015/01/arb-and-flint-in-python/), but then under the Sagemath umbrella. Is this already possible?
Thanks.RuudHWed, 28 Mar 2018 17:43:30 -0500http://ask.sagemath.org/question/41811/using sage library in C or C++?http://ask.sagemath.org/question/39196/using-sage-library-in-c-or-c/ Is there a way to run sage library in C/C++ or include the sage in C/C++? For example, I want to calculate minkowski sum, difference or decomposition in C or C++? studentboyTue, 17 Oct 2017 10:57:44 -0500http://ask.sagemath.org/question/39196/how to use sage in PyCharm on CoCalchttp://ask.sagemath.org/question/39262/how-to-use-sage-in-pycharm-on-cocalc/How can I use Sage library in PyCharm IDE like on CoCalc? studentboyTue, 24 Oct 2017 04:15:47 -0500http://ask.sagemath.org/question/39262/sage LD_LIBRARY_PATHhttp://ask.sagemath.org/question/38842/sage-ld_library_path/ I wrote a C++ library and the related python bindings, and I would like to make use of them from sage. But whatever I try, I cannot get sage to load the library. Here is what I tried:
1) FIRST ATTEMPT
export SAGE_PATH="$SAGE_PATH:/opt/mylib/bindings"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/opt/mylib/lib::/opt/mylib/dependencies"
However, when I try to import from a jupyter notebook:
import mylib.mymodule.mysubmodule
I get the error:
ImportError: /usr/lib/libblas.so.3: undefined symbol: sgemv_thread_n
I only could find one reference (ticket 22006 on trac.sagemath.org - sorry, cannot post links yet) to a similar problem, however without resolution (the suggested workaround, namely to install `libopenblas-dev` did not work for me). Note that `mylib` does not depend in any way on BLAS.
2) SECOND ATTEMPT
export SAGE_PATH="$SAGE_PATH:/opt/mylib/bindings"
echo -e "/opt/mylib/lib\n/opt/mylib/dependencies" > /etc/ld.so.conf/mylib.conf
sudo ldconfig
But the library cannot be found this way:
ImportError: libmylib.so.1: cannot open shared object file: No such file or directory
3) THIRD ATTEMPT
export SAGE_PATH="$SAGE_PATH:/opt/mylib/bindings"
sudo ln -s /opt/mylib/lib/libmylib.so.1 /usr/lib/sagemath/local/lib/libmylib.so.1
sudo ln -s /opt/mylib/lib/libmylib.so.1 /usr/local/lib/libmylib.so.1
This results in the same import error as in SECOND ATTEMPT.
4) FOURTH ATTEMPT
export SAGE_PATH="$SAGE_PATH:/opt/mylib/bindings"
sudo ln -s /opt/mylib/lib/libmylib.so.1 /lib/libmylib.so.1
Now the library finally got loaded, but it cannot find its dependencies.
ImportError: libdependency1.so.1.2.3: cannot open shared object file: No such file or directory
THE QUESTION
What is the proper way to make sage resolve a custom library and its dependencies? I understand that sage tries to isolate itself as much as possible from the system, and I have also read that there is some "magic" going on to adjust environment variables: this seems to cause the problem (1).
ADDITIONAL INFO
Working on Ubuntu 16.04 LTS with sage installed from ppa as follows:
apt-add-repository -y ppa:aims/sagemath
apt-get update
apt install sagemath-upstream-binary
Note that everything works as expected when using the library from the system python console.matpenMon, 18 Sep 2017 03:15:57 -0500http://ask.sagemath.org/question/38842/import CSV in python using jupyter notebook on cocalc cloudhttp://ask.sagemath.org/question/38546/import-csv-in-python-using-jupyter-notebook-on-cocalc-cloud/I want to import this [CVS file](https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data) in my python script with the commands:
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
dataset = pandas.read_csv(url, names=names)
The problem is that it returns this error message:
---------------------------------------------------------------------------
URLError Traceback (most recent call last)
<ipython-input-21-45b1e7974739> in <module>()
32 url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
33 names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
---> 34 dataset = pandas.read_csv(url, names=names)
/projects/sage/sage-7.6/local/lib/python2.7/site-packages/pandas/io/parsers.py in parser_f(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, escapechar, comment, encoding, dialect, tupleize_cols, error_bad_lines, warn_bad_lines, skipfooter, skip_footer, doublequote, delim_whitespace, as_recarray, compact_ints, use_unsigned, low_memory, buffer_lines, memory_map, float_precision)
653 skip_blank_lines=skip_blank_lines)
654
--> 655 return _read(filepath_or_buffer, kwds)
656
657 parser_f.__name__ = name
/projects/sage/sage-7.6/local/lib/python2.7/site-packages/pandas/io/parsers.py in _read(filepath_or_buffer, kwds)
390 compression = _infer_compression(filepath_or_buffer, compression)
391 filepath_or_buffer, _, compression = get_filepath_or_buffer(
--> 392 filepath_or_buffer, encoding, compression)
393 kwds['compression'] = compression
394
/projects/sage/sage-7.6/local/lib/python2.7/site-packages/pandas/io/common.py in get_filepath_or_buffer(filepath_or_buffer, encoding, compression)
184 if _is_url(filepath_or_buffer):
185 url = str(filepath_or_buffer)
--> 186 req = _urlopen(url)
187 content_encoding = req.headers.get('Content-Encoding', None)
188 if content_encoding == 'gzip':
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in urlopen(url, data, timeout, cafile, capath, cadefault, context)
152 else:
153 opener = _opener
--> 154 return opener.open(url, data, timeout)
155
156 def install_opener(opener):
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in open(self, fullurl, data, timeout)
427 req = meth(req)
428
--> 429 response = self._open(req, data)
430
431 # post-process response
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in _open(self, req, data)
445 protocol = req.get_type()
446 result = self._call_chain(self.handle_open, protocol, protocol +
--> 447 '_open', req)
448 if result:
449 return result
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in _call_chain(self, chain, kind, meth_name, *args)
405 func = getattr(handler, meth_name)
406
--> 407 result = func(*args)
408 if result is not None:
409 return result
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in https_open(self, req)
1239 def https_open(self, req):
1240 return self.do_open(httplib.HTTPSConnection, req,
-> 1241 context=self._context)
1242
1243 https_request = AbstractHTTPHandler.do_request_
/projects/sage/sage-7.6/local/lib/python/urllib2.pyc in do_open(self, http_class, req, **http_conn_args)
1196 except socket.error, err: # XXX what error?
1197 h.close()
-> 1198 raise URLError(err)
1199 else:
1200 try:
URLError: <urlopen error [Errno 110] Connection timed out>
What's the problem?
How could I solve?
I use a free account on CoCalc.
If it takes time to upload and so not enough for my free account, how I can upload the file as worksheet and link it? What will be the link in the URL specification?
ThankspspWed, 16 Aug 2017 09:40:27 -0500http://ask.sagemath.org/question/38546/How to use libraries installed in for Python in Sage?http://ask.sagemath.org/question/32321/how-to-use-libraries-installed-in-for-python-in-sage/ I've installed some libraries to the 'normal' or 'system' Python on my computer.
However, Sage cannot see them.
How can I get Sage to use the libraries installed for Python? I'd prefer not to double install for Python and Sage separately.
---
I've noticed that Sage uses some Python from the Sage package. Which is clearly different than the one I'm using as "system Python". Is there a way to make these the same?mavaviljSat, 23 Jan 2016 11:08:10 -0600http://ask.sagemath.org/question/32321/Using from Python (breaking the monolith)http://ask.sagemath.org/question/25964/using-from-python-breaking-the-monolith/Sage contains a many libraries (or Cython wrappers to libraries) that would be useful in other Python programs (to me anyway). So I would like to use some of them isolated from within Python programs. I understand that this is not easily possible. Is it still possible at all? This is actually not so much about how to use Sage from Python but about carving pieces out of the monolithic structure of Sage. (At the moment it seems like I have to implement all kinds of stuff from scratch which seems silly since much of it is already there.)
Concretely: Among other things I want to compute with matrices that have entries in rings of Laurent series over finite fields.
Now you will say: just write a Sage script rather than a Python script. So here are my reasons to prefer Python with isolated libraries (in increasing order of importance):
1. Practical: putting the full 1.5 GB (?) of sage in home on my office computer pushes me beyond the quota limit.
2. Extendability: Not everything I need is available in Sage. But if I want to extend the code I will have do wade through the Sage code making changes here and there and then defend them against updates (and maybe adjust the build process). This seems like a lot of hassle.
3. Verifiability: At some point my code will (hopefully) produce some solutions to some problem and I will want to say (maybe in a published paper) "these are solutions and they are all solutions to the problem". Then critical minds will not believe me and all I can say is: "Well, you only have to check the correctness of my 200 lines of code (say). -- Plus of course everything in the Sage git repository, plus the libraries it depends on (at least the part that my code uses, which you will have to work out for yourself); and make sure to patch in these changes I made here and there!"Stefan WitzelSat, 28 Feb 2015 03:34:49 -0600http://ask.sagemath.org/question/25964/can additional ipython libraries be installed? I look for mapq and wekahttp://ask.sagemath.org/question/10870/can-additional-ipython-libraries-be-installed-i-look-for-mapq-and-weka/I would love to use mapq and weka and actually networkX on your iPython notebooks, can they be installed please? Thank youArendHintzeMon, 30 Dec 2013 10:04:23 -0600http://ask.sagemath.org/question/10870/glibc 2.17 dependencyhttp://ask.sagemath.org/question/10259/glibc-217-dependency/Running Xubuntu 12.04 on an HP Compaq nc8430. 2 GB RAM. I installed Sage 5.9. I can get into the notebook interface and do some algebra and calculus. However, when I try to plot something, I get errors. Here's the traceback:
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> File "_sage_input_2.py", line 10, in
> <module>
> exec compile(u'open("___code___.py","w").write("#
> -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("Y2lyY2xlKCgwLDApLCAxLCByZ2Jjb2xvcj0oMSwxLDApKQ=="),globals())+"\\n");
> execfile(os.path.abspath("___code___.py"))
> File "", line 1, in <module>
> File "/tmp/tmp1iswRL/___code___.py", line
> 3, in <module>
> exec compile(u'circle((_sage_const_0
> ,_sage_const_0 ), _sage_const_1 ,
> rgbcolor=(_sage_const_1 ,_sage_const_1
> ,_sage_const_0 )) File "", line 1,
> in <module>
> File "sage_object.pyx", line 154, in
> sage.structure.sage_object.SageObject.__repr__
> (sage/structure/sage_object.c:1897)
> File
> "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/sage/plot/graphics.py",
> line 825, in _repr_
> self.show() File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/sage/misc/decorators.py",
> line 456, in wrapper
> return func(*args, **kwds) File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/sage/plot/graphics.py",
> line 1793, in show
> self.save(**kwds) File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/sage/misc/decorators.py",
> line 456, in wrapper
> return func(*args, **kwds) File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/sage/plot/graphics.py",
> line 2668, in save
> figure = self.matplotlib(**options) File
> "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/sage/plot/graphics.py",
> line 2170, in matplotlib
> from matplotlib.figure import Figure File
> "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/matplotlib/__init__.py",
> line 133, in <module>
> from matplotlib.rcsetup import (defaultParams, File
> "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/matplotlib/rcsetup.py",
> line 19, in <module>
> from matplotlib.colors import is_color_like File
> "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/matplotlib/colors.py",
> line 52, in <module>
> import numpy as np File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/numpy/__init__.py",
> line 136, in <module>
> import add_newdocs File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/numpy/add_newdocs.py",
> line 9, in <module>
> from numpy.lib import add_newdoc File
> "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/numpy/lib/__init__.py",
> line 13, in <module>
> from polynomial import * File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/numpy/lib/polynomial.py",
> line 17, in <module>
> from numpy.linalg import eigvals, lstsq File
> "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/numpy/linalg/__init__.py",
> line 48, in <module>
> from linalg import * File "/opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/python2.7/site-packages/numpy/linalg/linalg.py",
> line 23, in <module>
> from numpy.linalg import lapack_lite ImportError:
> /lib/i386-linux-gnu/libc.so.6: version
> `GLIBC_2.17' not found (required by
> /opt/sage-5.9-linux-32bit-ubuntu_13.04-i686-Linux/local/lib/libgfortran.so.3)
What's troubling me a bit is that I'm seeing some ubuntu 13.04 stuff in there. That's not what I'm running. Did I install a version of Sage that's not compatible with Xubuntu 12.04? It says it wants glibc 2.17. The installation procedure for that package seems ridiculously complex. I feel that Sage should have installed that package if it needed it. AckbachWed, 19 Jun 2013 11:07:54 -0500http://ask.sagemath.org/question/10259/local access to sage librarieshttp://ask.sagemath.org/question/9877/local-access-to-sage-libraries/how and where i can get all libraries for local development in my pc.
the develop is in c# and i need full and local access to sage libraries. sergMon, 04 Mar 2013 21:54:50 -0600http://ask.sagemath.org/question/9877/import sage packages in pythonhttp://ask.sagemath.org/question/9302/import-sage-packages-in-python/An easy way to use sage in python files is demonstrated in the [Sage Tutorial](http://www.sagemath.org/doc/tutorial/programming.html).
#!/usr/bin/env sage -python
import sys
from sage.all import *
if len(sys.argv) != 2:
print "Usage: %s <n>"%sys.argv[0]
print "Outputs the prime factorization of n."
sys.exit(1)
print factor(sage_eval(sys.argv[1]))
Well, what if I don't want to import *all* of sage as shown above using:
from sage.all import *
Instead of this command above, I just want to import the following:
1. Matrix -> type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'
2. vector -> type 'sage.modules.vector_integer_dense.Vector_integer_dense'
3. ZZ -> type 'sage.rings.integer_ring.IntegerRing_class'
4. MixedIntegerLinearProgram -> type 'sage.numerical.mip.MixedIntegerLinearProgram'
So I should be able to write something like this in python
from sage.library.package.for.Matrix import *
from sage.library.package.for.vector import *
from sage.library.package.for.ZZ import *
from sage.library.package.for.MixedIntegerLinearProgram import *
I just don't know what they are. Any help is appreciated.
Thanks.MaelstromYamatoThu, 06 Sep 2012 06:29:57 -0500http://ask.sagemath.org/question/9302/Using Psycopg2 and other libraries in sagehttp://ask.sagemath.org/question/8812/using-psycopg2-and-other-libraries-in-sage/Hi,
Seems like a simple question but have not found a good answer. Best way to use outside python libraries in SAGE in Ubuntu. Install in SAGE foulder if so (how?). Link in an IDE. I know how to do this in Pydev.
Thanks,
Matthewmcm3Wed, 21 Mar 2012 20:20:21 -0500http://ask.sagemath.org/question/8812/