From 5b68500333deb7e15bc86f9059f214d46afeaf7e Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Fri, 25 Jan 2019 19:05:44 +0100 Subject: Delete vector --- vector/makefile | 25 -------- vector/vector.c | 114 ----------------------------------- vector/vector.cpp | 175 ------------------------------------------------------ vector/vector.md | 32 ---------- 4 files changed, 346 deletions(-) delete mode 100644 vector/makefile delete mode 100644 vector/vector.c delete mode 100644 vector/vector.cpp delete mode 100644 vector/vector.md 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 -#include -#include - -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 - -#include -#include - -#include -#include -#include -#include - - -template -struct basic_vec : public std::array { - static constexpr std::size_t dimensions = d; - - static constexpr T null_element = static_cast(0); - static constexpr T unit_element = static_cast(1); - static constexpr T unit_additive_inverse_element = static_cast(-1); - - basic_vec(); - basic_vec(const std::initializer_list l); - template basic_vec(const basic_vec& other); - - T length() const; -}; - -template -basic_vec::basic_vec() : std::array() { - this->fill(basic_vec::null_element); -} - -template -basic_vec::basic_vec(const std::initializer_list 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 -template -basic_vec::basic_vec(const basic_vec& other) { - basic_vec(); - - static_assert(d >= n); - for (std::size_t i = 0; i < n; i++) { - this->at(i) = other.at(i); - } -} - -template -T basic_vec::length() const { - T res = basic_vec::null_element; - for (const T& val : *this) { - res += val * val; - } - - return res; -} - -template -basic_vec operator+(const basic_vec& rhs, const basic_vec& lhs) { - basic_vec out; - - std::transform(rhs.begin(), rhs.end(), lhs.begin(), out.begin(), - [](const T& r, const T& l) -> T { - return r + l; - } - ); - - return out; -} - -template -basic_vec operator*(const T& rhs, const basic_vec& lhs) { - basic_vec out; - - std::transform(lhs.begin(), lhs.end(), out.begin(), - [rhs](const T& t) -> T { - return t * rhs; - }); - - return out; -} - -template -basic_vec operator-(const basic_vec& rhs, const basic_vec& lhs) { - return rhs + basic_vec::unit_additive_inverse_element * lhs; -} - -template -T operator*(const basic_vec& rhs, const basic_vec& lhs) { - return std::inner_product(rhs.begin(), rhs.end(), lhs.begin(), 0); -} - -template -std::ostream& operator<<(std::ostream& os, const basic_vec& v) { - os << "<"; - for (std::size_t i = 0; i < d -1; i++) { - os << v[i] << ", "; - } - os << v[d-1] << ">"; - return os; -} - - -template -class vec: public basic_vec { -public: - vec(std::initializer_list l) : basic_vec(l) {} - - template - vec(const basic_vec& other) : basic_vec(other) {} -}; - -template -class vec3 : public basic_vec { -public: - vec3(std::initializer_list l) : basic_vec(l) {} - - template - vec3(const basic_vec& other) : basic_vec(other) {} - - static vec3 cross(const vec3& rhs, const vec3& lhs); -}; - -template -vec3 vec3::cross(const vec3& rhs, const vec3& lhs) { - vec3 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 -class vec2: public basic_vec { -public: - vec2(std::initializer_list l) : basic_vec(l) {} - - template - vec2(const basic_vec& other) : basic_vec(other) {} - - T polar(); - static vec3 cross(const vec2& rhs, const vec2& lhs); -}; - -template -T vec2::polar() { - return std::atan2(this->at(0), this->at(1)); -} - -template -vec3 vec2::cross(const vec2& rhs, const vec2& lhs) { - return vec3::cross(vec3(rhs), vec3(lhs)); -} - - -int main(int argc, char *argv[]) { - vec3 v{1, 2, 3}; - vec3 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));` -- cgit v1.2.1