Thread overview | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 06, 2014 transversal sum | ||||
---|---|---|---|---|
| ||||
I have rectangular forward range of forward ranges (not arrays): [ [a11, a12, ... a1N], [a21, a22, ... a2N], ... [aM1, aM2, ... aMN] ] I need lazy forward range: [ a11 + a21 + ... aM1, a12 + a22 + ... aM2, ... a1N + a2N + ... aMN ] Range of sum elements of every columns; M, N - runtime values; Is there a way to do this using only Phobos algorithms and range functions? |
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Thursday, 6 November 2014 at 15:53:27 UTC, Jack Applegame wrote:
> I have rectangular forward range of forward ranges (not arrays):
> [
> [a11, a12, ... a1N],
> [a21, a22, ... a2N],
> ...
> [aM1, aM2, ... aMN]
> ]
>
> I need lazy forward range:
> [
> a11 + a21 + ... aM1,
> a12 + a22 + ... aM2,
> ...
> a1N + a2N + ... aMN
> ]
> Range of sum elements of every columns;
>
> M, N - runtime values;
>
> Is there a way to do this using only Phobos algorithms and range functions?
Untested:
import std.algorithm: map, sum;
auto rangeOfSums = rectangularRange.map!(r => r.sum);
|
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Thu, 06 Nov 2014 16:57:48 +0000, Marc Schütz wrote:
> On Thursday, 6 November 2014 at 15:53:27 UTC, Jack Applegame wrote:
>> I have rectangular forward range of forward ranges (not arrays):
>> [
>> [a11, a12, ... a1N],
>> [a21, a22, ... a2N],
>> ...
>> [aM1, aM2, ... aMN]
>> ]
>>
>> I need lazy forward range:
>> [
>> a11 + a21 + ... aM1,
>> a12 + a22 + ... aM2,
>> ...
>> a1N + a2N + ... aMN
>> ]
>> Range of sum elements of every columns;
>>
>> M, N - runtime values;
>>
>> Is there a way to do this using only Phobos algorithms and range functions?
>
> Untested:
>
> import std.algorithm: map, sum;
> auto rangeOfSums = rectangularRange.map!(r => r.sum);
This would sum along the wrong dimension. I think the correct solution
will
make use of std.range.frontTraversal, but it will be a bit more complex
due to
needing to sum every column. std.range.traversal would make it easy, but
it
requires random access.
|
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Thu, 06 Nov 2014 17:08:23 +0000, Justin Whear wrote:
> I think the correct solution
> will make use of std.range.frontTraversal, but it will be a bit more
> complex due to needing to sum every column. std.range.traversal would
> make it easy, but it requires random access.
That should be std.range.frontTransversal and transversal.
|
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Thursday, 6 November 2014 at 16:57:50 UTC, Marc Schütz wrote:
> On Thursday, 6 November 2014 at 15:53:27 UTC, Jack Applegame wrote:
>> I have rectangular forward range of forward ranges (not arrays):
>> [
>> [a11, a12, ... a1N],
>> [a21, a22, ... a2N],
>> ...
>> [aM1, aM2, ... aMN]
>> ]
>>
>> I need lazy forward range:
>> [
>> a11 + a21 + ... aM1,
>> a12 + a22 + ... aM2,
>> ...
>> a1N + a2N + ... aMN
>> ]
>> Range of sum elements of every columns;
>>
>> M, N - runtime values;
>>
>> Is there a way to do this using only Phobos algorithms and range functions?
>
> Untested:
>
> import std.algorithm: map, sum;
> auto rangeOfSums = rectangularRange.map!(r => r.sum);
Sorry, I see you want columns...
I thought about std.range.frontTransversal, but it only returns a range of values, not a range of ranges. That's... unhelpful :-(
|
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Justin Whear | On Thursday, 6 November 2014 at 17:08:23 UTC, Justin Whear wrote:
> This would sum along the wrong dimension. I think the correct solution
> will
> make use of std.range.frontTraversal, but it will be a bit more complex
> due to
> needing to sum every column. std.range.traversal would make it easy, but
> it
> requires random access.
Yeah, I posted to soon. Was surprised that the solution would be so easy :-P
We'd need something taking and returning a RoR that "mirrors" them diagonally. Then we could simply apply `map!(r => r.sum)` on the result.
|
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | Marc Schütz:
> We'd need something taking and returning a RoR that "mirrors" them diagonally. Then we could simply apply `map!(r => r.sum)` on the result.
A simple solution is to create a row of values, and then sum them correctly while you scan the rows.
Bye,
bearophile
|
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 11/06/14 18:32, bearophile via Digitalmars-d-learn wrote: > Marc Schütz: > >> We'd need something taking and returning a RoR that "mirrors" them diagonally. Then we could simply apply `map!(r => r.sum)` on the result. > > A simple solution is to create a row of values, and then sum them correctly while you scan the rows. The simplest solution is probably something like: auto transversal_sum(FR)(FR rr) { static struct TS { FR rr; bool empty() @property const { return rr.front.empty; } auto front() @property { import std.algorithm; return reduce!((a, b)=>a+b.front)(rr.front.front.init, rr); } void popFront() { foreach (ref r; rr) r.popFront(); } } return TS(rr); } but I think OP wanted a ready-made phobos solution, w/o all the range boilerplate... artur |
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | > void popFront() { foreach (ref r; rr) r.popFront(); } I think it should be void popFront() { foreach (ref r; rr.save) r.popFront(); } > but I think OP wanted a ready-made phobos solution, w/o all the > range boilerplate... exactly. |
November 06, 2014 Re: transversal sum | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jack Applegame | On Thursday, 6 November 2014 at 15:53:27 UTC, Jack Applegame wrote:
> I have rectangular forward range of forward ranges (not arrays):
> [
> [a11, a12, ... a1N],
> [a21, a22, ... a2N],
> ...
> [aM1, aM2, ... aMN]
> ]
>
> I need lazy forward range:
> [
> a11 + a21 + ... aM1,
> a12 + a22 + ... aM2,
> ...
> a1N + a2N + ... aMN
> ]
> Range of sum elements of every columns;
>
> M, N - runtime values;
>
> Is there a way to do this using only Phobos algorithms and range functions?
Can I get random access in one or both dimensions?
|
Copyright © 1999-2021 by the D Language Foundation