Skip to content

Commit 13c4e6c

Browse files
authored
add fillVandermonde (#218)
* add fillVandermonde * fixup
1 parent 9998f66 commit 13c4e6c

File tree

5 files changed

+56
-2
lines changed

5 files changed

+56
-2
lines changed

doc/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ PACKAGE_mir_ndslice = \
4848
concatenation\
4949
dynamic\
5050
field\
51+
filling\
5152
fuse\
5253
iterator\
5354
mutation\

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ mir_algorithm_src = [
4040
'source/mir/ndslice/connect/cpython.d',
4141
'source/mir/ndslice/dynamic.d',
4242
'source/mir/ndslice/field.d',
43+
'source/mir/ndslice/filling.d',
4344
'source/mir/ndslice/fuse.d',
4445
'source/mir/ndslice/internal.d',
4546
'source/mir/ndslice/iterator.d',

source/mir/ndslice/filling.d

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/++
2+
This is a submodule of $(MREF mir,ndslice).
3+
4+
Initialisation routines.
5+
6+
License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
7+
Copyright: 2019 Symmetry Investments Group and Kaleidic Associates Advisory Limited.
8+
Authors: Ilya Yaroshenko
9+
10+
Macros:
11+
SUBREF = $(REF_ALTTEXT $(TT $2), $2, mir, ndslice, $1)$(NBSP)
12+
T2=$(TR $(TDNW $(LREF $1)) $(TD $+))
13+
+/
14+
module mir.ndslice.filling;
15+
16+
import mir.ndslice.slice: Slice, SliceKind;
17+
18+
/++
19+
Fills a matrix with the terms of a geometric progression in each row.
20+
Params:
21+
matrix = `m × n` matrix to fill
22+
vec = vector of progression coefficients length of `m`
23+
+/
24+
void fillVandermonde(F, SliceKind matrixKind, SliceKind kind)(Slice!(F*, 2, matrixKind) matrix, Slice!(const(F)*, 1, kind) vec)
25+
in {
26+
assert(matrix.length == vec.length);
27+
}
28+
body {
29+
import mir.conv: to;
30+
31+
foreach (v; matrix)
32+
{
33+
F a = vec.front;
34+
vec.popFront;
35+
F x = to!F(1);
36+
foreach (ref e; v)
37+
{
38+
e = x;
39+
x *= a;
40+
}
41+
}
42+
}

source/mir/ndslice/package.d

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ $(TR $(TDNW $(SUBMODULE topology) $(BR)
172172
)
173173
)
174174
175+
$(TR $(TDNW $(SUBMODULE filling) $(BR)
176+
$(SMALL Specialized initialisation routines))
177+
$(TD
178+
$(SUBREF filling, fillVandermonde)
179+
)
180+
)
181+
175182
$(TR $(TDNW $(SUBMODULE fuse) $(BR)
176183
$(SMALL Data fusing (stacking)
177184
$(BR) See also $(SUBMODULE concatenation) submodule.
@@ -474,6 +481,7 @@ public import mir.ndslice.concatenation;
474481
public import mir.ndslice.chunks;
475482
public import mir.ndslice.dynamic;
476483
public import mir.ndslice.field;
484+
public import mir.ndslice.filling;
477485
public import mir.ndslice.fuse;
478486
public import mir.ndslice.iterator;
479487
public import mir.ndslice.ndfield;

source/mir/ndslice/slice.d

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,11 @@ auto sliced(size_t N, Iterator)(Iterator iterator, size_t[N] lengths...)
271271
return ret;
272272
}
273273

274-
import mir.ndslice.topology: universal;
274+
import mir.ndslice.filling: fillVandermonde;
275+
import mir.ndslice.allocation: uninitSlice;
275276
auto x = [1.0, 2, 3, 4, 5].sliced;
276-
auto v = vandermondeMatrix(x);
277+
auto v = uninitSlice!double(x.length, x.length);
278+
v.fillVandermonde(x);
277279
assert(v ==
278280
[[ 1.0, 1, 1, 1, 1],
279281
[ 1.0, 2, 4, 8, 16],

0 commit comments

Comments
 (0)