ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 27 Jul 2024 09:17:05 +0200Anything similar to MatrixFormhttps://ask.sagemath.org/question/78473/anything-similar-to-matrixform/ Hi, is there anything similar to Mathematica's MatrixForm for pretty printing vectors and matrices?Fri, 26 Jul 2024 19:11:32 +0200https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/Comment by tolga for <p>Hi, is there anything similar to Mathematica's MatrixForm for pretty printing vectors and matrices?</p>
https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78474#post-id-78474Does "show" suit your needs?:
m=matrix([[1,2,3],[4,5,6]])
show(m)Fri, 26 Jul 2024 19:32:37 +0200https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78474#post-id-78474Answer by Emmanuel Charpentier for <p>Hi, is there anything similar to Mathematica's MatrixForm for pretty printing vectors and matrices?</p>
https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?answer=78490#post-id-78490I have to disagree with @tolga : a matrix is *not* a list of lists, it has a structure giving it specific properties. Let me illustrate :
sage: var("a, b, c, d, e, f")
(a, b, c, d, e, f)
sage: L1 = [[a, b], [c, d]]
sage: L2 = [e, f]
L1 is just a list, with no specific structure, happenning to be a list of two lists :
sage: L1
[[a, b], [c, d]]
You can create a *matrix* object from this list of lists with `matrix` ; this object is recognized as such (hence a diferent default printing format :
sage: matrix(L1)
[a b]
[c d]
Similarly, L2 is but a list :
sage: L2
[e, f]
whereas a `vector` has a specific structure with specific properties (including printing) :
sage: vector(L2)
(e, f)
The multiplication of two lists has no intrinsic meaning :
sage: L1*L2
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[15], line 1
----> 1 L1*L2
TypeError: can't multiply sequence by non-int of type 'list'
whereas the product of a matrix by a vector *is* well defined (and results in a vector, if dimensions are compatible) :
sage: matrix(L1)*vector(L2)
(a*e + b*f, c*e + d*f)
sage: (matrix(L1)*vector(L2)).parent()
Vector space of dimension 2 over Symbolic Ring
Here, Mathematica is weaker than Sage by missing this distinction ; be smart and use Sage's strengths !
A side note : there is, mathematically speaking, no *row* vectors and *column* vectors : a vector is a vector is a vector, period. The distinction between row and columns vectors is an anglo-saxon fetish imposed upon unlucky undergrads for mistaken "pedagogical" reasons (insist on the non-commutativity of matrix and vector-matrix products, I suppose).
And, BTW, in Sage, a so-called *column* vector :
sage: vector(L2).column()
[e]
[f]
isn't a vector but a single-column *matrix* :
sage: vector(L2).column().parent()
Full MatrixSpace of 2 by 1 dense matrices over Symbolic Ring
Not a pretty sight...Sat, 27 Jul 2024 09:17:05 +0200https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?answer=78490#post-id-78490Answer by akm for <p>Hi, is there anything similar to Mathematica's MatrixForm for pretty printing vectors and matrices?</p>
https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?answer=78475#post-id-78475Beautiful thank you; is there an option to display a vector vertically?Fri, 26 Jul 2024 19:54:06 +0200https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?answer=78475#post-id-78475Comment by akm for <p>Beautiful thank you; is there an option to display a vector vertically?</p>
https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78478#post-id-78478Yeah works great, thanks @tolgaFri, 26 Jul 2024 20:22:03 +0200https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78478#post-id-78478Comment by tolga for <p>Beautiful thank you; is there an option to display a vector vertically?</p>
https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78477#post-id-78477Please try this:
v=vector([1,2,3])
show(v.column())Fri, 26 Jul 2024 20:00:23 +0200https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78477#post-id-78477Comment by akm for <p>Beautiful thank you; is there an option to display a vector vertically?</p>
https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78476#post-id-78476Oh nevermind I got what I needed with .transpose()Fri, 26 Jul 2024 19:58:37 +0200https://ask.sagemath.org/question/78473/anything-similar-to-matrixform/?comment=78476#post-id-78476