Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) TestSimpleLinearRegression

 

TestSimpleLinearRegression is a tool to test the SimpleLinearRegression class.

 

 

 

 

 

Downloads

 

Technical facts

 

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegressionConsole.pro

 

include(../../ConsoleApplication.pri)
include(../../Libraries/Apfloat.pri)
include(../../Libraries/Boost.pri)
include(../../Libraries/GeneralConsole.pri)

include(../../Classes/CppApproximator/CppApproximator.pri)
include(../../Classes/CppCanvas/CppCanvas.pri)
include(../../Classes/CppContainer/CppContainer.pri)
include(../../Classes/CppDotMatrix/CppDotMatrix.pri)
include(../../Classes/CppDrawCanvas/CppDrawCanvas.pri)
include(../../Classes/CppGeometry/CppGeometry.pri)
include(../../Classes/CppPlane/CppPlane.pri)
include(../../Classes/CppRibiRegex/CppRibiRegex.pri)
include(../../Classes/CppSimpleLinearRegression/CppSimpleLinearRegression.pri)
include(../../Classes/CppXml/CppXml.pri)

include(ToolTestSimpleLinearRegressionConsole.pri)

SOURCES += main.cpp

 

 

 

 

 

Qt project file: ./ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegressionDesktop.pro

 

include(../../DesktopApplicationNoWeffcpp.pri)
include(../../Libraries/Boost.pri)
include(../../Libraries/GeneralConsole.pri)
include(../../Libraries/GeneralDesktop.pri)
include(../../Libraries/Qwt.pri)

include(../../Classes/CppApproximator/CppApproximator.pri)
include(../../Classes/CppSimpleLinearRegression/CppSimpleLinearRegression.pri)

include(ToolTestSimpleLinearRegressionDesktop.pri)

SOURCES += qtmain.cpp

 

 

 

 

 

./ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegressionConsole.pri

 

INCLUDEPATH += \
    ../../Tools/ToolTestSimpleLinearRegression

SOURCES += \
    ../../Tools/ToolTestSimpleLinearRegression/testsimplelinearregressionmenudialog.cpp

HEADERS += \
    ../../Tools/ToolTestSimpleLinearRegression/testsimplelinearregressionmenudialog.h

OTHER_FILES += \
    ../../Tools/ToolTestSimpleLinearRegression/Licence.txt

RESOURCES += \
    ../../Tools/ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegression.qrc

 

 

 

 

 

./ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegressionDesktop.pri

 

include(../../Tools/ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegressionConsole.pri)

HEADERS += \
    ../../Tools/ToolTestSimpleLinearRegression/qttestsimplelinearregressionmaindialog.h \
    ../../Tools/ToolTestSimpleLinearRegression/qttestsimplelinearregressionmenudialog.h

SOURCES += \
    ../../Tools/ToolTestSimpleLinearRegression/qttestsimplelinearregressionmaindialog.cpp \
    ../../Tools/ToolTestSimpleLinearRegression/qttestsimplelinearregressionmenudialog.cpp

FORMS += \
    ../../Tools/ToolTestSimpleLinearRegression/qttestsimplelinearregressionmaindialog.ui \
    ../../Tools/ToolTestSimpleLinearRegression/qttestsimplelinearregressionmenudialog.ui

 

 

 

 

 

./ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegressionWebsite.pri

 

include(../../Tools/ToolTestSimpleLinearRegression/ToolTestSimpleLinearRegressionConsole.pri)
SOURCES +=
HEADERS +=

 

 

 

 

 

./ToolTestSimpleLinearRegression/main.cpp

 

#include <algorithm>
#include <cassert>
#include <iostream>
#include <cmath>

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <boost/units/systems/si.hpp>
#include "simplelinearregression.h"
#include "drawcanvas.h"
#pragma GCC diagnostic pop

