-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.h
87 lines (68 loc) · 1.74 KB
/
vector.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
//
// vector.h
// Perceptron GLM NLP Tasks
//
// Created by husnu sensoy on 19/02/14.
// Copyright (c) 2014 husnu sensoy. All rights reserved.
//
#ifndef Perceptron_GLM_NLP_Tasks_vector_h
#define Perceptron_GLM_NLP_Tasks_vector_h
#include <float.h>
#define NEGATIVE_INFINITY (-(FLT_MAX - 10.))
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#include "uthash.h"
#include "debug.h"
struct vector {
size_t n;
size_t last_idx;
float* data;
};
typedef struct vector* vector;
vector parse_vector(char *buff);
vector vector_create(size_t n);
void vector_free(vector);
/**
* @brief Element-wise vector addition: target = target + mult * src
* @param target Target vector on which src to be added.
* @param src Vector to be added.
* @param mult Multiplier of src before adding on target
*/
void vadd(vector target, const vector src, float mult);
float vdot(vector v1, vector v2);
void vdiv(vector v, int div);
void vnorm(vector v, size_t n);
void vprint(vector v);
vector vlinear(vector target, vector src);
vector vquadratic(vector target, vector src, float d);
vector vcubic(vector target, vector src, size_t length);
/**
* @brief Vector concatenation like np.concatenate
* @param target
* @param v
* @return vector of true_n = target->true_n + v->true_n aligned to 64 bytes
*/
vector vconcat(vector target, const vector v);
vector vconcat_arr(vector target, size_t n, const float arr[]);
/**
*
* @param v1
* @param v2
* @return v1.v2
*/
static inline float linear(vector v1, vector v2) {
return vdot(v1, v2);
}
/**
*
* @param v1
* @param v2
* @param d
* @param n
* @return (v1.v2 + d )^n
*/
static inline float polynomial(vector v1, vector v2, float d, int n) {
return pow(vdot(v1, v2) + d, n);
}
#endif