Ask Your Question
0

import sage packages in python

asked 2012-09-06 06:29:57 -0500

MaelstromYamato gravatar image

updated 2012-09-06 07:02:41 -0500

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 07:03:39 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2012-09-06 07:08:28 -0500

updated 2012-09-06 11:14:35 -0500

  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 07:47:58 -0500 )edit
2

answered 2012-09-06 08:03:33 -0500

Jason Grout gravatar image

updated 2012-09-06 08:05:37 -0500

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 08:58:02 -0500 )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 06:29:57 -0500

Seen: 690 times

Last updated: Sep 06 '12