# Matrix-Vector Operations in IronPython QuickStart Sample

Illustrates how to perform operations that involve both matrices and vectors in IronPython.

View this sample in: C# Visual Basic F#

```Python import numerics from Extreme.Mathematics import * # The Vector and DenseMatrix classes reside in the # Extreme.Mathematics.LinearAlgebra namespace. from Extreme.Mathematics.LinearAlgebra import * # Illustrates operations on DenseMatrix objects and combined # operations on Vector and DenseMatrix objects from the # Extreme.Mathematics.LinearAlgebra namespace of Numerics.NET. # For details on the basic workings of Vector # objects, including constructing, copying and # cloning vectors, see the BasicVectors QuickStart # Sample. # # For details on the basic workings of DenseMatrix # objects, including constructing, copying and # cloning vectors, see the BasicVectors QuickStart # Sample. # # Let's create some vectors to work with. v1 = Vector([1, 2, 3, 4, 5]) v2 = Vector([1, -2, 3, -4, 5]) print "v1 = {0:.4f}".format(v1) print "v2 = {0:.4f}".format(v2) # Also, here are a couple of matrices. # We start out with a 5x5 identity matrix: m1 = DenseMatrix.GetIdentity(5) # Now we use the GetDiagonal method and combine it # with the SetValue method of the Vector class to # set some of the off-diagonal elements: m1.GetDiagonal(1).SetValue(2) m1.GetDiagonal(2).SetValue(3) m1.GetDiagonal(-1).SetValue(4) print "m1 = {0:.4f}".format(m1) # We define our second matrix by hand: m2 = Matrix([[1,1,1,1,1], [2,3,4,8,-1], [3,5,9,27,1], [4,7,16,64,-1], [5,9,25,125,1]]) print "m2 = {0:.4f}".format(m2) print # # Matrix arithmetic # # The Matrix class defines operator overloads for # addition, subtraction, and multiplication of # matrices. # Addition: print "Matrix arithmetic:" m = m1 + m2 print "m1 + m2 = {0:.4f}".format(m) # Subtraction: m = m1 - m2 print "m1 - m2 = {0:.4f}".format(m) # Multiplication is the True matrix product: m = m1 * m2 print "m1 * m2 = {0:.4f}".format(m) print # # Matrix-Vector products # # The DenseMatrix class defines overloaded addition, # subtraction, and multiplication operators # for vectors and matrices: print "Matrix-vector products:" v = m1 * v1 print "m1 v1 = {0:.4f}".format(v) # You can also multiply a vector by a matrix on the right. # This is equivalent to multiplying on the left by the # transpose of the matrix: v = v1 * m1 print "v1 m1 = {0:.4f}".format(v) # Now for some methods of the Vector class that # involve matrices: # Add a product of a matrix and a vector: v.Add(m1, v1) print "v + m1 v1 = {0:.4f}".format(v) # Or add a scaled product: v.Add(-2, m1, v2) print "v - 2 m1 v2 = {0:.4f}".format(v) # You can also use static Subtract methods: v.Subtract(m1, v1) print "v - m1 v1 = {0:.4f}".format(v) print # # Matrix norms # print "Matrix norms" # Matrix norms are not as easily defined as # vector norms. Three matrix norms are available. # 1. The one-norm through the OneNorm property: a = m2.OneNorm() print "OneNorm of m2 = {0:.4f}".format(a) # 2. The infinity norm through the # InfinityNorm property: a = m2.InfinityNorm() print "InfinityNorm of m2 = {0:.4f}".format(a) # 3. The Frobenius norm is often used because it # is easy to calculate. a = m2.FrobeniusNorm() print "FrobeniusNorm of m2 = {0:.4f}".format(a) print # The trace of a matrix is the sum of its diagonal # elements. It is returned by the Trace property: a = m2.Trace() print "Trace(m2) = {0:.4f}".format(a) # The Transpose method returns the transpose of a # matrix. This transposed matrix shares element storage # with the original matrix. Use the CloneData method # to give the transpose its own data storage. m = m2.Transpose() print "Transpose(m2) = {0:.4f}".format(m) ```