From ee820d4ed44875a4f0b55e7d140a63ea77ef4667 Mon Sep 17 00:00:00 2001 From: Jean-Philippe ARGAUD Date: Wed, 5 Jun 2013 15:49:58 +0200 Subject: [PATCH] Avoiding unnecessary calculations --- src/daComposant/daAlgorithms/Blue.py | 26 +++++++++++++------ .../daAlgorithms/LinearLeastSquares.py | 26 +++++++++++++------ 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/src/daComposant/daAlgorithms/Blue.py b/src/daComposant/daAlgorithms/Blue.py index 99619a2..915c51f 100644 --- a/src/daComposant/daAlgorithms/Blue.py +++ b/src/daComposant/daAlgorithms/Blue.py @@ -30,6 +30,12 @@ import numpy class ElementaryAlgorithm(BasicObjects.Algorithm): def __init__(self): BasicObjects.Algorithm.__init__(self, "BLUE") + self.defineRequiredParameter( + name = "StoreInternalVariables", + default = False, + typecast = bool, + message = "Stockage des variables internes ou intermédiaires du calcul", + ) self.defineRequiredParameter( name = "StoreSupplementaryCalculations", default = [], @@ -49,7 +55,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # Opérateur d'observation # ----------------------- Hm = HO["Tangent"].asMatrix(Xb) + Hm = Hm.reshape(Y.size,Xb.size) # ADAO & check shape Ha = HO["Adjoint"].asMatrix(Xb) + Ha = Ha.reshape(Xb.size,Y.size) # ADAO & check shape # # Utilisation éventuelle d'un vecteur H(Xb) précalculé # ---------------------------------------------------- @@ -93,17 +101,19 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): else: K = (Ha * RI * Hm + BI).I * Ha * RI Xa = Xb + K*d + self.StoredVariables["Analysis"].store( Xa.A1 ) # # Calcul de la fonction coût # -------------------------- - oma = Y - Hm * Xa - Jb = 0.5 * (Xa - Xb).T * BI * (Xa - Xb) - Jo = 0.5 * oma.T * RI * oma - J = float( Jb ) + float( Jo ) - self.StoredVariables["Analysis"].store( Xa.A1 ) - self.StoredVariables["CostFunctionJb"].store( Jb ) - self.StoredVariables["CostFunctionJo"].store( Jo ) - self.StoredVariables["CostFunctionJ" ].store( J ) + if self._parameters["StoreInternalVariables"] or "OMA" in self._parameters["StoreSupplementaryCalculations"] or "SigmaObs2" in self._parameters["StoreSupplementaryCalculations"] or "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]: + oma = Y - Hm * Xa + if self._parameters["StoreInternalVariables"] or "MahalanobisConsistency" in self._parameters["StoreSupplementaryCalculations"]: + Jb = 0.5 * (Xa - Xb).T * BI * (Xa - Xb) + Jo = 0.5 * oma.T * RI * oma + J = float( Jb ) + float( Jo ) + self.StoredVariables["CostFunctionJb"].store( Jb ) + self.StoredVariables["CostFunctionJo"].store( Jo ) + self.StoredVariables["CostFunctionJ" ].store( J ) # # Calcul de la covariance d'analyse # --------------------------------- diff --git a/src/daComposant/daAlgorithms/LinearLeastSquares.py b/src/daComposant/daAlgorithms/LinearLeastSquares.py index 5ef5e68..cd0496b 100644 --- a/src/daComposant/daAlgorithms/LinearLeastSquares.py +++ b/src/daComposant/daAlgorithms/LinearLeastSquares.py @@ -30,6 +30,12 @@ import numpy class ElementaryAlgorithm(BasicObjects.Algorithm): def __init__(self): BasicObjects.Algorithm.__init__(self, "LINEARLEASTSQUARES") + self.defineRequiredParameter( + name = "StoreInternalVariables", + default = False, + typecast = bool, + message = "Stockage des variables internes ou intermédiaires du calcul", + ) self.defineRequiredParameter( name = "StoreSupplementaryCalculations", default = [], @@ -49,7 +55,9 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # Opérateur d'observation # ----------------------- Hm = HO["Tangent"].asMatrix(None) + Hm = Hm.reshape(Y.size,-1) # ADAO & check shape Ha = HO["Adjoint"].asMatrix(None) + Ha = Ha.reshape(-1,Y.size) # ADAO & check shape # if R is not None: RI = R.I @@ -62,17 +70,19 @@ class ElementaryAlgorithm(BasicObjects.Algorithm): # -------------------------------------------- K = (Ha * RI * Hm ).I * Ha * RI Xa = K * Y + self.StoredVariables["Analysis"].store( Xa.A1 ) # # Calcul de la fonction coût # -------------------------- - oma = Y - Hm * Xa - Jb = 0. - Jo = 0.5 * oma.T * RI * oma - J = float( Jb ) + float( Jo ) - self.StoredVariables["Analysis"].store( Xa.A1 ) - self.StoredVariables["CostFunctionJb"].store( Jb ) - self.StoredVariables["CostFunctionJo"].store( Jo ) - self.StoredVariables["CostFunctionJ" ].store( J ) + if self._parameters["StoreInternalVariables"] or "OMA" in self._parameters["StoreSupplementaryCalculations"]: + oma = Y - Hm * Xa + if self._parameters["StoreInternalVariables"]: + Jb = 0. + Jo = 0.5 * oma.T * RI * oma + J = float( Jb ) + float( Jo ) + self.StoredVariables["CostFunctionJb"].store( Jb ) + self.StoredVariables["CostFunctionJo"].store( Jo ) + self.StoredVariables["CostFunctionJ" ].store( J ) # # Calculs et/ou stockages supplémentaires # --------------------------------------- -- 2.39.2