使用不同的方法对小数字进行严肃的科学计算

使用不同的方法对小数字进行严肃的科学计算

我陷入了 pgfmath 模块的一个问题。当尝试计算以下函数值时,打印出的结果始终为零。我猜问题是 pgfmaths 共域的下溢,数学引擎会截断较小的值。

这是我在介绍中试图解释的一个简单例子:

\documentclass[tikz]{standalone}
\usepackage[fleqn]{amsmath}
% physical constants:
\pgfmathdeclarefunction{m0}{0}{%
    \pgfmathparse{4*pi*1e-7}%
}

\pgfmathdeclarefunction{K}{0}{%
    \pgfmathparse{m0*pi/4}%
}
\pgfmathdeclarefunction{c1}{0}{% c1 = K/1.45
    \pgfmathparse{K/1.45}%
}
\pgfmathdeclarefunction{c2}{1}{% c2(gamma) = c1/gamma^2
    \pgfmathparse{c1/(#1)^2}%
}
\pgfmathdeclarefunction{c3}{2}{% c3(gamma,lambda)
    \pgfmathparse{K/((#1)*((#2)+0.45))}%
}
\pgfmathdeclarefunction{DL_rel}{2}{% DL_rel(lambda, gamma)
    \pgfmathparse{(2*sqrt((580*#1+261)*#2^3)+40*#1+18)/(29*#2^3-20*#1-9)}%
}

% the problematic pgfmath-function
\pgfmathdeclarefunction{N1}{5}{% N1(soluition, Lmin, gamma, lambda, d1)
    \pgfmathsetmacro\numa{(DL_rel(#4,#3)*#2)/2+#2}%
    \pgfmathsetmacro\numb{sqrt(\numa^2-c1*c3(#3,#4))/(4*c2(#3)^2)}%
    \pgfmathsetmacro\denom{2*c1*#5}%
    \pgfmathparse{(#1 -1) ?%
        (sqrt((\numa + \numb)/\denom))%
        :%
        (sqrt((\numa - \numb)/\denom))%
    }%
}

\begin{document}
    This is the equation im trying to solve:
    \begin{equation} \label{eq:N1}
    N_{1_{1,2}} =\sqrt{
        \frac{\frac{\Delta L}{2} +L_{min} \pm \sqrt{\left(\frac{\Delta L}{2} + L_{min}\right)^2 - \frac{c_1 c_3}{4\cdot c_2^2}\cdot \Delta L^2}}
        {2\cdot c_1 d_1}
    }
    \end{equation}
    This should print out its solution:
    \begin{equation*}
    N_{1_{1,2}} =
    \left\{ \begin{array}{l}
    \pgfmathparse{N1(1, 4e-4, 1.2, 0.466, 0.115)}\pgfmathresult\\
    \pgfmathparse{N1(2, 4e-4, 1.2, 0.466, 0.115)}\pgfmathresult
    \end{array}\right.
    \end{equation*}
\end{document}

在此处输入图片描述

我已经花了很长时间才走到今天这一步,如果就此止步,然后在其他应用程序中完成所有的数学运算,我会感到非常沮丧。

也许有人可以解决我的问题。

无论如何,我想用这篇文章来表达我的谢意!到目前为止,这是一个学习 Latex 的好地方,大家干得好!

答案1

更新以重新强调数值不稳定性

在此处输入图片描述

这是来自日志输出的结果,使用gamma=1.2lambda=0.466Lmin=4e-6,它们是原始 OP 中的值。使用 、 可以观察到类似但不同的结果gamma=1.2lambda=0.44

4: -1.00000e-13
5: 0
6: 0
7: 0
8: 0
9: 0
10: 2.00000e-19
11: 1.00000e-20
12: 2.00000e-21
13: 1.00000e-22
14: 1.00000e-23
15: 1.00000e-24
16: 0
17: 0
18: 0
19: 0
20: 1.00000e-29
21: 1.00000e-30
22: 0
23: 0
24: 1.00000e-33
25: 1.00000e-34
26: 0
27: 2.00000e-36
28: 0
29: 0
30: 0
31: -1.00000e-40
32: 2.00000e-41
33: 0
34: 2.00000e-43
35: 0
36: 1.00000e-45
37: 1.00000e-46
38: 0
39: -1.00000e-48
40: 0
41: 0
42: -1.00000e-51
43: 1.00000e-52
44: 1.00000e-53
45: 1.00000e-54
46: 0
47: 1.00000e-56
48: 1.00000e-57
49: 0
50: 1.00000e-59
51: 2.00000e-60
52: 0
53: 0
54: 2.00000e-63
55: 1.00000e-64
56: 2.00000e-65
57: 0
58: 0
59: 1.00000e-68
60: 1.00000e-69
61: 0
62: -1.00000e-71
63: 0
64: -1.00000e-73
65: 0
66: 0
67: 1.00000e-76
68: 0
69: 0
70: -1.00000e-79
71: 1.00000e-80
72: 0
73: 0
74: 0
75: 0
76: 0
77: 0
78: -1.00000e-87
79: 1.00000e-88
80: 1.00000e-89
81: 1.00000e-90
82: 1.00000e-91
83: 1.00000e-92
84: 1.00000e-93
85: 0
86: 0
87: 0
88: 1.00000e-97
89: 1.00000e-98
90: 1.00000e-99
91: 0
92: 1.00000e-101

注意:我已将常量设置K为值1以跳过计算Pi。这会修改此处的结果,因为舍入浮点运算并不完全相同。

生成上述内容的源代码。

\documentclass{article}
\usepackage{xintexpr}% tested with 1.2e release
%\xintverbosetrue

\usepackage[fleqn]{amsmath}

\begin{document}
This is the equation im trying to solve:
\begin{equation} \label{eq:N1}
    N_{1_{1,2}} =\sqrt{
      \frac{\frac{\Delta L}{2} +L_{min} \pm \sqrt{\left(\frac{\Delta L}{2} + L_{min}\right)^2 - \frac{c_1 c_3}{4\cdot c_2^2}\cdot \Delta L^2}}
           {2\cdot c_1 d_1}
    }
\end{equation}

The $\Delta L/L_{min}$, $c_1$, $c_2$, $c_3$ are functions of some parameters
($L_{min}$ is only an overall scaling) and these functions are set-up in such
a way that actually the square root at the numerator of the fraction vanishes
exactly, independently of the values of the parameters. Numerically though it
may be found non zero and this will then induce a catastrophic drop in
precision in the final result. And it may even happen that it will be found
negative, thus potentially raising an error in the complete evaluation.

See the compilation log for this problematic $\left(\frac{\Delta L}{2} +
  L_{min}\right)^2 - \frac{c_1 c_3}{4\cdot c_2^2}\cdot \Delta L^2$ evaluated
with the float precision set to various values. You will see it turns negative
at times.        

% \xintdeffloatvar pi:= 
% 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

\xintFor* #1 in {\xintSeq{4}{92}}\do
{
\xintDigits := #1;

% constants
% \xintdeffloatvar m0:= 4pi*1e-7;
% \xintdeffloatvar K := m0*pi/4;
\xintdeffloatvar K := 1;
\xintdeffloatvar c1:= K/1.45;


% functions
\xintdeffloatfunc c2(u)  := c1/u^2;
\xintdeffloatfunc c3(u,v):= K/(u(v+0.45));

% attention arguments like in OP-update, permuted compared to the OP-original
\xintdeffloatfunc DL_rel(u,v):= (2sqrt((580v+261)u^3)+40v+18)/(29u^3-20v-9);

\xintdeffloatfunc DL(t,u,v)  := DL_rel(u,v)*t;

% Notice that t=Lmin acts only as an overall scaling factor.
\xintdeffloatfunc numbsquared(t,u,v):=
                  subs((Z/2+t)^2-c1*c3(u,v)/(4*c2(u)^2)*Z^2, Z=DL(t,u,v));

\typeout{#1: \xintthefloatexpr [6] numbsquared (4e-6, 1.2, 0.466)\relax }

}

\end{document}

我也用 Maple 测试过。再次设置K1

numbsquared := proc (N)
local m0, K, c1, c2, c3, DL_rel, DL, localnumbsquared;
Digits:=N;
# m0 := 4*Pi*1e-7;
# K  :=  m0*Pi/4;
K  := 1;
c1 := K/1.45;
c2 := u->c1/u^2;
c3 := (u,v)->K/(u*(v+0.45));
DL_rel := (u,v)->(2*sqrt((580*v+261)*u^3)+40*v+18)/(29*u^3-20*v-9);
DL := (t,u,v)->DL_rel(u,v)*t;
localnumbsquared := (t,u,v)->subs(Z=DL(t,u,v),(Z/2+t)^2-c1*c3(u,v)/(4*c2(u)^2)*Z^2);
return localnumbsquared(4e-6, 1.2, 0.466)
end proc:
for N from 4 to 92 do printf("%2d, %e\n", N, numbsquared(N)) end do;

结果属于同一类型,既有巧合,又有差异。

 4, 0.000000e+00
 5, 0.000000e+00
 6, 0.000000e+00
 7, 0.000000e+00
 8, 0.000000e+00
 9, 1.000000e-18
10, 2.000000e-19
11, 1.000000e-20
12, -1.000000e-21
13, 1.000000e-22
14, -1.000000e-23
15, 1.000000e-24
16, 0.000000e+00
17, 0.000000e+00
18, 0.000000e+00
19, 0.000000e+00
20, 1.000000e-29
21, 1.000000e-30
22, 0.000000e+00
23, 1.000000e-32
24, -1.000000e-33
25, 1.000000e-34
26, 0.000000e+00
27, 2.000000e-36
28, 1.000000e-37
29, 0.000000e+00
30, 0.000000e+00
31, -1.000000e-40
32, 2.000000e-41
33, 0.000000e+00
34, 2.000000e-43
35, -1.000000e-44
36, 1.000000e-45
37, 1.000000e-46
38, 0.000000e+00
39, -1.000000e-48
40, 0.000000e+00
41, 0.000000e+00
42, -1.000000e-51
43, 1.000000e-52
44, -1.000000e-53
45, 1.000000e-54
46, 0.000000e+00
47, -1.000000e-56
48, 1.000000e-57
49, 0.000000e+00
50, 1.000000e-59
51, 0.000000e+00
52, 0.000000e+00
53, 0.000000e+00
54, 2.000000e-63
55, 1.000000e-64
56, 0.000000e+00
57, 0.000000e+00
58, 0.000000e+00
59, 1.000000e-68
60, 1.000000e-69
61, 0.000000e+00
62, -1.000000e-71
63, 0.000000e+00
64, -1.000000e-73
65, 0.000000e+00
66, 0.000000e+00
67, 1.000000e-76
68, -2.000000e-77
69, -1.000000e-78
70, -1.000000e-79
71, 1.000000e-80
72, 0.000000e+00
73, 0.000000e+00
74, 0.000000e+00
75, 0.000000e+00
76, 0.000000e+00
77, 0.000000e+00
78, 0.000000e+00
79, 1.000000e-88
80, -1.000000e-89
81, -1.000000e-90
82, 1.000000e-91
83, 0.000000e+00
84, -1.000000e-93
85, 0.000000e+00
86, 0.000000e+00
87, 0.000000e+00
88, -1.000000e-97
89, -2.000000e-98
90, 1.000000e-99
91, -2.000000e-100
92, 1.000000e-101

更新评论一个有趣的数字好奇心/不稳定性

OP 的第一个版本要求N1(1, 4e-6, 1.2, 0.466, 0.115),如下所示。显然,\numb在这种情况下 为零。但根据浮点精度,它可能为零或不为零。\numa仅关于1e-5最终结果的精度可能会因小但不为零而大幅降低\numb

xint与进行了比较maple,得到了类似的结果(仅在最后一位数字上有所不同)16,精度为 、 20、位(使用 maple 需要时间,需要进行更多测试)。对于,我进行了测试,从小数开始,先乘以 以减少到所述。因此,我使用以下代码运行了24Pixint941.0\xintDigits

\xintDigits := 16; % or 20, 24, 28, ... 
\xintdeffloatvar pi:= 1.*3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342;

在 Maple 方面,Pi直到最后都被视为符号evalf

xint以下是浮点精度越来越高的结果:

8.038962683509860
8.0389626847662074875
8.03896268352242165100730
8.038962683509858157707745289
8.0389626835098594140570752438847
8.03896268350985817027123858823346261
8.038962683509858157707745288681429203705
8.0389626835098581577090016380113844070447859
8.03896268350985815770775785217472875573691848736

(这是 的解决方案)。请注意数字\numa+\numb的结果16好多了比数字的还要多20!!!!!!!甚至相当好24数字的要好!(但不如28数字好)。这是因为在16数字处,xint 和 maple 都\numb为零,但3e-1520数字处,这会导致总和出现很大误差,就像\numa在数字处一样1e-5

使用92精度为 的数字,可以发现数字\numb约为3e-51。如果精确值为零,则意味着结果中约 100 个数字之后的数字会被破坏46……

凭借92精确的数字,Maple\numb发现

> evalf(Q(4e-6, 1.2, 0.466, 0.115)); 
0.3162277660168379331998893544432718533719555139325216826857504852792594438\

                          -50
    6392382213442481084 10

xint得到

3.1622776601683793319988935444327185337195551393252168268575048527925944386392382213442481084e-51 您可以看到它一直匹配到最后。;-)

更新我真是个傻瓜!多少年前我才认识到 ???? 的平方根,10注意到上面的 本质上是 的平方根1e-101... 原因似乎很简单,\numb是差的平方根,不知何故,这个差不是零,而是1e-101每个项的最后 92 位数字的舍入误差,可能是1e-10!!! 的数量级!!!是的,这应该可以解释所有N浮点精度级别。我猜有时差是零,有时是1e-(9+N)。例如,N=20可以预期差为1e-29,因此关于 的3e-15平方根,即确切地 观察到了什么。奇怪的是,数字似乎从来没有给出-1e-(9+N)会导致平方根误差增加的差异。

由于返回值会随着精度的提高而减少,因此我或许可以相信确切值为零(我没有做过代数运算)。如果确切值确实是零,则将上述值加或减到某个值上,该值会0.000010117182975...在约 46 位有效数字后损坏它,从而破坏获得它的 92 位浮点评估...

非常令人惊讶!(但请阅读上面引用的段落

与任何其他数学引擎进行比较时都应考虑到这一点:由于减法中可能发生灾难性的取消,该公式在数值上是不稳定的。


原始答案

这是使用另一个数学引擎的方法。它只知道平方根,但这已经足够了。请注意,在给定的示例中,\numb结果是恰好为零。

\documentclass[tikz]{standalone}
\usepackage{xintexpr}% tested with 1.2e release
\usepackage[fleqn]{amsmath}

% constants
\xintdeffloatvar pi:= 3.14159265358979323846;
\xintdeffloatvar m0:= 4pi*1e-7;
\xintdeffloatvar K := m0*pi/4;
\xintdeffloatvar c1:= K/1.45;

% functions
\xintdeffloatfunc c2(x)  := c1/x^2;

\xintdeffloatfunc c3(x,y):= K/(x(y+0.45));

\xintdeffloatfunc DL_rel(u,v):= 
    (2*sqrt((580*u+261)*v^3)+40*u+18)/(29*v^3-20*u-9);

% This is allowed by xint parser also (tacit multiplications):
% \xintdeffloatfunc DL_rel(u,v):= (2sqrt((580u+261)v^3)+40u+18)/(29v^3-20u-9);

% Of course we could simplify here by defining more intermediate functions.
% We could define "numa" and "numb" functions, and set them up as functions
% of an already computed "DL_rel" which serves in both.
% It is possible to use the "subs(expression, x=...)" syntax.
% Limitation is that the dummy parameter must be a single letter.
% Also, the inner-most subs will have the last defined thing, and the
% outer-most subs the first defined thing.
\xintdeffloatfunc N1(a,t,u,v,w):=
    subs(subs(subs(subs(
    if(a=1, sqrt((P+Q)/D), sqrt((P-Q)/D)),
% debugging because something is strange with Q = \numb which is zero
% (P, sqrt(c1*c3(u,v))/c2(u)*X ), 
% well after all it was CORRECT that Q was zero with these numerics
       Q = sqrt(P^2-c1*c3(u,v)/(c2(u)^2)*X^2)% =\numb,
                       ),
       P = X+t % P=\numa, and I think t is Lmin
                  ),
       X = DL_rel(v,u)*t/2 % X= DeltaL/2
              ),    
       D = 2c1*w % D=\denom
         )% must use single letters in subs
    ;%


\begin{document}
This is the equation im trying to solve:
\begin{equation} \label{eq:N1}
    N_{1_{1,2}} =\sqrt{
      \frac{\frac{\Delta L}{2} +L_{min} \pm \sqrt{\left(\frac{\Delta L}{2} + L_{min}\right)^2 - \frac{c_1 c_3}{4\cdot c_2^2}\cdot \Delta L^2}}
           {2\cdot c_1 d_1}
    }
\end{equation}

This should print out its solution:
\begin{equation*}
 N_{1_{1,2}} =
  \left\{ \begin{array}{l}
            \xintthefloatexpr N1(1, 4e-6, 1.2, 0.466, 0.115)\relax\\
            \xintthefloatexpr N1(2, 4e-6, 1.2, 0.466, 0.115)\relax
          \end{array}\right.
\end{equation*}
\end{document}

在这个例子中,两个解决方案是相同的,因为\numb消失......

引用

请注意,解决方案是可扩展计算的。这对某些人(傻瓜……)来说很重要。

如果您想要更精确,请从以下开始:

\xintDigits := 32;
\xintdeffloatvar pi:= 3.141592653589793238462643383279503;

答案2

您可以尝试走这条lualatex路线,然后将所有的数学精度都放在 中lua,所以pgf根本没有。以下可能显示错误的答案,但这并不是由于 的数学能力lua,更可能只是对所需公式的草率翻译:

\documentclass[preview,border=5]{standalone}
\usepackage[fleqn]{amsmath}
\usepackage{luacode}
\begin{luacode*}
pi = math.pi
sqrt = math.sqrt
m0 = 4 * pi * 1e-7
K = m0 * pi / 4
c1 = K / 1.45
c2 = function (x) return c1 / (x^2); end 
c3 = function (x, y) return K / (x * (y + 0.45)); end
DLrel = function (x, y) 
  return (2 * sqrt((580 * x + 261) * y^3) + 40 * x + 18) /
    (29 * y^3 - 20 * x - 9)
end
N1 = function (s, Lmin, g, l, d1) 
  dL = DLrel(l, g) * Lmin
  nm = dL / 2 + Lmin
  rt = (nm^2 - c1 * c3(g, l) * 0.25 * c2(g)^-2 * dL^2)
  dn = 2 * c1 * d1
  s = -(s % 2) * 2 + 1
  return sqrt((nm + s * sqrt(rt)) / dn) 
end
\end{luacode*}
\def\luaprint#1{\directlua{tex.print(#1)}}
\begin{document}
\begin{equation*}
  N_{1_{1,2}} = \left\{ 
\begin{array}{l}
  \luaprint{N1(1, 4e-4, 1.2, 0.44, 0.115)}
\\    
  \luaprint{N1(2, 4e-4, 1.2, 0.44, 0.115)}
\end{array}\right.
\end{equation*}
\end{document}

在此处输入图片描述

答案3

更新 1:pgfmath + fpu

我今天尝试了一下XINT,但由于缺乏数学函数,我又回到了我的pgfmath方法。稍后我需要嵌入更多公式,我需要三角函数等。据我所知,xint 中没有可用的实现。在 Marks 提示使用更\usetikzlibrary{fpu}高精度后,经过一些调整,我终于让它工作了。

这是我迄今为止的代码:

\documentclass[tikz]{standalone}
\usepackage[fleqn]{amsmath}
\usetikzlibrary{fpu}

% physical constants:
\pgfmathdeclarefunction{m0}{0}{%
    \pgfmathparse{4*pi*1e-7}%
}

\pgfmathdeclarefunction{K}{0}{%
    \pgfmathparse{m0*pi/4}%
}
\pgfmathdeclarefunction{c1}{0}{% c1 = K/1.45
    \pgfmathparse{K/1.45}%
}
\pgfmathdeclarefunction{c2}{1}{% c2(gamma) = c1/gamma^2
    \pgfmathparse{c1/(#1)^2}%
}
\pgfmathdeclarefunction{c3}{2}{% c3(gamma,lambda)
    \pgfmathparse{K/((#1)*((#2)+0.45))}%
}
\pgfmathdeclarefunction{DL_rel}{2}{% DL_rel(gamma, lambda)
    \pgfmathparse{(2*sqrt((580*#2+261)*#1^3)+40*#2+18)/(29*#1^3-20*#2-9)}%
}

% the problematic pgfmath-function
%                                   #1         #2    #3     #4      #5
\pgfmathdeclarefunction{N1}{5}{% N1(soluition, Lmin, gamma, lambda, d1)
    \pgfmathsetmacro\DL{(DL_rel(#3,#4)*#2)}%
    \pgfmathsetmacro\numa{\DL/2+#2}%
    \pgfmathsetmacro\numb{sqrt(\numa^2 - c1*c3(#3,#4)/(4*c2(#3)^2)*\DL^2)}%
    \pgfmathsetmacro\denom{2*c1*#5}%
    \pgfmathfloatparse{(#1 == 1) ?%
        (sqrt((\numa + \numb)/\denom))%
        :%
        (sqrt((\numa - \numb)/\denom))%
    }%
}

\begin{document}
    \pgfkeys{/pgf/fpu=true}
    \pgfmathparse{N1(1, 4e-6, 1.2, 0.44, 0.115)}\pgfmathprintnumber[sci, precision=2]{\pgfmathresult}\\
    \pgfkeys{/pgf/fpu=false}
    This is the equation im trying to solve:
    \begin{equation} \label{eq:N1}
    N_{1_{1,2}} =\sqrt{
        \frac{\frac{\Delta L}{2} +L_{min} \pm \sqrt{\left(\frac{\Delta L}{2} + L_{min}\right)^2 - \frac{c_1 c_3}{4\cdot c_2^2}\cdot \Delta L^2}}
        {2\cdot c_1 d_1}
    }
    \end{equation}
    This should print out its solution:
    \pgfkeys{/pgf/fpu=true}
    \begin{equation*}
    N_{1_{1,2}} =
    \left\{ \begin{array}{l}
    \pgfmathparse{N1(1, 4e-6, 1.2, 0.44, 0.115)}\pgfmathprintnumber[fixed, precision=2]{\pgfmathresult}\\
    \pgfmathparse{N1(2, 4e-6, 1.2, 0.44, 0.115)}\pgfmathprintnumber[fixed, precision=2]{\pgfmathresult}
    \end{array}\right.
    \end{equation*}
    \pgfkeys{/pgf/fpu=false}
\end{document}

但我不确定结果的精度。与 jfbus 解决方案和我从 wxMaxima 的输出相比,分子中的平方根现在不再是零,尽管它非常小(e-18)。即使差异很小,我对 pgfmaths 的整体精度也很感兴趣。因为我计划稍后在编译时在 Latex 中完成所有数学运算,然后我看到自己在事后质疑每个计算。

所以,各位,你们觉得在这种应用中使用 pgfmath 是个好主意吗?有人做过类似的事情吗?还是说在数学方面信任文字处理器是个坏主意?

顺便说一句,我现在用它\pgfmathsetmacro来保存稍后需要的结果。有没有更好的解决方案,或者这是常用的方法?


更新 2:LuaTeX + luacode

到目前为止一切顺利。我知道我放弃了我的pgfmath方法,因为它不准确,现在专注于 Marks luaTeX 版本。

此解决方案为我提供了一种将计算结果嵌入公式和文本的好方法。但要使这些东西真正有用,我需要让它在pgfplot环境中工作。以下是示例:

\documentclass[preview,border=5]{standalone}
\usepackage[fleqn]{amsmath}
\usepackage{luacode}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{luacode*}
    -- test
    pi = math.pi
    sqrt = math.sqrt
    m0 = 4 * pi * 1e-7
    K = m0 * pi / 4
    c1 = K / 1.45
    c2 = function (g) return c1 / (g^2); end 
    c3 = function (g, l) return K / (g * (l + 0.45)); end
    DLrel = function (g, l) 
        return  (2 * sqrt((580 * l + 261) * g^3) + 40 * l + 18) /
                (29 * g^3 - 20 * l - 9)
    end
    DLrel_lmd = function (l) return DLrel(sqrt(l^2+1),l); end

    N1 = function (s, Lmin, g, l, d1) 
        dL = DLrel(g, l) * Lmin
        nm = dL / 2 + Lmin
        rt = (nm^2 - c1 * c3(g, l) * 0.25 * c2(g)^-2 * dL^2)
        dn = 2 * c1 * d1
        s = -(s % 2) * 2 + 1
    return sqrt((nm + s * sqrt(rt)) / dn) 
    end

    print = function (d,s) 
        if d == 0 then
         format = "%d"
        else
         format = "%." .. d .. "f"
        end
        tex.sprint(string.format(format,s));
    end

\end{luacode*}
\newcommand{\lp}[2][16]{\directlua{print(#1,#2)}}

\begin{document}
    now i can do calculations in lua and print them out in formulas:
    \begin{equation*}
        N_{1_{1,2}} = \left\{ 
        \begin{array}{l}
            \lp[4]{N1(1, 4e-6, 1.2, 0.446, 0.115)}
            \\    
            \lp[4]{N1(2, 4e-6, 1.2, 0.446, 0.115)}
        \end{array}\right.
    \end{equation*}

    Some serious research done here..\\
    ..oh some new values i came across, i can embed them in my text:\\

\begin{luacode}
    now  = 42.42424242424242
\end{luacode}

    \(  now = \lp[4]{now}\)
    as you can see in this fancy plot:

    \begin{tikzpicture}
        \begin{axis}[
                width=0.5\linewidth,
                height=10cm,
                xmin=0, xmax=180,
                ymin=0, ymax=50,
                xlabel=$time$,
                ylabel=Questions accouring while learning LaTeX,
            ]
            \addplot[
            thick,
            blue,
            domain=0:180,
            samples=200,
            ]{42*sin(x)}; % This value should be taken from lua
            \addplot[
            red,
            only marks
            ] coordinates {
                (90, 42) % This value should be taken from lua
            }
            [yshift=10pt, xshift = 10pt]
            node[pos=0] {$now = 42$}; % This value should be taken from lua
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容