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

Tensor.valueOf   A

Complexity

Conditions 5

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 9.3333
c 0
b 0
f 0
cc 5
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