Hello Michael,

We investigated using ExprTk to parse analytical formulas instead of
muParser, it is known to be faster.
Almost all muParser formulas can be parsed by ExprTk without changes,
but it also provides many more features.  And multiple outputs are
trivial with this backend.
This has been implemented on current master, your example can be written:

inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B']
outputs = ['H', 'S']
formula = """var alpha := (Zm - Zv)/L;
H := (Q/(Ks*B*sqrt(alpha)))^(3.0/5.0);
var Zc := H + Zv;
var Zd := Zb + Hd;
S := Zc - Zd;"""
myFunction = SymbolicFunction(inputs, outputs, formula)

This is almost straightforward; input and output variables are
declared, all other variables must be prefixed by the keyword 'var',
assignment is ':=', and statements must be separated by semicolons.

But Python can be as fast as SymbolicFunction; pull request #699
avoids copies between OT and Python, your code could be rewritten as

import numpy as np
def functionCrueSample(X) :
    Q, Ks, Zv, Zm, Hd, Zb, L, B = np.array(X, copy=False).T
    alpha = (Zm - Zv)/L
    H = (Q/(Ks*B*np.sqrt(alpha)))**(3.0/5.0)
    Zc = H + Zv
    Zd = Zb + Hd
    S = Zc - Zd;
    Y = np.zeros((len(H), 2))
    Y[:,0] = H
    Y[:,1] = S
    return Y

myFunction = PythonFunction(8, 2, func_sample=functionCrueSample)

Denis

2018-02-14 12:30 GMT+01:00 BAUDIN Michael <[email protected]>:
> Hi,
>
>
>
> I have a symbolic function that I would like to simplify and I do not see
> how.
>
>
>
> Here is the test case. The function has 8 inputs and 2 outputs. In Python it
> is simple to define :
>
>
>
> def functionCrue(X) :
>
>     Q, Ks, Zv, Zm, Hd, Zb, L, B = X
>
>     alpha = (Zm - Zv)/L
>
>     H = (Q/(Ks*B*sqrt(alpha)))**(3.0/5.0)
>
>     Zc = H + Zv
>
>     Zd = Zb + Hd
>
>     S = Zc - Zd
>
>     return [H,S]
>
> myFunction = PythonFunction(8, 2, functionCrue)
>
>
>
> As you can see, the code is simplified by intermediate variables which are
> used by subsequent Python statements. For exemple, the slope of the river
> alpha is first computed, then the height H is computed depending on the
> slope. Then the variable S is computed based on Zc and Zd, where Zc is
> computed depending on the height H.
>
>
>
> When I define the function as a symbolic function, the current definition is
> more involved.
>
>
>
> inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B']
>
> formulas = ['(Q/(Ks*B*sqrt((Zm - Zv)/L)))^(3.0/5.0)','(Q/(Ks*B*sqrt((Zm -
> Zv)/L)))^(3.0/5.0)+Zv-(Zb + Hd)']
>
> myFunction = SymbolicFunction(inputs, formulas)
>
>
>
> As you can see, I cannot reuse the value of the first output into the second
> output.
>
>
>
> The following interface would be much easier to use. The formulas variable
> is a list of couples, where the first item is the name of the output
> variable and the second item is the string to evaluate it.
>
>
>
> inputs = ['Q', 'Ks', 'Zv', 'Zm', 'Hd', 'Zb', 'L', 'B']
>
> output1 = [‘alpha’, '(Zm - Zv)/L']
>
> output2 = [‘H’, '(Q/(Ks*B*sqrt(alpha)))^(3.0/5.0)']
>
> output3 = [‘Zc’, 'H + Zv']
>
> output4 = [‘Zd’, 'Zb + Hd']
>
> output5 = [‘S’, 'Zc - Zd']
>
> formulas = [output1,output2,output3,output4,output5]
>
> myFunction = SymbolicFunction(inputs, formulas)
>
>
>
> Is there another way of doing this ? Do you agree that the suggestion is
> worth being developed ?
>
>
>
> Best regards,
>
>
>
> Michaël
>
>
> Ce message et toutes les pièces jointes (ci-après le 'Message') sont établis
> à l'intention exclusive des destinataires et les informations qui y figurent
> sont strictement confidentielles. Toute utilisation de ce Message non
> conforme à sa destination, toute diffusion ou toute publication totale ou
> partielle, est interdite sauf autorisation expresse.
>
> Si vous n'êtes pas le destinataire de ce Message, il vous est interdit de le
> copier, de le faire suivre, de le divulguer ou d'en utiliser tout ou partie.
> Si vous avez reçu ce Message par erreur, merci de le supprimer de votre
> système, ainsi que toutes ses copies, et de n'en garder aucune trace sur
> quelque support que ce soit. Nous vous remercions également d'en avertir
> immédiatement l'expéditeur par retour du message.
>
> Il est impossible de garantir que les communications par messagerie
> électronique arrivent en temps utile, sont sécurisées ou dénuées de toute
> erreur ou virus.
> ____________________________________________________
>
> This message and any attachments (the 'Message') are intended solely for the
> addressees. The information contained in this Message is confidential. Any
> use of information contained in this Message not in accord with its purpose,
> any dissemination or disclosure, either whole or partial, is prohibited
> except formal approval.
>
> If you are not the addressee, you may not copy, forward, disclose or use any
> part of it. If you have received this message in error, please delete it and
> all copies from your system and notify the sender immediately by return
> message.
>
> E-mail communication cannot be guaranteed to be timely secure, error or
> virus-free.
>
>
> _______________________________________________
> OpenTURNS users mailing list
> [email protected]
> http://openturns.org/mailman/listinfo/users
>
_______________________________________________
OpenTURNS users mailing list
[email protected]
http://openturns.org/mailman/listinfo/users

Reply via email to