PeakLab v1 Documentation Contents            AIST Software Home            AIST Software Support

User Function Fitting - Maple and Mathematica Import

Maple

When importing Maple functions into PeakLab user functions, the process is straightforward. The following is the formula that appears in Maple for the PeakLab EMG model:

Direct Copy in Maple

You can highlight the formatted function in Maple and select Copy with the right click popup or use ^C. You can also use the lprint command to generate the expression string for the copy.

When you paste this Maple-copied expression from this example into the main Function field of a PeakLab user function, it appears as follows:

1/2*a0*exp(1/2*a2^2/a3^2+(a1-x)/a3)*erfc(-1/2*((x-a1)/a2-a2/a3)*signum(a3)*2^(1/2))*signum(a3)/a3

In PeakLab, use the Paste button or ^C to paste the expression into the user function (do not use the Paste Mathematica button). Add Y= to the beginning of the expression. Y is one of the reserved variable names in PeakLab and is used to indicate the final peak expression.

Code Generation in Maple

If you wish to use Maple's code generation capabilities, you should probably use the C language. For the example of this EMG function, we see the following:

t0 = a0*exp(a2*a2/(a3*a3)/2.0+(a1-x)/a3)*erfc(-((x-a1)/a2-a2/a3)*(a3<0 ? -1 : 1)*sqrt(2.0)/2.0)*(a3<0 ? -1 : 1)/a3/2.0;

There may be certain language code shorthands you will have to manually change as in the example of the two sign expressions. You must change the t0 to y.

y = a0*exp(a2*a2/(a3*a3)/2.0+(a1-x)/a3)*erfc(-((x-a1)/a2-a2/a3)*signum(a3)*sqrt(2.0)/2.0)*signum(a3)/a3/2.0;

Ending semicolons are ignored in the PeakLab parser.

If you have a particularly complicated function, Maple's C code generation may place an expression across multiple lines, each with a new line or carriage return. If that occurs, you will need to manually remove the carriage returns so that all of the expression is on a single line. PeakLab's user functions require each line to have an = assignment to a variable. Each line in a user function can be of any length, subject only to the program's 8192 character limit.

Optimized Code Generation in Maple

You may be able to create a faster executing function by using the optimized option in Maple for code generation.

t1 = a2*a2;

t2 = a3*a3;

t6 = a1-x;

t7 = 1/a3;

t10 = exp(t1/t2/2.0+t6*t7);

t16 = (a3<0 ? -1 : 1);

t18 = sqrt(2.0);

t21 = erfc(-(-t6/a2-a2*t7)*t16*t18/2.0);

t25 = a0*t10*t21*t16*t7/2.0;

In this case, paste the generated code directly in the main Function field of the PeakLab user function. The leading spaces and ending semicolons are fine. Here you will need to again replace the C-language shorthand in t16 for for the sign() function and you will need to change t25 to y:

t1 = a2*a2;

t2 = a3*a3;

t6 = a1-x;

t7 = 1/a3;

t10 = exp(t1/t2/2.0+t6*t7);

t16 = sign(a3);

t18 = sqrt(2.0);

t21 = erfc(-(-t6/a2-a2*t7)*t16*t18/2.0);

y = a0*t10*t21*t16*t7/2.0;

Because of the cross-compatibility with Maple and Mathematica, there may be multiple options for expressing a given basic function. An exponential can be expressed as E^x or exp(x) or Exp(x) or EXP(x).

In some instances, the same function may have multiple names. For example, the user function help dropdown for Statistical Functions will show the Psi(), DiGamma(), and PolyGamma() functions as the first derivative of the gamma function. The function Psi(z) in Maple and PolyGamma(z) in Mathematica are realized with the case-insensitive psi function in PeakLab. Because PeakLab doesn't offer a PolyGamma(n,z) as in Mathematica for arbitrary derivatives, there is a DiGamma(), this same first derivative of the gamma function, and a TriGramma(), the second derivative.

Please note that most PeakLab-specific functions are case sensitive. Only the functions which are used across these different platform languages are case-insensitive. Unless a function in this help is specifically listed as case-insensitive, it must be entered as shown. For the generalized HVL, for example, you must enter the built-in function exactly as GenHVL; all lower case or all upper case will fail.

In most instances, you will not need to make any modifications to the Maple code when using the Direct Copy approach. Note that complex math is not supported in user functions, and there may be a few special functions in Maple which are not supported. We have endeavored to include all special functions we have found to be even remotely useful in constructing density models.

User functions have a limit of 8192 total characters for the function expression. That is quite hard to exceed, but there is also a limit of a maximum of 100 separate expressions. You will need to use a single line expression for highly complex models if optimized Maple code generation produces more than 100 intermediates.

Mathematica

When importing Mathematica functions into user functions, additional steps are needed. The following is the formula that appears in Mathematica for the PeakLab EMG model:

Right click the expression in Mathematica and choose Copy As | Plain Text from the popup menu. Mathematica creates the following expression on the clipboard:

(a0 E^(a2^2/(2 a3^2)+(a1-x)/a3) Erfc[-(((-(a2/a3)+(-a1+x)/a2) Sign[a3])/Sqrt[2])] Sign[a3])/(2 a3)

To convert this expression to the format needed for PeakLab user functions, you cannot paste the expression directly. Place the cursor in the function field and click the Paste Mathematica button.

(a0*E^(a2^2/(2*a3^2)+(a1-x)/a3)*Erfc(-(((-(a2/a3)+(-a1+x)/a2)*Sign(a3))/Sqrt(2)))*Sign(a3))/(2*a3)

Simply add Y= to the beginning of the expression.

For Mathematica functions, it is necessary that you specify a single line expression in a PeakLab user function. You should be able to Copy As | Plain Text most any real-valued Mathematica model, no matter how complex, so long as all functions used are also present within PeakLab.