Constructing Matrices

159 views
Skip to first unread message

VictorMiller

unread,
Feb 1, 2008, 5:33:28 PM2/1/08
to sage-forum
Magma has the useful functions VerticalJoin and HorizontalJoin to
build matrices out of other matrices (numpy has similar functionality
with concatenate). Is there something similar for a Sage Matrix?

Victor

William Stein

unread,
Feb 1, 2008, 5:38:23 PM2/1/08
to sage-...@googlegroups.com

Yes. They are called "stack" and "augment" in Sage, and they
should work with any Sage matrix types.

sage: a = random_matrix(ZZ, 2,3); b = random_matrix(ZZ, 2, 3)
sage: a.stack(b)
[-4 20 -4]
[-4 -4 3]
[-1 25 -8]
[ 1 -2 1]
sage: a.augment(b)
[-4 20 -4 -1 25 -8]
[-4 -4 3 1 -2 1]

-- William

Mike Hansen

unread,
Feb 1, 2008, 5:40:30 PM2/1/08
to sage-...@googlegroups.com
Hello,

There is also the block_matrix command which may also be of interest:

sage: m = matrix(ZZ, 2, 2, 1); m
[1 0]
[0 1]
sage: block_matrix([0,m,m,0])
[0 0|1 0]
[0 0|0 1]
[---+---]
[1 0|0 0]
[0 1|0 0]
sage: block_matrix([m,m], nrows=1)
[1 0|1 0]
[0 1|0 1]
sage: block_matrix([m,m], ncols=1)
[1 0]
[0 1]
[---]
[1 0]
[0 1]

--Mike

VictorMiller

unread,
Feb 4, 2008, 9:50:42 PM2/4/08
to sage-forum
William and Mike, Thanks for the replies. However, this brings up a
slightly puzzling fact. When looking for these functions (using Sage
2.10) in the notebook, I just defined some integer matrix, like

a = random_matrix(ZZ,5,5)

and then typed a.<tab>

I got a long list of possible completions but augment and stack
weren't among them. Should they be?

Victor

William Stein

unread,
Feb 4, 2008, 11:40:40 PM2/4/08
to sage-...@googlegroups.com

They are for me (see attached screen shot where both augment and
stack appear), and I can't think of any conceivable reason why
they wouldn't appear for you. Could you check again and if
they really aren't there let us know, and we can do some things
to debug the problem?

William

Picture 6.png

victor miller

unread,
Feb 5, 2008, 7:41:19 AM2/5/08
to sage-forum
Please cancel that last remark.  I just checked again and they're there.  What I was referring to (and I don't know any easy solution to this) was that there was no easy way to find that this functionality was available unless I already knew the names.  If you already haven't seen it, Tim Gowers' blog has a discussion of making up what he calls a "tricki" -- a way of finding mathematical tricks.

Victor

William Stein

unread,
Feb 5, 2008, 8:04:10 AM2/5/08
to sage-...@googlegroups.com
On Feb 5, 2008 4:41 AM, victor miller <victor...@gmail.com> wrote:
> Please cancel that last remark. I just checked again and they're there.
> What I was referring to (and I don't know any easy solution to this) was
> that there was no easy way to find that this functionality was available
> unless I already knew the names.

I disagree. In fact, most people who use Sage (or Python for that
matter) create an object A of the type they want to do
something with then type A.[tab] and see a list of the
options. One would definitely see "augment" and "stack" among
the 100 or so options, and it's not unlikely they would realize
those functions can be used to augment or stack matrices.
This exactly addresses the problem of there being an easy way to
find functionality, certainly in a better way than it is addressed
in Magma, and it's something a lot of Sage users really appreciate
over other systems.

You could also type

sage: search_src('augment')

and get a list of places in the Sage library where the word augment
appears -- not so easy to use, but potentially very useful.

You can also do:

sage: search_src('victor')
----------------------------------------------------------------------
| SAGE Version 2.10.1, Release Date: 2008-02-02 |
| Type notebook() for the GUI, and license() for information. |
----------------------------------------------------------------------
libs/ntl/all.py:Victor Shoup's NTL C++ Library
libs/ntl/all.py:SAGE provides an interface to Victor Shoup's C++ library NTL.
modular/modform/all.py:from vm_basis import victor_miller_basis, delta_qexp
modular/modform/cuspidal_submodule.py: return
vm_basis.victor_miller_basis(self.weight(), prec,
modular/modform/vm_basis.py:The Victor Miller Basis
modular/modform/vm_basis.py:def victor_miller_basis(k, prec=10,
cusp_only=False, var='q'):
modular/modform/vm_basis.py: Compute and return the Victor-Miller basis for
modular/modform/vm_basis.py: sage: victor_miller_basis(1, 6)
modular/modform/vm_basis.py: sage: victor_miller_basis(0, 6)
modular/modform/vm_basis.py: sage: victor_miller_basis(2, 6)
modular/modform/vm_basis.py: sage: victor_miller_basis(4, 6)
modular/modform/vm_basis.py: sage: victor_miller_basis(6, 6, var='w')
modular/modform/vm_basis.py: sage: victor_miller_basis(6, 6)
modular/modform/vm_basis.py: sage: victor_miller_basis(12, 6)
modular/modform/vm_basis.py: sage: victor_miller_basis(12, 6,
cusp_only=True)
modular/modform/vm_basis.py: sage: victor_miller_basis(24, 6,
cusp_only=True)
modular/modform/vm_basis.py: sage: victor_miller_basis(24, 6)
modular/modform/vm_basis.py: sage: victor_miller_basis(32, 6)
matrix/matrix_integer_dense.pyx: ALGORITHM: Uses the NTL
library by Victor Shoup or fpLLL
rings/polynomial/polynomial_element.pyx: ## Victor Schoup was
very careful with the choice of strategies and


William

victor miller

unread,
Feb 5, 2008, 9:07:27 AM2/5/08
to sage-...@googlegroups.com
William,
Hmm, We obviously disagree.  I do agree that augment is a good image for that, and that once I know the name I'm not likely to forget it.  Sage is MUCH better than Magma in this regard.  I can't tell you how much time I've spent sifting through the Magma docs (and online help) trying to find a function to do something that I was sure was there.  Have search_src and search_doc is really great (and as I mentioned to you last week using agrep and glimpse -- see Udi Maber's old home page --  would make them even better).  But I'd still like to imagine that there would be even a better way (though I don't know exactly what it is).

Victor
Reply all
Reply to author
Forward
0 new messages