Accessing Matrix Elements in IronPython QuickStart Sample
Illustrates different ways of iterating through the rows and columns of a matrix using classes in the Numerics.NET.LinearAlgebra namespace in IronPython.
This sample is also available in: C#, Visual Basic, F#.
Overview
This QuickStart sample demonstrates the various ways to access and manipulate elements, rows, columns, and submatrices in Numerics.NET matrices.
The sample shows how to:
- Access individual matrix elements using index notation
- Extract complete or partial rows and columns using range syntax
- Use the
GetRow
andGetColumn
methods for accessing matrix components - Enumerate through matrix rows and columns
- Access matrix diagonals and superdiagonals
- Work with submatrices through both indexing and dedicated methods
- Assign values to matrix components, including rows, columns and submatrices
The sample provides practical examples of each operation, demonstrating both read and write operations on matrices. It illustrates the flexibility of Numerics.NET’s matrix access methods, from simple element access to sophisticated operations on matrix substructures.
The code
import numerics
# The Vector and Vector classes resides in the
# Extreme.Mathematics.LinearAlgebra namespace.
from Extreme.Mathematics import *
from Extreme.Mathematics.LinearAlgebra import *
from System import Array
# Illustrates accessing matrix components and iterating
# through the rows and columns of a matrix. Matrix classes
# reside in the Extreme.Mathematics.LinearAlgebra namespace
# of Extreme Numerics.NET.
# We'll work with this matrix:
m = Matrix.Create(2, 3, \
Array[float]([ 1, 2, 3, 4, 5, 6 ]), \
MatrixElementOrder.ColumnMajor)
#
# Individual components
#
# The Matrix class has an indexer property that takes two arguments:
# the row and column index. Both are zero based.
print "m[1,1] =", m[1, 1]
#
# Rows and columns
#
# Indexed range access
# The indexer property is overloaded to allow for direct indexed access
# to complete or partial rows or columns.
row1 = m[0, 1:3]
# This prints "[3, 5]":
print "row1 =", row1
# The special range : lets you access an entire row
# or column without having to specify any details about the range.
row2 = m[1, :]
# This prints "[2, 4, 6]":
print "2nd row =", row2
column1 = m[:, 0]
# This prints "[1, 2]":
print "column1 =", column1
# We can assign to rows and columns, too:
m[:, 0] = row1
# This prints "[[3, 3, 5] [5, 4, 6]]"
print "m =", m
# GetRow and GetColumn provide an alternate mechanism
# for achieving the same result.
# Passing just one parameter retrieves the specified row or column:
row1 = m.GetRow(1)
# This prints "[2, 4, 6]":
print "row1 =", row1
column1 = m.GetColumn(0)
# This prints "[1, 2]":
print "column1 =", column1
# You can also pass a start and end index:
row2 = m.GetRow(0, 1, 2)
# This prints "[3, 5]":
print "row2 =", row2
# We can assign to rows and columns, too, using CopyTo:
row2.CopyTo(m.GetColumn(0))
# This prints "[[3, 3, 5] [5, 4, 6]]"
print "m =", m
# Enumeration
# The Rows and Columns methods allow you to enumerate over
# the rows and columns of a matrix.
# For example: this calculates the sum of the absolute values
# of the components of the matrix m:
sum = 0
for column in m.Columns:
sum += column.OneNorm()
#
# Accessing diagonals
#
# Diagonals are retrieved using the GetDiagonal method:
mainDiagonal = m.GetDiagonal()
# An optional parameter specifies which diagonal:
# n < 0 means subdiagonal
# n > 0 means nth superdiagonal:
superDiagonal = m.GetDiagonal(1)
#
# Accessing submatrices
#
# Indexed range access
# A fourth overload of the indexer property lets you
# extract a part of a matrix. Both parameters are Range
# structures:
a = Matrix.Create(10, 10)
# Extract the 2nd to the 5th row of m:
a1 = a[1:5, :]
# Extract the odd columns:
a2 = a[:, 1:10:2]
# Extract the 4x4 leading submatrix of m:
a3 = a[0:4, 0:4]
# You can also assign to submatrices:
identity5 = DenseMatrix.GetIdentity(5)
a[0:5, 5:10] = identity5
a[5:10, 0:5] = identity5
# The same results can be achieved with the GetSubmatrix method.
# Extract the 2nd to the 5th row of m.
# Start and end columns are supplied manually.
a4 = a.GetSubmatrix(1, 4, 0, 9)
# Extract the odd columns:
# Here we need to supply the transpose parameter.
a5 = a.GetSubmatrix(0, 9, 1, 1, 9, 2, TransposeOperation.None)
# Extract the 4x4 leading submatrix of m.
# And let's get its transpose, just because we can.
# We need to specify the row and column stride:
a6 = a.GetSubmatrix(0, 3, 1, 0, 3, 1, TransposeOperation.Transpose)
# You can still assign to submatrices, using the
# CopyTo method:
identity5.CopyTo(a.GetSubmatrix(0, 4, 5, 9))
identity5.CopyTo(a.GetSubmatrix(5, 9, 0, 4))