Skip to content

Commit a0669de

Browse files
committed
Methods for solving Ax = b
1 parent 7b1f232 commit a0669de

File tree

5 files changed

+118
-10
lines changed

5 files changed

+118
-10
lines changed

lib/blis/base/level-1v.rb

+2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ def self.copyv(vx, vy)
8989
end
9090

9191
#------------------------------------------------------------------------------------
92+
# rho := conjx(x)^T * conjy(y)
93+
#
9294
# Performs a dotv operation between two vectors. No error checking is done. Vectors
9395
# can be row or column vectors that no error will be issued.
9496
# @param vecx [MDArray] an MDArray configured as a vector: dimension [x, 1] or [1, x]

lib/linalg/algebra.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ def self.forward_solve(mLU, vb)
5757

5858
def self.back_substitution(mLU, vb)
5959

60-
mUpart = mLU.part_by(:six, row_dir: :rl, column_dir: :bt, filter: 0b001000)
60+
mUpart = mLU.part_by(:six, row_dir: :rl, column_dir: :bt, filter: 0b100010)
6161
vbpart = vb.part_by(:row, column_dir: :bt, filter: 0b11)
6262

6363
loop do
64-
beta1, bottom = vbpart.next
65-
beta1.pp
66-
bottom.pp
67-
64+
beta1, b2 = vbpart.next
65+
v11, u12t = mUpart.next
66+
# b1 1 beta1 -1 u12t b2
67+
# rho := beta * rho + alpha * conjx(x)^T * conjy(y)
68+
# dotxv(alpha, vx, vy, beta, rho)
69+
Blis.dotxv(-1, u12t, b2, 1, beta1)
70+
beta1[0, 0] /= v11[0, 0]
6871
end
6972

7073
end

lib/linalg/decomposition.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ class LinAlg
3131
# @param a [MDArray]
3232
#--------------------------------------------------------------------------------------
3333

34-
def self.lu(a)
34+
def self.lu(mA)
3535

36-
apart = a.part_by(:six, row_dir: :lr, column_dir: :tb, filter: 0b101011)
36+
apart = mA.part_by(:six, row_dir: :lr, column_dir: :tb, filter: 0b101011)
3737

3838
loop do
3939
diag, l21, a12, a22 = apart.next
4040
Blis.scalv(1.0/diag[0, 0], l21)
4141
Blis.ger(-1, a22, l21, Blis.trnsp(a12))
4242
end
43+
mA
4344

4445
end
4546

test/test_decomposition.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,15 @@ class MDArrayTest < Test::Unit::TestCase
6363
#--------------------------------------------------------------------------------------
6464

6565
should "LU decompose a matrix" do
66-
=begin
66+
67+
lu_a = MDArray.double([4, 4],
68+
[2, 0, 1, 2,
69+
-1, -1, 2, 1,
70+
2, 1, 1, -1,
71+
-2, -1, 1, -2])
72+
6773
MDArray::LinAlg.lu(@a)
68-
@a.pp
74+
assert_equal(true, lu_a.identical(@a))
6975

7076
MDArray::LinAlg.lu(@b)
7177
@b.pp
@@ -84,7 +90,6 @@ class MDArrayTest < Test::Unit::TestCase
8490
b = MDArray.double([3, 1], [2, 10, -2])
8591
MDArray::LinAlg.forward_solve(@d, b)
8692
b.pp
87-
=end
8893

8994
MDArray::LinAlg.lu(@c)
9095
@c.pp

test/test_solvers.rb

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# -*- coding: utf-8 -*-
2+
3+
##########################################################################################
4+
# Copyright © 2017 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 "test/unit"
23+
require 'shoulda'
24+
25+
require '../config' if @platform == nil
26+
require 'mdarray-blis'
27+
28+
29+
class MDArrayTest < Test::Unit::TestCase
30+
31+
context "LinAlg" do
32+
33+
#--------------------------------------------------------------------------------------
34+
#
35+
#--------------------------------------------------------------------------------------
36+
37+
setup do
38+
39+
@a = MDArray.double([4, 4],
40+
[2, 0, 1, 2,
41+
-2, -1, 1, -1,
42+
4, -1, 5, 4,
43+
-4, 1, -3, -8])
44+
45+
@b = MDArray.double([4, 1], [2, 10, -2, 5])
46+
47+
48+
@c = MDArray.double([4, 4],
49+
[2, 0, 1, 2,
50+
-2, -1, 1, -1,
51+
4, -1, 5, 4,
52+
-4, 1, -3, -8])
53+
54+
@b_orig = MDArray.double([4, 1], [2, 10, -2, 5])
55+
56+
end
57+
58+
#--------------------------------------------------------------------------------------
59+
#
60+
#--------------------------------------------------------------------------------------
61+
62+
should "forward solve an equation " do
63+
64+
# perform LU factorization of a
65+
MDArray::LinAlg.lu(@a)
66+
67+
a_b = MDArray.double([4, 1], [2, 12, -18, 39])
68+
back_sub = MDArray.double([4, 1], [39.25, -106.5, -37.5, -19.5])
69+
70+
MDArray::LinAlg.forward_solve(@a, @b)
71+
assert_equal(true, a_b.identical(@b))
72+
73+
MDArray::LinAlg.back_substitution(@a, @b)
74+
assert_equal(true, back_sub.identical(@b))
75+
76+
vy = MDArray.double([4, 1])
77+
# def self.gemv_dot(alpha, mA, vx, beta, vy)
78+
Blis.gemv_dot(1, @c, @b, 1, vy)
79+
assert_equal(true, @b_orig.identical(vy))
80+
81+
end
82+
83+
#--------------------------------------------------------------------------------------
84+
#
85+
#--------------------------------------------------------------------------------------
86+
87+
should "solve an equation " do
88+
89+
MDArray::LinAlg.back_substitution(
90+
@a, MDArray::LinAlg.forward_solve(
91+
MDArray::LinAlg.lu(@a), @b))
92+
93+
end
94+
95+
end
96+
97+
end

0 commit comments

Comments
 (0)