-
Notifications
You must be signed in to change notification settings - Fork 2
/
sample.m
192 lines (162 loc) · 6.09 KB
/
sample.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
(***
This is a sample Mathematica source code for testing `math-mode'. It doesn't
exercise every feature, but has a broad enough range for basic testing.
***)
ClearAll[mxAnd, mxOr, mxInterval, mxMakeInterval, mxIntervalPlot, mxIntervalSymbol];
mxMakeInterval::badarg = "The expression `` is not a relational operator (Inequality|Less|LessEqual|GreaterEqual|Greater) nor a logical Or of relational operators.";
(*
Less[x,a] and LessEqual[x,a]
x < a
nx <= a
*)
mxMakeInterval[(rop:(Less|LessEqual))[var_, value_]] :=
mxInterval[LessEqual, -\[Infinity], value, rop];
(***
Less[a,x,b] and LessEqual[a,x,b]
a < x < b
a <= x <= b
***)
mxMakeInterval[(rop:(Less|LessEqual))[leftValue_, var_, rightValue_]] :=
mxInterval[rop, leftValue, rightValue, rop];
(***
Greater[x,a] and GreaterEqual[x,a]
a > x
a >= x
***)
mxMakeInterval[(rop:(Greater|GreaterEqual))[var_, value_]] :=
mxInterval[rop, value, \[Infinity], GreaterEqual];
(***
Greater[a,x,b] and GreaterEqual[a,x,b]
a > x > b
a >= x >= b
***)
mxMakeInterval[(rop:(Greater|GreaterEqual))[leftValue_, var_, rightValue_]] :=
mxInterval[rop, leftValue, rightValue, rop];
(***
Inequality[a, Less|LessEqual, x, Less|LessEqual, b]
***)
mxMakeInterval[HoldPattern[Inequality[valueLeft_, ropLeft_, var_, ropRight_, valueRight_]]] :=
mxInterval[ropLeft, valueLeft, valueRight, ropRight];
(***
Or[... (relational expressions for one of the above mxMakeInterval functions)]
***)
mxMakeInterval[HoldPattern[Or[x: (_Less|_LessEqual|_GreaterEqual|_Greater|_Inequality) ..]]] :=
mxOr @@ Replace[HoldComplete[x], elem_ :> mxMakeInterval[elem], {1}];
(***
And[... (relational expressions for one of the above mxMakeInterval functions)]
***)
mxMakeInterval[HoldPattern[And[x: (_Less|_LessEqual|_GreaterEqual|_Greater|_Inequality) ..]]] :=
mxAnd @@ Replace[HoldComplete[x], elem_ :> mxMakeInterval[elem], {1}];
(***
Catch all for errors.
***)
(*mxMakeInterval[expr_] := (Message[mxMakeInterval::badarg, expr]; Return $fail;)*)
(***
Returns interval notation bracket for the given relational operation
(Less|LessEqual|GreaterEqual|Greater) and side of the interval (l or
r).
***)
mxIntervalSymbol[op_, side_] := Switch[
op,
Less|Greater, Switch[side, l, "[", r, "]"],
LessEqual|GreaterEqual, Switch[side, l, "(", r, ")"]];
(***
Format mxInterval in interval notation, e.g. [a,b).
***)
mxInterval /: MakeBoxes[mxInterval[ropLeft : (Less|LessEqual|Greater|GreaterEqual),
valueLeft_, valueRight_,
ropRight : (Less|LessEqual|Greater|GreaterEqual)], form_] :=
RowBox[{mxIntervalSymbol[ropLeft, l], MakeBoxes[valueLeft, form], ",",
MakeBoxes[valueRight, form], mxIntervalSymbol[ropRight, r]}];
(***
Format disjunction of mxIntervals using union infix operator.
This is another line.
And another.
I need to get newline to do an automatic indent.
***)
mxOr /: MakeBoxes[HoldPattern[mxOr[terms : _mxInterval ..]], form_] :=
RowBox[Riffle[List @@ Replace[HoldComplete[terms],
elem_ :> Parenthesize[elem, form, Or, True],
{1}],
"\[Union]"]];
(***
Format conjunction of mxIntervals using intersection infix operator.
***)
mxAnd /: MakeBoxes[HoldPattern[mxAnd[terms : _mxInterval ..]], form_] :=
RowBox[Riffle[List @@ Replace[HoldComplete[terms],
elem_ :> Parenthesize[elem, form, Or, True],
{1}],
"\[Intersection]"]];
(***
Graph the intervals on a number line.
***)
Options[mxIntervalPlot] = {
Stack -> False,
Colors -> { Blue, Red, Green }
};
mxIntervalPlot[term_mxInterval] := mxIntervalPlot[mxOr[term]];
mxIntervalPlot[mxOr[term__mxInterval]] :=
Module[
{
terms = {term},
radius = 0.2, (* The radius of the circle/disk markers *)
offset = 0.5, (* The offset of the markers above the axis *)
min = -10.0, (* The minimum number on the extreme left of the number line *)
max = 10.0 (* The maximum number of the exterme right of the number line *)
},
{
range = {Min@#, Max@#} & /@ {
Select[Flatten[
terms /. mxInterval[lop_, lv_, rv_, rop_] :> {lv, rv}],
-\[Infinity] < # < \[Infinity] &]
};
range = range /. { -\[Infinity] :> 5, \[Infinity] :> -5 };
(*Print[range];*)
(* Return the end point graphic for the given operation. *)
endPoint[op_, px_] :=
Switch[op, Less | Greater, Circle, Equal | LessEqual | GreaterEqual, Disk] @@
List[{px, offset}, radius];
(* The segment is from -inf to +inf. *)
segment[opLeft_, -\[Infinity], \[Infinity], opRight_] :=
{
Blue,
Arrowheads[{-Medium, Medium}], Arrow[{ {min, offset}, {max, offset} }]
};
(* The segment is from -inf to a. *)
segment[opLeft_, -\[Infinity], valueRight_, opRight_] :=
{
Blue,
endPoint[opRight, valueRight],
Arrow[{ {valueRight - radius, offset}, {min, offset} }]
};
(* The segment is from a to +inf. *)
segment[opLeft_, valueLeft_, \[Infinity], opRight_] :=
{
Blue,
endPoint[opLeft, valueLeft],
Arrow[{ {valueLeft + radius, offset}, {max, offset} }]
};
(* The segment is from a to b. *)
segment[opLeft_, valueLeft_, valueRight_, opRight_] :=
{
Blue,
endPoint[opLeft, valueLeft],
endPoint[opRight, valueRight],
Line[{ {valueLeft + radius, offset}, {valueRight - radius, offset} }]
};
(***
Print[terms];
Print[# /. mxInterval[opLeft_, valueLeft_, valueRight_, opRight_] :>
segment[opLeft, valueLeft, valueRight, opRight]
& /@ {terms}];
***)
Show[{
Plot[0, {x, -10, 10}, Axes -> {True, False}],
Graphics[# /. mxInterval[opLeft_, valueLeft_, valueRight_, opRight_] :>
segment[opLeft, valueLeft, valueRight, opRight]
& /@ {terms}
] (*Close Graphics*)
},
AspectRatio -> Automatic
] (*Close Show*)
}] (*Close Module*)