6
$\begingroup$

I often need to translate results I derive in Mathematica into a MATLAB implementation. These results sometimes involve special functions (e.g. Erfc, BesselJ) whose names and argument order do not correspond with those used in MATLAB.

Are there any resources showing such correspondences?

$\endgroup$
7
  • $\begingroup$ Can't you just find out the function definitions? So for instance, Erfc[z] is the same as 1 - 2/Sqrt[Pi] Integrate[Exp[-t^2], {t, 0, z}] $\endgroup$
    – Feyre
    Commented Aug 21, 2016 at 21:20
  • $\begingroup$ @Feyre: I don't see that directly helps me guess what name I should be looking for in the MATLAB documentation. $\endgroup$
    – mikado
    Commented Aug 21, 2016 at 21:23
  • $\begingroup$ I thought you meant they had no equal, in which case an equivalent statement should be used. Wouldn't this otherwise be better asked at the Matlab forums? $\endgroup$
    – Feyre
    Commented Aug 21, 2016 at 21:32
  • $\begingroup$ @Feyre: this question is only of interest to people who use Mathematica (not all of them, obviously). There are other positively-rated questions on related topics here (e.g. mathematica.stackexchange.com/questions/44223/…) $\endgroup$
    – mikado
    Commented Aug 21, 2016 at 22:27
  • 2
    $\begingroup$ The elliptic integrals and elliptic functions are particularly tricky. Mathematica uses the parameter convention, while MATLAB uses the modulus convention. $\endgroup$ Commented Oct 2, 2016 at 12:54

3 Answers 3

9
$\begingroup$

I would first do the necessary transformations manually using a ReplaceAll. Then I would translate the result to MATLAB syntax using tools such as ToMatlab.

For example,

trafo = Dispatch@{
   Gamma[a_] :> "gamma"[a],
   Gamma[a_, z_] :> "gamma"[a] (1 - "gammainc"[z, a])
   (* and many more transformations here *)
  }

ToMatlab[ x Sin[x] - Gamma[x,2] /. trafo ]

(*

"(-1).*gamma(x).*(1+(-1).*gammainc(2,x))+x.*sin(x);"

*)

You would need to create the replacement table yourself, but this shouldn't be a lot of work. Finding the corresponding special functions int the documentation is straightforward and the definitions are given on both sides (so you can see if there's a significant difference, such as with the incomplete Γ function above).

$\endgroup$
1
  • $\begingroup$ +1 but I would argue that finding, understanding and testing the corresponding special functions (particularly when they take multiple arguments) is non-trivial and that it would be useful to have a reliable list. $\endgroup$
    – mikado
    Commented Aug 22, 2016 at 18:10
6
$\begingroup$

The following is a table that I have developed, tested and found to be useful over the years, showing Mathematica functions on the left and their MATLAB equivalents on the right.

ArcTan[x,y]                   atan2(y,x)                                                   
BetaRegularized[z,a,b]        betainc(z,a,b)                                               
ExpIntegralE[1,z]             expint(z)                                                    
Gamma[a,z]                    gammainc(z,a,'upper')*gamma(a)                               
GammaRegularized[n,0,x]       gammainc(x,n)                                                
InverseErf[z]                 erfinv(z)                                                    
InverseGammaRegularized[a,s]  None found                                                   
LegendreP[n,x]                first row of: legendre(n,x)                                  
PolyGamma[z]                  psi(z)                                                       
PolyGamma[n,z]                psi(n,z)                                                     
SphericalBesselJ[n,z]         sqrt(pi/(2*z))*besselj(n+1/2,z)*sign(z)                      
SphericalHankelH2[n,z]        sqrt(pi/(2*z))*(besselj(n+1/2,z)-i*bessely(n+1/2,z))*sign(z) 

I would encourage others to add to and improve this list.

$\endgroup$
3
$\begingroup$

Also, there is a way to convert MeijerG function to Matlab. A ready-to-use Mupad MeijerG function caller for Matlab is available at Matlab Cental. The following line need to be modified in ToMatlab package for cell array support:

ToMatlabaux[l_List] := "[" <> ToMatlabargs[l] <> "]"

Replace this line with:

