-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathutilPrivate.m
43 lines (33 loc) · 1.13 KB
/
utilPrivate.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
(* Yi Wang, 2013, tririverwangyi@gmail.com, GPLv3 *)
BeginPackage["MathGR`utilPrivate`"]
defQ
plus2list
expand2list
apply2term
replaceTo
getSampleTerm
times2prod
prod2times
prod
add2set
add2pattern
take
Begin["`Private`"]
SetAttributes[defQ, HoldAll];
defQ[x_]:= {OwnValues[x],UpValues[x],DownValues[x]} =!= {{},{},{}};
plus2list = If[Head[#]===Plus||Head[#]===List, List@@#, {#}]&
expand2list = plus2list[Expand@#]&
times2list = If[Head[#] === Times || Head[#] === List, List @@ #, {#}] &;
apply2term = Total[#1/@expand2list[#2]]&
replaceTo = Thread[RuleDelayed[##]]&
getSampleTerm = Function[e, If[Head@#===Plus, #[[1]], #]&[Expand@e]]
SetAttributes[prod,Flat]
times2prod[expr_]:= expr /. {Times->prod,Power[a_,n_/;IntegerQ[n]&&n>0]:>prod@@ConstantArray[a,n]}
times2prod[expr_, t_]:= times2prod[expr] /. prod->t
prod2times[expr_, t_:prod]:= expr /. t->Times
SetAttributes[{add2set, add2pattern}, HoldFirst]
add2set[li_, elem_]:= If[Head[li]===List, li=Union[li,Flatten@{elem}], li = Flatten@{elem}]
add2pattern[pi_, elem_]:= If[ValueQ[pi], pi=pi|elem, pi=elem]
take[list_, n_] := If[Length@list > n, Take[list, n], list]
End[]
EndPackage[]