def Scale_a_matrix_unb_var2(beta, A): """ Scale_a_matrix_unb_var2(scalar, matrix) Scales matrix A by a factor of beta. Traverses matrix A from TOP to BOTTOM. """ AT, \ AB = flame.part_2x1(A, \ 0, 'TOP') while AT.shape[0] < A.shape[0]: A0, \ a1t, \ A2 = flame.repart_2x1_to_3x1(AT, \ AB, \ 1, 'BOTTOM') laff.scal(beta,a1t) AT, \ AB = flame.cont_with_3x1_to_2x1(A0, \ a1t, \ A2, \ 'TOP') flame.merge_2x1(AT, \ AB, A)
def Trmv_ln_unb_var2(L, x): """ Trmv_ln_unb_var2(matrix, vector) Computes y = L * x using AXPY operations. L is the lower triangular matrix. Traverses matrix L from BOTTOM-RIGHT to TOP-LEFT, vector x from BOTTOM to TOP. """ LTL, LTR, \ LBL, LBR = flame.part_2x2(L, \ 0, 0, 'BR') xT, \ xB = flame.part_2x1(x, \ 0, 'BOTTOM') while LBR.shape[0] < L.shape[0]: L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22 = flame.repart_2x2_to_3x3(LTL, LTR, \ LBL, LBR, \ 1, 1, 'TL') x0, \ chi1, \ x2 = flame.repart_2x1_to_3x1(xT, \ xB, \ 1, 'TOP') laff.axpy( chi1, l21, x2 ) laff.scal( lambda11, chi1 ) LTL, LTR, \ LBL, LBR = flame.cont_with_3x3_to_2x2(L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22, \ 'BR') xT, \ xB = flame.cont_with_3x1_to_2x1(x0, \ chi1, \ x2, \ 'BOTTOM') flame.merge_2x1(xT, \ xB, x)
def Trmv_un_unb_var2(U, x): """ Trmv_un_unb_var2(matrix, vector) Computes y = U * x using AXPY operations. U is the upper triangular matrix. Traverses matrix U from TOP-LEFT to BOTTOM-RIGHT, vector x from TOP to BOTTOM. """ UTL, UTR, \ UBL, UBR = flame.part_2x2(U, \ 0, 0, 'TL') xT, \ xB = flame.part_2x1(x, \ 0, 'TOP') while UTL.shape[0] < U.shape[0]: U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22 = flame.repart_2x2_to_3x3(UTL, UTR, \ UBL, UBR, \ 1, 1, 'BR') x0, \ chi1, \ x2 = flame.repart_2x1_to_3x1(xT, \ xB, \ 1, 'BOTTOM') laff.axpy( chi1, u01, x0 ) laff.scal( upsilon11, chi1 ) UTL, UTR, \ UBL, UBR = flame.cont_with_3x3_to_2x2(U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22, \ 'TL') xT, \ xB = flame.cont_with_3x1_to_2x1(x0, \ chi1, \ x2, \ 'TOP') flame.merge_2x1(xT, \ xB, x)
def Trmv_un_unb_var1(U, x): """ Trmv_un_unb_var1(matrix, vector) Computes y = U * x using DOT products. U is the upper triangular matrix. Traverses matrix U from TOP-LEFT to BOTTOM-RIGHT, vector x from TOP to BOTTOM. """ UTL, UTR, \ UBL, UBR = flame.part_2x2(U, \ 0, 0, 'TL') xT, \ xB = flame.part_2x1(x, \ 0, 'TOP') while UTL.shape[0] < U.shape[0]: U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22 = flame.repart_2x2_to_3x3(UTL, UTR, \ UBL, UBR, \ 1, 1, 'BR') x0, \ chi1, \ x2 = flame.repart_2x1_to_3x1(xT, \ xB, \ 1, 'BOTTOM') laff.scal(upsilon11, chi1) laff.dots(u12t, x2, chi1) UTL, UTR, \ UBL, UBR = flame.cont_with_3x3_to_2x2(U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22, \ 'TL') xT, \ xB = flame.cont_with_3x1_to_2x1(x0, \ chi1, \ x2, \ 'TOP') flame.merge_2x1(xT, \ xB, x)
def Trmv_ut_unb_var1(U, x): """ Trmv_ut_unb_var1(matrix, vector) Computes x = U' * x using DOT products. Traverses matrix U from BOTTOM-RIGHT to TOP-LEFT, vector x from BOTTOM to TOP. """ UTL, UTR, \ UBL, UBR = flame.part_2x2(U, \ 0, 0, 'BR') xT, \ xB = flame.part_2x1(x, \ 0, 'BOTTOM') while UBR.shape[0] < U.shape[0]: U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22 = flame.repart_2x2_to_3x3(UTL, UTR, \ UBL, UBR, \ 1, 1, 'TL') x0, \ chi1, \ x2 = flame.repart_2x1_to_3x1(xT, \ xB, \ 1, 'TOP') laff.scal( upsilon11, chi1 ) laff.dots( u01, x0, chi1 ) UTL, UTR, \ UBL, UBR = flame.cont_with_3x3_to_2x2(U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22, \ 'BR') xT, \ xB = flame.cont_with_3x1_to_2x1(x0, \ chi1, \ x2, \ 'BOTTOM') flame.merge_2x1(xT, \ xB, x)
def Trmv_ut_unb_var2(U, x): """ Trmv_ut_unb_var2(matrix, vector) Computes x = U' * x using AXPY operations. Traverses matrix U from BOTTOM-RIGHT to TOP-LEFT, vector x from BOTTOM to TOP. """ UTL, UTR, \ UBL, UBR = flame.part_2x2(U, \ 0, 0, 'BR') xT, \ xB = flame.part_2x1(x, \ 0, 'BOTTOM') while UBR.shape[0] < U.shape[0]: U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22 = flame.repart_2x2_to_3x3(UTL, UTR, \ UBL, UBR, \ 1, 1, 'TL') x0, \ chi1, \ x2 = flame.repart_2x1_to_3x1(xT, \ xB, \ 1, 'TOP') laff.axpy(chi1, u12t, x2) laff.scal(upsilon11, chi1) UTL, UTR, \ UBL, UBR = flame.cont_with_3x3_to_2x2(U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22, \ 'BR') xT, \ xB = flame.cont_with_3x1_to_2x1(x0, \ chi1, \ x2, \ 'BOTTOM') flame.merge_2x1(xT, \ xB, x)
def Trmv_lt_unb_var1(L, x): """ Trmv_lt_unb_var1(matrix, vector) Computes x = L' * x using DOT products. Traverses matrix L from TOP-LEFT to BOTTOM-RIGHT, vector x from TOP to BOTTOM. """ LTL, LTR, \ LBL, LBR = flame.part_2x2(L, \ 0, 0, 'TL') xT, \ xB = flame.part_2x1(x, \ 0, 'TOP') while LTL.shape[0] < L.shape[0]: L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22 = flame.repart_2x2_to_3x3(LTL, LTR, \ LBL, LBR, \ 1, 1, 'BR') x0, \ chi1, \ x2 = flame.repart_2x1_to_3x1(xT, \ xB, \ 1, 'BOTTOM') laff.scal(lambda11, chi1) laff.dots(l21, x2, chi1) LTL, LTR, \ LBL, LBR = flame.cont_with_3x3_to_2x2(L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22, \ 'TL') xT, \ xB = flame.cont_with_3x1_to_2x1(x0, \ chi1, \ x2, \ 'TOP') flame.merge_2x1(xT, \ xB, x)
def norm2( x ): """ Compute the 2-norm of a vector, returning alpha x can be a row or column vector. """ assert type(x) is np.matrix and len(x.shape) is 2, \ "laff.norm2: vector x must be a 2D numpy.matrix" m, n = np.shape(x) assert m is 1 or n is 1, \ "laff.norm2: x is not a vector" #Ensure that we don't modify x in #any way by copying it to a new vector, y y = np.matrix( np.zeros( (m,n) ) ) laff.copy( x, y ) #Initialize variables that we will use to appropriate values alpha = 0 maxval = y[ 0, 0 ] if m is 1: #y is a row #Find a value to scale by to avoid under/overflow for i in range(n): if abs(y[ 0, i ]) > maxval: maxval = abs(y[ 0, i ]) elif n is 1: #y is a column #Find a value to scale by to avoid under/overflow for i in range(m): if abs(y[ i, 0 ]) > maxval: maxval = abs(y[ i, 0 ]) #If y is the zero vector, return 0 if abs(maxval) < 1e-7: return 0 #Scale all of the values by 1/maxval to prevent under/overflow laff.scal( 1.0/maxval, y ) alpha = maxval * sqrt( laff.dot( y, y ) ) return alpha
def trsv_ltu(L, B): LTL, LTR, \ LBL, LBR = flame.part_2x2(L, \ 0, 0, 'TL') BT, \ BB = flame.part_2x1(B, \ 0, 'TOP') while LTL.shape[0] < L.shape[0]: L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22 = flame.repart_2x2_to_3x3(LTL, LTR, \ LBL, LBR, \ 1, 1, 'BR') B0, \ b1t, \ B2 = flame.repart_2x1_to_3x1(BT, \ BB, \ 1, 'BOTTOM') #------------------------------------------------------------# dots( -l21, b2, beta1 ) scal( 1/lambda11, beta1 ) #------------------------------------------------------------# LTL, LTR, \ LBL, LBR = flame.cont_with_3x3_to_2x2(L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22, \ 'TL') BT, \ BB = flame.cont_with_3x1_to_2x1(B0, \ b1t, \ B2, \ 'TOP') flame.merge_2x1(BT, \ BB, B)
def Utrsv_notranspose_nonunit(U, b): UTL, UTR, \ UBL, UBR = flame.part_2x2(U, \ 0, 0, 'BR') bT, \ bB = flame.part_2x1(b, \ 0, 'BOTTOM') while UBR.shape[0] < U.shape[0]: U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22 = flame.repart_2x2_to_3x3(UTL, UTR, \ UBL, UBR, \ 1, 1, 'TL') b0, \ beta1, \ b2 = flame.repart_2x1_to_3x1(bT, \ bB, \ 1, 'TOP') #------------------------------------------------------------# laff.dots( -u12t, b2, beta1 ) laff.scal( 1/upsilon11, beta1 ) #------------------------------------------------------------# UTL, UTR, \ UBL, UBR = flame.cont_with_3x3_to_2x2(U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22, \ 'BR') bT, \ bB = flame.cont_with_3x1_to_2x1(b0, \ beta1, \ b2, \ 'BOTTOM') flame.merge_2x1(bT, \ bB, b)
def Utrsv_notranspose_nonunit(U, b): UTL, UTR, \ UBL, UBR = flame.part_2x2(U, \ 0, 0, 'BR') bT, \ bB = flame.part_2x1(b, \ 0, 'BOTTOM') while UBR.shape[0] < U.shape[0]: U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22 = flame.repart_2x2_to_3x3(UTL, UTR, \ UBL, UBR, \ 1, 1, 'TL') b0, \ beta1, \ b2 = flame.repart_2x1_to_3x1(bT, \ bB, \ 1, 'TOP') #------------------------------------------------------------# laff.dots(-u12t, b2, beta1) laff.scal(1 / upsilon11, beta1) #------------------------------------------------------------# UTL, UTR, \ UBL, UBR = flame.cont_with_3x3_to_2x2(U00, u01, U02, \ u10t, upsilon11, u12t, \ U20, u21, U22, \ 'BR') bT, \ bB = flame.cont_with_3x1_to_2x1(b0, \ beta1, \ b2, \ 'BOTTOM') flame.merge_2x1(bT, \ bB, b)
def trsv_lnn(L, b): LTL, LTR, \ LBL, LBR = flame.part_2x2(L, \ 0, 0, 'TL') bT, \ bB = flame.part_2x1(b, \ 0, 'TOP') while LTL.shape[0] < L.shape[0]: L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22 = flame.repart_2x2_to_3x3(LTL, LTR, \ LBL, LBR, \ 1, 1, 'BR') b0, \ beta1, \ b2 = flame.repart_2x1_to_3x1(bT, \ bB, \ 1, 'BOTTOM') #------------------------------------------------------------# scal( 1./lambda11, beta1 ) axpy( -beta1, l21, b2 ) #------------------------------------------------------------# LTL, LTR, \ LBL, LBR = flame.cont_with_3x3_to_2x2(L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22, \ 'TL') bT, \ bB = flame.cont_with_3x1_to_2x1(b0, \ beta1, \ b2, \ 'TOP') flame.merge_2x1(bT, \ bB, b)
def trsv_lnn(L, b): LTL, LTR, \ LBL, LBR = flame.part_2x2(L, \ 0, 0, 'TL') bT, \ bB = flame.part_2x1(b, \ 0, 'TOP') while LTL.shape[0] < L.shape[0]: L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22 = flame.repart_2x2_to_3x3(LTL, LTR, \ LBL, LBR, \ 1, 1, 'BR') b0, \ beta1, \ b2 = flame.repart_2x1_to_3x1(bT, \ bB, \ 1, 'BOTTOM') #------------------------------------------------------------# scal(1. / lambda11, beta1) axpy(-beta1, l21, b2) #------------------------------------------------------------# LTL, LTR, \ LBL, LBR = flame.cont_with_3x3_to_2x2(L00, l01, L02, \ l10t, lambda11, l12t, \ L20, l21, L22, \ 'TL') bT, \ bB = flame.cont_with_3x1_to_2x1(b0, \ beta1, \ b2, \ 'TOP') flame.merge_2x1(bT, \ bB, b)
def norm2( x ): """ Compute the 2-norm of a vector, returning alpha x can be a row or column vector. """ assert type(x) is np.matrix and len(x.shape) is 2, \ "laff.norm2: vector x must be a 2D numpy.matrix" m, n = np.shape(x) assert m is 1 or n is 1, \ "laff.norm2: x is not a vector" #Ensure that we don't modify x in #any way by copying it to a new vector, y y = np.matrix( np.zeros( (m,n) ) ) laff.copy( x, y ) #Initialize variables that we will use to appropriate values alpha = 0 maxval = y[ 0, 0 ] if m is 1: #y is a row #Find a value to scale by to avoid under/overflow for i in range(n): if abs(y[ 0, i ]) > maxval: maxval = y[ 0, i ] elif n is 1: #y is a column #Find a value to scale by to avoid under/overflow for i in range(m): if abs(y[ i, 0 ]) > maxval: maxval = y[ i, 0 ] #If y is the zero vector, return 0 if maxval is 0: return 0 #Scale all of the values by 1/maxval to prevent under/overflow laff.scal( 1.0/maxval, y ) alpha = maxval * sqrt( laff.dot( y, y ) ) return alpha
def Scale_a_matrix_unb_var1(beta, A): """ Scale_a_matrix_unb_var1(scalar, matrix) Scales matrix A by a factor of beta. Traverses matrix A from LEFT to RIGHT. """ AL, AR = flame.part_1x2(A, \ 0, 'LEFT') while AL.shape[1] < A.shape[1]: A0, a1, A2 = flame.repart_1x2_to_1x3(AL, AR, \ 1, 'RIGHT') laff.scal(beta,a1) AL, AR = flame.cont_with_1x3_to_1x2(A0, a1, A2, \ 'LEFT') flame.merge_1x2(AL, AR, A)
def norm2(x): """ """ assert type(x) is np.matrix and len( x.shape) is 2, "laff.norm2: vector x must be a 2D numpy.matrix" m, n = np.shape(x) assert m is 1 or n is 1, "laff.norm2: x is not a vector" y = np.matrix(np.zeros((m, n))) laff.copy(x, y) alpha = 0 maxval = y[0, 0] if m is 1: #y is a row for i in range(n): if abs(y[0, i]) > maxval: maxval = abs(y[0, i]) elif n is 1: #y is a column for i in range(m): if abs(y[i, 0]) > maxval: maxval = abs(y[i, 0]) if abs(maxval) < 1e-7: return 0 laff.scal(1.0 / maxval, y) alpha = maxval * sqrt(laff.dot(y, y)) return alpha
def norm2( x ): """ """ assert type(x) is np.matrix and len(x.shape) is 2, "laff.norm2: vector x must be a 2D numpy.matrix" m, n = np.shape(x) assert m is 1 or n is 1, "laff.norm2: x is not a vector" y = np.matrix( np.zeros( (m,n) ) ) laff.copy( x, y ) alpha = 0 maxval = y[ 0, 0 ] if m is 1: #y is a row for i in range(n): if abs(y[ 0, i ]) > maxval: maxval = abs(y[ 0, i ]) elif n is 1: #y is a column for i in range(m): if abs(y[ i, 0 ]) > maxval: maxval = abs(y[ i, 0 ]) if abs(maxval) < 1e-7: return 0 laff.scal( 1.0/maxval, y ) alpha = maxval * sqrt( laff.dot( y, y ) ) return alpha