Ask Your Question
1

import sage packages in python

asked 2012-09-06 13:29:57 +0100

MaelstromYamato gravatar image

updated 2012-09-06 14:02:41 +0100

kcrisman gravatar image

An easy way to use sage in python files is demonstrated in the Sage Tutorial.

#!/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.

edit retag flag offensive close merge delete

Comments

1

I'd be really careful with this. The cascade of imports is very convoluted, and there isn't any guarantee that importing something like `ZZ` won't import most of the rest of Sage. It may not be worth the effort.

kcrisman gravatar imagekcrisman ( 2012-09-06 14:03:39 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2012-09-06 14:08:28 +0100

updated 2012-09-06 18:14:35 +0100

  1. Matrix -> type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'

    from sage.matrix.all import Matrix

    or

    from sage.matrix.constructor import Matrix

  2. vector -> type 'sage.modules.vector_integer_dense.Vector_integer_dense'

    from sage.modules.free_module_element import vector

  3. ZZ -> type 'sage.rings.integer_ring.IntegerRing_class'

    from sage.rings.integer_ring import ZZ

  4. MixedIntegerLinearProgram -> type 'sage.numerical.mip.MixedIntegerLinearProgram'

    from sage.numerical.mip import MixedIntegerLinearProgram

Once you know roughly where the class is defined, for example Matrix is defined somewhere in SAGE_ROOT/devel/sage/sage/matrix/, take a look at the file all.py in that directory. In this case you'll find a line

from constructor import matrix, Matrix, column_matrix, random_matrix, diagonal_matrix, identity_matrix, block_matrix, block_diagonal_matrix, jordan_block, zero_matrix, ones_matrix, elementary_matrix, companion_matrix

So that tells you that you can import Matrix from sage.matrix.all or from sage.matrix.constructor. Your other examples can be dealt with similarly.

edit flag offensive delete link more

Comments

Thanks. This does help!

MaelstromYamato gravatar imageMaelstromYamato ( 2012-09-06 14:47:58 +0100 )edit

But this suggestion results in ImportError: cannot import name ZZ.

ml9nn gravatar imageml9nn ( 2018-02-03 18:09:44 +0100 )edit

Sage has changed in the past 5 1/2 years, so whatever suggestion may have worked then may not be valid any more. If you want to ask a new question, please do so.

John Palmieri gravatar imageJohn Palmieri ( 2018-02-19 23:27:01 +0100 )edit
3

answered 2012-09-06 15:03:33 +0100

Jason Grout gravatar image

updated 2012-09-06 15:05:37 +0100

It seems you are trying to avoid cluttering the namespace? Then do this:

import sage.all
from sage.all import Matrix,vector,ZZ, MixedIntegerLinearProgram

kcrisman was right---lots of Sage depends on lots of other things in Sage, and importing just a piece (i.e., without doing import sage.all) is definitely not supported and may often break or not provide any benefit.

edit flag offensive delete link more

Comments

Ah, the namespace. See, this doesn't occur to me :)

kcrisman gravatar imagekcrisman ( 2012-09-06 15:58:02 +0100 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2012-09-06 13:29:57 +0100

Seen: 19,647 times

Last updated: Sep 06 '12