def balance_ss_model(F,L,Qc,H,Pinf,P0,dF=None,dQc=None,dPinf=None,dP0=None):
bF,T,T_inv = balance_matrix(F)<EOL>bL = np.dot( T_inv, L)<EOL>bQc = Qc <EOL>bH = np.dot(H, T)<EOL>bPinf = np.dot(T_inv, np.dot(Pinf, T_inv.T))<EOL><INDENT>LL,islower = linalg.cho_factor(Pinf)<EOL>inds = np.triu_indices(Pinf.shape[<NUM_LIT:0>],k=<NUM_LIT:1>)<EOL>LL[inds] = <NUM_LIT:0.0><EOL>bLL = np.dot(T_inv, LL)<EOL>bPinf = np.dot( bLL, bLL.T)<EOL><DEDENT>bP0 = np.dot(T_inv, np.dot(P0, T_inv.T))<EOL>if dF is not None:<EOL><INDENT>bdF = np.zeros(dF.shape)<EOL>for i in range(dF.shape[<NUM_LIT:2>]):<EOL><INDENT>bdF[:,:,i] = np.dot( T_inv, np.dot( dF[:,:,i], T))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>bdF = None<EOL><DEDENT>if dPinf is not None:<EOL><INDENT>bdPinf = np.zeros(dPinf.shape)<EOL>for i in range(dPinf.shape[<NUM_LIT:2>]):<EOL><INDENT>bdPinf[:,:,i] = np.dot( T_inv, np.dot( dPinf[:,:,i], T_inv.T))<EOL><INDENT>LL,islower = linalg.cho_factor(dPinf[:,:,i])<EOL>inds = np.triu_indices(dPinf[:,:,i].shape[<NUM_LIT:0>],k=<NUM_LIT:1>)<EOL>LL[inds] = <NUM_LIT:0.0><EOL>bLL = np.dot(T_inv, LL)<EOL>bdPinf[:,:,i] = np.dot( bLL, bLL.T)<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>bdPinf = None<EOL><DEDENT>if dP0 is not None:<EOL><INDENT>bdP0 = np.zeros(dP0.shape)<EOL>for i in range(dP0.shape[<NUM_LIT:2>]):<EOL><INDENT>bdP0[:,:,i] = np.dot( T_inv, np.dot( dP0[:,:,i], T_inv.T))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>bdP0 = None<EOL><DEDENT>bdQc = dQc <EOL>return bF, bL, bQc, bH, bPinf, bP0, bdF, bdQc, bdPinf, bdP0<EOL>
Balances State-Space model for more numerical stability

This is based on the following:

 dx/dt = F x + L w
     y = H x

 Let T z = x, which gives

 dz/dt = inv(T) F T z + inv(T) L w
     y = H T z