It should be lightly easier than it is to convert a vector of length n
to either an nx1 matrix or a 1xn matrix:

sage: v = vector(srange(5))
sage: v
(0, 1, 2, 3, 4)
sage: matrix(QQ,1,5,[v])
[0 1 2 3 4]
sage: matrix(QQ,5,1,list(v))

[0]
[1]
[2]
[3]
[4]

I got neither of those right the first time!  I know that v*v is short
for the dot product, which is also (row v)*(col v), but sometimes one
wants (col v)*(row v) as a rank 1 nxn matrix:

sage: matrix(QQ,5,1,list(v)) * matrix(QQ,1,5,[v])

[ 0  0  0  0  0]
[ 0  1  2  3  4]
[ 0  2  4  6  8]
[ 0  3  6  9 12]
[ 0  4  8 12 16]

I suggest that vector methods row_matrix() and col_matrix() would be useful.

John


2008/9/7 Jason Merrill <[EMAIL PROTECTED]>:
>
> On Sep 6, 7:26 pm, Jason Merrill <[EMAIL PROTECTED]> wrote:
>> Is there a simple way to think of the difference between a vector with
>> n elements, and a 1 by n matrix in Sage.  When would I want to use one
>> instead of the other?
>>
>> sage: m = matrix([1,2,3,4,5])
>> sage: parent(m)
>> Full MatrixSpace of 1 by 5 dense matrices over Integer Ring
>>
>> sage: v = vector([1,2,3,4,5])
>> sage: parent(v)
>> Ambient free module of rank 5 over the principal ideal domain Integer
>> Ring
>>
>> m seems to have many more methods than v, but looking at matrix? and
>> vector? didn't make things perfectly clear.
>
> mhansen caught up with me on IRC and cleared things up a bit.  I
> thought I'd paste in the conversation for the benefit of any others
> who are wondering.
>
> [9:22pm] mhansen: jwmerrill: I don't know if I understand your
> question about matrices and vectors.  What are you trying to do?
> [9:22pm] mhansen: "Vectors" in Sage are elements of a free module /
> vector space.
> [9:23pm] mhansen: One usually thinks about matrices as representing
> homomorphisms between such spaces.
> [10:01pm] jwmerrill: mhansen: re vectors/matrices, I'm not trying to
> do anything too specific
> [10:01pm] jwmerrill: just trying to fit my head around sage
> [10:02pm] mhansen: Well, they're very different mathematical objects
> that just happened to have 5 "numbers" associated with them.
> [10:04pm] jwmerrill: fair enough
> [10:05pm] mhansen: Addition is defined component-wise for both of them
> and they both support scalar multiplication.
> [10:05pm] jwmerrill: can you right multiply either of them by an
> appropriately sized matrix?
> [10:06pm] mhansen: Yep.
> [10:06pm] mhansen: Vectors have no notion of being a "row vector" or
> "column vector".
> [10:06pm] jwmerrill: oh, that's interesting
> [10:07pm] mhansen: So, if you have a vector of size n, you can act on
> it on either side with an nxn matrix.
> [10:08pm] jwmerrill: ok
> [10:08pm] mhansen: Multiplying two vectors is a shortcut for the inner
> product on that space (typically the standard dot product).
> [10:09pm] jwmerrill: got it
> [10:09pm] jwmerrill: one of the things I was wondering about was what
> kind of sage object should represent the type of thing that ode
> solvers would want as the jacobian
> [10:11pm] jwmerrill: in practice, it has to be a function that returns
> a collection of numbers
> [10:11pm] jwmerrill: when evaluated at some point
> [10:11pm] jwmerrill: is that more like a vector, or a matrix?
> [10:12pm] jwmerrill: Hubbard and Hubbard makes a point of making the
> distinction that the gradient is a vector, but the jacobian is a row
> matrix
> [10:13pm] jwmerrill: but I didn't really get what the point was, other
> than that the gradient can change if you have a different inner
> product rule, but the jacobian doesn't need any inner product at all
> [10:15pm] mhansen: Yes, I would do the Jacobian as a matrix.
> [10:15pm] mhansen: You can evaluate matrices over the symbolic ring in
> Sage.
> [10:15pm] mhansen: sage: m = matrix(SR, [[x, x+1],[2*x,0]]); m
> [10:15pm] mhansen: [    x x + 1]
> [10:15pm] mhansen: [  2*x    0]
> [10:15pm] mhansen: sage: m(2)
> [10:15pm] mhansen: [2 3]
> [10:16pm] mhansen: [4 0]
> [10:16pm] jwmerrill: ok, cool
>
> JM
> >
>

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to