mdarray 0.5.4-java → 0.5.5-java

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.
@@ -164,9 +164,32 @@ class MDArrayTest < Test::Unit::TestCase
164
164
 
165
165
  end
166
166
 
167
+ #-------------------------------------------------------------------------------------
168
+ #
169
+ #-------------------------------------------------------------------------------------
170
+
171
+ should "create matrices from high order sliced MDArray" do
172
+
173
+ d1 = MDArray.typed_arange("double", 0, 420)
174
+ d1.reshape!([5, 4, 3, 7])
175
+
176
+ p "three dimensional array"
177
+ d1.print
178
+ matrix = MDMatrix.from_mdarray(d1.slice(0, 0))
179
+ p "two dimensional matrix from given array"
180
+ matrix.print
181
+ p " "
182
+
183
+ p "from 4 to two"
184
+ matrix = MDMatrix.from_mdarray(d1.region(:spec => "0:0, 0:0, 0:2, 0:6").reduce)
185
+ matrix.print
186
+ p " "
187
+ matrix.mdarray.print
188
+
189
+ # matrix = MDMatrix.from_mdarray(
190
+
191
+ end
192
+
167
193
  end
168
194
 
169
195
  end
170
-
171
- require_relative 'test_double_matrix2d'
172
- # require_relative 'test_float_matrix2d'
@@ -38,105 +38,122 @@ class MDArrayTest < Test::Unit::TestCase
38
38
  #
39
39
  #-------------------------------------------------------------------------------------
40
40
 
41
- should "test matrix functions" do
41
+ should "test 1d double matrix functions" do
42
42
 
43
43
  b = MDMatrix.double([3], [1.5, 1, 1.3])
44
44
 
45
- pos = MDArray.double([3, 3], [2, -1, 0, -1, 2, -1, 0, -1, 2])
46
- matrix = MDMatrix.from_mdarray(pos)
47
- result = matrix.chol
48
- p "Cholesky decomposition"
49
- result.print
45
+ pos = MDArray.double([9], [2, -1, 0, -1, 2, -1, 0, -1, 2])
46
+ matrix1 = MDMatrix.from_mdarray(pos)
47
+
48
+ p "getting regions from the above matrix"
49
+ p "specification is '0:6'"
50
+ matrix1.region(:spec => "0:6").print
50
51
  printf("\n\n")
51
52
 
52
- eig = matrix.eig
53
- p "eigen decomposition"
54
- p "eigenvalue matrix"
55
- eig[0].print
53
+ p "specification is '0:9:2'"
54
+ matrix1.region(:spec => "0:9:2").print
56
55
  printf("\n\n")
57
- p "imaginary parts of the eigenvalues"
58
- eig[1].print
56
+
57
+ p "flipping dim 0 of the matrix"
58
+ matrix1.flip(0).print
59
59
  printf("\n\n")
60
- p "real parts of the eigenvalues"
61
- eig[2].print
60
+
61
+ m = MDArray.typed_arange("double", 16, 32)
62
+ matrix2 = MDMatrix.from_mdarray(m).region(:spec => "0:9")
63
+ matrix2.print
62
64
  printf("\n\n")
63
- p "eigenvector matrix"
64
- eig[3].print
65
+
66
+ p "matrix multiplication - dot product of two vectors"
67
+ result = matrix1 * matrix2
68
+ p result
65
69
  printf("\n\n")
66
70
 
67
- lu = matrix.lu
68
- p "lu decomposition"
69
- p "is non singular: #{lu[0]}"
70
- p "determinant: #{lu[1]}"
71
- p "pivot vector: #{lu[2]}"
72
- p "lower triangular matrix"
73
- lu[3].print
71
+ result = matrix1.kron(matrix2)
72
+ p "Kronecker multiplication"
73
+ result.print
74
74
  printf("\n\n")
