Skip to content

Commit 193461f

Browse files
committed
Merge branch 'issue-30' of github.com:reside-ic/dust2-js into issue-32
2 parents 9712be3 + 608c417 commit 193461f

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/interfaces/generators/imports/array.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
type Op = (agg: number, x: number) => number;
22
type Range = [number, number];
33

4+
// this file is based on dust2's implementation, for more details see:
5+
// https://github.com/mrc-ide/dust2/blob/83721f78fda9b4124e1cda3ae24e3f2da82ca2d0/inst/include/dust2/array.hpp#L115
6+
//
7+
// it calculates column major flat indicies from subranges along each dimension and applies
8+
// +, *, ... operations to those values
9+
410
const reduceAll = (arr: number[], op: Op, init: number) => {
511
return arr.reduce(op, init);
612
};
@@ -131,6 +137,21 @@ export type DimUtils = {
131137
mult: number[];
132138
};
133139

140+
/*
141+
Example usage
142+
=============
143+
144+
const dim = getDimObj();
145+
146+
dim.x = [2, 3, 5]; // setter triggers and will calculate dim, size and mult
147+
148+
console.log(dim.x)
149+
-> {
150+
dim: [2, 3, 5], // same as input
151+
size: 30, // 2 * 3 * 5, total size of flat array
152+
mult: [1, 2, 6] // stride length, cumulative prod of dim starting at 1
153+
}
154+
*/
134155
const getDimObj = () => {
135156
const dim: Record<string, DimUtils> = {};
136157
return new Proxy(dim, {

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export const checkNestedArrayLengthsMatch = (
8080
}
8181
};
8282

83-
// Convert an arrays into an NdArray
83+
// Convert an array into an NdArray
8484
export const ndArrayFrom = (source: number[]): ndarray.NdArray => {
8585
if (source.length === 0) {
8686
throw new DustParameterError("Cannot convert from empty source");

0 commit comments

Comments
 (0)