algorithmic modeling for Rhino
some users reported that it is increasingly difficult to get access to numpy/scipy in IronPython on a 64-bit machine. It used to be simple on a 32-bit one. While hoping for the best from enthought, here is what we can do.
Because IronPython is natively able to support .Net libraries, I will write this tutorial about installing Math.Net Numerics and then using it successfully from GhPython.
1. Get nuget. This little command-line utility will ensure that you have the lastest version of Math.Net. Get the "Command-Line Utility Latest 3.X : Direct Download"
2. Run the utility to get Math.Net. You can simply place nuget.bat aside nuget.exe, double-click the .bat file, and it will create a folder called "MathNet.Numerics" on your desktop. The contents of this file are:
nuget install MathNet.Numerics -OutputDirectory %UserProfile%\Desktop\ -ExcludeVersion
3. Check that you have a folder called "MathNet.Numerics" on your desktop.
1. Open Grasshopper, add a Python component to your definition, then double click it to open the editor.
2. We need to find and import the .dll. This assumes that you installed MathNet.Numerics on your desktop. If you chose another place, you will have to update the location accordingly.
from clr import AddReferenceToFileAndPath as addref
location = os.path.join(os.path.expanduser("~"),
It is also possible to specify the local path of the Grasshopper definition, by using
location = os.path.join(os.path.dirname(ghenv.Component.OnPingDocument().FilePath), 'MathNet.Numerics.dll')
3. We import the main namespace and also define a convenient function to create arrays, so that their creation looks more pythonic
import MathNet.Numerics.LinearAlgebra as la
from System import Array as sys_array
def array(*x): return sys_array[float](x) #float is equivalent to .Net double
4. I translate the http://numerics.mathdotnet.com/LinearEquations.html example from Math.Net.
Rather than using the .Net generics [float] specification in IronPython where C# has <double>, which breaks autocompletion in Grasshopper, I chose to use the Double namespace, which has classes that are specialized for floats (double in C#).
A = la.Double.Matrix.Build.DenseOfRowArrays(
b = la.Double.Vector.Build.DenseOfArray(array(1, -2, 0))
x = A.Solve(b)
A1 = la.Double.Matrix.Build.DenseOfRowArrays(
array(3.0, 4.0, -1.0, 0.0),
array(4.0, 5.0, 0.0, -1.0),
array(5.0, 6.0, 0.0, 0.0),
array(6.0, 7.0, 0.0, 0.0)
b1 = la.Double.Vector.Build.DenseOfArray(array(0, 0, 20, 0))
x1 = A1.Solve(b1)
5. In order to export the Math.Net Matrix class in Grasshopper, and to use the RhinoCommon matrix class with Math.Net, we need to convert between the two. They happen to be accessed in the exact same manner. So I just defined a conversion method to copy the matrix, and define the constructor in two appropriate functions:
def copy_matrix(m, ctor):
n = ctor(m.RowCount, m.ColumnCount)
for r in range(m.RowCount):
for c in range(m.ColumnCount):
from Rhino.Geometry import Matrix as rgm
from MathNet.Numerics.LinearAlgebra.Double import Matrix as dnm
dense = dnm.Build.Dense
There is a lot more documentation and you can find very many examples at the Math.Net Numerics homepage.
Happy matrix solving!
for Robert McNeel & Associates