QuickStart Samples

# Linear Equations QuickStart Sample (IronPython)

Illustrates how to solve systems of simultaneous linear equations in IronPython.

C# code Visual Basic code F# code Back to QuickStart Samples

import numerics # The DenseMatrix and LUDecomposition classes reside in the # Extreme.Mathematics.LinearAlgebra namespace. from Extreme.Mathematics import * from Extreme.Mathematics.LinearAlgebra import * #/ Illustrates solving systems of simultaneous linear #/ equations using the DenseMatrix and LUDecomposition classes #/ in the Extreme.Mathematics.LinearAlgebra namespace of the Extreme #/ Optimization Mathematics Library for .NET. # A system of simultaneous linear equations is # defined by a square matrix A and a right-hand # side B, which can be a vector or a matrix. # # You can use any matrix type for the matrix A. # The optimal algorithm is automatically selected. # Let's start with a general matrix: m = Matrix([[1, 1, 1, 1], [1, 2, 4, 2], [1, 3, 9, 1], [1, 4, 16, 2]]) b1 = Vector([ 1, 3, 6, 3 ]) b2 = Matrix([[1, 6], [2, 5], [3, 3], [3, 8]]) print "m = {0:.4f}".format(m) # # The Solve method # # The following solves m x = b1. The second # parameter specifies whether to overwrite the # right-hand side with the result. x1 = m.Solve(b1, False) print "x1 = {0:.4f}".format(x1) # If the overwrite parameter is omitted, the # right-hand-side is overwritten with the solution: m.Solve(b1) print "b1 = {0:.4f}".format(b1) # You can solve for multiple right hand side # vectors by passing them in a DenseMatrix: x2 = m.Solve(b2, False) print "x2 = {0:.4f}".format(x2) # # Related Methods # # You can verify whether a matrix is singular # using the IsSingular method: print "IsSingular(m) =", m.IsSingular() # The inverse matrix is returned by the Inverse # method: print "Inverse(m) = {0:.4f}".format(m.GetInverse()) # The determinant is also available: print "Det(m) = {0:.4f}".format(m.GetDeterminant()) # The condition number is an estimate for the # loss of precision in solving the equations print "Cond(m) = {0:.4f}".format(m.EstimateConditionNumber()) print # # The LUDecomposition class # # If multiple operations need to be performed # on the same matrix, it is more efficient to use # the LUDecomposition class. This class does the # bulk of the calculations only once. print "Using LU Decomposition:" # The constructor takes an optional second argument # indicating whether to overwrite the original # matrix with its decomposition: lu = m.GetLUDecomposition(False) # All methods mentioned earlier are still available: x2 = lu.Solve(b2, False) print "x2 = {0:.4f}".format(x2) print "IsSingular(m) =", lu.IsSingular() print "Inverse(m) = {0:.4f}".format(lu.GetInverse()) print "Det(m) = {0:.4f}".format(lu.GetDeterminant()) print "Cond(m) = {0:.4f}".format(lu.EstimateConditionNumber()) # In addition, you have access to the # components, L and U of the decomposition. # L is lower unit-triangular: print " L = {0:.4f}".format(lu.LowerTriangularFactor) # U is upper triangular: print " U = {0:.4f}".format(lu.UpperTriangularFactor)

Copyright Â© 2003-2021, Extreme Optimization. All rights reserved.

*Extreme Optimization,* *Complexity made simple*, *M#*, and *M Sharp* are trademarks of ExoAnalytics Inc.

*Microsoft*, *Visual C#, Visual Basic, Visual Studio*, *Visual Studio.NET*, and the *Optimized for Visual Studio* logo

are registered trademarks of Microsoft Corporation.