sp = gp + sp*beta sx = gx + sx*beta sr = gr + sr*beta gnp = gn alpha = sr.dot(sr)+eps*(sp.dot(sp)-sx.dot(sx)) alpha = -gn/alpha p = p + sp*alpha x = x + sx*alpha r = r + sr*alpha return x if __name__ == "__main__": # test matrix and data matrix = rsf.File([[1,1,1,0], [1,2,0,0], [1,3,1,0], [1,4,0,1], [1,5,1,1]]) y = rsf.File([3,3,5,7,9]) x0 = rsf.File([0,0,0,0]) # matrix multiplication operator matmult = rsf.matmult(mat=matrix) copy = rsf.cp(x=1) x = conjgrad(matmult,copy,1,y,x0,6) y2 = matmult[x] print x[:] print y2[:]
for iter in range(niter): g = oper(adj=1)[R] G = oper(adj=0)[g] RG = R.dot(G) print "iter %d: %g" % (iter+1,RG) alpha = - RG/G.dot(G) x = x+g*alpha R = R+G*alpha return x if __name__ == "__main__": # test matrix and data matrix = rsf.File([[1,1,1,0], [1,2,0,0], [1,3,1,0], [1,4,0,1], [1,5,1,1]]) y = rsf.File([3,3,5,7,9]) x0 = rsf.File([0,0,0,0]) # matrix multiplication operator matmult = rsf.matmult(mat=matrix) # Using function above x = steepd(matmult,y,x0,100) y2 = matmult[x] print x[:] print y2[:]
cs = cg else: beta = gn/gnp s = g+ s*beta cs = cg+cs*beta gnp = gn alpha = -gn/cs.dot(s) x = x+ s*alpha g = g+cs*alpha return x if __name__ == "__main__": # test matrix and data matrix = rsf.File([[1,1,1,0], [1,2,0,0], [1,3,1,0], [1,4,0,1], [1,5,1,1]]) y = rsf.File([3,3,5,7,9]) x0 = rsf.File([0,0,0,0]) # matrix multiplication operator d = rsf.matmult(mat=matrix,adj=1)[y] matmult = rsf.matmult(mat=matrix,adj=0).matmult(mat=matrix,adj=1) x = conjgrad(matmult,d,x0,6) y2 = matmult[x] print x[:] print y2[:] print d[:]
print "iter %d: %g" % (iter + 1, gn) if 0 == iter: s = g cs = cg else: beta = gn / gnp s = g + s * beta cs = cg + cs * beta gnp = gn alpha = -gn / cs.dot(s) x = x + s * alpha g = g + cs * alpha return x if __name__ == "__main__": # test matrix and data matrix = rsf.File([[1, 1, 1, 0], [1, 2, 0, 0], [1, 3, 1, 0], [1, 4, 0, 1], [1, 5, 1, 1]]) y = rsf.File([3, 3, 5, 7, 9]) x0 = rsf.File([0, 0, 0, 0]) # matrix multiplication operator d = rsf.matmult(mat=matrix, adj=1)[y] matmult = rsf.matmult(mat=matrix, adj=0).matmult(mat=matrix, adj=1) x = conjgrad(matmult, d, x0, 6) y2 = matmult[x] print x[:] print y2[:] print d[:]
## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import rsf.api as rsf random = rsf.noise(rep=1,type=0) def dottest(oper,mod,dat): 'Dot product test' mod = random[mod] dat = random[dat] print " L[m]*d=%g" % oper(adj=0)[mod].dot(dat) print "L'[d]*m=%g" % oper(adj=1)[dat].dot(mod) if __name__ == "__main__": # Create random matrix matrix = rsf.spike(n1=10,n2=5,d1=1,d2=1).noise(rep=1,type=0)[0] # Matrix multiplication operator oper = rsf.matmult(mat=matrix) # Model space and data space vectors model = rsf.window(n2=1)[matrix] data = rsf.window(n1=1)[matrix] # Using sfdottest rsf.dottest(0,oper,mod=model,dat=data) # Using function above dottest(oper,model,data)
## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA import rsf.api as rsf random = rsf.noise(rep=1, type=0) def dottest(oper, mod, dat): 'Dot product test' mod = random[mod] dat = random[dat] print " L[m]*d=%g" % oper(adj=0)[mod].dot(dat) print "L'[d]*m=%g" % oper(adj=1)[dat].dot(mod) if __name__ == "__main__": # Create random matrix matrix = rsf.spike(n1=10, n2=5, d1=1, d2=1).noise(rep=1, type=0)[0] # Matrix multiplication operator oper = rsf.matmult(mat=matrix) # Model space and data space vectors model = rsf.window(n2=1)[matrix] data = rsf.window(n1=1)[matrix] # Using sfdottest rsf.dottest(0, oper, mod=model, dat=data) # Using function above dottest(oper, model, data)