def calculate_cubic_value_not_a_knot(t, y): n = len(t) h = [] b = [] v = [0] A = zeros(shape=(n, n)) for i in range(0, n - 1): h.append(t[i + 1] - t[i]) b.append(6 * (y[i + 1] - y[i]) / h[i]) A[0][0] = h[1] A[0][1] = - h[1] - h[0] A[0][2] = h[0] for i in range(1, n - 1): A[i][i - 1] = h[i - 1] A[i][i] = 2 * (h[i - 1] + h[i]) A[i][i + 1] = h[i] v.append(b[i] - b[i - 1]) v.append(0) A[n - 1][n - 3] = h[n - 2] A[n - 1][n - 2] = - h[n - 2] - h[n - 3] A[n - 1][n - 1] = h[n - 3] z = linalg.solve(A, v) return z, h
def test_the_matrix_class(self): a = numpy.matrix('1. 2.; 3. 4.') numpy.testing.assert_array_equal(a.transpose(), numpy.matrix('1. 3.; 2. 4.')) numpy.testing.assert_array_almost_equal(a.I, numpy.matrix('-2. 1.; 1.5 -0.5')) b = numpy.matrix('5. 7.') numpy.testing.assert_array_equal(b.transpose(), numpy.matrix('5.;7.')) numpy.testing.assert_array_equal(solve(a,b.transpose()), numpy.matrix('-3.;4.'))
def approximate(points, m): x_vector, y_vector = zip(*points) S = fill_s_matrix(x_vector, m) Y = fill_y_vector(x_vector, y_vector, m) A = linalg.solve(S, Y) def foo(x_point): result = 0 for i in range(0, m+1): result += pow(x_point, i) * A[i] return result return foo
def calculate_quadratic_value_not_a_knot(t, y): n = len(t) d = [(y[1] - y[0]) / (t[1] - t[0])] A = zeros(shape=(n, n)) A[0][0] = 1 for i in range(1, n): A[i][i-1] = 1 A[i][i] = 1 d.append(2 * ((y[i] - y[i-1]) / (t[i] - t[i-1]))) return linalg.solve(A, d)
def mahalanobis_distance(difference, num_random_features): num_samples, _ = np.shape(difference) sigma = np.cov(np.transpose(difference)) mu = np.mean(difference, 0) if num_random_features == 1: stat = float(num_samples * mu ** 2) / float(sigma) else: try: linalg.inv(sigma) except LinAlgError: print('covariance matrix is singular. Pvalue returned is 1.1') warnings.warn('covariance matrix is singular. Pvalue returned is 1.1') return 0 stat = num_samples * mu.dot(linalg.solve(sigma, np.transpose(mu))) return chi2.sf(stat, num_random_features)
def mahalanobis_distance(difference, num_random_features): num_samples, _ = np.shape(difference) sigma = np.cov(np.transpose(difference)) mu = np.mean(difference, 0) if num_random_features == 1: stat = float(num_samples * mu**2) / float(sigma) else: try: linalg.inv(sigma) except LinAlgError: print('covariance matrix is singular. Pvalue returned is 1.1') warnings.warn( 'covariance matrix is singular. Pvalue returned is 1.1') return 0 stat = num_samples * mu.dot(linalg.solve(sigma, np.transpose(mu))) return chi2.sf(stat, num_random_features)
def solve(self): # If Convection NOT Implemented # If Conduction (Neumann) [T(i) - T(i-1)] /DX B *= DX # If Temperature # TODO IMPORTANT # DO NOT GIVE 1-self.M DIRECTLY [1 -2 1] # Instead check If Convection / Conduction if internal nodes are BC # Now only internalNodes = array([1., -2., 1.]) for i in range(1, int(self.M - 1)): self.A[i, range(i - 1, i + 2)] = internalNodes self.B[i] *= self.deltaX**2 # %------------------------------------------------ A = self.A[self.filtera] a = int(sqrt(A.shape[0])) A.shape = ([a, a]) B = self.B[self.bcT] self.T[self.DOF] = solve(A, B)
def solve(self): # If Convection NOT Implemented # If Conduction (Neumann) [T(i) - T(i-1)] /DX B *= DX # If Temperature # TODO IMPORTANT # DO NOT GIVE 1-self.M DIRECTLY [1 -2 1] # Instead check If Convection / Conduction if internal nodes are BC # Now only internalNodes = array([1., -2., 1.]) for i in range(1, int(self.M-1)): self.A[i, range(i-1, i+2)] = internalNodes self.B[i] *= self.deltaX**2 # %------------------------------------------------ A = self.A[self.filtera] a = int(sqrt(A.shape[0])) A.shape = ([a, a]) B = self.B[self.bcT] self.T[self.DOF] = solve(A, B)
def test_simple_array_operations(self): a = array([[1.,2.], [3.,4.]]) numpy.testing.assert_array_equal(a.transpose(), array([[1.,3.], [2.,4.]])) numpy.testing.assert_array_almost_equal(trace(a), 5) inv_a = inv(a) b = array([[-2.,1.], [1.5,-.5]]) self.assertTrue(numpy.allclose(inv_a,b)) i = dot(a,inv_a) numpy.testing.assert_array_almost_equal(i, eye(2), 1) numpy.testing.assert_array_almost_equal(inv_a, b) # system of linear equations a = array([[3,2,-1], [2,-2,4], [-1,0.5,-1]]) b = array([1,-2,0]) c = solve(a,b) d = dot(a,c) numpy.testing.assert_array_almost_equal(b, d, 1) a = array([[.8,.3], [.2,.7]]) eigen_values, eigen_vectors = eig(a) lambda_1 = eigen_values[0] x_1 = eigen_vectors[:,0] lambda_2 = eigen_values[1] x_2 = eigen_vectors[:,1]
def log_prior_grad_vector(self, f): return -solve(self.K, f)
def resolver_sistema(self, raices, p): matriz_coeficientes, vector_resultado = self.generar_sistema_ecuaciones( raices, p) c0, c1, c2, c3 = linalg.solve(matriz_coeficientes, vector_resultado) return c0, c1, c2, c3
# Programa de Franco Benassi # Proyecto #3 Interfaces Graficas 2020 # Ejercicio 6 import numpy as np from numpy.linalg import linalg import numpy.polynomial as P import matplotlib.pyplot as pt data = np.load("/home/franco-os/Documentos/Informática/Interfaces de Grafica de Usuario/Proyecto 3/datas/cheby.npy") x = data[0] y = data[1] deg = len(x)-1 A = P.chebyshev.chebvander(x,deg) c = linalg.solve(A,y) Resutl = P.Chebyshev(c) xx = np.linspace(x.min(),x.max(),100) pt.plot(xx,Resutl(xx),'r',label="Interpolación Chebychev") pt.scatter(x,y,label="Puntos de datos") pt.ylabel("F(x)") pt.xlabel("T(s)") pt.legend() pt.show()
def Solve(self): ''' This method builds System Matrix and gets Solution ''' if self.SimulationContext.Id != self.NetworkMesh.Id: raise self.SimulationContext.XMLIdError() try: self.TimeStep = self.SimulationContext.Context['timestep'] self.SquareTimeStep = self.TimeStep*self.TimeStep except KeyError: print "Error, Please set timestep in Simulation Context XML File" raise try: self.Period = self.SimulationContext.Context['period'] self.TimeStepFreq = int(self.Period/self.TimeStep) except KeyError: print "Error, Please set period in Simulation Context XML File" raise try: self.Cycles = self.SimulationContext.Context['cycles'] self.NumberOfIncrements = (self.Cycles*self.TimeStepFreq) except KeyError: print "Error, Please set cycles number in Simulation Context XML File" raise history = [] assembler = Assembler() assembler.SetNetworkMesh(self.NetworkMesh) assembler.SetBoundaryConditions(self.BoundaryConditions) info = {'dofmap':assembler.DofMap,'solution':None,'incrementNumber':self.IncrementNumber,'history':history} self.Evaluator.SetInfo(info) self.PrescribedPressures = assembler.AssembleBoundaryConditions(self.SimulationContext) self.LinearZeroOrderGlobalMatrix, self.LinearFirstOrderGlobalMatrix, self.LinearSecondOrderGlobalMatrix = \ assembler.AssembleInit(self.SimulationContext, self.Evaluator) self.ZeroOrderGlobalMatrix = assembler.ZeroOrderGlobalMatrix self.FirstOrderGlobalMatrix = assembler.FirstOrderGlobalMatrix self.SecondOrderGlobalMatrix = assembler.SecondOrderGlobalMatrix NumberOfGlobalDofs = assembler.GetNumberOfGlobalDofs() # number of dofs self.UnknownPressures = arange(0,NumberOfGlobalDofs).reshape(NumberOfGlobalDofs,1) # unknown pressures self.UnknownPressures = delete(self.UnknownPressures, s_[self.PrescribedPressures[:,0]], axis=0) PressuresMatrix = zeros((NumberOfGlobalDofs, self.NumberOfIncrements)) self.p = zeros((NumberOfGlobalDofs,1)) self.pt = zeros((NumberOfGlobalDofs,1)) self.ptt = zeros((NumberOfGlobalDofs,1)) self.dp = zeros((NumberOfGlobalDofs,1)) self.ddp = zeros((NumberOfGlobalDofs,1)) self.dpt = zeros((NumberOfGlobalDofs,1)) self.ddpt = zeros((NumberOfGlobalDofs,1)) self.fe = zeros((NumberOfGlobalDofs,1)) self.fet = zeros((NumberOfGlobalDofs,1)) self.dfe = zeros((NumberOfGlobalDofs,1)) self.dfet = zeros((NumberOfGlobalDofs,1)) self.fi = zeros((NumberOfGlobalDofs,1)) self.fit = zeros((NumberOfGlobalDofs,1)) self.sumv = zeros((NumberOfGlobalDofs,1)) sumvbk = zeros((NumberOfGlobalDofs,1)) nonLinear = False for el in self.NetworkMesh.Elements: if el.IsNonLinear() == True: nonLinear = True break while self.IncrementNumber<=self.NumberOfIncrements: icc = (self.IncrementNumber%self.TimeStepFreq) if icc == 0: icc = self.TimeStepFreq #for flow in self.BoundaryConditions.elementFlow: for el in self.BoundaryConditions.elementFlow: if self.steady == True: self.Flow = assembler.BoundaryConditions.GetSteadyFlow(el, self.TimeStep,icc*self.TimeStep) else: self.Flow = assembler.BoundaryConditions.GetTimeFlow(el, icc*self.TimeStep) self.fe[assembler.FlowDof[el.Id]]= self.Flow CoeffRelax = 0.9 nltol = self.nltol self.pi = None pI = None sumvbk[:,:] = self.sumv[:,:] counter = 0 while True: #Build the algebric equation system for the increment SystemMatrix = (2.0/self.TimeStep)*self.SecondOrderGlobalMatrix + self.FirstOrderGlobalMatrix + (self.TimeStep/2.0)*self.ZeroOrderGlobalMatrix #system matrix RightVector = self.fe + (2.0/self.TimeStep)*dot(self.SecondOrderGlobalMatrix,(self.pt)) + dot(self.SecondOrderGlobalMatrix,(self.dpt)) - dot(self.ZeroOrderGlobalMatrix,(self.sumv))-(self.TimeStep/2.0)*dot(self.ZeroOrderGlobalMatrix,(self.pt)) # right hand side vector #The reduced (partioned) system of equations is generated. RightVector[:,:] = RightVector[:,:] - dot(SystemMatrix[:,self.PrescribedPressures[:,0]],self.PrescribedPressures[:,1:]) SystemMatrix = SystemMatrix[:,s_[self.UnknownPressures[:,0]]] if SystemMatrix.shape[0]> 0.0: SystemMatrix = SystemMatrix[s_[self.UnknownPressures[:,0]],:] RightVector = RightVector[s_[self.UnknownPressures[:,0]],:] #Unknown nodal point values are solved from this system. # Prescribed nodal values are inserted in the solution vector. Solution = solve(SystemMatrix,RightVector) # solutions, unknown pressures self.p[self.UnknownPressures,0] = Solution[:,:] self.p[self.PrescribedPressures[:,0],0] = self.PrescribedPressures[:,1] #Calculating derivatives. #Calculating internal nodal flow values. self.dp = dot((2.0/self.TimeStep),(self.p-self.pt)) - self.dpt self.ddp = dot((4.0/self.SquareTimeStep),(self.p-self.pt)) - dot((4.0/self.TimeStep),self.dpt) -self.ddpt self.sumv = sumvbk + dot((self.TimeStep/2.0),(self.pt+self.p)) self.fi = dot(self.SecondOrderGlobalMatrix,(self.dp)) + dot(self.FirstOrderGlobalMatrix,(self.p)) + dot(self.ZeroOrderGlobalMatrix,(self.sumv)) if not nonLinear : break if self.pi == None: self.pi = zeros((NumberOfGlobalDofs,1)) self.pi[:,:] = self.pt[:,:] pI = CoeffRelax * self.p + self.pi * (1.0-CoeffRelax) self.p[:,:] = pI[:,:] den = norm(self.pi,Inf) if den < 1e-12: den = 1.0 nlerr = norm(self.p-self.pi,Inf) / den info = {'dofmap':assembler.DofMap,'solution':[self.p, self.pt, self.ptt],'incrementNumber':self.IncrementNumber,'history':history} self.Evaluator.SetInfo(info) assembler.Assemble(self.SimulationContext, self.Evaluator, self.LinearZeroOrderGlobalMatrix, self.LinearFirstOrderGlobalMatrix, self.LinearSecondOrderGlobalMatrix) self.ZeroOrderGlobalMatrix = assembler.ZeroOrderGlobalMatrix self.FirstOrderGlobalMatrix = assembler.FirstOrderGlobalMatrix self.SecondOrderGlobalMatrix = assembler.SecondOrderGlobalMatrix #Dynamic nonlinear relaxing coefficient if counter == 100: print "relaxing..." print nlerr, nltol, CoeffRelax counter = 0 self.pi[:,:] = None self.sumv[:,:] = sumvbk[:,:] CoeffRelax *= 0.6 nltol *= 0.95 if nlerr < nltol: nltol = self.nltol counter = 0 break counter+=1 self.pi[:,:] = self.p[:,:] self.ptt[:,:] = self.pt[:,:] self.pt[:,:] = self.p[:,:] self.dpt[:,:] = self.dp[:,:] self.ddpt[:,:] = self.ddp[:,:] self.fet[:,:] = self.fe[:,:] self.fit[:,:] = self.fi[:,:] PressuresMatrix[:,(self.IncrementNumber-1)] = self.p[:,0] history.insert(0,self.IncrementNumber) history = history[:3] if self.steady == True: self.MinimumIncrementNumber = 0.01* self.NumberOfIncrements if norm(self.fi-self.fe,Inf)<self.convergence and self.IncrementNumber > self.MinimumIncrementNumber: self.IncrementNumber = self.NumberOfIncrements else: pass if self.IncrementNumber==ceil(0.05*self.NumberOfIncrements): print "->5%" if self.IncrementNumber==ceil(0.25*self.NumberOfIncrements): print "->25%" if self.IncrementNumber==ceil(0.5*self.NumberOfIncrements): print "->50%" if self.IncrementNumber==ceil(0.70*self.NumberOfIncrements): print "->70%" if self.IncrementNumber==ceil(0.90*self.NumberOfIncrements): print "->90%" if self.IncrementNumber==ceil(0.99*self.NumberOfIncrements): print "->99%" self.IncrementNumber = self.IncrementNumber+1 self.EndIncrementTime = self.EndIncrementTime + self.TimeStep # increment info = {'dofmap':assembler.DofMap,'solution':[self.p, self.pt, self.ptt],'incrementNumber':self.IncrementNumber,'history':history,'allSolution':PressuresMatrix} self.Evaluator.SetInfo(info) self.Solutions = PressuresMatrix return PressuresMatrix
def incomplete_cholesky(X, kernel, eta, power=1, blocksize=100): """ Computes the incomplete Cholesky factorisation of the kernel matrix defined by samples X and a given kernel. The kernel is evaluated on-the-fly. The optional power parameter is used to multiply the kernel output with itself. Original code from "Kernel Methods for Pattern Analysis" by Shawe-Taylor and Cristianini. Modified to compute kernel on the fly, to use kernels multiplied with themselves (tensor product), and optimised speed via using vector operations and not pre-allocate full kernel matrix memory, but rather allocate memory of low-rank kernel block-wise Changes by Heiko Strathmann parameters: X - list of input vectors to evaluate kernel on kernel - a kernel object with a kernel method that takes 2d-arrays and returns a psd kernel matrix eta - precision cutoff parameter for the low-rank approximation. Lies is (0,1) where smaller means more accurate. power - every kernel evaluation is multiplied with itself this number of times. Zero is supported blocksize - tuning parameter for speed, determines how rows elements are allocated in a block for the (growing) kernel matrix. Larger means faster algorithm (to some extend if low rank dimension is larger than blocksize) output: K_chol, ell, I, R, W, where K - is the kernel using only the pivot index features I - is a list containing the pivots used to compute K_chol R - is a low-rank factor such that R.T.dot(R) approximates the original K W - is a matrix such that W.T.dot(K_chol.dot(W)) approximates the original K """ assert(eta>0 and eta<1) assert(power>=0) assert(blocksize>=0) assert(len(X)>=0) m=len(X) # growing low rank basis R=zeros((blocksize,m)) # diagonal (assumed to be one) d=ones(m) # used indices I=[] nu=[] # algorithm is executed as long as a is bigger than eta precision a=d.max() I.append(d.argmax()) # growing set of evaluated kernel values K=zeros((blocksize,m)) j=0 while a>eta: nu.append(sqrt(a)) if power>=1: K[j,:]=kernel.kernel([X[I[j]]], X)**power else: K[j,:]=ones(m) if j==0: R_dot_j=0 elif j==1: R_dot_j=R[:j,:]*R[:j,I[j]] else: R_dot_j=R[:j,:].T.dot(R[:j,I[j]]) R[j,:]=(K[j,:] - R_dot_j)/nu[j] d=d-R[j,:]**2 a=d.max() I.append(d.argmax()) j=j+1 # allocate more space for kernel if j>=len(K): K=vstack((K, zeros((blocksize,m)))) R=vstack((R, zeros((blocksize,m)))) # remove un-used rows which were located unnecessarily K=K[:j,:] R=R[:j,:] # remove list pivot index since it is not used I=I[:-1] # from low rank to full rank W=solve(R[:,I], R) # low rank K K_chol=K[:,I] return K_chol, I, R, W
def sbgn_solver(Data, Model, Jacobian, Prior, TOL=1.0e-6, MAXIT=10, ALPHA=0.2, BETA=0.5, QUIET=False): """ sbgn_solver - Scalar Bayesian Gauss-Newton (sbgn) solver for a parameter estimation problem to fit a possibly non-linear model to a series of scalar observations poluted by IID Gaussian noise. This function seeks a solution to the maximum posterior probability of the parameters x and inverse noise variance s, given the measured Data, Prior information, and IID Gaussian measurement noise: maximize p( x, s | Data, Model, Prior ). A guarded Gauss-Newton method is used to find a local solution to this problem by successively approximating: Model(x - xo) ~ D*(x-xo) + Model(xo), where D is the Jacobian of the model function, so the approximation represents the local linear behavior of the model. Inputs: ------- Data: array-like vector of measured data Model: a function handle to the model function with the following prototype: g = Model(x), where x is a d-dimensional parameter vector, and g is equal to the model evaluated at x (i.e. g = Model(x)). Jacobian: a function handle to evaluate the Jacobian of the model at x, with the following prototype: D = Jacobian(x), where D is the Jacobian matrix used as the local linear approximation to the model function, which is defined as follows: D = [dg_1/dx_1 dg_1/dx_2 ... dg_1/dx_d; dg_2/dx_1 dg_2/dx_2 ... dg_2/dx_d; ... dg_n/dx_1 dg_n/dx_2 ... dg_n/dx_d]; Prior: Dictionary containing prior statistical information regarding the parameters x and s, including the initial guess, with the fields x_mean: dx1 vector of mean values for the nominal parameters. iSigma_x: dxd prior inverse covariance matrix for the model parameters psig: prior inverse variance exponential distribution parameter, defined such that psig represents worst case anticipated model and/or sensor error variance (before any measurements are made). Note: psig replaces lambda in previous versions. xo: initial guess for starting the Gauss-Newton algorithm Optional named parameters for algorithm options: TOL: exit tolerance ALPHA: backtracking line search parameter BETA: backtracking line search parameter MAXIT: maximum number of iterations to run QUIET: if true, progress output text is suppressed. Outputs: -------- Est: Dictionary containing the optimal estimate and accuracy information in the following fields: x_est: dx1 vector containing the estimate for all parameters s_est: scalar estimate for the inverse variance iSigma_est: inverse marginal parameter estimation covariance iSigma_xs_est: inverse joint covariance of x_est and s_est lnZ: estimated log evidence of the observed data model: nx1 vector containing the model output at x_est fo: 1x1 scalar objective value at (x_est, s_est) status: boolean status indicating convergence Note: iSigma_est, iSigma_xs_est, and lnZ are based on a local quadratic approximation of the objective function at the optimal solution and MAY have poor accuracy. This can/should be checked using MCMC methods, like the one provided by the bgn_ns_solver function (has yet to be implemented in python). Finally, note, this version was ported over from Matlab code and is not yet well tested for use with models that output complex numbers. TODO: Devise a test case for this scenario """ # convert input Data vector to a numpy array (in case it is not already) Data = np.array(Data) # convert to column vector (in-place), which is needed for the computation Data.resize((np.alen(Data), 1)) # calculate number of observations n, which includes both real and # imaginary parts (counted seperately) if np.all(np.isreal(Data)): n = np.alen(Data) else: n = 2. * np.alen(Data) x_mean = Prior['x_mean'] iSigma_x = Prior['iSigma_x'] psig = Prior['psig'] xo = Prior['xo'].copy() go = Model(xo) # Setup and run the Gauss-Newton solver # define the objective function sumsq = lambda x: x.conj().transpose().dot(x) # quadratic sum with symmetric matrix Q (for real x only) qsumsq = lambda x, Q: x.transpose().dot(Q.dot(x)) # evaluate the objective function using current model evaluated at x (m) objfun = lambda x,m,s: np.real( s*sumsq(m-Data) - n*np.log(s) + 2.*psig*s \ + qsumsq(x-x_mean, iSigma_x) ) # analytical measurement precision update # using the current model evaluated at x (m). supdate = lambda m: n / (sumsq(m - Data) + 2.0 * psig) # note: the above lambda functions make use of the current model output at x, # and therefore do not require any model evaluations. # initialize convergence status status = True # note: this starts as true and is set to false if there is a problem. # print progress output headers if not QUIET: hbar = '-' * 70 print '\nBayesian Gauss-Newton Solver 2.1' print hbar print ' Solving a %i-dimensional problem.\n' % np.alen(x_mean) # print algorithm progress feedback headers headers = ('Norm(dx)', 'Objective', 'Step Size', 'Norm(gradient)') print '%11s%17s%14s%18s' % headers print hbar # initialize the no improvement counter no_imp_cnt = 0 # solve for an optimal change in x for k in range(MAXIT): # On entry, xo and go are initialized above, # On repeat, xo and go are updated below. # update the Jacobian matrix at the current xo D = Jacobian(xo) b = Data - go c = x_mean - xo # compute the noise update first so = supdate(go) S = (1 / so) * iSigma_x # compute the current value of the objective function objfun_o = objfun(xo, go, so) # solve for the optimal update dx = linalg.solve( np.real(sumsq(D)) + S, np.real(D.conj().transpose().dot(b)) + S.dot(c)) # compute the objective function gradient g = -2.0 * so * np.real( D.conj().transpose().dot(b)) - 2. * iSigma_x.dot(c) # note the minus sign because of definition of b and c above # line-search guard to ensure descent t = 1.0 while True: xt = xo + t * dx gt = Model(xt) #[0]; objfun_t = objfun(xt, gt, so) if objfun_t > objfun_o + ALPHA * t * g.transpose().dot(dx): t = BETA * t else: break # if the objective is not improved after 3 tries, exit if objfun_t >= objfun_o and t < 1.0: no_imp_cnt += 1 if not QUIET: print 'No improvement made to objective. Strike {}.'.\ format(no_imp_cnt) if no_imp_cnt == 3: print 'No improvement made to objective. Exiting.' status = False break else: # reset the counter no_imp_cnt = 0 # update current guess and model output. xo = xt go = gt # print progress info if not QUIET: print '%11.3f%17.7f%14.2f%18.3f' % (linalg.norm(dx), objfun_t, t, linalg.norm(g)) # exit conditions if (linalg.norm(dx) <= TOL): if not QUIET: print "\nNorm(dx) less than TOL. Done." break # check norm of gradient elif (linalg.norm(g) <= TOL): if not QUIET: print "\nGradient less than TOL. Done." break # note: if the norm of the gradient is small, than the uncertainty # analysis computed below should be representative, and even though # the parameter update step may be non-zero, the estimate may have # been found accurately enough relative to the uncertainty. else: status = False print '\nBayesian Gauss-Newton did NOT converge after max iterations.\n' if not QUIET: print hbar # get the objective function value on exit fo = objfun(xo, go, so) # diagnostics if not QUIET: print 'Objective on exit = %0.6f' % fo # compute the final Jacobian at xo D = Jacobian(xo) # diagnostics: compute the gradient at the solution b = Data - go c = x_mean - xo g = -2.0 * so * np.real( D.conj().transpose().dot(b)) - 2.0 * iSigma_x.dot(c) if not QUIET: print 'Norm of gradient on exit = %f\n' % linalg.norm(g) # # Compute the estimation accuarcy (covariance) # # compute the parameter estimation error iSigma_est = so*np.real(sumsq(D)) + iSigma_x - \ (2.0*so**2/n)*np.real( sumsq(b.conj().transpose().dot(D)) ) Dtb = np.real(D.conj().transpose().dot(b)) iSigma_xs_est = np.vstack((np.hstack( (so * np.real(sumsq(D)) + iSigma_x, -Dtb)), np.hstack((-Dtb.transpose(), n / (2 * so**2))))) #iSigma_est = so*real(D'*D) + iSigma_x - (2*so^2/n)*real( (D'*b)*(b'*D) ); #iSigma_xs_est = [so*real(D'*D) + iSigma_x, -real(D'*b); ... # -real(b'*D), n/(2*so^2)]; # # Compute the evidence of the observed data under the BGN Model # d = xo.shape[0] lnK = (n/2.)*np.log(so/(2.*np.pi)) - (so/2.)*sumsq(b) \ - (d/2.)*np.log(2.*np.pi) \ + (1./2.)*np.log(linalg.det(iSigma_x)) - (1./2.)*qsumsq(c,iSigma_x) \ + np.log(psig) - psig*so lnZ = lnK + ((d+1.)/2.)*np.log(2.*np.pi) \ - (1./2.)*np.log(linalg.det(iSigma_xs_est)) # # Define outputs # Est = {} Est['x_est'] = xo Est['s_est'] = so[0, 0] Est['iSigma_est'] = iSigma_est Est['iSigma_xs_est'] = iSigma_xs_est Est['lnZ'] = lnZ[0, 0] Est['model'] = go Est['fo'] = fo[0, 0] Est['status'] = status # note: so, lnZ, and fo by themselves 1x1 numpy arrays, which are converted # to scalars simply by accessing their first (and only) element. return Est
import numpy as np from numpy.polynomial import Polynomial as Polynomial from numpy.linalg import linalg as linalg from scipy.optimize import linprog import matplotlib.pyplot as plt #Найти все корни уравнения p = Polynomial([2, -1, -2, 1]) print(p.roots()) #Найти решение системы уравнений a = np.array([[2,3], [2, 1,]]) b = np.array([11, 11]) x = linalg.solve(a, b) print(x) #Найти решение системы уравнений и ограничения c = [0, 0, 0] A = [[1, 2, -1], [-1, 1, 1], [1, 1, -2], [-2, 3, -1]] b = [2, 1, 0, -1] res = linprog(c, A_ub=A, b_ub=b, options={"disp": True}) print(res) #Нарисовать график функции # the function y = x**5 - 2 * x + 1 # settings x = np.linspace(-10,10,100)
def linesearch(recon, data, max_iter, fidelity, reg, Radon, view=None, guess=None, tol=1e-4, min_iter=1, **kwargs): c, E, F, nAtoms, max_iter, plot = _startup( recon, data, max_iter, Radon, view, guess, 1, **kwargs) eps = 1e-4 # Stepsizes h = [1] * nAtoms if guess is None: R = Radon(recon) F[0] = fidelity(R, data) E[0] = F[0] + reg(recon) n = nAtoms else: n = 1 dim, iso = recon.space.dim, recon.space.isotropic random.seed(1) jj = 1 while n <= nAtoms + 1: if n == nAtoms + 1: if guess is None or len(max_iter) == 2: break else: n = nAtoms max_iter = (max_iter[2], max_iter[1]) elif guess is not None: recon[n - 1] = guess(data - Radon(recon[:n]), recon[n - 1]) # recon.I[n - 1] = 0 R = Radon(recon[:n]) F[jj - 1] = fidelity(R, data) E[jj - 1] = F[jj - 1] + reg(recon[:n]) for _ in range(max_iter[0]): BREAK = [True, True] tmp = random.permutation(arange(n)) for j in tmp: for __ in range(max_iter[1]): BREAK[1] = True ___, df, ddf = [thing[0] for thing in Radon.L2_derivs(recon[j], (data + Radon(recon[j]) - R))] # f += reg(recon[j]) df += reg.grad(recon[j]) try: ddf += reg.hess(recon[j])[0] except TypeError: ddf += reg.hess(recon[j]) H = 1 / h[j] # H = max(0, H - eigvalsh(ddf).min()) for i in range(ddf.shape[0]): ddf[i, i] += H try: dx = solve(ddf, -df) except Exception as e: raise e R = _step(recon, Radon, R, dx, data, E, F, dim, iso, j, jj, n, fidelity, reg) if E[jj] > E[jj - 1]: E[jj] = E[jj - 1] if h[j] > 2 * eps: BREAK[0] = False BREAK[1] = False h[j] /= 10 else: if norm(dx) > tol: BREAK[0] = False BREAK[1] = False h[j] *= 2 h[j] = min(max(h[j], eps), 1 / eps) jj += 1 if BREAK[1]: break if BREAK[0] and _ > min_iter: break # print('Volume: ', recon.I / # recon.r[:, 0] / recon.r[:, 1] / recon.r[:, 2]) # print('Pos: ', recon.x) # print('Rad: ', recon.r) # print('Amplitude: ', recon.I) plot(recon, R, E, F, n, _, jj) n += 1 plot(recon, R, E, F, n, _, jj, True) return recon, E[:jj], F[:jj]
def linesearch_block(recon, data, max_iter, fidelity, reg, Radon, view=None, guess=None, tol=1e-4, min_iter=1, **kwargs): c, E, F, nAtoms, max_iter, plot = _startup( recon, data, max_iter, Radon, view, guess, 3, **kwargs) eps = 1e-4 # Stepsizes h = [[1] * nAtoms for _ in range(3)] iso = recon.space.isotropic if guess is None: R = Radon(recon) F[0] = fidelity(R, data) E[0] = F[0] + reg(recon) n = nAtoms else: n = 1 dim = 'Ixr' random.seed(1) jj = 1 while n <= nAtoms + 1: if n == nAtoms + 1: if guess is None or len(max_iter) == 2: break else: n = nAtoms max_iter = (max_iter[2], max_iter[1]) elif guess is not None: recon[n - 1] = guess(data - Radon(recon[:n]), recon[n - 1]) R = Radon(recon[:n]) F[jj - 1] = fidelity(R, data) E[jj - 1] = F[jj - 1] + reg(recon[:n]) for _ in range(max_iter[0]): BREAK = [True, True] tmp = random.permutation(arange(n)) for j in tmp: for t in range(len(dim)): for __ in range(max_iter[1]): BREAK[1] = True T = c.asarray(getattr(recon, dim[t])[j]) old = T.copy() ___, df, ddf = [thing[0] for thing in Radon.L2_derivs(recon[j], (data + Radon(recon[j]) - R))] df += reg.grad(recon[j]) ddf += reg.hess(recon[j]) H = 1 / h[t][j] for i in range(ddf.shape[0]): ddf[i, i] += H if t == 0: if ddf[0, 0] > 1e-8: dx = -df[0] / ddf[0, 0] else: break T += dx c.set(recon.I[j:j + 1], max(0, T)) elif t == 1: try: dx = solve( ddf[1:dim + 1, 1:dim + 1], -df[1:dim + 1]) except Exception: break T += dx c.set(recon.x[j], maximum(-.99, minimum(.99, T))) else: t = 2 try: dx = solve( ddf[dim + 1:, dim + 1:], -df[dim + 1:]) except Exception: break if iso: T = abs(T + dx) else: T += dx if T[0] < 0: T[0], T[3], T[5] = -\ T[0], -T[3], -T[5] if T[1] < 0: T[1], T[4] = -T[1], -T[4] if T[2] < 0: T[2] = -T[2] c.set(recon.r[j], T) R = Radon(recon[:n]) F[jj] = fidelity(R, data) E[jj] = F[jj] + reg(recon[:n]) if E[jj] > E[jj - 1]: c.set(getattr(recon, dim[t])[j:j + 1], old) R = Radon(recon[:n]) E[jj] = E[jj - 1] if h[t][j] > 2 * eps: BREAK[0] = False BREAK[1] = False h[t][j] /= 10 else: h[t][j] *= 1.3 if norm(old - T) > tol * norm(old): BREAK[0] = False BREAK[1] = False h[t][j] = min(max(h[t][j], eps), 1 / eps) jj += 1 if BREAK[1]: break # print('Pos: ', recon.x) # print('Rad: ', recon.r) # print('Amplitude: ', recon.I) if BREAK[0] and _ > min_iter: break # print('Volume: ', recon.I / # recon.r[:, 0] / recon.r[:, 1] / recon.r[:, 2]) # print('Pos: ', recon.x) # print('Rad: ', recon.r) # print('Amplitude: ', recon.I) plot(recon, R, E, F, n, _, jj) n += 1 plot(recon, R, E, F, n, _, jj, True) return recon, E[:jj], F[:jj]
def lsbgn_solver(Data, D, Prior, pEst=None, TOL=1.0e-6, MAXIT=300, MAXM=None, QUIET=False): """ lsbgn_solver - linear scalar Bayesian Gauss-Newton (lsbgn) solver for a parameter estimation problem to fit a LINEAR model to a series of scalar observations poluted by IID Gaussian noise. This function seeks a solution to the maximum posterior probability of the parameters x and inverse noise variance s, given the measured Data, Prior information, and IID Gaussian measurement noise: maximize p( x, s | Data, Model, Prior ). For the linear function the model is: Model(x) = D*x (and the Jacobbian of the model w.r.t. x is D), where x is a d-dimensional parameter vector. Inputs: ------- Data: array-like vector of measured data D: matrix defining the linear model function, as noted above. Prior: Dictionary containing prior statistical information regarding the parameters x and s, including the initial guess, with the fields x_mean: px1 vector of mean values for the nominal parameters. iSigma_x: pxp prior inverse covariance matrix for the model parameters psig: prior inverse variance exponential distribution parameter, defined such that psig represents worst case anticipated model and/or sensor error variance (before any measurements are made). xo: initial guess for starting the Gauss-Newton algorithm Optional named parameters for algorithm options: pEst: prior estimation structure used to accumulate data TOL: exit tolerance MAXIT: maximum number of iterations to run QUIET: if true, progress output text is suppressed. Outputs: -------- Est: Dictionary containing the optimal estimate and accuracy information in the following fields: x_est: dx1 vector containing the estimate for all parameters s_est: scalar estimate for the inverse variance iSigma_est: inverse marginal parameter estimation covariance iSigma_xs_est: inverse joint covariance of x_est and s_est lnZ: estimated log evidence of the observed data model: nx1 vector containing the model output at x_est fo: 1x1 scalar objective value at (x_est, s_est) status: boolean status indicating convergence Data: Data structure from previous run, used internally to update with new data. Finally, note, this version was ported over from Matlab code and is not yet well tested for use with models that output complex numbers. TODO: Devise a test case for this scenario """ # convert input Data vector to a numpy array (in case it is not already) Data = np.array(Data) # convert to column vector (in-place), which is needed for the computation Data.resize((np.alen(Data), 1)) # prefix previous data if available if pEst: Data = np.vstack((pEst['Data'][0], Data)) D = np.vstack((pEst['Data'][1], D)) # truncate if number of elements exceeds max permitted (memory limit) if MAXM and (np.alen(Data) > MAXM): Data = Data[-MAXM:, :] D = D[-MAXM:, :] # calculate number of observations n, which includes both real and # imaginary parts (counted seperately) if np.all(np.isreal(Data)): n = np.alen(Data) else: n = 2. * np.alen(Data) x_mean = Prior['x_mean'] iSigma_x = Prior['iSigma_x'] psig = Prior['psig'] # set initial parameter estimate if a previous estimate is available if pEst: xo = pEst['x_est'] elif 'xo' in Prior.keys(): xo = Prior['xo'] else: xo = x_mean # initialize the model output go = D.dot(xo) # # Setup and run the Gauss-Newton solver # # define the objective function sumsq = lambda x: x.conj().transpose().dot(x) # quadratic sum with symmetric matrix Q (for real x only) qsumsq = lambda x, Q: x.transpose().dot(Q.dot(x)) objfun = lambda x,m,s: np.real( s*sumsq(m-Data) - n*np.log(s) + 2.*psig*s \ + qsumsq(x-x_mean, iSigma_x) ) supdate = lambda m: n / (sumsq(m - Data) + 2.0 * psig) # note: the above lambda functions make use of the current model output at x, # and therefore do not require any model evaluations. # initialize convergence status status = True # note: this starts as true and is set to false if there is a problem. # print progress output headers if not QUIET: hbar = '-' * 70 print '\nLinear Bayesian Gauss-Newton Solver 2.0' print hbar print ' Solving a %i-dimensional problem.\n' % np.alen(x_mean) # print algorithm progress feedback headers headers = ('Norm(dx)', 'Objective', 'Step Size', 'Norm(gradient)') print '%11s%17s%14s%18s' % headers print hbar # solve for an optimal change in x for k in range(MAXIT): # On entry, xo and go are initialized above, # On repeat, xo and go are updated below. #go = D.dot(xo); #, D = Model(xo); b = Data - go c = x_mean - xo # compute the noise update first so = supdate(go) S = (1 / so) * iSigma_x # compute the current value of the ojective function # (without reevaluating the Model function) #objfun_o = so*sumsq(b) - n*np.log(so) + 2.0*psig*so \ # + qsumsq(c,iSigma_x); #c'*(iSigma_x*c); # solve for the optimal update dx = linalg.solve( np.real(sumsq(D)) + S, np.real(D.conj().transpose().dot(b)) + S.dot(c)) # compute the objective function gradient g = -2.0 * so * np.real( D.conj().transpose().dot(b)) - 2. * iSigma_x.dot(c) # note the minus sign because of definition of b and c above # line-search guard to ensure descent t = 1.0 #while objfun(xo + t*dx,so) > objfun_o + ALPHA*t*g.transpose().dot(dx): # t = BETA*t; # if the objective is not improved then break xt = xo + t * dx gt = D.dot(xt) objfun_t = objfun(xt, gt, so) #if objfun_t > objfun_o: # if not QUIET: # print 'No improvement made to objective. Exiting.\n'; # break; # update current guess xo = xt go = gt # print progress info if not QUIET: print '%11.3f%17.7f%14.2f%18.3f' % (linalg.norm(dx), objfun_t, t, linalg.norm(g)) if linalg.norm(dx) <= TOL: break else: status = False print '\nBayesian Gauss-Newton did NOT converge after max iterations.\n' if not QUIET: print hbar # get the objective function value on exit fo = objfun(xo, go, so) # diagnostics if not QUIET: print 'Objective on exit = %0.6f' % fo # diagnostics: compute the gradient at the solution b = Data - go c = x_mean - xo g = -2.0 * so * np.real( D.conj().transpose().dot(b)) - 2.0 * iSigma_x.dot(c) if not QUIET: print 'Norm of gradient on exit = %f\n' % linalg.norm(g) # # Compute the estimation accuarcy (covariance) # # compute the parameter estimation error iSigma_est = so*np.real(sumsq(D)) + iSigma_x - \ (2.0*so**2/n)*np.real( sumsq(b.conj().transpose().dot(D)) ) Dtb = np.real(D.conj().transpose().dot(b)) iSigma_xs_est = np.vstack((np.hstack( (so * np.real(sumsq(D)) + iSigma_x, -Dtb)), np.hstack((-Dtb.transpose(), n / (2 * so**2))))) # # Compute the evidence of the observed data under the BGN Model # d = xo.shape[0] lnK = (n/2.)*np.log(so/(2.*np.pi)) - (so/2.)*sumsq(b) \ - (d/2.)*np.log(2.*np.pi) \ + (1./2.)*np.log(linalg.det(iSigma_x)) - (1./2.)*qsumsq(c,iSigma_x) \ + np.log(psig) - psig*so lnZ = lnK + ((d+1.)/2.)*np.log(2.*np.pi) \ - (1./2.)*np.log(linalg.det(iSigma_xs_est)) # # Define outputs # Est = {} Est['x_est'] = xo Est['s_est'] = so[0, 0] Est['iSigma_est'] = iSigma_est Est['iSigma_xs_est'] = iSigma_xs_est Est['lnZ'] = lnZ[0, 0] Est['model'] = go Est['fo'] = fo[0, 0] Est['status'] = status Est['Data'] = [Data, D] # note: so, lnZ, and fo by themselves 1x1 numpy arrays, which are converted # to scalars simply by accessing their first (and only) element. return Est
def Solve(self): ''' This method builds System Matrix and gets Solution ''' if self.SimulationContext.Id != self.NetworkMesh.Id: raise self.SimulationContext.XMLIdError() try: self.TimeStep = self.SimulationContext.Context['timestep'] self.SquareTimeStep = self.TimeStep * self.TimeStep except KeyError: print "Error, Please set timestep in Simulation Context XML File" raise try: self.Period = self.SimulationContext.Context['period'] self.TimeStepFreq = int(self.Period / self.TimeStep) except KeyError: print "Error, Please set period in Simulation Context XML File" raise try: self.Cycles = self.SimulationContext.Context['cycles'] self.NumberOfIncrements = (self.Cycles * self.TimeStepFreq) except KeyError: print "Error, Please set cycles number in Simulation Context XML File" raise history = [] assembler = Assembler() assembler.SetNetworkMesh(self.NetworkMesh) assembler.SetBoundaryConditions(self.BoundaryConditions) info = { 'dofmap': assembler.DofMap, 'solution': None, 'incrementNumber': self.IncrementNumber, 'history': history } self.Evaluator.SetInfo(info) self.PrescribedPressures = assembler.AssembleBoundaryConditions( self.SimulationContext) self.LinearZeroOrderGlobalMatrix, self.LinearFirstOrderGlobalMatrix, self.LinearSecondOrderGlobalMatrix = \ assembler.AssembleInit(self.SimulationContext, self.Evaluator) self.ZeroOrderGlobalMatrix = assembler.ZeroOrderGlobalMatrix self.FirstOrderGlobalMatrix = assembler.FirstOrderGlobalMatrix self.SecondOrderGlobalMatrix = assembler.SecondOrderGlobalMatrix NumberOfGlobalDofs = assembler.GetNumberOfGlobalDofs( ) # number of dofs self.UnknownPressures = arange(0, NumberOfGlobalDofs).reshape( NumberOfGlobalDofs, 1) # unknown pressures self.UnknownPressures = delete(self.UnknownPressures, s_[self.PrescribedPressures[:, 0]], axis=0) PressuresMatrix = zeros((NumberOfGlobalDofs, self.NumberOfIncrements)) self.p = zeros((NumberOfGlobalDofs, 1)) self.pt = zeros((NumberOfGlobalDofs, 1)) self.ptt = zeros((NumberOfGlobalDofs, 1)) self.dp = zeros((NumberOfGlobalDofs, 1)) self.ddp = zeros((NumberOfGlobalDofs, 1)) self.dpt = zeros((NumberOfGlobalDofs, 1)) self.ddpt = zeros((NumberOfGlobalDofs, 1)) self.fe = zeros((NumberOfGlobalDofs, 1)) self.fet = zeros((NumberOfGlobalDofs, 1)) self.dfe = zeros((NumberOfGlobalDofs, 1)) self.dfet = zeros((NumberOfGlobalDofs, 1)) self.fi = zeros((NumberOfGlobalDofs, 1)) self.fit = zeros((NumberOfGlobalDofs, 1)) self.sumv = zeros((NumberOfGlobalDofs, 1)) sumvbk = zeros((NumberOfGlobalDofs, 1)) nonLinear = False for el in self.NetworkMesh.Elements: if el.IsNonLinear() == True: nonLinear = True break while self.IncrementNumber <= self.NumberOfIncrements: icc = (self.IncrementNumber % self.TimeStepFreq) if icc == 0: icc = self.TimeStepFreq #for flow in self.BoundaryConditions.elementFlow: for el in self.BoundaryConditions.elementFlow: if self.steady == True: self.Flow = assembler.BoundaryConditions.GetSteadyFlow( el, self.TimeStep, icc * self.TimeStep) else: self.Flow = assembler.BoundaryConditions.GetTimeFlow( el, icc * self.TimeStep) self.fe[assembler.FlowDof[el.Id]] = self.Flow CoeffRelax = 0.9 nltol = self.nltol self.pi = None pI = None sumvbk[:, :] = self.sumv[:, :] counter = 0 while True: #Build the algebric equation system for the increment SystemMatrix = ( 2.0 / self.TimeStep ) * self.SecondOrderGlobalMatrix + self.FirstOrderGlobalMatrix + ( self.TimeStep / 2.0) * self.ZeroOrderGlobalMatrix #system matrix RightVector = self.fe + (2.0 / self.TimeStep) * dot( self.SecondOrderGlobalMatrix, (self.pt)) + dot( self.SecondOrderGlobalMatrix, (self.dpt)) - dot( self.ZeroOrderGlobalMatrix, (self.sumv)) - (self.TimeStep / 2.0) * dot( self.ZeroOrderGlobalMatrix, (self.pt)) # right hand side vector #The reduced (partioned) system of equations is generated. RightVector[:, :] = RightVector[:, :] - dot( SystemMatrix[:, self.PrescribedPressures[:, 0]], self.PrescribedPressures[:, 1:]) SystemMatrix = SystemMatrix[:, s_[self.UnknownPressures[:, 0]]] if SystemMatrix.shape[0] > 0.0: SystemMatrix = SystemMatrix[ s_[self.UnknownPressures[:, 0]], :] RightVector = RightVector[s_[self.UnknownPressures[:, 0]], :] #Unknown nodal point values are solved from this system. # Prescribed nodal values are inserted in the solution vector. Solution = solve(SystemMatrix, RightVector) # solutions, unknown pressures self.p[self.UnknownPressures, 0] = Solution[:, :] self.p[self.PrescribedPressures[:, 0], 0] = self.PrescribedPressures[:, 1] #Calculating derivatives. #Calculating internal nodal flow values. self.dp = dot((2.0 / self.TimeStep), (self.p - self.pt)) - self.dpt self.ddp = dot((4.0 / self.SquareTimeStep), (self.p - self.pt)) - dot( (4.0 / self.TimeStep), self.dpt) - self.ddpt self.sumv = sumvbk + dot((self.TimeStep / 2.0), (self.pt + self.p)) self.fi = dot(self.SecondOrderGlobalMatrix, (self.dp)) + dot( self.FirstOrderGlobalMatrix, (self.p)) + dot(self.ZeroOrderGlobalMatrix, (self.sumv)) if not nonLinear: break if self.pi == None: self.pi = zeros((NumberOfGlobalDofs, 1)) self.pi[:, :] = self.pt[:, :] pI = CoeffRelax * self.p + self.pi * (1.0 - CoeffRelax) self.p[:, :] = pI[:, :] den = norm(self.pi, Inf) if den < 1e-12: den = 1.0 nlerr = norm(self.p - self.pi, Inf) / den info = { 'dofmap': assembler.DofMap, 'solution': [self.p, self.pt, self.ptt], 'incrementNumber': self.IncrementNumber, 'history': history } self.Evaluator.SetInfo(info) assembler.Assemble(self.SimulationContext, self.Evaluator, self.LinearZeroOrderGlobalMatrix, self.LinearFirstOrderGlobalMatrix, self.LinearSecondOrderGlobalMatrix) self.ZeroOrderGlobalMatrix = assembler.ZeroOrderGlobalMatrix self.FirstOrderGlobalMatrix = assembler.FirstOrderGlobalMatrix self.SecondOrderGlobalMatrix = assembler.SecondOrderGlobalMatrix #Dynamic nonlinear relaxing coefficient if counter == 100: print "relaxing..." print nlerr, nltol, CoeffRelax counter = 0 self.pi[:, :] = None self.sumv[:, :] = sumvbk[:, :] CoeffRelax *= 0.6 nltol *= 0.95 if nlerr < nltol: nltol = self.nltol counter = 0 break counter += 1 self.pi[:, :] = self.p[:, :] self.ptt[:, :] = self.pt[:, :] self.pt[:, :] = self.p[:, :] self.dpt[:, :] = self.dp[:, :] self.ddpt[:, :] = self.ddp[:, :] self.fet[:, :] = self.fe[:, :] self.fit[:, :] = self.fi[:, :] PressuresMatrix[:, (self.IncrementNumber - 1)] = self.p[:, 0] history.insert(0, self.IncrementNumber) history = history[:3] if self.steady == True: self.MinimumIncrementNumber = 0.01 * self.NumberOfIncrements if norm( self.fi - self.fe, Inf ) < self.convergence and self.IncrementNumber > self.MinimumIncrementNumber: self.IncrementNumber = self.NumberOfIncrements else: pass if self.IncrementNumber == ceil(0.05 * self.NumberOfIncrements): print "->5%" if self.IncrementNumber == ceil(0.25 * self.NumberOfIncrements): print "->25%" if self.IncrementNumber == ceil(0.5 * self.NumberOfIncrements): print "->50%" if self.IncrementNumber == ceil(0.70 * self.NumberOfIncrements): print "->70%" if self.IncrementNumber == ceil(0.90 * self.NumberOfIncrements): print "->90%" if self.IncrementNumber == ceil(0.99 * self.NumberOfIncrements): print "->99%" self.IncrementNumber = self.IncrementNumber + 1 self.EndIncrementTime = self.EndIncrementTime + self.TimeStep # increment info = { 'dofmap': assembler.DofMap, 'solution': [self.p, self.pt, self.ptt], 'incrementNumber': self.IncrementNumber, 'history': history, 'allSolution': PressuresMatrix } self.Evaluator.SetInfo(info) self.Solutions = PressuresMatrix return PressuresMatrix