I'm trying to plot a complex function and to that end I'm using pgfmathdeclarefunction. Still, I'm unable to get it to work, I get the error:
! Illegal parameter number in definition of \pgfmathNi@.
I start wondering whether what I'm trying to do is even possible: to call a function within another function in the definition of pgfmathdeclarefunction. This is the MWE:
\documentclass{standalone}
\usepackage{pgfplots}
\pgfmathdeclarefunction{detG}{8}{%
\pgfmathparse{#1*(#7*(#4 - #6)*(#2 - #8) - #5*(#2 - #6)*(#4 - #8) + #3*(#2 - #4)*(#6 - #8)) + #3*#5*(#4 - #6)*(#2 - #8) - #3*#7*(#2 - #6)*(#4 - #8) + #5*#7*(#2 - #4)*(#6 - #8)
}%
}
\pgfmathdeclarefunction{Hii}{8}{%
\pgfmathparse{#5*#7*#4*(#6 - #8) + #3*#5*(#4 - #6)*#8 + #3*#7*#6*(-#4 + #8)
}%
}
\pgfmathdeclarefunction{Hji}{8}{%
\pgfmathparse{#3*#4*(#6 - #8) + #7*(#4 - #6)*#8 + #5*#6*(-#4 + #8)
}%
}
\pgfmathdeclarefunction{Hki}{8}{%
\pgfmathparse{#3*#5*(-#4 + #6) + #3*#7*(#4 - #8) + #5*#7*(-#6 + #8)
}%
}
\pgfmathdeclarefunction{Hli}{8}{%
\pgfmathparse{#7*(-#4 + #6) + #5*(#4 - #8) + #3*(-#6 + #8)
}%
}
\pgfmathdeclarefunction{Ni}{10}{%
\pgfmathparse{Hii(#1,#2,#3,#4,#5,#6,#7,#8) + Hji(#1,#2,#3,#4,#5,#6,#7,#8)*#9 + Hki(#1,#2,#3,#4,#5,#6,#7,#8)*#10 + Hli(#1,#2,#3,#4,#5,#6,#7,#8)*#9*#10
}%
}
\begin{document}
\begin{tikzpicture}[]
\begin{axis}[]
\addplot3[surf,domain=0:1, samples=20] { Ni(-1,-1,0,-1,0,0,-1,0,x,y) / detG(-1,-1,0,-1,0,0,-1,0)};
\end{axis}
\end{tikzpicture}
\end{document}
EDIT Thanks to the answer provided by @Qrrbrbirlbel I could plot one function. I managed to generalize the code to plot 4 of these functions (very similar). The functions should make a tent, so that each function plotted in its own square domain has a value of 1 at coordinate (0,0). This is the result I have so far, and although it works, there is a lot of repeated code:
and the current code:
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfmathdeclarefunction{detGi}{8}{%
\pgfmathparse{-(#1*(#7*(#4-#6)*(#2-#8)-#5*(#2-#6)*(#4-#8)+#3*(#2-#4)*(#6-#8))
+#3*#5*(#4-#6)*(#2-#8)-#3*#7*(#2-#6)*(#4-#8)+#5*#7*(#2-#4)*(#6-#8))}}
\pgfmathdeclarefunction{detGj}{8}{%
\pgfmathparse{-(#1*(#7*(#4-#6)*(#2-#8)-#5*(#2-#6)*(#4-#8)+#3*(#2-#4)*(#6-#8))
+#3*#5*(#4-#6)*(#2-#8)-#3*#7*(#2-#6)*(#4-#8)+#5*#7*(#2-#4)*(#6-#8))}}
\pgfmathdeclarefunction{detGk}{8}{%
\pgfmathparse{-(#1*(#7*(#4-#6)*(#2-#8)-#5*(#2-#6)*(#4-#8)+#3*(#2-#4)*(#6-#8))
+#3*#5*(#4-#6)*(#2-#8)-#3*#7*(#2-#6)*(#4-#8)+#5*#7*(#2-#4)*(#6-#8))}}
\pgfmathdeclarefunction{detGl}{8}{%
\pgfmathparse{-(#1*(#7*(#4-#6)*(#2-#8)-#5*(#2-#6)*(#4-#8)+#3*(#2-#4)*(#6-#8))
+#3*#5*(#4-#6)*(#2-#8)-#3*#7*(#2-#6)*(#4-#8)+#5*#7*(#2-#4)*(#6-#8))}}
\pgfmathdeclarefunction{Hii}{8}{%
\pgfmathparse{#5*#7*#4*(#6-#8)+#3*#5*(#4-#6)*#8+#3*#7*#6*(-#4+#8)}}
\pgfmathdeclarefunction{Hji}{8}{%
\pgfmathparse{#3*#4*(#6-#8)+#7*(#4-#6)*#8+#5*#6*(-#4+#8)}}
\pgfmathdeclarefunction{Hki}{8}{%
\pgfmathparse{#3*#5*(-#4+#6)+#3*#7*(#4-#8)+#5*#7*(-#6+#8)}}
\pgfmathdeclarefunction{Hli}{8}{%
\pgfmathparse{#7*(-#4+#6)+#5*(#4-#8)+#3*(-#6+#8)}}
\pgfmathdeclarefunction{Hij}{8}{%
\pgfmathparse{#1*#7*#6*(#2-#8)+#1*#5*(-#2+#6)*#8+#5*#7*#2*(-#6+#8)}}
\pgfmathdeclarefunction{Hjj}{8}{%
\pgfmathparse{#5*#6*(#2-#8)+#7*(-#2+#6)*#8+#1*#2*(-#6+#8)}}
\pgfmathdeclarefunction{Hkj}{8}{%
\pgfmathparse{#1*#5*(#2-#6)+#5*#7*(#6-#8)+#1*#7*(-#2+#8)}}
\pgfmathdeclarefunction{Hlj}{8}{%
\pgfmathparse{#7*(#2-#6)+#1*(#6-#8)+#5*(-#2+#8)}}
\pgfmathdeclarefunction{Hik}{8}{%
\pgfmathparse{#3*#7*#2*(#4-#8)+#1*#3*(#2-#4)*#8+#1*#7*#4*(-#2+#8)}}
\pgfmathdeclarefunction{Hjk}{8}{%
\pgfmathparse{#1*#2*(#4-#8)+#7*(#2-#4)*#8+#3*#4*(-#2+#8)}}
\pgfmathdeclarefunction{Hkk}{8}{%
\pgfmathparse{#1*#3*(-#2+#4)+#1*#7*(#2-#8)+#3*#7*(-#4+#8)}}
\pgfmathdeclarefunction{Hlk}{8}{%
\pgfmathparse{#7*(-#2+#4)+#3*(#2-#8)+#1*(-#4+#8)}}
\pgfmathdeclarefunction{Hil}{8}{%
\pgfmathparse{#1*#5*#4*(#2-#6)+#1*#3*(-#2+#4)*#6+#3*#5*#2*(-#4+#6)}}
\pgfmathdeclarefunction{Hjl}{8}{%
\pgfmathparse{#3*#4*(#2-#6)+#5*(-#2+#4)*#6+#1*#2*(-#4+#6)}}
\pgfmathdeclarefunction{Hkl}{8}{%
\pgfmathparse{#1*#3*(#2-#4)+#3*#5*(#4-#6)+#1*#5*(-#2+#6)}}
\pgfmathdeclarefunction{Hll}{8}{%
\pgfmathparse{#5*(#2-#4)+#1*(#4-#6)+#3*(-#2+#6)}}
\makeatletter
\pgfmathdeclarefunction{Ni}{2}{%
\begingroup
\def\vo##1{\pgfkeysvalueof{/pgfplots/helper/##1}}% shortcut for here
\pgfmathparse{\vo{Hii}+\vo{Hji}*#1+\vo{Hki}*#2+\vo{Hli}*#1*#2}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfmathdeclarefunction{NiDet}{2}{%
\begingroup
\pgfmathNi@{#1}{#2}%
\pgfmathdivide@{\pgfmathresult}{\pgfkeysvalueof{/pgfplots/helper/detGi}}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfmathdeclarefunction{Nj}{2}{%
\begingroup
\def\vo##1{\pgfkeysvalueof{/pgfplots/helper/##1}}% shortcut for here
\pgfmathparse{\vo{Hij}+\vo{Hjj}*#1+\vo{Hkj}*#2+\vo{Hlj}*#1*#2}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfmathdeclarefunction{NjDet}{2}{%
\begingroup
\pgfmathNj@{#1}{#2}%
\pgfmathdivide@{\pgfmathresult}{\pgfkeysvalueof{/pgfplots/helper/detGj}}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfmathdeclarefunction{Nk}{2}{%
\begingroup
\def\vo##1{\pgfkeysvalueof{/pgfplots/helper/##1}}% shortcut for here
\pgfmathparse{\vo{Hik}+\vo{Hjk}*#1+\vo{Hkk}*#2+\vo{Hlk}*#1*#2}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfmathdeclarefunction{NkDet}{2}{%
\begingroup
\pgfmathNk@{#1}{#2}%
\pgfmathdivide@{\pgfmathresult}{\pgfkeysvalueof{/pgfplots/helper/detGk}}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfmathdeclarefunction{Nl}{2}{%
\begingroup
\def\vo##1{\pgfkeysvalueof{/pgfplots/helper/##1}}% shortcut for here
\pgfmathparse{\vo{Hil}+\vo{Hjl}*#1+\vo{Hkl}*#2+\vo{Hll}*#1*#2}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfmathdeclarefunction{NlDet}{2}{%
\begingroup
\pgfmathNl@{#1}{#2}%
\pgfmathdivide@{\pgfmathresult}{\pgfkeysvalueof{/pgfplots/helper/detGl}}%
\pgfmathsmuggle\pgfmathresult
\endgroup
}
\pgfkeys{
/handlers/.pgfmath/.code=%
\pgfmathparse{#1}\expandafter\pgfkeys@exp@call\expandafter{\pgfmathresult},
/handlers/.pgfmath fpu/.code=%
\begingroup\pgfset{fpu}\pgfmathparse{#1}\expandafter\endgroup
\expandafter\pgfkeys@exp@call\expandafter{\pgfmathresult}}
\makeatother
\begin{document}
\begin{tikzpicture}
\begin{axis}
% plot Nj in [-1,0]x[0,1]
\pgfplotsset{
helper/Hij/.initial=0, helper/Hjj/.initial=0,
helper/Hkj/.initial=0, helper/Hlj/.initial=0,
helper/detGj/.initial=0,
prepare matrix/.style={
helper/Hij/.pgfmath fpu={Hij(#1)}, helper/Hjj/.pgfmath fpu={Hjj(#1)},
helper/Hkj/.pgfmath fpu={Hkj(#1)}, helper/Hlj/.pgfmath fpu={Hlj(#1)},
helper/detGj/.pgfmath fpu={detGj(#1)}
}}
\addplot3[surf, domain=-1:0, y domain=0:1, samples=20, prepare matrix={-1,0,0,0,0,1,-1,1}]
{NjDet(x,y)};
% plot Ni in [0,1]x[0,1]
\pgfplotsset{
helper/Hii/.initial=0, helper/Hji/.initial=0,
helper/Hki/.initial=0, helper/Hli/.initial=0,
helper/detGi/.initial=0,
prepare matrix/.style={
helper/Hii/.pgfmath fpu={Hii(#1)}, helper/Hji/.pgfmath fpu={Hji(#1)},
helper/Hki/.pgfmath fpu={Hki(#1)}, helper/Hli/.pgfmath fpu={Hli(#1)},
helper/detGi/.pgfmath fpu={detGi(#1)}
}}
\addplot3[surf, domain=0:1, y domain=0:1, samples=20, prepare matrix={0,0,1,0,1,1,0,1}]
{NiDet(x,y)};
% plot Nk in [-1,0]x[-1,0]
\pgfplotsset{
helper/Hik/.initial=0, helper/Hjk/.initial=0,
helper/Hkk/.initial=0, helper/Hlk/.initial=0,
helper/detGk/.initial=0,
prepare matrix/.style={
helper/Hik/.pgfmath fpu={Hik(#1)}, helper/Hjk/.pgfmath fpu={Hjk(#1)},
helper/Hkk/.pgfmath fpu={Hkk(#1)}, helper/Hlk/.pgfmath fpu={Hlk(#1)},
helper/detGk/.pgfmath fpu={detGk(#1)}
}}
\addplot3[surf, domain=-1:0, y domain=-1:0, samples=20, prepare matrix={-1,-1,0,-1,0,0,-1,0}]
{NkDet(x,y)};
% plot Nl in [0,1]x[-1,0]
\pgfplotsset{
helper/Hil/.initial=0, helper/Hjl/.initial=0,
helper/Hkl/.initial=0, helper/Hll/.initial=0,
helper/detGl/.initial=0,
prepare matrix/.style={
helper/Hil/.pgfmath fpu={Hil(#1)}, helper/Hjl/.pgfmath fpu={Hjl(#1)},
helper/Hkl/.pgfmath fpu={Hkl(#1)}, helper/Hll/.pgfmath fpu={Hll(#1)},
helper/detGl/.pgfmath fpu={detGl(#1)}
}}
\addplot3[surf, domain=0:1, y domain=-1:0, samples=20, prepare matrix={0,-1,1,-1,1,0,0,0}]
{NlDet(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}


detfunctions are the same, you don't need four of them. You can dohelper/detGk/.pgfmath fpu={detG(#1)}and similar for the others, too. I'm surprised that the other functions are very similar but don't just reorder their arguments. I was hopingHji(1,2,3,4,5,6,7,8) = Hii(3,4,5,6,1,2,7,8)but I guess not …