forked from Expander/FlexibleSUSY
/
Utils.m
193 lines (139 loc) · 4.97 KB
/
Utils.m
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
BeginPackage["Utils`"];
AppendOrReplaceInList::usage="Replaces existing element in list,
or appends it if not already present.";
ApplyAndConcatenate::usage = "Applies a function to a list and
concatenates the resulting list.";
InputFormOfNonStrings::usage = "apply InputForm to non-strings";
MaxRelDiff::usage="Returns maximum relative difference between numbers
in a given list. The second argument defines the numeric underflow.
In[]:= MaxRelDiff[{1, 1.1, 0.9}]
Out[]= 0.181818
In[]:= MaxRelDiff[{0, 10^(-100)}]
Out[]= 0
In[]:= MaxRelDiff[{0, 10^(-100)}, 10^(-101)]
Out[]= 1
In[]:= MaxRelDiff[{1, -1}]
Out[]= 2
";
StringJoinWithSeparator::usage = "Joins a list of strings with a given separator string";
Zip::usage = "Combines two lists to a list of touples.
Example:
In[]:= Zip[{a,b,c},{d,e,f}]
Out[]= {{a, d}, {b, e}, {c, f}}
";
StringZip::usage = "Combines lists of strings to a list of concated
strings. Example:
In[]:= StringZip[{\"a\",\"b\"},{\"d\",\"e\"}]
Out[]= {\"ad\", \"be\"}}
";
StringZipWithSeparator::usage = "Combines lists of strings to a list
of concated strings using a separator. Example:
In[]:= StringZipWithSeparator[{\"a\",\"b\"},{\"d\",\"e\"}, \"_\"]
Out[]= {\"a_d\", \"b_e\"}}
";
SplitList::usage = "split list into list of sub-lists of given maximum
size";
ForceJoin::usage = "Joins the given arguments if they are lists.";
FSGetOption::usage = "Returns the value of an option from a list of
options.
Example:
In[]:= opts = {o1 -> x, o2 -> y}
In[]:= FSGetOption[opts, o1]
Out[]= x
In[]:= FSGetOption[opts, o2]
Out[]= y
In[]:= FSGetOption[opts, o3]
Error: option o3 not found
";
FSSetOption::usage = "Returns given list of options, where the
occurrence of the given rule is replaced (if it exists) or added (if
it does not exist).
Example:
In[]:= opts = {o1 -> x, o2 -> y}
In[]:= FSSetOption[opts, o1 -> z]
Out[]= {o2 -> y, o1 -> z}
In[]:= FSSetOption[opts, o3 -> z]
Out[]= {o1 -> x, o2 -> y, o3 -> z}
";
FSImportString::usage = "Returns the content of a file in form of a string. If the file does not exist, \"unknown\" is returned.";
FSStringPadLeft::usage = "StringPadLeft[] for Mathematica 9 and below.";
Begin["`Private`"];
AppendOrReplaceInList[values_List, elem_, test_:SameQ] :=
Module[{matches, result},
matches = test[elem, #]& /@ values;
matches = (If[# =!= True && # =!= False, False, #])& /@ matches;
If[!Or @@ matches,
result = Append[values, elem];,
result = ReplacePart[values, Position[matches, True] -> elem];
];
result
];
ApplyAndConcatenate[Func_, l_List] :=
Module[{result = ""},
(result = result <> Evaluate[Func[#]])& /@ l;
result
];
ApplyAndConcatenate[Func_, l_] := Evaluate[Func[l]];
SetAttributes[ApplyAndConcatenate, HoldFirst];
StringJoinWithSeparator[list_List, separator_String, transformer_:ToString] :=
StringJoin[Riffle[transformer /@ list, separator]];
Zip[list1_List, list2_List] :=
MapThread[List, {list1, list2}];
StringZip[lists___List] :=
MapThread[StringJoin, {lists}];
StringZipWithSeparator[lists___List, separator_String] :=
MapThread[StringJoinWithSeparator[{##},separator]&, {lists}];
SplitList[lst_List, 0] := {lst};
SplitList[lst_List, size_Integer] :=
Module[{result = {}, list = lst, drops},
While[list =!= {},
drops = Min[size,Length[list]];
AppendTo[result, Take[list, drops]];
list = Drop[list, drops];
];
result
];
FSGetOption[opts_List, opt_] :=
Module[{values},
values = Cases[opts, (Rule[opt, value_] | RuleDelayed[opt, value_]) :> value];
Switch[Length[values],
0, Print["Error: option ", opt, " not found"];
Null,
1, values[[1]],
_, Print["Warning: option ", opt, " not unique"];
values[[1]]
]
];
FSSetOption[opts_List, rule:Rule[opt_, value_]] :=
Join[
Cases[opts, Except[Rule[opt,_] | RuleDelayed[opt,_]]],
{rule}
];
FSSetOption[opts_List, rule:RuleDelayed[opt_, value_]] :=
Join[
Cases[opts, Except[Rule[opt,_] | RuleDelayed[opt,_]]],
{rule}
];
FSImportString[fileName_String] :=
Module[{str = Import[fileName, "String"]},
If[str =!= $Failed,
str,
"unknown"
]
];
FSStringPadLeft[str_String, width_, pad_String] :=
StringJoin[PadLeft[Characters[str], width, pad]];
ForceJoin[elem___] :=
Join[Sequence @@ Select[{elem}, (Head[#] === List)&]];
InputFormOfNonStrings[a_String] := a;
InputFormOfNonStrings[a_] := InputForm[a];
MaxRelDiff[{}, _] := 0;
MaxRelDiff[{a_, b_}, underflow_:10^(-16)] :=
If[Max[Abs[{a,b}]] < underflow,
0,
Abs[(a-b)/Max[Abs[{a,b}]]]
];
MaxRelDiff[numbers_List, underflow_:10^(-16)] :=
Max[MaxRelDiff[#,underflow]& /@ Tuples[numbers, 2]];
End[];
EndPackage[];