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

Hi,

Can someone elaborate on what is happening inside the defs here:
using-math-net-numerics-in-ghpython

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

EDIT:
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):
        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)
def copy_vector(m,ctor):
try:
    if m.RowCount==1:
        n=ctor(m.ColumnCount)
        for c in range(m.ColumnCount):
            n[c]=m[0,c]
    elif m.ColumnCount==1:
        n=ctor(m.RowCount)
        for r in range(m.RowCount):
            n[r]=m[r,0]
except:
    n=ctor(m.Count,1)
    for i in range(m.Count):
        n[1,0]=m[i]
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,

2018-05-08%2023_04_11-Grasshopper%20Python%20Script%20Editor

I define a matrix and I get output for A:
ANN_

and output for B:
ANN_

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?

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.

1 Like

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.

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.

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)

2 Likes

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