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.
Erfc[z]
is the same as1 - 2/Sqrt[Pi] Integrate[Exp[-t^2], {t, 0, z}]
$\endgroup$