-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmatrix.c
580 lines (480 loc) · 13.5 KB
/
matrix.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*==================================================
SET TABSTOPS AT EVERY FOUR SPACES FOR PROPER DISPLAY
====================================================*/
/****************************************************************************
* FILE: matrix.c
* DATE: June, 1993
* BY: Andrew L. Thomas
*
* This file contains several functions for manipulating 3-component vectors
* and 3x3 matrices (1st and 2nd rank tensors). The variable type assumed
* for vector and matrix components is set in the file matrix.h
*****************************************************************************/
/****************************** Includes/Defines ****************************/
#include <math.h>
#include "matrix.h"
#include "nr.h"
#include "nrutil.h"
/*************************** Function: matrix_mult ***************************
* Multiplies the 3x3 matrices a and b to obtain matrix c (i.e. c = ab).
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void matrix_mult(matrix_t a, matrix_t b, matrix_t c)
#else
void matrix_mult(a, b, c)
matrix_t a;
matrix_t b;
matrix_t c;
#endif
{
int i,j,k;
matrix_t a_copy;
matrix_t b_copy;
copy_matrix(a,a_copy);
copy_matrix(b,b_copy);
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
c[i][j] = 0.;
for (k=0; k < 3; k++) {
c[i][j] += a_copy[i][k] * b_copy[k][j];
}
}
}
}
/************************* Function: rotate_vector **************************
* Rotates the vector x to a new coord system using the rotation matrix a
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void rotate_vector(int inverse_rot, matrix_t a, vector_t x)
#else
void rotate_vector(inverse_rot, a, x)
int inverse_rot;
matrix_t a;
vector_t x;
#endif
{
matrix_t a_copy;
copy_matrix(a,a_copy);
if (inverse_rot) {
transpose_matrix(a_copy);
}
matrix_vector_mult(a_copy,x,x);
}
/************************ Function: matrix_vector_mult ***********************
* Multiplies the 3x3 matrices a and b to obtrain matrix c
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void matrix_vector_mult(matrix_t a, vector_t b, vector_t c)
#else
void matrix_vector_mult(a, b, c)
matrix_t a;
vector_t b;
vector_t c;
#endif
{
int i, j;
vector_t tmp_b;
copy_vector(b,tmp_b);
for (i=0; i < 3; i++) {
c[i] = 0.;
for (j=0; j < 3; j++) {
c[i] += a[i][j] * tmp_b[j];
}
}
}
/************************* Function: rotate_tensor **************************
* Rotates the tensor s to a new coord system using the rotation matrix a
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void rotate_tensor(int inverse_rot, matrix_t a, matrix_t s)
#else
void rotate_tensor(inverse_rot, a, s)
int inverse_rot;
matrix_t a;
matrix_t s;
#endif
{
matrix_t a_copy;
matrix_t a_copy_trans;
matrix_t temp;
copy_matrix(a,a_copy);
if (inverse_rot) {
transpose_matrix(a_copy);
}
copy_matrix(a_copy,a_copy_trans);
transpose_matrix(a_copy_trans);
matrix_mult(s,a_copy_trans,temp);
matrix_mult(a_copy,temp,s);
}
/*************************** Function: dot_product ***************************
* Calculates the dot product of the vectors v1 and v2
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
component_t dot_product(vector_t v1, vector_t v2)
#else
component_t dot_product(v1, v2)
vector_t v1;
vector_t v2;
#endif
{
return(v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2]);
}
/************************** Function: cross_product **************************
* Computes the cross product of vector1 and vector2, returning the result in
* vector3
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void cross_product(vector_t v1, vector_t v2, vector_t v3)
#else
void cross_product(v1, v2, v3)
vector_t v1;
vector_t v2;
vector_t v3;
#endif
{
int i, j, k;
for (i=0; i < 3; i++) {
j = i + 1;
k = j + 1;
if (j > 2)
j -= 3;
if (k > 2)
k -= 3;
v3[i] = v1[j]*v2[k] - v1[k]*v2[j];
}
}
/*********************** Function: vector_magnitude **************************
* Calculates the magnitude of the vector v.
******************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
component_t vector_magnitude(vector_t v)
#else
component_t vector_magnitude(v)
vector_t v;
#endif
{
component_t mag;
mag = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
return(mag);
}
/*********************** Function: subtract_vectors **************************
* Subtracts the vectors v2 and v1 to obtain v3
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void subtract_vectors(vector_t v1, vector_t v2, vector_t v3)
#else
void subtract_vectors(v1, v2, v3)
vector_t v1;
vector_t v2;
vector_t v3;
#endif
{
int i;
for (i=0; i < 3; i++) {
v3[i] = v1[i] - v2[i];
}
}
/******************* Function: transform_position_vector *********************
* Transforms the position vector x to a new coordinate system with the given
* origin using the coordinate rotation matrix a.
******************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void transform_position_vector(int inverse_trans, vector_t origin, matrix_t a,vector_t x)
#else
void transform_position_vector(inverse_trans, origin, a, x)
int inverse_trans;
vector_t origin;
matrix_t a;
vector_t x;
#endif
{
/*
if (inverse_trans) {
rotate_vector(INVERSE_ROT,a,x);
subtract_vectors(x,origin,x);
}
else {
rotate_vector(FORWARD_ROT,a,x);
add_vectors(x,origin,x);
}
*/
if (inverse_trans) {
rotate_vector(INVERSE_ROT,a,x);
add_vectors(x,origin,x);
} else {
subtract_vectors(x,origin,x);
rotate_vector(FORWARD_ROT,a,x);
}
}
/*************************** Function: transpose_matrix **********************
* Transposes the 3x3 matrix a.
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void transpose_matrix(matrix_t a)
#else
void transpose_matrix(a)
matrix_t a;
#endif
{
int i, j;
matrix_t a_copy;
copy_matrix(a,a_copy);
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
a[i][j] = a_copy[j][i];
}
}
}
/************************ Function: initialize_matrix ***********************
* Initializes the matrix m using the given value.
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void initialize_matrix(matrix_t m, component_t value)
#else
void initialize_matrix(m, value)
matrix_t m;
component_t value;
#endif
{
int i, j;
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
m[i][j] = value;
}
}
}
/************************** Function: initialize_vector *********************
* Initializes the vector v using the given value
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void initialize_vector(vector_t v, component_t value)
#else
void initialize_vector(v, value)
vector_t v;
component_t value;
#endif
{
int i;
for (i=0; i < 3; i++) {
v[i] = value;
}
}
/**************************** Function: add_matrices ************************
* Adds the 3x3 matrices m1 and m2 to obtain m3.
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void add_matrices(matrix_t m1, matrix_t m2, matrix_t m3)
#else
void add_matrices(m1, m2, m3)
matrix_t m1;
matrix_t m2;
matrix_t m3;
#endif
{
int i, j;
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
m3[i][j] = m1[i][j] + m2[i][j];
}
}
}
/*********************** Function: subtract_matrices ************************
* Subtracts the 3x3 matrices m1 and m2 to obtain m3
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void subtract_matrices(matrix_t m1, matrix_t m2, matrix_t m3)
#else
void subtract_matrices(m1, m2, m3)
matrix_t m1;
matrix_t m2;
matrix_t m3;
#endif
{
int i, j;
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
m3[i][j] = m1[i][j] - m2[i][j];
}
}
}
/************************* Function: add_vectors ****************************
* Adds the vectors v1 and v2 to obtain v3
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void add_vectors(vector_t v1, vector_t v2, vector_t v3)
#else
void add_vectors(v1, v2, v3)
vector_t v1;
vector_t v2;
vector_t v3;
#endif
{
int i;
for (i=0; i < 3; i++) {
v3[i] = v1[i] + v2[i];
}
}
/************************** Function: copy_vector ***************************
* Copies the vector v to v_copy
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void copy_vector(vector_t v, vector_t v_copy)
#else
void copy_vector(v, v_copy)
vector_t v;
vector_t v_copy;
#endif
{
int i;
for (i=0; i < 3; i++) {
v_copy[i] = v[i];
}
}
/************************** Function: copy_matrix ***************************
* Copies the matrix m to m_copy.
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void copy_matrix(matrix_t m, matrix_t m_copy)
#else
void copy_matrix(m, m_copy)
matrix_t m;
matrix_t m_copy;
#endif
{
int i, j;
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
m_copy[i][j] = m[i][j];
}
}
}
/*********************** Function: scalar_vector_mult ***********************
* Multiplies the vector v1 by the scalar s to obtain the vector v2.
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void scalar_vector_mult(component_t s, vector_t v1, vector_t v2)
#else
void scalar_vector_mult(s, v1, v2)
component_t s;
vector_t v1;
vector_t v2;
#endif
{
int i;
for (i=0; i < 3; i++) {
v2[i] = s * v1[i];
}
}
/*********************** Function: scalar_matrix_mult ***********************
* Multiplies the 3x3 matrix m1 by the scalar s to obtain the 3x3 matrix m2.
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void scalar_matrix_mult(component_t s, matrix_t m1, matrix_t m2)
#else
void scalar_matrix_mult(s, m1, m2)
component_t s;
matrix_t m1;
matrix_t m2;
#endif
{
int i, j;
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
m2[i][j] = s * m1[i][j];
}
}
}
/*********************** Function: normalize_vector ************************
* Normalizes the vector v.
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void normalize_vector(vector_t v)
#else
void normalize_vector(v)
vector_t v;
#endif
{
scalar_vector_mult(1/vector_magnitude(v),v,v);
}
/**************************** Function: cauchy ******************************
* Calculates the traction vector traction_v on a plane with unit normal vector
* normal_v due to the stress tensor stress.
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void cauchy(matrix_t stress, vector_t normal_v, vector_t traction_v)
#else
void cauchy(stress, normal_v, traction_v)
matrix_t stress;
vector_t normal_v;
vector_t traction_v;
#endif
{
int i,j;
initialize_vector(traction_v,0.0);
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
traction_v[i] += stress[j][i] * normal_v[j];
}
}
}
/************************** Function: principal ******************************
* Calculates the magnitudes, prin, and directions, traj, of principal
* strains/stresses given the stress/strain tensor, str.
*
* Uses functions d_jacobi and d_eigsrt adapted by permission from the book:
*
* Numerical Recipes in C: The Art of Scientific Computing, 2nd Ed.
* Press, W.H., Teukolsky, S.A., Vetterling, W.T. & Flannery, B.P.
* 1992, Cambridge University Press, Cambridge, 994 p.
*****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
void principal(matrix_t str, vector_t prin, matrix_t traj)
#else
void principal(str, prin, traj)
matrix_t str;
vector_t prin;
matrix_t traj;
#endif
{
double **nr_str;
double *nr_prin;
double **nr_traj;
int nrot;
int i, j;
nr_str = dmatrix(1,3,1,3);
nr_prin = dvector(1,3);
nr_traj = dmatrix(1,3,1,3);
for (i=0; i < 3; i++) {
for (j=0; j < 3; j++) {
nr_str[i+1][j+1] = str[i][j];
}
}
d_jacobi(nr_str,3,nr_prin,nr_traj,&nrot);
d_eigsrt(nr_prin,nr_traj,3);
for (i=0; i < 3; i++) {
prin[i] = nr_prin[i+1];
for (j=0; j < 3; j++) {
traj[i][j] = nr_traj[j+1][i+1];
}
}
free_dmatrix(nr_str,1,3,1,3);
free_dvector(nr_prin,1,3);
free_dmatrix(nr_traj,1,3,1,3);
}
/************************** Function: distance *********************
* return distance from point v to point w
****************************************************************************/
#if defined(__STDC__) || defined(ANSI) /* ANSI */
float distance(vector_t v, vector_t w)
#else
float distance(v, w)
vector_t v;
vector_t w;
#endif
{
int i;
float d = 0;
for (i=0; i < 3; i++)
d += (v[i] - w[i])*(v[i] - w[i]);
return sqrt(d);
}