Skip to content

Commit

Permalink
Added a Dataset object conversion to a RSparseMatrix object.
Browse files Browse the repository at this point in the history
  • Loading branch information
antononcube committed Nov 17, 2015
1 parent 6f71b00 commit b766ed4
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions Misc/RSparseMatrix.m
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Note that assignment (with Set[__]) is not implemented.
"The dimension names `1` are expected to be a list of two strings.";

ToRSparseMatrix::arg1 =
"The first argument is expected to a sparse array or a RSparseMatrix object.";
"The first argument is expected to be a sparse array, a dataset with two dimensions, or a RSparseMatrix object";

Options[MakeRSparseMatrix] = {"RowNames" -> None, "ColumnNames" -> None, "DimensionNames" -> None};

Expand Down Expand Up @@ -141,6 +141,30 @@ Note that assignment (with Set[__]) is not implemented.
If[dnames === None, None, AssociationThread[dnames, {1, 2}]]|>]
];

ToRSparseMatrix[ds_Dataset, opts : OptionsPattern[]] :=
Block[{rows, dsRownames, dsColnames, vals, res},
rows = Normal[ds];
If[AssociationQ[rows],
dsRownames = Keys[rows];
rows = rows /@ dsRownames,
(*ELSE*)
dsRownames = None;
];
If[AssociationQ[rows[[1]]],
dsColnames = Keys[rows[[1]]];
vals = Map[Values, rows],
(*ELSE*)
dsColnames = None;
vals = rows;
];

res = ToRSparseMatrix[SparseArray[vals], "RowNames" -> dsRownames, "ColumnNames" -> dsColnames];

If[ Length[{opts}] == 0, res,
ToRSparseMatrix[ res, opts ]
]
] /; Length[Dimensions[ds]] == 2;

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

SparseArray[rmat_RSparseMatrix] ^:= rmat[[1]]["sparseArray"];
Expand Down Expand Up @@ -419,22 +443,26 @@ There are three solutions (1) using array rules, (2) using matrix padding, Array

(*F_[rmat_RSparseMatrix, args___] ^:=*)
(*Block[{res = F[SparseArray[rmat], args]},*)
(*Print["RSparseMatrix decoration::F=",F];*)
(*Print["RSparseMatrix decoration::res=",res];*)
(*Print["RSparseMatrix decoration::F=",F];*)
(*Print["RSparseMatrix decoration::res=",res];*)
(*If[MatrixQ[res],*)
(*RSparseMatrix[*)
(*Join[<|"sparseArray" -> SparseArray[res]|>, Rest[rmat[[1]]]]],*)
(*res*)
(*]*)
(*] /; !*)
(*MemberQ[{SparseArray, ToRSparseMatrix,*)
(*RowNames, ColumnNames, DimensionNames,*)
(*SetRowNames, SetColumnNames, SetDimensionNames,*)
(*MatrixForm, MatrixPlot,*)
(*Dimensions, ArrayRules,*)
(*Total, RowSums, ColumnSums, RowsCount, ColumnsCount,*)
(*Dot, Plus, Times, Part,*)
(*RowBind, ColumnBind,*)
(*Head, Format, Print*)
(*}, F];*)
(*] /;*)
(*! MemberQ[*)
(*Join[{"SparseArray", "ToRSparseMatrix",*)
(*"RowNames", "ColumnNames", "DimensionNames",*)
(*"SetRowNames", "SetColumnNames", "SetDimensionNames",*)
(*"MatrixForm", "MatrixPlot",*)
(*"Dimensions", "ArrayRules",*)
(*"Total", "RowSums", "ColumnSums", "RowsCount", "ColumnsCount",*)
(*"Dot", "Plus", "Times", "Part",*)
(*"RowBind", "ColumnBind",*)
(*"Head", "Format", "Print"},*)
(*Names["System`*Hold*"],*)
(*Names["System`Inactiv*"],*)
(*Names["System`Activ*"]*)
(*], SymbolName[F] ];*)

0 comments on commit b766ed4

Please sign in to comment.