本文整理匯總了Golang中github.com/hrautila/cmat.FloatMatrix.Diag方法的典型用法代碼示例。如果您正苦於以下問題:Golang FloatMatrix.Diag方法的具體用法?Golang FloatMatrix.Diag怎麽用?Golang FloatMatrix.Diag使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hrautila/cmat.FloatMatrix
的用法示例。
在下文中一共展示了FloatMatrix.Diag方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestGivensLQ
// Simple and slow LQ decomposition with Givens rotations
func TestGivensLQ(t *testing.T) {
var d cmat.FloatMatrix
M := 149
N := 167
A := cmat.NewMatrix(M, N)
A1 := cmat.NewCopy(A)
ones := cmat.NewFloatConstSource(1.0)
src := cmat.NewFloatNormSource()
A.SetFrom(src)
A0 := cmat.NewCopy(A)
Qt := cmat.NewMatrix(N, N)
d.Diag(Qt)
d.SetFrom(ones)
// R = G(n)...G(2)G(1)*A; Q = G(1).T*G(2).T...G(n).T ; Q.T = G(n)...G(2)G(1)
for i := 0; i < M; i++ {
// zero elements right of diagonal
for j := N - 2; j >= i; j-- {
c, s, r := lapackd.ComputeGivens(A.Get(i, j), A.Get(i, j+1))
A.Set(i, j, r)
A.Set(i, j+1, 0.0)
// apply rotation to this column starting from row i+1
lapackd.ApplyGivensRight(A, j, j+1, i+1, M-i-1, c, s)
// update Qt = G(k)*Qt
lapackd.ApplyGivensRight(Qt, j, j+1, 0, N, c, s)
}
}
// A = L*Q
blasd.Mult(A1, A, Qt, 1.0, 0.0, gomas.TRANSB)
blasd.Plus(A0, A1, 1.0, -1.0, gomas.NONE)
nrm := lapackd.NormP(A0, lapackd.NORM_ONE)
t.Logf("M=%d, N=%d ||A - L*G(1)..G(n)||_1: %e\n", M, N, nrm)
}
示例2: SolveDiag
/*
* Compute
* B = B*diag(D).-1 flags & RIGHT == true
* B = diag(D).-1*B flags & LEFT == true
*
* If flags is LEFT (RIGHT) then element-wise divides columns (rows) of B with vector D.
*
* Arguments:
* B M-by-N matrix if flags&RIGHT == true or N-by-M matrix if flags&LEFT == true
*
* D N element column or row vector or N-by-N matrix
*
* flags Indicator bits, LEFT or RIGHT
*/
func SolveDiag(B, D *cmat.FloatMatrix, flags int, confs ...*gomas.Config) *gomas.Error {
var c, d0 cmat.FloatMatrix
var d *cmat.FloatMatrix
conf := gomas.CurrentConf(confs...)
d = D
if !D.IsVector() {
d0.Diag(D)
d = &d0
}
dn := d0.Len()
br, bc := B.Size()
switch flags & (gomas.LEFT | gomas.RIGHT) {
case gomas.LEFT:
if br != dn {
return gomas.NewError(gomas.ESIZE, "SolveDiag")
}
// scale rows;
for k := 0; k < dn; k++ {
c.Row(B, k)
blasd.InvScale(&c, d.GetAt(k), conf)
}
case gomas.RIGHT:
if bc != dn {
return gomas.NewError(gomas.ESIZE, "SolveDiag")
}
// scale columns
for k := 0; k < dn; k++ {
c.Column(B, k)
blasd.InvScale(&c, d.GetAt(k), conf)
}
}
return nil
}
示例3: TestDTrmmUnitUpperRight
func TestDTrmmUnitUpperRight(t *testing.T) {
var d cmat.FloatMatrix
N := 563
K := 171
A := cmat.NewMatrix(N, N)
B := cmat.NewMatrix(K, N)
B0 := cmat.NewMatrix(K, N)
C := cmat.NewMatrix(K, N)
zeros := cmat.NewFloatConstSource(0.0)
ones := cmat.NewFloatConstSource(1.0)
zeromean := cmat.NewFloatNormSource()
A.SetFrom(zeromean, cmat.UPPER|cmat.UNIT)
B.SetFrom(ones)
B0.SetFrom(ones)
// B = B*A
blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.RIGHT|gomas.UNIT)
d.Diag(A).SetFrom(ones)
blasd.Mult(C, B0, A, 1.0, 0.0, gomas.NONE)
ok := C.AllClose(B)
t.Logf("trmm(B, A, R|U|N|U) == gemm(C, B, TriUU(A)) : %v\n", ok)
B.SetFrom(ones)
// B = B*A.T
d.SetFrom(zeros)
blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.RIGHT|gomas.TRANSA|gomas.UNIT)
d.SetFrom(ones)
blasd.Mult(C, B0, A, 1.0, 0.0, gomas.TRANSB)
ok = C.AllClose(B)
t.Logf("trmm(B, A, R|U|T|U) == gemm(C, B, TriUU(A).T) : %v\n", ok)
}
示例4: EigenSym
func EigenSym(D, A, W *cmat.FloatMatrix, bits int, confs ...*gomas.Config) (err *gomas.Error) {
var sD, sE, E, tau, Wred cmat.FloatMatrix
var vv *cmat.FloatMatrix
err = nil
vv = nil
conf := gomas.CurrentConf(confs...)
if m(A) != n(A) || D.Len() != m(A) {
err = gomas.NewError(gomas.ESIZE, "EigenSym")
return
}
if bits&gomas.WANTV != 0 && W.Len() < 3*n(A) {
err = gomas.NewError(gomas.EWORK, "EigenSym")
return
}
if bits&(gomas.LOWER|gomas.UPPER) == 0 {
bits = bits | gomas.LOWER
}
ioff := 1
if bits&gomas.LOWER != 0 {
ioff = -1
}
E.SetBuf(n(A)-1, 1, n(A)-1, W.Data())
tau.SetBuf(n(A), 1, n(A), W.Data()[n(A)-1:])
wrl := W.Len() - 2*n(A) - 1
Wred.SetBuf(wrl, 1, wrl, W.Data()[2*n(A)-1:])
// reduce to tridiagonal
if err = TRDReduce(A, &tau, &Wred, bits, conf); err != nil {
err.Update("EigenSym")
return
}
sD.Diag(A)
sE.Diag(A, ioff)
blasd.Copy(D, &sD)
blasd.Copy(&E, &sE)
if bits&gomas.WANTV != 0 {
if err = TRDBuild(A, &tau, &Wred, n(A), bits, conf); err != nil {
err.Update("EigenSym")
return
}
vv = A
}
// resize workspace
wrl = W.Len() - n(A) - 1
Wred.SetBuf(wrl, 1, wrl, W.Data()[n(A)-1:])
if err = TRDEigen(D, &E, vv, &Wred, bits, conf); err != nil {
err.Update("EigenSym")
return
}
return
}
示例5: unblkBuildQL
/*
* Unblocked code for generating M by N matrix Q with orthogonal columns which
* are defined as the last N columns of the product of K first elementary
* reflectors.
*
* Parameter nk is last nk elementary reflectors that are not used in computing
* the matrix Q. Parameter mk length of the first unused elementary reflectors
* First nk columns are zeroed and subdiagonal mk-nk is set to unit.
*
* Compatible with lapack.DORG2L subroutine.
*/
func unblkBuildQL(A, Tvec, W *cmat.FloatMatrix, mk, nk int, mayClear bool) {
var ATL, ATR, ABL, ABR cmat.FloatMatrix
var A00, a01, a10, a11, a21, A22 cmat.FloatMatrix
var tT, tB cmat.FloatMatrix
var t0, tau1, t2, w12, D cmat.FloatMatrix
// (mk, nk) = (rows, columns) of upper left partition
util.Partition2x2(
&ATL, &ATR,
&ABL, &ABR, A, mk, nk, util.PTOPLEFT)
util.Partition2x1(
&tT,
&tB, Tvec, nk, util.PTOP)
// zero the left side
if nk > 0 && mayClear {
blasd.Scale(&ABL, 0.0)
blasd.Scale(&ATL, 0.0)
D.Diag(&ATL, nk-mk)
blasd.Add(&D, 1.0)
}
for m(&ABR) > 0 && n(&ABR) > 0 {
util.Repartition2x2to3x3(&ATL,
&A00, &a01, nil,
&a10, &a11, nil,
nil, &a21, &A22, A, 1, util.PBOTTOMRIGHT)
util.Repartition2x1to3x1(&tT,
&t0,
&tau1,
&t2, Tvec, 1, util.PBOTTOM)
// ------------------------------------------------------
w12.SubMatrix(W, 0, 0, a10.Len(), 1)
applyHouseholder2x1(&tau1, &a01, &a10, &A00, &w12, gomas.LEFT)
blasd.Scale(&a01, -tau1.Get(0, 0))
a11.Set(0, 0, 1.0-tau1.Get(0, 0))
// zero bottom elements
blasd.Scale(&a21, 0.0)
// ------------------------------------------------------
util.Continue3x3to2x2(
&ATL, &ATR,
&ABL, &ABR, &A00, &a11, &A22, A, util.PBOTTOMRIGHT)
util.Continue3x1to2x1(
&tT,
&tB, &t0, &tau1, Tvec, util.PBOTTOM)
}
}
示例6: TestTrdMultUpper
func TestTrdMultUpper(t *testing.T) {
var dt, et, da, ea cmat.FloatMatrix
N := 843
nb := 48
conf := gomas.NewConf()
conf.LB = nb
A := cmat.NewMatrix(N, N)
tau := cmat.NewMatrix(N, 1)
src := cmat.NewFloatNormSource()
// create symmetric matrix
A.SetFrom(src, cmat.SYMM)
A0 := cmat.NewCopy(A)
W := lapackd.Workspace(lapackd.TRDReduceWork(A, conf))
lapackd.TRDReduce(A, tau, W, gomas.UPPER, conf)
// make tridiagonal matrix T
T0 := cmat.NewMatrix(N, N)
dt.Diag(T0)
da.Diag(A)
blasd.Copy(&dt, &da)
ea.Diag(A, 1)
et.Diag(T0, 1)
blasd.Copy(&et, &ea)
et.Diag(T0, -1)
blasd.Copy(&et, &ea)
T1 := cmat.NewCopy(T0)
// compute Q*T*Q.T (unblocked)
conf.LB = 0
lapackd.TRDMult(T0, A, tau, W, gomas.LEFT|gomas.UPPER, conf)
lapackd.TRDMult(T0, A, tau, W, gomas.RIGHT|gomas.TRANS|gomas.UPPER, conf)
blasd.Plus(T0, A0, 1.0, -1.0, gomas.NONE)
nrm := lapackd.NormP(T0, lapackd.NORM_ONE)
t.Logf("N=%d, unblk.||A - Q*T*Q.T||_1: %e\n", N, nrm)
// compute Q*T*Q.T (blocked)
conf.LB = nb
W = lapackd.Workspace(lapackd.TRDMultWork(A, gomas.LEFT|gomas.UPPER, conf))
lapackd.TRDMult(T1, A, tau, W, gomas.LEFT|gomas.UPPER, conf)
lapackd.TRDMult(T1, A, tau, W, gomas.RIGHT|gomas.TRANS|gomas.UPPER, conf)
blasd.Plus(T1, A0, 1.0, -1.0, gomas.NONE)
nrm = lapackd.NormP(T1, lapackd.NORM_ONE)
t.Logf("N=%d, blk.||A - Q*T*Q.T||_1: %e\n", N, nrm)
}
示例7: unblkBuildLQ
/*
* Unblocked code for generating M by N matrix Q with orthogonal columns which
* are defined as the first N columns of the product of K first elementary
* reflectors.
*
* Parameters nk = n(A)-K, mk = m(A)-K define the initial partitioning of
* matrix A.
*
* Q = H(k)H(k-1)...H(1) , 0 < k <= M, where H(i) = I - tau*v*v.T
*
* Computation is ordered as H(k)*H(k-1)...*H(1)*I ie. from bottom to top.
*
* If k < M rows k+1:M are cleared and diagonal entries [k+1:M,k+1:M] are
* set to unit. Then the matrix Q is generated by right multiplying elements below
* of i'th elementary reflector H(i).
*
* Compatible to lapack.xORG2L subroutine.
*/
func unblkBuildLQ(A, Tvec, W *cmat.FloatMatrix, mk, nk int, mayClear bool) {
var ATL, ATR, ABL, ABR cmat.FloatMatrix
var A00, a10, a11, a12, a21, A22 cmat.FloatMatrix
var tT, tB cmat.FloatMatrix
var t0, tau1, t2, w12, D cmat.FloatMatrix
util.Partition2x2(
&ATL, &ATR,
&ABL, &ABR, A, mk, nk, util.PBOTTOMRIGHT)
util.Partition2x1(
&tT,
&tB, Tvec, mk, util.PBOTTOM)
// zero the bottom part
if mk > 0 && mayClear {
blasd.Scale(&ABL, 0.0)
blasd.Scale(&ABR, 0.0)
D.Diag(&ABR)
blasd.Add(&D, 1.0)
}
for m(&ATL) > 0 && n(&ATL) > 0 {
util.Repartition2x2to3x3(&ATL,
&A00, nil, nil,
&a10, &a11, &a12,
nil, &a21, &A22, A, 1, util.PTOPLEFT)
util.Repartition2x1to3x1(&tT,
&t0,
&tau1,
&t2, Tvec, 1, util.PTOP)
// ------------------------------------------------------
w12.SubMatrix(W, 0, 0, a21.Len(), 1)
applyHouseholder2x1(&tau1, &a12, &a21, &A22, &w12, gomas.RIGHT)
blasd.Scale(&a12, -tau1.Get(0, 0))
a11.Set(0, 0, 1.0-tau1.Get(0, 0))
// zero
blasd.Scale(&a10, 0.0)
// ------------------------------------------------------
util.Continue3x3to2x2(
&ATL, &ATR,
&ABL, &ABR, &A00, &a11, &A22, A, util.PTOPLEFT)
util.Continue3x1to2x1(
&tT,
&tB, &t0, &tau1, Tvec, util.PTOP)
}
}
示例8: TestQLBuildwithK
func TestQLBuildwithK(t *testing.T) {
var dc cmat.FloatMatrix
M := 711
N := 707
K := 691
lb := 36
conf := gomas.NewConf()
A := cmat.NewMatrix(M, N)
src := cmat.NewFloatNormSource()
A.SetFrom(src)
tau := cmat.NewMatrix(N, 1)
W := cmat.NewMatrix(M+N, 1)
C := cmat.NewMatrix(N, N)
conf.LB = lb
lapackd.QLFactor(A, tau, W, conf)
A1 := cmat.NewCopy(A)
conf.LB = 0
lapackd.QLBuild(A, tau, W, K, conf)
blasd.Mult(C, A, A, 1.0, 0.0, gomas.TRANSA, conf)
dc.Diag(C)
blasd.Add(&dc, -1.0)
if N < 10 {
t.Logf("unblk.QLBuild Q:\n%v\n", A)
t.Logf("unblk.QLBuild Q.T*Q:\n%v\n", C)
}
n0 := lapackd.NormP(C, lapackd.NORM_ONE)
conf.LB = lb
W1 := lapackd.Workspace(lapackd.QLBuildWork(A1, conf))
lapackd.QLBuild(A1, tau, W1, K, conf)
if N < 10 {
t.Logf("blk.QLBuild Q:\n%v\n", A1)
}
// compute: I - Q.T*Q
blasd.Mult(C, A1, A1, 1.0, 0.0, gomas.TRANSA, conf)
blasd.Add(&dc, -1.0)
n1 := lapackd.NormP(C, lapackd.NORM_ONE)
blasd.Plus(A, A1, 1.0, -1.0, gomas.NONE)
n2 := lapackd.NormP(A, lapackd.NORM_ONE)
t.Logf("M=%d, N=%d, K=%d ||unblk.QLBuild(A) - blk.QLBuild(A)||_1 :%e\n", M, N, K, n2)
t.Logf("unblk M=%d, N=%d, K=%d ||Q.T*Q - I||_1 : %e\n", M, N, K, n0)
t.Logf("blk M=%d, N=%d, K=%d ||Q.T*Q - I||_1 : %e\n", M, N, K, n1)
}
示例9: TestLQBuild
func TestLQBuild(t *testing.T) {
var dc cmat.FloatMatrix
M := 877
N := 913
K := 831
lb := 48
conf := gomas.NewConf()
_ = lb
A := cmat.NewMatrix(M, N)
src := cmat.NewFloatNormSource()
A.SetFrom(src)
tau := cmat.NewMatrix(M, 1)
W := cmat.NewMatrix(M, 1)
C := cmat.NewMatrix(M, M)
dc.Diag(C)
conf.LB = lb
lapackd.LQFactor(A, tau, W, conf)
A1 := cmat.NewCopy(A)
conf.LB = 0
lapackd.LQBuild(A, tau, W, K, conf)
if N < 10 {
t.Logf("unblk.LQBuild Q:\n%v\n", A)
}
blasd.Mult(C, A, A, 1.0, 0.0, gomas.TRANSB, conf)
blasd.Add(&dc, -1.0)
n0 := lapackd.NormP(C, lapackd.NORM_ONE)
conf.LB = lb
W2 := lapackd.Workspace(lapackd.LQBuildWork(A, conf))
lapackd.LQBuild(A1, tau, W2, K, conf)
if N < 10 {
t.Logf("blk.LQBuild Q:\n%v\n", A1)
}
blasd.Mult(C, A1, A1, 1.0, 0.0, gomas.TRANSB, conf)
blasd.Add(&dc, -1.0)
n1 := lapackd.NormP(C, lapackd.NORM_ONE)
blasd.Plus(A, A1, 1.0, -1.0, gomas.NONE)
n2 := lapackd.NormP(A, lapackd.NORM_ONE)
t.Logf("M=%d, N=%d, K=%d ||unblk.LQBuild(A) - blk.LQBuild(A)||_1 :%e\n", M, N, K, n2)
t.Logf("unblk M=%d, N=%d, K=%d ||I - Q*Q.T||_1 : %e\n", M, N, K, n0)
t.Logf(" blk M=%d, N=%d, K=%d ||I - Q*Q.T||_1 : %e\n", M, N, K, n1)
}
示例10: test_trdevd
func test_trdevd(N, flags, kind int, verbose bool, t *testing.T) {
var At, sD, sE, tmp cmat.FloatMatrix
A0 := cmat.NewMatrix(N, N)
desc := setTrdDiagonals(A0, kind)
At.SubMatrix(A0, 0, 0, N, N)
sD.Diag(A0, 0)
sE.Diag(A0, 1)
D := cmat.NewCopy(&sD)
E := cmat.NewCopy(&sE)
V := cmat.NewMatrix(N, N)
sD.Diag(V, 0)
sD.Add(1.0)
W := cmat.NewMatrix(4*N, 1)
C := cmat.NewMatrix(N, N)
if verbose && N < 10 {
t.Logf("A0:\n%v\n", A0.ToString("%6.3f"))
t.Logf("V.pre:\n%v\n", V.ToString("%6.3f"))
}
lapackd.TRDEigen(D, E, V, W, flags|gomas.WANTV)
for k := 0; k < N-1; k++ {
if E.GetAt(k) != 0.0 {
t.Logf("E[%d] != 0.0 (%e)\n", k, E.GetAt(k))
}
}
blasd.Mult(C, V, V, 1.0, 0.0, gomas.TRANSB)
sD.Diag(C)
sD.Add(-1.0)
nrmv := lapackd.NormP(C, lapackd.NORM_ONE)
blasd.Mult(C, V, A0, 1.0, 0.0, gomas.TRANSA)
blasd.Mult(&At, C, V, 1.0, 0.0, gomas.NONE)
if verbose && N < 10 {
t.Logf("D:\n%v\n", asRow(&tmp, D).ToString("%6.3f"))
t.Logf("V:\n%v\n", V.ToString("%6.3f"))
t.Logf("V.T*A*V\n%v\n", At.ToString("%6.3f"))
}
sD.Diag(&At)
blasd.Axpy(&sD, D, -1.0)
nrma := lapackd.NormP(&At, lapackd.NORM_ONE)
t.Logf("N=%d [%s] ||V.T*A*V - eigen(A)||_1: %e\n", N, desc, nrma)
t.Logf(" ||I - V.T*V||_1: %e\n", nrmv)
}
示例11: TestQRBuild
func TestQRBuild(t *testing.T) {
var d cmat.FloatMatrix
M := 911
N := 899
K := 873
lb := 36
conf := gomas.NewConf()
A := cmat.NewMatrix(M, N)
src := cmat.NewFloatNormSource()
A.SetFrom(src)
tau := cmat.NewMatrix(N, 1)
W := cmat.NewMatrix(N+M, 1)
C := cmat.NewMatrix(N, N)
d.Diag(C)
conf.LB = lb
lapackd.QRFactor(A, tau, W, conf)
A1 := cmat.NewCopy(A)
conf.LB = 0
lapackd.QRBuild(A, tau, W, K, conf)
blasd.Mult(C, A, A, 1.0, 0.0, gomas.TRANSA, conf)
blasd.Add(&d, -1.0)
n0 := lapackd.NormP(C, lapackd.NORM_ONE)
conf.LB = lb
W2 := lapackd.Workspace(lapackd.QRBuildWork(A, conf))
lapackd.QRBuild(A1, tau, W2, K, conf)
blasd.Mult(C, A1, A1, 1.0, 0.0, gomas.TRANSA, conf)
blasd.Add(&d, -1.0)
n1 := lapackd.NormP(C, lapackd.NORM_ONE)
blasd.Plus(A, A1, 1.0, -1.0, gomas.NONE)
n2 := lapackd.NormP(A, lapackd.NORM_ONE)
t.Logf("M=%d, N=%d, K=%d ||unblk.QRBuild(A) - blk.QRBuild(A)||_1 :%e\n", M, N, K, n2)
t.Logf("unblk M=%d, N=%d, K=%d ||I - Q.T*Q||_1: %e\n", M, N, K, n0)
t.Logf(" blk M=%d, N=%d, K=%d ||I - Q.T*Q||_1: %e\n", M, N, K, n1)
}
示例12: testEigen
func testEigen(N int, bits int, t *testing.T) {
var A, A0, W, D, V *cmat.FloatMatrix
var sD cmat.FloatMatrix
var s string = "lower"
if bits&gomas.UPPER != 0 {
s = "upper"
}
wsize := N * N
if wsize < 100 {
wsize = 100
}
D = cmat.NewMatrix(N, 1)
A = cmat.NewMatrix(N, N)
V = cmat.NewMatrix(N, N)
src := cmat.NewFloatNormSource()
A.SetFrom(src, cmat.SYMM)
A0 = cmat.NewCopy(A)
W = cmat.NewMatrix(wsize, 1)
if err := lapackd.EigenSym(D, A, W, bits|gomas.WANTV); err != nil {
t.Errorf("EigenSym error: %v\n", err)
return
}
// ||I - V.T*V||
sD.Diag(V)
blasd.Mult(V, A, A, 1.0, 0.0, gomas.TRANSA)
blasd.Add(&sD, -1.0)
nrm1 := lapackd.NormP(V, lapackd.NORM_ONE)
// left vectors are M-by-N
V.Copy(A)
lapackd.MultDiag(V, D, gomas.RIGHT)
blasd.Mult(A0, V, A, -1.0, 1.0, gomas.TRANSB)
nrm2 := lapackd.NormP(A0, lapackd.NORM_ONE)
t.Logf("N=%d, [%s] ||A - V*D*V.T||_1 :%e\n", N, s, nrm2)
t.Logf(" ||I - V.T*V||_1 : %e\n", nrm1)
}
示例13: MVUpdateDiag
/*
* Generic rank update of diagonal matrix.
* diag(D) = diag(D) + alpha * x * y.T
*
* Arguments:
* D N element column or row vector or N-by-N matrix
*
* x, y N element vectors
*
* alpha scalar
*/
func MVUpdateDiag(D, x, y *cmat.FloatMatrix, alpha float64, confs ...*gomas.Config) *gomas.Error {
var d *cmat.FloatMatrix
var d0 cmat.FloatMatrix
if !x.IsVector() || !y.IsVector() {
return gomas.NewError(gomas.ENEED_VECTOR, "MvUpdateDiag")
}
d = D
if !D.IsVector() {
d0.Diag(D)
d = &d0
}
for k := 0; k < d.Len(); k++ {
val := d.GetAt(k)
val += x.GetAt(k) * y.GetAt(k) * alpha
d.SetAt(k, val)
}
return nil
}
示例14: TestDTrmmUnitUpper
func TestDTrmmUnitUpper(t *testing.T) {
var d cmat.FloatMatrix
N := 563
K := 171
A := cmat.NewMatrix(N, N)
B := cmat.NewMatrix(N, K)
B0 := cmat.NewMatrix(N, K)
C := cmat.NewMatrix(N, K)
zeros := cmat.NewFloatConstSource(0.0)
ones := cmat.NewFloatConstSource(1.0)
zeromean := cmat.NewFloatNormSource()
A.SetFrom(zeromean, cmat.UPPER|cmat.UNIT)
B.SetFrom(ones)
B0.SetFrom(ones)
// B = A*B
blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT|gomas.UNIT)
d.Diag(A).SetFrom(ones)
blasd.Mult(C, A, B0, 1.0, 0.0, gomas.NONE)
ok := C.AllClose(B)
t.Logf("trmm(B, A, L|U|N|U) == gemm(C, TriUU(A), B) : %v\n", ok)
B.SetFrom(ones)
// B = A.T*B
d.Diag(A).SetFrom(zeros)
blasd.MultTrm(B, A, 1.0, gomas.UPPER|gomas.LEFT|gomas.TRANSA|gomas.UNIT)
d.Diag(A).SetFrom(ones)
blasd.Mult(C, A, B0, 1.0, 0.0, gomas.TRANSA)
ok = C.AllClose(B)
t.Logf("trmm(B, A, L|U|T|U) == gemm(C, TriUU(A).T, B) : %v\n", ok)
}
示例15: setDiagonals
func setDiagonals(A *cmat.FloatMatrix, offdiag, kind int) string {
var sD, sE cmat.FloatMatrix
var desc string
switch kind {
case 2:
desc = "middle"
case 1:
desc = "top "
default:
desc = "bottom"
}
sD.Diag(A, 0)
sE.Diag(A, offdiag)
N := sD.Len()
for k := 0; k < N; k++ {
if k < N-1 {
sE.SetAt(k, 1.0)
}
switch kind {
case 2: // midheavy
if k < N/2 {
sD.SetAt(k, float64(k+1))
} else {
sD.SetAt(k, float64(N-k))
}
break
case 1: // top heavy
sD.SetAt(N-1-k, float64(k+1))
break
default: // bottom heavy
sD.SetAt(k, float64(k+1))
break
}
}
return desc
}