This project has moved and is read-only. For the latest updates, please go here.

Example of Usage in PowerShell 2.0

Oct 18, 2011 at 10:13 PM

Hi,

We are (1) doing some modest statistical work and (2) are needing memory-intensive stress-test scripts for our new batch worker-machines. With those needs in mind, I started working with the MathNet.Numerics library using PowerShell.

For anyone who's interested, here's an example of how to call MathNet.Numerics methods from PowerShell in a batch (or interactive) environment:

# batch worker testing
# requires: .NET 4.0 (or above) , PowerShell 2.0 (or above)

# put the numerical library where we can load it
$_source = 'I:\Math_Libraries\Math_dotNET_Numerics\lib\Net40\MathNet.Numerics.dll'
$_target = 'C:\TEMP\MathNet.Numerics.dll'
cp $_source $_target

# load up all its tools, widgets and gizmos
add-type -path 'C:\TEMP\MathNet.Numerics.dll'

# make a note of what version/level we just loaded, in case future historians need the reference
write-host ("=" * 100)
( [system.appdomain]::CurrentDomain.GetAssemblies()          | `
  foreach { $_fullname = $_.fullname
            if ( $_fullname.toupper().startswith( "MATH" ) )
              { write-host $_fullname }
          }
)

# create two 4-by-4 matrices, with initial values
$_matrix_A = new-object MathNet.Numerics.LinearAlgebra.Double.DenseMatrix (4,4, 2.0)
$_matrix_B = new-object MathNet.Numerics.LinearAlgebra.Double.DenseMatrix (4,4, 3.0)

# add them together using overloaded '+' operator
$_matrix_C = $_matrix_A + $_matrix_B

# show the world what we got
write-host ("=" * 100)
write-host " "
write-host " ... an example of MathNet usage from PowerShell"
write-host " ... matrix 'A' :"
write-host $_matrix_A.tostring()
write-host " "
write-host " ... matrix 'B' :"
write-host $_matrix_B.tostring()
write-host " "
write-host " ... matrix resulting from 'A' + 'B' :  (using overloaded '+' operator)"
write-host $_matrix_C.tostring()
write-host ("=" * 100)

exit

 

... and here's the output:

====================================================================================================
MathNet.Numerics, Version=2011.4.17.0, Culture=neutral, PublicKeyToken=cd8b63ad3d691a37
====================================================================================================
 
 ... an example of MathNet usage from PowerShell
 ... matrix 'A' :
2,2,2,2
2,2,2,2
2,2,2,2
2,2,2,2
 
 ... matrix 'B' :
3,3,3,3
3,3,3,3
3,3,3,3
3,3,3,3
 
 ... matrix resulting from 'A' + 'B' :  (using overloaded '+' operator)
5,5,5,5
5,5,5,5
5,5,5,5
5,5,5,5
====================================================================================================

Chuck

P.S.  Using the get-member cmdlet, one can figure what can be done with/to an object. For a matrix, for example, one might do/type (using a matrix defined int the preceding script) :

( $_matrix_A                                      | `
  get-member -membertype 'method' | `
  select-object -property name,definition
)

... to get :

Name                                    Definition                            
----                                    ----------                            
Add                                     MathNet.Numerics.LinearAlgebra.Gener...
Append                                  MathNet.Numerics.LinearAlgebra.Gener...
At                                      double At(int row, int column), Syst...
Clear                                   System.Void Clear()                   
Clone                                   MathNet.Numerics.LinearAlgebra.Gener...
Column                                  MathNet.Numerics.LinearAlgebra.Gener...
ColumnEnumerator                        System.Collections.Generic.IEnumerab...
ConditionNumber                         double ConditionNumber()              
ConjugateTranspose                      MathNet.Numerics.LinearAlgebra.Gener...
CopyTo                                  System.Void CopyTo(MathNet.Numerics....
CreateMatrix                            MathNet.Numerics.LinearAlgebra.Gener...
CreateVector                            MathNet.Numerics.LinearAlgebra.Gener...
Determinant                             double Determinant()                  
Diagonal                                MathNet.Numerics.LinearAlgebra.Gener...
DiagonalStack                           MathNet.Numerics.LinearAlgebra.Gener...
Divide                                  MathNet.Numerics.LinearAlgebra.Gener...
Equals                                  bool Equals(MathNet.Numerics.LinearA...
FrobeniusNorm                           double FrobeniusNorm()                
GetHashCode                             int GetHashCode()                     
GetType                                 type GetType()                        
IndexedEnumerator                       System.Collections.Generic.IEnumerab...
InfinityNorm                            double InfinityNorm()                 
InsertColumn                            MathNet.Numerics.LinearAlgebra.Gener...
InsertRow                               MathNet.Numerics.LinearAlgebra.Gener...
Inverse                                 MathNet.Numerics.LinearAlgebra.Gener...
KroneckerProduct                        MathNet.Numerics.LinearAlgebra.Gener...
L1Norm                                  double L1Norm()                       
L2Norm                                  double L2Norm()                       
LeftMultiply                            MathNet.Numerics.LinearAlgebra.Gener...
LowerTriangle                           MathNet.Numerics.LinearAlgebra.Gener...
Modulus                                 MathNet.Numerics.LinearAlgebra.Gener...
Multiply                                MathNet.Numerics.LinearAlgebra.Gener...
Negate                                  MathNet.Numerics.LinearAlgebra.Gener...
NormalizeColumns                        MathNet.Numerics.LinearAlgebra.Gener...
NormalizeRows                           MathNet.Numerics.LinearAlgebra.Gener...
PermuteColumns                          System.Void PermuteColumns(MathNet.N...
PermuteRows                             System.Void PermuteRows(MathNet.Nume...
PointwiseDivide                         MathNet.Numerics.LinearAlgebra.Gener...
PointwiseMultiply                       MathNet.Numerics.LinearAlgebra.Gener...
Random                                  MathNet.Numerics.LinearAlgebra.Gener...
Rank                                    int Rank()                            
Row                                     MathNet.Numerics.LinearAlgebra.Gener...
RowEnumerator                           System.Collections.Generic.IEnumerab...
SetColumn                               System.Void SetColumn(int columnInde...
SetDiagonal                             System.Void SetDiagonal(MathNet.Nume...
SetRow                                  System.Void SetRow(int rowIndex, Mat...
SetSubMatrix                            System.Void SetSubMatrix(int rowInde...
Stack                                   MathNet.Numerics.LinearAlgebra.Gener...
StrictlyLowerTriangle                   MathNet.Numerics.LinearAlgebra.Gener...
StrictlyUpperTriangle                   MathNet.Numerics.LinearAlgebra.Gener...
SubMatrix                               MathNet.Numerics.LinearAlgebra.Gener...
Subtract                                MathNet.Numerics.LinearAlgebra.Gener...
ToArray                                 double[,] ToArray()                   
ToColumnWiseArray                       double[] ToColumnWiseArray()          
ToRowWiseArray                          double[] ToRowWiseArray()             
ToString                                string ToString(), string ToString(s...
Trace                                   double Trace()                        
Transpose                               MathNet.Numerics.LinearAlgebra.Gener...
TransposeAndMultiply                    System.Void TransposeAndMultiply(Mat...
TransposeThisAndMultiply                MathNet.Numerics.LinearAlgebra.Gener...
UpperTriangle                           MathNet.Numerics.LinearAlgebra.Gener...

Nov 4, 2011 at 1:22 AM
Edited Nov 4, 2011 at 1:23 AM

Awesome, thanks a lot for sharing!

Btw, the F# modules might be interesting in such scenarios as well, in the interactive F# shell.

Thanks,
Christoph

Jun 28, 2013 at 2:23 AM
Thanks for sharing, I published a PowerShell module that does that with a little bit of syntactic tricks to make using Math.Net with PowerShell easier, please feel free to check it out, your feedback is always welcome.

https://mathnetpowershell.codeplex.com/

Here is a sample code/output:

PS C:> $m = m "1 5;5 9"
PS C:> $m

SparseMatrix 2x2-Single 100.00 % Filled
       1            5
       5            9

PS C:> $n = i 2
PS C:> $n

SparseMatrix 2x2-Single 50.00 % Filled
       1            0
       0            1

PS C:> $n[1,1]=2
PS C:> $n

SparseMatrix 2x2-Single 50.00 % Filled
       1            0
       0            2

PS C:> inv $n

SparseMatrix 2x2-Single 50.00 % Filled
       1            0
       0          0.5

PS C:> $p = $m * $n
PS C:> $p

SparseMatrix 2x2-Single 100.00 % Filled
       1           10
       5           18

PS C:> trc $p
19
PS C:> det $p
-32
PS C:> t $p

SparseMatrix 2x2-Single 100.00 % Filled
       1            5
      10           18

PS C:> m "1 0" | t

SparseMatrix 2x1-Single 50.00 % Filled
       1
       0

PS C:> m -rows 2 -columns 1

SparseMatrix 2x1-Single 0.00 % Filled
       0
       0

PS C:> $o = m -matrixFile Samples\sampleMatrix.txt
PS C:> $o

SparseMatrix 3x3-Single 100.00 % Filled
       1            5            6
       7            8            9
       6            5            4

PS C:> $o.Diagonal()
1
8
4