1 /* 2 Copyright (c) 2013-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Linear equation system solvers 31 * 32 * Description: 33 * A system is given in matrix form: 34 * --- 35 * Ax = b 36 * --- 37 * For example: 38 * --- 39 * x + 3y - 2z = 5 40 * 3x + 5y + 6z = 7 41 * 2x + 4y + 3z = 8 42 * --- 43 * For this system, A (coefficient matrix) will be 44 * --- 45 * [1, 3, -2] 46 * [3, 5, 6] 47 * [2, 4, 3] 48 * --- 49 * And b (right side vector) will be 50 * --- 51 * [5, 7, 8] 52 * --- 53 * x is a vector of unknowns: 54 * --- 55 * [x, y, z] 56 * --- 57 * 58 * Copyright: Timur Gafarov 2013-2021. 59 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 60 * Authors: Timur Gafarov 61 */ 62 module dlib.math.linsolve; 63 64 import std.math; 65 import dlib.math.matrix; 66 import dlib.math.vector; 67 import dlib.math.decomposition; 68 69 // TODO: use arrays instead of Matrix structs to support big systems stored in heap 70 71 /// Solve Ax = b directly using LUP decomposition 72 void solve(T, size_t N)( 73 Matrix!(T,N) a, 74 ref Vector!(T,N) x, 75 Vector!(T,N) b) 76 { 77 Matrix!(T,N) L, U, P; 78 decomposeLUP(a, L, U, P); 79 solveLU(L, U, x, b * P); 80 } 81 82 /// 83 unittest 84 { 85 bool isConsiderZeroTolerant(T) (T f) nothrow 86 { 87 return (abs(f) < 0.0001f); 88 } 89 90 Matrix3f a = matrixf( 91 1, 3, -2, 92 3, 5, 6, 93 2, 4, 3 94 ); 95 Vector3f b = Vector3f(5, 7, 8); 96 Vector3f x = Vector3f(0, 0, 0); 97 98 solve(a, x, b); 99 100 assert(isConsiderZeroTolerant(-15 - x[0])); 101 assert(isConsiderZeroTolerant(8 - x[1])); 102 assert(isConsiderZeroTolerant(2 - x[2])); 103 } 104 105 /// Solve LUx = b directly 106 void solveLU(T, size_t N)( 107 Matrix!(T,N) L, 108 Matrix!(T,N) U, 109 ref Vector!(T,N) x, 110 Vector!(T,N) b) 111 { 112 int i = 0; 113 int j = 0; 114 Vector!(T,N) y; 115 116 // Forward solve Ly = b 117 for (i = 0; i < N; i++) 118 { 119 y[i] = b[i]; 120 for (j = 0; j < i; j++) 121 y[i] -= L[i, j] * y[j]; 122 y[i] /= L[i, i]; 123 } 124 125 // Backward solve Ux = y 126 for (i = N - 1; i >= 0; i--) 127 { 128 x[i] = y[i]; 129 for (j = i + 1; j < N; j++) 130 x[i] -= U[i, j] * x[j]; 131 x[i] /= U[i, i]; 132 } 133 } 134 135 /// Solve Ax = b iteratively using Gauss-Seidel method (implementation is bugged) 136 /* 137 void solveGS(T, size_t N)( 138 Matrix!(T,N) a, 139 ref Vector!(T,N) x, 140 Vector!(T,N) b, 141 uint iterations = 10) 142 { 143 T delta; 144 145 for (int k = 0; k < iterations; ++k) 146 { 147 for (int i = 0; i < N; ++i) 148 { 149 delta = 0.0; 150 151 for (int j = 0; j < i; ++j) 152 delta += a[j, i] * x[j]; 153 154 for (int j = i + 1; j < N; ++j) 155 delta += a[j, i] * x[j]; 156 157 delta = (b[i] - delta) / a[i, i]; 158 x[i] = delta; 159 } 160 } 161 } 162 */