int main()
{
  //Let Boost.Units check for compiling
  {
    typedef boost::units::quantity<boost::units::si::time> Time;
    typedef boost::units::quantity<boost::units::si::length> Distance;
    typedef boost::units::quantity<boost::units::si::velocity> Velocity;
    const std::vector<Time> xs
      =
      {
        Time(1.0 * boost::units::si::second),
        Time(2.0 * boost::units::si::second),
        Time(3.0 * boost::units::si::second)
      };
    const std::vector<Distance> ys
      =
      {
        Distance(2.0 * boost::units::si::meter),
        Distance(3.0 * boost::units::si::meter),
        Distance(4.0 * boost::units::si::meter)
      };

    const Velocity expected_slope(1.0 * boost::units::si::meter / boost::units::si::second);
    const Distance expected_offset(1.0 * boost::units::si::meter);

    const std::pair<Velocity,Distance> p
      = ribi::SimpleLinearRegression::CalculateBestFit(xs,ys);
    const Velocity slope(p.first);
    const Distance offset(p.second);
    assert(std::abs( Velocity(slope - expected_slope).value() )
      < Velocity(0.0001 * boost::units::si::meter / boost::units::si::second).value() );
    assert(std::abs( Distance(offset - expected_offset).value() )
      < Distance(0.0001 * boost::units::si::meter).value() );

  }
  for (int i=1; i!=5; ++i) //Human-based counting, following the Ansombe's Quartet indices
  {
    const std::vector<double> xs
      = ribi::SimpleLinearRegression::GetAnscombesQuartetX(i);
    const std::vector<double> ys
      = ribi::SimpleLinearRegression::GetAnscombesQuartetY(i);

    const std::pair<double,double> p
      = ribi::SimpleLinearRegression::CalculateBestFit(xs,ys);

    const double expected_regression_line_a = 0.500; //to 3 decimal places
    const double expected_regression_line_b = 3.00;  //to 2 decimal places

    const double e = 0.01;
    assert(expected_regression_line_a - e < p.first  && expected_regression_line_a + e > p.first);
    assert(expected_regression_line_b - e < p.second && expected_regression_line_b + e > p.second);

    //Plot the values
    const int max_x = 78;
    const int max_y = 20;
    ribi::DrawCanvas c(max_x,max_y,ribi::CanvasColorSystem::invert,ribi::CanvasCoordinatSystem::graph);
    c.DrawLine(0.0,0.0,max_x,0.0);
    c.DrawLine(0.0,0.0,0.0,max_y);
    assert(xs.size() == ys.size());
    const double ray = 1.0;
    const double max_x_co = ray + *std::max_element(xs.begin(),xs.end());
    const double stretch_x = static_cast<double>(max_x) / max_x_co;
    const double max_y_co = ray + *std::max_element(ys.begin(),ys.end());
    const double stretch_y = static_cast<double>(max_y) / max_y_co;

    const std::size_t n_values = xs.size();
    for (std::size_t i=0; i!=n_values; ++i)
    {
      //c.DrawDot(xs[i] * stretch_x,ys[i] * stretch_y);
      c.DrawCircle(xs[i] * stretch_x,ys[i] * stretch_y,ray);
    }
    c.DrawLine(
      stretch_x * 0.0,
      stretch_y * p.second,
      stretch_x * static_cast<double>(max_x),
      stretch_y * (p.second + static_cast<double>(max_x) * p.first)
    );
    std::cout << c << std::endl;
  }
}

