doc.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright ©2015 The Gonum Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package mat provides implementations of float64 and complex128 matrix
  5. // structures and linear algebra operations on them.
  6. //
  7. // Overview
  8. //
  9. // This section provides a quick overview of the mat package. The following
  10. // sections provide more in depth commentary.
  11. //
  12. // mat provides:
  13. // - Interfaces for Matrix classes (Matrix, Symmetric, Triangular)
  14. // - Concrete implementations (Dense, SymDense, TriDense)
  15. // - Methods and functions for using matrix data (Add, Trace, SymRankOne)
  16. // - Types for constructing and using matrix factorizations (QR, LU)
  17. // - The complementary types for complex matrices, CMatrix, CSymDense, etc.
  18. // In the documentation below, we use "matrix" as a short-hand for all of
  19. // the FooDense types implemented in this package. We use "Matrix" to
  20. // refer to the Matrix interface.
  21. //
  22. // A matrix may be constructed through the corresponding New function. If no
  23. // backing array is provided the matrix will be initialized to all zeros.
  24. // // Allocate a zeroed real matrix of size 3×5
  25. // zero := mat.NewDense(3, 5, nil)
  26. // If a backing data slice is provided, the matrix will have those elements.
  27. // All matrices are all stored in row-major format and users should consider
  28. // this when expressing matrix arithmetic to ensure optimal performance.
  29. // // Generate a 6×6 matrix of random values.
  30. // data := make([]float64, 36)
  31. // for i := range data {
  32. // data[i] = rand.NormFloat64()
  33. // }
  34. // a := mat.NewDense(6, 6, data)
  35. // Operations involving matrix data are implemented as functions when the values
  36. // of the matrix remain unchanged
  37. // tr := mat.Trace(a)
  38. // and are implemented as methods when the operation modifies the receiver.
  39. // zero.Copy(a)
  40. // Note that the input arguments to most functions and methods are interfaces
  41. // rather than concrete types `func Trace(Matrix)` rather than
  42. // `func Trace(*Dense)` allowing flexible use of internal and external
  43. // Matrix types.
  44. //
  45. // When a matrix is the destination or receiver for a function or method,
  46. // the operation will panic if the matrix is not the correct size.
  47. // An exception is if that destination is empty (see below).
  48. //
  49. // Empty matrix
  50. //
  51. // An empty matrix is one that has zero size. Empty matrices are used to allow
  52. // the destination of a matrix operation to assume the correct size automatically.
  53. // This operation will re-use the backing data, if available, or will allocate
  54. // new data if necessary. The IsEmpty method returns whether the given matrix
  55. // is empty. The zero-value of a matrix is empty, and is useful for easily
  56. // getting the result of matrix operations.
  57. // var c mat.Dense // construct a new zero-value matrix
  58. // c.Mul(a, a) // c is automatically adjusted to be the right size
  59. // The Reset method can be used to revert a matrix to an empty matrix.
  60. // Reset should not be used when multiple different matrices share the same backing
  61. // data slice. This can cause unexpected data modifications after being resized.
  62. // An empty matrix can not be sliced even if it does have an adequately sized
  63. // backing data slice, but can be expanded using its Grow method if it exists.
  64. //
  65. // The Matrix Interfaces
  66. //
  67. // The Matrix interface is the common link between the concrete types of real
  68. // matrices, The Matrix interface is defined by three functions: Dims, which
  69. // returns the dimensions of the Matrix, At, which returns the element in the
  70. // specified location, and T for returning a Transpose (discussed later). All of
  71. // the matrix types can perform these behaviors and so implement the interface.
  72. // Methods and functions are designed to use this interface, so in particular the method
  73. // func (m *Dense) Mul(a, b Matrix)
  74. // constructs a *Dense from the result of a multiplication with any Matrix types,
  75. // not just *Dense. Where more restrictive requirements must be met, there are also
  76. // additional interfaces like Symmetric and Triangular. For example, in
  77. // func (s *SymDense) AddSym(a, b Symmetric)
  78. // the Symmetric interface guarantees a symmetric result.
  79. //
  80. // The CMatrix interface plays the same role for complex matrices. The difference
  81. // is that the CMatrix type has the H method instead T, for returning the conjugate
  82. // transpose.
  83. //
  84. // (Conjugate) Transposes
  85. //
  86. // The T method is used for transposition on real matrices, and H is used for
  87. // conjugate transposition on complex matrices. For example, c.Mul(a.T(), b) computes
  88. // c = aᵀ * b. The mat types implement this method implicitly —
  89. // see the Transpose and Conjugate types for more details. Note that some
  90. // operations have a transpose as part of their definition, as in *SymDense.SymOuterK.
  91. //
  92. // Matrix Factorization
  93. //
  94. // Matrix factorizations, such as the LU decomposition, typically have their own
  95. // specific data storage, and so are each implemented as a specific type. The
  96. // factorization can be computed through a call to Factorize
  97. // var lu mat.LU
  98. // lu.Factorize(a)
  99. // The elements of the factorization can be extracted through methods on the
  100. // factorized type, i.e. *LU.UTo. The factorization types can also be used directly,
  101. // as in *Dense.SolveCholesky. Some factorizations can be updated directly,
  102. // without needing to update the original matrix and refactorize,
  103. // as in *LU.RankOne.
  104. //
  105. // BLAS and LAPACK
  106. //
  107. // BLAS and LAPACK are the standard APIs for linear algebra routines. Many
  108. // operations in mat are implemented using calls to the wrapper functions
  109. // in gonum/blas/blas64 and gonum/lapack/lapack64 and their complex equivalents.
  110. // By default, blas64 and lapack64 call the native Go implementations of the
  111. // routines. Alternatively, it is possible to use C-based implementations of the
  112. // APIs through the respective cgo packages and "Use" functions. The Go
  113. // implementation of LAPACK (used by default) makes calls
  114. // through blas64, so if a cgo BLAS implementation is registered, the lapack64
  115. // calls will be partially executed in Go and partially executed in C.
  116. //
  117. // Type Switching
  118. //
  119. // The Matrix abstraction enables efficiency as well as interoperability. Go's
  120. // type reflection capabilities are used to choose the most efficient routine
  121. // given the specific concrete types. For example, in
  122. // c.Mul(a, b)
  123. // if a and b both implement RawMatrixer, that is, they can be represented as a
  124. // blas64.General, blas64.Gemm (general matrix multiplication) is called, while
  125. // instead if b is a RawSymmetricer blas64.Symm is used (general-symmetric
  126. // multiplication), and if b is a *VecDense blas64.Gemv is used.
  127. //
  128. // There are many possible type combinations and special cases. No specific guarantees
  129. // are made about the performance of any method, and in particular, note that an
  130. // abstract matrix type may be copied into a concrete type of the corresponding
  131. // value. If there are specific special cases that are needed, please submit a
  132. // pull-request or file an issue.
  133. //
  134. // Invariants
  135. //
  136. // Matrix input arguments to functions are never directly modified. If an operation
  137. // changes Matrix data, the mutated matrix will be the receiver of a method, or
  138. // will be the first argument to a method or function.
  139. //
  140. // For convenience, a matrix may be used as both a receiver and as an input, e.g.
  141. // a.Pow(a, 6)
  142. // v.SolveVec(a.T(), v)
  143. // though in many cases this will cause an allocation (see Element Aliasing).
  144. // An exception to this rule is Copy, which does not allow a.Copy(a.T()).
  145. //
  146. // Element Aliasing
  147. //
  148. // Most methods in mat modify receiver data. It is forbidden for the modified
  149. // data region of the receiver to overlap the used data area of the input
  150. // arguments. The exception to this rule is when the method receiver is equal to one
  151. // of the input arguments, as in the a.Pow(a, 6) call above, or its implicit transpose.
  152. //
  153. // This prohibition is to help avoid subtle mistakes when the method needs to read
  154. // from and write to the same data region. There are ways to make mistakes using the
  155. // mat API, and mat functions will detect and complain about those.
  156. // There are many ways to make mistakes by excursion from the mat API via
  157. // interaction with raw matrix values.
  158. //
  159. // If you need to read the rest of this section to understand the behavior of
  160. // your program, you are being clever. Don't be clever. If you must be clever,
  161. // blas64 and lapack64 may be used to call the behavior directly.
  162. //
  163. // mat will use the following rules to detect overlap between the receiver and one
  164. // of the inputs:
  165. // - the input implements one of the Raw methods, and
  166. // - the address ranges of the backing data slices overlap, and
  167. // - the strides differ or there is an overlap in the used data elements.
  168. // If such an overlap is detected, the method will panic.
  169. //
  170. // The following cases will not panic:
  171. // - the data slices do not overlap,
  172. // - there is pointer identity between the receiver and input values after
  173. // the value has been untransposed if necessary.
  174. //
  175. // mat will not attempt to detect element overlap if the input does not implement a
  176. // Raw method. Method behavior is undefined if there is undetected overlap.
  177. //
  178. package mat // import "gonum.org/v1/gonum/mat"