-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputMatrix.tsx
More file actions
159 lines (135 loc) · 5.1 KB
/
InputMatrix.tsx
File metadata and controls
159 lines (135 loc) · 5.1 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use client';
import React, { useState, useImperativeHandle, forwardRef, useEffect, useRef } from "react";
import { RoundedArrow } from "./RoundedArrow";
export interface MatrixProps {
rows?: number;
cols?: number;
input?: boolean;
values?: number[][];
className?: string;
matrixName?: string;
locked?: boolean;
animationState?: string;
weight_counter?: number;
systolic_array_size?: number;
}
export interface MatrixRef {
getValues: () => number[][];
}
export const InputMatrix = forwardRef<MatrixRef, MatrixProps>(({ rows = 2, cols = 2, input = false, values, className, matrixName, locked = false, animationState, weight_counter, systolic_array_size }, ref) => {
const defaultValues = Array.from({ length: rows }, (_, i) =>
Array.from({ length: cols }, (_, j) => values?.[i]?.[j] ?? 0)
);
const [matrixValues, setMatrixValues] = useState(defaultValues);
const [highlightedCells, setHighlightedCells] = useState<boolean[][]>(
Array.from({ length: rows }, () => Array(cols).fill(false))
);
const prevValuesRef = useRef<number[][]>(defaultValues);
// Expose the getValues method to parent components
useImperativeHandle(ref, () => ({
getValues: () => matrixValues
}));
const bracket_height = 40 * rows + 8;
const handleKeyPress = (e: React.KeyboardEvent<HTMLInputElement>, i: number, j: number) => {
const key = e.key;
// Only allow digits
if (!/^\d$/.test(key)) {
e.preventDefault();
return;
}
const currentValue = matrixValues[i][j];
const currentStr = currentValue.toFixed(2);
// Shift digits to the left and add new digit to the right
const newStr = currentStr.slice(1) + key;
const newValue = parseFloat(newStr) * 10;
// Update the matrix value
const newMatrixValues = [...matrixValues];
newMatrixValues[i][j] = newValue;
setMatrixValues(newMatrixValues);
e.preventDefault();
};
useEffect(() => {
if (values) {
setMatrixValues(values);
}
}, [values]);
// Update values and highlight cells based on weight_counter
useEffect(() => {
// Clear any existing highlights when animation state changes
setHighlightedCells(Array.from({ length: rows }, () => Array(cols).fill(false)));
if (animationState === "update" && values) {
const newValues = Array.from({ length: rows }, (_, i) =>
Array.from({ length: cols }, (_, j) => values?.[i]?.[j] ?? 0)
);
// Create new highlights based on weight_counter logic
const newHighlights = Array.from({ length: rows }, (_, i) =>
Array.from({ length: cols }, (_, j) => {
if (weight_counter !== undefined) {
// Highlight based on weight_counter logic similar to MACUnit
return weight_counter - 1 == i + j;
}
return false;
})
);
setMatrixValues(newValues);
setHighlightedCells(newHighlights);
prevValuesRef.current = newValues;
// Reset highlights after a short delay
const timer = setTimeout(() => {
setHighlightedCells(Array.from({ length: rows }, () => Array(cols).fill(false)));
}, 1000);
return () => clearTimeout(timer);
}
}, [animationState, values, rows, cols, weight_counter, systolic_array_size]);
return (
<div className={`relative m-4 w-fit ${className}`}>
{/* Left bracket */}
<div className="absolute -left-[10px] -top-[10px] -translate-y-1/2">
<RoundedArrow
points={[[0, 0], [-5, 0], [-5, -bracket_height], [0, -bracket_height]]}
color="#666"
strokeWidth={0.75}
className="transform"
radius={2}
arrowHeadSize={0}
/>
</div>
{/* Right bracket */}
<div className="absolute -right-[5px] -top-[10px] -translate-y-1/2">
<RoundedArrow
points={[[0, 0], [5, 0], [5, -bracket_height], [0, -bracket_height]]}
color="#666"
strokeWidth={0.75}
className="transform"
radius={2}
arrowHeadSize={0}
/>
</div>
<div className={`inline-grid gap-2 ${className}`} style={{ gridTemplateColumns: `repeat(${cols}, 60px)` }}>
{matrixValues.map((row, i) =>
row.map((val, j) => (
<input
key={`${i}-${j}`}
type="text"
inputMode="numeric"
value={val.toFixed(2)}
disabled={!input || locked}
onKeyDown={(e) => input && !locked && handleKeyPress(e, i, j)}
readOnly
className={`text-center text-white font-mono text-sm px-2 py-1 bg-black border rounded ${
highlightedCells[i]?.[j] ? "border-red-500" : "border-neutral-700"
} ${
input && !locked ? "focus:outline-none focus:border-red-500" : "text-neutral-500"
} transition-colors duration-300 ease-in`}
/>
))
)}
</div>
<p className="mt-2 text-center text-sm font-light">
{matrixName}
</p>
</div>
);
});
// Add display name to the component
InputMatrix.displayName = 'InputMatrix';