-
Notifications
You must be signed in to change notification settings - Fork 0
/
YoujackPackage.wl
141 lines (126 loc) · 4.39 KB
/
YoujackPackage.wl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
BeginPackage["YoujackPackage`"];
(* Styles *)
ItalicStyle;
PlainStyle;
NumStyle;
(* LinearFitPlot *)
LinearFitPlot::usage =
"LinearFitPlot[{labels},
{data}, IncludeConstantBasis->False,
range, axesQty, axesDim]
= {TableForm@info, figure}";
(* Wolfram Player *)
WolframPlayer::usage = "WolframPlayer[expr]\nWolframPlayer[expr, name]";
(* InteractiveMapAt *)
InteractiveMapAt::usage = "";
Begin["`Private`"];
(* Styles *)
$YoujackPlotColor = ColorData[97,"ColorList"];
(* SetAttributes[YoujackMathForm, HoldAll]; *)
(* YoujackMathForm[expr_] := Style[TraditionalForm@HoldForm@expr, FontFamily->"Times"] *)
ItalicStyle[expr_] := Style[expr, Italic, FontFamily->"Times"];
PlainStyle[expr_] := Style[expr, Plain, FontFamily->"Times"];
NumStyle[num_?NumberQ] := PlainStyle@num;
(* LinearFitPlot *)
LinearFitPlot[labels_?ListQ,
data_?ListQ, includeConstantBasis_?OptionQ,
range_, axesQty_?ListQ, axesDim_?ListQ] := Module[
{
len,
model, func, info,
color, listPlot, plot, axesLabel, figure
},
len = Length@labels;
(* Fit *)
model = Table[LinearModelFit[data[[n]], x,x, includeConstantBasis], {n,1,len}];
func = Table[model[[n]]@"BestFit", {n,1,len}];
info = Table[{
labels[[n]],
func[[n]] /. {x->"x"},
Row@{Superscript["R",2],"=",model[[n]]@"RSquared"}
}, {n,1,len}];
(* Plot *)
color = Table[$YoujackPlotColor[[n]], {n,1,len}];
listPlot = ListPlot[data, PlotLegends->labels, PlotStyle->color];
plot = Plot[func, {x,range[[1]],range[[2]]}, PlotStyle->color];
axesLabel = Table[DisplayForm@Row@{
ItalicStyle@axesQty[[i]], PlainStyle@Row@{" (",axesDim[[i]],")"}
}, {i,1,2}];
figure = Show[listPlot, plot,
PlotRange->All, PlotRangePadding->None, AxesOrigin->{0,0},
AxesLabel->axesLabel, GridLines->Automatic];
(* Return *)
{TableForm@info, figure}
];
(* WolframPlayer *)
WolframPlayer[expr_, name_?StringQ] :=
With[
{ dir = Evaluate@FileNameJoin@{$UserDocumentsDirectory, "Wolfram Player", name<>".cdf"} },
Export[dir, #, "CDF"]& @ Notebook[{Cell[BoxData@ToBoxes@expr,"Output"]}, WindowSize->All];
StartProcess@{"WolframPlayer", "\""<>dir<>"\""};
ToString@Head@expr<>" in Wolfram Player"
];
WolframPlayer[expr_] := WolframPlayer[expr, CreateUUID["CDFOutput-"]];
(* InteractiveMapAt *)
InteractiveMapAt::wrongsize = "The argument list has a wrong size.";
InteractiveMapAt::backat0 = "Already back to depth\[Hyphen]0.";
InteractiveMapAt[fSeq___, OptionsPattern[{Print -> True}]][expr_] := Module[
{
fList, fListLen,
exprList, posList, exprP, fexprP, posString,
LocalVarColor = RGBColor[0.235, 0.49, 0.568],
HeadColor = RGBColor[1., 0.72, 0.]
},
If[OddQ[fListLen = Length[fList = {fSeq}]],
Message[InteractiveMapAt::wrongsize]; Abort[]];
exprList = {expr}; posList = {};
Do[
Which[
fList[[2i-1]] === 0, (
posString = {"Depth[", Length@posList, "] = "};
exprP = exprList[[-1]];
fexprP = fList[[2i]][exprP];
exprList[[-1]] = fexprP;
),
fList[[2i-1]] === Back, If[posList =!= {},
posString = {"Back to Depth[", Length@posList - 1, "] = "};
exprP = Head[exprList[[-2]]][
Delete[exprList[[-2]], List /@ posList[[-1]]],
exprList[[-1]]
];
fexprP = fList[[2i]][exprP];
exprList = Delete[exprList, -1]; exprList[[-1]] = fexprP;
posList = Delete[posList, -1],
(* posList === {} *)
Message[InteractiveMapAt::backat0]; Continue[]
],
True, (
posString = {
"Depth[", Length@posList + 1, "] \[Congruent] ",
"Depth[", Length@posList, "]",
"\[LeftDoubleBracket]", fList[[2 i - 1]], "\[RightDoubleBracket] = "};
exprP = exprList[[-1, fList[[2i-1]]]];
fexprP = fList[[2i]][exprP];
AppendTo[exprList, fexprP];
AppendTo[posList, fList[[2i-1]]];
)
];
If[OptionValue[Print], Print @@
(posString // Map[Style[#,LocalVarColor]&]) ~Join~
{Style[Head@exprP,HeadColor], Style[List@@exprP,Black]} ~Join~
If[fList[[2 i]] === Identity, {}, {
Style[" \[Rule] ",LocalVarColor],
Style[Head@fexprP,HeadColor], Style[List@@fexprP,Black]
}]
],
{i, fListLen/2}];
Do[
exprList[[i]] = Head[exprList[[i]]][
Delete[exprList[[i]], List /@ posList[[i]]],
exprList[[i + 1]]
],
{i, Length@posList, 1, -1}];
exprList[[1]]
];
End[];
EndPackage[];