/* Screen output

TRACE '"Starting SimpleLinearRegression::Test"' line 155 in fi
le '../ToolTestSimpleLinearRegression/tooltestsimplelinearregressionmaindialog.c
pp': 'Starting SimpleLinearRegression::Test'
TRACE '"Finished SimpleLinearRegression::Test successfully"' l
ine 200 in file '../ToolTestSimpleLinearRegression/tooltestsimplelinearregressio
nmaindialog.cpp': 'Finished SimpleLinearRegression::Test succe
ssfully'
.                                                            -o.
:                                                            s-+        `    `
:                                                            +o:       /hmooso
:                                             .               `  `-:+osmdm:`
:                                            :os`       .- .-:+os+/:.`  :-
:                                            :+s` `os:-+MMN+/-.`   .
:                             -/`        `    //:/hNNy//ho:       /oo
:                            `o/:       +hd/oso+:--o+`  `.        +:s
:                            `oo- `.:/osMhN-`                     `/.
:                       `oo.`-/ooso/:-` `+.
:                     `-smNdo/:.`  `/
:               .-/omdo//so`       s/+
:         .-/+so+/-o+s`            oo/
:  `.:/+so+:-.     -oo`             .
hoso+:-`            `
o`
:
:
:
s::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::.

`                                                  ``   `-    .     `.:/o+/-.`
-                                             -.  .++. `o/:  +o+-:+do:-`
-                                        .   :/o` .++. `oo+-/Mdd:.o/o   `
-                                       /+o  :+o`  -/-:/sy/-.-/`  //o  -++`
-                                  `:   /:o   ..-/+o+:-`           -   ::o`
-                                  o:/   /::/+o/:.`                     :-
-                             `.   oos:+o+/-.
-                            `sso/+os:-`
-                        `.-/sMds.`
-                   `-:/shy:.``-
-              .-/+o+:-`-+/.
-        `.:/+o/:.`     .++.
-   `-:+o+/-.       -`   ``
o/+o+:-`           :/o`
s.`                :+o`
-                   .`
-
-
-
o----------------------------------------------------------------------------`

.                                                                 -s/
:                                                                 +.o
:                                                                 .s:
:
:                                                                        `.-:/
:                                                                 `.-:+oho/:-.
:                                                          `.:o+os+/:-./+o`
:                                                  .-.:/dNy+/dyo       :/o`
:                                        .   -ys/+hMNo::y/:  s:+        :.
:                              .   -o-.-ymMoodyd:`-++-  +o.  `/
:                   `   `//` `syo:/MdN+/hoy  -o+   --
:                  -+o``/yhsoyMdy-.oo:   /`
:               `.-ydMy+odh:` .:    `
:        `.-/+oo+/::+-
: `.:/+oo+/:-.
do+/:.`
:
:
:
s::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::.

                                                                         .-``.
`                                                                     ``-/:-``
`                                                                ``.-..``..
`                                                           `..-..`
`                                                      `....``
`                              `                 ``....``
`                             //.           ``....`
`                            `hy/      `..-.``
`                            `dm+``..-.``
`                           `-MMy.``
`                      ``.-..-NN/
`                 `..-.``    `hy/
`           ``....``         `oo:
`      ``.-..`                `.
` `..-..`
:.``
`
`
`
-````````````````````````````````````````````````````````````````````````````

Press <RETURN> to close this window...

*/

 

 

 

 

 

./ToolTestSimpleLinearRegression/qtmain.cpp

 

//---------------------------------------------------------------------------
/*
ToolTestSimpleLinearRegression, tests the SimpleLinearRegression class
Copyright (C) 2013  Richel Bilderbeek

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.If not, see <http://www.gnu.org/licenses/>.
*/
//---------------------------------------------------------------------------
//From http://www.richelbilderbeek.nl/ToolTestSimpleLinearRegression.htm
//---------------------------------------------------------------------------
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <QApplication>

#include "qttestsimplelinearregressionmenudialog.h"
#include "trace.h"
#pragma GCC diagnostic pop

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);
  START_TRACE();
  ribi::QtToolTestSimpleLinearRegressionMenuDialog d;
  d.show();
  return a.exec();
}

 

 

 

 

 

./ToolTestSimpleLinearRegression/qttestsimplelinearregressionmaindialog.h

 

#ifndef QTTOOLTESTAPPROXIMATORMAINDIALOG_H
#define QTTOOLTESTAPPROXIMATORMAINDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include <boost/shared_ptr.hpp>
#include "approximator.h"
#include "qthideandshowdialog.h"
#pragma GCC diagnostic pop

struct QwtPlot;
struct QwtPlotCurve;

namespace Ui {
  class QtToolTestSimpleLinearRegressionMainDialog;
}

namespace ribi {

class QtToolTestSimpleLinearRegressionMainDialog : public QtHideAndShowDialog
{
  Q_OBJECT
  
public:
  explicit QtToolTestSimpleLinearRegressionMainDialog(QWidget *parent = 0) noexcept;
  QtToolTestSimpleLinearRegressionMainDialog(const QtToolTestSimpleLinearRegressionMainDialog&) = delete;
  QtToolTestSimpleLinearRegressionMainDialog& operator=(const QtToolTestSimpleLinearRegressionMainDialog&) = delete;
  ~QtToolTestSimpleLinearRegressionMainDialog() noexcept;
  
private slots:
  void on_button_clicked() noexcept;

private:
  Ui::QtToolTestSimpleLinearRegressionMainDialog *ui;

