Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #12003 > unrolled thread
| Started by | Chris <kristoph.steikert@gmail.com> |
|---|---|
| First post | 2012-10-08 06:57 +0000 |
| Last post | 2012-10-11 06:13 +0000 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
fast summing alternative? Chris <kristoph.steikert@gmail.com> - 2012-10-08 06:57 +0000
Re: fast summing alternative? Roland Franzius <roland.franzius@uos.de> - 2012-10-09 04:46 +0000
Re: fast summing alternative? Ray Koopman <koopman@sfu.ca> - 2012-10-09 04:46 +0000
Re: fast summing alternative? Ray Koopman <koopman@sfu.ca> - 2012-10-10 05:33 +0000
Re: fast summing alternative? Ray Koopman <koopman@sfu.ca> - 2012-10-11 06:13 +0000
| From | Chris <kristoph.steikert@gmail.com> |
|---|---|
| Date | 2012-10-08 06:57 +0000 |
| Subject | fast summing alternative? |
| Message-ID | <k4tth1$sgq$1@smc.vnet.net> |
Dear all
The code below is part of a large compile function I wrote and the inputs, like the matrices weights and newData, are in fact large data files. Thus, I provide them in the following problem as random. Although, written as compile code (with CompilationTarget->"C") it is still too slow for the amounts of data I have to process. I provide two of my fastest alternatives, which sadly are not that fast. I do appreciate any help to make the code faster. Thanks in advance!
(*
What I do is simple: For each row of newMat and weights I construct a new row. The elements of this new row are the sums of the elements in weights if a certain requirement of the same element in newMat is met.
*)
Clear[bNum,nNum,weights,newMat,sortMat,tab];
bNum;
nNum=100;
weights=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
newMat=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
sortMat=Table[i,{i, -10, 10, 0.1}]; (*elements always evenly spaced*)
(*first alternative*)
tab=Table[
Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{k,1,bNum},
{j,1,Length[sortMat]}];//AbsoluteTiming
(*second alternative*)
mat=Block[{k=1},
Reap[Do[
Sow[Table[Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{j,1,Length[sortMat]}]];
k++,{bNum}]]][[2,1]];//AbsoluteTiming
[toc] | [next] | [standalone]
| From | Roland Franzius <roland.franzius@uos.de> |
|---|---|
| Date | 2012-10-09 04:46 +0000 |
| Message-ID | <k50a74$3p9$1@smc.vnet.net> |
| In reply to | #12003 |
Am 08.10.2012 08:57, schrieb Chris:
> Dear all
>
> The code below is part of a large compile function I wrote and the inputs, like the matrices weights and newData, are in fact large data files. Thus, I provide them in the following problem as random. Although, written as compile code (with CompilationTarget->"C") it is still too slow for the amounts of data I have to process. I provide two of my fastest alternatives, which sadly are not that fast. I do appreciate any help to make the code faster. Thanks in advance!
>
>
> (*
> What I do is simple: For each row of newMat and weights I construct a new row. The elements of this new row are the sums of the elements in weights if a certain requirement of the same element in newMat is met.
> *)
>
> Clear[bNum,nNum,weights,newMat,sortMat,tab];
>
> bNum;
Number missing
bNum=100;
> nNum=100;
>
> weights=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
> newMat=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
> sortMat=Table[i,{i, -10, 10, 0.1}]; (*elements always evenly spaced*)
>
> (*first alternative*)
> tab=Table[
> Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{k,1,bNum},
> {j,1,Length[sortMat]}];//AbsoluteTiming
>
{3.9624069, Null}
> (*second alternative*)
> mat=Block[{k=1},
> Reap[Do[
> Sow[Table[Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{j,1,Length[sortMat]}]];
> k++,{bNum}]]][[2,1]];//AbsoluteTiming
>
{3.9624069, Null}
List manipulations without references to elements and Replace instead of
If's will save you more than half of the time
((projector = Outer[ #1 >= #2 &, sortMat, newMat] /.
{True -> 1, False -> 0});
pw = Plus @@@
Transpose[ Map[(Plus @@ (weights*#) &), projector]];) //
AbsoluteTiming
{1.5288027, Null}
I only checked dimensions of arrays for a short run time check.
The correct use of Outer, Transpose and element multiplication is up to
you. For Plus@@@ a*b for vectors you can use Dot or Inner instead
--
Roland Franzius
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2012-10-09 04:46 +0000 |
| Message-ID | <k50a7p$3sh$1@smc.vnet.net> |
| In reply to | #12003 |
On Oct 7, 11:59 pm, Chris <kristoph.steik...@gmail.com> wrote:
> Dear all
>
> The code below is part of a large compile function I wrote and the inputs, like the matrices weights and newData, are in fact large data files. Thus, I provide them in the following problem as random. Although, written as compile code (with CompilationTarget->"C") it is still too slow for the amounts of data I have to process. I provide two of my fastest alternatives, which sadly are not that fast. I do appreciate any help to make the code faster. Thanks in advance!
>
> (*
> What I do is simple: For each row of newMat and weights I construct a new row. The elements of this new row are the sums of the elements in weights if a certain requirement of the same element in newMat is met.
> *)
>
> Clear[bNum,nNum,weights,newMat,sortMat,tab];
>
> bNum;
> nNum=100;
>
> weights=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
> newMat=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
> sortMat=Table[i,{i, -10, 10, 0.1}]; (*elements always evenly spaced*)
>
> (*first alternative*)
> tab=Table[
> Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}], {k,1,bNum},
> {j,1,Length[sortMat]}];//AbsoluteTiming
>
> (*second alternative*)
> mat=Block[{k=1},
> Reap[Do[
> Sow[Table[Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{j,1,Length[sortMat]}]];
> k++,{bNum}]]][[2,1]];//AbsoluteTiming
tab2 = Transpose @ With[{negnewMat = - newMat}, MapThread[
Dot, {weights, UnitStep[# + negnewMat]} ]& /@ sortMat ]
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2012-10-10 05:33 +0000 |
| Message-ID | <k531af$93q$1@smc.vnet.net> |
| In reply to | #12016 |
On Oct 8, 9:48 pm, Ray Koopman <koop...@sfu.ca> wrote:
> On Oct 7, 11:59 pm, Chris <kristoph.steik...@gmail.com> wrote:
>> Dear all
>>
>> The code below is part of a large compile function I wrote and the inputs, like the matrices weights and newData, are in fact large data files. Thus, I provide them in the following problem as random. Although, written as compile code (with CompilationTarget->"C") it is still too slow for the amounts of data I have to process. I provide two of my fastest alternatives, which sadly are not that fast. I do appreciate any help to make the code faster. Thanks in advance!
>>
>> (*
>> What I do is simple: For each row of newMat and weights I construct a new row. The elements of this new row are the sums of the elements in weights
if a certain requirement of the same element in newMat is met.
>> *)
>>
>> Clear[bNum,nNum,weights,newMat,sortMat,tab];
>>
>> bNum;
>> nNum=100;
>>
>> weights=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
>> newMat=Table[RandomReal[{-10,10}],{b,1,bNum},{n,1,nNum}];
>> sortMat=Table[i,{i, -10, 10, 0.1}]; (*elements always evenly spaced*)
>>
>> (*first alternative*)
>> tab=Table[
>> Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}], {k,1,bNum},
>> {j,1,Length[sortMat]}];//AbsoluteTiming
>>
>> (*second alternative*)
>> mat=Block[{k=1},
>> Reap[Do[
>> Sow[Table[Sum[weights[[k,i]]*If[newMat[[k,i]] <= sortMat[[j]],1.,0.],{i,1,nNum}],{j,1,Length[sortMat]}]];
>> k++,{bNum}]]][[2,1]];//AbsoluteTiming
>
> tab2 = Transpose @ With[{negnewMat = - newMat}, MapThread[
> Dot, {weights, UnitStep[# + negnewMat]} ]& /@ sortMat ]
This should be faster:
tab3 = Transpose[ MapThread[Dot,{weights,#}]& /@ Partition[
UnitStep[Plus@@Transpose@Tuples@{sortMat,-newMat}], bNum] ]
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2012-10-11 06:13 +0000 |
| Message-ID | <k55o32$e7l$1@smc.vnet.net> |
| In reply to | #12028 |
On Oct 9, 10:34 pm, Ray Koopman <koop...@sfu.ca> wrote:
> On Oct 8, 9:48 pm, Ray Koopman <koop...@sfu.ca> wrote:> On Oct 7, 11:59
>>
>> tab2 = Transpose @ With[{negnewMat = - newMat}, MapThread[
>> Dot, {weights, UnitStep[# + negnewMat]} ]& /@ sortMat ]
>
> This should be faster:
>
> tab3 = Transpose[ MapThread[Dot,{weights,#}]& /@ Partition[
> UnitStep[Plus@@Transpose@Tuples@{sortMat,-newMat}], bNum] ]
This may be marginally faster yet:
tab4 = Transpose@Developer`PartitionMap[ MapThread[Dot,{weights,#}]&,
UnitStep[Plus@@Transpose@Tuples@{sortMat,-newMat}], bNum]
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web