Skip to content

Commit

Permalink
[DOC] Updates on Numpy-to-DaphneLib translation.
Browse files Browse the repository at this point in the history
- As right indexing is available in DaphneLib now, some more Numpy operations can be translated to DaphneLib.
- Updated the guidelines on translating Numpy to DaphneLib.
- Some small changes to the sample-kernel were necessary (otherwise warnings when instantiating it with an unsigned value type).
  • Loading branch information
pdamme committed Jul 16, 2024
1 parent ae21092 commit 6e04253
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 13 deletions.
16 changes: 6 additions & 10 deletions doc/DaphneLib/Numpy2DaphneLib.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ Porting for other Numpy versions may be possible by following similar lines of t

*Example 2* (draw 2d array from 1d array with replacement)

- Note that this example doesn't work in DaphneLib yet, since right indexing is still missing (see #657).

- Numpy

```python
Expand All @@ -306,13 +304,11 @@ Porting for other Numpy versions may be possible by following similar lines of t

```python
# Assumes that `X` is a column (m x 1) matrix.
Y = X[dc.sample(X.nrow(), 3 * 2, True), ].reshape(3, 2)
Y = X[dc.sample(X.nrow(), 3 * 2, True), :].reshape(3, 2)
```

- `numpy.`**`random.permutation`**`(x)`

*Note: Doesn't work in DaphneLib yet, since right indexing is still missing (see #657).*

*Parameters*

- `x`: supported
Expand All @@ -329,7 +325,9 @@ Porting for other Numpy versions may be possible by following similar lines of t
- DaphneLib

```python
Y = X[dc.sample(X.nrow(), X.rnow(), False), ]
nr = X.nrow()
nc = X.ncol()
Y = X.reshape(X.ncell(), 1)[dc.sample(X.ncell(), X.ncell(), False), :].reshape(nr, nc)
```

- `numpy.`**`repeat`**`(a, repeats, axis=None)`
Expand Down Expand Up @@ -376,14 +374,12 @@ Porting for other Numpy versions may be possible by following similar lines of t

- `numpy.`**`tile`**`(A, reps)`

*Note that this doesn't work in DaphneLib yet, as right indexing is still missing (see #657).*

*Parameters*

- `A`: supported
- Only 2d matrices are supported.
- `reps`: supported
- Only scalars and 2x1 matrices are supported, i.e., repetition along row dimension or row and column dimension.
- Only scalars and 2x1 matrices are supported, i.e., repetition along row and/or column dimension.

*Example*

Expand All @@ -396,7 +392,7 @@ Porting for other Numpy versions may be possible by following similar lines of t
- DaphneLib

```python
Y = X[dc.seq(0, X.nrow() * 3).mod(X.nrow()), dc.seq(0, X.ncol() * 2).mod(X.ncol())]
Y = X[dc.seq(0, X.nrow() * 3 - 1).mod(X.nrow()), dc.seq(0, X.ncol() * 2 - 1).mod(X.ncol())]
```

<!-- TODO
Expand Down
5 changes: 3 additions & 2 deletions src/runtime/local/kernels/Sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ struct Sample<DenseMatrix<VT>, VT> {
if (range <= 0)
throw std::runtime_error("range must be > 0");
if (!withReplacement && !std::is_floating_point<VT>::value &&
range < size) {
range < static_cast<VT>(size)) {
throw std::runtime_error("if no duplicates are allowed, "
"then must be range >= size");
}
Expand Down Expand Up @@ -127,7 +127,8 @@ struct Sample<DenseMatrix<VT>, VT> {
// to create non-duplicate numbers (see Knuth's algorithm).
else {
VT *valuesRes = res->getValues();
int64_t iRange, iSize;
VT iRange;
int64_t iSize;
iSize = 0;

for (iRange = 0; iRange < range && iSize < size; iRange++) {
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/local/kernels/kernels.json
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,8 @@
},
"instantiations": [
[["DenseMatrix", "double"], "double"],
[["DenseMatrix", "int64_t"], "int64_t"]
[["DenseMatrix", "int64_t"], "int64_t"],
[["DenseMatrix", "size_t"], "size_t"]
]
},
{
Expand Down

0 comments on commit 6e04253

Please sign in to comment.