AP_NavEKF3: derivation: auto-substitute optimized power functions

Avoids the need to manually insert them after generation.
This commit is contained in:
Thomas Watson 2024-08-11 13:33:55 -05:00 committed by Andrew Tridgell
parent f3a39105b9
commit c880bf57cd
1 changed files with 12 additions and 1 deletions

View File

@ -9,11 +9,22 @@ class CodeGenerator:
self.file_name = file_name
self.file = open(self.file_name, 'w')
# custom SymPy -> C function mappings. note that at least one entry must
# match, the last entry will always be used if none match!
self._custom_funcs = {
"Pow": [
(lambda b, e: e == 2, lambda b, e: f"sq({b})"), # use square function for b**2
(lambda b, e: e == -1, lambda b, e: f"1.0F/({b})"), # inverse
(lambda b, e: e == -2, lambda b, e: f"1.0F/sq({b})"), # inverse square
(lambda b, e: True, "powf"), # otherwise use default powf
],
}
def print_string(self, string):
self.file.write("// " + string + "\n")
def get_ccode(self, expression):
return ccode(expression, type_aliases={real:float32})
return ccode(expression, type_aliases={real:float32}, user_functions=self._custom_funcs)
def write_subexpressions(self,subexpressions):
write_string = ""