[Solved] MathNet.numerics GH_Python using panel component to create matrix



Can someone elaborate on what is happening inside the defs here:

@piac, I understand the result but what is happening inside the defs?

I figured out that ctor is a ‘constructor’, not sure what that means but I’ll read about it. You can ignore the first part of this post.

def copy_matrix(m, ctor):
n = ctor(m.RowCount, m.ColumnCount)
for r in range(m.RowCount):
    for c in range(m.ColumnCount):
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)
def copy_vector(m,ctor):
    if m.RowCount==1:
        for c in range(m.ColumnCount):
    elif m.ColumnCount==1:
        for r in range(m.RowCount):
    for i in range(m.Count):
return n
def to_rh_vector(dotnet_v):
from Rhino.Geometry import Matrix as rgm
return copy_vector(dotnet_v,rgm)
def to_dotnet_vector(rhinocommon_v):
from MathNet.Numerics.LinearAlgebra.Double import Vector as dnv
dense = dnv.Build.Dense
return copy_vector (rhinocommon_v,dense)

So about this code what is ctor?
If it is a variable how can you use it without defining it?

Another question is,


I define a matrix and I get output for A:

and output for B:

However, when I try to convert from output A to GH.matrix I get:

Is there a way to define the matrix as a string from the “panel” component?

(qythium) #2

Don’t pass data through a panel component, it turns everything into text. You can parse the text output back into numbers but there’s no reason to do it here, and you will lose accuracy in floating point numbers.

You should put this at the top of your post, not after people have already spent a few minutes following the links, trying to parse the code and reached the bottom :neutral_face:

Also your python code is not formatted properly, parts inside the def should be indented.


Thanks, yes you’re right. TBH I thought about removing the first part and changing the name, but I thought someone could use the code if he’s not aware of the old GH forum thread.

(Giulio Piacentino) #4

Great, it seems you are all set!


Not really, instead of inserting python component everytime I want to create dense matrix, I would like to create them from a panel. or be able to create it as normal GH matrix component.

How can I do that? @qythium only said it’s not advisable.

the reason is determined by what I want to achieve, not by what is the common practice.

(Giulio Piacentino) #7

The code you need is in the definition already. I put it together so it does what you want.
matricesdotnet-solve.gh (12.2 KB)


Thanks Guilio, it’s not exactly what I expected, but it does give me some ideas how to proceed.