-
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should Vector
support a second dimension?
#16
Comments
ConstructingLogically, an arrayed vector is a vector of arrays. Arrays are not something that currently exists in the language so I clearly need new syntax, but do I also need new semantics? Semicolon is currently used to concatenate vectors and I don't think it's a good idea to change that. Is an array also a vector? In which case, arrayed vectors are actually vectors of vectors? The vectors-of-vectors idea kind of makes sense looking at a multiple-binding
The I could use a different character for constructing arrayed-vectors, like a comma – for example: So I'm not a big fan of the square bracket syntax, but if I ditched queries (see #17) then I could re-purpose braces, a la:
and the old If I go for vectors-of-vectors – which does appear to be most orthogonal on the face of it – then I should consider that a vector can contain vectors of different length or of non-numeric types. So the following would also be valid?
Perhaps the smart thing to do under-the-hood is to have vectors of fixed-length numeric sub-vectors be a single array of doubles in Vector concatenation would consist of quickly checking the strides and types (numeric/other) of the vectors to see if they are compatible and then either writing them into a single Maths operations will need some re-thinking. What does
To be consistent with current maths operations, it would probably also need to be the case that:
I think I'd need to consider all other variants as invalid, including operations on numerical vectors with different strides – repeating elements the same way I do for vectors now sounds viable, but I think it's going to be a mistake. Likewise, I think I'd need to disallow vectors-of-vectors containing numerical vectors of different lengths in the An alternative that keeps this rule sacrosanct is actually to say that a vector-of-vectors always has a single |
IndexingGoing with the vectors-of-vectors thing, indexing should also return a vector-of-vectors to be consistent with current indexing, which returns a vector containing zero or more items from the original vector. So:
What about How do I unpack a sub-vector and index into that? Perhaps the obvious thing is a second "argument" to index brackets. So:
I am assuming here that a stride of 1 is a normal vector, so that's what the first two expressions evaluate to. It is likely to be useful to pull out an element from every sub-vector/array without knowing the length of the vector, maybe:
I could allow ranges with no start or stop to be valid and have them return a special "infinite" range object that would only be valid for indexing and would equate to "everything". This would be a bit like the special random-source
There is an argument that this might as well be extended to ranges that exclude only the stop value, i.e., Logically, any indexing operation without a second argument has an implicit
I think I'm going to say that indexing with a non-unit-stride vector is an error and returns
However, logically
Also, continuing on with the current tradition, because every value is a vector it is valid to index multiple times in a row:
|
On pseudo-random sourcesI can foresee a bunch of use cases for being able to generate pseudo-random vectors-of-vectors, like creating random coordinates. I would suggest that it is logically sensible to support:
To create a 10-length, 3-stride vector of uniform values. As these values must be repeatable, it'll require a bit of re-jigging of how pseudo-random sources are indexed. The current algorithms expect a single In fact, if I did:
then this would be identical to the current behaviour where the secondary index is 0, which would need to be the case when a secondary index is not given. Secondary indices larger than 16-bits would also still return a varying result, it would just overlap with another part of the pseudo-random-number space. Interestingly, this would provide an alternative way to get a new set of numbers instead of changing the key, i.e.:
instead of:
The former being faster as Infinite ranges would, of course, be invalid for indexing pseudo-random sources. |
Required changes
|
Something I've not considered so far is how to rotate an array back to a vector – i.e., if What would this look like? Trying to think of characters I've not already used it could be something like: Also, what does Finally, thinking again about
Seems complicated, but also makes sense… |
There are a few places where I am implicitly treating
Vector
as having a second dimension:for
loops (for a;b;c in v
)zip()
, which takes multiple vectors and produces a vector suitable for multiple-bindingsum()
andaccumulate()
polar()
andangle()
Matrix33
andMatrix44
sub-classes ofVector
Would all of this be neater if I just supported a second dimension to vectors? I hesitate to call this a "matrix" as I feel that ought to be reserved for fixed-size objects rather than the variable length objects the first four of these usages operate on. I'm going to call these "arrayed vectors" for the moment based on the C++ terminology of arrays being fixed-length and vectors being variable-length.
So the array part of an arrayed vector would be a size value that the vector length becomes a multiple of. Mostly no changes to the model would be required except for adding a new attribute that contains this array size and some changes to how indexing works.
Let's say that
zip
consumes m flat vectors (i.e., vectors with an array size of 1) and returns an arrayed vector with an array size m. Indexing or iterating over this should produce m-vectors. If multiple-bindingfor
loops were redefined such that the names are bound to each element of the vector produced by iterating over the loop source, then the overall behaviour of:stays the same. However, looping with multiple names over a flat vector would always result in binding each element to the first name and setting the other names to
null
.Functions that implicitly return arrayed vectors, like
polar()
could just explicitly do that and therefore could still be iterated over naturally:However, now one could also naturally index the results:
which would improve the various places in my code where I have ugly indexing like
coords[i*2..(i+1)*2]
. Functions likeangle()
would take 2-arrayed vectors and return flat vectors.hypot()
would take an n-arrayed m-item vector and return a flat m-item vector with the n-dimensioned hypotenuse of each item.sum()
andacculumate()
would lose their second argument and just do The Right Thing with any n-arrayed vector they are given.Matrix33
andMatrix44
are only used internally, but it would make sense for these to match 3-arrayed and 4-arrayed vectors in terms of their attributes. It might be neat to abstract out operations intoVector
that can easily work with any size of matrix – liketranslate
,scale
,vmul
,mmul
andtranspose
.The text was updated successfully, but these errors were encountered: