Exemplo n.º 1
0
 def check_simple(self):
     a = [[8,2,3],[2,9,3],[3,3,6]]
     c = cholesky(a)
     assert_array_almost_equal(dot(transpose(c),c),a)
     c = transpose(c)
     a = dot(c,transpose(c))
     assert_array_almost_equal(cholesky(a,lower=1),c)
Exemplo n.º 2
0
 def check_simple_complex(self):
     m = array([[3+1j,3+4j,5],[0,2+2j,2+7j],[0,0,7+4j]])
     a = dot(transpose(conjugate(m)),m)
     c = cholesky(a)
     a1 = dot(transpose(conjugate(c)),c)
     assert_array_almost_equal(a,a1)
     c = transpose(c)
     a = dot(c,transpose(conjugate(c)))
     assert_array_almost_equal(cholesky(a,lower=1),c)
Exemplo n.º 3
0
 def check_random_complex(self):
     n = 20
     for k in range(2):
         m = random([n,n])+1j*random([n,n])
         for i in range(n):
             m[i,i] = 20*(.1+abs(m[i,i]))
         a = dot(transpose(conjugate(m)),m)
         c = cholesky(a)
         a1 = dot(transpose(conjugate(c)),c)
         assert_array_almost_equal(a,a1)
         c = transpose(c)
         a = dot(c,transpose(conjugate(c)))
         assert_array_almost_equal(cholesky(a,lower=1),c)
Exemplo n.º 4
0
 def check_random(self):
     n = 20
     for k in range(2):
         m = random([n,n])
         for i in range(n):
             m[i,i] = 20*(.1+m[i,i])
         a = dot(transpose(m),m)
         c = cholesky(a)
         a1 = dot(transpose(c),c)
         assert_array_almost_equal(a,a1)
         c = transpose(c)
         a = dot(c,transpose(c))
         assert_array_almost_equal(cholesky(a,lower=1),c)
Exemplo n.º 5
0
	def __new__(subtype, 
				eval_fun,	
				base_mesh,
				obs_mesh, 
				obs_taus, 
				withsigma = False, 
				withtau = False, 
				lintrans = None, 
				**params):
		
		# You may need to reshape these so f2py doesn't puke.
		subtype.base_mesh = base_mesh
		subtype.obs_mesh = obs_mesh
		subtype.obs_taus = obs_taus
		subtype.withsigma = withsigma
		subtype.withtau = withtau
		subtype.lintrans = lintrans
		subtype.params = params
		subtype.eval_fun = eval_fun

		# Call the covariance evaluation function
		length = (base_mesh.shape)[0]
		subtype.data = zeros((length,length), dtype=float)
		eval_fun(subtype.data, base_mesh, base_mesh, symm=True, **params)

		# Condition
		condition(subtype.data, eval_fun, base_mesh, obs_mesh, **params)
		
		if withsigma:
	        # Try Cholesky factorization
	        try:
	            subtype.sigma = cholesky(subtype.data)

	        # If there's a small eigenvalue, diagonalize
	        except linalg.linalg.LinAlgError:
	            subtype.eigval, subtype.eigvec = eigh(subtype.data)
	            subtype.sigma = subtype.eigvec * sqrt(subtype.eigval)
	
		if withtau:
			# Make this faster.
			subtype.tau = inv(subtype.data)
		
		# Return the data
		return subtype.data.view(subtype)
		
	def __array__(self):
		return self.data
		
	def __call__(self, point_1, point_2):
		value = zeros((1,1),dtype=float)
		
		# Evaluate the covariance
		self.eval_fun(value,point_1,point_2,**self.params)
		if not obs_mesh:
			return value[0,0]
		
		# Condition on the observed values
		nobs = shape(obs_mesh)[0]
		ndim = shape(obs_mesh)[1]
		Q = zeros((1,1),dtype=float)
		RF = zeros((2,1),dtype=float)
		
		base_mesh = vstack([point_1,point_2])

		for i in range(len(obs_mesh)):
			om_now = reshape(obs_mesh[i,:],(1,ndim))
			eval_fun(Q,om_now,om_now,**params)
			eval_fun(RF,base_mesh,om_now,**params)
			value -= RF[0]*RF[1]/Q		
		
		return value[0,0]