digitalmars.D.learn - mir: How to change iterator?
- jmh530 (19/19) Apr 14 2020 In the code below, I multiply some slice by 5 and then check
- Basile B. (10/29) Apr 16 2020 `approxEqual` cant work with ranges. If you look at the signature
- Basile B. (3/13) Apr 16 2020 And remove the extra assert() BTW... I don't know why this is
- jmh530 (20/23) Apr 16 2020 Thanks, I hadn't realized about approxEqual. I think that
- WebFreak001 (4/23) Apr 17 2020 Use std.algorithm:equal for range compare with approxEqual for
- WebFreak001 (3/8) Apr 17 2020 simplified:
- 9il (2/7) Apr 18 2020 or mir.algorithm.iteration: each that can work with nd-ranges.
- 9il (3/12) Apr 18 2020 EDIT:
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))); }
Apr 14 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.
Apr 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:And remove the extra assert() BTW... I don't know why this is accepted.[...]`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]))));
Apr 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); }
Apr 16 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); }
Apr 19 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: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); }[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); }
Apr 19 2020
On Monday, 20 April 2020 at 00:27:40 UTC, 9il wrote:[snip] 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); }Thanks, but I was thinking about the situation where someone else has written the function and didn't allow for multiple iterators for whatever reason.
Apr 20 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)));
Apr 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:simplified: assert(equal!approxEqual(y, [2.5, 2.5]));[...]Use std.algorithm:equal for range compare with approxEqual for your comparator: assert(equal!approxEqual(y, [2.5, 2.5].sliced(2)));
Apr 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:or mir.algorithm.iteration: each that can work with nd-ranges.[...]Use std.algorithm:equal for range compare with approxEqual for your comparator: assert(equal!approxEqual(y, [2.5, 2.5].sliced(2)));
Apr 18 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:EDIT: or mir.algorithm.iteration: equal that can work with nd-ranges.On Tuesday, 14 April 2020 at 20:24:05 UTC, jmh530 wrote:or mir.algorithm.iteration: each that can work with nd-ranges.[...]Use std.algorithm:equal for range compare with approxEqual for your comparator: assert(equal!approxEqual(y, [2.5, 2.5].sliced(2)));
Apr 18 2020