jblas-ruby 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,92 @@
1
+ # Mixins for complex numbers. Defines JBLAS::ComplexMixin. See also
2
+ # ComplexDouble and ComplexFloat.
3
+
4
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
5
+ # All rights reserved.
6
+ #
7
+ # Redistribution and use in source and binary forms, with or without
8
+ # modification, are permitted provided that the following conditions are
9
+ # met:
10
+ #
11
+ # * Redistributions of source code must retain the above copyright
12
+ # notice, this list of conditions and the following disclaimer.
13
+ #
14
+ # * Redistributions in binary form must reproduce the above
15
+ # copyright notice, this list of conditions and the following
16
+ # disclaimer in the documentation and/or other materials provided
17
+ # with the distribution.
18
+ #
19
+ # * Neither the name of the Technische Universität Berlin nor the
20
+ # names of its contributors may be used to endorse or promote
21
+ # products derived from this software without specific prior
22
+ # written permission.
23
+ #
24
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
+
36
+ module JBLAS
37
+ # Syntactic sugar for complex numbers.
38
+ #
39
+ # Defines the arithmetic operators *, +, -, /, and coercion such that
40
+ # the complex numbers interact well with the builtin numerics.
41
+ module ComplexMixin
42
+ # Convert to a string.
43
+ def to_s
44
+ "#{real} + #{imag}i"
45
+ end
46
+
47
+ # Same as to_s.
48
+ def inspect
49
+ to_s
50
+ end
51
+
52
+ # Multiply complex values.
53
+ def *(o); mul(o); end
54
+ # Add complex values.
55
+ def +(o); add(o); end
56
+ # Subtract complex values.
57
+ def -(o); sub(o); end
58
+ # Divide complex values.
59
+ def /(o); div(o); end
60
+ # Negative complex values.
61
+ def -@; neg; end
62
+
63
+ # Subtract with swapped operands.
64
+ #
65
+ # This means that a.rsub(b) = b - a. This method is necessary to
66
+ # make coercion work.
67
+ def rsub(o); -self + o; end
68
+ # Divide with swapped operands.
69
+ #
70
+ # This means that a.rdiv(b) = b / a. This method is necessary to
71
+ # make coercion work.
72
+ def rdiv(o); inv * o; end
73
+
74
+ def coerce(o) #:nodoc:
75
+ unless self.class === o
76
+ [ReversedArithmetic.new(self), o]
77
+ end
78
+ end
79
+
80
+ # Compute the square root
81
+ def sqrt; JAVA_METHOD; end
82
+
83
+ # Get the length of the complex number
84
+ def abs; JAVA_METHOD; end
85
+
86
+ # Get the angle of the complex number
87
+ def arg; JAVA_METHOD; end
88
+
89
+ # Get the conjugate of the complex number
90
+ def conj; JAVA_METHOD; end
91
+ end
92
+ end
@@ -0,0 +1,52 @@
1
+ # Errors used by JBLAS. Contains the module JBLAS::Errors
2
+
3
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are
8
+ # met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright
11
+ # notice, this list of conditions and the following disclaimer.
12
+ #
13
+ # * Redistributions in binary form must reproduce the above
14
+ # copyright notice, this list of conditions and the following
15
+ # disclaimer in the documentation and/or other materials provided
16
+ # with the distribution.
17
+ #
18
+ # * Neither the name of the Technische Universität Berlin nor the
19
+ # names of its contributors may be used to endorse or promote
20
+ # products derived from this software without specific prior
21
+ # written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ module JBLAS
36
+ # This modules contains special exceptions raised by JBLAS.
37
+ module Errors
38
+ # The routine required a positive definite matrix.
39
+ class MatrixNotPositiveDefinite < ArgumentError
40
+ def initialize
41
+ super "Matrix is not positive definite."
42
+ end
43
+ end
44
+
45
+ # The routine required a square matrix.
46
+ class MatrixNotSquare < ArgumentError
47
+ def initialize
48
+ super "Matrix is not square."
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,114 @@
1
+ # Define extensions to Object, Array, Numeric and Range to play well with
2
+ # matrices.
3
+ #
4
+ # Main additions are: Object#to_mat, and Numeric#columns, Numeric#rows, and so on.
5
+
6
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
7
+ # All rights reserved.
8
+ #
9
+ # Redistribution and use in source and binary forms, with or without
10
+ # modification, are permitted provided that the following conditions are
11
+ # met:
12
+ #
13
+ # * Redistributions of source code must retain the above copyright
14
+ # notice, this list of conditions and the following disclaimer.
15
+ #
16
+ # * Redistributions in binary form must reproduce the above
17
+ # copyright notice, this list of conditions and the following
18
+ # disclaimer in the documentation and/or other materials provided
19
+ # with the distribution.
20
+ #
21
+ # * Neither the name of the Technische Universität Berlin nor the
22
+ # names of its contributors may be used to endorse or promote
23
+ # products derived from this software without specific prior
24
+ # written permission.
25
+ #
26
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37
+
38
+ # Extensions to Ruby's Object class
39
+ class Object
40
+ # convert object to a matrix through to_mat
41
+ def to_matrix
42
+ to_mat
43
+ end
44
+ end
45
+
46
+ # Extensions to Ruby's Array class
47
+ class Array
48
+ # Convert array to matrix.
49
+ def to_mat
50
+ JBLAS::DoubleMatrix[*self]
51
+ end
52
+
53
+ # Convert array to matrix string representation
54
+ def to_matlab_string
55
+ '[' + map{|e| e.to_s}.join(', ') + ']'
56
+ end
57
+ end
58
+
59
+ # Extensions to Ruby's Range class
60
+ class Range
61
+ # Convert to matrix object.
62
+ def to_mat
63
+ self.to_a.to_mat
64
+ end
65
+ end
66
+
67
+ # Extensions to Ruby's Numeric class
68
+ class Numeric
69
+ # Convert to matlab. Just returns the object itself. Other
70
+ # methods are added below to make a Numeric object look like
71
+ # a matrix.
72
+ def to_mat
73
+ self # JBLAS::DoubleMatrix[self]
74
+ end
75
+
76
+ # Convert to matrix.
77
+ def to_matrix
78
+ JBLAS::DoubleMatrix[self]
79
+ end
80
+
81
+ # Returns number of columns (= 1).
82
+ def columns
83
+ 1
84
+ end
85
+
86
+ # Returns number of rows (= 1).
87
+ def rows
88
+ 1
89
+ end
90
+
91
+ # Element access (always returns the Numeric object itself)
92
+ def [](*args)
93
+ self
94
+ end
95
+
96
+ # Is a scalar? (always true)
97
+ def scalar?
98
+ true
99
+ end
100
+
101
+ # Transpose (is the same object).
102
+ def t
103
+ self
104
+ end
105
+
106
+ # Length of the Numeric object (= 1).
107
+ def length
108
+ 1
109
+ end
110
+
111
+ def i
112
+ ComplexDouble.new(0, self)
113
+ end
114
+ end
@@ -0,0 +1,404 @@
1
+ # Functions within JBLAS, for example, sin, cos, and so on. See JBLAS.
2
+
3
+ # Copyright (c) 2009-2010, Mikio L. Braun and contributors
4
+ # All rights reserved.
5
+ #
6
+ # Redistribution and use in source and binary forms, with or without
7
+ # modification, are permitted provided that the following conditions are
8
+ # met:
9
+ #
10
+ # * Redistributions of source code must retain the above copyright
11
+ # notice, this list of conditions and the following disclaimer.
12
+ #
13
+ # * Redistributions in binary form must reproduce the above
14
+ # copyright notice, this list of conditions and the following
15
+ # disclaimer in the documentation and/or other materials provided
16
+ # with the distribution.
17
+ #
18
+ # * Neither the name of the Technische Universität Berlin nor the
19
+ # names of its contributors may be used to endorse or promote
20
+ # products derived from this software without specific prior
21
+ # written permission.
22
+ #
23
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
+ # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
+ # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26
+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27
+ # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
+ # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
+ # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
+ # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
+ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
+ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
+
35
+ require 'jblas/errors'
36
+
37
+ module JBLAS
38
+ module_function
39
+
40
+ ######################################################################
41
+ #
42
+ # Helper methods
43
+ #
44
+ ######################################################################
45
+
46
+ # Check whether matrix is square. Raises Errors::MatrixNotSquare if it isn't.
47
+ def check_matrix_square(m)
48
+ unless m.square?
49
+ raise Errors::MatrixNotSquare
50
+ end
51
+ end
52
+
53
+ ######################################################################
54
+ #
55
+ # Module Functions
56
+ #
57
+ ######################################################################
58
+
59
+
60
+ MatrixFactory = {
61
+ :double => DoubleMatrix,
62
+ :float => FloatMatrix
63
+ }
64
+
65
+ # Construct a matrix. Use it like this:
66
+ #
67
+ # mat[1,2,3] -> constructs a column vector
68
+ # mat[[1,2,3],[4,5,6]] -> construct a rectangular matrix
69
+ def mat(type=:double)
70
+ MatrixFactory[type]
71
+ end
72
+
73
+ # Construct a matrix of all zeros.
74
+ #
75
+ # zeros(2, 3) == mat[[0, 0, 0],[0, 0, 0]]
76
+ #
77
+ # If the second argument is omitted, a column vector is constructed.
78
+ def zeros(n,m=nil)
79
+ if m
80
+ mat.zeros(n,m)
81
+ else
82
+ mat.zeros(n)
83
+ end
84
+ end
85
+
86
+ # Construct a matrix of all ones.
87
+ #
88
+ # ones(2, 3) == mat[[1, 1, 1],[1, 1, 1]]
89
+ #
90
+ # If the second argument is omitted, a column vector is constructed.
91
+ def ones(n,m=nil)
92
+ if m
93
+ mat.ones(n,m)
94
+ else
95
+ mat.ones(n)
96
+ end
97
+ end
98
+
99
+ # Return the diagonal of a matrix or return a matrix whose diagonal
100
+ # is specified by the vector
101
+ def diag(x)
102
+ if x.vector?
103
+ mat.diag(x)
104
+ else
105
+ x.diag
106
+ end
107
+ end
108
+
109
+ # Return the identity matrix of given size.
110
+ def eye(n)
111
+ mat.eye(n)
112
+ end
113
+
114
+ # Construct a matrix or vector with elements randomly
115
+ # drawn uniformly from [0, 1].
116
+ #
117
+ # If the second argument is omitted, a column vector is constructed.
118
+ def rand(n=1,m=nil)
119
+ if m
120
+ mat.rand(n,m)
121
+ else
122
+ mat.rand(n)
123
+ end
124
+ end
125
+
126
+ # Construct a matrix or vector with elements randomly drawn from a
127
+ # Gaussian distribution with mean 0 and variance 1. With one
128
+ # argument, construct a vector, with two a matrix.
129
+ #
130
+ # If the second argument is omitted, a column vector is constructed.
131
+ def randn(n=1,m=nil)
132
+ if m
133
+ mat.randn(n,m)
134
+ else
135
+ mat.randn(n)
136
+ end
137
+ end
138
+
139
+ FUNCTIONS = {
140
+ 'abs' => 'Compute the absolute value.',
141
+ 'acos' => 'Compute the arcus cosine.',
142
+ 'asin' => 'Compute the arcus sine.',
143
+ 'atan' => 'Compute the arcus tangens.',
144
+ 'cbrt' => 'Compute the cube root.',
145
+ 'ceil' => 'Round up to the next integer',
146
+ 'cos' => 'Compute the cosine.',
147
+ 'cosh' => 'Compute the hyperbolic cosine.',
148
+ 'exp' => 'Compute the exponential function.',
149
+ 'floor' => 'Round down to the next integer',
150
+ 'log' => 'Compute the natural logarithm',
151
+ 'log10' => 'Compute the base-10 logarithm',
152
+ 'signum' => 'Compute the sign',
153
+ 'sin' => 'Compute the sine',
154
+ 'sinh' => 'Compute the hyperbolic sine',
155
+ 'sqrt' => 'Compute the square root',
156
+ 'tan' => 'Compute the tangens',
157
+ 'tanh' => 'Compute the hyperbolic tangens'
158
+ }
159
+
160
+ FUNCTIONS.each_key do |fn|
161
+ module_eval <<-EOS
162
+ def #{fn}(x)
163
+ if Array === x
164
+ x = x.to_mat
165
+ end
166
+ s = MatrixFunctions.#{fn}(x)
167
+ end
168
+ module_function :#{fn}
169
+
170
+ def #{fn}i(x)
171
+ MatrixFunctions.#{fn}i(x)
172
+ end
173
+ module_function :#{fn}i
174
+
175
+ alias #{fn}! #{fn}i
176
+ EOS
177
+ end
178
+
179
+ # Computer power. pow(x, y) = x ^ y.
180
+ def pow(x, y)
181
+ x = x.to_mat if Array === x
182
+ y = y.to_amt if Array === y
183
+ return MatrixFunctions.pow(x, y)
184
+ end
185
+
186
+ # Compute power, in-place.
187
+ def powi(x, y)
188
+ MatrixFunctions.powi(x, y)
189
+ end
190
+
191
+ alias pow! powi
192
+
193
+ PI = Math::PI
194
+
195
+ # Computing the trace of a matrix.
196
+ def trace(x)
197
+ JBLAS::check_matrix_square(x)
198
+ x.diag.sum
199
+ end
200
+
201
+ # Computing the eigenvalues of matrix.
202
+ def eig(x)
203
+ check_matrix_square(x)
204
+ if x.symmetric?
205
+ return Eigen.symmetric_eigenvalues(x)
206
+ else
207
+ return Eigen.eigenvalues(x)
208
+ end
209
+ end
210
+
211
+ # Computing the eigenvectors of matrix.
212
+ #
213
+ # u, v = eigv(x)
214
+ #
215
+ # u are the eigenvalues and v is a diagonal matrix containing the
216
+ # eigenvalues
217
+ def eigv(x)
218
+ check_matrix_square(x)
219
+ if x.symmetric?
220
+ return Eigen.symmetric_eigenvectors(x).to_a
221
+ else
222
+ return Eigen.eigenvectors(x).to_a
223
+ end
224
+ end
225
+
226
+ # Solve the linear equation a*x = b. See also MatrixMixin#solve.
227
+ def solve(a, b)
228
+ a.solve b
229
+ end
230
+
231
+ # Compute the norm of a vector
232
+ def norm(x, type=2)
233
+ case type
234
+ when 1
235
+ x.norm1
236
+ when 2
237
+ x.norm2
238
+ when :inf
239
+ x.normmax
240
+ end
241
+ end
242
+
243
+ # Returns the horizontal concatenation of all arguments.
244
+ #
245
+ # See also MatrixGeneralMixin#hcat.
246
+ def hcat(*args)
247
+ args.map {|s| s.to_matrix}.inject{|s,x| s = s.hcat x}
248
+ end
249
+
250
+ # Returns the vertical concatenation of all arguments
251
+ #
252
+ # See also MatrixGeneralMixin#vcat.
253
+ def vcat(*args)
254
+ args.map {|s| s.to_matrix}.inject{|s,x| s = s.vcat x}
255
+ end
256
+
257
+ # Replicate a matrix a certain number of horizontal and
258
+ # vertical times.
259
+ #
260
+ # For example:
261
+ #
262
+ # repmat(mat[[1,2,3],[4,5,6]], 1, 2)
263
+ # => [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
+ def repmat(m, r, c)
265
+ m.to_matrix.repmat(r, c)
266
+ end
267
+
268
+ # Generate an array of n linearly spaced points starting at a, ending
269
+ # at b.
270
+ def linspace(a, b, n)
271
+ (0...n).map do |i|
272
+ t = Float(i) / (n-1)
273
+ (1-t)*a + t*b
274
+ end
275
+ end
276
+
277
+ # Generate an array of n logarithmically spaced points starting at
278
+ # 10^a and ending at 10^b.
279
+ def logspace(a, b, n)
280
+ (0...n).map do |i|
281
+ t = Float(i) / (n-1)
282
+ 10**( (1-t)*a + t*b )
283
+ end
284
+ end
285
+
286
+ # Generate a range from a to b with step size s.
287
+ def range(a, s, b)
288
+ x = []
289
+ while a < b
290
+ x << a
291
+ a += s
292
+ end
293
+ return x
294
+ end
295
+
296
+ # The sum of a vector. See also
297
+ def sum(x)
298
+ x.sum
299
+ end
300
+
301
+ # The mean of a vector.
302
+ def mean(x)
303
+ x.to_mat.mean
304
+ end
305
+
306
+ # Sort the elements of a vector.
307
+ def sort(x)
308
+ x.sort
309
+ end
310
+
311
+ # Return the smallest element of a vector.
312
+ def min(x)
313
+ x.min
314
+ end
315
+
316
+ # Return the largest element of a vector.
317
+ def max(x)
318
+ x.max
319
+ end
320
+
321
+ # The sinc function (Defined as sin(x)/x if x != 0 and 1 else).
322
+ def sinc(x)
323
+ sin(x) / x
324
+ end
325
+
326
+ # Compute the cumulative sum of a vector.
327
+ def cumsum(x)
328
+ x.cumulative_sum
329
+ end
330
+
331
+ # Compute the LU factorization with pivoting.
332
+ #
333
+ # Returns matrices l, u, p
334
+ def lup(x)
335
+ check_matrix_square(x)
336
+ result = Decompose.lu(x)
337
+ return result.l, result.u, result.p
338
+ end
339
+
340
+ # Compute the Cholesky decomposition of a square,
341
+ # positive definite matrix.
342
+ #
343
+ # Returns a matrix an upper triangular matrix u such
344
+ # that u * u.t is the original matrix.
345
+ def cholesky(x)
346
+ check_matrix_square(x)
347
+ begin
348
+ Decompose.cholesky(x)
349
+ rescue org.jblas.exceptions.LapackPositivityException
350
+ raise Errors::MatrixNotPositiveDefinite
351
+ end
352
+ end
353
+
354
+ # Compute the determinant of a square matrix.
355
+ #
356
+ # Internally computes the LU factorization and
357
+ # then takes the product of the diagonal elements.
358
+ def det(x)
359
+ check_matrix_square(x)
360
+ l, u, p = lup(x)
361
+ return u.diag.prod
362
+ end
363
+
364
+ # Compute the singular value decompositon of a
365
+ # rectangular matrix.
366
+ #
367
+ # Returns matrices u, s, v such that u*diag(s)*v.t is
368
+ # the original matrix. Put differently, the columns of
369
+ # u are the left singular vectors, the columns of v are
370
+ # the right singular vectors, and s are the singular values.
371
+ def svd(x, sparse=false)
372
+ if sparse
373
+ usv = Singular.sparseSVD(x)
374
+ else
375
+ usv = Singular.fullSVD(x)
376
+ end
377
+ return usv.to_a
378
+ end
379
+
380
+ # Compute the singular values of a rectangular matrix.
381
+ def svdv(x)
382
+ Singular.SVDValues(x)
383
+ end
384
+
385
+ #########################################################################
386
+ #
387
+ # Utilities
388
+ #
389
+ #########################################################################
390
+
391
+ # Times a block and returns the elapsed time in seconds.
392
+ #
393
+ # For example:
394
+ #
395
+ # tictoc { n = 100; x = randn(n, n); u, d = eigv(x) }
396
+ #
397
+ # Times how long it takes to generate a random 100*100 matrix and compute
398
+ # its eigendecomposition.
399
+ def tictoc
400
+ saved_time = Time.now
401
+ yield
402
+ return Time.now - saved_time
403
+ end
404
+ end