75
- p "upper triangular matrix"
76
- lu[4].print
75
+
76
+ p "norm1"
77
+ p result.norm1
78
+
79
+ p "norm2"
80
+ p result.norm2
81
+
82
+ p "Returns the Frobenius norm of matrix A, which is Sqrt(Sum(A[i,j]^2))"
83
+ p result.normF
84
+
85
+ p "Returns the infinity norm of matrix A, which is the maximum absolute row sum."
86
+ p result.norm_infinity
87
+
88
+ result.normalize!
89
+ result.print
77
90
  printf("\n\n")
78
91
 
79
- # Returns the condition of matrix A, which is the ratio of largest to
80
- # smallest singular value.
81
- p "condition of matrix"
82
- p matrix.cond
92
+ p "summing all values of result: #{result.sum}"
93
+
94
+ result.mdarray.print
83
95
 
84
- # Solves the upper triangular system U*x=b;
85
- p "solving the equation by backward_solve"
86
- solve = lu[4].backward_solve(b)
87
- solve.print
96
+ # val1 is not a matrix2d... wrong test file. Need to actually work with division on
97
+ # Matrix2D.
98
+ p "dividing two matrices... actually multiply by the inverse"
99
+ val1 = MDMatrix.double([2], [118.4, 135.2])
100
+ val2 = MDMatrix.double([2, 2], [3, 3.5, 3.2, 3.6])
101
+ val1.print
102
+ printf("\n\n")
103
+ val2.print
88
104
  printf("\n\n")
89
105
 
90
- # Solves the lower triangular system U*x=b;
91
- p "solving the equation by forward_solve"
92
- solve = lu[3].forward_solve(b)
93
- solve.print
106
+ div = val1 / val2
107
+ p "result of division"
108
+ div.print
94
109
  printf("\n\n")
95
110
 
96
- qr = matrix.qr
97
- p "QR decomposition"
98
- p "Matrix has full rank: #{qr[0]}"
99
- p "Orthogonal factor Q:"
100
- qr[1].print
111
+ p "multiply by the inverse"
112
+ r1 = val2.inverse
113
+ r1.print
101
114
  printf("\n\n")
102
- p "Upper triangular factor, R"
103
- qr[2].print
115
+
116
+ val1.print
104
117
  printf("\n\n")
105
118
 
106
- svd = matrix.svd
107
- p "Singular value decomposition"
108
- p "operation success? #{svd[0]}" # 0 success; < 0 ith value is illegal; > 0 not converge
109
- p "cond: #{svd[1]}"
110
- p "norm2: #{svd[2]}"
111
- p "rank: #{svd[3]}"
112
- p "singular values"
113
- p svd[4]
114
- p "Diagonal matrix of singular values"
115
- # svd[5].print
119
+ mult = r1 * val1
120
+ mult.print
116
121
  printf("\n\n")
117
- p "left singular vectors U"
118
- svd[6].print
122
+
123
+ end
124
+
125
+ #-------------------------------------------------------------------------------------
126
+ #
127
+ #-------------------------------------------------------------------------------------
128
+
129
+ should "test 1D float matrix functions" do
130
+
131
+ b = MDMatrix.float([3], [1.5, 1, 1.3])
132
+
133
+ pos = MDArray.float([9], [2, -1, 0, -1, 2, -1, 0, -1, 2])
134
+ matrix1 = MDMatrix.from_mdarray(pos)
135
+
136
+ p "getting regions from the above matrix"
137
+ p "specification is '0:6'"
138
+ matrix1.region(:spec => "0:6").print
119
139
  printf("\n\n")
120
- p "right singular vectors V"
121
- svd[7].print
140
+
141
+ p "specification is '0:9:2'"
142
+ matrix1.region(:spec => "0:9:2").print
122
143
  printf("\n\n")
123
144
 
