-
Notifications
You must be signed in to change notification settings - Fork 3
/
matrix_angle.h
166 lines (134 loc) · 4.38 KB
/
matrix_angle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* @file
*/
/*
* Copyright (c) 2016 Andrei Parente
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
**/
#ifndef MATRIX_ANGLE_OPS_H
#define MATRIX_ANGLE_OPS_H
#include "matrix.h"
#include "matrix_norms.h"
/**
* Calcula o produto interno entre a linha row e a coluna column de mat
* @author Andrei Parente
*/
static double row_column_dot_product(const matrix_t *mat, size_t row, size_t column)
{
double product = 0;
size_t i;
assert(mat->rows == mat->columns);
for (i = 0; i < mat->rows; ++i) {
product += matrix_get_at(mat, row, i)* matrix_get_at(mat, i, column);
}
return product;
}
/**
* Calcula a magnitude do vetor vector
*/
static double vector_length(const matrix_t *vector)
{
size_t i;
double accumulator = 0;
assert(vector->rows == 1);
for (i = 0; i < vector->columns; ++i) {
accumulator += pow(matrix_get_at(vector, 0, i), 2);
}
return sqrt(accumulator);
}
/**
* Calcula o ângulo entre a linha row e a coluna column de mat
* @author Andrei Parente
*/
static double row_column_angle(const matrix_t *mat, size_t row, size_t column)
{
size_t i;
double row_length = 0,
column_length = 0;
matrix_t *temp = matrix_new(1, mat->columns);
for (i = 0; i < mat->columns; ++i) {
matrix_set_at(temp, 0, i, matrix_get_at(mat, row, i));
}
row_length = vector_length(temp);
matrix_free(temp);
temp = matrix_new(1, mat->rows);
for (i = 0; i < mat->rows; ++i) {
matrix_set_at(temp, 0, i, matrix_get_at(mat, i, column));
}
column_length = vector_length(temp);
matrix_free(temp);
double angle = row_column_dot_product(mat, row, column) / (row_length * column_length);
return acos(angle);
}
/**
* Calcula a distância entre dois vetores
*
* @param u e v, vetor em matriz linha
*
* @author Pedro da Luz
*/
static double vector_distance_vector(const matrix_t * restrict u, const matrix_t * restrict v)
{
double accumulator;
size_t i;
assert(u->rows == 1);
assert(v->rows == 1);
assert(u->columns == v->columns);
for(i = 0; i < u->columns; i++)
accumulator += pow(matrix_get_at(v, 0, i) - matrix_get_at(u, 0, i), 2);
return sqrt(accumulator);
}
/**
* Calcula o ângulo interno entre dois vetores
*
* @param u e v, vetores em matriz linha
* @return angle, valor do ângulo em graus
*
* @author Pedro da Luz
*/
static double vector_angle_vector(const matrix_t * restrict u, const matrix_t * restrict v)
{
double angle;
double accumulator = 0;
assert(u->rows == 1);
assert(v->rows == 1);
assert(u->columns == v->columns);
size_t i;
for(i = 0; i < u->columns; i++)
accumulator += matrix_get_at(u, 0, i) * matrix_get_at(v, 0, i);
angle = acos(accumulator / (vector_norm2(u) * vector_norm2(v))); // Arco-Cosseno em Radianos
angle = angle * (180/M_PI); // Converte para graus
return angle;
}
/**
* Calcula o produto interno entre dois vetores
*
* @param u e v, vetores em matriz linha
* @return innner_product_space, produto interno
*
* @author Pedro da Luz
*/
static double vector_innerProductSpace(const matrix_t * restrict u, const matrix_t * restrict v)
{
double innner_product_space;
innner_product_space = (vector_norm2(u) * vector_norm2(v)) * cos(vector_angle_vector(u, v) * (M_PI/180));
return innner_product_space;
}
#endif