Grasshopper

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.


USAGE

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:


Views: 8117

Comment

You need to be a member of Grasshopper to add comments!

Comment by Fernando Ferraz Ribeiro on February 12, 2022 at 4:58am

Hi, Giulio.

I'm trying to replicate dose steps.

nuget.exe and the bat file are in the same folder on my machine.

but I keep running into an error:

D:\Apps\nuget>nuget install MathNet.Numerics -OutputDirectory C:\Users\User\Desktop\ -ExcludeVersion
Feeds used:
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

Installing package 'MathNet.Numerics' to 'C:\Users\User\Desktop\'.
Unable to find package 'MathNet.Numerics'

I'm also trying to install the package below. Having similar problems:

https://github.com/SciSharp/Numpy.NET

Can you give me any direction?

Best regards,

Fernando

Comment by charles trevino on January 24, 2022 at 8:20am

It's fantastic to get your helpful knowledge here. This game introduces a new 2 player games theme. There are hundreds of online games on a range of themes in this intriguing game collection. You will have exciting entertaining moments if you explore and experience.

Comment by David Bachman on May 6, 2020 at 10:00am

Can someone help me out getting this to work on a mac?

Comment by Giulio Piacentino on October 28, 2016 at 4:03am

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

Comment by Hao Sun on October 26, 2016 at 3:57pm

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.

Comment by David Chen on November 4, 2015 at 12:00am

Thanks Giulio. I just mimic your methods and add these to the "def"

And here is an example to extract eigenvalues and eigenvectors for others who might need it.

Comment by Anders Holden Deleuran on October 27, 2015 at 6:20am

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

Comment by Florian ROCHEREAU on October 23, 2015 at 6:12am

Great news Giulio, thank you !

Comment by djordje on October 22, 2015 at 10:41am

Very useful post.
Thank you for sharing it.

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service