Skip to content

Commit

Permalink
[system-a] tensor/ -- zeroTensor
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyuheng committed May 28, 2024
1 parent dc55c89 commit 63194dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# system-a

[system-a] `randomTensor`
[system-a] `tensor/` -- test `zeroTensor`
[system-a] `tensor/` -- `randomTensor`
[system-a] `tensor/` -- test `randomTensor`

[system-a] `networks/` -- `initParameters` & `initShape`
[system-a] `irisTestXs`, `irisTestYs`
[system-a] `irisTrainXs`, `irisTrainYs`
[system-a] `model`

# the-book

Expand Down
1 change: 1 addition & 0 deletions src/system-a/tensor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./tensorEvery.js"
export * from "./tensorMap.js"
export * from "./tensorReal.js"
export * from "./tensorZeros.js"
export * from "./zeroTensor.js"
19 changes: 19 additions & 0 deletions src/system-a/tensor/zeroTensor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { Tensor } from "./Tensor.js"
import type { Shape } from "./shape.js"

export function zeroTensor(shape: Shape): Tensor {
if (shape.length === 0) return 0

const [length, ...restShape] = shape
return repeat(length, zeroTensor, restShape)
}

function repeat<A, B>(n: number, f: (x: A) => B, x: A): Array<B> {
const results = []
while (n > 0) {
results.push(f(x))
n--
}

return results
}

0 comments on commit 63194dd

Please sign in to comment.