Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: LS 30B-2 S19
Views: 122

Matrix

  • A collection of vectors!

Why we said is a "collection" of "vectors"

Matrix Multiplication

Discrete System

Programming

Matrix is a collection of vectors

mat1 = matrix([[1,2],[3,4]]) mat2 = matrix([[2,3],[4,5]])
mat1
[1 2] [3 4]
mat2
[2 3] [4 5]
mat1+mat2
[3 5] [7 9]
mat1-mat2
[-1 -1] [-1 -1]
mat1*mat2
[10 13] [22 29]

Build Matrix by Columns

mat3 = column_matrix([[1,2],[3,4]]) mat3
[1 3] [2 4]

Retrieve element in a matrix

mat1[:,1] mat1.column(1)
[2] [4] (2, 4)
# this cannot work mat1[:,1]*mat1[:,1]
Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags) in namespace, locals File "", line 1, in <module> File "sage/structure/element.pyx", line 3694, in sage.structure.element.Matrix.__mul__ (build/cythonized/sage/structure/element.c:22621) raise TypeError("unsupported operand parent(s) for *: '{}' and '{}'".format(parent, parent)) TypeError: unsupported operand parent(s) for *: 'Full MatrixSpace of 2 by 1 dense matrices over Integer Ring' and 'Full MatrixSpace of 2 by 1 dense matrices over Integer Ring'
# this will work! mat1.column(1)*mat1.column(1)
20
# however, row*column will work mat1[1,:]*mat1[:,1]
[22]

Hint for today's lab

The rule for matrix multiplication

  1. Only rows multiplies columns

  2. The result of ith row multiplies nth column will be storate in (i,n) of output matrix

M1 = matrix([[1,2,3],[4,5,6]]) M2 = matrix([[1,0],[0,1],[0,0]]) matrix([M1.row(0)*M2.column(i) for i in srange(2)])
[1 2]

Object

What is . for? e.g., mat1.column(0)

Get the properties of the variable

We can pack a lot of properties in one variable!!!

# define a variable Hao Hao.height() Hao.salary() Hao.gender()
mat1 = matrix([[1,2,3],[3,6,3],[1,4,6]]) mat2 = matrix()
mat1.dimensions()
(3, 3)
type(mat1)
<type 'sage.matrix.matrix_integer_dense.Matrix_integer_dense'>