Skip to content

Commit

Permalink
feat:Creation of association of numeric-arrays. (For Python's SSparse…
Browse files Browse the repository at this point in the history
…Matrix.to_wl().)
  • Loading branch information
antononcube committed Nov 21, 2021
1 parent afce476 commit b2c6475
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion SSparseMatrix.m
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,29 @@ In order to facilitate further package development (and demonstrate what the pac

ToSSparseMatrix[aRows : Association[ (_String -> Association[ (_String -> _?NumericQ) .. ]).. ], opts : OptionsPattern[] ] :=
Block[{arules},
arules = Join @@ KeyValueMap[ Function[{k,v}, KeyMap[ {k,#}&, v]], aRows];
arules = Join @@ KeyValueMap[ Function[{k, v}, KeyMap[ {k, #}&, v]], aRows];
ToSSparseMatrix[ arules, opts]
];

Clear[NumericArraySpecQ];
NumericArraySpecQ[x_Association] :=
Apply[And, Map[ KeyExistsQ[x, #]&, {"rowIndexes", "columnIndexes", "values", "shape", "rowNames", "columnNames"}]] &&
Apply[And, NumericArrayQ /@ Values[KeyTake[x, {"rowIndexes", "columnIndexes", "values"}]]];
NumericArraySpecQ[___] := False;

ToSSparseMatrix[spec_?AssociationQ] :=
ToSSparseMatrix[
SparseArray[
Map[(Most[#] + 1) -> Last[#] &,
Transpose[Normal /@ Values[KeyTake[spec, {"rowIndexes", "columnIndexes", "values"}]]]
],
spec["shape"]
],
"RowNames" -> spec["rowNames"],
"ColumnNames" -> spec["columnNames"]
] /; NumericArraySpecQ[spec];


ToSSparseMatrix[___] := Message[ToSSparseMatrix::arg1];

SparseArray[rmat_SSparseMatrix] ^:= First[rmat]["SparseMatrix"];
Expand Down

0 comments on commit b2c6475

Please sign in to comment.