  //const boost::shared_ptr<QwtPlotCurve> m_curve_approximation;
  //const boost::shared_ptr<QwtPlotCurve> m_curve_values;
  QwtPlotCurve * const m_curve_approximation;
  QwtPlotCurve * const m_curve_values;

  ///Contains the plot
  const boost::shared_ptr<QwtPlot> m_plot;

  std::vector<double> m_xs;
  std::vector<double> m_ys;

  void Plot() noexcept;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // QTTOOLTESTAPPROXIMATORMAINDIALOG_H

 

 

 

 

 

./ToolTestSimpleLinearRegression/qttestsimplelinearregressionmaindialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qttestsimplelinearregressionmaindialog.h"

#include <cassert>
#include <sstream>

#include <qwt_legend.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_zoomer.h>

#if QWT_VERSION >= 0x060100 || !WIN32
#include "qwt_point_data.h"
#endif

#include "simplelinearregression.h"
#include "testtimer.h"
#include "trace.h"
#include "ui_qttestsimplelinearregressionmaindialog.h"

#pragma GCC diagnostic pop

ribi::QtToolTestSimpleLinearRegressionMainDialog::QtToolTestSimpleLinearRegressionMainDialog(QWidget *parent) noexcept
  : QtHideAndShowDialog(parent),
    ui(new Ui::QtToolTestSimpleLinearRegressionMainDialog),
    m_curve_approximation(new QwtPlotCurve),
    m_curve_values(new QwtPlotCurve),
    m_plot(new QwtPlot),
    m_xs{},
    m_ys{}
{
  #ifndef NDEBUG
  Test();
  #endif
  ui->setupUi(this);

  //Set up the plot
  assert(m_plot);
  m_plot->setTitle("SimpleLinearRegression");
  m_plot->setAxisTitle(QwtPlot::xBottom,"X");
  m_plot->setAxisTitle(QwtPlot::yLeft,"Y");
  #ifdef _WIN32
  m_plot->setCanvasBackground(QBrush(QColor(255,255,255)));
  #else
  m_plot->setCanvasBackground(QColor(255,255,255));
  #endif

  //Create plots
  assert(m_curve_values);
  m_curve_values->setTitle("Points");
  m_curve_values->attach(m_plot.get());
  m_curve_values->setStyle(QwtPlotCurve::Dots);
  m_curve_values->setPen(QPen(QColor(255,0,0),5));

  assert(m_curve_approximation);
  m_curve_approximation->setTitle("Trendline");
  m_curve_approximation->attach(m_plot.get());
  m_curve_approximation->setStyle(QwtPlotCurve::Dots);
  m_curve_approximation->setPen(QPen(QColor(0,0,255),3));

  //Add grid
  {
    QwtPlotGrid * const grid = new QwtPlotGrid;
    grid->setPen(QPen(QColor(128,128,128)));
    grid->attach(m_plot.get());
  }
  //Add zoomer
  {
    new QwtPlotZoomer(m_plot->canvas());
  }
  {
    // legend
    QwtLegend * const legend = new QwtLegend;
    legend->setFrameStyle(QFrame::Box|QFrame::Sunken);
    m_plot->insertLegend(legend, QwtPlot::RightLegend);
  }
  assert(ui->verticalLayout->layout());
  ui->verticalLayout->layout()->addWidget(m_plot.get());

  m_plot->setAxisScale(
    QwtPlot::xBottom,
    static_cast<double>(ui->box_int_x->minimum()),
    static_cast<double>(ui->box_int_x->maximum())
  );
  m_plot->setAxisScale(
    QwtPlot::yLeft,
    static_cast<double>(ui->box_double_y->minimum()),
    static_cast<double>(ui->box_double_y->maximum())
  );

  //Add some nice testing values
  ui->box_int_x->setValue(ui->box_int_x->minimum() / 2);
  ui->box_double_y->setValue(ui->box_double_y->maximum() / 2.0);
  on_button_clicked();

  ui->box_int_x->setValue(ui->box_int_x->minimum() / 4);
  ui->box_double_y->setValue(ui->box_double_y->minimum() / 2.0);
  on_button_clicked();

  ui->box_int_x->setValue(ui->box_int_x->maximum() / 4);
  ui->box_double_y->setValue(ui->box_double_y->maximum() / 2.0);
  on_button_clicked();

  ui->box_int_x->setValue(ui->box_int_x->maximum() / 2);
  ui->box_double_y->setValue(ui->box_double_y->minimum() / 2.0);
  on_button_clicked();

  ui->box_int_x->setValue(0);
  ui->box_double_y->setValue(0.0);

  TRACE_FUNC();
}

ribi::QtToolTestSimpleLinearRegressionMainDialog::~QtToolTestSimpleLinearRegressionMainDialog() noexcept
{
  delete ui;
}

void ribi::QtToolTestSimpleLinearRegressionMainDialog::on_button_clicked() noexcept
{
  const int x = ui->box_int_x->value();
  const double y = ui->box_double_y->value();
  m_xs.push_back(x);
  m_ys.push_back(y);
  Plot();
}

void ribi::QtToolTestSimpleLinearRegressionMainDialog::Plot() noexcept
{
  //Plot values
  {
    #if QWT_VERSION >= 0x060100 || !WIN32
    m_curve_values->setData(new QwtPointArrayData(&m_xs[0],&m_ys[0],m_xs.size()));
    #else
    m_curve_values->setData(&m_xs[0],&m_y[0],m_xs.size());
    #endif
  }

  const double min_x = static_cast<double>(ui->box_int_x->minimum());
  const double max_x = static_cast<double>(ui->box_int_x->maximum());

  //Plot approximation
  {
    const std::pair<double,double> p
      = SimpleLinearRegression::CalculateBestFit(m_xs,m_ys);
    const double slope = p.first;
    const double intercept = p.second;
    {
      std::stringstream s;
      s << "y = " << slope << ".x" << (intercept >= 0.0 ? "+":"") << intercept;
      this->setWindowTitle(s.str().c_str());
    }
    std::vector<double> xs;
    std::vector<double> ys;
    for (double t=min_x; t < max_x; t+=0.5)
    {
      const double x = static_cast<double>(t);
      const double y = intercept + (slope * x);
      ys.push_back(y);
      xs.push_back(x);
    }
    assert(m_curve_approximation);
    #if QWT_VERSION >= 0x060000
    m_curve_approximation->setData(new QwtPointArrayData(&xs[0],&ys[0],xs.size()));
    #else
    m_curve_approximation->setData(&xs[0],&y[0],xs.size());
    #endif
  }
  assert(m_plot);
  m_plot->replot();
}

#ifndef NDEBUG
void ribi::QtToolTestSimpleLinearRegressionMainDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif

 

 

 

 

 

./ToolTestSimpleLinearRegression/qttestsimplelinearregressionmenudialog.h

 

#ifndef QTTOOLTESTSIMPLELINEARREGRESSIONMENUDIALOG_H
#define QTTOOLTESTSIMPLELINEARREGRESSIONMENUDIALOG_H

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qthideandshowdialog.h"
#pragma GCC diagnostic pop

namespace Ui {
  class QtToolTestSimpleLinearRegressionMenuDialog;
}

namespace ribi {

class QtToolTestSimpleLinearRegressionMenuDialog : public QtHideAndShowDialog
{
  Q_OBJECT

public:
  explicit QtToolTestSimpleLinearRegressionMenuDialog(QWidget *parent = 0) noexcept;
  QtToolTestSimpleLinearRegressionMenuDialog(const QtToolTestSimpleLinearRegressionMenuDialog&) = delete;
  QtToolTestSimpleLinearRegressionMenuDialog& operator=(const QtToolTestSimpleLinearRegressionMenuDialog&) = delete;
  ~QtToolTestSimpleLinearRegressionMenuDialog() noexcept;

protected:
  void keyPressEvent(QKeyEvent * event) noexcept;

private:
  Ui::QtToolTestSimpleLinearRegressionMenuDialog *ui;

private slots:
  void on_button_about_clicked() noexcept;
  void on_button_quit_clicked() noexcept;
  void on_button_start_clicked() noexcept;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // QTTOOLTESTSIMPLELINEARREGRESSIONMENUDIALOG_H

 

 

 

 

 

./ToolTestSimpleLinearRegression/qttestsimplelinearregressionmenudialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "qttestsimplelinearregressionmenudialog.h"

#include <boost/units/systems/si.hpp>

#include <QDesktopWidget>
#include <QKeyEvent>

#include "simplelinearregression.h"
#include "testsimplelinearregressionmenudialog.h"
#include "qtaboutdialog.h"
#include "qttestsimplelinearregressionmaindialog.h"
#include "qthideandshowdialog.h"
#include "testtimer.h"
#include "trace.h"
#include "ui_qttestsimplelinearregressionmenudialog.h"

#pragma GCC diagnostic pop

ribi::QtToolTestSimpleLinearRegressionMenuDialog::QtToolTestSimpleLinearRegressionMenuDialog(
  QWidget *parent) noexcept
  : QtHideAndShowDialog(parent),
    ui(new Ui::QtToolTestSimpleLinearRegressionMenuDialog)
{
  #ifndef NDEBUG
  Test();
  #endif
  ui->setupUi(this);
}

ribi::QtToolTestSimpleLinearRegressionMenuDialog::~QtToolTestSimpleLinearRegressionMenuDialog() noexcept
{
  delete ui;
}

void ribi::QtToolTestSimpleLinearRegressionMenuDialog::keyPressEvent(
  QKeyEvent * event) noexcept
{
  if (event->key() == Qt::Key_Escape) { close(); return; }
}

void ribi::QtToolTestSimpleLinearRegressionMenuDialog::on_button_about_clicked() noexcept
{
  About a = ToolTestSimpleLinearRegressionMenuDialog().GetAbout();
  a.AddLibrary("QtHideAndShowDialog version: " + QtHideAndShowDialog::GetVersion());
  QtAboutDialog d(a);
  d.setWindowIcon(this->windowIcon());
  d.setStyleSheet(this->styleSheet());
  this->ShowChild(&d);
}

void ribi::QtToolTestSimpleLinearRegressionMenuDialog::on_button_quit_clicked() noexcept
{
  close();
}

void ribi::QtToolTestSimpleLinearRegressionMenuDialog::on_button_start_clicked() noexcept
{
  QtToolTestSimpleLinearRegressionMainDialog d;
  d.setWindowIcon(this->windowIcon());
  d.setStyleSheet(this->styleSheet());
  ShowChild(&d);
}

#ifndef NDEBUG
void ribi::QtToolTestSimpleLinearRegressionMenuDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  QtToolTestSimpleLinearRegressionMainDialog();
  const TestTimer test_timer(__func__,__FILE__,1.0);
  //Let Boost.Units check for compiling
  {
    typedef boost::units::quantity<boost::units::si::time> Time;
    typedef boost::units::quantity<boost::units::si::length> Distance;
    typedef boost::units::quantity<boost::units::si::velocity> Velocity;
    const std::vector<Time> xs
      =
      {
        Time(1.0 * boost::units::si::second),
        Time(2.0 * boost::units::si::second),
        Time(3.0 * boost::units::si::second)
      };
    const std::vector<Distance> ys
      =
      {
        Distance(2.0 * boost::units::si::meter),
        Distance(3.0 * boost::units::si::meter),
        Distance(4.0 * boost::units::si::meter)
      };

    const Velocity expected_slope(1.0 * boost::units::si::meter / boost::units::si::second);
    const Distance expected_offset(1.0 * boost::units::si::meter);

    const std::pair<Velocity,Distance> p
      = SimpleLinearRegression::CalculateBestFit(xs,ys);
    const Velocity slope(p.first);
    const Distance offset(p.second);
    assert(std::abs( Velocity(slope - expected_slope).value() )
      < Velocity(0.0001 * boost::units::si::meter / boost::units::si::second).value() );
    assert(std::abs( Distance(offset - expected_offset).value() )
      < Distance(0.0001 * boost::units::si::meter).value() );

  }
}
#endif

 

 

 

 

 

./ToolTestSimpleLinearRegression/testsimplelinearregressionmenudialog.h

 

#ifndef TOOLTESTSIMPLELINEARREGRESSIONMENUDIALOG_H
#define TOOLTESTSIMPLELINEARREGRESSIONMENUDIALOG_H

#include "menudialog.h"

namespace ribi {

struct ToolTestSimpleLinearRegressionMenuDialog final : public MenuDialog
{
  About GetAbout() const noexcept override;
  Help GetHelp() const noexcept override;
  boost::shared_ptr<const Program> GetProgram() const noexcept override;
  std::string GetVersion() const noexcept override;
  std::vector<std::string> GetVersionHistory() const noexcept override;

