Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions PLAN.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ General formula: `flat = Σ(indices[d] * product(shape[d+1..]))`

**Goal**: `reshape`, `transpose`, `flatten`. All return new arrays (copies).

- [ ] **4.1** `reshape(self, allocator, new_shape) → NDArray` — validate `product(new_shape) == product(old_shape)`, create new NDArray, copy data (`@memcpy`), assign new shape
- [ ] **4.2** `transpose(self, allocator) → NDArray` — 2D only. Create new `(cols, rows)` array, copy `result[j][i] = self[i][j]`
- [ ] **4.3** `flatten(self, allocator) → NDArray` — reshape to `[total_elements]`
- [X] **4.1** `reshape(self, allocator, new_shape) → NDArray` — validate `product(new_shape) == product(old_shape)`, create new NDArray, copy data (`@memcpy`), assign new shape
- [X] **4.2** `transpose(self, allocator) → NDArray` — 2D only. Create new `(cols, rows)` array, copy `result[j][i] = self[i][j]`
- [X] **4.3** `flatten(self, allocator) → NDArray` — reshape to `[total_elements]`
- [ ] **4.4** `squeeze(self, allocator) → NDArray` — remove dimensions of size 1 from shape, copy data
- [ ] **4.5** Tests: reshape `(2, 6)` → `(3, 4)`, verify values preserved. Transpose `(3, 4)` → verify `(4, 3)` with correct element positions

Expand Down
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# num-wasm
# nw.js

A NumPy-like array library written in Zig, compiled to WebAssembly for JavaScript/TypeScript.

Expand Down Expand Up @@ -68,17 +68,17 @@ These simplifications keep the codebase approachable. They can be upgraded later

## Roadmap

| Phase | Feature | Status |
|-------|---------|--------|
| 1 | Toolchain setup, hello WASM | Done |
| 2 | NDArray core data structure | Done |
| 3 | Array creation functions | Done |
| 4 | Shape manipulation (reshape, transpose, flatten) | Upcoming |
| 5 | Broadcasting | Upcoming |
| 6 | Element-wise operations (add, multiply, sqrt, etc.) | Upcoming |
| 7 | Reduction operations (sum, mean, max, min) | Upcoming |
| 8 | Slicing and indexing | Upcoming |
| 9 | Linear algebra (dot, matmul) | Upcoming |
| 10 | JS glue library (clean API) | Upcoming |
| Phase | Feature | Status |
| ----- | --------------------------------------------------- | -------- |
| 1 | Toolchain setup, hello WASM | Done |
| 2 | NDArray core data structure | Done |
| 3 | Array creation functions | Done |
| 4 | Shape manipulation (reshape, transpose, flatten) | Upcoming |
| 5 | Broadcasting | Upcoming |
| 6 | Element-wise operations (add, multiply, sqrt, etc.) | Upcoming |
| 7 | Reduction operations (sum, mean, max, min) | Upcoming |
| 8 | Slicing and indexing | Upcoming |
| 9 | Linear algebra (dot, matmul) | Upcoming |
| 10 | JS glue library (clean API) | Upcoming |

See [PLAN.md](./PLAN.md) for detailed implementation plans.
39 changes: 39 additions & 0 deletions src/core/shape.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const testing = std.testing;

const NDArray = @import("ndarray.zig").NDArray;

pub fn reshape(allocator: Allocator, arr: *const NDArray, new_shape: []const usize) !NDArray {
var new_total: usize = 1;
for (new_shape) |size| {
new_total *= size;
}

if (new_total != arr.data.len) return error.ShapeMismatch;

const res = try NDArray.init(allocator, new_shape);
@memcpy(res.data, arr.data);

return res;
}

pub fn transpose(allocator: Allocator, arr: *const NDArray) !NDArray {
if (arr.ndim != 2) return error.MismatchDimension;

const rows = arr.shape[0];
const cols = arr.shape[1];
const res = try NDArray.init(allocator, &[_]usize{ cols, rows });

for (0..cols) |c| {
for (0..rows) |r| {
res.setItem(&[_]usize{ c, r }, arr.getItem(&[_]usize{ r, c }));
}
}

return res;
}

pub fn flatten(allocator: Allocator, arr: *const NDArray) !NDArray {
return reshape(allocator, arr, &[_]usize{arr.data.len});
}