def givensCoef(x1_in,x2_in): """ Code adapted from Algorithm 1 of LAPACK working Notes lawn148 """ if type(x1_in) is int: x1=float(x1_in) else: x1 = x1_in if type(x2_in) is int: x2=float(x2_in) else: x2 = x2_in if type(x1) is float and type(x2) is float: t=pv.vsip_hypot_d(x1,x2) if x2 == 0.0: return (1.0,0.0,x1) elif x1 == 0.0: return (0.0,sign(x2),t) else: # return (c,s,r) sn=sign(x1) return(pv.vsip_mag_d(x1)/t,sn*x2/t,sn*t) elif type(x1) is complex or type(x2) is complex: mx1=pv.vsip_hypot_d(x1.real,x1.imag) mx2=pv.vsip_hypot_d(x2.real,x2.imag) if mx2 == 0.0: return(1.0,0.0,x1) elif mx1 == 0.0: return(0,sign(x2.conjugate()),mx2) else: t=pv.vsip_hypot_d(mx1,mx2) c=mx1/t sn=sign(x1) s=(sn * x2.conjugate())/t r=sn * t return(c,s,r) else: print('Type <:'+repr(type(x1)) + ':> or <:'+ \ repr(type(x2))+':> not recognized by givensCoef') return
def diagPhaseToZero(L,B): """ To phase shift the main diagonal entries of a matrix B so entries are real (imaginary zero) use this routine. """ d = B.diagview(0) for i in range(d.length): ps=d[i] #phase shift if ps.imag != 0.0: #ignore if already real m = pv.vsip_hypot_d(ps.real,ps.imag) ps /= m L.colview(i)[:] *= ps B.rowview(i)[:] *= ps # if B is strictly diagonal don't need this step d[i] = m
def diagPhaseToZero(L, B): """ To phase shift the main diagonal entries of a matrix B so entries are real (imaginary zero) use this routine. """ d = B.diagview(0) for i in range(d.length): ps = d[i] # phase shift if ps.imag != 0.0: # ignore if already real m = pv.vsip_hypot_d(ps.real, ps.imag) ps /= m L.colview(i)[:] *= ps B.rowview(i)[:] *= ps # if B is strictly diagonal don't need this step d[i] = m
def givensCoef(x1_in, x2_in): """ Code adapted from Algorithm 1 of LAPACK working Notes lawn148 """ if type(x1_in) is int: x1 = float(x1_in) else: x1 = x1_in if type(x2_in) is int: x2 = float(x2_in) else: x2 = x2_in if type(x1) is float and type(x2) is float: t = pv.vsip_hypot_d(x1, x2) if x2 == 0.0: return (1.0, 0.0, x1) elif x1 == 0.0: return (0.0, sign(x2), t) else: # return (c,s,r) sn = sign(x1) return (pv.vsip_mag_d(x1) / t, sn * x2 / t, sn * t) elif type(x1) is complex or type(x2) is complex: mx1 = pv.vsip_hypot_d(x1.real, x1.imag) mx2 = pv.vsip_hypot_d(x2.real, x2.imag) if mx2 == 0.0: return (1.0, 0.0, x1) elif mx1 == 0.0: return (0, sign(x2.conjugate()), mx2) else: t = pv.vsip_hypot_d(mx1, mx2) c = mx1 / t sn = sign(x1) s = (sn * x2.conjugate()) / t r = sn * t return (c, s, r) else: print("Type <:" + repr(type(x1)) + ":> or <:" + repr(type(x2)) + ":> not recognized by givensCoef") return
def sign(a_in): # see LAPACK Working Notes 148 for definition of sign if type(a_in) is int: a=float(a_in) else: a=a_in if type(a) is float or type(a) is complex: t=pv.vsip_hypot_d(a.real,a.imag) if t == 0.0: return 1.0 elif a.imag==0.0: if a.real < 0.0: return -1.0 else: return 1.0 else: return a/t else: print('sign function only works on scalars') return
def biDiagPhaseToZero(L,d,f,R,eps0): """ For a Bidiagonal matrix B This routine uses subview vectors `d=B.diagview(0)` and `f=B.diagview(1)` and phase shifts vectors d and f so that B has zero complex part. Matrices L and R are update matrices. eps0 is a small real number used to check for zero. If an element meets a zero check then that element is set to zero. """ for i in range(d.length): ps=d[i] if ps.imag == 0.0: m = ps.real if m < 0.0: ps=-1.0 else: ps= 1.0 m = abs(m) else: m=pv.vsip_hypot_d(ps.real,ps.imag) ps /= m if m > eps0: L.colview(i)[:] *= ps d[i] = m if i < f.length: f[i] *= ps.conjugate() else: d[i] = 0.0 svdZeroCheckAndSet(eps0,d,f) for i in range(f.length-1): j=i+1 ps = f[i] if ps.imag == 0.0: m = ps.real if m < 0.0: ps=-1.0 else: ps= 1.0 m = abs(m) else: m=pv.vsip_hypot_d(ps.real,ps.imag) ps /= m L.colview(j)[:] *= ps.conjugate() R.rowview(j)[:] *= ps f[i] = m; f[j] *= ps j=f.length i=j-1 ps=f[i] if ps.imag == 0.0: m = ps.real if m < 0.0: ps=-1.0 else: ps= 1.0 m = abs(m) else: m=pv.vsip_hypot_d(ps.real,ps.imag) ps /= m f[i]=m L.colview(j)[:] *= ps.conjugate() R.rowview(j)[:] *= ps
def biDiagPhaseToZero(L, d, f, R, eps0): """ For a Bidiagonal matrix B This routine uses subview vectors `d=B.diagview(0)` and `f=B.diagview(1)` and phase shifts vectors d and f so that B has zero complex part. Matrices L and R are update matrices. eps0 is a small real number used to check for zero. If an element meets a zero check then that element is set to zero. """ for i in range(d.length): ps = d[i] if ps.imag == 0.0: m = ps.real if m < 0.0: ps = -1.0 else: ps = 1.0 m = abs(m) else: m = pv.vsip_hypot_d(ps.real, ps.imag) ps /= m if m > eps0: L.colview(i)[:] *= ps d[i] = m if i < f.length: f[i] *= ps.conjugate() else: d[i] = 0.0 svdZeroCheckAndSet(eps0, d, f) for i in range(f.length - 1): j = i + 1 ps = f[i] if ps.imag == 0.0: m = ps.real if m < 0.0: ps = -1.0 else: ps = 1.0 m = abs(m) else: m = pv.vsip_hypot_d(ps.real, ps.imag) ps /= m L.colview(j)[:] *= ps.conjugate() R.rowview(j)[:] *= ps f[i] = m f[j] *= ps j = f.length i = j - 1 ps = f[i] if ps.imag == 0.0: m = ps.real if m < 0.0: ps = -1.0 else: ps = 1.0 m = abs(m) else: m = pv.vsip_hypot_d(ps.real, ps.imag) ps /= m f[i] = m L.colview(j)[:] *= ps.conjugate() R.rowview(j)[:] *= ps