-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolve_GaussElim.h
More file actions
70 lines (62 loc) · 2.03 KB
/
solve_GaussElim.h
File metadata and controls
70 lines (62 loc) · 2.03 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
/*
Copyright [2024] [Yao Yao]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// Created by yao on 15/01/18.
//
#pragma once
#include <cuda_runtime_api.h>
//@todo: check correctness!
template <typename T>
__host__ __device__ __forceinline__ void solve_GaussElim(const T(&A)[3][3], const T(&b)[3], T(&x)[3]){
T Ab[3][4] = {
A[0][0], A[0][1], A[0][2], b[0],
A[1][0], A[1][1], A[1][2], b[1],
A[2][0], A[2][1], A[2][2], b[2]
};
for (int i = 0; i < 3; i++) {
const T inv = 1.f / Ab[i][i];
for (int j = i + 1; j < 3; j++) {
const T factor = Ab[j][i] * inv;
for (int k = 0; k < 4; k++) {
if(k > i)
Ab[j][k] -= factor * Ab[i][k];
}
}
}
for (int i = 2; i >= 0; i--) {
const T inv = 1.f / Ab[i][i];
Ab[i][3] *= inv;
x[i] = Ab[i][3];
for (int j = i - 1; j >= 0; j--){
const T factor = Ab[j][i];
Ab[j][3] -= factor * Ab[i][3];
}
}
}
template <typename T>
__host__ __device__ __forceinline__ void decompose_LU(const T(&M)[3][3], T(&LU)[3][3]){
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
LU[i][j] = M[i][j];
for (int i = 0; i < 3; i++) {
const T inv = 1.f / LU[i][i];
for (int j = i + 1; j < 3; j++) {
const T factor = LU[j][i] * inv;
LU[j][i] = factor;
for (int k = 0; k < 3; k++) {
if(k > i)
LU[j][k] -= factor * LU[i][k];
}
}
}
}