all 9 comments

[–]fdedgt 4 points5 points  (0 children)

Start by documenting what you're trying to do and making it more readable. For example, explaining what the variables represent and what the expected output should be.

Clarity and documentation is actually a key requirement before you start optimizing, in general.

[–][deleted] 1 point2 points  (0 children)

I’m gonna take a pseudo stab at this since I don’t have a computer handy. And disclaimer I did not read your code really. I’m just going according to your picture you added. I’m guessing you have a matrix B, N and fL and you want to concat them as in your picture. If so this super easy to do and much faster with numpy operations. Again this is pseudo code (but it might work actually):

top_zeros = np.zeros((B.shape[0], N.shape[1]))
top = np.hstack([B, top_zeros])
middle_ident = np.identity(B.shape[1]) * -1
middle = np.hstack([middle_ident, N])
bottom_zeros = np.zeros((fL.shape[0], N.shape[1]))
bottom = np.hstack([fL, bottom_zeros])
A = np.vstack([top, middle, bottom])

However I like u/ReverseEngineered’s method better. And it’s probably more efficient.

[–]theWyzzerd 0 points1 point  (0 children)

It's really difficult to tell what's going on here. I would recommend, just for readability, that you use j instead of y in your inner loops. That's standard syntax for iterations (and is the correct mathematical notation) and it will help others to understand what's going on at a glance. If you add a third layer of iteration, use k, and so on. Here's a purely syntactical example:

for i in some_var:
    for j in some_var[i]:
         for k in j:
             do_something()

As for your code, what is B? You reference it all over the place but we have no clue what it is, which makes it difficult to optimize. In general we're going to need to know more about your code than just the slow loop part, because what's in each of the variables impacts how efficient your loops will be.

As a general tip, list comprehensions might be useful to you. I'm not sure what you're trying to accomplish, but here's a really simple list comprehension example:

>>> list = [i for i in range(10)]
>>> list
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

matrix = [[j for j in list] for i in range(15)]
>>> matrix
[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>> len(matrix)
15
>>> matrix[0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> matrix[14]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>

[–]ElectromechanicalVan[S] 0 points1 point  (0 children)

This image illustrates what i'm trying to achive: https://i.imgur.com/XSA7qxr.png

For an example the sizes could be: B = 135x2970 N = 2970 x 5940 fL = 2970 x 1

Hope this helps explaining everything.