  private:
  int ExecuteSpecific(const std::vector<std::string>& argv) noexcept override;

  #ifndef NDEBUG
  static void Test() noexcept;
  #endif
};

} //~namespace ribi

#endif // TOOLTESTSIMPLELINEARREGRESSIONMENUDIALOG_H

 

 

 

 

 

./ToolTestSimpleLinearRegression/testsimplelinearregressionmenudialog.cpp

 

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Weffc++"
#include "testsimplelinearregressionmenudialog.h"

#include <cassert>
#include <iostream>

#include "richelbilderbeekprogram.h"
#include "testtimer.h"
#include "simplelinearregression.h"
#include "trace.h"
#pragma GCC diagnostic pop

int ribi::ToolTestSimpleLinearRegressionMenuDialog::ExecuteSpecific(const std::vector<std::string>& argv) noexcept
{
  #ifndef NDEBUG
  Test();
  #endif
  const int argc = static_cast<int>(argv.size());
  if (argc == 1)
  {
    std::cout << GetHelp() << '\n';
    return 1;
  }
  assert(!"TODO");
  return 1;
}

ribi::About ribi::ToolTestSimpleLinearRegressionMenuDialog::GetAbout() const noexcept
{
  About a(
    "Richel Bilderbeek",
    "ToolTestSimpleLinearRegression",
    "tests a simple linear regression",
    "the 28th of August 2013",
    "2013-2015",
    "http://www.richelbilderbeek.nl/ToolTestSimpleLinearRegression.htm",
    GetVersion(),
    GetVersionHistory());
  a.AddLibrary("Trace version: " + Trace::GetVersion());
  a.AddLibrary("SimpleLinearRegression version: " + SimpleLinearRegression::GetVersion());
  return a;
}

ribi::Help ribi::ToolTestSimpleLinearRegressionMenuDialog::GetHelp() const noexcept
{
  return Help(
    this->GetAbout().GetFileTitle(),
    this->GetAbout().GetFileDescription(),
    {

    },
    {

    }
  );
}

boost::shared_ptr<const ribi::Program> ribi::ToolTestSimpleLinearRegressionMenuDialog::GetProgram() const noexcept
{
  const boost::shared_ptr<const Program> p {
    new ProgramTestSimpleLinearRegression
  };
  assert(p);
  return p;
}

std::string ribi::ToolTestSimpleLinearRegressionMenuDialog::GetVersion() const noexcept
{
  return "1.2";
}

std::vector<std::string> ribi::ToolTestSimpleLinearRegressionMenuDialog::GetVersionHistory() const noexcept
{
  return {
    "2013-08-27: version 1.0: initial version",
    "2013-08-28: version 1.1: isolated and templated SimpleLinearRegression class",
    "2013-11-05: version 1.2: conformized for ProjectRichelBilderbeekConsole"
  };
}

#ifndef NDEBUG
void ribi::ToolTestSimpleLinearRegressionMenuDialog::Test() noexcept
{
  {
    static bool is_tested{false};
    if (is_tested) return;
    is_tested = true;
  }
  const TestTimer test_timer(__func__,__FILE__,1.0);
}
#endif

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml