init
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
---
|
||||
BasedOnStyle: LLVM
|
||||
---
|
||||
Language: Cpp
|
||||
AllowShortFunctionsOnASingleLine: Empty
|
||||
DerivePointerAlignment: false
|
||||
PointerAlignment: Left
|
||||
ColumnLimit: 120
|
||||
TabWidth: 4
|
||||
IndentWidth: 2
|
||||
...
|
||||
@@ -0,0 +1,23 @@
|
||||
# debug: clangd --check=modules/iue-io/ccsv.h
|
||||
# debug: clangd --check=task1.hpp
|
||||
# debug: clangd --check=task1.test.cpp
|
||||
InlayHints:
|
||||
Enabled: No
|
||||
ParameterNames: No
|
||||
DeducedTypes: No
|
||||
---
|
||||
CompileFlags:
|
||||
Add:
|
||||
- -Wall
|
||||
- -Wno-unused-function
|
||||
- -Wno-unused-variable
|
||||
---
|
||||
If:
|
||||
PathMatch: [.*\\.c, .*\\.h]
|
||||
CompileFlags:
|
||||
Add: [-std=c11]
|
||||
---
|
||||
If:
|
||||
PathMatch: [.*\\.cpp, .*\\.hpp]
|
||||
CompileFlags:
|
||||
Add: [-std=c++20]
|
||||
@@ -0,0 +1,16 @@
|
||||
## source: https://docs.github.com/en/get-started/getting-started-with-git/configuring-git-to-handle-line-endings
|
||||
|
||||
# Set the default behavior, in case people don't have core.autocrlf set.
|
||||
* text=auto
|
||||
|
||||
# Explicitly declare text files you want to always be normalized and converted
|
||||
# to native line endings on checkout.
|
||||
*.h text
|
||||
*.hpp text
|
||||
*.c text
|
||||
*.cpp text
|
||||
*.py text
|
||||
*.ipynb text
|
||||
*.md text
|
||||
*.txt text
|
||||
*.csv text
|
||||
+364
@@ -0,0 +1,364 @@
|
||||
# custom
|
||||
|
||||
taskA
|
||||
taskB
|
||||
taskC
|
||||
taskAref
|
||||
taskBref
|
||||
taskCref
|
||||
build
|
||||
doc
|
||||
.cache
|
||||
.vscode
|
||||
.idea
|
||||
|
||||
# https://github.com/github/gitignore/blob/main/CMake.gitignore
|
||||
|
||||
CMakeLists.txt.user
|
||||
CMakeCache.txt
|
||||
CMakeFiles
|
||||
CMakeScripts
|
||||
Testing
|
||||
Makefile
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
compile_commands.json
|
||||
CTestTestfile.cmake
|
||||
_deps
|
||||
|
||||
# ttps://github.com/github/gitignore/blob/main/C.gitignore
|
||||
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Object files
|
||||
*.o
|
||||
*.ko
|
||||
*.obj
|
||||
*.elf
|
||||
|
||||
# Linker output
|
||||
*.ilk
|
||||
*.map
|
||||
*.exp
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Libraries
|
||||
*.lib
|
||||
*.a
|
||||
*.la
|
||||
*.lo
|
||||
|
||||
# Shared objects (inc. Windows DLLs)
|
||||
*.dll
|
||||
*.so
|
||||
*.so.*
|
||||
*.dylib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
*.i*86
|
||||
*.x86_64
|
||||
*.hex
|
||||
|
||||
# Debug files
|
||||
*.dSYM/
|
||||
*.su
|
||||
*.idb
|
||||
*.pdb
|
||||
|
||||
# Kernel Module Compile Results
|
||||
*.mod*
|
||||
*.cmd
|
||||
.tmp_versions/
|
||||
modules.order
|
||||
Module.symvers
|
||||
Mkfile.old
|
||||
dkms.conf
|
||||
|
||||
# https://github.com/github/gitignore/blob/main/C%2B%2B.gitignore
|
||||
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
|
||||
# source: https://github.com/github/gitignore/blob/main/Python.gitignore
|
||||
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
cover/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
.pybuilder/
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
# For a library or package, you might want to ignore these files since the code is
|
||||
# intended to run in multiple environments; otherwise, check them in:
|
||||
# .python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# poetry
|
||||
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
||||
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
||||
# commonly ignored for libraries.
|
||||
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
||||
#poetry.lock
|
||||
|
||||
# pdm
|
||||
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
||||
#pdm.lock
|
||||
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
||||
# in version control.
|
||||
# https://pdm.fming.dev/#use-with-ide
|
||||
.pdm.toml
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
|
||||
# pytype static type analyzer
|
||||
.pytype/
|
||||
|
||||
# Cython debug symbols
|
||||
cython_debug/
|
||||
|
||||
# jetbrain IDEs: https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
||||
|
||||
# User-specific stuff
|
||||
.idea/**/workspace.xml
|
||||
.idea/**/tasks.xml
|
||||
.idea/**/usage.statistics.xml
|
||||
.idea/**/dictionaries
|
||||
.idea/**/shelf
|
||||
|
||||
# AWS User-specific
|
||||
.idea/**/aws.xml
|
||||
|
||||
# Generated files
|
||||
.idea/**/contentModel.xml
|
||||
|
||||
# Sensitive or high-churn files
|
||||
.idea/**/dataSources/
|
||||
.idea/**/dataSources.ids
|
||||
.idea/**/dataSources.local.xml
|
||||
.idea/**/sqlDataSources.xml
|
||||
.idea/**/dynamic.xml
|
||||
.idea/**/uiDesigner.xml
|
||||
.idea/**/dbnavigator.xml
|
||||
|
||||
# Gradle
|
||||
.idea/**/gradle.xml
|
||||
.idea/**/libraries
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/artifacts
|
||||
# .idea/compiler.xml
|
||||
# .idea/jarRepositories.xml
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-*/
|
||||
|
||||
# Mongo Explorer plugin
|
||||
.idea/**/mongoSettings.xml
|
||||
|
||||
# File-based project format
|
||||
*.iws
|
||||
|
||||
# IntelliJ
|
||||
out/
|
||||
|
||||
# mpeltonen/sbt-idea plugin
|
||||
.idea_modules/
|
||||
|
||||
# JIRA plugin
|
||||
atlassian-ide-plugin.xml
|
||||
|
||||
# Cursive Clojure plugin
|
||||
.idea/replstate.xml
|
||||
|
||||
# SonarLint plugin
|
||||
.idea/sonarlint/
|
||||
|
||||
# Crashlytics plugin (for Android Studio and IntelliJ)
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Editor-based Rest Client
|
||||
.idea/httpRequests
|
||||
|
||||
# Android studio 3.1+ serialized cache file
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
# VSCODE source: https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
||||
@@ -0,0 +1,8 @@
|
||||
[submodule "eigen"]
|
||||
path = eigen
|
||||
url = https://sgit.iue.tuwien.ac.at/360050/eigen
|
||||
branch = main
|
||||
[submodule "modules"]
|
||||
path = modules
|
||||
url = https://sgit.iue.tuwien.ac.at/360050/modules
|
||||
branch = main
|
||||
@@ -0,0 +1,38 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
# define project metadata
|
||||
|
||||
project(lab6 LANGUAGES C CXX
|
||||
DESCRIPTION "lab6"
|
||||
HOMEPAGE_URL "https://sgit.iue.tuwien.ac.at/360050/lab6")
|
||||
|
||||
# setting required language standards
|
||||
|
||||
set(CMAKE_C_STANDARD 11)
|
||||
set(CMAKE_C_STANDARD_REQUIRED True)
|
||||
set(CMAKE_C_EXTENSIONS OFF)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
|
||||
# misc settings
|
||||
|
||||
# generate a compile_commands.json
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
# make all symbols visible on windows (which is default on unix)
|
||||
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
|
||||
|
||||
# find math library and link to all targets
|
||||
|
||||
find_library(MATH_LIBRARY m)
|
||||
link_libraries(${MATH_LIBRARY})
|
||||
|
||||
# global includes
|
||||
|
||||
include_directories(modules)
|
||||
include_directories(eigen)
|
||||
|
||||
# include own targets
|
||||
|
||||
add_executable(taskB taskB.cpp)
|
||||
@@ -0,0 +1,24 @@
|
||||
# Labor VI: Abgabe (45min)
|
||||
|
||||
**Dieser Teil wird über [TUWEL, Labor VI: Abgabe (45min)](https://tuwel.tuwien.ac.at/course/view.php?id=62042#coursecontentcollapse15)** abgewickelt.
|
||||
|
||||
# Labor VI: Praxisteil (90min)
|
||||
|
||||
- **Fragen Sie frühzeitig nach, falls Unklarheiten bestehen**.
|
||||
- Fragen Sie alles, was Ihnen im Rahmen der Lehrveranstaltung wichtig erscheint.
|
||||
- Es gibt keine Einschränkungen für die Zusammenarbeit zwischen Studierenden beim Bearbeiten der untenstehenden Aufgaben.
|
||||
- Sie haben das Labor erfolgreich absolviert, wenn Sie alle drei Teilaufgaben bei einem Betreuer **demonstriert** haben.
|
||||
- Melden Sie sich bei einem Betreuer, sobald Sie sich in der Lage sehen alle drei Aufgaben zu demonstrieren.
|
||||
|
||||
---
|
||||
|
||||
Im heutigen Labor sollen Sie die folgenden drei Aufgabengebiete bearbeiten.
|
||||
|
||||
### A: TISS-Feedback (optional, ~15min)
|
||||
|
||||
### B. Laufzeitmessung fuer eine Ausgleichungsrechnung (*polynomial fitting*)
|
||||
|
||||
---
|
||||
|
||||
Details zu den Aufgaben finden Sie in [`main.ipynb`](main.ipynb).
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
-Imodules
|
||||
-Ieigen
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 37 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
@@ -0,0 +1,94 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## A. TISS-Feedback (optional, ~15min)\n",
|
||||
"\n",
|
||||
"Diese Aufgabe ist optional; wir bitten Sie sich dennoch explizit Zeit zu nehmen, und uns Rückmeldungen zu geben.\n",
|
||||
"\n",
|
||||
"Bitte nutzen Sie auch die Freitext Felder, diese sind für uns besonders wertvoll.\n",
|
||||
"\n",
|
||||
"Hier ein paar Anregungen für Ihre Rückmeldungen via Freitext:\n",
|
||||
"\n",
|
||||
"- Bitte wenn es geht immer kurz Ihre Vorerfahrung erwähnen, z.B. mit \"Vorerfahrung: viel\" oder \"Vorerfahrung: wenig\"\n",
|
||||
"- Bitte beziehen Sie gerne auch die vorangehende LV (Python 360.049) in Ihre Überlegungen mit ein.\n",
|
||||
"- Bitte beziehen Sie gerne auch explizit auf einzelne Aspekte des diessemestrigen Modus ein (Hörsaal/Hausübungen/Labor Abgaben/Labor Praxisteil)\n",
|
||||
"\n",
|
||||
"TISS-Fragebogen: https://tiss.tuwien.ac.at/survey/surveyForm.xhtml?dswid=1580&dsrid=261&courseNumber=360050&semesterCode=2024S\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## B. Laufzeitmessung für eine Ausgleichungsrechnung (*polynomial fitting*)\n",
|
||||
"\n",
|
||||
"[taskB.py](taskB.py): gegebener Python-Quellcode zur Ausgleichungsrechnung mit Polynomen (ident zu [360.049/homework8/task3](https://sgit.iue.tuwien.ac.at/360049/homework8/src/branch/main/main.ipynb#user-content-Aufgabe-3:-Ausgleichungsrechnung-mit-Polynomen-(1-Punkt))).\n",
|
||||
"\n",
|
||||
"[taskB.cpp](taskB.cpp): gegebener C++-Quellcode mit Funktionalität analog zu [taskB.py](taskB.py).\n",
|
||||
"\n",
|
||||
"Beide Quellcodes berechnen die Koeffizienten eines Ausgleichploynoms für einen generisch erzeugten Datensatz von Funktionswertepaaren (+ Rauschen) im Intervall $[0,5]$ für die Funktion: \n",
|
||||
"\n",
|
||||
"$f(x) = 2(1 - \\exp^{-x})$\n",
|
||||
"\n",
|
||||
"**Aufgabe:**\n",
|
||||
"\n",
|
||||
"1. Inspizieren Sie den gegebenen Programmcode.\n",
|
||||
"2. Führen Sie den die Programme aus (die Anzahl an Datenpunkten ist im Quellcode mit 30000 festgelegt).\n",
|
||||
"\t```shell\n",
|
||||
"\tpython taskB.py \n",
|
||||
"\t./taskB \n",
|
||||
"\t```\n",
|
||||
"3. Gestalten Sie beide gegebenen Programme um, so dass die Anzahl an Datenpunkten als Argument in der Kommandozeile übergeben werden kann, z.B. so:\n",
|
||||
"\t```shell\n",
|
||||
"\tpython taskB.py 30000 \n",
|
||||
"\t./taskB 30000\n",
|
||||
"\t```\n",
|
||||
"4. Messen Sie die Laufzeit für die Ausgleichungsrechnung für mindestens 5 verschiedene Anzahlen an Datenpunkten,\n",
|
||||
"\t- für die Python-Implementierung (taskB.py),\n",
|
||||
"\t- für die C++-Implementierung mit Debug Einstellungen (`-O0`), und\n",
|
||||
"\t- für die C++-Implementierung it Release Einstellungen\t(`-O3`).\n",
|
||||
"5. Erstellen Sie einen Plot der Laufzeiten, dies könnte z.B. so aussehen:\n",
|
||||
"\n",
|
||||
"\t\n",
|
||||
"\t\n",
|
||||
"\t\t\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"**Demonstration:**\n",
|
||||
"\n",
|
||||
"- Welche Bibliothek kommt in der C++-Implementierung (statt numpy) zum Einsatz.\n",
|
||||
"- Warum sind die errechneten Koeffizienten nicht exakt gleich (Python vs C++). \n",
|
||||
"- Präsentieren Sie Ihre Messungen/Plot\n",
|
||||
"- Diskutieren Sie die Laufzeitunterschiede.\n",
|
||||
"- Diskutieren Sie die Abhängigkeit der Laufzeit von der Problemgrösse.\n",
|
||||
"- Diskutieren Sie mögliche Ursachen für die Abhängigkeit (z.B. Speicherzugriffszeiten, Rechenoperationen)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {},
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"#"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"name": "python",
|
||||
"version": "3.12.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
Submodule
+1
Submodule lab6/modules added at 33515ac3ad
+101
@@ -0,0 +1,101 @@
|
||||
/// @file
|
||||
/// @brief Task A
|
||||
/// Debug: g++ -O0 -g -std=c++20 taskB.cpp -Ieigen -Imodules -o taskB && ./taskB
|
||||
/// Release: g++ -DNDEBUG -O3 -std=c++20 taskB.cpp -Ieigen -Imodules -o taskB && ./taskB
|
||||
|
||||
// http://eigen.tuxfamily.org/dox/group__QuickRefPage.html#title4
|
||||
#include <Eigen/Dense> // MatrixXd, VectorXd
|
||||
|
||||
#include <cassert>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
|
||||
/// @brief Funtionality equivalent function 'poly_fit' in taskA.py
|
||||
std::vector<double> poly_fit(std::vector<double> x_coords, std::vector<double> y_coords, size_t order) {
|
||||
|
||||
assert(x_coords.size() == y_coords.size());
|
||||
|
||||
using Eigen::MatrixXd;
|
||||
using Eigen::VectorXd;
|
||||
|
||||
size_t m = y_coords.size();
|
||||
size_t n = order + 1;
|
||||
|
||||
auto A = MatrixXd(m, n);
|
||||
auto b = VectorXd(m);
|
||||
|
||||
for (size_t i = 0; i != m; ++i) {
|
||||
auto row = VectorXd(n);
|
||||
for (size_t j = 0; j != n; ++j) {
|
||||
row(j) = std::pow(x_coords[i], j);
|
||||
}
|
||||
A.row(i) = row;
|
||||
b(i) = y_coords[i];
|
||||
}
|
||||
|
||||
using std::chrono::duration;
|
||||
using std::chrono::duration_cast;
|
||||
using std::chrono::high_resolution_clock;
|
||||
using std::chrono::milliseconds;
|
||||
|
||||
auto t1 = high_resolution_clock::now();
|
||||
MatrixXd x = (A.transpose() * A).ldlt().solve(A.transpose() * b);
|
||||
auto t2 = high_resolution_clock::now();
|
||||
|
||||
duration<double, std::milli> ms_double = t2 - t1;
|
||||
std::cout << "N =" << x_coords.size() << " runtime: " << ms_double.count() << "ms\n";
|
||||
std::vector<double> res(&x(0), x.data() + x.cols() * x.rows());
|
||||
return res;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
/// @todo Change implementation s.t. rhe value for N can be supplied via a command line argument
|
||||
size_t N = 30000;
|
||||
|
||||
{
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(1);
|
||||
std::normal_distribution<double> dis(0, 0.2);
|
||||
|
||||
auto x = std::vector<double>(N, 0);
|
||||
auto y = std::vector<double>(N, 0);
|
||||
for (size_t i = 0; i != N; ++i) {
|
||||
x[i] = 0 + i * (5.0 - 0) / (N - 1);
|
||||
//y = 2*(1 - exp(-x))
|
||||
y[i] = 2.0 * (1.0 - std::exp(-x[i]));
|
||||
}
|
||||
|
||||
{
|
||||
auto coeffs = poly_fit(x, y, 3);
|
||||
std::cout << "[ ";
|
||||
for (const auto& coeff : coeffs) {
|
||||
std::cout << coeff << " ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
for (size_t i = 0; i != N; ++i) {
|
||||
y[i] = y[i] + dis(gen);
|
||||
}
|
||||
|
||||
{
|
||||
auto coeffs = poly_fit(x, y, 3);
|
||||
std::cout << "[ ";
|
||||
for (const auto& coeff : coeffs) {
|
||||
std::cout << coeff << " ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
{
|
||||
auto coeffs = poly_fit(x, y, 4);
|
||||
std::cout << "[ ";
|
||||
for (const auto& coeff : coeffs) {
|
||||
std::cout << coeff << " ";
|
||||
}
|
||||
std::cout << "]" << std::endl;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import numpy as np
|
||||
import matplotlib.pyplot as plt
|
||||
import time
|
||||
|
||||
def poly_fit(x, y, order):
|
||||
"""
|
||||
Fits the coefficients of a polynomial function to
|
||||
a set of two-dimensional data points using the least-squares method.
|
||||
|
||||
Note: uses the function 'numpy.linalg.lstsq' which returns a tuple, where the first item is the solution vector.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
x : list
|
||||
x-coordinates of the data points
|
||||
y : list
|
||||
y-coordinates of the data points
|
||||
order: int
|
||||
Order of the polynomial to fit the data, see eq. (6) in 'main.ipynb' for the polynomial form:
|
||||
https://sgit.iue.tuwien.ac.at/360049/homework8/src/branch/main/main.ipynb#user-content-Aufgabe-3:-Ausgleichungsrechnung-mit-Polynomen-(1-Punkt)
|
||||
|
||||
Returns
|
||||
-------
|
||||
list
|
||||
Coefficients of the polynomial
|
||||
"""
|
||||
A = np.array([[xi**n for n in range(order+1)] for xi in x])
|
||||
b = np.array(y)
|
||||
start_time = time.perf_counter()
|
||||
# coeff,res,rank,s = np.linalg.lstsq(A, b, rcond=None)
|
||||
coeff = np.linalg.solve(A.T@A, A.T@b)
|
||||
end_time = time.perf_counter()
|
||||
execution_time_ms = float((end_time - start_time) * 1000)
|
||||
print(f"N={len(x)} runtime: {execution_time_ms}ms")
|
||||
return coeff
|
||||
|
||||
|
||||
def plot_plot(x, y, func, filename):
|
||||
|
||||
plt.figure()
|
||||
|
||||
plt.plot(x, y, marker="o", linestyle="", label="Data")
|
||||
|
||||
x_samples = np.linspace(min(x),max(x),100)
|
||||
yp = func(x_samples)
|
||||
|
||||
plt.plot(x_samples, yp, label="Fitting")
|
||||
|
||||
plt.xlabel("x")
|
||||
plt.ylabel("y")
|
||||
plt.legend()
|
||||
plt.savefig(filename)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
|
||||
|
||||
N = 30000
|
||||
|
||||
|
||||
# w/o noise, , fit n=3
|
||||
x = np.linspace(0, 5, N)
|
||||
y = 2*(1 - np.exp(-x)) # y = 2*(1 - exp(-x))
|
||||
n = 3
|
||||
coeff = poly_fit(x, y, n)
|
||||
print(coeff)
|
||||
|
||||
# note: plotting disabled
|
||||
# func = lambda x : sum([coeff[i]*x**i for i in range(0, n+1)])
|
||||
# plot_plot(x, y, func, "taskA_wo_noise_n3.png")
|
||||
|
||||
# exponential w/ noise, fit n=3
|
||||
mean = 0.0
|
||||
sigma = 0.2
|
||||
y = y + np.random.normal(mean, sigma, len(y))
|
||||
n = 3
|
||||
coeff = poly_fit(x, y, n)
|
||||
print(coeff)
|
||||
|
||||
# note: plotting disabled
|
||||
# func = lambda x : sum([coeff[i]*x**i for i in range(0, n+1)])
|
||||
# plot_plot(x, y, func, "taskA_w_noise_n3.png")
|
||||
|
||||
# exponential w/ noise, fit n=4
|
||||
n = 4
|
||||
coeff = poly_fit(x, y, n)
|
||||
print(coeff)
|
||||
|
||||
# note: plotting disabled
|
||||
# func = lambda x : sum([coeff[i]*x**i for i in range(0, n+1)])
|
||||
# plot_plot(x, y, func, "taskA_w_noise_n4.png")
|
||||
|
||||
Reference in New Issue
Block a user