algorithmic modeling for Rhino
Hi all,
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.
Expected time to install and have sample working: ~5 minutes.
INSTALLATION
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.
import os
from clr import AddReferenceToFileAndPath as addref
location = os.path.join(os.path.expanduser("~"),
'Desktop/MathNet.Numerics/lib/net40/MathNet.Numerics.dll')
addref(location)
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(
array(3, 2,-1),
array(2,-2,4),
array(-1,.5,-1)
)b = la.Double.Vector.Build.DenseOfArray(array(1, -2, 0))
x = A.Solve(b)
print x#second example
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):
n[r,c]=m[r,c]
return n
def to_rh_matrix(dotnet_m):
from Rhino.Geometry import Matrix as rgm
return copy_matrix(dotnet_m,rgm)
def to_dotnet_matrix(rhinocommon_m):
from MathNet.Numerics.LinearAlgebra.Double import Matrix as dnm
dense = dnm.Build.Dense
return copy_matrix(rhinocommon_m,dense)
There is a lot more documentation and you can find very many examples at the Math.Net Numerics homepage.
Happy matrix solving!
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Downloads:
Comment
Hi Hao Sun
if you want to get help with a specific problem, you will possibly get it if you follow the recommendations in How to get help on this forum!
Especially important: 1. Create new topic, 2. Post what you have, 3. Include links, 4. Include a definition that you are trying to solve
Thanks a lot for your recommendation. Math.Net looks good.
However, it seems that there are not too much examples of Math.Net. Do you know where we can find a tutorial or example code of linear sparse solver? I try to use it, but I don't understand the configuration of the solver.
Excellent, thanks Giulio. I just tested your definition with a compiled assembly I got from Dave Reeves when we were up in Aarhus. Seems to work really well:
I really like your design of wrapping the .NET array instantiation in a function. Very clean and neat. The nuget tutorial is also greatly appreciated.
Best,
Anders
Great news Giulio, thank you !
Very useful post.
Thank you for sharing it.
© 2019 Created by Scott Davidson. Powered by
You need to be a member of Grasshopper to add comments!
Join Grasshopper