forked from KirkMcDonald/kirkmcdonald.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.js
132 lines (122 loc) · 4.51 KB
/
debug.js
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
/*Copyright 2015-2019 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
"use strict"
function getSolutionHeader(matrixSolver, costHeader) {
var row = document.createElement("tr")
var items = matrixSolver.items
for (var i = 0; i < items.length; i++) {
var item = items[i]
var cell = document.createElement("th")
cell.appendChild(new Text("s"))
cell.appendChild(getImage(item))
row.appendChild(cell)
}
var cell = document.createElement("th")
cell.appendChild(new Text("tax"))
row.appendChild(cell)
var recipes = matrixSolver.recipes
for (var i = 0; i < recipes.length; i++) {
var obj = recipes[i]
var cell = document.createElement("th")
cell.appendChild(getImage(obj))
row.appendChild(cell)
}
cell = document.createElement("th")
cell.appendChild(new Text("answer"))
row.appendChild(cell)
if (costHeader) {
var cell = document.createElement("th")
cell.appendChild(new Text("C"))
row.appendChild(cell)
}
return row
}
function renderMatrix(matrixSolver, A, rowIcons) {
var table = document.createElement("table")
table.border = "1"
var header = getSolutionHeader(matrixSolver, true)
if (rowIcons) {
header.insertBefore(document.createElement("th"), header.firstChild)
}
table.appendChild(header)
for (var j = 0; j < A.rows; j++) {
var row = document.createElement("tr")
table.appendChild(row)
if (rowIcons) {
var td = document.createElement("td")
if (j < matrixSolver.recipes.length) {
var recipes = matrixSolver.recipes[j]
td.appendChild(getImage(recipes))
} else if (j == A.rows - 2) {
td.appendChild(new Text("tax"))
} else if (j == A.rows - 1) {
td.appendChild(new Text("answer"))
}
row.appendChild(td)
}
for (var k = 0; k < A.cols; k++) {
var cell = document.createElement("td")
cell.classList.add("right-align")
row.appendChild(cell)
var x = A.index(j, k)
var tt = document.createElement("tt")
tt.textContent = x.toMixed()
cell.appendChild(tt)
}
}
return table
}
function renderDebug() {
var debugTab = document.getElementById("debug_tab")
var oldMatrixes = document.getElementById("matrixes")
var node = document.createElement("div")
node.id = "matrixes"
debugTab.replaceChild(node, oldMatrixes)
for (var i = 0; i < solver.matrixSolvers.length; i++) {
var matrixSolver = solver.matrixSolvers[i]
var A = matrixSolver.matrix
var table = renderMatrix(matrixSolver, A, true)
node.appendChild(table)
}
var oldSolutions = document.getElementById("solution")
node = document.createElement("div")
node.id = "solution"
debugTab.replaceChild(node, oldSolutions)
for (var i = 0; i < solver.matrixSolvers.length; i++) {
var matrixSolver = solver.matrixSolvers[i]
var A = matrixSolver.lastProblem
if (A) {
var table = renderMatrix(matrixSolver, A, false)
node.appendChild(table)
}
A = matrixSolver.lastSolution
if (A) {
//var basis = getBasis(A)
var table = document.createElement("table")
table.border = "1"
var header = getSolutionHeader(matrixSolver, true)
table.appendChild(header)
var row = document.createElement("tr")
table.appendChild(row)
for (var j = 0; j < A.cols; j++) {
var cell = document.createElement("td")
cell.classList.add("right-align")
row.appendChild(cell)
//var x = basis[j]
var x = A.index(A.rows - 1, j)
var tt = document.createElement("tt")
tt.textContent = x.toDecimal(3)
cell.appendChild(tt)
}
node.appendChild(table)
}
}
}