Jump to page: 1 2
Thread overview
mir: How to change iterator?
Apr 14, 2020
jmh530
Apr 16, 2020
Basile B.
Apr 16, 2020
Basile B.
Apr 16, 2020
jmh530
Apr 19, 2020
jmh530
Apr 20, 2020
9il
Apr 20, 2020
jmh530
Apr 17, 2020
WebFreak001
Apr 17, 2020
WebFreak001
Apr 19, 2020
9il
Apr 19, 2020
9il
April 14, 2020
In the code below, I multiply some slice by 5 and then check whether it equals another slice. This fails for mir's approxEqual because the two are not the same types (yes, I know that isClose in std.math works). I was trying to convert the y variable below to have the same double* iterator as the term on the right, but without much success. I tried std.conv.to and the as, slice, and sliced functions in mir.

I figure I am missing something basic, but I can't quite figure it out...


/+dub.sdl:
dependency "mir-algorithm" version="~>3.7.28"
+/

import mir.math.common: approxEqual;
import mir.ndslice.slice : sliced;

void main() {
    auto x = [0.5, 0.5].sliced(2);
    auto y = x * 5.0;

    assert(approxEqual(y, [2.5, 2.5].sliced(2)));
}
April 16, 2020
On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:
> In the code below, I multiply some slice by 5 and then check whether it equals another slice. This fails for mir's approxEqual because the two are not the same types (yes, I know that isClose in std.math works). I was trying to convert the y variable below to have the same double* iterator as the term on the right, but without much success. I tried std.conv.to and the as, slice, and sliced functions in mir.
>
> I figure I am missing something basic, but I can't quite figure it out...
>
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.28"
> +/
>
> import mir.math.common: approxEqual;
> import mir.ndslice.slice : sliced;
>
> void main() {
>     auto x = [0.5, 0.5].sliced(2);
>     auto y = x * 5.0;
>
>     assert(approxEqual(y, [2.5, 2.5].sliced(2)));
> }

`approxEqual` cant work with ranges. If you look at the signature there is a use of the constructor syntax, e.g const `T maxRelDiff = T(0x1p-20f)` so when `T` is not a basic FP type that just does not compile (note the error message if you try with .array on both operands)

I'd just use zip(...).each!(...), e.g

    assert(zip(y, [2.5, 2.5].sliced(2)).each!(a => assert(approxEqual(a[0], a[1]))));

But I don't know MIR at all.
April 16, 2020
On Thursday, 16 April 2020 at 19:56:21 UTC, Basile B. wrote:
> On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:
>> [...]
>
> `approxEqual` cant work with ranges. If you look at the signature there is a use of the constructor syntax, e.g const `T maxRelDiff = T(0x1p-20f)` so when `T` is not a basic FP type that just does not compile (note the error message if you try with .array on both operands)
>
> I'd just use zip(...).each!(...), e.g
>
>     assert(zip(y, [2.5, 2.5].sliced(2)).each!(a => assert(approxEqual(a[0], a[1]))));

And remove the extra assert() BTW... I don't know why this is accepted.
April 16, 2020
On Thursday, 16 April 2020 at 19:59:57 UTC, Basile B. wrote:
> [snip]
>
> And remove the extra assert() BTW... I don't know why this is accepted.

Thanks, I hadn't realized about approxEqual. I think that resolves my near-term issue, I would need to play around with things a little more to be 100% sure though.

That being said, I'm still unsure of what I would need to do to get the following code to compile.

/+dub.sdl:
dependency "mir-algorithm" version="~>3.7.28"
+/

import mir.ndslice;

void foo(Iterator, SliceKind kind)(Slice!(Iterator, 1, kind) x, Slice!(Iterator, 1, kind) y) {
    import std.stdio : writeln;
    writeln("here");
}

void main() {
    auto x = [0.5, 0.5].sliced(2);
    auto y = x * 5.0;
    foo(x, y);
}
April 17, 2020
On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:
> In the code below, I multiply some slice by 5 and then check whether it equals another slice. This fails for mir's approxEqual because the two are not the same types (yes, I know that isClose in std.math works). I was trying to convert the y variable below to have the same double* iterator as the term on the right, but without much success. I tried std.conv.to and the as, slice, and sliced functions in mir.
>
> I figure I am missing something basic, but I can't quite figure it out...
>
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.28"
> +/
>
> import mir.math.common: approxEqual;
> import mir.ndslice.slice : sliced;
>
> void main() {
>     auto x = [0.5, 0.5].sliced(2);
>     auto y = x * 5.0;
>
>     assert(approxEqual(y, [2.5, 2.5].sliced(2)));
> }