ToMatlabaux[l_List] := 
    If[FreeQ[l,_List,{1,Infinity}],
        "[" <> ToMatlabargs[l] <> "]"(*in case of Matlab vector*),
        "{" <> ToMatlabargs[l] <> "}"(*in case of Matlab cell array*)]

Also, for support of empty cells the following line

ToMatlabargs[e_] :=
    If[Length[e] === 1, 
        ToMatlabaux[e[[1]]],
        ToMatlabaux[e[[1]]] <> "," <>
            ToMatlabargs[ argslistdrop[e] ] ]

need to be replaced with

ToMatlabargs[e_] :=
    If[Length[e] === 1, 
        ToMatlabaux[e[[1]]],
        If[Length[e]>0,
        ToMatlabaux[e[[1]]] <> "," <>
            ToMatlabargs[ argslistdrop[e] ],e]]

The following lines need to be added to ToMatlab for the case of presence of additional argument r in Mathematica MeijerG:

(*Convert generalized MeijerG to usual MeijerG. MeijerGsubstitute is used in order to avoid potential recursion problem caused by automatic transformation of MeijerG*)
(*  http://functions.wolfram.com/07.35.26.0004.01  *)
ToMatlabaux[MeijerG[{a1__,a2__},{b1__,b2__},z_,r_]]:=ToMatlabaux[MeijerGsubstitute[{a1,a2},{b1,b2},z^(1/r)]]/;Refine[r>=1||r<-1||-Pi*r<Arg[z]<=Pi*r]
(*  http://functions.wolfram.com/07.35.26.0005.01  *)
ToMatlabaux[MeijerG[{a1__,a2__},{b1__,b2__},z_,r_]]:=Module[{n,p,m,q,\[Xi],\[Mu],g,ik},
    n=Length[a1];p=n+Length[a2];m=Length[b1];q=m+Length[b2];\[Xi]=((p+q)/2-m-n)(r-1);
    \[Mu]=Total@b1+Total@b2-(Total@a1+Total@a2)+(p-q)/2+1;
    g[x_]:=Table[(x+ik)/r,{ik,0,r-1}];
    ToMatlabaux[(2 \[Pi])^\[Xi]*r^\[Mu]*MeijerGsubstitute[{Flatten[g/@a1],Flatten[g/@a2]},{Flatten[g/@b1],Flatten[g/@b2]},r^(r(p-q))*z]]]/;Refine[r\[Element]Integers&&r>0]

Then MeijerGsubstitute converted to proper function name for call from Matlab:

ToMatlabaux[MeijerGsubstitute]:="MeijerG"

P.S. ToMatlab package definitely requires an update after 18 years. Updated by me version of ToMatlab is available here. It also includes support for Piecewise, BesselJ, BesselY, BesselI, BesselK, HypergeometricPFQ, HypergeometricPFQRegularized.

Updated conversion for Piecewise and If functions in my version of ToMatlab. Values for all conditions of Piecewise are converted to Matlab cell array of strings. The element of this cell array is extracted by indexing based on the index of condition, that yields True, in array of conditions. Thus, Matlab evaluates only expression, condition of which is satisfied:

ToMatlabaux[If[test_, t_, f_]] :="evalin('caller',subsref({"<>"'"<>Assuming[test,ToMatlabaux[t]]<>
    "','"<>Assuming[!test,ToMatlabaux[f]]<>"'"<>
    "},struct('type','{}','subs',{{find("<>ToMatlabaux[{test,True}]<>",1,'first')}})))"

Off[Piecewise::pairs]
ToMatlabaux[Piecewise[z_List, f_.]]:=Module[{lastCondition=Simplify[And@@Not/@z[[All,2]]]},
"evalin('caller',subsref({"<>StringJoin@Map["'"<>Assuming[#[[2]],ToMatlabaux[#[[1]]]]<>"',"&,z]<>"'"<>
    Assuming[lastCondition===False||lastCondition,ToMatlabaux[f]]<>"'"<>
    "},struct('type','{}','subs',{{find("<>ToMatlabaux[Flatten@{z[[All,2]],True}]<>",1,'first')}})))"]
On[Piecewise::pairs]

For those, who do not like this ugly/reliable conversion, conversion of Piecewise and If in terms of products of conditions by corresponding values are left commented in ToMatlab file.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.