如果 edef 中的条件

如果 edef 中的条件

我有一个代码来计算以下插值多项式的除差:

\documentclass[tikz,border=5]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{fp}
\usepackage{xfp}
\usetikzlibrary{math}
\usepackage{etoolbox}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usepackage{booktabs}
\usepackage{siunitx} 
\usepackage{expl3}
\usepackage{etoolbox}
\newcommand{\ar}[1]{\fpeval{round(#1,8)}}


\begin{document}

\tikzset{fixed point arithmetic}
\pgfkeys{/pgf/number format/.cd,fixed,precision=6,zerofill}%
\tikzmath{
    \n=3;
    \xdado=0.9;
    \M=1/4;
    int \i;
    \i=1;
    for \v in {0.67284,0.733849,1.3837}{%Insira as coodenadas dos pontos X
        \x{\i} = \v; 
        \a{\i} = 0;
        \i=\i+1;
    };
    int \j;
    \j=1;
    for \v in {4.12,3.83,3.57}{%Insira as coodenadas dos pontos Y
        \y{\j} = \v; 
        \D{\j} = \v;
        \j=\j+1;
    };
    \a{1} = \y{1};
    for \j in {1,...,\n}{
        for \i in {1,...,\n}{
            \R{\i,\j} = 0.0;        
        };
    };
}

 \tikzmath{
 int \i; 
 int \j;
   for \i in {1,...,\fpeval{\n-1}}{
       for \j in {1,...,\fpeval{\n-\i}}{
         \D{\j} = (\D{\fpeval{\j+1}}-\D{\j})/(\x{\fpeval{\j+\i}}-\x{\j});
         \R{\j,\i} = \D{\j};
       };
      \a{\fpeval{\i+1}} = \D{1}; 
   };
  }



\def\tmp{$i$ & $x_i$ & $y_i$ & ordem 1 & ordem 2\\ \midrule}%
\edef\k{0}%
\loop
\edef\k{\the\numexpr\k+1}%
\xappto\tmp{$\fpeval{\k-1}$ & $\fpeval{round(\x{\k},4)}$&  $\fpeval{round(\y{\k},4)}$ & $\fpeval{round(\R{\k,1},4)}$ & $\fpeval{round(\R{\k,2},4)}$}%
\gappto\tmp{\\}%
\ifnum\k<\n\repeat

\begin{tikzpicture}
%\clip (-9,-3) rectangle (9,3);
\node at (0,0) {\parbox{20cm}{
\underline{Método de Newton} para calcular o polinômio interpolador sobre os pontos: \\

$\foreach \i in {1,...,\n}{(\ar{\x{\i}},\ar{\y{\i}}),}$ \\

e para tanto, usamos a Tabela das \underline{Diferenças Divididas} resumida a seguir:\\

\begin{tabular}{r|r|r|r|r}
\toprule
\tmp
\bottomrule
\end{tabular}\\[0.5cm]
Os cálculos estão detalhados a seguir.
}};
\end{tikzpicture}

\end{document}

我使用以下结构:

\def\tmp{$i$ & $x_i$ & $y_i$ & ordem 1 & ordem 2\\ \midrule}%
\edef\k{0}%
\loop
\edef\k{\the\numexpr\k+1}%
\xappto\tmp{$\fpeval{\k-1}$ & $\fpeval{round(\x{\k},4)}$&  $\fpeval{round(\y{\k},4)}$ & $\fpeval{round(\R{\k,1},4)}$ & $\fpeval{round(\R{\k,2},4)}$}%
\gappto\tmp{\\}%
\ifnum\k<\n\repeat

在此处输入图片描述

构建表格,其中包含\R上述代码中计算的变量(矩阵)。此矩阵对角线下方有空组件,我不想在此表格中打印这些组件(组件\R{3,1}, \R{2,2}, \R{3,2})。在这种情况下,我想在迭代表中打印符号 *。我该怎么做?

答案1

\fpeval是完全可扩展的,因此您可以(直接)将其结果与零进行比较:

% using \protected\def will make each \fpeval evaluated only once
\protected\def\zeroToStar[1]{%
  \ifdim#1pt=0pt\relax*\else#1\fi
}

% then use
$\zeroToStar{\fpeval{<math expression>}}$

完整示例

\documentclass[tikz,border=5]{standalone}
\usepackage{tikz}
\usepackage{amsmath}
\usepackage{fp}
\usepackage{xfp}
\usetikzlibrary{math}
\usepackage{etoolbox}
\usetikzlibrary{fixedpointarithmetic}
\usepackage{booktabs}
\usepackage{siunitx} 
\usepackage{expl3}
\usepackage{etoolbox}
\newcommand{\ar}[1]{\fpeval{round(#1,8)}}


\begin{document}

\tikzset{fixed point arithmetic}
\pgfkeys{/pgf/number format/.cd,fixed,precision=6,zerofill}%
\tikzmath{
    \n=3;
    \xdado=0.9;
    \M=1/4;
    int \i;
    \i=1;
    for \v in {0.67284,0.733849,1.3837}{%Insira as coodenadas dos pontos X
        \x{\i} = \v; 
        \a{\i} = 0;
        \i=\i+1;
    };
    int \j;
    \j=1;
    for \v in {4.12,3.83,3.57}{%Insira as coodenadas dos pontos Y
        \y{\j} = \v; 
        \D{\j} = \v;
        \j=\j+1;
    };
    \a{1} = \y{1};
    for \j in {1,...,\n}{
        for \i in {1,...,\n}{
            \R{\i,\j} = 0.0;        
        };
    };
}

 \tikzmath{
 int \i; 
 int \j;
   for \i in {1,...,\fpeval{\n-1}}{
       for \j in {1,...,\fpeval{\n-\i}}{
         \D{\j} = (\D{\fpeval{\j+1}}-\D{\j})/(\x{\fpeval{\j+\i}}-\x{\j});
         \R{\j,\i} = \D{\j};
       };
      \a{\fpeval{\i+1}} = \D{1}; 
   };
  }

\newcommand\zeroToStar[1]{%
  \ifdim#1pt=0pt\relax*\else#1\fi
}

\def\tmp{$i$ & $x_i$ & $y_i$ & ordem 1 & ordem 2\\ \midrule}%
\edef\k{0}%
\loop
\edef\k{\the\numexpr\k+1}%
\xappto\tmp{$\fpeval{\k-1}$ & $\fpeval{round(\x{\k},4)}$ &  $\fpeval{round(\y{\k},4)}$ & $\zeroToStar{\fpeval{round(\R{\k,1},4)}}$ & $\zeroToStar{\fpeval{round(\R{\k,2},4)}}$}%
\gappto\tmp{\\}%
\ifnum\k<\n\repeat

\begin{tikzpicture}
%\clip (-9,-3) rectangle (9,3);
\node at (0,0) {\parbox{20cm}{
\underline{Método de Newton} para calcular o polinômio interpolador sobre os pontos: \\

$\foreach \i in {1,...,\n}{(\ar{\x{\i}},\ar{\y{\i}}),}$ \\

e para tanto, usamos a Tabela das \underline{Diferenças Divididas} resumida a seguir:\\

\begin{tabular}{r|r|r|r|r}
\toprule
\tmp
\bottomrule
\end{tabular}\\[0.5cm]
Os cálculos estão detalhados a seguir.
}};
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容