-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.c
114 lines (80 loc) · 2.92 KB
/
Utils.c
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
/** ***************************************************** **/
/** ** Phonon Vibration Analysis ** **/
/** **/
/** ** Version 2 ** **/
/** **/
/** By: Pedro Brandimarte ([email protected]) and **/
/** Alexandre Reily Rocha ([email protected]) **/
/** **/
/** ***************************************************** **/
/** Implementation of matrix (vector) algebra utilities. **/
/** ***************************************************** **/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "Check.h"
#include "Utils.h"
/** *********** Matrix and Vectors Utilities ************ **/
/* ********************************************************* */
/* Allocates and initialize a vector 'V[n]' of integers. */
void *UTILintVector (int n)
{
register int i;
int *V;
V = CHECKmalloc (n * sizeof (int));
for (i = 0; i < n; i++)
V[i] = 0;
return V;
} /* UTILintVector */
/* ********************************************************* */
/* Allocates and initializes a vector 'V[n]' of doubles. */
void *UTILdoubleVector (int n)
{
register int i;
double *V;
V = CHECKmalloc (n * sizeof (double));
for (i = 0; i < n; i++)
V[i] = 0.0;
return V;
} /* UTILdoubleVector */
/* ********************************************************* */
/* Resets a vector 'V[n]' of doubles with 0.0's. */
void UTILresetDoubleVector (int n, double *V)
{
register int i;
for (i = 0; i < n; i++)
V[i] = 0.0;
} /* UTILresetDoubleVector */
/* ********************************************************* */
/* Copies the elements form a vector 'Orig[n]' to another */
/* vector 'Dest[n]'. */
void UTILcopyVector (double *Dest, double *Orig, int n)
{
register int i;
for (i = 0; i < n; i++)
Dest[i] = Orig[i];
} /* UTILcopyVector */
/* ********************************************************* */
/* Checks if the matrix 'M[n][n]' is symetric. */
void UTILcheckSym (char *name, double *M, int n, double lim)
{
register int i, j;
for (j = 0; j < n - 1; j++)
for (i = j + 1; i < n; i++)
if (fabs(M[idx(i,j,n)] - M[idx(j,i,n)]) > lim) {
printf ("\n The matrix %s is not symmetric!\n", name);
return ;
}
} /* UTILcheckSym */
/* ********************************************************* */
/* Computes the Euclidean norm of a double precision vector */
/* 'V[n]'. */
double UTILnorm (int n, double *V)
{
register int i;
double x = 0.0;
for (i = 0; i < n; i++)
x = x + V[i] * V[i];
return (sqrt(x));
} /* UTILnorm */
/* ************************ Drafts ************************* */