Skip to content

Commit 92194cd

Browse files
committed
Implement binsparse_write bindings.
1 parent 7e18e4d commit 92194cd

9 files changed

+691
-38
lines changed

bindings/matlab/README.md

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ mkoctfile --version
5555

5656
3. Test the installation:
5757
```matlab
58-
test_bsp_hello()
58+
test_binsparse_read()
59+
test_binsparse_write()
5960
```
6061

6162
#### Option 2: Octave (from within Octave)
@@ -72,7 +73,8 @@ mkoctfile --version
7273

7374
3. Test the installation:
7475
```octave
75-
test_bsp_hello_octave()
76+
test_binsparse_read()
77+
test_binsparse_write()
7678
```
7779

7880
#### Option 3: Octave (from command line)
@@ -89,23 +91,56 @@ mkoctfile --version
8991

9092
3. Test the installation:
9193
```bash
92-
octave --eval "test_bsp_hello_octave()"
94+
octave --eval "test_binsparse_read()"
95+
octave --eval "test_binsparse_write()"
9396
```
9497

9598
## Usage Examples
9699

97-
### Basic Usage
100+
### Reading Binsparse Files
98101

99102
**In MATLAB or Octave:**
100103

101104
```matlab
102-
% Simple greeting
103-
result = bsp_hello()
104-
% Output: 'Binsparse MEX binding is working!'
105+
% Read a Binsparse matrix file
106+
matrix = binsparse_read('path/to/matrix.bsp.h5');
107+
108+
% Read from a specific group
109+
matrix = binsparse_read('path/to/matrix.bsp.h5', 'group_name');
110+
111+
% Matrix will be a struct with fields:
112+
% - values: array of matrix values
113+
% - indices_0, indices_1: row/column indices
114+
% - pointers_to_1: pointer array (for CSR/CSC formats)
115+
% - nrows, ncols, nnz: matrix dimensions
116+
% - is_iso: boolean for iso-value matrices
117+
% - format: string ('COO', 'CSR', 'CSC', etc.)
118+
% - structure: string ('general', 'symmetric', etc.)
119+
```
120+
121+
### Writing Binsparse Files
105122

106-
% Get Binsparse version
107-
[version, success] = bsp_hello('version')
108-
% Output: version = '0.1', success = true
123+
```matlab
124+
% Create a matrix struct (example: 3x3 COO matrix)
125+
matrix = struct();
126+
matrix.values = [1.0; 2.0; 3.0];
127+
matrix.indices_0 = uint64([0; 1; 2]); % 0-based row indices
128+
matrix.indices_1 = uint64([0; 1; 2]); % 0-based col indices
129+
matrix.pointers_to_1 = uint64([]); % Empty for COO
130+
matrix.nrows = 3;
131+
matrix.ncols = 3;
132+
matrix.nnz = 3;
133+
matrix.is_iso = false;
134+
matrix.format = 'COO';
135+
matrix.structure = 'general';
136+
137+
% Write to file
138+
binsparse_write('output.bsp.h5', matrix);
139+
140+
% Write with optional parameters
141+
binsparse_write('output.bsp.h5', matrix, 'my_group');
142+
binsparse_write('output.bsp.h5', matrix, 'my_group', '{"author": "me"}');
143+
binsparse_write('output.bsp.h5', matrix, 'my_group', '{"author": "me"}', 6);
109144
```
110145

111146
### Error Handling
@@ -114,7 +149,7 @@ The MEX functions include proper error handling:
114149

115150
```matlab
116151
try
117-
result = bsp_hello('invalid_mode')
152+
matrix = binsparse_read('nonexistent_file.bsp.h5')
118153
catch ME
119154
fprintf('Error: %s\n', ME.message)
120155
end
@@ -124,25 +159,32 @@ end
124159

125160
| File | Description |
126161
|------|-------------|
127-
| `bsp_hello.c` | Simple MEX function demonstrating Binsparse integration |
162+
| `binsparse_read.c` | MEX function for reading Binsparse matrix files |
163+
| `binsparse_write.c` | MEX function for writing Binsparse matrix files |
128164
| `build_matlab_bindings.m` | Main build script for MATLAB MEX functions |
129165
| `build_octave_bindings.m` | Main build script for Octave MEX functions |
130-
| `compile_bsp_hello.m` | Simple compilation script for the demo function (MATLAB) |
166+
| `compile_binsparse_read.m` | Simple compilation script for read function (MATLAB) |
167+
| `compile_binsparse_write.m` | Simple compilation script for write function (MATLAB) |
168+
| `compile_binsparse_read_octave.m` | Simple compilation script for read function (Octave) |
169+
| `compile_binsparse_write_octave.m` | Simple compilation script for write function (Octave) |
131170
| `compile_octave.sh` | Shell script for building Octave MEX functions |
132-
| `test_bsp_hello.m` | Test script to verify functionality (MATLAB) |
133-
| `test_bsp_hello_octave.m` | Test script to verify functionality (Octave) |
171+
| `test_binsparse_read.m` | Test script for read functionality |
172+
| `test_binsparse_write.m` | Test script for write functionality |
173+
| `bsp_matrix_create.m` | Utility function for creating matrix structs |
174+
| `bsp_matrix_info.m` | Utility function for displaying matrix information |
134175
| `README.md` | This documentation file |
135176

136177
## Technical Details
137178

138179
### MEX Function Structure
139180

140-
The `bsp_hello` MEX function demonstrates:
181+
The Binsparse MEX functions demonstrate:
141182

142183
1. **Header inclusion**: Proper inclusion of `<binsparse/binsparse.h>`
143-
2. **Error handling**: Using Binsparse error types (`bsp_error_t`)
144-
3. **Memory management**: Safe allocation and cleanup
145-
4. **MATLAB interface**: Proper MEX function structure
184+
2. **Type conversion**: Complete mapping between MATLAB and Binsparse types
185+
3. **Error handling**: Using Binsparse error types (`bsp_error_t`)
186+
4. **Memory management**: Safe allocation and cleanup
187+
5. **MATLAB interface**: Proper MEX function structure with validation
146188

147189
### Build Process
148190

@@ -222,14 +264,17 @@ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
222264
223265
## Development Status
224266
225-
This is a minimal demonstration of MATLAB/Octave bindings for Binsparse. Currently implemented:
267+
This provides complete MATLAB/Octave bindings for Binsparse. Currently implemented:
226268
227-
- ✅ Basic MEX function structure
228-
- ✅ Binsparse header inclusion
229-
- ✅ Error handling with Binsparse error types
269+
- ✅ Matrix reading (`binsparse_read`)
270+
- ✅ Matrix writing (`binsparse_write`)
271+
- ✅ Complete type support (all Binsparse types including complex numbers)
272+
- ✅ Optional parameters (groups, JSON metadata, compression)
273+
- ✅ Comprehensive error handling
230274
- ✅ Build system and testing framework
231-
- ⏳ Matrix reading/writing functions (future work)
232-
- ⏳ Advanced Binsparse features (future work)
275+
- ✅ Round-trip compatibility (read → write → read)
276+
- ⏳ Tensor support (future work)
277+
- ⏳ Advanced sparse matrix operations (future work)
233278
234279
## License
235280

0 commit comments

Comments
 (0)