-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvmath.c
More file actions
106 lines (91 loc) · 2.7 KB
/
Copy pathvmath.c
File metadata and controls
106 lines (91 loc) · 2.7 KB
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
/* this file will contain all math functions for vector operations
required in the QPP problem*/
#include <complex.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "structs.h"
long int ipow(long int base, unsigned int exp) {
/*integer power function so math does not need to be included*/
/*peer reviewed by frohlofl*/
int result = 1;
for (unsigned int i=0; i<exp; i++){
result *= base;
}
return result;
}
double complex dot_product(double complex *v, double complex *w, long int L) {
/*returns the complex dot product of two vectors v, w*/
double complex sprod = 0.0;
for(long int i=0; i<L; i++) {
sprod+=(~(v[i]))*w[i];
}
return sprod;
}
void scalar_vec(double complex *out, double complex *vec, double complex s, long int L) {
/*returns the product of a scalar s and a vector vec*/
for(long int i=0; i<L; i++) {
out[i] = s*vec[i];
}
}
void mul_compl_vec_real_scal(double complex *out, double complex *vec, double s, long int L) {
/*same as previous function but with real scalar*/
for(long int i=0; i<L; i++){
out[i] = s*vec[i];
}
}
void mul_element(double complex *out, double complex *w, double complex *v, long int L) {
/*returns the elementwise multiplication of two complex vectors v, w*/
for(long int i=0; i<L; i++) {
out[i] = v[i]*w[i];
}
}
void add_vec(double complex *out, double complex *w, double complex *v, long int L) {
/*returns the sum of two complex vectors v, w*/
for(long int i=0; i<L; i++) {
out[i] = v[i]+w[i];
}
}
void sub_vec(double complex *out, double complex *v, double complex *w, long int L) {
/*returns the difference of two complex vectors v, w*/
for(long int i=0; i<L; i++) {
out[i] = v[i]-w[i];
}
}
void assign_vec(double complex *out, double complex *in, long int L) {
/*copy the values of in to values of out*/
for(long int i=0; i<L; i++) {
out[i] = in[i];
}
}
void set_zero(double complex *in, long int L) {
/*set all values of in to zero*/
for(long int i=0; i<L; i++) {
in[i] = 0.0;
}
}
double abs_vec(double complex *in, long int L) {
return sqrt(creal(dot_product(in, in, L)));
}
long int fact(int n){
long int res = 1;
for(int i=1; i<=n; i++){
res *= i;
}
return res;
}
void mat_vec_mul(double complex *out, double complex *mat, double complex *vec, long int N){ //N is the length of the vector
set_zero(out, N);
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
out[i] += mat[N*i+j] * vec[j];
}
}
}
void hermtransp_quadr_matrix(double complex *out, double complex *in, long int N){ //N is the length of one size
for(int i=0; i<N; i++){
for(int j=0; j<N; j++){
out[i*N+j] = (~(in[j*N+i]));
}
}
}