Basic Matrices in IronPython QuickStart Sample

Illustrates the basic use of the Matrix class for working with matrices in IronPython.

View this sample in: C# Visual Basic F#

```Python
import numerics

from System import Array

from Extreme.Mathematics import *
# The DenseMatrix class resides in the Extreme.Mathematics.LinearAlgebra
# namespace.
from Extreme.Mathematics.LinearAlgebra import *

# Illustrates the use of the DenseMatrix class in the 
# Extreme.Mathematics.LinearAlgebra namespace of Extreme Numerics.NET.

#
# Constructing matrices
#

# Option #1: specify number of rows and columns.
# The following constructs a matrix with 3 rows
# and 5 columns:
m1 = Matrix.Create(3, 5)
print "m1 =", m1
# Option #2: specify a rank 2 double array. 
# By default, elements are taken in column-major
# order. Therefore, the following creates a matrix
# with 3 rows and 4 columns:
m2 = Matrix([[1, 2, 3], [2, 3, 4], [3, 4, 5], [4, 5, 6]])
print "m2 =", m2
m3 = m2
# Option #4: Specify component array, and number
# of rows and columns. The elements are listed
# in column-major order. The following matrix
# is identical to m3:
components = Array[float]([ 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6 ])
m4 = Matrix.Create(3, 4, components, MatrixElementOrder.ColumnMajor)
print "m4 =", m4
# Option #5: same as above, but specify element
# order. The following matrix is identical to m4:
m5 = Matrix.Create(4, 3, components, MatrixElementOrder.RowMajor)
print "m5 =", m5
# Option #6: same as #4, but specify whether to copy
# the matrix components, or use the specified array
# as internal storage.
m6 = Matrix.Create(3, 4, components, MatrixElementOrder.ColumnMajor, False)
# Option #7: same as #5, but specify whether to copy
# the matrix components, or use the specified array
# as internal storage.
m7 = Matrix.Create(4, 3, components, MatrixElementOrder.RowMajor, False)
# In addition, you can also create an identity 
# matrix by calling the static GetIdentity method.
# The following constructs a 4x4 identity matrix:
m8 = DenseMatrix.GetIdentity(4)
print "m8 =", m8

#
# DenseMatrix properties
#
			
# The RowCount and ColumnCount properties give the 
# number of rows and columns, respectively:
print "m1.RowCount =", m1.RowCount
print "m1.ColumnCount =", m1.ColumnCount
# The ToArray method returns a one-dimensional
# double array that contains the components of the 
# vector. By default, elements are returned in
# column major order. This is always a copy:
components = m3.ToArray()
print "Components:"
print "components[3] =", components[3]
components[3] = 1
print "m3[0,1] =", m3[0,1]
# The ToArray method is overloaded, so you can
# choose whether you want the elements in row major 
# or in column major order. The order parameter is
# of type MatrixElementOrder:
components = m3.ToArray(MatrixElementOrder.RowMajor)
print "In row major order:"
print "components[3] =", components[3]

#
# Accessing matrix elements
#

# The DenseMatrix class defines an indexer property 
# that takes zero-based row and column indices.
print "Assigning with private storage:"
print "m1[0,2] =", m1[0,2]
# You can assign to this property:
m1[0,2] = 7
print "m1[0,2] =", m1[0,2]

# The matrices m6 and m7 had the copy parameter in
# the constructor set to false. As a result, they
# share their component storage. Changing one vector
# also changes the other:
print "Assigning with shared storage:"
print "m6[0,0] =", m6[0,0]
m7[0,0] = 3
print "m6[0,0] =", m6[0,0]

#
# Copying and cloning matrices
#

# A shallow copy of a matrix constructs a matrix
# that shares the component storage with the original.
# This is done using the ShallowCopy method. Note
# that we have to cast the return value since it is
# of type Matrix, the abstract base type of all
# the matrix classes:
print "Shallow copy vs. clone:"
m10 = m2.ShallowCopy()
# The clone method creates a full copy.
m11 = m2.Clone()
# When we change m2, m10 changes, but m11 is left
# unchanged:
print "m2[1,1] =", m2[1,1]
m2[1,1] = -2
print "m10[1,1] =", m10[1,1]
print "m11[1,1] =", m11[1,1]
# We can give a matrix its own component storage
# by calling the CloneData method:
print "CloneData:"
m11.CloneData()
# Now, changing the original v2 no longer changes v7:
m2[1,1] = 4
print "m11[1,1] =", m11[1,1]

```