Skip to content
Ax Kr edited this page Mar 10, 2021 · 1 revision

Context functions

A "script-"package can be loaded with the Get("file-name.m") function or in operator form << with <<"file-name.m". The following functions are available to develop a package:

WL packages

If you're using the MMA Console or local MMA web browser notebook installation, you can use some of the existing open source Wolfram Language(Wikipedia) packages.

BoolEval

You can load BoolEval.m from the Github repo with this command:

<<"https://raw.githubusercontent.com/szhorvat/BoolEval/master/BoolEval/BoolEval.m"

Here are some usage examples:

>> <<"https://raw.githubusercontent.com/szhorvat/BoolEval/master/BoolEval/BoolEval.m"
In[1]:= <<"https://raw.githubusercontent.com/szhorvat/BoolEval/master/BoolEval/BoolEval.m"
>> array=Range[10]
In[2]:= array=Range[10]
Out[2]= {1,2,3,4,5,6,7,8,9,10}
>> BoolEval[array>3]
In[3]:= BoolEval[array>3]
Out[3]= {0,0,0,1,1,1,1,1,1,1}
>> BoolPick[array,array>3]
In[4]:= BoolPick[array,array>3]
Out[4]= {4,5,6,7,8,9,10}
>> BoolPick[array, Mod[array,3] == 1 && array<5]
In[5]:= BoolPick[array, Mod[array,3] == 1 && array<5]
Out[5]= {1,4}
>> 

SSparseMatrix

You can load SSparseMatrix.m from the Github repo with this command:

<<"https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/SSparseMatrix.m"

The related unit tests can be executed with the following TestReport[] command, but take some minutes to run through:

TestReport["https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/UnitTests/SSparseMatrix-tests.wlt"]

Here are some usage examples:

>> <<"https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/SSparseMatrix.m"
In[1]:= <<"https://raw.githubusercontent.com/antononcube/MathematicaForPrediction/master/SSparseMatrix.m"
>> rmat = MakeSSparseMatrix[
             {{1, 1} -> 1, {2, 2} -> 2, {4, 3} -> 3, {1, 4} -> 4, {3, 5} -> 2},
             "ColumnNames" -> {"a", "b", "c", "d", "e"},
             "RowNames" -> {"A", "B", "C", "D"},
             "DimensionNames" -> {"U", "V"}]
In[2]:= rmat = MakeSSparseMatrix[             {{1, 1} -> 1, {2, 2} -> 2, {4, 3} -> 3, {1, 4} -> 4, {3, 5} -> 2},
     "ColumnNames" -> {"a", "b", "c", "d", "e"},             "RowNames" -> {"A", "B", "C", "D"},             "DimensionNames" -> {"U", "V"}]
Out[2]= SSparseMatrix[<|SparseMatrix->SparseArray(Number of elements: 5 Dimensions: {4,5} Default value: 0),RowNames-><|
A->
1,B->2,C->3,D->4|>,ColumnNames-><|a->1,b->2,c->3,d->4,e->5|>,DimensionNames-><|U->
1,V->2|>|>]
>> rmat // MatrixForm
In[3]:= rmat // MatrixForm
Out[3]=
{{1,0,0,4,0},
 {0,2,0,0,0},
 {0,0,0,0,2},
 {0,0,3,0,0}}
>> rmat // MatrixPlot
In[4]:= rmat // MatrixPlot
Out[4]= <<<browser-graphics>>>
>> RowNames[rmat]
In[5]:= RowNames[rmat]
Out[5]= {A,B,C,D}
>> ColumnNames[rmat]
In[6]:= ColumnNames[rmat]
Out[6]= {a,b,c,d,e}
>> DimensionNames[rmat]
In[7]:= DimensionNames[rmat]
Out[7]= {U,V}
>> rmat.Transpose[rmat[[{1}, All]]]
In[8]:= rmat.Transpose[rmat[[{1}, All]]]
Out[8]= SSparseMatrix[<|SparseMatrix->SparseArray(Number of elements: 1 Dimensions: {4,1} Default value: 0),RowNames-><|
A->
1,B->2,C->3,D->4|>,ColumnNames-><|A->1|>,DimensionNames-><|1->1,2->2|>|>]
>> rmat[[{"C", "D", "A", "B"}, {"c", "d", "e", "a", "b"}]]
In[9]:= rmat[[{"C", "D", "A", "B"}, {"c", "d", "e", "a", "b"}]]
Out[9]= SSparseMatrix[<|SparseMatrix->SparseArray(Number of elements: 5 Dimensions: {4,5} Default value: 0),RowNames-><|
C->
1,D->2,A->3,B->4|>,ColumnNames-><|c->1,d->2,e->3,a->4,b->5|>,DimensionNames-><|U->
1,V->2|>|>]
>> rmat2 = ToSSparseMatrix[rmat, "RowNames" -> Map["s." <> # &, RowNames[rmat]]]
In[10]:= rmat2 = ToSSparseMatrix[rmat, "RowNames" -> Map["s." <> # &, RowNames[rmat]]]
Out[10]= SSparseMatrix[<|SparseMatrix->SparseArray(Number of elements: 5 Dimensions: {4,5} Default value: 0),RowNames-><
|s.A->
1,s.B->2,s.C->3,s.D->4|>,ColumnNames-><|a->1,b->2,c->3,d->4,e->5|>,DimensionNames-><|U->
1,V->2|>|>]
>> RowBind[rmat, rmat] // MatrixForm
In[11]:= RowBind[rmat, rmat] // MatrixForm
Out[11]=
{{1,0,0,4,0},
 {0,2,0,0,0},
 {0,0,0,0,2},
 {0,0,3,0,0},
 {1,0,0,4,0},
 {0,2,0,0,0},
 {0,0,0,0,2},
 {0,0,3,0,0}}
>> RowBind[rmat, rmat2] // MatrixForm
In[12]:= RowBind[rmat, rmat2] // MatrixForm
Out[12]=
{{1,0,0,4,0},
 {0,2,0,0,0},
 {0,0,0,0,2},
 {0,0,3,0,0},
 {1,0,0,4,0},
 {0,2,0,0,0},
 {0,0,0,0,2},
 {0,0,3,0,0}}
Clone this wiki locally