124
- m = MDArray.typed_arange("double", 0, 16)
125
- m.reshape!([4, 4])
126
- matrix1 = MDMatrix.from_mdarray(m)
127
- # mat2 = matrix.chol
128
- matrix1.print
145
+ p "flipping dim 0 of the matrix"
146
+ matrix1.flip(0).print
129
147
  printf("\n\n")
130
148
 
131
- m = MDArray.typed_arange("double", 16, 32)
132
- m.reshape!([4, 4])
133
- matrix2 = MDMatrix.from_mdarray(m)
149
+ m = MDArray.typed_arange("float", 16, 32)
150
+ matrix2 = MDMatrix.from_mdarray(m).region(:spec => "0:9")
134
151
  matrix2.print
135
152
  printf("\n\n")
136
153
 
154
+ p "matrix multiplication - dot product of two vectors"
137
155
  result = matrix1 * matrix2
138
- p "matrix multiplication"
139
- result.print
156
+ p result
140
157
  printf("\n\n")
141
158
 
142
159
  result = matrix1.kron(matrix2)
@@ -144,9 +161,6 @@ class MDArrayTest < Test::Unit::TestCase
144
161
  result.print
145
162
  printf("\n\n")
146
163
 
147
- print "determinant is: #{result.det}"
148
- printf("\n\n")
149
-
150
164
  p "norm1"
151
165
  p result.norm1
152
166
 
@@ -159,18 +173,6 @@ class MDArrayTest < Test::Unit::TestCase
159
173
  p "Returns the infinity norm of matrix A, which is the maximum absolute row sum."
160
174
  p result.norm_infinity
161
175
 
162
- power3 = result ** 3
163
- power3.print
164
- printf("\n\n")
165
-
166
- p result.trace
167
-
168
- trap_lower = result.trapezoidal_lower
169
- trap_lower.print
170
- printf("\n\n")
171
-
172
- p result.vector_norm2
173
-
174
176
  result.normalize!
175
177
  result.print
176
178
  printf("\n\n")
@@ -180,7 +182,7 @@ class MDArrayTest < Test::Unit::TestCase
180
182
  result.mdarray.print
181
183
 
182
184
  end
183
- #=end
185
+
184
186
  end
185
187
 
186
188
  end
