# GHpython - looping through matrix to extract values

Hey everyone,

So I have managed to create a matrix with different solar radiation values relative to the orientation. These values are divided into each month (therefore a matrix has been created). Now I want to extract these values so that I can calculate with them. I have a list with orientation and I want to loop through the list using this orientation-list to extract the values. At the same time I’m trying to loop through with the months, but I must admit that I have come up short when trying to loop through a matrix as I have never done it before. I’ll link a picture and the file and hopefully some of you know what to do.

Working_Weatherfile.gh (39.6 KB)

I would say the error message is obvious. If your jacket has zero pockets, it makes no sense to put your wallet into the first pocket. Although that weird Numpy port into IronPython looks quite strange as well…

In order to solve the issue, create a jacket with at least 1 pocket. If you want one pocket for each gadget, you need to match the amount of pockets. Hope this analogy helps.

Can you make an example what you mean with jacket and pocket? I’m not sure how you would define it.

The ironpython is as far as I can tell just how it displays arrays and matrices from numpy. It is a matrix and I can do radiation_solar_monthly[3,1] and get a value matching the forth month and orientation = 1.

Maybe this answer was too abstract…

1. Foremost, np.matrix is obsolete.Use np.array.

2. Apart from that, arrays (and “matrices”) are fixed in size. When you initialize it, you need to specify the count in each dimension. You can do that by prefilling it with items or calling np.empty, np.zeros or np.ones.

3. If you access your array/matrix with an index, you need to make sure the index is lower than the size of the array/matrix in the given dimension

Execute this code and you get your error message

``````import numpy as np
data = np.matrix([])
data[0, 0] = 5
``````

``````import numpy as np
data = np.zeros((1,1))
data[0, 0] = 5
print(data)
``````
1 Like

Tom is referring to how there historically hasn’t been any “good” methods for implementing `numpy`/`scipy` in GHPython. Being that it implements the C#/.NET IronPython version of Python. And not the standard CPython implementation (at least, not quite yet, which should help these incompatibility issues tremendously). Out of curiosity, how are you running `numpy`/`scipy` in GHPython?

1 Like

I’m using GHremote, which was a lot of work only to get numpy and scipy working. If there is any better way please let me know. I see ghremote as an issue for my script since I want it to be easy for people to use and the need for extra plugins/programs running such as ghpython is a barrier.

If you look for something like NumPy in the world of DotNet, then go for MathNet.
numerics.mathdotnet.com

IronPython also offers the default Net arrays. If it’s just about storing values.

``````import System
array = Array[Array[int]]( ( (1,2), (3,4) ) )
``````

Rhino also offers a Matrix class:

Thank - I’ll take a look at that. I do use scipy for interpolation though, but is there a good alternative for that as well?

https://numerics.mathdotnet.com/Interpolation.html

But of course you could implement ‘Newton’ etc. quite easy on your own. It’s not rocket science.

2 Likes

I would echo what Tom said: Implement Math.NET in GHPython for now (which also has the added benefit of being easily portable to C# if you require/want to build a “proper” plugin at some point). Then, when CPython is natively supported in Rhino 8 Grasshopper in the hopefully not so distant future, you can likely implement numpy/scipy quite straightforwardly.

Thanks - I’m not so familiar with Math.NET, but I’ll take a look at the tutorial. One question though - would my script end up working for others opening it without having to install that same things as I do. Or just some way I can “bake” my final script so it works without extra plugins? Sorry for my newbie questions ^^

Not unless you can package everything into a compiled plugin. Also, it depends on the type of dependency you implement. I’ve tried a lot of different issuing methods over the years, but have come around to keeping it as simple and transparent as possible. Which basically means instructing the user to unzip/move .dll assemblies to the standard Grasshopper library:

`C:\Users\USERNAME\AppData\Roaming\Grasshopper\Libraries`

And Python modules/packages to the standard scripts folder:

`C:\Users\USERNAME\AppData\Roaming\McNeel\Rhinoceros\7.0\scripts`

This makes them simple/terse to implement in GHPython. For instance to implement `MIConvexHull` you go:

``````import clr
import Grasshopper as gh
And to implement `networkx` you simply go (because the default script folder is already in the path):
``````import networkx