algorithmic modeling for Rhino
Hi,
I'm a beginner of programming and I want to use scipy to calculate the svd(singular value decomposition) of the input 3*3 matrix, but when I run the scipy.linalg.svd command there is a runtime error says that "expected matrix", as show in the picture. Is there any way to fix the problem?
Thanks,
Summer
Tags:
Hi Summer
I believe that this is due to scipy not having knowledge about the shape of the data structure used by Grasshopper to store matrices. Basically, you would have to create a new scipy matrix from the Grasshopper one, in order to feed it to scipy.
Does this help?
Thanks
Giulio
--
Giulio Piacentino
for Robert McNeel & Associatesgiulio@mcneel.com
Hi Giulio,
This is very helpful, thanks. Though it may be a little difficult for me, could you please give me some more information on how to create a new scipy matrix from the Grasshopper one? Or if I move my work into the python editor would avoid problems like this?
Thanks
Summer
Hi again Summer
I do not currently have scipy installed, so this might be a bit tricky for me to show. But it's really not that complicated.
Information about using numpy/scipy is available by their developers. I took the liberty to look it up on a search engine for you:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.matrix.html
You can see that it will accept a list of lists as input for one matrix. So we could just transform our Grasshopper Matrix into one like this:
def matrix_to_list_of_lists(m):
result = []
for i in range(m.ColumnCount):
row = []
for j in range(m.RowCount):
value = m[i,j]
row.append(value)
result.append(row)
return result
Singular Value Decomposition information is here:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.linalg.sv...
It returns a http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
among other things. So, I looked up how to get a specific value from it.
And wrote this untested script... (I might have swapped rows and columns, I am not sure, you should test this -- but it should give you an idea of how to proceed)
I hope this helps,
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Hi Giulio
Thanks a lot for your fast reply, the info and script is truly helpful!
I also tried to move my work into the python editor of rhino, and it seems that it works fine with numpy.array and svd.
Thanks again
Summer
Hi Giulio,
do you have a hint how to do this in C# in Visual Studio?
I want to get the rowcount / columncount of an GH_Matrix, but i'm not sure how to do it.
This is the code:
protected override void SolveInstance(IGH_DataAccess DA)
{
GH_Matrix inputMatrix = new GH_Matrix();
if(!DA.GetData<GH_Matrix>(0, ref inputMatrix)) return;
GH_Number _out = new GH_Number();
_out.CastFrom(inputMatrix.ColumnCount); <----doesn't work'
DA.SetData(0, _out);
}
Edit:
I ultimately want to turn my GH_Matrix into a Rhino Matrix since <correct me if I'm wrong> there are some more tools to treat Rhino Matrices
Thanks a lot in advance
Benjamin
Oh sorry never mind,
I just figured out, that it resizes an arbitrary Rhino Matrix if I use
Matrix RhinoMatrix = new Matrix(0,0);
inputMatrix.CastTo<Matrix>(out RhinoMatrix);
Then I can use the rowcount / columncount for Rhino Matrices
Hi Benjamin,
out means "will be assigned in every case". So, you do not need to specify any matrix at all. Like so will work:
Matrix RhinoMatrix = default(Matrix);
if(!inputMatrix.CastTo<Matrix>(out RhinoMatrix)) throw Exception("No matrix");
because Matrix is a class, then also this will work:
Matrix RhinoMatrix = null;
if(!inputMatrix.CastTo<Matrix>(out RhinoMatrix)) throw Exception("No matrix");
Btw, it would be better if you started a new conversation next time. Reviving old ones makes it look at first as though somebody else has already replied.
I hope this helps,
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Thanks a lot Giulio,
I'll keep it in mind (also the part about opening a new thread)
See you......
Hey Giulio! Its an old post but exactly what I´m looking for. Your script for transforming the GH Matrix to numpy works perfect for me. But how I do it backwards? I did the SVD like above and it gives me Lists with the different elements, but no vectors or matrices. I´m a real beginner and would appreciate your help! I attached the code I use.
import clr
clr.AddReference("mtrand")
import numpy as np
import rhinoscriptsyntax as rs
from Rhino.Geometry import Matrix
def matrix_to_list_of_lists(A):
result = []
for i in range(A.ColumnCount):
row = []
for j in range(A.RowCount):
value = A[i,j]
row.append(value)
result.append(row)
return result
def list_of_lists_to_matrix(nda):
shape = nda.shape
result = Matrix(shape[0], shape[1])
for i in range(shape[0]):
for j in range(shape[1]):
result[i,j] = nda.item((i,j))
return result
ll = matrix_to_list_of_lists(A)
a = np.array(ll)
U, s, V = np.linalg.svd(a)
This is probably a good case for a new topic.
In general, you construct types with their constructor ( Matrix() ) and assign element ( [x, y] = 12.3).
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Hey! I know that I normally have to open a new discussion. But this was exactely what I needed. I already got it with your hint so I don´t need to ask further.
Thanks for that!
I'm happy about this. If you want to post also your answer, it might help some other people doing the same :)
Cheers for this,
Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by