@@ -0,0 +1,531 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require 'rubygems'
23
+ require "test/unit"
24
+ require 'shoulda'
25
+
26
+ require 'mdarray'
27
+
28
+ class MDArrayTest < Test::Unit::TestCase
29
+
30
+ context "Colt Matrix" do
31
+
32
+ setup do
33
+
34
+
35
+ end
36
+
37
+ #-------------------------------------------------------------------------------------
38
+ #
39
+ #-------------------------------------------------------------------------------------
40
+
41
+ should "get and set values for long Matrix" do
42
+
43
+ a = MDMatrix.long([4, 4])
44
+ a[0, 0] = 1
45
+ assert_equal(1, a[0, 0])
46
+ assert_equal(0.0, a[0, 1])
47
+
48
+ a.fill(2)
49
+ assert_equal(2, a[3, 3])
50
+
51
+ b = MDMatrix.long([4, 4])
52
+ b.fill(a)
53
+ assert_equal(2, b[1, 3])
54
+
55
+ # fill the matrix with the value of a Proc evaluation. The argument to the
56
+ # Proc is the content of the array at the given index, i.e, x = b[i] for all i.
57
+ func = Proc.new { |x| x ** 2 }
58
+ b.fill(func)
59
+ assert_equal(4, b[0, 3])
60
+ b.print
61
+ printf("\n\n")
62
+
63
+ # fill the Matrix with the value of method apply from a given class.
64
+ # In general this solution is more efficient than the above solution with
65
+ # Proc.
66
+ class LongFunc
67
+ def self.apply(x)
68
+ x/2
69
+ end
70
+ end
71
+
72
+ b.fill(LongFunc)
73
+ assert_equal(2, b[2,0])
74
+ b.print
75
+ printf("\n\n")
76
+
77
+ # defines a class with a method apply with two arguments
78
+ class LongFunc2
79
+ def self.apply(x, y)
80
+ (x + y) ** 2
81
+ end
82
+ end
83
+
84
+ # fill array a with the value the result of a function to each cell;
85
+ # x[row,col] = function(x[row,col],y[row,col]).
86
+ a.fill(b, LongFunc2)
87
+ a.print
88
+ printf("\n\n")
89
+
90
+ tens = MDMatrix.init_with("long", [5, 3], 10.0)
91
+ tens.print
92
+ printf("\n\n")
93
+
94
+ typed_arange = MDMatrix.typed_arange("long", 0, 20, 2)
95
+ typed_arange.print
96
+ printf("\n\n")
97
+
98
+ linspace = MDMatrix.linspace("long", 0, 10, 50)
99
+ linspace.print
100
+ printf("\n\n")
101
+
102
+ # set the value of all cells that are bigger than 5 to 1.0
103
+ linspace.fill_cond(Proc.new { |x| x > 5 }, 1.0)
104
+ linspace.print
105
+ printf("\n\n")
106
+
107
+ # set the value of all cells that are smaller than 5 to the square value
108
+ linspace.fill_cond(Proc.new { |x| x < 5 }, Proc.new { |x| x * x })
109
+ linspace.print
110
+ printf("\n\n")
111
+
112
+ ones = MDMatrix.ones("long", [3, 5])
113
+ ones.print
114
+ printf("\n\n")
115
+
116
+ arange = MDMatrix.arange(0, 10)
117
+ arange.print
118
+ printf("\n\n")
119
+
120
+ end
121
+
122
+ #-------------------------------------------------------------------------------------
123
+ #
124
+ #-------------------------------------------------------------------------------------
125
+
126
+ should "test 2d long matrix functions" do
127
+
128
+ m = MDArray.typed_arange("long", 0, 16)
129
+ m.reshape!([4, 4])
130
+ matrix1 = MDMatrix.from_mdarray(m)
131
+ # mat2 = matrix.chol
132
+ matrix1.print
133
+ printf("\n\n")
134
+
135
+ p "Transposing the above matrix"
136
+ matrix1.transpose.print
137
+ printf("\n\n")
138
+
139
+ p "getting regions from the above matrix"
140
+ p "specification is '0:0, 1:3'"
141
+ matrix1.region(:spec => "0:0, 1:3").print
142
+ printf("\n\n")
143
+
144
+ p "specification is '0:3:2, 1:3'"
145
+ matrix1.region(:spec => "0:3:2, 1:3").print
146
+ printf("\n\n")
147
+
148
+ p "flipping dim 0 of the matrix"
149
+ matrix1.flip(0).print
150
+ printf("\n\n")
151
+
152
+ m = MDArray.typed_arange("long", 16, 32)
153
+ m.reshape!([4, 4])
154
+ matrix2 = MDMatrix.from_mdarray(m)
155
+ matrix2.print
156
+ printf("\n\n")
157
+
158
+ result = matrix1 * matrix2
159
+ p "matrix multiplication of square matrices"
160
+ result.print
161
+ printf("\n\n")
162
+
163
+ p "matrix multiplication of rec matrices"
164
+ array1 = MDMatrix.long([2, 3], [1, 2, 3, 4, 5, 6])
165
+ array2 = MDMatrix.long([3, 2], [1, 2, 3, 4, 5, 6])
166
+ mult = array1 * array2
167
+ mult.print
168
+ printf("\n\n")
169
+
170
+ p "matrix multiplication of rec matrices passing alpha and beta parameters."
171
+ p "C = alpha * A x B + beta*C"
172
+ mult = array1.mult(array2, 2, 2)
173
+ mult.print
174
+ printf("\n\n")
175
+
176
+ p "matrix multiplication of rec matrices passing alpha, beta and return (C) parameters."
177
+ p "C = alpha * A x B + beta*C"
178
+ result = MDMatrix.long([2, 2], [2, 2, 2, 2])
179
+ mult = array1.mult(array2, 2, 2, false, false, result)
180
+ mult.print
181
+ printf("\n\n")
182
+
183
+ p "matrix multiplication by vector"
184
+ array1 = MDMatrix.long([2, 3], [1, 2, 3, 4, 5, 6])
185
+ array2 = MDMatrix.long([3], [4, 5, 6])
186
+ mult = array1 * array2
187
+ mult.print
188
+ printf("\n\n")
189
+
190
+ end
191
+
192
+ #-------------------------------------------------------------------------------------
193
+ #
194
+ #-------------------------------------------------------------------------------------
195
+
196
+ should "get and set values for int Matrix" do
197
+
198
+ a = MDMatrix.int([4, 4])
199
+ a[0, 0] = 1
200
+ assert_equal(1, a[0, 0])
201
+ assert_equal(0.0, a[0, 1])
202
+
203
+ a.fill(2)
204
+ assert_equal(2, a[3, 3])
205
+
206
+ b = MDMatrix.int([4, 4])
207
+ b.fill(a)
208
+ assert_equal(2, b[1, 3])
209
+
210
+ # fill the matrix with the value of a Proc evaluation. The argument to the
211
+ # Proc is the content of the array at the given index, i.e, x = b[i] for all i.
212
+ func = Proc.new { |x| x ** 2 }
213
+ b.fill(func)
214
+ assert_equal(4, b[0, 3])
215
+ b.print
216
+ printf("\n\n")
217
+
218
+ # fill the Matrix with the value of method apply from a given class.
219
+ # In general this solution is more efficient than the above solution with
220
+ # Proc.
221
+ class IntFunc
222
+ def self.apply(x)
223
+ x/2
224
+ end
225
+ end
226
+
227
+ b.fill(IntFunc)
228
+ assert_equal(2, b[2,0])
229
+ b.print
230
+ printf("\n\n")
231
+
232
+ # defines a class with a method apply with two arguments
233
+ class IntFunc2
234
+ def self.apply(x, y)
235
+ (x + y) ** 2
236
+ end
237
+ end
238
+
239
+ # fill array a with the value the result of a function to each cell;
240
+ # x[row,col] = function(x[row,col],y[row,col]).
241
+ a.fill(b, IntFunc2)
242
+ a.print
243
+ printf("\n\n")
244
+
245
+ tens = MDMatrix.init_with("int", [5, 3], 10.0)
246
+ tens.print
247
+ printf("\n\n")
248
+
249
+ typed_arange = MDMatrix.typed_arange("int", 0, 20, 2)
250
+ typed_arange.print
251
+ printf("\n\n")
252
+
253
+ linspace = MDMatrix.linspace("int", 0, 10, 50)
254
+ linspace.print
255
+ printf("\n\n")
256
+
257
+ # set the value of all cells that are bigger than 5 to 1.0
258
+ linspace.fill_cond(Proc.new { |x| x > 5 }, 1.0)
259
+ linspace.print
260
+ printf("\n\n")
261
+
262
+ # set the value of all cells that are smaller than 5 to the square value
263
+ linspace.fill_cond(Proc.new { |x| x < 5 }, Proc.new { |x| x * x })
264
+ linspace.print
265
+ printf("\n\n")
266
+
267
+ ones = MDMatrix.ones("int", [3, 5])
268
+ ones.print
269
+ printf("\n\n")
270
+
271
+ arange = MDMatrix.arange(0, 10)
272
+ arange.print
273
+ printf("\n\n")
274
+
275
+ end
276
+
277
+ #-------------------------------------------------------------------------------------
278
+ #
279
+ #-------------------------------------------------------------------------------------
280
+
281
+ should "test 2d int matrix functions" do
282
+
283
+ m = MDArray.typed_arange("int", 0, 16)
284
+ m.reshape!([4, 4])
285
+ matrix1 = MDMatrix.from_mdarray(m)
286
+ # mat2 = matrix.chol
287
+ matrix1.print
288
+ printf("\n\n")
289
+
290
+ p "Transposing the above matrix"
291
+ matrix1.transpose.print
292
+ printf("\n\n")
293
+
294
+ p "getting regions from the above matrix"
295
+ p "specification is '0:0, 1:3'"
296
+ matrix1.region(:spec => "0:0, 1:3").print
297
+ printf("\n\n")
298
+
299
+ p "specification is '0:3:2, 1:3'"
300
+ matrix1.region(:spec => "0:3:2, 1:3").print
301
+ printf("\n\n")
302
+
303
+ p "flipping dim 0 of the matrix"
304
+ matrix1.flip(0).print
305
+ printf("\n\n")
306
+
307
+ m = MDArray.typed_arange("int", 16, 32)
308
+ m.reshape!([4, 4])
309
+ matrix2 = MDMatrix.from_mdarray(m)
310
+ matrix2.print
311
+ printf("\n\n")
312
+
313
+ result = matrix1 * matrix2
314
+ p "matrix multiplication of square matrices"
315
+ result.print
316
+ printf("\n\n")
317
+
318
+ p "matrix multiplication of rec matrices"
319
+ array1 = MDMatrix.int([2, 3], [1, 2, 3, 4, 5, 6])
320
+ array2 = MDMatrix.int([3, 2], [1, 2, 3, 4, 5, 6])
321
+ mult = array1 * array2
322
+ mult.print
323
+ printf("\n\n")
324
+
325
+ p "matrix multiplication of rec matrices passing alpha and beta parameters."
326
+ p "C = alpha * A x B + beta*C"
327
+ mult = array1.mult(array2, 2, 2)
328
+ mult.print
329
+ printf("\n\n")
330
+
331
+ p "matrix multiplication of rec matrices passing alpha, beta and return (C) parameters."
332
+ p "C = alpha * A x B + beta*C"
333
+ result = MDMatrix.int([2, 2], [2, 2, 2, 2])
334
+ mult = array1.mult(array2, 2, 2, false, false, result)
335
+ mult.print
336
+ printf("\n\n")
337
+
338
+ p "matrix multiplication by vector"
339
+ array1 = MDMatrix.int([2, 3], [1, 2, 3, 4, 5, 6])
340
+ array2 = MDMatrix.int([3], [4, 5, 6])
341
+ mult = array1 * array2
342
+ mult.print
343
+ printf("\n\n")
344
+
345
+ end
346
+
347
+ #=end
348
+ end
349
+
350
+ end
351
+
352
+ =begin
353
+
354
+ Example of SVD decomposition with PColt. Check if working!
355
+
356
+ A =2.0 0.0 8.0 6.0 0.0
357
+ 1.0 6.0 0.0 1.0 7.0
358
+ 5.0 0.0 7.0 4.0 0.0
359
+ 7.0 0.0 8.0 5.0 0.0
360
+ 0.0 10.0 0.0 0.0 7.0
361
+
362
+ U = -0.542255 0.0649957 0.821617 0.105747 -0.124490
363
+ -0.101812 -0.593461 -0.112552 0.788123 0.0602700
364
+ -0.524953 0.0593817 -0.212969 -0.115742 0.813724
365
+ -0.644870 0.0704063 -0.508744 -0.0599027 -0.562829
366
+ -0.0644952 -0.796930 0.0900097 -0.592195 -0.0441263
367
+
368
+ VT =-0.464617 0.0215065 -0.868509 0.000799554 -0.171349
369
+ -0.0700860 -0.759988 0.0630715 -0.601346 -0.227841
370
+ -0.735094 0.0987971 0.284009 -0.223485 0.565040
371
+ -0.484392 0.0254474 0.398866 0.332684 -0.703523
372
+ -0.0649698 -0.641520 -0.0442743 0.691201 0.323284
373
+
374
+ S =
375
+ (00) 17.91837085874625
376
+ (11) 15.17137188041607
377
+ (22) 3.5640020352605677
378
+ (33) 1.9842281528992616
379
+ (44) 0.3495556671751232
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+ Dot product (or inner product of scalar product) of 2 vectors
388
+
389
+ >>> import scipy as sp
390
+ >>> x = sp.array([1,-2, .0])
391
+ >>> y = sp.array([2, 5, -1.0])
392
+ >>> dotproduct = sp.dot(x,y)
393
+ >>> print dotproduct
394
+ -8.0
395
+ Matrix product.
396
+
397
+ >>> import scipy as sp
398
+ >>> A = sp.array([[3.2, -1, 2],[2,-2,4],[1.5,-1,-4]])
399
+ >>> B = sp.array([[1., 2.],[-1., 2.0],[2, -1.5]])
400
+
401
+ >>> Matrixproduct = sp.dot(A,B)
402
+ >>> print Matrixproduct
403
+ [[ 8.2, 1.4]
404
+ [ 12., -6. ]
405
+ [ -5.5, 7. ]]
406
+ Matrix-vector product
407
+
408
+ >>> import scipy as sp
409
+ >>> x = sp.array([1,-2, .0])
410
+ >>> A = sp.array([[3.2, -1, 2],[2,-2,4],[1.5,-1,-4]])
411
+ >>> b = sp.dot(A,x)
412
+ >>> print b
413
+ [ 5.2, 6., 3.5]
414
+ Diagonal, transpose and trace
415
+
416
+ >>> print sp.diagonal(A) #returns the diagonal of A
417
+ array([ 3.2, -2., -4.])
418
+ >>> D = sp.diag((1, 2, 3)) # return a diagonal matrix
419
+ [[1 0 0]
420
+ [0 2 0]
421
+ [0 0 3]]
422
+ >>> print sp.transpose(A)
423
+ [[ 3.2 2. 1.5]
424
+ [-1. -2. -1. ]
425
+ [ 2. 4. -4. ]]
426
+ >>> print sp.trace(A)
427
+ -2.7999999999999998
428
+ Determinant and inverse. To compute the determinant or the inverse of a matrix, we need the numpy linear algebra submodule linalg
429
+
430
+ >>> import numpy
431
+ >>> import numpy.linalg
432
+ >>> A = numpy.array([[3.2, -1, 2],[2,-2,4],[1.5,-1,-4]])
433
+ >>> determinant = numpy.linalg.det(A)
434
+ >>> print determinant
435
+ 26.4
436
+ >>> inverse = numpy.linalg.inv(A)
437
+ [[ 0.45454545 -0.22727273 0. ]
438
+ [ 0.53030303 -0.59848485 -0.33333333]
439
+ [ 0.03787879 0.06439394 -0.16666667]]
440
+ 1.2 Eigenvalue and Eigenvectors
441
+
442
+
443
+ >>> import numpy as np
444
+ >>> import np.linalg
445
+ >>> A = np.array([[3.2, -1, 2],[2,-2,4],[1.5,-1,-4]])
446
+ >>> e_values, e_vectors = np.linalg.eig(A) #returns a list e_values of eigenvalues and a matrix e_vector whose row i corresponds
447
+ #to the eigenvectors associated with eigenvalue i
448
+ >>> print e_values
449
+ [ 2.965908+0.j, -2.882954+0.76793809j, -2.882954-0.76793809j ]
450
+ >>> print e_vectors # row i corresponds to the eigenvector
451
+ # associated with eigenvalues i
452
+ [[ 0.88158576+0.j, -0.25687900+0.02847007j, -0.25687900-0.02847007j]
453
+ [ 0.45531661+0.j, -0.89064266+0.j, -0.89064266+0.j ]
454
+ [ 0.12447222+0.j, 0.32503863-0.18522464j, 0.32503863+0.18522464j]]
455
+ 1.3 Matrix factorization
456
+
457
+ LU factorization (LU_Decomposition.py).
458
+ Given a matrix A, A can be written as A = PLU where L lower triangular matrix U upper triangular matrix P is the matrix whose row i is a permutation of the identity matrix row i
459
+ LU_Decomposition.py
460
+ """
461
+ LU decomposition: M = PLU
462
+ """
463
+ import scipy
464
+ import scipy.linalg
465
+
466
+ if __name__ == "__main__":
467
+ M = scipy.array([ [1,-2, 3], [0,2,-1], [1,3,-2] ])
468
+ P, L, U = scipy.linalg.lu(M)
469
+
470
+ print "P = ",P
471
+ print "L = ",L
472
+ print 'U = ', U
473
+ QR factorization (QR_Decomposition.py).
474
+ Given a mxn matrix M, find a mxm unitary matrix Q - that is where is the adjoint of Q - and a mxn upper triangular matrix R such that M = QR.
475
+ """
476
+ QR decomposition: M = QR
477
+ """
478
+ import scipy
479
+ import scipy.linalg
480
+
481
+ if __name__ == "__main__":
482
+ M = scipy.array([ [1,-2,3], [2,1,-1], [1,0,2], [0,-2,-1]])
483
+ Q, R = scipy.linalg.qr(M)
484
+
485
+ print "Q = ",Q
486
+ print "R = ", R
487
+ Singular values decomposition:(SVD.py)
488
+ For a given mxn matrix A. and are Hermitians - a matrix H is Hermitian if it is equal to its adjoint, i.e. - so their eigenvalues are all real positive numbers. Further there are at most Min(m,n) non-zero identical eigenvalues for and . The square roots of these are called singular values. A can be decomposed as where U is a mxm matrix of eigenvectors of , V is a nxn matrix of eigenvectors of and is mxn diagonal matrix whose entries are the singular values. The command linalg.svd will return U, V, and a list of singular values. To obtain the matrix use linalg.diagsvd.
489
+
490
+ """
491
+ SVD, A = U \Sigma V
492
+ """
493
+ import scipy
494
+ import scipy.linalg
495
+ if __name__ == "__main__":
496
+ A = scipy.array([[1,-2],[2,0],[-1,3]])
497
+ U,s,V = scipy.linalg.svd(A) #s is the list os singular values
498
+
499
+ print "U = ",U
500
+ print "V = ", V
501
+ print "singular values list = ",s
502
+ Sigma = scipy.mat(scipy.linalg.diagsvd(s,3,2))
503
+ print "Sigma = ", Sigma
504
+ Cholesky factorization (CholeskyDecomposition.py): Given a Hermitian matrix M. Find a decomposition as M = where U is a upper triangular matrix and is the adjoint of U
505
+ """
506
+ Cholesky decomposition. M = VU where
507
+ U is a upper triangular matrix and V is the adjoint of U
508
+ """
509
+ import scipy
510
+ import scipy.linalg
511
+
512
+ if __name__ == "__main__":
513
+ M = scipy.array([[1,0,-1],[1,2,1],[0,-1,2]])
514
+ U = scipy.linalg.cholesky(M)
515
+
516
+ print "U = ", U
517
+ Schur factorization (SchurDecomposition.py): For a square nxn matrix M, find a unitary matrix Z and a upper-triangular (or quasi-triangular) matrix T such that
518
+ """
519
+ Schur decomposition: M = ZTZh, where Zh is the adjoint of Z
520
+ """
521
+ import scipy
522
+ import scipy.linalg
523
+
524
+ if __name__ == "__main__":
525
+ M = scipy.array([[1,0,-1],[1,2,1],[0,-1,2]])
526
+ Z, T = scipy.linalg.schur(M)
527
+
528
+ print "Z = ", Z
529
+ print 'T = ', T
530
+
531
+ =end