6663b6c9
adorian
projet complet av...
|
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
#include "calculation_controller.h"
#include "../constant.h"
#include "../apps_container.h"
#include "../shared/poincare_helpers.h"
#include "../../poincare/src/layout/char_layout.h"
#include "../../poincare/src/layout/horizontal_layout.h"
#include "../../poincare/src/layout/vertical_offset_layout.h"
#include <poincare.h>
#include <assert.h>
using namespace Poincare;
using namespace Shared;
namespace Regression {
static inline int max(int x, int y) { return (x>y ? x : y); }
CalculationController::CalculationController(Responder * parentResponder, ButtonRowController * header, Store * store) :
TabTableController(parentResponder, this),
ButtonRowDelegate(header, nullptr),
m_titleCells{},
m_r2TitleCell(nullptr),
m_columnTitleCells{},
m_doubleCalculationCells{},
m_calculationCells{},
m_hideableCell(nullptr),
m_store(store)
{
m_r2Layout = new HorizontalLayout(new CharLayout('r', KDText::FontSize::Small), new VerticalOffsetLayout(new CharLayout('2', KDText::FontSize::Small), VerticalOffsetLayout::Type::Superscript, false), false);
}
CalculationController::~CalculationController() {
if (m_r2Layout) {
delete m_r2Layout;
m_r2Layout = nullptr;
}
}
const char * CalculationController::title() {
return I18n::translate(I18n::Message::StatTab);
}
bool CalculationController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::Up) {
selectableTableView()->deselectTable();
app()->setFirstResponder(tabController());
return true;
}
return false;
}
void CalculationController::didBecomeFirstResponder() {
if (selectedRow() == -1) {
selectCellAtLocation(1, 0);
} else {
selectCellAtLocation(selectedColumn(), selectedRow());
}
TabTableController::didBecomeFirstResponder();
}
void CalculationController::tableViewDidChangeSelection(SelectableTableView * t, int previousSelectedCellX, int previousSelectedCellY) {
/* To prevent selecting cell with no content (top left corner of the table),
* as soon as the selected cell is the top left corner, we either reselect
* the previous cell or select the tab controller depending on from which cell
* the selection comes. This trick does not create an endless loop as the
* previous cell cannot be the top left corner cell if it also is the
* selected one. */
if (t->selectedRow() == 0 && t->selectedColumn() == 0) {
if (previousSelectedCellX == 0 && previousSelectedCellY == 1) {
selectableTableView()->deselectTable();
app()->setFirstResponder(tabController());
} else {
t->selectCellAtLocation(0, 1);
}
}
if (t->selectedColumn() > 0 && t->selectedRow() >= 0 && t->selectedRow() <= k_totalNumberOfDoubleBufferRows) {
EvenOddDoubleBufferTextCellWithSeparator * myCell = (EvenOddDoubleBufferTextCellWithSeparator *)t->selectedCell();
bool firstSubCellSelected = true;
if (previousSelectedCellX > 0 && previousSelectedCellY >= 0 && previousSelectedCellY <= k_totalNumberOfDoubleBufferRows) {
EvenOddDoubleBufferTextCellWithSeparator * myPreviousCell = (EvenOddDoubleBufferTextCellWithSeparator *)t->cellAtLocation(previousSelectedCellX, previousSelectedCellY);
firstSubCellSelected = myPreviousCell->firstTextSelected();
}
myCell->selectFirstText(firstSubCellSelected);
}
}
bool CalculationController::isEmpty() const {
return m_store->isEmpty();
}
I18n::Message CalculationController::emptyMessage() {
return I18n::Message::NoValueToCompute;
}
Responder * CalculationController::defaultController() {
return tabController();
}
int CalculationController::numberOfRows() {
return 1 + k_totalNumberOfDoubleBufferRows + 4 + maxNumberOfCoefficients() + hasLinearRegression() * 2;
}
int CalculationController::numberOfColumns() {
return 1 + m_store->numberOfNonEmptySeries();
}
void CalculationController::willDisplayCellAtLocation(HighlightCell * cell, int i, int j) {
if (i == 0 && j == 0) {
return;
}
EvenOddCell * myCell = (EvenOddCell *)cell;
myCell->setEven(j%2 == 0);
myCell->setHighlighted(i == selectedColumn() && j == selectedRow());
// Calculation title
if (i == 0) {
bool shouldDisplayRAndR2 = hasLinearRegression();
int numberRows = numberOfRows();
if (shouldDisplayRAndR2 && j == numberRows-1) {
EvenOddExpressionCell * myCell = (EvenOddExpressionCell *)cell;
myCell->setExpressionLayout(m_r2Layout);
return;
}
MarginEvenOddMessageTextCell * myCell = (MarginEvenOddMessageTextCell *)cell;
myCell->setAlignment(1.0f, 0.5f);
if (j <= k_regressionCellIndex) {
I18n::Message titles[k_regressionCellIndex] = {I18n::Message::Mean, I18n::Message::Sum, I18n::Message::SquareSum, I18n::Message::StandardDeviation, I18n::Message::Deviation, I18n::Message::NumberOfDots, I18n::Message::Covariance, I18n::Message::Sxy, I18n::Message::Regression};
myCell->setMessage(titles[j-1]);
return;
}
if (shouldDisplayRAndR2 && j == numberRows - 2) {
myCell->setMessage(I18n::Message::R);
return;
}
I18n::Message titles[5] = {I18n::Message::A, I18n::Message::B, I18n::Message::C, I18n::Message::D, I18n::Message::E};
myCell->setMessage(titles[j - k_regressionCellIndex - 1]);
return;
}
int seriesNumber = m_store->indexOfKthNonEmptySeries(i - 1);
assert(i >= 0 && seriesNumber < DoublePairStore::k_numberOfSeries);
// Coordinate and series title
if (j == 0 && i > 0) {
ColumnTitleCell * myCell = (ColumnTitleCell *)cell;
char buffer[] = {'X', static_cast<char>('1' + seriesNumber), 0};
myCell->setFirstText(buffer);
buffer[0] = 'Y';
myCell->setSecondText(buffer);
myCell->setColor(Palette::DataColor[seriesNumber]);
return;
}
// Calculation cell
if (i > 0 && j > 0 && j <= k_totalNumberOfDoubleBufferRows) {
ArgCalculPointer calculationMethods[k_totalNumberOfDoubleBufferRows] = {&Store::meanOfColumn, &Store::sumOfColumn, &Store::squaredValueSumOfColumn, &Store::standardDeviationOfColumn, &Store::varianceOfColumn};
double calculation1 = (m_store->*calculationMethods[j-1])(seriesNumber, 0);
double calculation2 = (m_store->*calculationMethods[j-1])(seriesNumber, 1);
EvenOddDoubleBufferTextCellWithSeparator * myCell = (EvenOddDoubleBufferTextCellWithSeparator *)cell;
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
PoincareHelpers::ConvertFloatToText<double>(calculation1, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
myCell->setFirstText(buffer);
PoincareHelpers::ConvertFloatToText<double>(calculation2, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
myCell->setSecondText(buffer);
return;
}
SeparatorEvenOddBufferTextCell * bufferCell = (SeparatorEvenOddBufferTextCell *)cell;
if (i > 0 && j == k_regressionCellIndex) {
Model * model = m_store->modelForSeries(seriesNumber);
const char * formula = I18n::translate(model->formulaMessage());
bufferCell->setText(formula);
return;
}
if (i > 0 && j > k_totalNumberOfDoubleBufferRows && j < k_regressionCellIndex) {
assert(j != k_regressionCellIndex);
CalculPointer calculationMethods[] = {&Store::doubleCastedNumberOfPairsOfSeries, &Store::covariance, &Store::columnProductSum};
double calculation = (m_store->*calculationMethods[j-k_totalNumberOfDoubleBufferRows-1])(seriesNumber);
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
PoincareHelpers::ConvertFloatToText<double>(calculation, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
bufferCell->setText(buffer);
return;
}
if (i > 0 && j > k_totalNumberOfDoubleBufferRows) {
assert(j > k_regressionCellIndex);
int maxNumberCoefficients = maxNumberOfCoefficients();
Model::Type modelType = m_store->seriesRegressionType(seriesNumber);
// Put dashes if regression is not defined
Poincare::Context * globContext = const_cast<AppsContainer *>(static_cast<const AppsContainer *>(app()->container()))->globalContext();
double * coefficients = m_store->coefficientsForSeries(seriesNumber, globContext);
bool coefficientsAreDefined = true;
int numberOfCoefs = m_store->modelForSeries(seriesNumber)->numberOfCoefficients();
for (int i = 0; i < numberOfCoefs; i++) {
if (std::isnan(coefficients[i])) {
coefficientsAreDefined = false;
break;
}
}
if (!coefficientsAreDefined) {
bufferCell->setText(I18n::translate(I18n::Message::Dash));
return;
}
if (j > k_regressionCellIndex + maxNumberCoefficients) {
// Fill r and r2 if needed
if (modelType == Model::Type::Linear) {
CalculPointer calculationMethods[2] = {&Store::correlationCoefficient, &Store::squaredCorrelationCoefficient};
double calculation = (m_store->*calculationMethods[j - k_regressionCellIndex - maxNumberCoefficients - 1])(seriesNumber);
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
PoincareHelpers::ConvertFloatToText<double>(calculation, buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
bufferCell->setText(buffer);
return;
} else {
bufferCell->setText(I18n::translate(I18n::Message::Dash));
return;
}
} else {
// Fill the current coefficient if needed
int currentNumberOfCoefs = m_store->modelForSeries(seriesNumber)->numberOfCoefficients();
if (j > k_regressionCellIndex + currentNumberOfCoefs) {
bufferCell->setText(I18n::translate(I18n::Message::Dash));
return;
} else {
char buffer[PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits)];
PoincareHelpers::ConvertFloatToText<double>(coefficients[j - k_regressionCellIndex - 1], buffer, PrintFloat::bufferSizeForFloatsWithPrecision(Constant::LargeNumberOfSignificantDigits), Constant::LargeNumberOfSignificantDigits);
bufferCell->setText(buffer);
return;
}
}
}
}
KDCoordinate CalculationController::columnWidth(int i) {
if (i == 0) {
return k_smallCalculationCellWidth;
}
Model::Type currentType = m_store->seriesRegressionType(m_store->indexOfKthNonEmptySeries(i-1));
if (currentType == Model::Type::Quartic) {
return k_quarticCalculationCellWidth;
}
if (currentType == Model::Type::Cubic) {
return k_cubicCalculationCellWidth;
}
return k_smallCalculationCellWidth;
}
KDCoordinate CalculationController::rowHeight(int j) {
return k_cellHeight;
}
KDCoordinate CalculationController::cumulatedHeightFromIndex(int j) {
return j*rowHeight(0);
}
int CalculationController::indexFromCumulatedHeight(KDCoordinate offsetY) {
return (offsetY-1) / rowHeight(0);
}
HighlightCell * CalculationController::reusableCell(int index, int type) {
if (type == k_standardCalculationTitleCellType) {
assert(index >= 0 && index < k_maxNumberOfDisplayableRows);
assert(m_titleCells[index] != nullptr);
return m_titleCells[index];
}
if (type == k_r2CellType) {
assert(index == 0);
return m_r2TitleCell;
}
if (type == k_columnTitleCellType) {
assert(index >= 0 && index < Store::k_numberOfSeries);
return m_columnTitleCells[index];
}
if (type == k_doubleBufferCalculationCellType) {
assert(index >= 0 && index < k_numberOfDoubleCalculationCells);
assert(m_doubleCalculationCells[index] != nullptr);
return m_doubleCalculationCells[index];
}
if (type == k_hideableCellType) {
return m_hideableCell;
}
assert(index >= 0 && index < k_numberOfCalculationCells);
assert(m_calculationCells[index] != nullptr);
return m_calculationCells[index];
}
int CalculationController::reusableCellCount(int type) {
if (type == k_standardCalculationTitleCellType) {
return k_maxNumberOfDisplayableRows;
}
if (type == k_r2CellType) {
return 1;
}
if (type == k_columnTitleCellType) {
return Store::k_numberOfSeries;
}
if (type == k_doubleBufferCalculationCellType) {
return k_numberOfDoubleCalculationCells;
}
if (type == k_hideableCellType) {
return 1;
}
assert(type == k_standardCalculationCellType);
return k_numberOfCalculationCells;
}
int CalculationController::typeAtLocation(int i, int j) {
if (i == 0 && j == 0) {
return k_hideableCellType;
}
bool shouldDisplayRAndR2 = hasLinearRegression();
int numberRows = numberOfRows();
if (shouldDisplayRAndR2 && i == 0 && j == numberRows-1) {
return k_r2CellType;
}
if (i == 0) {
return k_standardCalculationTitleCellType;
}
if (j == 0) {
return k_columnTitleCellType;
}
if (j > 0 && j <= k_totalNumberOfDoubleBufferRows) {
return k_doubleBufferCalculationCellType;
}
return k_standardCalculationCellType;
}
Responder * CalculationController::tabController() const {
return (parentResponder()->parentResponder()->parentResponder());
}
View * CalculationController::loadView() {
SelectableTableView * tableView = new SelectableTableView(this, this, this, this);
tableView->setVerticalCellOverlap(0);
tableView->setBackgroundColor(Palette::WallScreenDark);
tableView->setMargins(k_margin, k_scrollBarMargin, k_scrollBarMargin, k_margin);
m_r2TitleCell = new EvenOddExpressionCell(1.0f, 0.5f);
m_r2TitleCell->setRightMargin(k_r2CellMargin);
for (int i = 0; i < Store::k_numberOfSeries; i++) {
m_columnTitleCells[i] = new ColumnTitleCell(tableView);
}
for (int i = 0; i < k_maxNumberOfDisplayableRows; i++) {
m_titleCells[i] = new MarginEvenOddMessageTextCell(KDText::FontSize::Small);
}
for (int i = 0; i < k_numberOfDoubleCalculationCells; i++) {
m_doubleCalculationCells[i] = new EvenOddDoubleBufferTextCellWithSeparator();
m_doubleCalculationCells[i]->setTextColor(Palette::GreyDark);
m_doubleCalculationCells[i]->setParentResponder(tableView);
}
for (int i = 0; i < k_numberOfCalculationCells;i++) {
m_calculationCells[i] = new SeparatorEvenOddBufferTextCell(KDText::FontSize::Small);
m_calculationCells[i]->setTextColor(Palette::GreyDark);
}
m_hideableCell = new HideableEvenOddCell();
m_hideableCell->setHide(true);
return tableView;
}
void CalculationController::unloadView(View * view) {
delete m_r2TitleCell;
m_r2TitleCell = nullptr;
for (int i = 0; i < Store::k_numberOfSeries; i++) {
delete m_columnTitleCells[i];
m_columnTitleCells[i] = nullptr;
}
for (int i = 0; i < k_numberOfDoubleCalculationCells; i++) {
delete m_doubleCalculationCells[i];
m_doubleCalculationCells[i] = nullptr;
}
for (int i = 0; i < k_numberOfCalculationCells;i++) {
delete m_calculationCells[i];
m_calculationCells[i] = nullptr;
}
for (int i = 0; i < k_maxNumberOfDisplayableRows; i++) {
delete m_titleCells[i];
m_titleCells[i] = nullptr;
}
delete m_hideableCell;
m_hideableCell = nullptr;
TabTableController::unloadView(view);
}
bool CalculationController::hasLinearRegression() const {
int numberOfDefinedSeries = m_store->numberOfNonEmptySeries();
for (int i = 0; i < numberOfDefinedSeries; i++) {
if (m_store->seriesRegressionType(m_store->indexOfKthNonEmptySeries(i)) == Model::Type::Linear) {
return true;
}
}
return false;
}
int CalculationController::maxNumberOfCoefficients() const {
int maxNumberCoefficients = 0;
int numberOfDefinedSeries = m_store->numberOfNonEmptySeries();
for (int i = 0; i < numberOfDefinedSeries; i++) {
int currentNumberOfCoefs = m_store->modelForSeries(m_store->indexOfKthNonEmptySeries(i))->numberOfCoefficients();
maxNumberCoefficients = max(maxNumberCoefficients, currentNumberOfCoefs);
}
return maxNumberCoefficients;
}
}
|