-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools_mpfr.c
More file actions
105 lines (96 loc) · 2.7 KB
/
tools_mpfr.c
File metadata and controls
105 lines (96 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
//
// Created by Jad Yehya and Ezzedine Chahine on 24/10/2022.
//
#include "tools_mpfr.h"
#include <mpfr.h>
/**
* Swaps two rows of a mpfr_t matrix
* @param A : the matrix
* @param row1 : the first row
* @param row2 : the second row
* @param n : size of the matrix
*/
void swap_row_mpfr(mpfr_t **A, int row1, int row2, int n) {
mpfr_t temp;
mpfr_init(temp);
for (int i = 0; i < n; ++i) {
mpfr_set(temp, A[row1][i], MPFR_RNDN);
mpfr_set(A[row1][i], A[row2][i], MPFR_RNDN);
mpfr_set(A[row2][i], temp, MPFR_RNDN);
}
mpfr_clear(temp);
}
/**
* Swaps two columns of a mpfr_t matrix
* @param A : the matrix
* @param col1 : the first column
* @param col2 : the second column
* @param n : size of the matrix
*/
void swap_columns_mpfr(mpfr_t **A, int col1, int col2, int n) {
mpfr_t temp;
mpfr_init(temp);
for (int i = 0; i < n; ++i) {
mpfr_set(temp, A[i][col1], MPFR_RNDN);
mpfr_set(A[i][col1], A[i][col2], MPFR_RNDN);
mpfr_set(A[i][col2], temp, MPFR_RNDN);
}
mpfr_clear(temp);
}
/**
* Print a matrix of mpfr_t
* @param A : Matrix to print
* @param n : Size of the matrix
*/
void print_mpfr_matrix(mpfr_t **A, int n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
mpfr_printf("%Rf\t", A[i][j]);
}
printf("\n");
}
}
/**
* Multiply two matrices of mpfr_t
* @param A : the first matrix
* @param B : the second matrix
* @param n : the size of the matrices
* @return the result of the multiplication
*/
mpfr_t **multiply_matrices_mpfr(mpfr_t **A, mpfr_t **B, int n) {
mpfr_t **C = (mpfr_t **) malloc(n * sizeof(mpfr_t *));
for (int i = 0; i < n; ++i) {
C[i] = (mpfr_t *) malloc(n * sizeof(mpfr_t));
}
for (int i = 0; i < n; ++i) { // Rows of A
for (int j = 0; j < n; ++j) { // Columns of B
mpfr_init(C[i][j]);
mpfr_set_d(C[i][j], 0, MPFR_RNDN);
for (int k = 0; k < n; ++k) { // Columns of A
mpfr_t temp;
mpfr_init2(temp, 128);
mpfr_mul(temp, A[i][k], B[k][j], MPFR_RNDN);
mpfr_add(C[i][j], C[i][j], temp, MPFR_RNDN);
}
}
}
return C;
}
/**
* Check if 2 mpfr_t matrices are equal
* @param A : the first matrix
* @param B : the second matrix
* @param n : the size of the matrices
* @return 0 if they are equal, 1 otherwise
*/
int check_equal_mpfr(mpfr_t **A, mpfr_t **B, int n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (mpfr_cmp(A[i][j], B[i][j]) != 0) {
fprintf(stderr, "Matrices are not equal");
return 1;
}
}
}
return 0;
}