Module: JBLAS
- Includes:
- Java
- Defined in:
- lib/jblas.rb,
lib/jblas/java.rb,
lib/jblas/arith.rb,
lib/jblas/errors.rb,
lib/jblas/complex.rb,
lib/jblas/proxies.rb,
lib/jblas/functions.rb,
lib/jblas/mixin_enum.rb,
lib/jblas/mixin_arith.rb,
lib/jblas/mixin_class.rb,
lib/jblas/matrix_mixin.rb,
lib/jblas/mixin_convert.rb,
lib/jblas/mixin_general.rb,
lib/jblas/mixin_complex_matrix.rb,
lib/jblas/mixin_access.rb
Overview
Copyright © 2009-2010, Mikio L. Braun and contributors All rights reserved.
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 in the documentation and/or other materials provided
with the distribution.
* Neither the name of the Technische Universität Berlin 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 HOLDER 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.
Defined Under Namespace
Modules: ComplexMatrixMixin, ComplexMixin, Errors, MatrixAccessMixin, MatrixArithMixin, MatrixClassMixin, MatrixConvertMixin, MatrixEnumMixin, MatrixGeneralMixin, MatrixMixin Classes: ComplexDouble, ComplexDoubleMatrix, ComplexFloat, ComplexFloatMatrix, DoubleMatrix, FloatMatrix, MatrixDotProxy, MatrixElementWiseProxy, ReversedArithmetic
Constant Summary collapse
- I =
ComplexDouble::I
- MatrixFactory =
{ :double => DoubleMatrix, :float => FloatMatrix }
- FUNCTIONS =
{ 'abs' => 'Compute the absolute value.', 'acos' => 'Compute the arcus cosine.', 'asin' => 'Compute the arcus sine.', 'atan' => 'Compute the arcus tangens.', 'cbrt' => 'Compute the cube root.', 'ceil' => 'Round up to the next integer', 'cos' => 'Compute the cosine.', 'cosh' => 'Compute the hyperbolic cosine.', 'exp' => 'Compute the exponential function.', 'floor' => 'Round down to the next integer', 'log' => 'Compute the natural logarithm', 'log10' => 'Compute the base-10 logarithm', 'signum' => 'Compute the sign', 'sin' => 'Compute the sine', 'sinh' => 'Compute the hyperbolic sine', 'sqrt' => 'Compute the square root', 'tan' => 'Compute the tangens', 'tanh' => 'Compute the hyperbolic tangens' }
- PI =
Math::PI
Class Method Summary collapse
-
.check_matrix_square(m) ⇒ Object
Check whether matrix is square.
-
.cholesky(x) ⇒ Object
Compute the Cholesky decomposition of a square, positive definite matrix.
-
.cumsum(x) ⇒ Object
Compute the cumulative sum of a vector.
-
.det(x) ⇒ Object
Compute the determinant of a square matrix.
-
.diag(x) ⇒ Object
Return the diagonal of a matrix or return a matrix whose diagonal is specified by the vector.
-
.eig(x) ⇒ Object
Computing the eigenvalues of matrix.
-
.eigv(x) ⇒ Object
Computing the eigenvectors of matrix.
-
.eye(n) ⇒ Object
Return the identity matrix of given size.
-
.hcat(*args) ⇒ Object
Returns the horizontal concatenation of all arguments.
-
.idx(i) ⇒ Object
Convenience function for converting arbitrary objects into indices.
-
.linspace(a, b, n) ⇒ Object
Generate an array of n linearly spaced points starting at a, ending at b.
-
.logspace(a, b, n) ⇒ Object
Generate an array of n logarithmically spaced points starting at 10^a and ending at 10^b.
-
.lup(x) ⇒ Object
Compute the LU factorization with pivoting.
-
.mat(type = :double) ⇒ Object
Construct a matrix.
-
.max(x) ⇒ Object
Return the largest element of a vector.
-
.mean(x) ⇒ Object
The mean of a vector.
-
.min(x) ⇒ Object
Return the smallest element of a vector.
-
.norm(x, type = 2) ⇒ Object
Compute the norm of a vector.
-
.ones(n, m = nil) ⇒ Object
Construct a matrix of all ones.
-
.pow(x, y) ⇒ Object
Computer power.
-
.powi(x, y) ⇒ Object
(also: pow!)
Compute power, in-place.
-
.rand(n = 1, m = nil) ⇒ Object
Construct a matrix or vector with elements randomly drawn uniformly from [0, 1].
-
.randn(n = 1, m = nil) ⇒ Object
Construct a matrix or vector with elements randomly drawn from a Gaussian distribution with mean 0 and variance 1.
-
.range(a, s, b) ⇒ Object
Generate a range from a to b with step size s.
-
.repmat(m, r, c) ⇒ Object
Replicate a matrix a certain number of horizontal and vertical times.
-
.sinc(x) ⇒ Object
The sinc function (Defined as sin(x)/x if x != 0 and 1 else).
-
.solve(a, b) ⇒ Object
Solve the linear equation a*x = b.
-
.sort(x) ⇒ Object
Sort the elements of a vector.
-
.sum(x) ⇒ Object
The sum of a vector.
-
.svd(x, sparse = false) ⇒ Object
Compute the singular value decompositon of a rectangular matrix.
-
.svdv(x) ⇒ Object
Compute the singular values of a rectangular matrix.
-
.tictoc ⇒ Object
Times a block and returns the elapsed time in seconds.
-
.trace(x) ⇒ Object
Computing the trace of a matrix.
-
.vcat(*args) ⇒ Object
Returns the vertical concatenation of all arguments.
-
.zeros(n, m = nil) ⇒ Object
Construct a matrix of all zeros.
Class Method Details
.check_matrix_square(m) ⇒ Object
Check whether matrix is square. Raises Errors::MatrixNotSquare if it isn’t.
47 48 49 50 51 |
# File 'lib/jblas/functions.rb', line 47 def check_matrix_square(m) unless m.square? raise Errors::MatrixNotSquare end end |
.cholesky(x) ⇒ Object
Compute the Cholesky decomposition of a square, positive definite matrix.
Returns a matrix an upper triangular matrix u such that u * u.t is the original matrix.
345 346 347 348 349 350 351 352 |
# File 'lib/jblas/functions.rb', line 345 def cholesky(x) check_matrix_square(x) begin Decompose.cholesky(x) rescue org.jblas.exceptions.LapackPositivityException raise Errors::MatrixNotPositiveDefinite end end |
.cumsum(x) ⇒ Object
Compute the cumulative sum of a vector.
327 328 329 |
# File 'lib/jblas/functions.rb', line 327 def cumsum(x) x.cumulative_sum end |
.det(x) ⇒ Object
Compute the determinant of a square matrix.
Internally computes the LU factorization and then takes the product of the diagonal elements.
358 359 360 361 362 |
# File 'lib/jblas/functions.rb', line 358 def det(x) check_matrix_square(x) l, u, p = lup(x) return u.diag.prod end |
.diag(x) ⇒ Object
Return the diagonal of a matrix or return a matrix whose diagonal is specified by the vector
101 102 103 104 105 106 107 |
# File 'lib/jblas/functions.rb', line 101 def diag(x) if x.vector? mat.diag(x) else x.diag end end |
.eig(x) ⇒ Object
Computing the eigenvalues of matrix.
202 203 204 205 206 207 208 209 |
# File 'lib/jblas/functions.rb', line 202 def eig(x) check_matrix_square(x) if x.symmetric? return Eigen.symmetric_eigenvalues(x) else return Eigen.eigenvalues(x) end end |
.eigv(x) ⇒ Object
Computing the eigenvectors of matrix.
u, v = eigv(x)
u are the eigenvalues and v is a diagonal matrix containing the eigenvalues
217 218 219 220 221 222 223 224 |
# File 'lib/jblas/functions.rb', line 217 def eigv(x) check_matrix_square(x) if x.symmetric? return Eigen.symmetric_eigenvectors(x).to_a else return Eigen.eigenvectors(x).to_a end end |
.eye(n) ⇒ Object
Return the identity matrix of given size.
110 111 112 |
# File 'lib/jblas/functions.rb', line 110 def eye(n) mat.eye(n) end |
.hcat(*args) ⇒ Object
Returns the horizontal concatenation of all arguments.
See also MatrixGeneralMixin#hcat.
246 247 248 |
# File 'lib/jblas/functions.rb', line 246 def hcat(*args) args.map {|s| s.to_matrix}.inject{|s,x| s = s.hcat x} end |
.idx(i) ⇒ Object
Convenience function for converting arbitrary objects into indices.
152 153 154 |
# File 'lib/jblas/mixin_access.rb', line 152 def idx(i) i.to_indices end |
.linspace(a, b, n) ⇒ Object
Generate an array of n linearly spaced points starting at a, ending at b.
270 271 272 273 274 275 |
# File 'lib/jblas/functions.rb', line 270 def linspace(a, b, n) (0...n).map do |i| t = Float(i) / (n-1) (1-t)*a + t*b end end |
.logspace(a, b, n) ⇒ Object
Generate an array of n logarithmically spaced points starting at 10^a and ending at 10^b.
279 280 281 282 283 284 |
# File 'lib/jblas/functions.rb', line 279 def logspace(a, b, n) (0...n).map do |i| t = Float(i) / (n-1) 10**( (1-t)*a + t*b ) end end |
.lup(x) ⇒ Object
Compute the LU factorization with pivoting.
Returns matrices l, u, p
334 335 336 337 338 |
# File 'lib/jblas/functions.rb', line 334 def lup(x) check_matrix_square(x) result = Decompose.lu(x) return result.l, result.u, result.p end |
.mat(type = :double) ⇒ Object
Construct a matrix. Use it like this:
mat[1,2,3] -> constructs a column vector
mat[[1,2,3],[4,5,6]] -> construct a rectangular matrix
69 70 71 |
# File 'lib/jblas/functions.rb', line 69 def mat(type=:double) MatrixFactory[type] end |
.max(x) ⇒ Object
Return the largest element of a vector.
317 318 319 |
# File 'lib/jblas/functions.rb', line 317 def max(x) x.max end |
.mean(x) ⇒ Object
The mean of a vector.
302 303 304 |
# File 'lib/jblas/functions.rb', line 302 def mean(x) x.to_mat.mean end |
.min(x) ⇒ Object
Return the smallest element of a vector.
312 313 314 |
# File 'lib/jblas/functions.rb', line 312 def min(x) x.min end |
.norm(x, type = 2) ⇒ Object
Compute the norm of a vector
232 233 234 235 236 237 238 239 240 241 |
# File 'lib/jblas/functions.rb', line 232 def norm(x, type=2) case type when 1 x.norm1 when 2 x.norm2 when :inf x.normmax end end |
.ones(n, m = nil) ⇒ Object
Construct a matrix of all ones.
ones(2, 3) == mat[[1, 1, 1],[1, 1, 1]]
If the second argument is omitted, a column vector is constructed.
91 92 93 94 95 96 97 |
# File 'lib/jblas/functions.rb', line 91 def ones(n,m=nil) if m mat.ones(n,m) else mat.ones(n) end end |
.pow(x, y) ⇒ Object
Computer power. pow(x, y) = x ^ y.
180 181 182 183 184 |
# File 'lib/jblas/functions.rb', line 180 def pow(x, y) x = x.to_mat if Array === x y = y.to_amt if Array === y return MatrixFunctions.pow(x, y) end |
.powi(x, y) ⇒ Object Also known as: pow!
Compute power, in-place.
187 188 189 |
# File 'lib/jblas/functions.rb', line 187 def powi(x, y) MatrixFunctions.powi(x, y) end |
.rand(n = 1, m = nil) ⇒ Object
Construct a matrix or vector with elements randomly drawn uniformly from [0, 1].
If the second argument is omitted, a column vector is constructed.
118 119 120 121 122 123 124 |
# File 'lib/jblas/functions.rb', line 118 def rand(n=1,m=nil) if m mat.rand(n,m) else mat.rand(n) end end |
.randn(n = 1, m = nil) ⇒ Object
Construct a matrix or vector with elements randomly drawn from a Gaussian distribution with mean 0 and variance 1. With one argument, construct a vector, with two a matrix.
If the second argument is omitted, a column vector is constructed.
131 132 133 134 135 136 137 |
# File 'lib/jblas/functions.rb', line 131 def randn(n=1,m=nil) if m mat.randn(n,m) else mat.randn(n) end end |
.range(a, s, b) ⇒ Object
Generate a range from a to b with step size s.
287 288 289 290 291 292 293 294 |
# File 'lib/jblas/functions.rb', line 287 def range(a, s, b) x = [] while a < b x << a a += s end return x end |
.repmat(m, r, c) ⇒ Object
Replicate a matrix a certain number of horizontal and vertical times.
For example:
repmat(mat[[1,2,3],[4,5,6]], 1, 2)
=> [1.0, 2.0, 3.0, 1.0, 2.0, 3.0; 4.0, 5.0, 6.0, 4.0, 5.0, 6.0]
264 265 266 |
# File 'lib/jblas/functions.rb', line 264 def repmat(m, r, c) m.to_matrix.repmat(r, c) end |
.sinc(x) ⇒ Object
The sinc function (Defined as sin(x)/x if x != 0 and 1 else).
322 323 324 |
# File 'lib/jblas/functions.rb', line 322 def sinc(x) sin(x) / x end |
.solve(a, b) ⇒ Object
Solve the linear equation a*x = b. See also MatrixMixin#solve.
227 228 229 |
# File 'lib/jblas/functions.rb', line 227 def solve(a, b) a.solve b end |
.sort(x) ⇒ Object
Sort the elements of a vector.
307 308 309 |
# File 'lib/jblas/functions.rb', line 307 def sort(x) x.sort end |
.sum(x) ⇒ Object
The sum of a vector. See also
297 298 299 |
# File 'lib/jblas/functions.rb', line 297 def sum(x) x.sum end |
.svd(x, sparse = false) ⇒ Object
Compute the singular value decompositon of a rectangular matrix.
Returns matrices u, s, v such that u*diag(s)*v.t is the original matrix. Put differently, the columns of u are the left singular vectors, the columns of v are the right singular vectors, and s are the singular values.
371 372 373 374 375 376 377 378 |
# File 'lib/jblas/functions.rb', line 371 def svd(x, sparse=false) if sparse usv = Singular.sparseSVD(x) else usv = Singular.fullSVD(x) end return usv.to_a end |
.svdv(x) ⇒ Object
Compute the singular values of a rectangular matrix.
381 382 383 |
# File 'lib/jblas/functions.rb', line 381 def svdv(x) Singular.SVDValues(x) end |
.tictoc ⇒ Object
Times a block and returns the elapsed time in seconds.
For example:
tictoc { n = 100; x = randn(n, n); u, d = eigv(x) }
Times how long it takes to generate a random 100*100 matrix and compute its eigendecomposition.
399 400 401 402 403 |
# File 'lib/jblas/functions.rb', line 399 def tictoc saved_time = Time.now yield return Time.now - saved_time end |
.trace(x) ⇒ Object
Computing the trace of a matrix.
196 197 198 199 |
# File 'lib/jblas/functions.rb', line 196 def trace(x) JBLAS::check_matrix_square(x) x.diag.sum end |
.vcat(*args) ⇒ Object
Returns the vertical concatenation of all arguments
See also MatrixGeneralMixin#vcat.
253 254 255 |
# File 'lib/jblas/functions.rb', line 253 def vcat(*args) args.map {|s| s.to_matrix}.inject{|s,x| s = s.vcat x} end |
.zeros(n, m = nil) ⇒ Object
Construct a matrix of all zeros.
zeros(2, 3) == mat[[0, 0, 0],[0, 0, 0]]
If the second argument is omitted, a column vector is constructed.
78 79 80 81 82 83 84 |
# File 'lib/jblas/functions.rb', line 78 def zeros(n,m=nil) if m mat.zeros(n,m) else mat.zeros(n) end end |