@@ -8,6 +8,7 @@ module mir.sparse.blas.dot;
88import std.traits ;
99import mir.ndslice.slice;
1010import mir.sparse;
11+ import mir.series;
1112
1213/+ +
1314Dot product of two vectors
@@ -19,8 +20,8 @@ Returns:
1920 scalar `xᵀ × y`
2021+/
2122Unqual! (CommonType! (T1 , T2 )) dot(
22- V1 : CompressedArray ! ( T1 , I1 ),
23- V2 : CompressedArray ! ( T2 , I2 ),
23+ V1 : Series ! ( I1 * , T1 * ),
24+ V2 : Series ! ( I2 * , T2 * ),
2425 T1 , T2 , I1 , I2 )
2526(V1 x, V2 y)
2627{
@@ -30,8 +31,8 @@ Unqual!(CommonType!(T1, T2)) dot(
3031// / ditto
3132D dot (
3233 D,
33- V1 : CompressedArray ! ( T1 , I1 ),
34- V2 : CompressedArray ! ( T2 , I2 ),
34+ V1 : Series ! ( I1 * , T1 * ),
35+ V2 : Series ! ( I2 * , T2 * ),
3536 T1 , T2 , I1 , I2 )
3637(V1 x, V2 y)
3738{
@@ -42,40 +43,38 @@ D dot(
4243 Unqual! I1 ai0 = void ;
4344 Unqual! I2 bi0 = void ;
4445
45- if (x.indexes. length && y.indexes .length) for (;;)
46+ if (x.length && y.length) for (;;)
4647 {
47- bi0 = y.indexes [0 ];
48- if (x.indexes [0 ] < bi0)
48+ bi0 = y.index [0 ];
49+ if (x.index [0 ] < bi0)
4950 {
5051 do
5152 {
52- x.values = x.values [1 .. $];
53- x.indexes = x.indexes[1 .. $];
54- if (x.indexes.length == 0 )
53+ x.popFront;
54+ if (x.length == 0 )
5555 {
5656 break ;
5757 }
5858 }
59- while (x.indexes [0 ] < bi0);
59+ while (x.index [0 ] < bi0);
6060 done = 2 ;
6161 }
6262 if (-- done == 0 )
6363 {
6464 goto L;
6565 }
66- ai0 = x.indexes [0 ];
67- if (y.indexes [0 ] < ai0)
66+ ai0 = x.index [0 ];
67+ if (y.index [0 ] < ai0)
6868 {
6969 do
7070 {
71- y.values = y.values [1 .. $];
72- y.indexes = y.indexes[1 .. $];
73- if (y.indexes.length == 0 )
71+ y.popFront;
72+ if (y.length == 0 )
7473 {
7574 break ;
7675 }
7776 }
78- while (y.indexes [0 ] < ai0);
77+ while (y.index [0 ] < ai0);
7978 done = 2 ;
8079 }
8180 if (-- done == 0 )
@@ -84,19 +83,17 @@ D dot(
8483 }
8584 continue ;
8685 L:
87- s = x.values [0 ] * y.values [0 ] + s;
88- x.indexes = x.indexes[ 1 .. $] ;
89- if (x.indexes. length == 0 )
86+ s = x.value [0 ] * y.value [0 ] + s;
87+ x.popFront ;
88+ if (x.length == 0 )
9089 {
9190 break ;
9291 }
93- y.indexes = y.indexes[ 1 .. $] ;
94- if (y.indexes. length == 0 )
92+ y.popFront ;
93+ if (y.length == 0 )
9594 {
9695 break ;
9796 }
98- x.values = x.values [1 .. $];
99- y.values = y.values [1 .. $];
10097 }
10198
10299 return s;
@@ -105,8 +102,8 @@ D dot(
105102// /
106103unittest
107104{
108- auto x = CompressedArray ! ( int , uint )([ 1 , 3 , 4 , 9 , 10 ], [0 , 3 , 5 , 9 , 100 ]);
109- auto y = CompressedArray ! ( int , uint )([ 1 , 10 , 100 , 1000 ], [1 , 3 , 4 , 9 ]);
105+ auto x = series([ 0u , 3 , 5 , 9 , 100 ], [1 , 3 , 4 , 9 , 10 ]);
106+ auto y = series([ 1u , 3 , 4 , 9 ], [1 , 10 , 100 , 1000 ]);
110107 // x = [1, 0, 0, 3, 0, 4, 0, 0, 0, 9, ... ,10]
111108 // y = [0, 1, 0, 10, 0, 0, 0, 0, 0, 1000]
112109 assert (dot(x, y) == 9030 );
@@ -122,7 +119,7 @@ Returns:
122119 scalar `x × y`
123120+/
124121Unqual! (CommonType! (T1 , ForeachType! V2 )) dot(
125- V1 : CompressedArray ! ( T1 , I1 ),
122+ V1 : Series ! ( I1 * , T1 * ),
126123 T1 , I1 , V2 )
127124(V1 x, V2 y)
128125 if (isDynamicArray! V2 || isSlice! V2 )
@@ -133,14 +130,14 @@ Unqual!(CommonType!(T1, ForeachType!V2)) dot(
133130// /ditto
134131D dot (
135132 D,
136- V1 : CompressedArray ! ( T1 , I1 ),
133+ V1 : Series ! ( I1 * , T1 * ),
137134 T1 , I1 , V2 )
138135(V1 x, V2 y)
139136 if (isDynamicArray! V2 || isSlice! V2 )
140137in
141138{
142- if (x.indexes. length)
143- assert (x.indexes [$- 1 ] < y.length);
139+ if (x.length)
140+ assert (x.index [$- 1 ] < y.length);
144141}
145142body
146143{
151148
152149 alias F = Unqual! (CommonType! (T1 , T2 ));
153150 F s = 0 ;
154- foreach (size_t i; 0 .. x.indexes .length)
151+ foreach (size_t i; 0 .. x.index .length)
155152 {
156- s = y[x.indexes [i]] * x.values [i] + s;
153+ s = y[x.index [i]] * x.value [i] + s;
157154 }
158155
159156 return s;
163160unittest
164161{
165162 import std.typecons : No;
166- auto x = CompressedArray ! ( double , uint )([ 1.0 , 3 , 4 , 9 , 13 ], [ 0 , 3 , 5 , 9 , 10 ]);
163+ auto x = [ 0u , 3 , 5 , 9 , 10 ].series([ 1. 0 , 3 , 4 , 9 , 13 ]);
167164 auto y = [0.0 , 1.0 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ];
168165 // x: [1, 0, 0, 3, 0, 4, 0, 0, 0, 9, 13, 0, 0, 0]
169166 // y: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
0 commit comments