File size: 23,171 Bytes
b1b3bae |
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 |
Namespace MathEx.MatrixOps
'/*************************************************************************
'Copyright (c) 1992-2007 The University of Tennessee. All rights reserved.
'Contributors:
' * Sergey Bochkanov (ALGLIB project). Translation from FORTRAN to
' pseudocode.
'See subroutines comments for additional copyrights.
'Redistribution and use in source and binary forms, with or without
'modification, are permitted provided that the following conditions are
'met:
'- Redistributions of source code must retain the above copyright
' notice, this list of conditions and the following disclaimer.
'- Redistributions in binary form must reproduce the above copyright
' notice, this list of conditions and the following disclaimer listed
' in this license in the documentation and/or other materials
' provided with the distribution.
'- Neither the name of the copyright holders nor the names of its
' contributors may be used to endorse or promote products derived from
' this software without specific prior written permission.
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
'"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
'LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
'A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
'OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
'SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
'LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
'DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
'THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
'(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
'OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
'*************************************************************************/
Public Class Determinant
'/*************************************************************************
'Calculation of the determinant of a general matrix
'Input parameters:
' A - matrix, array[0..N-1, 0..N-1]
' N - size of matrix A.
'Result: determinant of matrix A.
' -- ALGLIB --
' Copyright 2005 by Bochkanov Sergey
'*************************************************************************/
Public Shared Function rmatrixdet(ByVal a As Double(,), ByVal n As Integer) As Double
Dim pivots As Integer() = New Integer(0) {}
Dim a2 = DirectCast(a.Clone, Double(,))
MathEx.SysLin.lu.rmatrixlu(a2, n, n, pivots)
Return MathEx.MatrixOps.Determinant.rmatrixludet(a2, pivots, n)
End Function
'/*************************************************************************
'Determinant calculation of the matrix given by its LU decomposition.
'Input parameters:
' A - LU decomposition of the matrix (output of
' RMatrixLU subroutine).
' Pivots - table of permutations which were made during
' the LU decomposition.
' Output of RMatrixLU subroutine.
' N - size of matrix A.
'Result: matrix determinant.
' -- ALGLIB --
' Copyright 2005 by Bochkanov Sergey
'*************************************************************************/
Public Shared Function rmatrixludet(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Double
Dim num As Double = 0
Dim index As Integer = 0
Dim num3 As Integer = 0
num = 1
num3 = 1
index = 0
Do While (index <= (n - 1))
num = (num * a(index, index))
If (pivots(index) <> index) Then
num3 = -num3
End If
index += 1
Loop
Return (num * num3)
End Function
Public Shared Function determinant(ByVal a As Double(,), ByVal n As Integer) As Double
Dim pivots As Integer() = New Integer(0) {}
a = DirectCast(a.Clone, Double(,))
MathEx.SysLin.lu.ludecomposition(a, n, n, pivots)
Return MathEx.MatrixOps.Determinant.determinantlu(a, pivots, n)
End Function
Public Shared Function determinantlu(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Double
Dim num As Double = 0
Dim index As Integer = 0
Dim num3 As Integer = 0
num = 1
num3 = 1
index = 1
Do While (index <= n)
num = (num * a(index, index))
If (pivots(index) <> index) Then
num3 = -num3
End If
index += 1
Loop
Return (num * num3)
End Function
End Class
Public Class Inverse
'/*************************************************************************
'Inversion of a general matrix.
'Input parameters:
' A - matrix. Array whose indexes range within [0..N-1, 0..N-1].
' N - size of matrix A.
'Output parameters:
' A - inverse of matrix A.
' Array whose indexes range within [0..N-1, 0..N-1].
'Result:
' True, if the matrix is not singular.
' False, if the matrix is singular.
' -- ALGLIB --
' Copyright 2005 by Bochkanov Sergey
'*************************************************************************/
Public Shared Function rmatrixinverse(ByRef a As Double(,), ByVal n As Integer) As Boolean
Dim pivots As Integer() = New Integer() {}
MathEx.SysLin.lu.rmatrixlu(a, n, n, pivots)
Return rmatrixluinverse(a, pivots, n)
End Function
'/*************************************************************************
'Inversion of a matrix given by its LU decomposition.
'Input parameters:
' A - LU decomposition of the matrix (output of RMatrixLU subroutine).
' Pivots - table of permutations which were made during the LU decomposition
' (the output of RMatrixLU subroutine).
' N - size of matrix A.
'Output parameters:
' A - inverse of matrix A.
' Array whose indexes range within [0..N-1, 0..N-1].
'Result:
' True, if the matrix is not singular.
' False, if the matrix is singular.
' -- LAPACK routine (version 3.0) --
' Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
' Courant Institute, Argonne National Lab, and Rice University
' February 29, 1992
'*************************************************************************/
Public Shared Function rmatrixluinverse(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Boolean
Dim flag As Boolean = False
Dim numArray As Double() = New Double() {}
Dim index As Integer = 0
Dim num2 As Integer = 0
Dim num3 As Integer = 0
Dim num4 As Double = 0
Dim num5 As Integer = 0
flag = True
If (n <> 0) Then
numArray = New Double(((n - 1) + 1)) {}
If Not TRInverse.rmatrixtrinverse(a, n, True, False) Then
Return False
End If
num2 = (n - 1)
Do While (num2 >= 0)
index = (num2 + 1)
Do While (index <= (n - 1))
numArray(index) = a(index, num2)
a(index, num2) = 0
index += 1
Loop
If (num2 < (n - 1)) Then
index = 0
Do While (index <= (n - 1))
num4 = 0
num5 = (num2 + 1)
Do While (num5 <= (n - 1))
num4 = (num4 + (a(index, num5) * numArray(num5)))
num5 += 1
Loop
a(index, num2) = (a(index, num2) - num4)
index += 1
Loop
End If
num2 -= 1
Loop
num2 = (n - 2)
Do While (num2 >= 0)
num3 = pivots(num2)
If (num3 <> num2) Then
num5 = 0
Do While (num5 <= (n - 1))
numArray(num5) = a(num5, num2)
num5 += 1
Loop
num5 = 0
Do While (num5 <= (n - 1))
a(num5, num2) = a(num5, num3)
num5 += 1
Loop
num5 = 0
Do While (num5 <= (n - 1))
a(num5, num3) = numArray(num5)
num5 += 1
Loop
End If
num2 -= 1
Loop
End If
Return flag
End Function
Public Shared Function inverse(ByRef a As Double(,), ByVal n As Integer) As Boolean
Dim pivots As Integer() = New Integer() {}
MathEx.SysLin.lu.ludecomposition(a, n, n, pivots)
Return MathEx.MatrixOps.Inverse.inverselu(a, pivots, n)
End Function
Public Shared Function inverselu(ByRef a As Double(,), ByRef pivots As Integer(), ByVal n As Integer) As Boolean
Dim flag As Boolean = False
Dim numArray As Double() = New Double() {}
Dim index As Integer = 0
Dim num2 As Integer = 0
Dim num3 As Integer = 0
Dim num4 As Integer = 0
Dim num5 As Double = 0
Dim num6 As Integer = 0
flag = True
If (n <> 0) Then
numArray = New Double((n + 1)) {}
If Not TRInverse.invtriangular(a, n, True, False) Then
Return False
End If
num2 = n
Do While (num2 >= 1)
index = (num2 + 1)
Do While (index <= n)
numArray(index) = a(index, num2)
a(index, num2) = 0
index += 1
Loop
If (num2 < n) Then
num4 = (num2 + 1)
index = 1
Do While (index <= n)
num5 = 0
num6 = num4
Do While (num6 <= n)
num5 = (num5 + (a(index, num6) * numArray(num6)))
num6 += 1
Loop
a(index, num2) = (a(index, num2) - num5)
index += 1
Loop
End If
num2 -= 1
Loop
num2 = (n - 1)
Do While (num2 >= 1)
num3 = pivots(num2)
If (num3 <> num2) Then
num6 = 1
Do While (num6 <= n)
numArray(num6) = a(num6, num2)
num6 += 1
Loop
num6 = 1
Do While (num6 <= n)
a(num6, num2) = a(num6, num3)
num6 += 1
Loop
num6 = 1
Do While (num6 <= n)
a(num6, num3) = numArray(num6)
num6 += 1
Loop
End If
num2 -= 1
Loop
End If
Return flag
End Function
End Class
Public Class TRInverse
'/*************************************************************************
' Triangular matrix inversion
' The subroutine inverts the following types of matrices:
' * upper triangular
' * upper triangular with unit diagonal
' * lower triangular
' * lower triangular with unit diagonal
' In case of an upper (lower) triangular matrix, the inverse matrix will
' also be upper (lower) triangular, and after the end of the algorithm, the
' inverse matrix replaces the source matrix. The elements below (above) the
' main diagonal are not changed by the algorithm.
' If the matrix has a unit diagonal, the inverse matrix also has a unit
' diagonal, and the diagonal elements are not passed to the algorithm.
' Input parameters:
' A - matrix.
' Array whose indexes range within [0..N-1, 0..N-1].
' N - size of matrix A.
' IsUpper - True, if the matrix is upper triangular.
' IsUnitTriangular
' - True, if the matrix has a unit diagonal.
' Output parameters:
' A - inverse matrix (if the problem is not degenerate).
' Result:
' True, if the matrix is not singular.
' False, if the matrix is singular.
' -- LAPACK routine (version 3.0) --
' Univ. of Tennessee, Univ. of California Berkeley, NAG Ltd.,
' Courant Institute, Argonne National Lab, and Rice University
' February 29, 1992
' *************************************************************************/
Public Shared Function rmatrixtrinverse(ByRef a As Double(,), ByVal n As Integer, ByVal isupper As Boolean, ByVal isunittriangular As Boolean) As Boolean
Dim flag As Boolean = False
Dim flag2 As Boolean = False
Dim index As Integer = 0
Dim num2 As Integer = 0
Dim num3 As Double = 0
Dim num4 As Double = 0
Dim numArray As Double() = New Double() {}
Dim num5 As Integer = 0
flag = True
numArray = New Double(((n - 1) + 1)) {}
flag2 = Not isunittriangular
If isupper Then
num2 = 0
Do While (num2 <= (n - 1))
If flag2 Then
If (a(num2, num2) = 0) Then
Return False
End If
a(num2, num2) = (1 / a(num2, num2))
num4 = -a(num2, num2)
Else
num4 = -1
End If
If (num2 > 0) Then
num5 = 0
Do While (num5 <= (num2 - 1))
numArray(num5) = a(num5, num2)
num5 += 1
Loop
index = 0
Do While (index <= (num2 - 1))
If (index < (num2 - 1)) Then
num3 = 0
num5 = (index + 1)
Do While (num5 <= (num2 - 1))
num3 = (num3 + (a(index, num5) * numArray(num5)))
num5 += 1
Loop
Else
num3 = 0
End If
If flag2 Then
a(index, num2) = (num3 + (a(index, index) * numArray(index)))
Else
a(index, num2) = (num3 + numArray(index))
End If
index += 1
Loop
num5 = 0
Do While (num5 <= (num2 - 1))
a(num5, num2) = (num4 * a(num5, num2))
num5 += 1
Loop
End If
num2 += 1
Loop
Return flag
End If
num2 = (n - 1)
Do While (num2 >= 0)
If flag2 Then
If (a(num2, num2) = 0) Then
Return False
End If
a(num2, num2) = (1 / a(num2, num2))
num4 = -a(num2, num2)
Else
num4 = -1
End If
If (num2 < (n - 1)) Then
num5 = (num2 + 1)
Do While (num5 <= (n - 1))
numArray(num5) = a(num5, num2)
num5 += 1
Loop
index = (num2 + 1)
Do While (index <= (n - 1))
If (index > (num2 + 1)) Then
num3 = 0
num5 = (num2 + 1)
Do While (num5 <= (index - 1))
num3 = (num3 + (a(index, num5) * numArray(num5)))
num5 += 1
Loop
Else
num3 = 0
End If
If flag2 Then
a(index, num2) = (num3 + (a(index, index) * numArray(index)))
Else
a(index, num2) = (num3 + numArray(index))
End If
index += 1
Loop
num5 = (num2 + 1)
Do While (num5 <= (n - 1))
a(num5, num2) = (num4 * a(num5, num2))
num5 += 1
Loop
End If
num2 -= 1
Loop
Return flag
End Function
Public Shared Function invtriangular(ByRef a As Double(,), ByVal n As Integer, ByVal isupper As Boolean, ByVal isunittriangular As Boolean) As Boolean
Dim flag As Boolean = False
Dim flag2 As Boolean = False
Dim index As Integer = 0
Dim num2 As Integer = 0
Dim num3 As Integer = 0
Dim num4 As Integer = 0
Dim num5 As Double = 0
Dim num6 As Double = 0
Dim numArray As Double() = New Double() {}
Dim num7 As Integer = 0
flag = True
numArray = New Double((n + 1)) {}
flag2 = Not isunittriangular
If isupper Then
num2 = 1
Do While (num2 <= n)
If flag2 Then
If (a(num2, num2) = 0) Then
Return False
End If
a(num2, num2) = (1 / a(num2, num2))
num6 = -a(num2, num2)
Else
num6 = -1
End If
If (num2 > 1) Then
num3 = (num2 - 1)
num7 = 1
Do While (num7 <= num3)
numArray(num7) = a(num7, num2)
num7 += 1
Loop
index = 1
Do While (index <= (num2 - 1))
If (index < (num2 - 1)) Then
num5 = 0
num7 = (index + 1)
Do While (num7 <= num3)
num5 = (num5 + (a(index, num7) * numArray(num7)))
num7 += 1
Loop
Else
num5 = 0
End If
If flag2 Then
a(index, num2) = (num5 + (a(index, index) * numArray(index)))
Else
a(index, num2) = (num5 + numArray(index))
End If
index += 1
Loop
num7 = 1
Do While (num7 <= num3)
a(num7, num2) = (num6 * a(num7, num2))
num7 += 1
Loop
End If
num2 += 1
Loop
Return flag
End If
num2 = n
Do While (num2 >= 1)
If flag2 Then
If (a(num2, num2) = 0) Then
Return False
End If
a(num2, num2) = (1 / a(num2, num2))
num6 = -a(num2, num2)
Else
num6 = -1
End If
If (num2 < n) Then
num4 = (num2 + 1)
num7 = num4
Do While (num7 <= n)
numArray(num7) = a(num7, num2)
num7 += 1
Loop
index = (num2 + 1)
Do While (index <= n)
If (index > (num2 + 1)) Then
num5 = 0
num7 = num4
Do While (num7 <= (index - 1))
num5 = (num5 + (a(index, num7) * numArray(num7)))
num7 += 1
Loop
Else
num5 = 0
End If
If flag2 Then
a(index, num2) = (num5 + (a(index, index) * numArray(index)))
Else
a(index, num2) = (num5 + numArray(index))
End If
index += 1
Loop
num7 = num4
Do While (num7 <= n)
a(num7, num2) = (num6 * a(num7, num2))
num7 += 1
Loop
End If
num2 -= 1
Loop
Return flag
End Function
End Class
End Namespace |