def Hmat(d,i,j,obs_str,Mlist): #Creates H^hat matrix for given obs, i,j and data d Hlist=[-9999]*(j-i) Obslist=re.findall(r'[^,;\s]+', obs_str) Hhold=[-9999]*len(Obslist) obsdict={'nee': Mod.NEE, 'lf': Mod.LF, 'lw': Mod.LW, 'cf': Mod.Cf, \ 'cr': Mod.Cr, 'cw': Mod.Cw, 'cl': Mod.Cl, 'cs': Mod.Cs} for x in xrange(i,j): Cf=ad.adnumber(d.Cf) #Clist[x-i][0]) #Redo this!!! Cr=ad.adnumber(d.Cr) #Clist[x-i][1]) Cw=ad.adnumber(d.Cw) #Clist[x-i][2]) Cl=ad.adnumber(d.Cl) #Clist[x-i][3]) Cs=ad.adnumber(d.Cs) #Clist[x-i][4]) for y in range(0,len(Obslist)): Hhold[y]=ad.jacobian(obsdict[Obslist[y]](Cf,Cr,Cw,Cl,Cs,x,d),[Cf,Cr,Cw,Cl,Cs]) Hlist[x-i]=np.vstack(Hhold) stacklist=[Hlist[0]]+[-9999]*(j-i-1) #Creates H hat matrix for x in xrange(1,j-i): stacklist[x]=Hlist[x]*Mfac(Mlist,x-1) Hmat=np.vstack(stacklist) return np.matrix(Hmat)
def partials(self, state_vec, stn_pos): """Computes the partial matrix with respect to the estimated state vector returns: list(list): jacobian matrix of the measurement with respect to the given state vector """ return jacobian(self.calc_msr(state_vec, stn_pos), state_vec)
def LinDALEC(Cf, Cr, Cw, Cl, Cs, x, d): #Tangent Linear DALEC evergreen model Cf = ad.adnumber(Cf) Cr = ad.adnumber(Cr) Cw = ad.adnumber(Cw) Cl = ad.adnumber(Cl) Cs = ad.adnumber(Cs) Dalecoutput = DALEC(Cf, Cr, Cw, Cl, Cs, x, d) M = np.matrix(ad.jacobian(Dalecoutput, [Cf, Cr, Cw, Cl, Cs])) return Dalecoutput, M
def DoJac(f, X): """Takes a function and a numpy array. Returns (J,Y) where Y = f(X) and J is the Jacobian.""" Xad = adnumber(X) Yad = f(Xad) J = np.array(jacobian(Yad.flatten(), Xad.flatten())) return J, Yad.astype(float)
def linob(ob, pvals, dC, x): """Function returning jacobian of observation with respect to the parameter list. Takes an obs string, a parameters list, a dataClass and a time step x. """ modobdict = {'gpp': gpp, 'nee': nee, 'rt': rec, 'cf': cf, 'clab': clab, 'cr': cr, 'cw': cw, 'cl': cl, 'cs': cs, 'lf': lf, 'lw': lw, 'lai':lai} dpvals = ad.adnumber(pvals) output = modobdict[ob](dpvals, dC, x) return np.array(ad.jacobian(output, dpvals))
def lin_dalecv2(pvals, dC, x): """Linear DALEC model passes a list or array of parameters to the function dalecv2 and returns a linearized model M for timestep xi. Takes, a list of parameters (pvals), a dataClass (dC) and a time step (x). """ p = ad.adnumber(pvals) dalecoutput = dalecv2(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15], p[16], p[17], p[18], p[19], p[20], p[21], p[22], dC, x) lin_model = ad.jacobian(dalecoutput, p) modval = np.array([float(x) for x in dalecoutput]) return modval, lin_model
def linmod_list2(pvals, dC, start, fin): """Creates an array of linearized models (Mi's) taking a list of initial param values, a dataClass (dC) and a start and finish time. """ mod_list = np.concatenate((np.array([pvals]),\ np.ones((fin - start, len(pvals)))*-9999.)) matlist = np.ones((fin - start,23,23))*-9999. dv2=dalecv2_input for x in xrange(start, fin): p=ad.adnumber(mod_list[x-start]) mod_list[(x+1)-start] = dv2(p, dC, x) matlist[x-start] = ad.jacobian(mod_list[(x+1)-start], p) mod_list[(x+1)-start] = np.array([float(y) for y in\ mod_list[(x+1)-start]]) return mod_list, matlist
def _derivatives(self, state): """ Computes the jacobian and state derivatives Args: state (np.ndarray): state vector to find derivatives of Returns: (np.ndarray [1 x n], np.ndarray [n x n]) """ ad_state = ad.adnumber(state) state_deriv = self.force_model(0, ad_state) a_matrix = jacobian(state_deriv, ad_state) # remove ad number from all state values before returning state_deriv = [state.real for state in state_deriv] return state_deriv, a_matrix
def addDForce(self, df, dx, kFactor, bFactor): #print("===============================") #print(" F", self.res) #print(" pos", self.p) #print(" Python::addDForce df(in): ", df) #print(" Python::addDForce dx: ", np.ndarray.flatten(dx)) #print(" Python::addDForce kFactor: ", (kFactor, bFactor)) #print("RES is ", self.res[0,0].d()) #print("RES is ", self.res[0,1].d()) #print("RES is ", self.res[0,2].d()) return from ad import jacobian j = jacobian(self.res, self.p) #print(" Python::addDForce J:", j) tdf = j @ (np.ndarray.flatten(dx) * kFactor) #print(" Python::addDForce df: ", tdf) df += tdf.reshape((-1, 3)) print(" Python::addDForce df: ", df)
def H(d, i, j, obs_str): Hlist = [-9999]*(j-i) Obslist = re.findall(r'[^,;\s]+', obs_str) Hhold = [-9999]*len(Obslist) obsdict = {'nee': Mod.NEE, 'lf': Mod.LF, 'lw': Mod.LW, 'cf': Mod.Cf, \ 'cr': Mod.Cr, 'cw': Mod.Cw, 'cl': Mod.Cl, 'cs': Mod.Cs} for x in xrange(i,j): Cf = ad.adnumber(d.Cf) #Clist[x-i][0]) #Redo this!!! Cr = ad.adnumber(d.Cr) #Clist[x-i][1]) Cw = ad.adnumber(d.Cw) #Clist[x-i][2]) Cl = ad.adnumber(d.Cl) #Clist[x-i][3]) Cs = ad.adnumber(d.Cs) #Clist[x-i][4]) for y in range(0,len(Obslist)): Hhold[y] = ad.jacobian(obsdict[Obslist[y]](Cf,Cr,Cw,Cl,Cs,x,d),[Cf,Cr,Cw,Cl,Cs]) Hlist[x-i] = np.vstack(Hhold) return np.vstack(Hlist)
def rpc_affine_approximation(rpc, p): """ Compute the first order Taylor approximation of an RPC projection function. Args: rpc (rpc_model.RPCModel): RPC camera model p (3-tuple of float): lon, lat, z coordinates Return: array of shape (3, 4) representing the affine camera matrix equal to the first order Taylor approximation of the RPC projection function at point p. """ p = ad.adnumber(p) q = rpc.projection(*p) J = ad.jacobian(q, p) A = np.zeros((3, 4)) A[:2, :3] = J A[:2, 3] = np.array(q) - np.dot(J, p) A[2, 3] = 1 return A
def residual(self, state): ad_state = ad.adnumber(state) est_msr = self.gen_meas(ad_state) resid = self.value - est_msr.real H_tilde = ad.jacobian(est_msr, ad_state) return resid, H_tilde