emoncms / postprocess

Post Processing module for emoncms
GNU Affero General Public License v3.0
3 stars 12 forks source link

basic formula improvments #19

Closed alexandrecuer closed 4 years ago

alexandrecuer commented 4 years ago

Last year, I've introduced the basic_formula process, without brackets or mathematical functions. The simpliest formula example can be symbolized as follow, assuming you work on feeds 1,2,3 :

f1+2*f2-f3/12 

This 2020 pull request implements brackets and the max function.... So you can elaborate more complex expression such as :

1162.5*5.19*max(f7-f11,0)

or :

1162.5*f10*(f7-11) 

This is usefull when you have to deal with hot water flows

Caution : (f12-f13)*(f7-f11) will not be recognized as a valid function !!

Others functions can be easily injected by adding a new element to the functions library

// functions list
// brackets must always be the last function in the list
$functions=[
      ["name"=>"max","f"=>"max\(($Xbf),($Xnbr)\)"],
      ["name"=>"brackets","f"=>"\(($Xbf)\)"],
 ];

$Xbf invokes a regular expression to catch a basic formula such as f1+2*f2-f3/12 $Xnbr invokes a regular expression to catch a float or an int

In order to implement a new function, others part of the code have to be updated :

TrystanLea commented 4 years ago

Thanks @alexandrecuer this looks great! I've found this postprocess really useful quite a few times now, much appreciated!