Book Image

Learning SciPy for Numerical and Scientific Computing

Book Image

Learning SciPy for Numerical and Scientific Computing

Overview of this book

Table of Contents (15 chapters)
Learning SciPy for Numerical and Scientific Computing Second Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Creating a matrix


In SciPy, a matrix structure is given to any one- or two-dimensional ndarray, with either the matrix or mat command. The complete syntax is as follows:

numpy.matrix(data=object, dtype=None, copy=True)

Creating matrices, the data may be given as ndarray, a string or a Python list (as the second example below), which is very convenient. When using strings, the semicolon denotes change of row and the comma, change of column:

>>> A=numpy.matrix("1,2,3;4,5,6")
>>> A

The output is shown a follows s:

matrix([[1, 2, 3],
        [4, 5, 6]])

Let's look at another example:

>>> A=numpy.matrix([[1,2,3],[4,5,6]])
>>> A

The output is shown as follows:

matrix([[1, 2, 3],
        [4, 5, 6]])

Another technique to create a matrix from a two-dimensional array is to enforce the matrix structure on a new object, copying the data of the former with the asmatrix routine.

A matrix is said to be sparse (http://en.wikipedia.org/wiki/Sparse_matrix) if most of its entries...