-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvexFunction.html
executable file
·374 lines (318 loc) · 13.7 KB
/
ConvexFunction.html
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!DOCTYPE html>
<html>
<head>
<title>Convex Functions</title>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
showProcessingMessages: false,
tex2jax: { inlineMath: [['$','$'],['\\(','\\)']] },
TeX: { extensions: ["color.js"] }
});
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-debug.js"></script>
<script type="text/javascript" src="https://d3js.org/d3.v5.min.js"></script>
<script type="text/javascript" src="https://d3js.org/d3-selection-multi.v1.min.js"></script>
<!-- <script type="text/javascript" src="./ko/knockout-3.4.2.debug.js"></script>
<script type="text/javascript" src="./d3/d3.v5.js"></script>
<script type="text/javascript" src="./d3-selection-multi/d3-selection-multi.js"></script>
<script type="text/javascript" src="./mathjax-2.7.5/MathJax.js?config=TeX-MML-AM_HTMLorMML"></script> -->
<script type="text/javascript" src="./js/mathexpressions.js"></script>
<script type="text/javascript" src="./js/space2dim.js"></script>
<script type="text/javascript" src="./js/space3dim.js"></script>
<script type="text/javascript" src="./js/point.js"></script>
<script type="text/javascript" src="./js/circle.js"></script>
<script type="text/javascript" src="./js/vector.js"></script>
<script type="text/javascript" src="./js/mathfunction.js"></script>
<script type="text/javascript" src="./js/rico.js"></script>
<script type="text/javascript" src="./js/line.js"></script>
<script type="text/javascript" src="./js/dimlength.js"></script>
<script type="text/javascript" src="./js/mathfunction3d.js"></script>
<link rel="stylesheet" type="text/css" href="./css/article.css">
<link rel="stylesheet" type="text/css" href="./css/space2dim.css">
<link rel="stylesheet" type="text/css" href="./css/space3dim.css">
<link rel="stylesheet" type="text/css" href="./css/vector.css">
<link rel="stylesheet" type="text/css" href="./css/line.css">
<link rel="stylesheet" type="text/css" href="./css/dimlength.css">
<style>
.funcplot {
stroke: #000000;
stroke-width: 1;
fill: none;
}
.abovefunc {
stroke: #00ff21;
stroke-width: 1;
}
.belowfunc {
stroke: #ff0000;
stroke-width: 1;
}
.xsegment ,
.vectorarrow.xsegment
{
stroke: #949410;
stroke-width: 1;
fill: #949410;
}
.absegemnt ,
.vectorarrow.absegemnt
{
stroke: #105f94;
stroke-width: 4;
fill: #105f94;
}
</style>
</head>
<body>
<div>
<h1>Definition</h1>
<p>
$$f( \lambda A + (1- \lambda)B) \leq \lambda f(A) + (1- \lambda)f(B)$$
</p>
<p>in which $A$ and $B$ are two points in $\mathbb{R}^n$ and $\lambda$ is in the interval $(0, 1)$</p>
<p>What the above expression says in words is:</p>
<p>The value of the function for any value $X$ on the line segment between $A$ and $B$ is less then any value on the line segment between the result of the function in $A$ and $B$.</p>
</div>
<div>
<h1 id="learn_convex">A Convex Function</h1>
<div>
<div>
<p>A sample:</p>
<p>The function is:</p>
<p>$$f(x) = 0.04 * x^2 - 40 $$</p>
</div>
<div>
<p>The above expression becomes:</p>
<div class="container_height250">
<div id="convexfunction_sample">
$${}$$
</div>
</div>
</div>
</div>
<div style="width:100%">
<p>You can drag the two black dots to change the endpoints $A$ and $B$ of the linesegment.</p>
<p>The yellow-ish arrows represent the expression $\lambda A + (1- \lambda)B$ and then the value of the function $f()$. The blue-ish arrow represents the expression $\lambda f(A) + (1- \lambda)f(B)$</p>
<p>Change the value of $\lambda$ by dragging the slider:</p>
<div class="divcentered">
<input type="range" min="0" max="1" step="0.01" data-bind="value: ConvexLinesegment.lambda, valueUpdate: 'input'" />
</div>
<div id="convexfunctionsample" class="divcentered"></div>
</div>
</div>
<div>
<h1 id="learn_notconvex">A NOT Convex function</h1>
<div>
<div>
<p>A sample:</p>
<p>The function is:</p>
<p>$$g(x) = x - 10 $$</p>
<p>$$f(x) = (0.001 * g(x)^3) + (0.03 * g(x)^2) - 10 $$</p>
</div>
<div>
<p>The above expression becomes:</p>
<div class="container_height250">
<div id="notconvexfunction_sample">
$${}$$
</div>
</div>
</div>
</div>
<div style="width:100%">
<p>You can drag the two black dots to change the endpoints $A$ and $B$ of the linesegment.</p>
<p>The yellow-ish arrows represent the expression $\lambda A + (1- \lambda)B$ and then he value of the function $f()$. The blue-ish arrow represents the expression $\lambda f(A) + (1- \lambda)f(B)$</p>
<p>Change the value of $\lambda$ by dragging the slider:</p>
<div class="divcentered">
<input type="range" min="0" max="1" step="0.01" data-bind="value: ConcaveLinesegment.lambda, valueUpdate: 'input'" />
</div>
<div id="notconvexfunctionsample" class="divcentered"></div>
</div>
</div>
<script type="text/javascript">
let someConvexFunctionFactory = function () {
let f = function (x) {
return 0.04 * x * x - 40;
}
return f;
}
let someNotconvexFunctionFactory = function () {
let f = function (x) {
let xt = (x - 10);
return (0.001 * xt * xt * xt) + (0.03 * xt * xt) - 10;
}
return f;
}
function createConvexSample(divid, mathid, funcFactory) {
let mathExpression = new MathExpressions();
let space = new Space2Dim(divid, 300, 300, -50, 50, -50, 50);
function ConvexSampleViewModel() {
this.lambda = ko.observable(0.5);
this.lambda.subscribe(function (newValue) {
space.update();
mathExpression.QueueUpdate();
});
let convexFunc = funcFactory();
this.p1x = ko.observable(-30);
this.p1y = ko.computed(function () {
return convexFunc(this.p1x());
}, this);
this.p2x = ko.observable(30);
this.p2y = ko.computed(function () {
return convexFunc(this.p2x());
}, this);
this.circlesToShow = ko.observableArray([]);
this.dimlengthToShow = ko.observableArray([]);
let px1s = this.p1x;
let py1s = this.p1y;
let px2s = this.p2x;
let py2s = this.p2y;
let convexEvaluation = function (src) {
src.circlesToShow.removeAll();
src.dimlengthToShow.removeAll();
//convexStartPoints.removeAll();
let idealStepWidth = 5;
let x1s = px1s();
let y1s = py1s();
let x2s = px2s();
let y2s = py2s();
let nofSteps = Math.round((x2s - x1s) / idealStepWidth);
let lambdaStep = 1.0 / nofSteps;
for (let i = 1; i < nofSteps; i++)
{
let lambda = i * lambdaStep;
let xd = (1 - lambda) * x1s + lambda * x2s;
let ys = convexFunc(xd);
let ye = (1 - lambda) * y1s + lambda * y2s;
let ps = new Point2Dim({ x: ko.observable(xd), y: ko.observable(ys) }, {});
let pe = new Point2Dim({ x: ko.observable(xd), y: ko.observable(ye) }, {});
//convexStartPoints.push(ps);
//convexStartPoints.push(pe);
let cssclass = "abovefunc";
if (ye <= ys)
cssclass = "belowfunc";
let dim = new DimLength2Dim(
{ p1: ps, p2: pe },
{ cssclass: cssclass }
);
src.dimlengthToShow.push(dim);
let psHandle = new Circle(
{ center: ps, radius: ko.observable(0.5) },
{ draggable: 0 }
);
let peHandle = new Circle(
{ center: pe, radius: ko.observable(0.5) },
{ draggable: 0 }
);
src.circlesToShow.push(psHandle);
src.circlesToShow.push(peHandle);
}
}
let me = this;
this.p1x.subscribe(function (newValue) {
space.update();
mathExpression.QueueUpdate();
convexEvaluation(me);
});
this.p2x.subscribe(function (newValue) {
space.update();
mathExpression.QueueUpdate();
convexEvaluation(me);
});
let pl1 = new Point2Dim({ x: this.p1x, y: this.p1y }, {});
let pl2 = new Point2Dim({ x: this.p2x, y: this.p2y }, {});
let linesegment = new Vector2Dim(
{ p1: pl1, p2: pl2 },
{ showEndArrow: 0, dvaluelvl: 0, p1draggable: 0, p2draggable: 0 }
);
let func = new Function2Dim({ func: funcFactory() }, {});
let xX = ko.computed(function(){
let a = this.p1x();
let b = this.p2x();
return this.lambda() * a + (1 - this.lambda()) * b;
}, this);
let xsegmentp1 = new Point2Dim({ x: xX, y: ko.observable(0) }, {});
let xsegmentp2 = new Point2Dim({ x: ko.observable(0), y: ko.observable(0) }, {});
let xsegment = new Vector2Dim(
{ p1: xsegmentp2, p2: xsegmentp1 },
{ name: "X", showEndArrow: 1, dvaluelvl: 0, p1draggable: 0, p2draggable: 0, cssclass: "xsegment" }
);
let fx = ko.computed(function(){
return convexFunc(xX());
})
let fxp = new Point2Dim({ x: xX, y: fx }, {});
let fxsegment = new Vector2Dim(
{ p1: xsegmentp1, p2: fxp },
{ name: "f(X)", showEndArrow: 1, dvaluelvl: 0, p1draggable: 0, p2draggable: 0, cssclass: "xsegment" }
);
let fa = ko.computed(function(){
let a = this.p1x();
return convexFunc(a);
}, this);
let fb = ko.computed(function(){
let b = this.p2x();
return convexFunc(b);
}, this);
let m = ko.computed(function(){
return this.lambda() * fa() + (1 - this.lambda()) * fb();
}, this);
let absegemntpa = new Point2Dim({ x: this.p1x, y: fa }, {});
let absegemntpm = new Point2Dim({ x: xX, y: m }, {});
let absegemnt = new Vector2Dim(
{ p1: absegemntpa, p2: absegemntpm },
{ showEndArrow: 1, dvaluelvl: 0, p1draggable: 0, p2draggable: 0, cssclass: "absegemnt" }
);
let getMathExpression = function () {
let a = me.p1x();
let b = me.p2x();
let lambda = me.lambda();
let aFormatted = d3.format(".2f")(a);
let bFormatted = d3.format(".2f")(b);
let lambdaFormatted = d3.format(".2f")(lambda);
let xFormatted = d3.format(".2f")(xX());
let fxFormatted = d3.format(".2f")(fx());
let faFormatted = d3.format(".2f")(fa());
let fbFormatted = d3.format(".2f")(fb());
let mFormatted = d3.format(".2f")(m());
let mathExpression = "\\begin{aligned}"
+ "f( \\lambda A + (1- \\lambda)B)"
+ " &= f(" + lambdaFormatted + "*" + aFormatted + "+(1-" + lambdaFormatted + ")*" + bFormatted + ")\\\\"
+ " &= f(" + xFormatted + ")\\\\"
+ " &= " + fxFormatted + "\\\\"
+ "\\lambda f(A) + (1- \\lambda)f(B)"
+ " &= " + lambdaFormatted + "f(" + aFormatted + ") + (1-" + lambdaFormatted + ")f(" + bFormatted + ")\\\\"
+ " &= " + lambdaFormatted + "*" + faFormatted + " + (1-" + lambdaFormatted + ")*" + fbFormatted + "\\\\"
+ " &= " + mFormatted + "\\\\"
+ "f( \\lambda A + (1- \\lambda)B) &\\leq \\lambda f(A) + (1- \\lambda)f(B)\\\\"
+ fxFormatted + " &\\leq " + mFormatted + "\\\\"
+ "\\end{aligned}";
return mathExpression;
}
mathExpression.RegisterExpression(mathid, getMathExpression);
this.funcsToShow = [func];
this.pointsToShow = [pl1, pl2];
this.getPointsToShow = function () {
result = this.pointsToShow; //.concat(convexStartPoints());
return result;
};
this.vectorsToShow = [linesegment, xsegment, fxsegment, absegemnt];
convexEvaluation(me);
}
let vm = new ConvexSampleViewModel();
space.registerHandler(Function2Dim.draw, Function2Dim.update, function () { return vm.funcsToShow; });
space.registerHandler(Point2Dim.draw, Point2Dim.update, function () { return vm.getPointsToShow(); });
space.registerHandler(Circle.draw, Circle.update, function () { return vm.circlesToShow(); });
space.registerHandler(Vector2Dim.draw, Vector2Dim.update, function () { return vm.vectorsToShow; });
space.registerHandler(DimLength2Dim.draw, DimLength2Dim.update, function () { return vm.dimlengthToShow(); });
space.show();
return vm;
}
function ViewModel() {
this.ConvexLinesegment = createConvexSample("convexfunctionsample", "convexfunction_sample", someConvexFunctionFactory);
this.ConcaveLinesegment = createConvexSample("notconvexfunctionsample", "notconvexfunction_sample", someNotconvexFunctionFactory);
}
let vm = new ViewModel();
ko.applyBindings(vm);
</script>
</body>
</html>