Skip to content

Commit 142890d

Browse files
committed
feat: DotNetRunScript internals for dotnet run << <code>
1 parent 95ea0dd commit 142890d

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

.claude/CLAUDE.md

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,82 @@ switch (nd.typecode)
309309
## Build & Test
310310

311311
```bash
312-
dotnet build src/NumSharp.Core/NumSharp.Core.csproj
313-
dotnet test test/NumSharp.UnitTest/NumSharp.UnitTest.csproj
312+
dotnet build -v q --nologo "-clp:NoSummary;ErrorsOnly" -p:WarningLevel=0
313+
dotnet test -v q --nologo "-clp:ErrorsOnly" test/NumSharp.UnitTest/NumSharp.UnitTest.csproj
314314
```
315315

316+
## Scripting with `dotnet run` (.NET 10 file-based apps)
317+
318+
### Accessing Internal Members
319+
320+
NumSharp has many key types/fields/methods marked `internal` (Shape.dimensions, Shape.strides, NDArray.Storage, np._FindCommonType, etc.). To access them from a `dotnet run` script, override the assembly name to match an existing `InternalsVisibleTo` entry:
321+
322+
```csharp
323+
#:project path/to/src/NumSharp.Core
324+
#:property AssemblyName=NumSharp.DotNetRunScript
325+
#:property PublishAot=false
326+
```
327+
328+
**How it works:** NumSharp declares `[assembly: InternalsVisibleTo("NumSharp.DotNetRunScript")]` in `src/NumSharp.Core/Assembly/Properties.cs`. The `#:property AssemblyName=NumSharp.DotNetRunScript` directive overrides the script's assembly name (which normally derives from the filename) to match, granting full access to all `internal` and `protected internal` members.
329+
330+
### Script Template (copy-paste ready)
331+
332+
```csharp
333+
#:project path/to/src/NumSharp.Core
334+
#:property AssemblyName=NumSharp.DotNetRunScript
335+
#:property PublishAot=false
336+
337+
```
338+
339+
### Quick One-Liners
340+
341+
```bash
342+
# Run a script with full internal access
343+
dotnet run my_script.cs
344+
345+
# Compare NumPy vs NumSharp type promotion
346+
dotnet run script.cs # where script.cs contains:
347+
# var ct = np._FindCommonType(np.array(1), np.array(1.5));
348+
# Console.WriteLine(ct); // Double
349+
350+
# Inspect Shape internals
351+
dotnet run script.cs # where script.cs contains:
352+
# var s = new Shape(new int[]{2,3,4});
353+
# Console.WriteLine($"dims={string.Join(",",s.dimensions)} strides={string.Join(",",s.strides)} size={s.size}");
354+
355+
# Check ViewInfo after slicing
356+
dotnet run script.cs # where script.cs contains:
357+
# var arr = np.arange(24).reshape(2,3,4);
358+
# var sliced = arr["1, :, ::2"];
359+
# Console.WriteLine($"ViewInfo: {sliced.Shape.ViewInfo != null}, BroadcastInfo: {sliced.Shape.BroadcastInfo != null}");
360+
```
361+
362+
### Key Internal Members Available
363+
364+
| Member | What it exposes |
365+
|--------|----------------|
366+
| `shape.dimensions` | Raw int[] of dimension sizes |
367+
| `shape.strides` | Raw int[] of stride values |
368+
| `shape.size` | Total element count |
369+
| `shape.ViewInfo` | Slice/view metadata (null if not a view) |
370+
| `shape.BroadcastInfo` | Broadcast metadata (null if not broadcast) |
371+
| `arr.Storage` | Underlying `UnmanagedStorage` |
372+
| `arr.GetTypeCode` | `NPTypeCode` of the array |
373+
| `arr.Array` | `IArraySlice` — raw data access |
374+
| `np._FindCommonType(...)` | Type promotion logic |
375+
| `np.powerOrder` | Type promotion ordering |
376+
| `NPTypeCode.GetGroup()` | Type category (int/uint/float/etc.) |
377+
| `NPTypeCode.GetPriority()` | Type priority for promotion |
378+
| `NPTypeCode.AsNumpyDtypeName()` | NumPy dtype name (e.g. "int32") |
379+
| `Shape.NewScalar()` | Create scalar shapes |
380+
| `Shape.ComputeHashcode()` | Recalculate shape hash |
381+
316382
## Adding New Features
317383

318384
1. Read NumPy docs for the function
319-
2. **Run actual Python code** to observe exact behavior
385+
2. **Run actual Python code** to observe exact behavior and fuzzy all possible inputs to define a behavior matrix.
320386
3. Check existing similar implementations
321-
4. Implement matching behavior exactly
387+
4. Implement behavior matching exactly that of numpy.
322388
5. Write tests based on observed NumPy output
323389
6. Handle all 12 dtypes
324390

src/NumSharp.Core/Assembly/Properties.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
[assembly: InternalsVisibleTo("NumSharp.UnitTest")]
44
[assembly: InternalsVisibleTo("NumSharp.Benchmark")]
55
[assembly: InternalsVisibleTo("TensorFlowNET.UnitTest")]
6+
[assembly: InternalsVisibleTo("NumSharp.DotNetRunScript")]
67
#endif

0 commit comments

Comments
 (0)