Use std.algorithm:equal for range compare with approxEqual for your comparator:

assert(equal!approxEqual(y, [2.5, 2.5].sliced(2)));
April 17, 2020
On Friday, 17 April 2020 at 08:40:36 UTC, WebFreak001 wrote:
> On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:
>> [...]
>
> Use std.algorithm:equal for range compare with approxEqual for your comparator:
>
> assert(equal!approxEqual(y, [2.5, 2.5].sliced(2)));

simplified:

assert(equal!approxEqual(y, [2.5, 2.5]));
April 19, 2020
On Friday, 17 April 2020 at 08:40:36 UTC, WebFreak001 wrote:
> On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:
>> [...]
>
> Use std.algorithm:equal for range compare with approxEqual for your comparator:
>
> assert(equal!approxEqual(y, [2.5, 2.5].sliced(2)));

or mir.algorithm.iteration: each that can work with nd-ranges.
April 19, 2020
On Sunday, 19 April 2020 at 02:56:30 UTC, 9il wrote:
> On Friday, 17 April 2020 at 08:40:36 UTC, WebFreak001 wrote:
>> On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:
>>> [...]
>>
>> Use std.algorithm:equal for range compare with approxEqual for your comparator:
>>
>> assert(equal!approxEqual(y, [2.5, 2.5].sliced(2)));
>
> or mir.algorithm.iteration: each that can work with nd-ranges.

EDIT:

 or mir.algorithm.iteration: equal that can work with nd-ranges.

April 19, 2020
On Thursday, 16 April 2020 at 20:59:36 UTC, jmh530 wrote:
> [snip]
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.28"
> +/
>
> import mir.ndslice;
>
> void foo(Iterator, SliceKind kind)(Slice!(Iterator, 1, kind) x, Slice!(Iterator, 1, kind) y) {
>     import std.stdio : writeln;
>     writeln("here");
> }
>
> void main() {
>     auto x = [0.5, 0.5].sliced(2);
>     auto y = x * 5.0;
>     foo(x, y);
> }

This is really what I was looking for (need to make allocation, unfortunately)

/+dub.sdl:
dependency "mir-algorithm" version="~>3.7.28"
+/

import mir.ndslice;

void foo(Iterator, SliceKind kind)(Slice!(Iterator, 1, kind) x, Slice!(Iterator, 1, kind) y) {
    import std.stdio : writeln;
    writeln("here");
}

void main() {
    auto x = [0.5, 0.5].sliced(2);
    auto y = x * 5.0;
    foo(x, y.slice);
}
April 20, 2020
On Sunday, 19 April 2020 at 22:07:30 UTC, jmh530 wrote:
> On Thursday, 16 April 2020 at 20:59:36 UTC, jmh530 wrote:
>> [snip]
>>
>> /+dub.sdl:
>> dependency "mir-algorithm" version="~>3.7.28"
>> +/
>>
>> import mir.ndslice;
>>
>> void foo(Iterator, SliceKind kind)(Slice!(Iterator, 1, kind) x, Slice!(Iterator, 1, kind) y) {
>>     import std.stdio : writeln;
>>     writeln("here");
>> }
>>
>> void main() {
>>     auto x = [0.5, 0.5].sliced(2);
>>     auto y = x * 5.0;
>>     foo(x, y);
>> }
>
> This is really what I was looking for (need to make allocation, unfortunately)
>
> /+dub.sdl:
> dependency "mir-algorithm" version="~>3.7.28"
> +/
>
> import mir.ndslice;
>
> void foo(Iterator, SliceKind kind)(Slice!(Iterator, 1, kind) x, Slice!(Iterator, 1, kind) y) {
>     import std.stdio : writeln;
>     writeln("here");
> }
>
> void main() {
>     auto x = [0.5, 0.5].sliced(2);
>     auto y = x * 5.0;
>     foo(x, y.slice);
> }

Using two arguments Iterator1, Iterator2 works without allocation

/+dub.sdl: dependency "mir-algorithm" version="~>3.7.28" +/
import mir.ndslice;

void foo(Iterator1, Iterator2, SliceKind kind)
 	(Slice!(Iterator1, 1, kind) x, Slice!(Iterator2, 1, kind) y)
{
    import std.stdio : writeln;
    writeln("here");
}

void main() {
    auto x = [0.5, 0.5].sliced(2);
    auto y = x * 5.0;
    foo(x, y);
}

« First   ‹ Prev
1 2