-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
More missing numpy and matlab #649
More missing numpy and matlab #649
Conversation
`union` returns the unique, unsorted Tensor of values that are found in either of the two input Tensors. Note that an equivalent function exists both in `numpy` (where it is called `union1d`) and in `Matlab` (where it is called `union1d`). However, those functions always sort the output, while Arraymancer's version does not. To replicate the same behavior, simply apply `sort` to the output of this function.
02be765
to
28c8ec1
Compare
let data = toSeq(a) | ||
result = toTensor(data, shape) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose we could also do:
result = newTensorUninit[T](a.card)
for i, x in a:
result[i] = x
to avoid the double copy (set -> seq -> tensor)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I'll make that change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interestingly a performance measurement using timeit
showed that the original version was faster on Windows. We will leave this as is for now while we investigate what is going on (and if we find out why we'll update this in a separate PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, let's investigate it further after this PR.
Maybe we can reuse the internal logic? In general |
You are right. Let me remove those from this PR. We can discuss offline and I can create another separate PR later. |
This will let us avoid having to convert HashSets into seqs before converting them into tensors. Note that this also improves a little the docstrings of a couple of the existing `toTensor` procedures.
`intersection` returns the "intersection" of 2 Tensors as an unsorted rank-1 Tensor. Note that an equivalent function exists both in `numpy` (where it is called `intersect1d`) and in `Matlab` (where it is called `intersect`). However, those functions always sort the output, while Arraymancer's version does not. To replicate the same behavior, simply apply `sort` to the output of this function. Also note that to implement this feature we moved (and made public) the existing, private toHashSet procedure from spatial/distances.nim into tensor/initialization.nim.
`setDiff` returns the (symmetric or non symmetric) "difference" between 2 Tensors as an unsorted rank-1 Tensor. Note that an equivalent function exists both in `numpy` (where it is called `setdiff1d`) and in `Matlab` (where it is called `setdiff`). However, those functions always sort the output, while Arraymancer's version does not. To replicate the same behavior, simply apply `sort` to the output of this function.
`find` (which is used to implement `contains`) was already supported (since `system.find` is generic and works with Tensors) but was untested, so this also adds a test for it.
This was a useful std/math function that we did not support yet.
28c8ec1
to
381c5ba
Compare
I've updated the PR as discussed here. |
381c5ba
to
2c7752b
Compare
I just pushed a fix to the original (non gemm) version which solves a problem with convolving a slice (was using |
# The folling export is needed to avoid an compilation error in | ||
# algorithms.nim/intersection() when running the test_algorithms test: | ||
# `Error: type mismatch - Expression: items(s1)` | ||
export sets |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that we could also get away with adding a bind sets.items
inside of intersection
and setDiff
. However, given that we now depend on sets
, I don't see an issue exporting it too.
Fix typo in export comment & add alternative for reader
Merging, we can look into the |
This PR adds multiple functions that are available both in numpy and Matlab:
union
: returns the unique, unsorted Tensor of values that are found in either of the two input Tensors.intersection
: returns the "intersection" of 2 Tensors as an unsorted rank-1 Tensor.setDiff
: returns the (symmetric or non symmetric) "difference" between 2 Tensors as an unsorted rank-1 Tensor.toHashSet
: convert a Tensor into HashSet.contains
(and thusin
andnotin
).sub2ind
andind2sub
: convert "subscript" sequences into linear tensor indexes and vice-versa.almostEqual
: missing from nim's std/math.