Closed pgkirsch closed 4 years ago
Whoops, I submitted the wrong MWE! Fixing...
Corrected the example. It might just be user error, i.e. that I need to index differently, but from what I can tell as_view
doesn't handle nested vectorizing?
The following works (unexpectedly) fine:
In [5]: with Vectorize(4):
...: with Vectorize(2):
...: t = Test()
...: y = t.as_view()[3].x
...:
It does handle nested vectorization! But to match vectorization, it indexes a little funny. Your first (leftmost) vectorization is indexed first/from the right, like so:
from gpkit import *
class Test(Model):
def setup(self):
self.x = Variable("x")
with Vectorize(4):
with Vectorize(2):
t = Test()
y = t.as_view()[3][1].x
print(y)
outputs
Test.x[:,3]
Test.x[1,3]
I did this for a bit of a reason, but an actual usecase beats that any day.
In [4]: with Vectorize(1):
...: with Vectorize(2):
...: t = Test()
...: y = t.as_view()[1].x
I think what you were going for with this example was something like this?
with Vectorize(1):
with Vectorize(2):
t = Test()
y = t.as_view()[1,:].x
print(y)
outputting
Test1.x[1,:]
Ah perfect thank you, user error as always!
Interesting side effect: this seems to require a substitution to change shape to (2,1).
with
'fixedvar': [1, 2.2] # previously worked
I get
ValueError: cannot substitute array of shape (2,) for variable fixedvar[1,0] of shape (2, 1).
and with
'fixedvar': [[1, 2.2]]*numberofoutervectorizations # what I was thinking I'd use
I get
ValueError: cannot substitute array of shape (1, 2) for variable fixedvar[1,0] of shape (2, 1).
so
'fixedvar': np.array([[1], [2.2]]),
it is.
huh, that's not ideal.
Yes, it seems weird to do
Vectorize(1)
but it would be nice if a Vectorize-able model can also work when only being used for point-design.MWE: