1 #Region "Microsoft.VisualBasic::8e82f2529ec9f0bcd074d4defebc0b2d, Microsoft.VisualBasic.Core\Extensions\Math\Matrix.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module MatrixExtensions
35     
36     '         Function: MatrixMult, ScalarMult
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 '     Copyright Jean-Francois Larcher-Pelland 2013-2014
44 ' Distributed under the Boost Software License, Version 1.0.
45 '    (See accompanying file LICENSE_1_0.txt or copy at
46 '          http://www.boost.org/LICENSE_1_0.txt)
47
48 Namespace Math
49
50     ''' <summary>
51     ''' This class contains methods that perform mathematical operations on matrices.
52     ''' Operations currently supported are matrix multiplication and scalar multiplication.
53     ''' 
54     ''' @author Jean-Francois Larcher-Pelland
55     ''' </summary>
56     Public Module MatrixExtensions
57
58         ''' <summary>
59         ''' Multiplies matrices <i>a</i> and <i>b</i> using the brute-force algorithm.
60         ''' </summary>
61         ''' <param name="a"> The matrix on the left. </param>
62         ''' <param name="b"> The matrix on the right. </param>
63         ''' <returns> The product of the two matrices. </returns>
64         Public Function MatrixMult(a As Double()(), b As Double()()) As Double()()
65             If a Is Nothing OrElse b Is Nothing Then
66                 Throw New ArgumentException("One or both input matrices are null")
67             ElseIf a(0).Length <> b.Length Then
68                 Throw New ArgumentException("Column count of left matrix not equal to row count of right matrix.")
69             End If
70             Dim row As Integer = a.Length
71             Dim col As Integer = b(0).Length
72             Dim inner As Integer = b.Length
73             Dim out As Double()() = MAT(Of Double)(row, col)
74             For i As Integer = 0 To row - 1
75                 For j As Integer = 0 To col - 1
76                     For k As Integer = 0 To inner - 1
77                         out(i)(j) += a(i)(k) * b(k)(j)
78                     Next
79                 Next
80             Next
81             Return out
82         End Function
83
84         ''' <summary>
85         ''' Performs a scalar multiplication on matrix <i>a</i> using scalar value <i>b</i>.
86         ''' </summary>
87         ''' <param name="a"> The matrix to be multiplied. </param>
88         ''' <param name="b"> Scalar value used in the multiplication. </param>
89         ''' <returns> The result of the scalar multiplication. </returns>
90         Public Function ScalarMult(a As Double()(), b As DoubleAs Double()()
91             If a Is Nothing Then
92                 Throw New ArgumentException("Input matrix is null")
93             End If
94             Dim out As Double()() = MAT(Of Double)(a.Length, a(0).Length)
95             For i As Integer = 0 To out.Length - 1
96                 For j As Integer = 0 To out(0).Length - 1
97                     out(i)(j) = a(i)(j) * b
98                 Next
99             Next
100             Return out
101         End Function
102     End Module
103 End Namespace