Ask Your Question
2

matrix over TropicalSemiring

asked 2016-02-19 12:45:59 +0100

wrogn gravatar image

I try to build a matrix with entries from TropicalSemiring, but I get an error. This was done on SageMathCell:

T=TropicalSemiring(RR)
matrix([[T(1),T(2)],[T(3),T(4)]])

and yields:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-415e7359a236> in <module>()
      1 T=TropicalSemiring(RR)
----> 2 matrix([[T(Integer(1)),T(Integer(2))],[T(Integer(3)),T(Integer(4))]])

/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/matrix/constructor.pyc in _matrix_constructor(*args, **kwds)
    647 
    648             if nrows > 0 and ncols > 0 and ring is None:
--> 649                 entries, ring = prepare(entries)
    650 
    651         elif isinstance(args[0], dict):

/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/matrix/constructor.pyc in prepare(w)
    805         ring = rings.CDF
    806     elif not is_Ring(ring):
--> 807         raise TypeError("unable to find a common ring for all elements")
    808     return entries, ring
    809 

TypeError: unable to find a common ring for all elements

If instead I use this syntax:

T=TropicalSemiring(RR)
matrix(T,[[1,2],[3,4]])

I get a different error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-1-02718e38d49f> in <module>()
      1 T=TropicalSemiring(RR)
----> 2 matrix(T,[[Integer(1),Integer(2)],[Integer(3),Integer(4)]])

/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/matrix/constructor.pyc in _matrix_constructor(*args, **kwds)
    713                 raise ValueError("Invalid matrix constructor.  Type matrix? for help")
    714     else:
--> 715         raise ValueError("Invalid matrix constructor.  Type matrix? for help")
    716 
    717     if nrows is None:

ValueError: Invalid matrix constructor.  Type matrix? for help

How can I do it correctly?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2016-02-19 23:29:47 +0100

tmonteil gravatar image

updated 2016-02-22 14:13:56 +0100

Unfortunately, matrices can only be defined over a ring, not a semiring. For example it is the same with nonnegative integers:

sage: matrix(NN, 2)
ValueError: Invalid matrix constructor.  Type matrix? for help

EDIT You can use numpy matrices for basic operations:

sage: import numpy as np
sage: M = np.matrix([[T(1),T(2)],[T(3),T(4)]])
sage: M
matrix([[1.00000000000000, 2.00000000000000],
        [3.00000000000000, 4.00000000000000]], dtype=object)
sage: M*M
matrix([[2.00000000000000, 3.00000000000000],
        [4.00000000000000, 5.00000000000000]], dtype=object)
sage: M+M
matrix([[1.00000000000000, 2.00000000000000],
        [3.00000000000000, 4.00000000000000]], dtype=object)
edit flag offensive delete link more

Comments

Is there anything I could do about it? Like deriving a class from matrix and overloading the contructor? I'm new to Sage, so I have no idea how easy or hard that is.

wrogn gravatar imagewrogn ( 2016-02-22 10:56:18 +0100 )edit

I updated my answer, you can use numpy matrices, though they offer much less features.

tmonteil gravatar imagetmonteil ( 2016-02-22 14:14:30 +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

1 follower

Stats

Asked: 2016-02-19 12:45:59 +0100

Seen: 484 times

Last updated: Feb 22 '16