RcdMathLib_doc
Open Source Library for Linear and Non-linear Algebra
vector_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
3  * 2020 Freie Universität Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser General
6  * Public License v2.1. See the file LICENSE in the top level directory for more
7  * details.
8  */
9 
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26 #include <math.h>
27 
28 #include "vector.h"
29 
30 void vector_test(void)
31 {
32  puts("############ Test Vector Algebra ###############");
33  vector_t a[5] = { 0.0975, 0.2785, 0.5469, 0.9575, 0.9649 };
34  vector_t b[5] = { 0.1576, 0.9706, 0.9572, 0.4854, 0.8003 };
35  vector_t c[5];
36  vector_t d[5] = { 0.1576, 0.9706, 0.9572, 0.4854, 0.8003 };
37 
38  uint8_t size = 5;
39 
40  vector_add(size, a, b, c);
41  printf("c = a + b = ");
42  vector_flex_print(size, c, 3, 4);
43  puts("");
44 
45  vector_sub(size, a, b, c);
46  printf("c = a - b = ");
47  vector_flex_print(size, c, 3, 4);
48  puts("");
49 
50  vector_mul(size, a, b, c);
51  printf("c = a * b = ");
52  vector_flex_print(size, c, 3, 4);
53  puts("");
54 
55  vector_t mean = vector_get_mean_value(size, a);
56  printf("mean(a) = %.4f\n", mean);
57 
58  vector_t norm2 = vector_get_norm2(size, a);
59  printf("norm2(a) = %.4f\n", norm2);
60 
61  vector_t squ_norm2 = vector_get_square_norm2(size, b);
62  printf("squ_norm2(b) = %.4f\n", squ_norm2);
63 
64  vector_t scal = vector_get_scalar_product(size, a, b);
65  printf("scal(a) = %.4f\n", scal);
66 
67  vector_t sum = vector_get_sum(size, a);
68  printf("sum(a) = %.4f\n", sum);
69 
70  vector_t euc_dist = vector_get_euclidean_distance(size, a, b);
71  printf("euc_dist(a,b) = %.4f\n", euc_dist);
72 
73  if (vector_is_equal(size, b, d)) {
74  puts("vectors are equal !!");
75  }
76  else {
77  puts("vectors are not equal !!");
78  }
79 
80  vector_copy(size, a, d);
81  vector_flex_print(size, d, 3, 4);
82  puts("");
83 
84  vector_clear(size, d);
85  vector_flex_print(size, d, 3, 4);
86  puts("");
87 
88 }
vector_get_norm2
vector_t vector_get_norm2(uint8_t length, vector_t arr[])
Compute the 2-norm norm of a vector.
Definition: vector.c:42
vector_flex_print
void vector_flex_print(uint32_t length, vector_t arr[], uint8_t before_dot, uint8_t after_dot)
Display the values of the vector's elements.
Definition: vector.c:284
vector_get_euclidean_distance
vector_t vector_get_euclidean_distance(uint8_t length, vector_t vec1[], vector_t vec2[])
Compute the Euclidean distance between two vectors.
Definition: vector.c:163
vector_get_scalar_product
vector_t vector_get_scalar_product(uint8_t n, vector_t vec1[n], vector_t vec2[n])
Compute the dot product of two vectors.
Definition: vector.c:190
vector_get_square_norm2
vector_t vector_get_square_norm2(uint8_t length, vector_t arr[])
Compute the squared 2-norm norm of a vector .
Definition: vector.c:54
vector_t
#define vector_t
Define the data type of the vector elements.
Definition: vector.h:33
vector_clear
void vector_clear(uint8_t size, vector_t arr[])
Clear all the elements of the vector.
Definition: vector.c:32
vector_copy
void vector_copy(uint8_t size, vector_t src_arr[], vector_t dest_arr[])
Copy the elements of the source vector to the destination vector.
Definition: vector.c:37
vector_is_equal
bool vector_is_equal(uint16_t length, vector_t vec_1[], vector_t vec_2[])
Determine the equality of two vectors.
Definition: vector.c:202
vector_mul
void vector_mul(uint8_t size, vector_t a_vec[size], vector_t b_vec[size], vector_t a_mul_b_vec[size])
Compute the multiplication of two vectors.
Definition: vector.c:114
vector_add
void vector_add(uint8_t size, vector_t a_vec[size], vector_t b_vec[size], vector_t a_plus_b_vec[size])
Compute the addition of two vectors.
Definition: vector.c:104
vector_get_sum
vector_t vector_get_sum(uint8_t length, vector_t arr[])
Compute the sum of the elements of a vector.
Definition: vector.c:66
vector.h
Vector computations.
vector_get_mean_value
vector_t vector_get_mean_value(uint8_t length, vector_t arr[])
Compute the average or mean value of a vector.
Definition: vector.c:78
vector_test
void vector_test(void)
Examples of vector operations.
Definition: vector_test.c:30
vector_sub
void vector_sub(uint8_t size, vector_t a_vec[], vector_t b_vec[], vector_t a_minus_b[])
Compute the subtraction of two vectors.
Definition: vector.c:94