Passed
Push — main ( d18637...0ef6dc )
by Dylan
05:09
created

src/Tensor.ts   A

Complexity

Total Complexity 6
Complexity/F 3

Size

Lines of Code 53
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 24
dl 0
loc 53
rs 10
c 0
b 0
f 0
mnd 4
bc 4
fnc 2
bpm 2
cpm 3
noi 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A Tensor.valueOf 0 6 5
A Tensor.toString 0 6 1
1
export type TensorInput = Array<TensorInput|bigint>
2
3
/**
4
 * Find the number of dimensions of the array.
5
 */
6
export const arrayDepth = (a: TensorInput): number => {
7
  let i=0
8
  for (let current=a; Array.isArray(current[0]); i++) {
9
    current = current[0]
10
  }
11
  return i
12
}
13
14
/**
15
 * @class A tensor representing an immutable, multidimensional array of numbers with a defined shape and data type.
16
 * @name Tensor
17
 */
18
export class Tensor {
19
  values: BigInt[] = []
20
  shape: [number, number]
21
22
  /**
23
   * Initialize a tensor.
24
   */
25
  constructor(values: TensorInput, shape?:[number, number]) {
26
    this.shape = shape ?? [values.length, arrayDepth(values)]
27
    for (let i=0; i<this.shape[0]; i++) {
28
      // const row = []
29
      // for (let j=0; j<this.shape[1]; j++) {
30
      //   // row.push(values[i][j])
31
      // }
32
      this.values.push(0n)
33
    }
34
  }
35
36
  /**
37
   * The value.
38
   */
39
  valueOf(): number {
40
    return this.values.length
41
  }
42
43
  /**
44
   * The text representation.
45
   */
46
  toString(): string {
47
    return this.values.toString()
48
  }
49
50
}
51
52
export default Tensor
53