2D matrix operation (subtraction)

jmh530 john.michael.hall at gmail.com
Tue Feb 25 15:45:27 UTC 2020


On Saturday, 22 February 2020 at 08:29:32 UTC, 9il wrote:
> [snip]
>
> Maybe mir.series [1] can work for you.

I had a few other thoughts after looking at septc's solution of 
using
y[0..$, 0] *= 100;
to do the calculation.

1) There is probably scope for an additional select function to 
handle the use case of choosing a specific row/column. For 
instance, what if instead of
y[0..$, 0]
you want
y[0..$, b, 0..$]
for some arbitrary b. I think you would need to do something like
y.select!1(b, b + 1);
which doesn't have the best API, IMO, because you have to repeat 
b. Maybe just an overload for select that only takes one input 
instead of two?

2) The select series of functions does not seem to work as easily 
as array indexing does. When I tried to use the 
select/selectFront functions to do what he is doing, I had to 
something like
auto z = y.selectFront!1(1);
z[] *= 100;
This would adjust y as expected (not z). However, I couldn't 
figure out how to combine these together to one line. For 
instance, I couldn't do
y.selectFront!1(1) *= 100;
or
auto y = x.selectFront!1(1).each!(a => a * 100);
though something like
y[0..$, 0].each!"a *= 100";
works without issue.

It got a little frustrating to combine those with any kind of 
iteration. TBH, I think more than the select functions, the 
functionality I would probably be looking for is more what I was 
doing with byDim!1[0] in the prior post.

I could imagine some very simple version looking like below
auto selectDim(size_t dim, T)(T x, size_t a, size_t b) {
     return byDim!dim[a .. b];
}
with a corresponding version
auto selectDim(size_t dim, T)(T x, size_t a) {
     return byDim!dim[a .. (a + 1)];
}
This simple version would only work with one dimension, even 
though byDim can handle multiple.


More information about the Digitalmars-d-learn mailing list