diff options
author | Nao Pross <naopross@thearcway.org> | 2019-01-25 19:05:44 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2019-01-25 19:05:44 +0100 |
commit | 5b68500333deb7e15bc86f9059f214d46afeaf7e (patch) | |
tree | b9a9a9658331fc64863a189011646327d1823998 | |
parent | Update vector.cpp to use STL std::transform and std::inner_product (diff) | |
download | cplusplus-5b68500333deb7e15bc86f9059f214d46afeaf7e.tar.gz cplusplus-5b68500333deb7e15bc86f9059f214d46afeaf7e.zip |
Delete vector
-rw-r--r-- | vector/makefile | 25 | ||||
-rw-r--r-- | vector/vector.c | 114 | ||||
-rw-r--r-- | vector/vector.cpp | 175 | ||||
-rw-r--r-- | vector/vector.md | 32 |
4 files changed, 0 insertions, 346 deletions
diff --git a/vector/makefile b/vector/makefile deleted file mode 100644 index 31136ea..0000000 --- a/vector/makefile +++ /dev/null @@ -1,25 +0,0 @@ -CC := gcc -CARGS := -Wall -Werror -I. -LDARGS := -lm - -CPPC := g++-8 -CPPARGS := -Wall -I. -std=c++17 - -all: vector.pdf c_build/vector cpp_build/vector - -%.pdf: %.md - pandoc \ - --from=markdown+raw_tex \ - --latex-engine=xelatex \ - --number-sections \ - --filter pandoc-include-code \ - --highlight-style tango \ - $< -o $@ - -c_build/%: %.c - mkdir -p c_build - $(CC) $(CARGS) $< -o $@ $(LDARGS) - -cpp_build/%: %.cpp - mkdir -p cpp_build - $(CPPC) $(CPPARGS) $< -o $@ diff --git a/vector/vector.c b/vector/vector.c deleted file mode 100644 index 59f48cc..0000000 --- a/vector/vector.c +++ /dev/null @@ -1,114 +0,0 @@ -#include <stddef.h> -#include <stdio.h> -#include <math.h> - -struct vec3 { - double x; - double y; - double z; -}; - -struct vec3 vec3_add(struct vec3 * const v, struct vec3 * const u) -{ - struct vec3 result = {0, 0, 0}; - - if (v == NULL || u == NULL) - return result; - - result.x = v->x + u->x; - result.y = v->y + u->y; - result.z = v->z + u->z; - - return result; -} - -struct vec3 vec3_mul(struct vec3 * const v, const double scalar) -{ - struct vec3 result = {0, 0, 0}; - - if (v == NULL) - return result; - - result.x = scalar * v->x; - result.y = scalar * v->y; - result.z = scalar * v->z; - - return result; -} - -struct vec3 vec3_sub(struct vec3 * const v, struct vec3 * const u) -{ - struct vec3 result = {0, 0, 0}; - - result = vec3_mul(u, -1.0); - result = vec3_add(v, &result); - - return result; -} - -double vec3_dot(struct vec3 * const v, struct vec3 * const u) -{ - if (v == NULL || u == NULL) - return 0; - - return (v->x * u->x) + (v->y * u->y) + (v->z * u->y); -} - -struct vec3 vec3_cross(struct vec3 * const v, struct vec3 * const u) -{ - struct vec3 result = {0, 0, 0}; - - result.x = (v->y * u->z) - (v->z * u->y); - result.y = (v->z * u->x) - (v->x * u->z); - result.z = (v->x * u->y) - (v->y * u->x); - - return result; -} - -double vec3_mag(struct vec3 * const v) { - return sqrt((v->x * v->x) + (v->y * v->y) + (v->z * v->z)); -} - -void vec3_print(struct vec3 * const v) -{ - if (v == NULL) - return; - - printf("<%.2f,%.2f,%.2f>\n", v->x, v->y, v->z); -} - - -int main(int argc, char *argv[]) -{ - struct vec3 a = {1, 2, 3}; - struct vec3 b = {4, 5, 6}; - struct vec3 res; - - printf("a = "); - vec3_print(&a); - printf("b = "); - vec3_print(&b); - - printf("||a|| = %f\n", vec3_mag(&a)); - - printf("sum a + b : "); - res = vec3_add(&a, &b); - vec3_print(&res); - - printf("sub a - b : "); - res = vec3_sub(&a, &b); - vec3_print(&res); - - printf("product 3*a : "); - res = vec3_mul(&a, 3); - vec3_print(&res); - - printf("dot product : "); - printf("%.2f\n", vec3_dot(&a, &b)); - - printf("cross product : "); - res = vec3_cross(&a, &b); - vec3_print(&res); - - return 0; -} diff --git a/vector/vector.cpp b/vector/vector.cpp deleted file mode 100644 index 260f08f..0000000 --- a/vector/vector.cpp +++ /dev/null @@ -1,175 +0,0 @@ -#include <iostream> - -#include <cassert> -#include <cmath> - -#include <array> -#include <algorithm> -#include <numeric> -#include <initializer_list> - - -template<typename T, std::size_t d> -struct basic_vec : public std::array<T, d> { - static constexpr std::size_t dimensions = d; - - static constexpr T null_element = static_cast<T>(0); - static constexpr T unit_element = static_cast<T>(1); - static constexpr T unit_additive_inverse_element = static_cast<T>(-1); - - basic_vec(); - basic_vec(const std::initializer_list<T> l); - template<std::size_t n> basic_vec(const basic_vec<T, n>& other); - - T length() const; -}; - -template<typename T, std::size_t d> -basic_vec<T, d>::basic_vec() : std::array<T, d>() { - this->fill(basic_vec<T,d>::null_element); -} - -template<typename T, std::size_t d> -basic_vec<T, d>::basic_vec(const std::initializer_list<T> l) { - basic_vec(); - - // why can't this sh*t be a constexpr with static_assert??? - assert(l.size() <= d); - std::copy(l.begin(), l.end(), this->begin()); -} - -template<typename T, std::size_t d> -template<std::size_t n> -basic_vec<T, d>::basic_vec(const basic_vec<T, n>& other) { - basic_vec(); - - static_assert(d >= n); - for (std::size_t i = 0; i < n; i++) { - this->at(i) = other.at(i); - } -} - -template<typename T, std::size_t d> -T basic_vec<T, d>::length() const { - T res = basic_vec<T, d>::null_element; - for (const T& val : *this) { - res += val * val; - } - - return res; -} - -template<typename T, std::size_t d> -basic_vec<T, d> operator+(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) { - basic_vec<T, d> out; - - std::transform(rhs.begin(), rhs.end(), lhs.begin(), out.begin(), - [](const T& r, const T& l) -> T { - return r + l; - } - ); - - return out; -} - -template<typename T, std::size_t d> -basic_vec<T, d> operator*(const T& rhs, const basic_vec<T, d>& lhs) { - basic_vec<T, d> out; - - std::transform(lhs.begin(), lhs.end(), out.begin(), - [rhs](const T& t) -> T { - return t * rhs; - }); - - return out; -} - -template<typename T, std::size_t d> -basic_vec<T, d> operator-(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) { - return rhs + basic_vec<T, d>::unit_additive_inverse_element * lhs; -} - -template<typename T, std::size_t d> -T operator*(const basic_vec<T, d>& rhs, const basic_vec<T, d>& lhs) { - return std::inner_product(rhs.begin(), rhs.end(), lhs.begin(), 0); -} - -template<typename T, std::size_t d> -std::ostream& operator<<(std::ostream& os, const basic_vec<T, d>& v) { - os << "<"; - for (std::size_t i = 0; i < d -1; i++) { - os << v[i] << ", "; - } - os << v[d-1] << ">"; - return os; -} - - -template<typename T, std::size_t d> -class vec: public basic_vec<T, d> { -public: - vec(std::initializer_list<T> l) : basic_vec<T, d>(l) {} - - template<std::size_t n> - vec(const basic_vec<T, n>& other) : basic_vec<T, d>(other) {} -}; - -template<typename T> -class vec3 : public basic_vec<T, 3> { -public: - vec3(std::initializer_list<T> l) : basic_vec<T, 3>(l) {} - - template<std::size_t n> - vec3(const basic_vec<T, n>& other) : basic_vec<T, 3>(other) {} - - static vec3<T> cross(const vec3<T>& rhs, const vec3<T>& lhs); -}; - -template<typename T> -vec3<T> vec3<T>::cross(const vec3<T>& rhs, const vec3<T>& lhs) { - vec3<T> res; - - res[0] = (rhs[1] * lhs[2]) - (rhs[2] * lhs[1]); - res[1] = (rhs[2] * lhs[0]) - (rhs[0] * lhs[2]); - res[2] = (rhs[0] * lhs[1]) - (rhs[1] * lhs[0]); - - return res; -} - - -template<typename T> -class vec2: public basic_vec<T, 2> { -public: - vec2(std::initializer_list<T> l) : basic_vec<T, 2>(l) {} - - template<std::size_t n> - vec2(const basic_vec<T, n>& other) : basic_vec<T, 2>(other) {} - - T polar(); - static vec3<T> cross(const vec2<T>& rhs, const vec2<T>& lhs); -}; - -template<typename T> -T vec2<T>::polar() { - return std::atan2(this->at(0), this->at(1)); -} - -template<typename T> -vec3<T> vec2<T>::cross(const vec2<T>& rhs, const vec2<T>& lhs) { - return vec3<T>::cross(vec3<T>(rhs), vec3<T>(lhs)); -} - - -int main(int argc, char *argv[]) { - vec3<double> v{1, 2, 3}; - vec3<double> u{3, 4, 5}; - - std::cout << v << std::endl; - std::cout << v.length() << std::endl; - std::cout << v + u << std::endl; - std::cout << v - u << std::endl; - std::cout << 2.0 * u << std::endl; - std::cout << v * u << std::endl; - - return 0; -} diff --git a/vector/vector.md b/vector/vector.md deleted file mode 100644 index abbc0f9..0000000 --- a/vector/vector.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: Costruire dei vettori matematici dal C al C++ -date: 9 Dicembre 2018 -author: - - Naoki Pross -papersize: a4 ---- - -# Premessa -Si da per assunto che si ha delle conscenze di C di funzioni, strutture dati e -puntatori e si vuole imparare il C++11. - -L'obiettivo è di ottenere una libreria di vettori matematici con le operazioni -vettoriali di somma, sottrazione, prodotto con scalare, scalare e vettoriale. -Per il primo esempio i vettori saranno unicamente tridimensionali. - -Sarà inoltre utilizzata la terminologia inglese di *dot product* per il -prodotto scalare e *cross product* per il prodotto vettoriale. - -# Un implementazione in C -Per introdurre il concetto, a seguire vi è un esempio di un implementazione -semplice in C. -```{.C include=vector.c startLine=1 endLine=73} -``` -Per alcune applicazioni ciò è assolutamente sufficiente, ma osserviamo alcune -limitazioni: - -- `vec3` contiene solo informazioni di tipo `double` -- Si possono creare dei `vec3` non inzializzati -- La notazione delle operazioni è scomoda. - Per esempio $\vec{v} \cdot 3 \cdot \vec{u}$ diventa - `vec3_dot(&v, &vec3_mul(&u, 3));` |