www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - mir: How to change iterator?

reply jmh530 <john.michael.hall gmail.com> writes:
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
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
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
parent reply Basile B. <b2.temp gmx.com> writes:
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.
Apr 16 2020
parent reply jmh530 <john.michael.hall gmail.com> writes:
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
parent reply jmh530 <john.michael.hall gmail.com> writes:
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
parent reply 9il <ilyayaroshenko gmail.com> writes:
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); }
Apr 19 2020
parent jmh530 <john.michael.hall gmail.com> writes:
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
prev sibling parent reply WebFreak001 <d.forum webfreak.org> writes:
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
next sibling parent WebFreak001 <d.forum webfreak.org> writes:
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]));
Apr 17 2020
prev sibling parent reply 9il <ilyayaroshenko gmail.com> writes:
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.
Apr 18 2020
parent 9il <ilyayaroshenko gmail.com> writes:
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.
Apr 18 2020