-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmatrixCreateEmpty.ts
More file actions
57 lines (50 loc) · 1.47 KB
/
matrixCreateEmpty.ts
File metadata and controls
57 lines (50 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { DoubleMatrix } from 'cheminfo-types';
import type {
NumberArrayConstructor,
NumberArrayType,
} from '../utils/index.ts';
import { createNumberArray } from '../utils/index.ts';
export interface MatrixCreateEmptyOptions<
ArrayConstructorType extends NumberArrayConstructor = Float64ArrayConstructor,
> {
/**
* Reference matrix used to derive default row and column counts.
*/
matrix?: DoubleMatrix;
/**
* Number of rows in the new matrix.
* @default matrix.length || 1
*/
nbRows?: number;
/**
* Number of columns in the new matrix.
* @default matrix[0].length || 1
*/
nbColumns?: number;
/**
* Allows to specify the type of array to use
* @default Float64Array
*/
ArrayConstructor?: ArrayConstructorType;
}
/**
* Create a new matrix based on the size of the current one or by using specific dimensions.
* @param options
*/
export function matrixCreateEmpty<
ArrayConstructorType extends NumberArrayConstructor = Float64ArrayConstructor,
>(
options: MatrixCreateEmptyOptions<ArrayConstructorType>,
): Array<NumberArrayType<ArrayConstructorType>> {
const {
matrix,
nbRows = matrix?.length || 1,
nbColumns = matrix?.[0].length || 1,
ArrayConstructor = Float64Array as ArrayConstructorType,
} = options;
const newMatrix: Array<NumberArrayType<ArrayConstructorType>> = [];
for (let row = 0; row < nbRows; row++) {
newMatrix.push(createNumberArray(ArrayConstructor, nbColumns));
}
return newMatrix;
}